mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 08:46:56 +08:00
raft cluster
wip: raft does not care about migrating wip: optimize code wip: raft election wip wip: fix raft leader missing log entries wip fix a dead lock batch set slot route wip: raft persist wip refactor cluster suite remove relay rename relay2 refactor: allow customizing client factory test raft refactor re-balance avoid errors caused by inconsistent status on follower nodes during raft commits test raft election
This commit is contained in:
@@ -9,109 +9,117 @@ import (
|
||||
|
||||
func TestCopy(t *testing.T) {
|
||||
conn := new(connection.FakeConn)
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("FlushALL"))
|
||||
testNodeA := testCluster[0]
|
||||
testNodeB := testCluster[1]
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("FlushALL"))
|
||||
testNodeB.Exec(conn, utils.ToCmdLine("FlushALL"))
|
||||
|
||||
// cross node copy
|
||||
srcKey := testNodeA.self + utils.RandString(10)
|
||||
srcKey := "127.0.0.1:6399Bk2r3Sz0V5" // use fix key to ensure hashing to different node
|
||||
destKey := "127.0.0.1:7379CcdC0QOopF"
|
||||
value := utils.RandString(10)
|
||||
destKey := testNodeB.self + utils.RandString(10)
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
result := Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("GET", srcKey))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
result = testNodeB.db.Exec(conn, utils.ToCmdLine("GET", destKey))
|
||||
result = testNodeB.Exec(conn, utils.ToCmdLine("GET", destKey))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
// key exists
|
||||
result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
|
||||
asserts.AssertErrReply(t, result, keyExistsErr)
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
// replace
|
||||
value = utils.RandString(10)
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "REPLACE"))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("GET", srcKey))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
result = testNodeB.db.Exec(conn, utils.ToCmdLine("GET", destKey))
|
||||
result = testNodeB.Exec(conn, utils.ToCmdLine("GET", destKey))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
// test copy expire time
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "EX", "1000"))
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "EX", "1000"))
|
||||
result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "REPLACE"))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("TTL", srcKey))
|
||||
asserts.AssertIntReplyGreaterThan(t, result, 0)
|
||||
result = testNodeB.db.Exec(conn, utils.ToCmdLine("TTL", destKey))
|
||||
result = testNodeB.Exec(conn, utils.ToCmdLine("TTL", destKey))
|
||||
asserts.AssertIntReplyGreaterThan(t, result, 0)
|
||||
|
||||
// same node copy
|
||||
srcKey = testNodeA.self + utils.RandString(10)
|
||||
srcKey = "{" + testNodeA.self + "}" + utils.RandString(10)
|
||||
destKey = "{" + testNodeA.self + "}" + utils.RandString(9)
|
||||
value = utils.RandString(10)
|
||||
destKey = srcKey + utils.RandString(2)
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("GET", srcKey))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", destKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("GET", destKey))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
// key exists
|
||||
result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
// replace
|
||||
value = utils.RandString(10)
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "REPLACE"))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("GET", srcKey))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", destKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("GET", destKey))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
// test copy expire time
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "EX", "1000"))
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "EX", "1000"))
|
||||
result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "REPLACE"))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("TTL", srcKey))
|
||||
asserts.AssertIntReplyGreaterThan(t, result, 0)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", destKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("TTL", destKey))
|
||||
asserts.AssertIntReplyGreaterThan(t, result, 0)
|
||||
}
|
||||
|
||||
func TestCopyTimeout(t *testing.T) {
|
||||
conn := new(connection.FakeConn)
|
||||
testNodeA := testCluster[0]
|
||||
testNodeB := testCluster[1]
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("FlushALL"))
|
||||
testNodeB.Exec(conn, utils.ToCmdLine("FlushALL"))
|
||||
|
||||
// test src prepare failed
|
||||
*simulateATimout = true
|
||||
srcKey = testNodeA.self + utils.RandString(10)
|
||||
destKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode
|
||||
value = utils.RandString(10)
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "ex", "1000"))
|
||||
result = Rename(testNodeB, conn, utils.ToCmdLine("RENAME", srcKey, destKey))
|
||||
timeoutFlags[0] = true
|
||||
srcKey := "127.0.0.1:6399Bk2r3Sz0V5" // use fix key to ensure hashing to different node
|
||||
destKey := "127.0.0.1:7379CcdC0QOopF"
|
||||
value := utils.RandString(10)
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "ex", "1000"))
|
||||
result := Rename(testNodeB, conn, utils.ToCmdLine("RENAME", srcKey, destKey))
|
||||
asserts.AssertErrReply(t, result, "ERR timeout")
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("EXISTS", srcKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("TTL", srcKey))
|
||||
asserts.AssertIntReplyGreaterThan(t, result, 0)
|
||||
result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", destKey))
|
||||
result = testNodeB.Exec(conn, utils.ToCmdLine("EXISTS", destKey))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
*simulateATimout = false
|
||||
timeoutFlags[0] = false
|
||||
|
||||
// test dest prepare failed
|
||||
*simulateBTimout = true
|
||||
srcKey = testNodeA.self + utils.RandString(10)
|
||||
destKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode
|
||||
timeoutFlags[1] = true
|
||||
value = utils.RandString(10)
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "ex", "1000"))
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "ex", "1000"))
|
||||
result = Rename(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
|
||||
asserts.AssertErrReply(t, result, "ERR timeout")
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("EXISTS", srcKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", srcKey))
|
||||
result = testNodeA.Exec(conn, utils.ToCmdLine("TTL", srcKey))
|
||||
asserts.AssertIntReplyGreaterThan(t, result, 0)
|
||||
result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", destKey))
|
||||
result = testNodeB.Exec(conn, utils.ToCmdLine("EXISTS", destKey))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
*simulateBTimout = false
|
||||
|
||||
timeoutFlags[1] = false
|
||||
// Copying to another database
|
||||
srcKey = testNodeA.self + utils.RandString(10)
|
||||
value = utils.RandString(10)
|
||||
destKey = srcKey + utils.RandString(2)
|
||||
testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
testNodeA.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
|
||||
result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "db", "1"))
|
||||
asserts.AssertErrReply(t, result, copyToAnotherDBErr)
|
||||
|
||||
|
Reference in New Issue
Block a user