|
| 1 | +package kv |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/bootjp/elastickv/store" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCoordinateDispatch_RawPut(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + st := store.NewMVCCStore() |
| 15 | + fsm := NewKvFSM(st) |
| 16 | + r, stop := newSingleRaft(t, "dispatch-raw-put", fsm) |
| 17 | + t.Cleanup(stop) |
| 18 | + |
| 19 | + tm := NewTransaction(r) |
| 20 | + c := NewCoordinator(tm, r) |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + resp, err := c.Dispatch(ctx, &OperationGroup[OP]{ |
| 24 | + Elems: []*Elem[OP]{ |
| 25 | + {Op: Put, Key: []byte("k1"), Value: []byte("v1")}, |
| 26 | + }, |
| 27 | + }) |
| 28 | + require.NoError(t, err) |
| 29 | + require.NotNil(t, resp) |
| 30 | + |
| 31 | + // Verify the value was written. |
| 32 | + val, err := st.GetAt(ctx, []byte("k1"), ^uint64(0)) |
| 33 | + require.NoError(t, err) |
| 34 | + require.Equal(t, []byte("v1"), val) |
| 35 | +} |
| 36 | + |
| 37 | +func TestCoordinateDispatch_RawDel(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + st := store.NewMVCCStore() |
| 41 | + fsm := NewKvFSM(st) |
| 42 | + r, stop := newSingleRaft(t, "dispatch-raw-del", fsm) |
| 43 | + t.Cleanup(stop) |
| 44 | + |
| 45 | + tm := NewTransaction(r) |
| 46 | + c := NewCoordinator(tm, r) |
| 47 | + ctx := context.Background() |
| 48 | + |
| 49 | + // Write a value first. |
| 50 | + _, err := c.Dispatch(ctx, &OperationGroup[OP]{ |
| 51 | + Elems: []*Elem[OP]{ |
| 52 | + {Op: Put, Key: []byte("k1"), Value: []byte("v1")}, |
| 53 | + }, |
| 54 | + }) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + // Delete the value. |
| 58 | + _, err = c.Dispatch(ctx, &OperationGroup[OP]{ |
| 59 | + Elems: []*Elem[OP]{ |
| 60 | + {Op: Del, Key: []byte("k1")}, |
| 61 | + }, |
| 62 | + }) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + // Verify the key is gone. |
| 66 | + _, err = st.GetAt(ctx, []byte("k1"), ^uint64(0)) |
| 67 | + require.ErrorIs(t, err, store.ErrKeyNotFound) |
| 68 | +} |
| 69 | + |
| 70 | +func TestCoordinateDispatch_TxnOnePhase(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + st := store.NewMVCCStore() |
| 74 | + fsm := NewKvFSM(st) |
| 75 | + r, stop := newSingleRaft(t, "dispatch-txn", fsm) |
| 76 | + t.Cleanup(stop) |
| 77 | + |
| 78 | + tm := NewTransaction(r) |
| 79 | + c := NewCoordinator(tm, r) |
| 80 | + ctx := context.Background() |
| 81 | + |
| 82 | + startTS := c.clock.Next() |
| 83 | + resp, err := c.Dispatch(ctx, &OperationGroup[OP]{ |
| 84 | + IsTxn: true, |
| 85 | + StartTS: startTS, |
| 86 | + Elems: []*Elem[OP]{ |
| 87 | + {Op: Put, Key: []byte("a"), Value: []byte("1")}, |
| 88 | + {Op: Put, Key: []byte("b"), Value: []byte("2")}, |
| 89 | + }, |
| 90 | + }) |
| 91 | + require.NoError(t, err) |
| 92 | + require.NotNil(t, resp) |
| 93 | + |
| 94 | + // Both keys should be readable. |
| 95 | + v1, err := st.GetAt(ctx, []byte("a"), ^uint64(0)) |
| 96 | + require.NoError(t, err) |
| 97 | + require.Equal(t, []byte("1"), v1) |
| 98 | + |
| 99 | + v2, err := st.GetAt(ctx, []byte("b"), ^uint64(0)) |
| 100 | + require.NoError(t, err) |
| 101 | + require.Equal(t, []byte("2"), v2) |
| 102 | +} |
| 103 | + |
| 104 | +func TestCoordinateDispatch_NilRequest(t *testing.T) { |
| 105 | + t.Parallel() |
| 106 | + |
| 107 | + c := &Coordinate{ |
| 108 | + clock: NewHLC(), |
| 109 | + } |
| 110 | + |
| 111 | + _, err := c.Dispatch(context.Background(), nil) |
| 112 | + require.ErrorIs(t, err, ErrInvalidRequest) |
| 113 | +} |
| 114 | + |
| 115 | +func TestCoordinateDispatch_EmptyElems(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + |
| 118 | + c := &Coordinate{ |
| 119 | + clock: NewHLC(), |
| 120 | + } |
| 121 | + |
| 122 | + _, err := c.Dispatch(context.Background(), &OperationGroup[OP]{}) |
| 123 | + require.ErrorIs(t, err, ErrInvalidRequest) |
| 124 | +} |
| 125 | + |
| 126 | +func TestCoordinateDispatch_TxnAssignsStartTS(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + tx := &stubTransactional{} |
| 130 | + st := store.NewMVCCStore() |
| 131 | + fsm := NewKvFSM(st) |
| 132 | + r, stop := newSingleRaft(t, "dispatch-ts-assign", fsm) |
| 133 | + t.Cleanup(stop) |
| 134 | + |
| 135 | + c := &Coordinate{ |
| 136 | + transactionManager: tx, |
| 137 | + raft: r, |
| 138 | + clock: NewHLC(), |
| 139 | + } |
| 140 | + |
| 141 | + // When StartTS is 0 for a txn, Dispatch should assign one. |
| 142 | + resp, err := c.Dispatch(context.Background(), &OperationGroup[OP]{ |
| 143 | + IsTxn: true, |
| 144 | + Elems: []*Elem[OP]{ |
| 145 | + {Op: Put, Key: []byte("k"), Value: []byte("v")}, |
| 146 | + }, |
| 147 | + }) |
| 148 | + require.NoError(t, err) |
| 149 | + require.NotNil(t, resp) |
| 150 | + require.Equal(t, 1, tx.commits) |
| 151 | + |
| 152 | + // The request should have a non-zero startTS. |
| 153 | + require.Len(t, tx.reqs, 1) |
| 154 | + require.Len(t, tx.reqs[0], 1) |
| 155 | + require.Greater(t, tx.reqs[0][0].Ts, uint64(0)) |
| 156 | +} |
| 157 | + |
| 158 | +func TestCoordinateDispatchRaw_CallsTransactionManager(t *testing.T) { |
| 159 | + t.Parallel() |
| 160 | + |
| 161 | + tx := &stubTransactional{} |
| 162 | + st := store.NewMVCCStore() |
| 163 | + fsm := NewKvFSM(st) |
| 164 | + r, stop := newSingleRaft(t, "dispatch-raw-tm", fsm) |
| 165 | + t.Cleanup(stop) |
| 166 | + |
| 167 | + c := &Coordinate{ |
| 168 | + transactionManager: tx, |
| 169 | + raft: r, |
| 170 | + clock: NewHLC(), |
| 171 | + } |
| 172 | + |
| 173 | + resp, err := c.Dispatch(context.Background(), &OperationGroup[OP]{ |
| 174 | + Elems: []*Elem[OP]{ |
| 175 | + {Op: Put, Key: []byte("k"), Value: []byte("v")}, |
| 176 | + }, |
| 177 | + }) |
| 178 | + require.NoError(t, err) |
| 179 | + require.NotNil(t, resp) |
| 180 | + require.Equal(t, 1, tx.commits) |
| 181 | +} |
0 commit comments