-
Notifications
You must be signed in to change notification settings - Fork 0
/
gloader.go
312 lines (255 loc) · 7.68 KB
/
gloader.go
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package gloader
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/mohammadv184/gloader/data"
"github.com/mohammadv184/gloader/driver"
"github.com/mohammadv184/gloader/pkg/stats"
)
const (
DefaultRowsPerBatch = 100
DefaultWorkers = 3
)
var (
ErrBufferNotSet = errors.New("buffer not set")
ErrConnectionPoolNotSet = errors.New("connection pool not set")
ErrDataMapNotSet = errors.New("data map not set")
ErrEndOffsetLessThanStartOffset = errors.New("end offset less than start offset")
ErrEndOffsetRequired = errors.New("end offset required")
ErrSrcConnectionIsRequired = errors.New("source connection is required")
ErrDestConnectionIsRequired = errors.New("destination connection is required")
)
var ErrCCCauseStopFuncCalled = errors.New("stop func called")
type GLoader struct {
srcConnector *driver.Connector
destConnector *driver.Connector
reader *Reader
writer *Writer
dataCollectionEndOffset map[string]uint64
dataCollectionStartOffset map[string]uint64
includedDataCollections []string
excludedDataCollections []string
rowsPerBatch uint64
workers uint
ctx context.Context
ctxCancelFunc context.CancelCauseFunc
stats *stats.Stats
}
func NewGLoader() *GLoader {
return &GLoader{
rowsPerBatch: DefaultRowsPerBatch,
workers: DefaultWorkers,
dataCollectionEndOffset: make(map[string]uint64),
dataCollectionStartOffset: make(map[string]uint64),
stats: NewStats(),
}
}
func (g *GLoader) Src(name, dsn string) error {
d, err := driver.GetDriver(name)
if err != nil {
return err
}
dc := driver.NewConnector(d, dsn)
if !dc.IsReadable() {
return driver.ErrConnectionNotReadable
}
g.srcConnector = dc
return nil
}
func (g *GLoader) Dest(name, dsn string) error {
d, err := driver.GetDriver(name)
if err != nil {
return err
}
dc := driver.NewConnector(d, dsn)
if !dc.IsWritable() {
return driver.ErrConnectionNotWritable
}
g.destConnector = dc
return nil
}
func (g *GLoader) GetSrcDetails(ctx context.Context) (driver.DatabaseDetail, error) {
if g.srcConnector == nil {
return driver.DatabaseDetail{}, ErrSrcConnectionIsRequired
}
conn, err := g.srcConnector.Connect(ctx)
if err != nil {
return driver.DatabaseDetail{}, err
}
return conn.GetDetails(ctx)
}
func (g *GLoader) GetDestDetails(ctx context.Context) (driver.DatabaseDetail, error) {
if g.destConnector == nil {
return driver.DatabaseDetail{}, ErrDestConnectionIsRequired
}
conn, err := g.destConnector.Connect(ctx)
if err != nil {
return driver.DatabaseDetail{}, err
}
return conn.GetDetails(ctx)
}
func (g *GLoader) Filter(dataCollection, key string, condition driver.Condition, value string) *GLoader {
if g.srcConnector == nil {
panic(ErrSrcConnectionIsRequired)
}
g.srcConnector.WhereCondition(dataCollection, condition, key, value)
return g
}
func (g *GLoader) OrderBy(dataCollection, key string, direction driver.Direction) *GLoader {
if g.srcConnector == nil {
panic(ErrSrcConnectionIsRequired)
}
g.srcConnector.OrderBy(dataCollection, key, direction)
return g
}
func (g *GLoader) FilterAll(key string, condition driver.Condition, value string) *GLoader {
if g.srcConnector == nil {
panic(ErrSrcConnectionIsRequired)
}
g.srcConnector.WhereRootCondition(condition, key, value)
return g
}
func (g *GLoader) OrderByAll(key string, direction driver.Direction) *GLoader {
if g.srcConnector == nil {
panic(ErrSrcConnectionIsRequired)
}
g.srcConnector.OrderByRoot(key, direction)
return g
}
func (g *GLoader) Include(dataCollections ...string) *GLoader {
g.includedDataCollections = dataCollections
return g
}
func (g *GLoader) Exclude(dataCollections ...string) *GLoader {
g.excludedDataCollections = dataCollections
return g
}
func (g *GLoader) SetEndOffset(dataCollection string, offset uint64) *GLoader {
g.dataCollectionEndOffset[dataCollection] = offset
return g
}
func (g *GLoader) SetStartOffset(dataCollection string, offset uint64) *GLoader {
g.dataCollectionStartOffset[dataCollection] = offset
return g
}
func (g *GLoader) SetRowsPerBatch(rowsPerBatch uint64) *GLoader {
g.rowsPerBatch = rowsPerBatch
return g
}
func (g *GLoader) SetWorkers(workers uint) *GLoader {
g.workers = workers
return g
}
func (g *GLoader) Stats() *stats.Stats {
return g.stats
}
func (g *GLoader) Start() error {
return g.StartWithContext(context.Background())
}
func (g *GLoader) StartWithContext(ctx context.Context) error {
c, cCancelCauseFunc := context.WithCancelCause(ctx)
g.ctx = c
g.ctxCancelFunc = cCancelCauseFunc
srcConn, err := g.srcConnector.Connect(c)
if err != nil {
return err
}
defer srcConn.Close()
sDetails, err := srcConn.GetDetails(c)
if err != nil {
return err
}
destConn, err := g.destConnector.Connect(c)
if err != nil {
return err
}
defer destConn.Close()
dDetails, err := destConn.GetDetails(c)
if err != nil {
return err
}
var DCs []driver.DataCollectionDetail
DCs = sDetails.DataCollections
if len(g.includedDataCollections) > 0 {
DCs = sDetails.OnlyDataCollections(g.includedDataCollections...)
}
if len(g.excludedDataCollections) > 0 {
DCs = sDetails.AllDataCollectionsExcept(g.excludedDataCollections...)
}
wg := sync.WaitGroup{}
for i, dc := range DCs {
if dc.DataSetCount == 0 {
continue
}
dDC, err := dDetails.GetDataCollection(dc.Name)
if err != nil {
return fmt.Errorf("GLoader: failed to get destination data collection details for %s", dc.Name)
}
var srcSchema strings.Builder
srcSchema.WriteString("Migration Source Schema:\n")
var destSchema strings.Builder
destSchema.WriteString("Migration Destination Schema:\n")
for k, v := range dc.GetDataMap().GetTypeMap() {
srcSchema.WriteString(fmt.Sprintf("%d \t %s: \t %s \t IS NULLABLE: %t\n", i, k, v.GetTypeName(), dc.GetDataMap().IsNullable(k)))
dDT := dDC.GetDataMap().Get(k)
if dDT == nil {
continue
}
destSchema.WriteString(fmt.Sprintf("%d \t %s: \t %s \t IS NULLABLE: %t\n", i, k, dDT.GetTypeName(), dDC.GetDataMap().IsNullable(k)))
}
fmt.Println(srcSchema.String())
fmt.Println(destSchema.String())
wg.Add(2)
buffer := data.NewBuffer(c).
WithObserver(NewBufferObserverAdapter(g.stats, dc.Name))
rConnectionPool := driver.NewConnectionPool(g.srcConnector)
wConnectionPool := driver.NewConnectionPool(g.destConnector)
reader := NewReader(c, dc.Name, buffer, dc.DataMap, rConnectionPool)
if offset, ok := g.dataCollectionStartOffset[dc.Name]; ok {
reader.SetStartOffset(offset)
}
if offset, ok := g.dataCollectionEndOffset[dc.Name]; ok {
reader.SetEndOffset(offset)
} else {
reader.SetEndOffset(uint64(dc.DataSetCount))
}
fmt.Println("Starting to load", dc.Name, "from", reader.startOffset, "to", reader.endOffset)
reader.SetRowsPerBatch(g.rowsPerBatch)
reader.SetWorkers(g.workers)
writer := NewWriter(c, dc.Name, buffer, wConnectionPool)
writer.SetRowsPerBatch(g.rowsPerBatch)
writer.SetWorkers(g.workers)
go func(reader *Reader, rcPool *driver.ConnectionPool) {
err := reader.Start()
if err != nil {
panic(err)
}
wg.Done()
err = rcPool.CloseAll()
if err != nil {
// TODO: logging system
//log.Println(err)
}
}(reader, rConnectionPool)
go func(writer *Writer, wcPool *driver.ConnectionPool) {
err := writer.Start()
if err != nil {
panic(err)
}
wg.Done()
err = wcPool.CloseAll()
if err != nil {
// TODO: logging system
//log.Println(err)
}
}(writer, wConnectionPool)
}
wg.Wait()
return nil
}
func (g *GLoader) Stop() {
g.ctxCancelFunc(ErrCCCauseStopFuncCalled)
}