-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdc.go
More file actions
296 lines (267 loc) · 9.73 KB
/
Copy pathcdc.go
File metadata and controls
296 lines (267 loc) · 9.73 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package app
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/nixrajput/siphon/internal/audit"
"github.com/nixrajput/siphon/internal/canonical"
"github.com/nixrajput/siphon/internal/driver"
"github.com/nixrajput/siphon/internal/errs"
"github.com/nixrajput/siphon/internal/jobs"
)
// CDCState is persisted between runs so a long-running continuous sync can
// resume from the last applied position after a restart.
type CDCState struct {
JobID string `json:"job_id"`
Source string `json:"source_profile"`
Target string `json:"target_profile"`
LastAppliedLSN string `json:"last_applied_lsn,omitempty"`
LastBinlogFile string `json:"last_binlog_file,omitempty"`
LastBinlogPos uint64 `json:"last_binlog_pos,omitempty"`
UpdatedAt time.Time `json:"updated_at"`
}
// position renders the resume cursor stored in this state as a canonical.Position.
func (s *CDCState) position() canonical.Position {
return canonical.Position{
LSN: s.LastAppliedLSN,
BinlogFile: s.LastBinlogFile,
BinlogPos: s.LastBinlogPos,
}
}
// setPosition records p as the new resume cursor.
func (s *CDCState) setPosition(p canonical.Position) {
s.LastAppliedLSN = p.LSN
s.LastBinlogFile = p.BinlogFile
s.LastBinlogPos = p.BinlogPos
}
// hasPosition reports whether any resume cursor has been recorded.
func (s *CDCState) hasPosition() bool {
return s.LastAppliedLSN != "" || s.LastBinlogFile != ""
}
// CDCStateDir returns the per-user directory holding CDC resume state. It
// honors SIPHON_STATE_HOME, then XDG_STATE_HOME, then $HOME/.local/state —
// mirroring how internal/config resolves its config path, so tests can redirect
// it without writing to the real home.
func CDCStateDir() string {
if v := os.Getenv("SIPHON_STATE_HOME"); v != "" {
return filepath.Join(v, "cdc")
}
if v := os.Getenv("XDG_STATE_HOME"); v != "" {
return filepath.Join(v, "siphon", "cdc")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".local", "state", "siphon", "cdc")
}
func saveCDCState(s *CDCState) error {
if err := os.MkdirAll(CDCStateDir(), 0o700); err != nil {
return err
}
s.UpdatedAt = time.Now().UTC()
body, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
return os.WriteFile(filepath.Join(CDCStateDir(), s.JobID+".state"), body, 0o600)
}
func loadCDCState(jobID string) (*CDCState, error) {
body, err := os.ReadFile(filepath.Join(CDCStateDir(), jobID+".state"))
if err != nil {
return nil, err
}
s := &CDCState{}
return s, json.Unmarshal(body, s)
}
// cdcJobID derives a stable per-(source,target) identifier so a restart of the
// same continuous sync resumes from the same state file. A short hash keeps the
// filename safe regardless of profile-name characters.
func cdcJobID(from, to string) string {
sum := sha256.Sum256([]byte(from + "\x00" + to))
return "cdc-" + hex.EncodeToString(sum[:8])
}
// RunCDC starts (or resumes) a continuous, unbounded sync: it tails the source's
// logical change stream and applies each engine-neutral CanonicalChange to the
// target. It works same-engine and cross-engine alike because CanonicalChange is
// engine-neutral and ApplyChange replays it natively on the target.
//
// Both drivers must advertise CapCDC. On a first run (no prior state) it captures
// a consistent start position, takes an initial schema+data snapshot, then streams
// changes committed after that position. On a restart it resumes from the saved
// position with no snapshot.
//
// Resume granularity is "since the last clean exit": RunCDC persists the
// streamer's returned final position when the stream stops (the position is tied
// to what was actually delivered, never ahead of it). Re-applying the tail after
// a crash is safe because ApplyChange is idempotent (INSERT upserts; UPDATE and
// DELETE target by primary key).
//
// ctx cancellation is the normal stop signal (matching the bounded-stream
// convention): on cancel RunCDC persists final state and returns nil; only a
// non-cancel StreamChanges error is surfaced.
func RunCDC(parent context.Context, d Deps, opt SyncOpts) (<-chan jobs.Event, string, error) {
src, err := d.Profiles.Resolve(opt.From)
if err != nil {
return nil, "", err
}
dst, err := d.Profiles.Resolve(opt.To)
if err != nil {
return nil, "", err
}
if err := RequireCapability(d, opt.From, CapCDC); err != nil {
return nil, "", err
}
if err := RequireCapability(d, opt.To, CapCDC); err != nil {
return nil, "", err
}
srcDrv, err := d.Drivers.Get(src.Driver)
if err != nil {
return nil, "", err
}
dstDrv, err := d.Drivers.Get(dst.Driver)
if err != nil {
return nil, "", err
}
// CDC is destructive (it continuously applies changes to the target), so it
// runs through the same gate/audit seam as the other verbs.
done, err := guardedOp(parent, d, audit.OpSync, opt.From, opt.To)
if err != nil {
return nil, "", err
}
jobID := cdcJobID(opt.From, opt.To)
return launchGuarded(d.Runner, parent, done, jobs.Job{
Stage: "cdc",
Func: func(ctx context.Context, emit func(jobs.Event)) (retErr error) {
defer func() { done(retErr) }()
emit(jobs.Event{Phase: jobs.PhaseProgress, Message: "CDC mode started"})
srcConn, err := srcDrv.Connect(ctx, src)
if err != nil {
return err
}
defer func() { _ = srcConn.Close() }()
dstConn, err := dstDrv.Connect(ctx, dst)
if err != nil {
return err
}
defer func() { _ = dstConn.Close() }()
srcStreamer, ok := srcConn.(driver.ChangeStreamer)
if !ok {
return cdcUnsupported(src.Driver, "ChangeStreamer")
}
srcPositioner, ok := srcConn.(driver.BasePositioner)
if !ok {
return cdcUnsupported(src.Driver, "BasePositioner")
}
dstXfer, ok := dstConn.(driver.CanonicalTransfer)
if !ok {
return cdcUnsupported(dst.Driver, "CanonicalTransfer")
}
// Resume from prior state when present; otherwise capture a consistent
// start position and take an initial snapshot before streaming.
state := &CDCState{JobID: jobID, Source: opt.From, Target: opt.To}
var from canonical.Position
if prev, loadErr := loadCDCState(jobID); loadErr == nil && prev.hasPosition() {
state = prev
from = prev.position()
emit(jobs.Event{Phase: jobs.PhaseProgress, Message: "CDC resuming from saved position"})
} else {
from, err = srcPositioner.CurrentPosition(ctx)
if err != nil {
return err
}
if snapErr := cdcSnapshot(ctx, srcConn, dstXfer, src.Driver, opt.Tables); snapErr != nil {
return snapErr
}
state.setPosition(from)
if saveErr := saveCDCState(state); saveErr != nil {
return saveErr
}
emit(jobs.Event{Phase: jobs.PhaseProgress, Message: "CDC initial snapshot complete; streaming changes"})
}
applied := 0
emit2 := func(ch canonical.CanonicalChange) error {
// Honor --table: the streamer surfaces changes for every table (the
// source's publication/binlog is database-wide), so skip any change
// outside the requested set before applying it to the target.
if !tableAllowed(ch.Table, opt.Tables) {
return nil
}
if applyErr := dstXfer.ApplyChange(ctx, ch); applyErr != nil {
return applyErr
}
applied++
emit(jobs.Event{
Phase: jobs.PhaseProgress,
Message: fmt.Sprintf("applied %s on %s (%d total)", ch.Op, ch.Table, applied),
})
// No periodic mid-stream checkpoint here: srcPositioner.CurrentPosition
// returns the source's CURRENT WAL/binlog end, which is ahead of the
// change we just applied. Persisting it would, after a crash, resume
// PAST changes that streamed but were never applied — silent data loss.
// We checkpoint only the streamer's returned final position on exit
// (below), which is tied to what was actually delivered; at-least-once
// redelivery on resume is safe because ApplyChange is idempotent.
return nil
}
finalPos, streamErr := srcStreamer.StreamChanges(ctx, from, emit2)
// On any exit, persist the furthest position we know about. The
// streamer returns the final position even on ctx-cancel.
if finalPos.LSN != "" || finalPos.BinlogFile != "" {
state.setPosition(finalPos)
}
_ = saveCDCState(state)
// ctx cancellation is the normal stop signal — report clean.
if ctx.Err() != nil {
emit(jobs.Event{Phase: jobs.PhaseProgress, Message: "CDC stopped"})
return nil
}
return streamErr
},
})
}
// cdcSnapshot performs the initial schema+data copy from source to target using
// the canonical transfer pipe (the same pattern as runCrossEngineSync). Both the
// source's SchemaInspector+CanonicalTransfer and the target's CanonicalTransfer
// are required.
func cdcSnapshot(ctx context.Context, srcConn driver.Conn, dstXfer driver.CanonicalTransfer, srcDriverName string, tables []string) error {
srcInsp, ok := srcConn.(driver.SchemaInspector)
if !ok {
return cdcUnsupported(srcDriverName, "SchemaInspector")
}
srcXfer, ok := srcConn.(driver.CanonicalTransfer)
if !ok {
return cdcUnsupported(srcDriverName, "CanonicalTransfer")
}
schema, err := srcInsp.InspectSchema(ctx)
if err != nil {
return err
}
// Snapshot only the requested tables; the streamed-change phase applies the
// same filter in emit2.
schema = filterSchemaTables(schema, tables)
stream := jobs.NewStream(64)
errCh := make(chan error, 1)
go func() {
emitErr := srcXfer.EmitCanonical(ctx, schema, stream)
_ = stream.CloseErr(emitErr)
errCh <- emitErr
}()
consumeErr := dstXfer.ConsumeCanonical(ctx, stream)
_ = stream.Close()
emitErr := <-errCh
if emitErr != nil {
return emitErr
}
return consumeErr
}
func cdcUnsupported(driverName, iface string) error {
return &errs.Error{
Op: "cdc",
Code: errs.CodeUser,
Cause: errs.ErrDriverUnsupported,
Hint: driverName + " driver does not implement " + iface,
}
}