|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package wag |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/cockroachdb/cockroach/pkg/kv/kvpb" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvstorage/wag/wagpb" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/print" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/roachpb" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/storage" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/testutils/echotest" |
| 21 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 22 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestWrite(t *testing.T) { |
| 27 | + defer leaktest.AfterTest(t)() |
| 28 | + defer log.Scope(t).Close(t) |
| 29 | + |
| 30 | + eng := storage.NewDefaultInMemForTesting() |
| 31 | + defer eng.Close() |
| 32 | + s := store{eng: eng} |
| 33 | + |
| 34 | + var out string |
| 35 | + write := func(name string, f func(w storage.Writer) error) { |
| 36 | + b := eng.NewWriteBatch() |
| 37 | + defer b.Close() |
| 38 | + require.NoError(t, f(b)) |
| 39 | + |
| 40 | + str, err := print.DecodeWriteBatch(b.Repr()) |
| 41 | + require.NoError(t, err) |
| 42 | + out += fmt.Sprintf(">> %s\n%s", name, str) |
| 43 | + |
| 44 | + require.NoError(t, b.Commit(false /* sync */)) |
| 45 | + } |
| 46 | + |
| 47 | + id := roachpb.FullReplicaID{RangeID: 123, ReplicaID: 4} |
| 48 | + write("create", func(w storage.Writer) error { return createReplica(&s, w, id) }) |
| 49 | + write("init", func(w storage.Writer) error { return initReplica(&s, w, id, 10) }) |
| 50 | + write("split", func(w storage.Writer) error { return splitReplica(&s, w, id, 200) }) |
| 51 | + |
| 52 | + // TODO(pav-kv): the trailing \n in DecodeWriteBatch is duplicated with |
| 53 | + // recursion. Remove it, and let the caller handle new lines. |
| 54 | + out = strings.ReplaceAll(out, "\n\n", "\n") |
| 55 | + echotest.Require(t, out, filepath.Join("testdata", t.Name()+".txt")) |
| 56 | + |
| 57 | + // Smoke check that the iterator works. |
| 58 | + var iter Iterator |
| 59 | + count := 0 |
| 60 | + for range iter.Iter(context.Background(), s.eng) { |
| 61 | + count++ |
| 62 | + } |
| 63 | + require.NoError(t, iter.Error()) |
| 64 | + require.Equal(t, 4, count) |
| 65 | +} |
| 66 | + |
| 67 | +type store struct { |
| 68 | + eng storage.Engine |
| 69 | + seq Seq |
| 70 | +} |
| 71 | + |
| 72 | +func createReplica(s *store, w storage.Writer, id roachpb.FullReplicaID) error { |
| 73 | + b := s.eng.NewWriteBatch() |
| 74 | + defer b.Close() |
| 75 | + if err := writeStateMachine(b, "state-machine-key", "state"); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + return Write(w, s.seq.Next(1), wagpb.Node{ |
| 79 | + Addr: wagpb.Addr{RangeID: id.RangeID, ReplicaID: id.ReplicaID, Index: 0}, |
| 80 | + Type: wagpb.NodeType_NodeCreate, |
| 81 | + Mutation: wagpb.Mutation{Batch: b.Repr()}, |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func initReplica(s *store, w storage.Writer, id roachpb.FullReplicaID, index uint64) error { |
| 86 | + return Write(w, s.seq.Next(1), wagpb.Node{ |
| 87 | + Addr: wagpb.Addr{RangeID: id.RangeID, ReplicaID: id.ReplicaID, Index: kvpb.RaftIndex(index)}, |
| 88 | + Type: wagpb.NodeType_NodeSnap, |
| 89 | + Mutation: wagpb.Mutation{Ingestion: &wagpb.Ingestion{ |
| 90 | + SSTs: []string{"tmp/1.sst", "tmp/2.sst"}, |
| 91 | + }}, |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +func splitReplica(s *store, w storage.Writer, id roachpb.FullReplicaID, index uint64) error { |
| 96 | + b := s.eng.NewWriteBatch() |
| 97 | + defer b.Close() |
| 98 | + if err := writeStateMachine(b, "lhs-key", "lhs-state"); err != nil { |
| 99 | + return err |
| 100 | + } else if err := writeStateMachine(b, "rhs-key", "rhs-state"); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + seq := s.seq.Next(2) |
| 105 | + if err := Write(w, seq, wagpb.Node{ |
| 106 | + Addr: wagpb.Addr{RangeID: id.RangeID, ReplicaID: id.ReplicaID, Index: kvpb.RaftIndex(index - 1)}, |
| 107 | + Type: wagpb.NodeType_NodeApply, |
| 108 | + }); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + return Write(w, seq+1, wagpb.Node{ |
| 112 | + Addr: wagpb.Addr{RangeID: id.RangeID, ReplicaID: id.ReplicaID, Index: kvpb.RaftIndex(index)}, |
| 113 | + Type: wagpb.NodeType_NodeSplit, |
| 114 | + Mutation: wagpb.Mutation{Batch: b.Repr()}, |
| 115 | + Create: 567, // the RHS range ID |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +func writeStateMachine(w storage.Writer, k, v string) error { |
| 120 | + return w.PutUnversioned(roachpb.Key(k), []byte(v)) |
| 121 | +} |
0 commit comments