-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from youzan/fix-checkbackup-timeout
Optimize the sync between clusters
- Loading branch information
Showing
23 changed files
with
682 additions
and
93 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strconv" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
const ( | ||
ConfCheckSnapTimeout = "check_snap_timeout" | ||
ConfCheckRaftTimeout = "check_raft_timeout" | ||
ConfIgnoreStartupNoBackup = "ignore_startup_nobackup" | ||
ConfIgnoreRemoteFileSync = "ignore_remote_file_sync" | ||
ConfMaxRemoteRecover = "max_remote_recover" | ||
) | ||
|
||
var intConfMap map[string]*int64 | ||
var strConfMap sync.Map | ||
|
||
func init() { | ||
intConfMap = make(map[string]*int64) | ||
snapCheckTimeout := int64(60) | ||
intConfMap[ConfCheckSnapTimeout] = &snapCheckTimeout | ||
raftCheckTimeout := int64(5) | ||
intConfMap[ConfCheckRaftTimeout] = &raftCheckTimeout | ||
emptyInt := int64(0) | ||
intConfMap["empty_int"] = &emptyInt | ||
maxRemoteRecover := int64(2) | ||
intConfMap[ConfMaxRemoteRecover] = &maxRemoteRecover | ||
|
||
strConfMap.Store("test_str", "test_str") | ||
} | ||
|
||
func DumpDynamicConf() []string { | ||
cfs := make([]string, 0, len(intConfMap)*2) | ||
for k, v := range intConfMap { | ||
iv := atomic.LoadInt64(v) | ||
cfs = append(cfs, k+":"+strconv.Itoa(int(iv))) | ||
} | ||
strConfMap.Range(func(k, v interface{}) bool { | ||
cfs = append(cfs, fmt.Sprintf("%v:%v", k, v)) | ||
return true | ||
}) | ||
sort.Sort(sort.StringSlice(cfs)) | ||
return cfs | ||
} | ||
|
||
func SetIntDynamicConf(k string, newV int) { | ||
v, ok := intConfMap[k] | ||
if ok { | ||
atomic.StoreInt64(v, int64(newV)) | ||
} | ||
} | ||
|
||
func IsConfSetted(k string) bool { | ||
iv := GetIntDynamicConf(k) | ||
if iv != 0 { | ||
return true | ||
} | ||
sv := GetStrDynamicConf(k) | ||
if sv != "" { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func GetIntDynamicConf(k string) int { | ||
v, ok := intConfMap[k] | ||
if ok { | ||
return int(atomic.LoadInt64(v)) | ||
} | ||
return 0 | ||
} | ||
|
||
func SetStrDynamicConf(k string, newV string) { | ||
strConfMap.Store(k, newV) | ||
} | ||
|
||
func GetStrDynamicConf(k string) string { | ||
v, ok := strConfMap.Load(k) | ||
if !ok { | ||
return "" | ||
} | ||
return v.(string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package common | ||
|
||
import ( | ||
"reflect" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestDumpDynamicConf(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
want []string | ||
}{ | ||
// Add test cases. | ||
{"dump", []string{"check_raft_timeout:5", "check_snap_timeout:60", | ||
"empty_int:0", | ||
"max_remote_recover:2", | ||
"test_str:test_str"}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := DumpDynamicConf(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("DumpDynamicConf() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetIntDynamicConf(t *testing.T) { | ||
type args struct { | ||
k string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want int | ||
}{ | ||
//Add test cases. | ||
{"get default check_snap_timeout", args{ConfCheckSnapTimeout}, 60}, | ||
{"get changed check_snap_timeout", args{ConfCheckSnapTimeout}, 2}, | ||
{"get non exist", args{"noexist"}, 0}, | ||
{"get after set non exist", args{"noexist-set"}, 0}, | ||
} | ||
SetIntDynamicConf("noexist-set", 2) | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := GetIntDynamicConf(tt.args.k); got != tt.want { | ||
t.Errorf("GetIntDynamicConf() = %v, want %v", got, tt.want) | ||
} | ||
SetIntDynamicConf(ConfCheckSnapTimeout, 2) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetStrDynamicConf(t *testing.T) { | ||
type args struct { | ||
k string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{"get default test_str", args{"test_str"}, "test_str"}, | ||
{"get changed test_str", args{"test_str"}, "test_str_changed"}, | ||
{"get non exist", args{"noexist"}, ""}, | ||
{"get after set non exist", args{"noexist-set"}, "set-noexist"}, | ||
} | ||
SetStrDynamicConf("noexist-set", "set-noexist") | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := GetStrDynamicConf(tt.args.k); got != tt.want { | ||
t.Errorf("GetStrDynamicConf() = %v, want %v", got, tt.want) | ||
} | ||
SetStrDynamicConf("test_str", "test_str_changed") | ||
}) | ||
} | ||
} | ||
|
||
func TestIsConfSetted(t *testing.T) { | ||
type args struct { | ||
k string | ||
} | ||
tests := []struct { | ||
pre func() | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{nil, "check default check_snap_timeout", args{"check_snap_timeout"}, true}, | ||
{func() { SetIntDynamicConf("check_snap_timeout", 0) }, "check empty check_snap_timeout", args{"check_snap_timeout"}, false}, | ||
{nil, "check non exist", args{"noexist"}, false}, | ||
{nil, "check empty str conf", args{"empty_str"}, false}, | ||
{nil, "check empty int conf", args{"empty_int"}, false}, | ||
{nil, "check non exist str", args{"noexist-set-str"}, false}, | ||
{func() { SetStrDynamicConf("noexist-set-str", "v") }, "check after set non exist str", args{"noexist-set-str"}, true}, | ||
} | ||
SetStrDynamicConf("empty_str", "") | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.pre != nil { | ||
tt.pre() | ||
} | ||
if got := IsConfSetted(tt.args.k); got != tt.want { | ||
t.Errorf("IsConfSetted() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestConfRace(t *testing.T) { | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for i := 0; i < 1000000; i++ { | ||
GetIntDynamicConf("check_snap_timeout") | ||
GetIntDynamicConf("check_raft_timeout") | ||
GetStrDynamicConf("test_str") | ||
GetIntDynamicConf("noexist") | ||
GetStrDynamicConf("noexist") | ||
} | ||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for i := 0; i < 1000000; i++ { | ||
SetIntDynamicConf("check_snap_timeout", i) | ||
SetIntDynamicConf("check_raft_timeout", i) | ||
SetStrDynamicConf("test_str", strconv.Itoa(i)) | ||
SetIntDynamicConf("noexist", i) | ||
SetStrDynamicConf("noexist", "v") | ||
} | ||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for i := 0; i < 1000000; i++ { | ||
GetIntDynamicConf("check_snap_timeout") | ||
GetIntDynamicConf("check_raft_timeout") | ||
GetStrDynamicConf("test_str") | ||
GetIntDynamicConf("noexist") | ||
GetStrDynamicConf("noexist") | ||
} | ||
}() | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.