-
Notifications
You must be signed in to change notification settings - Fork 24
/
keyspan_sink.go
74 lines (62 loc) · 2.39 KB
/
keyspan_sink.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package sink
import (
"context"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/tikv/migration/cdc/cdc/model"
"go.uber.org/zap"
)
type keyspanSink struct {
keyspanID model.KeySpanID
manager *Manager
buffer []*model.RawKVEntry
}
var _ Sink = (*keyspanSink)(nil)
func (t *keyspanSink) EmitChangedEvents(ctx context.Context, rawKVEntries ...*model.RawKVEntry) error {
t.buffer = append(t.buffer, rawKVEntries...)
t.manager.metricsKeySpanSinkTotalEvents.Add(float64(len(rawKVEntries)))
return nil
}
// FlushRowChangedEvents flushes sorted rows to sink manager, note the resolvedTs
// is required to be no more than global resolvedTs, keyspan barrierTs and keyspan
// redo log watermarkTs.
func (t *keyspanSink) FlushChangedEvents(ctx context.Context, keyspanID model.KeySpanID, resolvedTs uint64) (uint64, error) {
if keyspanID != t.keyspanID {
log.Panic("inconsistent keyspan sink",
zap.Uint64("keyspanID", keyspanID), zap.Uint64("sinkKeySpanID", t.keyspanID))
}
resolvedRawKVEntries := t.buffer
t.buffer = []*model.RawKVEntry{}
err := t.manager.bufSink.EmitChangedEvents(ctx, resolvedRawKVEntries...)
if err != nil {
return t.manager.getCheckpointTs(keyspanID), errors.Trace(err)
}
return t.flushResolvedTs(ctx, resolvedTs)
}
func (t *keyspanSink) flushResolvedTs(ctx context.Context, resolvedTs uint64) (uint64, error) {
return t.manager.flushBackendSink(ctx, t.keyspanID, resolvedTs)
}
func (t *keyspanSink) EmitCheckpointTs(ctx context.Context, ts uint64) error {
// the keyspan sink doesn't receive the checkpoint event
return nil
}
// Close once the method is called, no more events can be written to this keyspan sink
func (t *keyspanSink) Close(ctx context.Context) error {
return t.manager.destroyKeySpanSink(ctx, t.keyspanID)
}
// Barrier is not used in keyspan sink
func (t *keyspanSink) Barrier(ctx context.Context, keyspanID model.KeySpanID) error {
return nil
}