Skip to content

Commit a23bb53

Browse files
hustjiekebohutang
authored andcommitted
src: add and implement shiftmanger and refactor code to adapt to it #583
[summary] add shiftmanager module and implement shiftMgr interface [test case] src/plugins/shiftmanager/shiftmanager_test src/vendor/github.com/radondb/shift/shift/shift_test.go src/vendor/github.com/radondb/shift/shift/pool_test.go [patch codecov] src/plugins/plugin.go 87.5% src/plugins/shiftmanager/mockshift.go 94.1% src/plugins/shiftmanager/shiftmanager.go 97.1% src/vendor/github.com/radondb/shift/shift/shift.go 87.6%
1 parent e4cd7a7 commit a23bb53

File tree

14 files changed

+1309
-54
lines changed

14 files changed

+1309
-54
lines changed

makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ testplugins:
7070
go test -v plugins
7171
go test -v plugins/autoincrement
7272
go test -v plugins/privilege
73+
go test -v plugins/shiftmanager
7374
testmysqlstack:
7475
cd src/vendor/github.com/xelabs/go-mysqlstack&&make test
7576

@@ -92,7 +93,7 @@ allpkgs = xbase\
9293
audit\
9394
syncer\
9495
monitor\
95-
plugins
96+
plugins/...
9697
coverage:
9798
go build -v -o bin/gotestcover \
9899
src/vendor/github.com/pierrre/gotestcover/*.go;

src/plugins/plugin.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"plugins/autoincrement"
1717
"plugins/privilege"
18+
"plugins/shiftmanager"
1819

1920
"github.com/xelabs/go-mysqlstack/xlog"
2021
)
@@ -27,6 +28,7 @@ type Plugin struct {
2728
scatter *backend.Scatter
2829
autoincrement autoincrement.AutoIncrementHandler
2930
privilege privilege.PrivilegeHandler
31+
shiftMgr shiftmanager.ShiftMgrHandler
3032
}
3133

3234
// NewPlugin -- creates new Plugin.
@@ -60,13 +62,21 @@ func (plugin *Plugin) Init() error {
6062
}
6163
plugin.privilege = privilegePlug
6264

65+
// Register shiftmanager plug
66+
shiftMgr := shiftmanager.NewShiftManager(log)
67+
if err := shiftMgr.Init(); err != nil {
68+
return err
69+
}
70+
plugin.shiftMgr = shiftMgr
71+
6372
return nil
6473
}
6574

6675
// Close -- do nothing.
6776
func (plugin *Plugin) Close() {
6877
plugin.autoincrement.Close()
6978
plugin.privilege.Close()
79+
plugin.shiftMgr.Close()
7080
}
7181

7282
// PlugAutoIncrement -- return AutoIncrement plug.
@@ -78,3 +88,8 @@ func (plugin *Plugin) PlugAutoIncrement() autoincrement.AutoIncrementHandler {
7888
func (plugin *Plugin) PlugPrivilege() privilege.PrivilegeHandler {
7989
return plugin.privilege
8090
}
91+
92+
// PlugShiftMgr -- return ShiftMgr plug.
93+
func (plugin *Plugin) PlugShiftMgr() shiftmanager.ShiftMgrHandler {
94+
return plugin.shiftMgr
95+
}

src/plugins/plugin_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ func TestPlugins(t *testing.T) {
3737

3838
privilegePlug := plugin.PlugPrivilege()
3939
assert.NotNil(t, privilegePlug)
40+
41+
shiftMgrPlug := plugin.PlugShiftMgr()
42+
assert.NotNil(t, shiftMgrPlug)
4043
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Radon
3+
*
4+
* Copyright 2018-2020 The Radon Authors.
5+
* Code is licensed under the GPLv3.
6+
*
7+
*/
8+
9+
package shiftmanager
10+
11+
import (
12+
"github.com/radondb/shift/shift"
13+
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
14+
)
15+
16+
// ShiftStatus used to indicates shift instance's status.
17+
type ShiftStatus int
18+
19+
const (
20+
// ShiftStatusNone enum, if status is none, some errors happen
21+
ShiftStatusNone ShiftStatus = iota
22+
23+
// ShiftStatusMigrating enum
24+
ShiftStatusMigrating
25+
26+
// ShiftStatusSuccess enum
27+
ShiftStatusSuccess
28+
29+
// ShiftStatusFail enum
30+
ShiftStatusFail
31+
)
32+
33+
// ShiftType is used to distinguish what different type of shift
34+
// If it is called by reshard, the type will be ShiftTypeReshard
35+
// If it is called by rebalance, the type will be ShiftTypeRebalance
36+
type ShiftType int
37+
38+
const (
39+
// ShiftTypeNone enum, a shift type should not be none
40+
ShiftTypeNone ShiftType = iota
41+
42+
// ShiftTypeReshard enum
43+
ShiftTypeReshard
44+
45+
// ShiftTypeRebalance enum
46+
ShiftTypeRebalance
47+
)
48+
49+
// ShiftInfo used to record basic infos used by shift
50+
type ShiftInfo struct {
51+
From string
52+
FromUser string
53+
FromPassword string
54+
FromDatabase string
55+
FromTable string
56+
57+
To string
58+
ToUser string
59+
ToPassword string
60+
ToDatabase string
61+
ToTable string
62+
63+
Cleanup bool
64+
Checksum bool
65+
MysqlDump string
66+
Threads int
67+
PosBehinds int
68+
WaitTimeBeforeChecksum int
69+
RadonURL string
70+
}
71+
72+
type ShiftMgrHandler interface {
73+
Init() error
74+
NewShiftInstance(shiftInfo *ShiftInfo, typ ShiftType) (shift.ShiftHandler, error)
75+
StartShiftInstance(key string, shift shift.ShiftHandler, typ ShiftType) error
76+
WaitInstanceFinishThread(key string) error
77+
WaitInstanceFinish(key string) error
78+
StopOneInstance(key string) error
79+
StopAllInstance() error
80+
GetStatus(key string) ShiftStatus
81+
GetProgress(key string) *sqltypes.Result
82+
GetShiftType(key string) ShiftType
83+
Close() error
84+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Radon
3+
*
4+
* Copyright 2018-2020 The Radon Authors.
5+
* Code is licensed under the GPLv3.
6+
*
7+
*/
8+
9+
package shiftmanager
10+
11+
import (
12+
"fmt"
13+
14+
"github.com/radondb/shift/shift"
15+
"github.com/xelabs/go-mysqlstack/xlog"
16+
)
17+
18+
// MockShiftInfo used to mock a shift info
19+
var MockShiftInfo = &ShiftInfo{
20+
From: "src",
21+
FromUser: "test",
22+
FromPassword: "test",
23+
FromDatabase: "testdb",
24+
FromTable: "tbl",
25+
26+
To: "dst",
27+
ToUser: "user",
28+
ToPassword: "user",
29+
ToDatabase: "todb",
30+
ToTable: "totbl",
31+
32+
Cleanup: false,
33+
Checksum: true,
34+
MysqlDump: "mysqldump",
35+
Threads: 128,
36+
PosBehinds: 2048,
37+
WaitTimeBeforeChecksum: 10,
38+
RadonURL: "",
39+
}
40+
41+
// MockShift used to mock a shift for test
42+
type MockShift struct {
43+
log *xlog.Log
44+
45+
err chan struct{}
46+
allDone chan struct{}
47+
stopSignal chan struct{}
48+
}
49+
50+
// NewMockShift used to new a mockshift
51+
func NewMockShift(log *xlog.Log) shift.ShiftHandler {
52+
return &MockShift{
53+
log: log,
54+
err: make(chan struct{}),
55+
allDone: make(chan struct{}),
56+
stopSignal: make(chan struct{}),
57+
}
58+
}
59+
60+
// Start used to start a shift work.
61+
func (mockShift *MockShift) Start() error {
62+
return nil
63+
}
64+
65+
// WaitFinish used to wait success or fail signal to finish.
66+
func (mockShift *MockShift) WaitFinish() error {
67+
log := mockShift.log
68+
select {
69+
case <-mockShift.getAllDoneCh():
70+
log.Info("mockshift.table.OK")
71+
return nil
72+
case <-mockShift.getErrorCh():
73+
log.Error("mockshift.table.get.error")
74+
return fmt.Errorf("mockshift.table.get.error")
75+
case <-mockShift.getStopSignal():
76+
log.Info("mockshift.table.get.stop.signal")
77+
return fmt.Errorf("mockshift.table.get.stop.signal")
78+
}
79+
}
80+
81+
// ChecksumTable used to checksum data src tbl and dst tbl.
82+
func (mockShift *MockShift) ChecksumTable() error {
83+
return nil
84+
}
85+
86+
// SetStopSignal used set a stop signal to stop a shift work.
87+
func (mockShift *MockShift) SetStopSignal() {
88+
close(mockShift.stopSignal)
89+
}
90+
91+
// setAllDoneSignal used to set allDone signal
92+
func (mockShift MockShift) setAllDoneSignal() {
93+
close(mockShift.allDone)
94+
}
95+
96+
// setErrSignal used to set allDone signal
97+
func (mockShift MockShift) setErrSignal() {
98+
close(mockShift.err)
99+
}
100+
101+
// getAllDoneCh used to get success signal
102+
func (mockShift *MockShift) getAllDoneCh() <-chan struct{} {
103+
return mockShift.allDone
104+
}
105+
106+
// getErrorCh used to get error signal
107+
func (mockShift *MockShift) getErrorCh() <-chan struct{} {
108+
return mockShift.err
109+
}
110+
111+
// getStopSignal used to get stop signal
112+
func (mockShift *MockShift) getStopSignal() <-chan struct{} {
113+
return mockShift.stopSignal
114+
}

0 commit comments

Comments
 (0)