MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / _reserveSequenceBatch

Method _reserveSequenceBatch

db/sequence_allocator.go:344–380  ·  view source on GitHub ↗

Reserve a new sequence range, based on batch size. Called by nextSequence when the previously allocated sequences have all been used.

(ctx context.Context)

Source from the content-addressed store, hash-verified

342
343// Reserve a new sequence range, based on batch size. Called by nextSequence when the previously allocated sequences have all been used.
344func (s *sequenceAllocator) _reserveSequenceBatch(ctx context.Context) error {
345
346 // If the time elapsed since the last reserveSequenceRange invocation reserve is shorter than our target frequency,
347 // this indicates we're making an incr call more frequently than we want to. Triggers an increase in batch size to
348 // reduce incr frequency.
349 if time.Since(s.lastSequenceReserveTime) < MaxSequenceIncrFrequency {
350 s.sequenceBatchSize = s.sequenceBatchSize * sequenceBatchMultiplier
351 if s.sequenceBatchSize > maxBatchSize {
352 s.sequenceBatchSize = maxBatchSize
353 }
354 base.DebugfCtx(ctx, base.KeyCRUD, "Increased sequence batch to %d", s.sequenceBatchSize)
355 }
356
357 max, err := s._incrementSequence(s.sequenceBatchSize)
358 if err != nil {
359 base.WarnfCtx(ctx, "Error from incrementSequence in _reserveSequences(%d): %v", s.sequenceBatchSize, err)
360 return err
361 }
362
363 // check for rollback of _sync:seq document
364 minimumExpectedValue := s.max + s.sequenceBatchSize
365 if max < minimumExpectedValue {
366 // rollback of _sync:seq detected
367 max, err = s._fixSyncSeqRollback(ctx, max, minimumExpectedValue)
368 if err != nil {
369 return err
370 }
371 }
372
373 // Update max and last used sequences. Last is updated here to account for sequences allocated/used by other
374 // Sync Gateway nodes
375 s.max = max
376 s.last = max - s.sequenceBatchSize
377 s.lastSequenceReserveTime = time.Now()
378
379 return nil
380}
381
382// Gets the _sync:seq document value. Retry handling provided by bucket.Get.
383func (s *sequenceAllocator) getSequence() (max uint64, err error) {

Callers 1

_nextSequenceMethod · 0.95

Calls 5

_incrementSequenceMethod · 0.95
_fixSyncSeqRollbackMethod · 0.95
DebugfCtxFunction · 0.92
WarnfCtxFunction · 0.92
SinceMethod · 0.45

Tested by

no test coverage detected