|
| 1 | +package snapshottest |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + log "github.com/sirupsen/logrus" |
| 6 | + "github.com/squareup/pranadb/cluster" |
| 7 | + "github.com/squareup/pranadb/cluster/dragon" |
| 8 | + "github.com/squareup/pranadb/conf" |
| 9 | + "github.com/squareup/pranadb/errors" |
| 10 | + "github.com/squareup/pranadb/table" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "io/ioutil" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +func TestSnapshot(t *testing.T) { |
| 18 | + dataDir, err := ioutil.TempDir("", "dragon-test") |
| 19 | + if err != nil { |
| 20 | + panic("failed to create temp dir") |
| 21 | + } |
| 22 | + // We start just two nodes of the three node cluster - this provides a quorum so they will accept writes |
| 23 | + dragonCluster, err := startDragonNodes(dataDir, 0, 2) |
| 24 | + if err != nil { |
| 25 | + panic(fmt.Sprintf("failed to start dragon cluster %+v", err)) |
| 26 | + } |
| 27 | + node := dragonCluster[0] |
| 28 | + |
| 29 | + // Write a bunch of data to a shard |
| 30 | + numKeys := 50 |
| 31 | + batchSize := 1 |
| 32 | + var wb *cluster.WriteBatch |
| 33 | + shardID := node.GetAllShardIDs()[0] |
| 34 | + tableID := uint64(1056) |
| 35 | + var keys [][]byte |
| 36 | + var vals []string |
| 37 | + for i := 0; i < numKeys; i++ { |
| 38 | + skey := fmt.Sprintf("some-key-%06d", i) |
| 39 | + value := fmt.Sprintf("some-value-%d", i) |
| 40 | + key := table.EncodeTableKeyPrefix(tableID, shardID, 16) |
| 41 | + key = append(key, []byte(skey)...) |
| 42 | + keys = append(keys, key) |
| 43 | + vals = append(vals, value) |
| 44 | + if wb == nil { |
| 45 | + wb = cluster.NewWriteBatch(shardID) |
| 46 | + } |
| 47 | + wb.AddPut(key, []byte(value)) |
| 48 | + if i%batchSize == 0 { |
| 49 | + err := node.WriteBatch(wb) |
| 50 | + require.NoError(t, err) |
| 51 | + log.Info("wrote batch") |
| 52 | + wb = nil |
| 53 | + } |
| 54 | + } |
| 55 | + if wb != nil { |
| 56 | + err := node.WriteBatch(wb) |
| 57 | + require.NoError(t, err) |
| 58 | + log.Info("wrote batch") |
| 59 | + } |
| 60 | + |
| 61 | + // At this point no snapshots should have been done. |
| 62 | + // We use an IOnDiskStateMachine and Dragon will NOT create snapshots periodically because the state machine |
| 63 | + // is persisted to disk anyway (that's not true for standard state machines) |
| 64 | + // Snapshots are only created when another node starts and needs to get its state machine in sync with the rest |
| 65 | + // of the group. In this case a snapshot stream is requested from an existing node, the snapshot is created and |
| 66 | + // streamed to the new node |
| 67 | + require.Equal(t, int64(0), dragonCluster[0].SaveSnapshotCount()) |
| 68 | + require.Equal(t, int64(0), dragonCluster[0].RestoreSnapshotCount()) |
| 69 | + require.Equal(t, int64(0), dragonCluster[2].SaveSnapshotCount()) |
| 70 | + require.Equal(t, int64(0), dragonCluster[2].RestoreSnapshotCount()) |
| 71 | + |
| 72 | + // Now start the another node |
| 73 | + // This should trigger a snapshot on one of the existing nodes which should then be streamed to the new node |
| 74 | + // so it can get up to state |
| 75 | + dragonCluster2, err := startDragonNodes(dataDir, 1) |
| 76 | + if err != nil { |
| 77 | + panic(fmt.Sprintf("failed to start dragon cluster %+v", err)) |
| 78 | + } |
| 79 | + require.Equal(t, int64(0), dragonCluster2[1].SaveSnapshotCount()) |
| 80 | + require.Equal(t, int64(1), dragonCluster2[1].RestoreSnapshotCount()) |
| 81 | + |
| 82 | + require.Equal(t, int64(0), dragonCluster[0].RestoreSnapshotCount()) |
| 83 | + require.Equal(t, int64(0), dragonCluster[2].RestoreSnapshotCount()) |
| 84 | + |
| 85 | + require.True(t, dragonCluster[0].SaveSnapshotCount() == 1 || dragonCluster[2].SaveSnapshotCount() == 1) |
| 86 | + require.False(t, dragonCluster[0].SaveSnapshotCount() == 1 && dragonCluster[2].SaveSnapshotCount() == 1) |
| 87 | + |
| 88 | + // Now we read the data back from this node |
| 89 | + // We read it directly from Pebble to make sure it's actually got to that node |
| 90 | + for i, key := range keys { |
| 91 | + va, err := dragonCluster2[1].LocalGet(key) |
| 92 | + require.NoError(t, err) |
| 93 | + require.Equal(t, vals[i], string(va)) |
| 94 | + } |
| 95 | + |
| 96 | + stopDragonCluster(dragonCluster) |
| 97 | + stopDragonCluster(dragonCluster2) |
| 98 | +} |
| 99 | + |
| 100 | +func startDragonNodes(dataDir string, nodes ...int) ([]*dragon.Dragon, error) { |
| 101 | + |
| 102 | + nodeAddresses := []string{ |
| 103 | + "localhost:63101", |
| 104 | + "localhost:63102", |
| 105 | + "localhost:63103", |
| 106 | + } |
| 107 | + |
| 108 | + requiredNodes := map[int]struct{}{} |
| 109 | + for _, node := range nodes { |
| 110 | + requiredNodes[node] = struct{}{} |
| 111 | + } |
| 112 | + |
| 113 | + var chans []chan error |
| 114 | + clusterNodes := make([]*dragon.Dragon, len(nodeAddresses)) |
| 115 | + for i := 0; i < len(nodeAddresses); i++ { |
| 116 | + _, ok := requiredNodes[i] |
| 117 | + if !ok { |
| 118 | + continue |
| 119 | + } |
| 120 | + ch := make(chan error) |
| 121 | + chans = append(chans, ch) |
| 122 | + cnf := conf.NewDefaultConfig() |
| 123 | + cnf.NodeID = i |
| 124 | + cnf.ClusterID = 123 |
| 125 | + cnf.RaftAddresses = nodeAddresses |
| 126 | + cnf.NumShards = 10 |
| 127 | + cnf.DataDir = dataDir |
| 128 | + cnf.ReplicationFactor = 3 |
| 129 | + cnf.TestServer = true |
| 130 | + cnf.DataSnapshotEntries = 10 |
| 131 | + cnf.DataCompactionOverhead = 5 |
| 132 | + clus, err := dragon.NewDragon(*cnf) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + clusterNodes[i] = clus |
| 137 | + clus.RegisterShardListenerFactory(&cluster.DummyShardListenerFactory{}) |
| 138 | + clus.SetRemoteQueryExecutionCallback(&cluster.DummyRemoteQueryExecutionCallback{}) |
| 139 | + |
| 140 | + go startDragonNode(clus, ch) |
| 141 | + } |
| 142 | + |
| 143 | + for _, ch := range chans { |
| 144 | + err, ok := <-ch |
| 145 | + if !ok { |
| 146 | + return nil, errors.Error("channel was closed") |
| 147 | + } |
| 148 | + if err != nil { |
| 149 | + return nil, errors.WithStack(err) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + time.Sleep(5 * time.Second) |
| 154 | + return clusterNodes, nil |
| 155 | +} |
| 156 | + |
| 157 | +func startDragonNode(clus cluster.Cluster, ch chan error) { |
| 158 | + err := clus.Start() |
| 159 | + ch <- err |
| 160 | +} |
| 161 | + |
| 162 | +func stopDragonCluster(dragonCluster []*dragon.Dragon) { |
| 163 | + for _, dragon := range dragonCluster { |
| 164 | + if dragon != nil { |
| 165 | + err := dragon.Stop() |
| 166 | + if err != nil { |
| 167 | + panic(fmt.Sprintf("failed to stop dragon cluster %+v", err)) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments