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:
finley
2023-01-02 21:27:06 +08:00
parent df672d4c92
commit bf7f628810
54 changed files with 3122 additions and 703 deletions

View File

@@ -159,7 +159,7 @@ func (dict *ConcurrentDict) PutIfExists(key string, val interface{}) (result int
}
// Remove removes the key and return the number of deleted key-value
func (dict *ConcurrentDict) Remove(key string) (result int) {
func (dict *ConcurrentDict) Remove(key string) (val interface{}, result int) {
if dict == nil {
panic("dict is nil")
}
@@ -169,12 +169,12 @@ func (dict *ConcurrentDict) Remove(key string) (result int) {
s.mutex.Lock()
defer s.mutex.Unlock()
if _, ok := s.m[key]; ok {
if val, ok := s.m[key]; ok {
delete(s.m, key)
dict.decreaseCount()
return 1
return val, 1
}
return 0
return nil, 0
}
func (dict *ConcurrentDict) addCount() int32 {

View File

@@ -123,7 +123,7 @@ func TestConcurrentRemove(t *testing.T) {
key := "k" + strconv.Itoa(i)
d.Put(key, i)
}
if d.Len()!=totalCount{
if d.Len() != totalCount {
t.Error("put test failed: expected len is 100, actual: " + strconv.Itoa(d.Len()))
}
for i := 0; i < totalCount; i++ {
@@ -139,22 +139,22 @@ func TestConcurrentRemove(t *testing.T) {
t.Error("put test failed: expected true, actual: false")
}
ret := d.Remove(key)
_, ret := d.Remove(key)
if ret != 1 {
t.Error("remove test failed: expected result 1, actual: " + strconv.Itoa(ret) + ", key:" + key)
}
if d.Len()!=totalCount-i-1{
if d.Len() != totalCount-i-1 {
t.Error("put test failed: expected len is 99, actual: " + strconv.Itoa(d.Len()))
}
_, ok = d.Get(key)
if ok {
t.Error("remove test failed: expected true, actual false")
}
ret = d.Remove(key)
_, ret = d.Remove(key)
if ret != 0 {
t.Error("remove test failed: expected result 0 actual: " + strconv.Itoa(ret))
}
if d.Len()!=totalCount-i-1{
if d.Len() != totalCount-i-1 {
t.Error("put test failed: expected len is 99, actual: " + strconv.Itoa(d.Len()))
}
}
@@ -179,7 +179,7 @@ func TestConcurrentRemove(t *testing.T) {
t.Error("put test failed: expected true, actual: false")
}
ret := d.Remove(key)
_, ret := d.Remove(key)
if ret != 1 {
t.Error("remove test failed: expected result 1, actual: " + strconv.Itoa(ret))
}
@@ -187,7 +187,7 @@ func TestConcurrentRemove(t *testing.T) {
if ok {
t.Error("remove test failed: expected true, actual false")
}
ret = d.Remove(key)
_, ret = d.Remove(key)
if ret != 0 {
t.Error("remove test failed: expected result 0 actual: " + strconv.Itoa(ret))
}
@@ -215,7 +215,7 @@ func TestConcurrentRemove(t *testing.T) {
t.Error("put test failed: expected true, actual: false")
}
ret := d.Remove(key)
_, ret := d.Remove(key)
if ret != 1 {
t.Error("remove test failed: expected result 1, actual: " + strconv.Itoa(ret))
}
@@ -223,7 +223,7 @@ func TestConcurrentRemove(t *testing.T) {
if ok {
t.Error("remove test failed: expected true, actual false")
}
ret = d.Remove(key)
_, ret = d.Remove(key)
if ret != 0 {
t.Error("remove test failed: expected result 0 actual: " + strconv.Itoa(ret))
}

View File

@@ -10,7 +10,7 @@ type Dict interface {
Put(key string, val interface{}) (result int)
PutIfAbsent(key string, val interface{}) (result int)
PutIfExists(key string, val interface{}) (result int)
Remove(key string) (result int)
Remove(key string) (val interface{}, result int)
ForEach(consumer Consumer)
Keys() []string
RandomKeys(limit int) []string

View File

@@ -57,13 +57,13 @@ func (dict *SimpleDict) PutIfExists(key string, val interface{}) (result int) {
}
// Remove removes the key and return the number of deleted key-value
func (dict *SimpleDict) Remove(key string) (result int) {
_, existed := dict.m[key]
func (dict *SimpleDict) Remove(key string) (val interface{}, result int) {
val, existed := dict.m[key]
delete(dict.m, key)
if existed {
return 1
return val, 1
}
return 0
return nil, 0
}
// Keys returns all keys in dict

View File

@@ -25,7 +25,8 @@ func (set *Set) Add(val string) int {
// Remove removes member from set
func (set *Set) Remove(val string) int {
return set.dict.Remove(val)
_, ret := set.dict.Remove(val)
return ret
}
// Has returns true if the val exists in the set