-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitter.go
More file actions
534 lines (482 loc) · 16.1 KB
/
Copy pathsplitter.go
File metadata and controls
534 lines (482 loc) · 16.1 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package monstera
import (
"context"
"errors"
"fmt"
"log"
"sort"
"time"
"github.com/evrblk/monstera/cluster"
"github.com/evrblk/monstera/internal/raft"
"github.com/evrblk/monstera/internal/replication"
"github.com/evrblk/monstera/internal/replication/replicationpb"
)
// splitter is the node-local shard-split seeding pipeline for ONE parent
// replica (see notes/shard-split-design.md, Phase 2). It runs while the
// node's applied config says the parent shard is SPLITTING and this node
// hosts a parent replica, and it fills the durable state of the co-located
// DORMANT children:
//
// - CoreTypeInMemory: copies the parent's snapshot as each child's base
// (content verbatim, metadata rewritten) and tails the parent's applied
// log, copying each committed entry into the log of the child that owns
// its stamped shard key (a NOOP filler goes to the siblings, keeping
// child index == parent index).
// - CoreTypePersistedExclusive: restores the parent's snapshot into a live
// child core (bounds-filtered by the core's portable Restore) and tails
// the parent's applied log, applying each owned entry to the child core
// through plain Update; progress is tracked durably as catchUpIndex.
// - CoreTypePersistedShared: no splitter runs at all — the children's rows
// are the parent's live rows.
//
// Everything the splitter writes is durable and resumable; it is stopped and
// restarted wholesale on every config apply and simply continues from its
// recorded progress. It is self-healing: whenever it cannot continue (parent
// log compacted past the seed, an unstamped update entry from a leader that
// predates the splitting config), it restarts the affected child's seed from
// a fresh parent snapshot at a later base index.
type splitter struct {
parent *replica
coreType CoreType
children []*splitChild
factory func(*cluster.Shard, *cluster.Replica) ApplicationCore
// promote is the node callback that promotes the seeded dormant children
// into serving replicas; called once, after cutoff finalization.
promote func() error
codec replication.CommandCodec
logger *log.Logger
cancel context.CancelFunc
done chan struct{}
}
// splitChild is one co-located dormant child the splitter seeds.
type splitChild struct {
shard *cluster.Shard // bounds (immutable during a split)
replicaSet []raft.RaftServer
dormant *dormantReplica
// core is the live child core for CoreTypePersistedExclusive (owned by
// the splitter, closed on stop); nil for CoreTypeInMemory.
core ApplicationCore
// seeded is the in-memory copy of the durable seed progress: the last
// parent log index reflected in this child's seed.
seeded uint64
// hasBase reports whether the child has a base (a seeded base snapshot
// for CoreTypeInMemory; a restored core with recorded catchUpIndex for
// CoreTypePersistedExclusive).
hasBase bool
}
// splitterPollInterval is how often the splitter re-checks the parent's
// applied index when it is fully caught up.
const splitterPollInterval = 50 * time.Millisecond
// splitterBatchSize bounds how many parent entries are processed per child
// batch (one AppendEntries call / one catchUpIndex write).
const splitterBatchSize = 256
func newSplitter(parent *replica, coreType CoreType, children []*splitChild,
factory func(*cluster.Shard, *cluster.Replica) ApplicationCore, promote func() error, logger *log.Logger) *splitter {
// Sort children by lower bound so routing can mirror FindShardByShardKey.
sort.Slice(children, func(i, j int) bool {
return children[i].shard.LowerBound < children[j].shard.LowerBound
})
return &splitter{
parent: parent,
coreType: coreType,
children: children,
factory: factory,
promote: promote,
codec: &replication.ProtoCommandCodec{},
logger: logger,
}
}
// start launches the splitter goroutine.
func (s *splitter) start() {
ctx, cancel := context.WithCancel(context.Background())
s.cancel = cancel
s.done = make(chan struct{})
go func() {
defer close(s.done)
s.run(ctx)
}()
}
// stop terminates the splitter and waits for it to exit. Idempotent.
func (s *splitter) stop() {
if s.cancel == nil {
return
}
s.cancel()
<-s.done
s.cancel = nil
for _, ch := range s.children {
if ch.core != nil {
ch.core.Close()
ch.core = nil
}
}
}
func (s *splitter) run(ctx context.Context) {
ticker := time.NewTicker(splitterPollInterval)
defer ticker.Stop()
for {
if err := s.step(ctx); err != nil {
if ctx.Err() != nil {
return
}
s.logger.Printf("split seeding of shard %s: %v (will retry)", s.parent.shardId, err)
} else if m := s.parent.frozenAt(); m > 0 {
// The CUTOFF applied at m: drain the seed to exactly m, finalize
// the children's Raft state, and promote them in place.
done, err := s.finalize(m)
if err != nil {
s.logger.Printf("split finalization of shard %s: %v (will retry)", s.parent.shardId, err)
} else if done {
s.logger.Printf("Split of shard %s finalized at cutoff index %d; children promoted", s.parent.shardId, m)
return
}
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
// finalize completes the split once the parent froze at m: it verifies every
// child's seed reaches exactly m, writes the persisted children's base
// snapshots (metadata-only at m, empty log — they replay nothing), primes the
// children's stable stores, and promotes them into serving replicas. Returns
// false (retry later) while children are still draining to m.
func (s *splitter) finalize(m uint64) (bool, error) {
if s.coreType != CoreTypePersistedShared {
for _, ch := range s.children {
if ch.seeded < m {
return false, nil // still draining; step() keeps copying
}
}
}
for _, ch := range s.children {
switch s.coreType {
case CoreTypePersistedShared, CoreTypePersistedExclusive:
// Persisted children start with lastApplied = m and replay
// nothing: metadata-only base snapshot at m, empty log.
base, err := ch.dormant.seeder.LatestBaseIndex()
if err != nil {
return false, err
}
if base < m {
if err := ch.dormant.seeder.SeedBaseSnapshot(m, ch.replicaSet, nil); err != nil {
return false, err
}
}
}
if err := ch.dormant.seeder.Finalize(); err != nil {
return false, err
}
if ch.core != nil {
// The splitter-owned seeding core hands over to the replica's own.
ch.core.Close()
ch.core = nil
}
}
if err := s.promote(); err != nil {
return false, err
}
return true, nil
}
// step makes seeding progress: establishes missing bases and copies/applies
// the parent log tail up to the parent's current applied index. It returns
// early on any error; the run loop retries.
func (s *splitter) step(ctx context.Context) error {
// Fully shared stores need no seeding at all: the children's rows are the
// parent's live rows. The splitter exists only to finalize at the cutoff.
if s.coreType == CoreTypePersistedShared {
return nil
}
// Recover durable progress on the first pass (and after base restarts).
for _, ch := range s.children {
if ch.hasBase {
continue
}
if err := s.recoverOrSeedBase(ch); err != nil {
return fmt.Errorf("base seed of child %s: %w", ch.shard.Id, err)
}
}
applied := s.parent.GetRaftStats().AppliedIndex
// Once the parent froze at m, the seed ends at exactly m: entries after it
// are deterministic rejections that mutated nothing, and the children's
// own Raft groups take over from m+1.
if m := s.parent.frozenAt(); m > 0 && m < applied {
applied = m
}
for {
if ctx.Err() != nil {
return ctx.Err()
}
lowest := s.lowestSeeded()
if lowest >= applied {
return nil // fully caught up
}
to := min(lowest+splitterBatchSize, applied)
if err := s.copyRange(lowest+1, to); err != nil {
return err
}
}
}
// lowestSeeded returns the least seed progress across children — the next
// copyRange starts right after it. Children ahead of it skip already-seeded
// entries idempotently.
func (s *splitter) lowestSeeded() uint64 {
lowest := s.children[0].seeded
for _, ch := range s.children[1:] {
if ch.seeded < lowest {
lowest = ch.seeded
}
}
return lowest
}
// recoverOrSeedBase brings a child to the "has a base" state: it recovers
// durable progress recorded by a previous splitter run, or seeds a fresh base
// from the parent's snapshot.
func (s *splitter) recoverOrSeedBase(ch *splitChild) error {
switch s.coreType {
case CoreTypeInMemory:
base, err := ch.dormant.seeder.LatestBaseIndex()
if err != nil {
return err
}
if base == 0 {
base, err = s.seedBaseSnapshot(ch)
if err != nil {
return err
}
}
last, err := ch.dormant.seeder.LastSeededIndex()
if err != nil {
return err
}
ch.seeded = max(base, last)
ch.hasBase = true
return nil
case CoreTypePersistedExclusive:
if ch.core == nil {
// The child core is constructed once per splitter lifetime; the
// factory receives the child shard so the core knows its bounds.
ch.core = s.factory(ch.shard, replicaOf(ch.shard, ch.dormant.replicaId))
}
idx, err := ch.dormant.seeder.CatchUpIndex()
if err != nil {
return err
}
if idx == 0 {
meta, rc, err := s.parent.TakeAndOpenSnapshot()
if err != nil {
return err
}
// The core's portable Restore keeps only the child's half.
if err := ch.core.Restore(rc); err != nil {
return fmt.Errorf("restoring parent snapshot into child core: %w", err)
}
if err := ch.dormant.seeder.SetCatchUpIndex(meta.Index); err != nil {
return err
}
idx = meta.Index
}
ch.seeded = idx
ch.hasBase = true
return nil
default:
return fmt.Errorf("unexpected core type %v in splitter", s.coreType)
}
}
// seedBaseSnapshot copies the parent's snapshot into the child's snapshot
// store as its base (content verbatim, metadata rewritten to the child's
// identity and membership) and returns the base index.
func (s *splitter) seedBaseSnapshot(ch *splitChild) (uint64, error) {
meta, rc, err := s.parent.TakeAndOpenSnapshot()
if err != nil {
return 0, err
}
defer rc.Close()
if err := ch.dormant.seeder.SeedBaseSnapshot(meta.Index, ch.replicaSet, rc); err != nil {
return 0, err
}
s.logger.Printf("Seeded base snapshot for child %s at parent index %d", ch.shard.Id, meta.Index)
return meta.Index, nil
}
// restartChild discards a child's seed progress and forces a fresh base at a
// later parent index (the self-healing path).
func (s *splitter) restartChild(ch *splitChild, reason string) error {
s.logger.Printf("Restarting seed of child %s from a fresh base: %s", ch.shard.Id, reason)
switch s.coreType {
case CoreTypeInMemory:
if err := ch.dormant.seeder.ResetLog(); err != nil {
return err
}
base, err := s.seedBaseSnapshot(ch)
if err != nil {
return err
}
ch.seeded = base
return nil
case CoreTypePersistedExclusive:
meta, rc, err := s.parent.TakeAndOpenSnapshot()
if err != nil {
return err
}
// Restore replaces the core state wholesale.
if err := ch.core.Restore(rc); err != nil {
return err
}
if err := ch.dormant.seeder.SetCatchUpIndex(meta.Index); err != nil {
return err
}
ch.seeded = meta.Index
return nil
default:
return fmt.Errorf("unexpected core type %v in splitter", s.coreType)
}
}
// copyRange processes parent log entries (from, to] into every child that has
// not seeded them yet.
func (s *splitter) copyRange(from, to uint64) error {
type routed struct {
index uint64
// owner is the child that receives the full entry; nil means every
// child receives it in full (unsharded updates, non-update entries
// copied as NOOPs have data == nil instead).
owner *splitChild
data []byte // full MonsteraCommand bytes; nil => NOOP filler for everyone
}
entries := make([]routed, 0, to-from+1)
for i := from; i <= to; i++ {
e, err := s.parent.GetLogEntry(i)
if err != nil {
if errors.Is(err, raft.ErrLogEntryNotFound) {
// Compacted past the seed: restart lagging children from a
// fresh base.
return s.restartLagging(i, "parent log compacted past the seed")
}
return err
}
if !e.IsCommand {
// Raft-internal entry (membership, barrier): NOOP filler.
entries = append(entries, routed{index: i})
continue
}
cmd, err := s.codec.Decode(e.Data)
if err != nil {
return fmt.Errorf("decoding parent log entry %d: %w", i, err)
}
switch cmd.Type {
case replicationpb.CommandType_COMMAND_TYPE_UPDATE:
switch cmd.Routing {
case replicationpb.CommandRouting_COMMAND_ROUTING_UNSTAMPED:
// Proposed by a leader that had not applied the splitting
// config yet: unroutable. Restart from a base past it.
return s.restartLagging(i, "unstamped update entry in the seed tail")
case replicationpb.CommandRouting_COMMAND_ROUTING_SHARD_WIDE:
// Shard-wide update: every child receives it in full.
entries = append(entries, routed{index: i, data: e.Data})
continue
case replicationpb.CommandRouting_COMMAND_ROUTING_SHARDED:
owner := s.childOwning(cluster.ShardKey(cmd.ShardKey))
if owner == nil {
// The children partition the parent's range and the key was
// routed to the parent: this cannot happen on a valid config.
panic(fmt.Sprintf("split seeding of shard %s: no child owns shard key %d", s.parent.shardId, cmd.ShardKey))
}
entries = append(entries, routed{index: i, owner: owner, data: e.Data})
default:
panic(fmt.Sprintf("split seeding of shard %s: unknown command routing %v at index %d", s.parent.shardId, cmd.Routing, i))
}
default:
// Framework commands (NOOP, CUTOFF) carry no application state
// during seeding; children get a NOOP at this index. (The cutoff
// itself is handled by cutoff finalization, not the tailer.)
entries = append(entries, routed{index: i})
}
}
noop, err := s.codec.Encode(&replicationpb.MonsteraCommand{Type: replicationpb.CommandType_COMMAND_TYPE_NOOP})
if err != nil {
return err
}
for _, ch := range s.children {
batch := make([]raft.SeedEntry, 0, len(entries))
for _, e := range entries {
if e.index <= ch.seeded {
continue // already seeded by a previous run
}
switch s.coreType {
case CoreTypeInMemory:
data := noop
if e.data != nil && (e.owner == nil || e.owner == ch) {
data = e.data
}
batch = append(batch, raft.SeedEntry{Index: e.index, Data: data})
case CoreTypePersistedExclusive:
if e.data != nil && (e.owner == nil || e.owner == ch) {
cmd, err := s.codec.Decode(e.data)
if err != nil {
return err
}
// A first apply into the child's private store. Core
// errors explode, mirroring the FSM apply contract.
if _, err := ch.core.Update(cmd.Payload); err != nil {
panic(fmt.Sprintf("split catch-up of child %s: core.Update failed at parent index %d: %v", ch.shard.Id, e.index, err))
}
}
}
}
switch s.coreType {
case CoreTypeInMemory:
if err := ch.dormant.seeder.AppendEntries(batch); err != nil {
return err
}
case CoreTypePersistedExclusive:
if err := ch.dormant.seeder.SetCatchUpIndex(to); err != nil {
return err
}
}
ch.seeded = to
}
return nil
}
// restartLagging restarts every child whose seed does not include index yet.
func (s *splitter) restartLagging(index uint64, reason string) error {
for _, ch := range s.children {
if ch.seeded >= index {
continue
}
if err := s.restartChild(ch, reason); err != nil {
return err
}
}
return nil
}
// childOwning routes a stamped shard key to the child whose bounds contain
// it, mirroring Router.FindShardByShardKey (children are sorted by lower
// bound).
func (s *splitter) childOwning(shardKey cluster.ShardKey) *splitChild {
for _, ch := range s.children {
if ch.shard.ContainsKey(shardKey) {
return ch
}
}
return nil
}
// shardOwningKey returns the shard from shards whose bounds contain shardKey,
// or nil. Same routing rules as Router.FindShardByShardKey.
func shardOwningKey(shards []*cluster.Shard, shardKey cluster.ShardKey) *cluster.Shard {
for _, sh := range shards {
if sh.ContainsKey(shardKey) {
return sh
}
}
return nil
}
// replicaOf finds the replica entry with the given id in a shard.
func replicaOf(shard *cluster.Shard, replicaId string) *cluster.Replica {
for _, r := range shard.Replicas {
if r.Id == replicaId {
return r
}
}
return nil
}