Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-calculate reasonable values for WriteBatchPoolSize and return batches to the pool even if commitlog queue is full #1236

Merged
merged 2 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/dbnode/persist/fs/commitlog/commit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,13 @@ func (l *commitLog) writeWait(
if numEnqueued > l.maxQueueSize {
atomic.AddInt64(&l.numWritesInQueue, int64(-numToEnqueue))
l.closedState.RUnlock()

if write.writeBatch != nil {
// Make sure to finalize the write batch even though we didn't accept the writes
// so it can be returned to the pool.
write.writeBatch.Finalize()
}

return ErrCommitLogQueueFull
}

Expand Down Expand Up @@ -676,6 +683,13 @@ func (l *commitLog) writeBehind(
if numEnqueued > l.maxQueueSize {
atomic.AddInt64(&l.numWritesInQueue, int64(-numToEnqueue))
l.closedState.RUnlock()

if write.writeBatch != nil {
// Make sure to finalize the write batch even though we didn't accept the writes
// so it can be returned to the pool.
write.writeBatch.Finalize()
}

return ErrCommitLogQueueFull
}

Expand Down
20 changes: 19 additions & 1 deletion src/dbnode/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,14 +984,32 @@ func withEncodingAndPoolingOptions(

var writeBatchPoolInitialBatchSize *int
if policy.WriteBatchPool.InitialBatchSize != nil {
// Use config value if available.
writeBatchPoolInitialBatchSize = policy.WriteBatchPool.InitialBatchSize
} else {
// Otherwise use the default batch size that the client will use.
clientDefaultSize := client.DefaultWriteBatchSize
writeBatchPoolInitialBatchSize = &clientDefaultSize
}

var writeBatchPoolMaxBatchSize *int
if policy.WriteBatchPool.MaxBatchSize != nil {
writeBatchPoolMaxBatchSize = policy.WriteBatchPool.MaxBatchSize
}

writeBatchPoolPolicy := policy.WriteBatchPool.Pool
if writeBatchPoolPolicy.Size == 0 {
// If no value set, calculate a reasonabble value based on the commit log
// queue size. We base it off the commitlog queue size because we will
// want to be able to buffer at least one full commitlog queues worth of
// writes without allocating because these objects are very expensive to
// allocate.
commitlogQueueSize := opts.CommitLogOptions().BacklogQueueSize()
expectedBatchSize := *writeBatchPoolInitialBatchSize
writeBatchPoolPolicy.Size = commitlogQueueSize / expectedBatchSize
}
writeBatchPool := ts.NewWriteBatchPool(
poolOptions(policy.WriteBatchPool.Pool, scope.SubScope("write-batch-pool")),
poolOptions(writeBatchPoolPolicy, scope.SubScope("write-batch-pool")),
writeBatchPoolInitialBatchSize,
writeBatchPoolMaxBatchSize)

Expand Down
44 changes: 31 additions & 13 deletions src/dbnode/storage/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,6 @@ func (d *db) Write(
}

series, err := n.Write(ctx, id, timestamp, value, unit, annotation)
if err == commitlog.ErrCommitLogQueueFull {
d.errors.Record(1)
}
if err != nil {
return err
}
Expand All @@ -517,7 +514,15 @@ func (d *db) Write(
}

dp := ts.Datapoint{Timestamp: timestamp, Value: value}
return d.commitLog.Write(ctx, series, dp, unit, annotation)
err = d.commitLog.Write(ctx, series, dp, unit, annotation)
if err == commitlog.ErrCommitLogQueueFull {
d.errors.Record(1)
}
if err != nil {
return err
}

return nil
}

func (d *db) WriteTagged(
Expand All @@ -537,9 +542,6 @@ func (d *db) WriteTagged(
}

series, err := n.WriteTagged(ctx, id, tags, timestamp, value, unit, annotation)
if err == commitlog.ErrCommitLogQueueFull {
d.errors.Record(1)
}
if err != nil {
return err
}
Expand All @@ -549,7 +551,15 @@ func (d *db) WriteTagged(
}

dp := ts.Datapoint{Timestamp: timestamp, Value: value}
return d.commitLog.Write(ctx, series, dp, unit, annotation)
err = d.commitLog.Write(ctx, series, dp, unit, annotation)
if err == commitlog.ErrCommitLogQueueFull {
d.errors.Record(1)
}
if err != nil {
return err
}

return nil
}

func (d *db) BatchWriter(namespace ident.ID, batchSize int) (ts.BatchWriter, error) {
Expand Down Expand Up @@ -634,10 +644,6 @@ func (d *db) writeBatch(
write.Write.Annotation,
)
}

if err == commitlog.ErrCommitLogQueueFull {
d.errors.Record(1)
}
if err != nil {
// Return errors with the original index provided by the caller so they
// can associate the error with the write that caused it.
Expand All @@ -653,10 +659,22 @@ func (d *db) writeBatch(
}

if !n.Options().WritesToCommitLog() {
// Finalize here because we can't rely on the commitlog to do it since we're not
// using it.
writes.Finalize()
return nil
}

return d.commitLog.WriteBatch(ctx, writes)
err = d.commitLog.WriteBatch(ctx, writes)
if err == commitlog.ErrCommitLogQueueFull {
numFailedWrites := int64(len(writes.Iter()))
d.errors.Record(numFailedWrites)
}
if err != nil {
return err
}

return nil
}

func (d *db) QueryIDs(
Expand Down