Skip to content

Commit

Permalink
kvclient/rangefeed: emit checkpoint events
Browse files Browse the repository at this point in the history
Grafted from cockroachdb#69269. This seems like a useful primitive for users of
this library. We intend to use it in cockroachdb#69661 and cockroachdb#69614.

Release note: None

Co-authored-by: irfan sharif <[email protected]>
  • Loading branch information
ajwerner and irfansharif committed Sep 16, 2021
1 parent 1f98510 commit 72e9869
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/kv/kvclient/rangefeed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package rangefeed
import (
"context"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/retry"
)

Expand All @@ -27,6 +28,7 @@ type config struct {
withInitialScan bool
withDiff bool
onInitialScanError OnInitialScanError
onCheckpoint OnCheckpoint
}

type optionFunc func(*config)
Expand Down Expand Up @@ -80,6 +82,17 @@ func WithRetry(options retry.Options) Option {
})
}

// OnCheckpoint is called when a rangefeed checkpoint occurs.
type OnCheckpoint func(ctx context.Context, checkpoint *roachpb.RangeFeedCheckpoint)

// WithOnCheckpoint sets up a callback that's invoked whenever a check point
// event is emitted.
func WithOnCheckpoint(f OnCheckpoint) Option {
return optionFunc(func(c *config) {
c.onCheckpoint = f
})
}

func initConfig(c *config, options []Option) {
*c = config{} // the default config is its zero value
for _, o := range options {
Expand Down
3 changes: 3 additions & 0 deletions pkg/kv/kvclient/rangefeed/rangefeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ func (f *RangeFeed) processEvents(
if _, err := frontier.Forward(ev.Checkpoint.Span, ev.Checkpoint.ResolvedTS); err != nil {
return err
}
if f.onCheckpoint != nil {
f.onCheckpoint(ctx, ev.Checkpoint)
}
case ev.Error != nil:
// Intentionally do nothing, we'll get an error returned from the
// call to RangeFeed.
Expand Down

0 comments on commit 72e9869

Please sign in to comment.