Limit rac concurrent reader buffer sizes

This commit is contained in:
Nigel Tao committed 2019-09-01 15:09:56 +10:00
1 parent 396921817d
commit c3f45a7aed
3 files changed
+165 -93

No files matched your search

+161 -92
View File
@@ -15,27 +15,62 @@
package rac package rac
import ( import (
"bytes"
"io" "io"
) )
// rWork is a unit of work for concurrent reading. The Manager sends empty const (
// buffers. Workers receive empty buffers and send filled buffers. numRBuffersPerWorker = 2
rBufferSize = 65536
)
type rBuffer [rBufferSize]byte
// rWork is a unit of work for concurrent reading. The Manager sends dRanges
// for Workers to read. Workers send filled buffers to the concReader.
type rWork struct { type rWork struct {
err error
// dRange is set by the Manager goroutine, for a Worker's incoming work.
// That Worker may slice that dRange into smaller pieces of outgoing work.
// Each outgoing piece has a dRange.Size() of at most rBufferSize.
dRange Range dRange Range
buf *bytes.Buffer
err error // buffer[i:j] holds bytes decompressed from the underlying RAC file but
// not yet served onwards to concReader.Read's caller.
//
// j should equal dRange.Size().
//
// When the buffer is done with (e.g. if i == j, or if we've canceled a
// read-in-progress), the buffer is returned to its owning Worker
// goroutine via recyclec.
//
// These fields are not used by the Manager goroutine.
buffer *rBuffer
i, j uint32
recyclec chan<- *rBuffer
} }
// stopWork is a cancel (non-permanent) or close (permanent) notice to the func (r *rWork) recycle() {
// Manager and Workers. The recipient needs to acknowledge the request by if (r.recyclec != nil) && (r.buffer != nil) {
// receiving from ackc (if non-nil). r.recyclec <- r.buffer
r.recyclec = nil
r.buffer = nil
r.i = 0
r.j = 0
}
}
// stopWork is a cancel (non-permanent, keepWorking = true) or close
// (permanent, keepWorking = false) notice to the Manager and Workers. The
// recipient needs to acknowledge the request by receiving from ackc (if
// non-nil).
type stopWork struct { type stopWork struct {
ackc <-chan struct{} ackc <-chan struct{}
keepWorking bool keepWorking bool
} }
// concReader co-ordinates multiple goroutines serving a Reader. // concReader co-ordinates multiple goroutines (1 Manager, multiple Workers)
// serving a Reader.
type concReader struct { type concReader struct {
// Channels between the concReader, the Manager and multiple Workers. // Channels between the concReader, the Manager and multiple Workers.
// //
@@ -46,9 +81,6 @@ type concReader struct {
reqc chan rWork // Work-Request channel. reqc chan rWork // Work-Request channel.
resc chan rWork // Work-Response channel. resc chan rWork // Work-Response channel.
// bufc holds re-usable buffers.
bufc chan *bytes.Buffer
// stopc and ackc are used to synchronize the concReader, Manager and // stopc and ackc are used to synchronize the concReader, Manager and
// Workers, either canceling work-in-progress or closing everything down. // Workers, either canceling work-in-progress or closing everything down.
// //
@@ -60,12 +92,7 @@ type concReader struct {
// currWork holds the unit of work currently being processed by Read. It // currWork holds the unit of work currently being processed by Read. It
// will be recycled after Read is done with it, or if a Seek causes us to // will be recycled after Read is done with it, or if a Seek causes us to
// cancel the work-in-progress. // cancel the work-in-progress.
// currWork rWork
// currBytes is currWork's buffer's bytes. currBytes[:currIndex] has
// already been sent out via Read. currBytes[currIndex:] has not.
currWork rWork
currBytes []byte
currIndex int
// completedWorks hold completed units of work that are not the next unit // completedWorks hold completed units of work that are not the next unit
// to be sent out via Read. Works may arrive out of order. // to be sent out via Read. Works may arrive out of order.
@@ -112,28 +139,22 @@ func (c *concReader) initialize(racReader *Reader) {
c.posLimit = racReader.chunkReader.decompressedSize c.posLimit = racReader.chunkReader.decompressedSize
c.decompressedSize = racReader.chunkReader.decompressedSize c.decompressedSize = racReader.chunkReader.decompressedSize
// Set up re-usable buffers to hold decoded bytes. The number of buffers is
// arbitrary, but is larger than the number of Workers because individual
// pieces of work (the rWork type) may complete in a different order than
// they need to be written to dst. Having spare buffers means fewer idle
// Workers.
c.bufc = make(chan *bytes.Buffer, 2*c.numWorkers)
for i := 0; i < cap(c.bufc); i++ {
c.bufc <- &bytes.Buffer{}
}
// Set up the Manager and the Workers. // Set up the Manager and the Workers.
c.roic = make(chan Range, 1) c.roic = make(chan Range)
c.reqc = make(chan rWork, c.numWorkers) c.reqc = make(chan rWork, c.numWorkers)
c.resc = make(chan rWork, c.numWorkers) c.resc = make(chan rWork, c.numWorkers*numRBuffersPerWorker)
c.ackc = make(chan struct{})
// Set up the channels used in stopAnyWorkInProgress. It is important that
// these are unbuffered, so that communication is also synchronization.
c.stopc = make(chan stopWork) c.stopc = make(chan stopWork)
c.ackc = make(chan struct{})
for i := 0; i < c.numWorkers; i++ { for i := 0; i < c.numWorkers; i++ {
rr := racReader.clone() rr := racReader.clone()
rr.Concurrency = 0 rr.Concurrency = 0
go runRWorker(c.stopc, c.resc, c.reqc, rr) go runRWorker(c.stopc, c.resc, c.reqc, rr)
} }
go runRManager(c.stopc, c.roic, c.bufc, c.reqc, &racReader.chunkReader) go runRManager(c.stopc, c.roic, c.reqc, &racReader.chunkReader)
} }
func (c *concReader) ready() bool { func (c *concReader) ready() bool {
@@ -211,20 +232,17 @@ func (c *concReader) Read(p []byte) (int, error) {
} }
// Fill p from c.currWork. // Fill p from c.currWork.
if c.currWork.buf != nil { if c.currWork.i < c.currWork.j {
n := copy(p, c.currBytes[c.currIndex:]) n := copy(p, c.currWork.buffer[c.currWork.i:c.currWork.j])
p = p[n:] p = p[n:]
numRead += n numRead += n
c.pos += int64(n) c.pos += int64(n)
c.currIndex += n c.currWork.i += uint32(n)
err := c.currWork.err err := c.currWork.err
// Recycle c.currWork if we're done with it. // Recycle c.currWork if we're done with it.
if c.currIndex == len(c.currBytes) { if c.currWork.i >= c.currWork.j {
c.bufc <- c.currWork.buf c.currWork.recycle()
c.currWork = rWork{}
c.currBytes = nil
c.currIndex = 0
} }
if err != nil { if err != nil {
@@ -238,8 +256,6 @@ func (c *concReader) Read(p []byte) (int, error) {
return numRead, err return numRead, err
} }
c.currWork = work c.currWork = work
c.currBytes = work.buf.Bytes()
c.currIndex = 0
} }
} }
@@ -249,13 +265,6 @@ func (c *concReader) nextWork() (rWork, error) {
delete(c.completedWorks, c.pos) delete(c.completedWorks, c.pos)
return work, nil return work, nil
} }
// This shouldn't happen, but if it does, print a specific error
// message instead of the more general "deadlock" message.
if len(c.completedWorks) == cap(c.bufc) {
return rWork{}, errInternalAllWorkersIdle
}
work := <-c.resc work := <-c.resc
c.completedWorks[work.dRange[0]] = work c.completedWorks[work.dRange[0]] = work
} }
@@ -281,22 +290,23 @@ func (c *concReader) stopAnyWorkInProgress(keepWorking bool) {
} }
func (c *concReader) recycleBuffers() { func (c *concReader) recycleBuffers() {
if c.currWork.buf != nil { c.currWork.recycle()
c.bufc <- c.currWork.buf
c.currWork = rWork{}
c.currBytes = nil
c.currIndex = 0
}
for k, work := range c.completedWorks { for k, work := range c.completedWorks {
c.bufc <- work.buf work.recycle()
delete(c.completedWorks, k) delete(c.completedWorks, k)
} }
// Drain c's buffered channels.
drainWorkChan(c.reqc)
drainWorkChan(c.resc)
}
func drainWorkChan(c chan rWork) {
for { for {
select { select {
case work := <-c.resc: case work := <-c:
c.bufc <- work.buf work.recycle()
default: default:
return return
} }
@@ -305,7 +315,17 @@ func (c *concReader) recycleBuffers() {
func runRWorker(stopc <-chan stopWork, resc chan<- rWork, reqc <-chan rWork, racReader *Reader) { func runRWorker(stopc <-chan stopWork, resc chan<- rWork, reqc <-chan rWork, racReader *Reader) {
input, output := reqc, (chan<- rWork)(nil) input, output := reqc, (chan<- rWork)(nil)
work := rWork{} outWork := rWork{}
// dRange is what part of incoming work remains to be read from the
// racReader.
dRange := Range{}
// Each worker owns up to numRBuffersPerWorker buffers, some of which may
// be temporarily loaned to the concReader goroutine.
buffers := [numRBuffersPerWorker]*rBuffer{}
recyclec := make(chan *rBuffer, numRBuffersPerWorker)
canAlloc := numRBuffersPerWorker
loop: loop:
for { for {
@@ -321,26 +341,93 @@ loop:
} }
continue loop continue loop
case work = <-input: case inWork := <-input:
input, output = nil, resc input = nil
if work.err == nil { if inWork.err == nil {
work.err = racReader.SeekRange(work.dRange[0], work.dRange[1]) dRange = inWork.dRange
if dRange.Empty() {
inWork.err = errInternalEmptyDRange
} else {
inWork.err = racReader.SeekRange(dRange[0], dRange[1])
}
if inWork.err == io.EOF {
inWork.err = io.ErrUnexpectedEOF
}
} }
if work.err == nil { if inWork.err != nil {
_, work.err = io.Copy(work.buf, racReader) output, outWork = resc, inWork
} continue loop
if work.err == io.EOF {
work.err = io.ErrUnexpectedEOF
} }
case output <- work: case output <- outWork:
input, output = reqc, nil output, outWork = nil, rWork{}
work = rWork{}
case recycledBuffer := <-recyclec:
for i := range buffers {
if buffers[i] == nil {
buffers[i], recycledBuffer = recycledBuffer, nil
break
}
}
if recycledBuffer != nil {
panic("unreachable")
}
}
// If there's existing outWork, sending it trumps making new outWork.
if output != nil {
continue loop
}
// If dRange was completely processsed, get new inWork.
if dRange.Empty() {
input = reqc
continue loop
}
// Find a new or recycled buffer.
buffer := (*rBuffer)(nil)
{
b := -1
if buffers[0] != nil {
b = 0
} else if buffers[1] != nil {
b = 1
}
if b >= 0 {
buffer, buffers[b] = buffers[b], nil
} else if canAlloc == 0 {
// Wait until we receive a recycled buffer.
continue loop
} else {
canAlloc--
buffer = &rBuffer{}
}
}
// Make a new outWork, shrinking dRange to be whatever's left over.
{
n, err := racReader.Read(buffer[:])
if err == io.EOF {
err = nil
}
oldDPos := dRange[0]
newDPos := dRange[0] + int64(n)
dRange[0] = newDPos
output, outWork = resc, rWork{
err: err,
dRange: Range{oldDPos, newDPos},
buffer: buffer,
i: 0,
j: uint32(n),
recyclec: recyclec,
}
} }
} }
} }
func runRManager(stopc <-chan stopWork, roic <-chan Range, bufc <-chan *bytes.Buffer, reqc chan<- rWork, chunkReader *ChunkReader) { func runRManager(stopc <-chan stopWork, roic <-chan Range, reqc chan<- rWork, chunkReader *ChunkReader) {
input, output := roic, (chan<- rWork)(nil) input, output := roic, (chan<- rWork)(nil)
roi := Range{} roi := Range{}
work := rWork{} work := rWork{}
@@ -392,26 +479,8 @@ loop:
input, output = roic, nil input, output = roic, nil
continue loop continue loop
} }
dr := chunk.DRange.Intersect(roi) if dr := chunk.DRange.Intersect(roi); !dr.Empty() {
if dr.Empty() { work = rWork{dRange: dr}
continue
}
select {
case stop := <-stopc:
if stop.ackc != nil {
<-stop.ackc
} else {
// No need to ack. This is CloseWithoutWaiting.
}
if !stop.keepWorking {
return
}
continue loop
case buf := <-bufc:
buf.Reset()
work = rWork{dRange: dr, buf: buf}
continue loop continue loop
} }
} }
+1 -1
View File
@@ -89,8 +89,8 @@ var (
errTooMuchInput = errors.New("rac: too much input") errTooMuchInput = errors.New("rac: too much input")
errUnsupportedRACFileVersion = errors.New("rac: unsupported RAC file version") errUnsupportedRACFileVersion = errors.New("rac: unsupported RAC file version")
errInternalAllWorkersIdle = errors.New("rac: internal error: all workers idle")
errInternalArityIsTooLarge = errors.New("rac: internal error: arity is too large") errInternalArityIsTooLarge = errors.New("rac: internal error: arity is too large")
errInternalEmptyDRange = errors.New("rac: internal error: empty DRange")
errInternalInconsistentArity = errors.New("rac: internal error: inconsistent arity") errInternalInconsistentArity = errors.New("rac: internal error: inconsistent arity")
errInternalInconsistentPosition = errors.New("rac: internal error: inconsistent position") errInternalInconsistentPosition = errors.New("rac: internal error: inconsistent position")
errInternalShortCSize = errors.New("rac: internal error: short CSize") errInternalShortCSize = errors.New("rac: internal error: short CSize")
+3
View File
@@ -514,6 +514,9 @@ func (r *Reader) seek(offset int64, whence int, limit int64) (int64, error) {
// which may take some time. If the caller does not care about waiting until it // which may take some time. If the caller does not care about waiting until it
// is safe to close or otherwise release the r.ReadSeeker's resources, call // is safe to close or otherwise release the r.ReadSeeker's resources, call
// CloseWithoutWaiting instead. // CloseWithoutWaiting instead.
//
// It is not safe to call Close from a separate goroutine while another method
// call like Read or Seek is in progress.
func (r *Reader) Close() error { func (r *Reader) Close() error {
if err := r.initialize(); err != nil { if err := r.initialize(); err != nil {
r.concReader.Close() r.concReader.Close()