-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration_test.go
More file actions
329 lines (281 loc) · 8.09 KB
/
integration_test.go
File metadata and controls
329 lines (281 loc) · 8.09 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
package clusterkit
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"time"
)
// TestMultiNodeReplication verifies data is properly partitioned and replicated
func TestMultiNodeReplication(t *testing.T) {
// Skip in short mode (requires time for cluster formation)
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Create temporary directories for each node
tempDir := t.TempDir()
// Create 3 nodes
nodes := make([]*ClusterKit, 3)
// Node 1 (bootstrap)
node1Dir := filepath.Join(tempDir, "node-1")
os.MkdirAll(node1Dir, 0755)
var err error
nodes[0], err = New(Options{
NodeID: "node-1",
HTTPAddr: ":18080",
DataDir: node1Dir,
Bootstrap: true,
PartitionCount: 16,
ReplicationFactor: 3,
Logger: &NoOpLogger{},
})
if err != nil {
t.Fatalf("Failed to create node-1: %v", err)
}
if err := nodes[0].Start(); err != nil {
t.Fatalf("Failed to start node-1: %v", err)
}
defer nodes[0].Stop()
// Wait for bootstrap node to be ready
time.Sleep(2 * time.Second)
// Node 2
node2Dir := filepath.Join(tempDir, "node-2")
os.MkdirAll(node2Dir, 0755)
nodes[1], err = New(Options{
NodeID: "node-2",
HTTPAddr: ":18081",
DataDir: node2Dir,
JoinAddr: "localhost:18080",
PartitionCount: 16,
ReplicationFactor: 3,
Logger: &NoOpLogger{},
})
if err != nil {
t.Fatalf("Failed to create node-2: %v", err)
}
if err := nodes[1].Start(); err != nil {
t.Fatalf("Failed to start node-2: %v", err)
}
defer nodes[1].Stop()
time.Sleep(2 * time.Second)
// Node 3
node3Dir := filepath.Join(tempDir, "node-3")
os.MkdirAll(node3Dir, 0755)
nodes[2], err = New(Options{
NodeID: "node-3",
HTTPAddr: ":18082",
DataDir: node3Dir,
JoinAddr: "localhost:18080",
PartitionCount: 16,
ReplicationFactor: 3,
Logger: &NoOpLogger{},
})
if err != nil {
t.Fatalf("Failed to create node-3: %v", err)
}
if err := nodes[2].Start(); err != nil {
t.Fatalf("Failed to start node-3: %v", err)
}
defer nodes[2].Stop()
// Wait for cluster to stabilize
time.Sleep(5 * time.Second)
t.Run("VerifyClusterFormation", func(t *testing.T) {
// Verify all nodes see 3 nodes
for i, node := range nodes {
cluster := node.GetCluster()
if len(cluster.Nodes) != 3 {
t.Errorf("Node %d sees %d nodes, expected 3", i+1, len(cluster.Nodes))
}
}
})
t.Run("VerifyPartitionDistribution", func(t *testing.T) {
// Check partition distribution
for i, node := range nodes {
cluster := node.GetCluster()
if cluster.PartitionMap == nil {
t.Errorf("Node %d has no partition map", i+1)
continue
}
partitionCount := len(cluster.PartitionMap.Partitions)
if partitionCount != 16 {
t.Errorf("Node %d has %d partitions, expected 16", i+1, partitionCount)
}
}
// Verify each partition has primary + 2 replicas
cluster := nodes[0].GetCluster()
for partID, partition := range cluster.PartitionMap.Partitions {
if partition.PrimaryNode == "" {
t.Errorf("Partition %s has no primary", partID)
}
if len(partition.ReplicaNodes) != 2 {
t.Errorf("Partition %s has %d replicas, expected 2", partID, len(partition.ReplicaNodes))
}
}
})
t.Run("VerifyDataPartitioning", func(t *testing.T) {
// Test keys and their expected partitions
testKeys := []string{
"user:1", "user:2", "user:3",
"order:100", "order:101",
"product:abc", "product:xyz",
}
// Verify each key consistently maps to same partition
for _, key := range testKeys {
var partitionID string
for i, node := range nodes {
partition, err := node.GetPartition(key)
if err != nil {
t.Errorf("Node %d failed to get partition for %s: %v", i+1, key, err)
continue
}
if i == 0 {
partitionID = partition.ID
} else if partition.ID != partitionID {
t.Errorf("Key %s maps to different partitions: %s vs %s",
key, partitionID, partition.ID)
}
}
}
})
t.Run("VerifyReplicationFactor", func(t *testing.T) {
// Count how many nodes each partition is assigned to
cluster := nodes[0].GetCluster()
for partID, partition := range cluster.PartitionMap.Partitions {
// Should have 1 primary + 2 replicas = 3 total nodes
totalNodes := 1 + len(partition.ReplicaNodes)
if totalNodes != 3 {
t.Errorf("Partition %s has %d total nodes, expected 3 (RF=3)",
partID, totalNodes)
}
// Verify no duplicate nodes
allNodes := append([]string{partition.PrimaryNode}, partition.ReplicaNodes...)
seen := make(map[string]bool)
for _, nodeID := range allNodes {
if seen[nodeID] {
t.Errorf("Partition %s has duplicate node: %s", partID, nodeID)
}
seen[nodeID] = true
}
}
})
t.Run("VerifyCustomDataReplication", func(t *testing.T) {
// Set custom data on leader
testData := map[string]string{
"config": "test-config-value",
}
configJSON, _ := json.Marshal(testData)
// Find leader
var leader *ClusterKit
for _, node := range nodes {
if node.consensusManager.IsLeader() {
leader = node
break
}
}
if leader == nil {
t.Skip("No leader found, skipping custom data test")
return
}
// Set data on leader
err := leader.SetCustomData("test-config", configJSON)
if err != nil {
t.Fatalf("Failed to set custom data: %v", err)
}
// Wait for replication
time.Sleep(2 * time.Second)
// Verify all nodes can read the data
for i, node := range nodes {
data, err := node.GetCustomData("test-config")
if err != nil {
t.Errorf("Node %d failed to get custom data: %v", i+1, err)
continue
}
if string(data) != string(configJSON) {
t.Errorf("Node %d has incorrect data", i+1)
}
}
})
t.Run("VerifyPartitionOwnership", func(t *testing.T) {
// Verify each node knows which partitions it owns
cluster := nodes[0].GetCluster()
for _, node := range nodes {
primaryCount := 0
replicaCount := 0
for _, partition := range cluster.PartitionMap.Partitions {
if node.IsPrimary(partition) {
primaryCount++
}
if node.IsReplica(partition) {
replicaCount++
}
}
t.Logf("Node %s: %d primary, %d replica partitions",
node.nodeID, primaryCount, replicaCount)
// With 16 partitions and 3 nodes, each should have ~5-6 primaries
if primaryCount < 3 || primaryCount > 8 {
t.Errorf("Node %s has unusual primary count: %d",
node.nodeID, primaryCount)
}
}
})
}
// TestPartitionBalancing verifies partitions are evenly distributed
func TestPartitionBalancing(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Create a cluster with known configuration
ck := &ClusterKit{
cluster: &Cluster{
Nodes: []Node{
{ID: "node-1"},
{ID: "node-2"},
{ID: "node-3"},
},
Config: &Config{
PartitionCount: 16,
ReplicationFactor: 3,
},
PartitionMap: &PartitionMap{
Partitions: make(map[string]*Partition),
},
},
}
// Assign partitions
for i := 0; i < 16; i++ {
partitionID := fmt.Sprintf("partition-%d", i)
primary, replicas := ck.assignNodesToPartition(partitionID, 3)
ck.cluster.PartitionMap.Partitions[partitionID] = &Partition{
ID: partitionID,
PrimaryNode: primary,
ReplicaNodes: replicas,
}
}
// Count assignments per node
primaryCounts := make(map[string]int)
replicaCounts := make(map[string]int)
for _, partition := range ck.cluster.PartitionMap.Partitions {
primaryCounts[partition.PrimaryNode]++
for _, replica := range partition.ReplicaNodes {
replicaCounts[replica]++
}
}
t.Logf("Primary distribution: %v", primaryCounts)
t.Logf("Replica distribution: %v", replicaCounts)
// Verify reasonable distribution (no node should have 0 or all partitions)
for nodeID, count := range primaryCounts {
if count == 0 {
t.Errorf("Node %s has no primary partitions", nodeID)
}
if count == 16 {
t.Errorf("Node %s has all primary partitions", nodeID)
}
}
// Each node should have some replicas
for _, node := range ck.cluster.Nodes {
if replicaCounts[node.ID] == 0 {
t.Errorf("Node %s has no replica partitions", node.ID)
}
}
}