-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathadmin_reshard.go
More file actions
193 lines (163 loc) · 4.75 KB
/
admin_reshard.go
File metadata and controls
193 lines (163 loc) · 4.75 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
/*
* Radon
*
* Copyright 2018-2019 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package proxy
import (
"errors"
"fmt"
"sync"
"time"
"backend"
"router"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
"github.com/xelabs/go-mysqlstack/xlog"
)
const (
shiftUnfinished = 0
shiftFinished = 1
)
// Reshard ...
type Reshard struct {
mu sync.RWMutex
wg sync.WaitGroup
log *xlog.Log
scatter *backend.Scatter
router *router.Router
spanner *Spanner
user string
db string
singleTable string
dstDB string
reshardTable string
tmpReshardTable string
ticker *time.Ticker
handle ReshardHandle
shiftProcessBar int
shiftStatus error
}
var _ ReshardHandle = &Reshard{}
// ReshardHandle ...
type ReshardHandle interface {
ShiftProcess() error
}
// ShiftProcess is call the shift tool cmd.
func (reshard *Reshard) ShiftProcess() error {
return shiftTableLow(reshard.db, reshard.singleTable, reshard.dstDB, reshard.reshardTable, reshard.user, reshard.spanner)
}
// ShiftProcessBar about status of the Shift Process Bar.
func (reshard *Reshard) ShiftProcessBar() int {
reshard.mu.RLock()
defer reshard.mu.RUnlock()
return reshard.shiftProcessBar
}
// SetShiftProcessBar set the Shift Process Bar.
func (reshard *Reshard) SetShiftProcessBar(finished int) {
reshard.mu.Lock()
defer reshard.mu.Unlock()
reshard.shiftProcessBar = finished
}
// ShiftStatus about shift status.
func (reshard *Reshard) ShiftStatus() error {
reshard.mu.RLock()
defer reshard.mu.RUnlock()
return reshard.shiftStatus
}
// SetShiftStatus set the shift status.
func (reshard *Reshard) SetShiftStatus(err error) {
reshard.mu.Lock()
defer reshard.mu.Unlock()
reshard.shiftStatus = err
}
// NewReshard ...
func NewReshard(log *xlog.Log, scatter *backend.Scatter, router *router.Router,
spanner *Spanner, user string) *Reshard {
return &Reshard{
log: log,
scatter: scatter,
router: router,
spanner: spanner,
ticker: time.NewTicker(time.Duration(time.Second * 5)),
user: user,
}
}
// SetHandle set the handle
func (reshard *Reshard) SetHandle(r ReshardHandle) {
reshard.handle = r
}
// CheckReshardDBTable check the database and table.
func (reshard *Reshard) CheckReshardDBTable(db, singleTable, dstDB, dstTable string) (bool, error) {
isSingle, err := reshard.IsSingleTable(db, singleTable)
if err != nil {
err := fmt.Errorf("reshard.check.[%s].is.singleTable.err.%v", singleTable, err)
return false, err
}
if isSingle != true {
err := fmt.Errorf("reshard.check.[%s].is.not.singleTable", singleTable)
return false, err
}
err = reshard.router.CheckDatabase(dstDB)
if err != nil {
err := fmt.Errorf("reshard.check.[%s].is.not.exist", dstDB)
return false, err
}
// make sure the dstTable is not exist to the shift.
isExist, err := reshard.router.CheckTable(dstDB, dstTable)
if err == nil && isExist == false {
return true, nil
}
if err == nil {
err = fmt.Errorf("reshard.check.[%s].is.exist", dstTable)
}
return false, err
}
// IsSingleTable check the table is Single or not.
func (reshard *Reshard) IsSingleTable(db, singleTable string) (bool, error) {
table, err := reshard.router.TableConfig(db, singleTable)
if err != nil {
return false, err
}
if table.ShardType == "SINGLE" {
return true, nil
}
return false, nil
}
// ReShardTable just reshard single table to the sharding table now.
func (reshard *Reshard) ReShardTable(db, singleTable, dstDB, dstTable string) (*sqltypes.Result, error) {
log := reshard.log
qr := &sqltypes.Result{}
if ok, err := reshard.CheckReshardDBTable(db, singleTable, dstDB, dstTable); ok != true {
log.Error("reshard.check[%s.%s->%s.%s].is.not.ok:%v.", db, singleTable, dstDB, dstTable, err)
err := fmt.Sprintf("reshard.check[%s.%s->%s.%s].is.not.ok:%v.", db, singleTable, dstDB, dstTable, err)
return qr, errors.New(err)
}
reshard.db = db
reshard.singleTable = singleTable
reshard.dstDB = dstDB
reshard.reshardTable = dstTable
// start the shift process.
reshard.shiftTable(reshard.user)
return qr, nil
}
// The call is returned immediately, won't call wg.Wait()
// 1. the shift status will be filled by rc when finished
// 2. the shift progress bar will call other interface.
func (reshard *Reshard) shiftTable(user string) error {
var wg sync.WaitGroup
oneshift := func(db, srcTable, dstDB, dstTable string, user string, spanner *Spanner) {
defer wg.Done()
err := reshard.handle.ShiftProcess()
reshard.SetShiftProcessBar(shiftFinished)
if err != nil {
reshard.SetShiftStatus(err)
return
}
reshard.SetShiftStatus(nil)
}
wg.Add(1)
go oneshift(reshard.db, reshard.singleTable, reshard.dstDB, reshard.reshardTable, user, reshard.spanner)
return nil
}