/
etcd.go
350 lines (317 loc) · 10.7 KB
/
etcd.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Licensed to the LF AI & Data foundation under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* //
* http://www.apache.org/licenses/LICENSE-2.0
* //
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package store
import (
"context"
"time"
"github.com/cockroachdb/errors"
"github.com/goccy/go-json"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"github.com/zilliztech/milvus-cdc/core/log"
"github.com/zilliztech/milvus-cdc/core/util"
"github.com/zilliztech/milvus-cdc/server/api"
"github.com/zilliztech/milvus-cdc/server/model/meta"
)
var (
EtcdOpTimeout = 5 * time.Second
EtcdOpRetryTime uint = 5
)
type EtcdMetaStore struct {
log *zap.Logger
etcdClient *clientv3.Client
taskInfoStore *TaskInfoEtcdStore
taskCollectionPositionStore *TaskCollectionPositionEtcdStore
txnMap map[any][]clientv3.Op
}
var _ api.MetaStoreFactory = &EtcdMetaStore{}
func NewEtcdMetaStore(ctx context.Context, endpoints []string, rootPath string) (*EtcdMetaStore, error) {
log := log.With(zap.Strings("endpoints", endpoints)).Logger
etcdClient, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: 5 * time.Second,
Logger: log,
})
if err != nil {
log.Warn("fail to get etcd client")
return nil, err
}
txnMap := make(map[any][]clientv3.Op)
taskInfoStore, err := NewTaskInfoEtcdStore(ctx, etcdClient, rootPath, txnMap)
if err != nil {
log.Warn("fail to get task info store")
return nil, err
}
taskCollectionPositionStore, err := NewTaskCollectionPositionEtcdStore(ctx, etcdClient, rootPath, txnMap)
if err != nil {
log.Warn("fail to get task collection position store")
return nil, err
}
return &EtcdMetaStore{
log: log,
etcdClient: etcdClient,
taskInfoStore: taskInfoStore,
taskCollectionPositionStore: taskCollectionPositionStore,
txnMap: txnMap,
}, nil
}
func (e *EtcdMetaStore) GetTaskInfoMetaStore(ctx context.Context) api.MetaStore[*meta.TaskInfo] {
return e.taskInfoStore
}
func (e *EtcdMetaStore) GetTaskCollectionPositionMetaStore(ctx context.Context) api.MetaStore[*meta.TaskCollectionPosition] {
return e.taskCollectionPositionStore
}
func (e *EtcdMetaStore) Txn(ctx context.Context) (any, func(err error) error, error) {
txn := e.etcdClient.Txn(ctx)
commitFunc := func(err error) error {
defer delete(e.txnMap, txn)
if err == nil {
txn.Then(e.txnMap[txn]...)
_, err = txn.Commit()
if err != nil {
return err
}
return nil
}
return err
}
e.txnMap[txn] = []clientv3.Op{}
return txn, commitFunc, nil
}
type TaskInfoEtcdStore struct {
log *zap.Logger
rootPath string
etcdClient *clientv3.Client
txnMap map[any][]clientv3.Op
}
var _ api.MetaStore[*meta.TaskInfo] = &TaskInfoEtcdStore{}
func NewTaskInfoEtcdStore(ctx context.Context, etcdClient *clientv3.Client, rootPath string, txnMap map[any][]clientv3.Op) (*TaskInfoEtcdStore, error) {
t := &TaskInfoEtcdStore{
rootPath: rootPath,
etcdClient: etcdClient,
txnMap: txnMap,
}
t.log = log.With(zap.String("meta_store", "etcd"), zap.String("table", "task_info"), zap.String("root_path", rootPath)).Logger
err := EtcdStatus(ctx, etcdClient)
if err != nil {
t.log.Warn("unavailable etcd server, please check it", zap.Error(err))
return nil, err
}
return t, nil
}
func (t *TaskInfoEtcdStore) Put(ctx context.Context, metaObj *meta.TaskInfo, txn any) error {
timeCtx, cancel := context.WithTimeout(ctx, EtcdOpTimeout)
defer cancel()
objBytes, err := json.Marshal(metaObj)
if err != nil {
t.log.Warn("fail to marshal task info", zap.Error(err))
return err
}
taskInfoKey := getTaskInfoKey(t.rootPath, metaObj.TaskID)
if txn != nil {
if _, ok := t.txnMap[txn]; !ok {
t.log.Warn("txn not exist")
return errors.New("txn not exist")
}
t.txnMap[txn] = append(t.txnMap[txn], clientv3.OpPut(taskInfoKey, util.ToString(objBytes)))
return nil
}
_, err = t.etcdClient.Put(timeCtx, taskInfoKey, util.ToString(objBytes))
if err != nil {
t.log.Warn("fail to put task info", zap.Error(err))
return err
}
return nil
}
func (t *TaskInfoEtcdStore) Get(ctx context.Context, metaObj *meta.TaskInfo, txn any) ([]*meta.TaskInfo, error) {
timeCtx, cancel := context.WithTimeout(ctx, EtcdOpTimeout)
defer cancel()
key := getTaskInfoPrefix(t.rootPath)
ops := []clientv3.OpOption{clientv3.WithPrefix()}
if metaObj.TaskID != "" {
key = getTaskInfoKey(t.rootPath, metaObj.TaskID)
ops = []clientv3.OpOption{}
}
if txn != nil {
if _, ok := t.txnMap[txn]; !ok {
return nil, errors.New("txn not exist")
}
t.txnMap[txn] = append(t.txnMap[txn], clientv3.OpGet(key, ops...))
return nil, nil
}
getResp, err := t.etcdClient.Get(timeCtx, key, ops...)
if err != nil {
t.log.Warn("fail to get task info", zap.Error(err))
return nil, err
}
var taskInfos []*meta.TaskInfo
for _, kv := range getResp.Kvs {
taskInfoValue := kv.Value
var taskInfo meta.TaskInfo
err = json.Unmarshal(taskInfoValue, &taskInfo)
if err != nil {
t.log.Warn("fail to unmarshal task info", zap.Error(err))
return nil, err
}
taskInfos = append(taskInfos, &taskInfo)
}
return taskInfos, nil
}
func (t *TaskInfoEtcdStore) Delete(ctx context.Context, metaObj *meta.TaskInfo, txn any) error {
if metaObj.TaskID == "" {
return errors.New("task id is empty")
}
timeCtx, cancel := context.WithTimeout(ctx, EtcdOpTimeout)
defer cancel()
taskInfoKey := getTaskInfoKey(t.rootPath, metaObj.TaskID)
if txn != nil {
if _, ok := t.txnMap[txn]; !ok {
return errors.New("txn not exist")
}
t.txnMap[txn] = append(t.txnMap[txn], clientv3.OpDelete(taskInfoKey))
return nil
}
_, err := t.etcdClient.Delete(timeCtx, taskInfoKey)
if err != nil {
t.log.Warn("fail to delete task info", zap.Error(err))
return err
}
return err
}
type TaskCollectionPositionEtcdStore struct {
log *zap.Logger
rootPath string
etcdClient *clientv3.Client
txnMap map[any][]clientv3.Op
}
var _ api.MetaStore[*meta.TaskCollectionPosition] = &TaskCollectionPositionEtcdStore{}
func NewTaskCollectionPositionEtcdStore(ctx context.Context, etcdClient *clientv3.Client, rootPath string, txnMap map[any][]clientv3.Op) (*TaskCollectionPositionEtcdStore, error) {
t := &TaskCollectionPositionEtcdStore{
rootPath: rootPath,
etcdClient: etcdClient,
txnMap: txnMap,
}
t.log = log.With(zap.String("meta_store", "etcd"), zap.String("table", "task_collection_position"), zap.String("root_path", rootPath)).Logger
err := EtcdStatus(ctx, etcdClient)
if err != nil {
t.log.Warn("unavailable etcd server, please check it", zap.Error(err))
return nil, err
}
return t, nil
}
func (t *TaskCollectionPositionEtcdStore) Put(ctx context.Context, metaObj *meta.TaskCollectionPosition, txn any) error {
timeCtx, cancel := context.WithTimeout(ctx, EtcdOpTimeout)
defer cancel()
positionBytes, err := json.Marshal(metaObj)
taskPositionKey := getTaskCollectionPositionKey(t.rootPath, metaObj.TaskID, metaObj.CollectionID)
if err != nil {
t.log.Warn("fail to marshal task position", zap.Error(err))
return err
}
if txn != nil {
if _, ok := t.txnMap[txn]; !ok {
t.log.Warn("txn not exist")
return errors.New("txn not exist")
}
t.txnMap[txn] = append(t.txnMap[txn], clientv3.OpPut(taskPositionKey, util.ToString(positionBytes)))
return nil
}
_, err = t.etcdClient.Put(timeCtx, taskPositionKey, util.ToString(positionBytes))
if err != nil {
t.log.Warn("fail to put task position", zap.Error(err))
return err
}
return nil
}
func (t *TaskCollectionPositionEtcdStore) Get(ctx context.Context, metaObj *meta.TaskCollectionPosition, txn any) ([]*meta.TaskCollectionPosition, error) {
timeCtx, cancel := context.WithTimeout(ctx, EtcdOpTimeout)
defer cancel()
key := getTaskCollectionPositionPrefix(t.rootPath)
ops := []clientv3.OpOption{clientv3.WithPrefix()}
if metaObj.TaskID != "" && metaObj.CollectionID != 0 {
key = getTaskCollectionPositionKey(t.rootPath, metaObj.TaskID, metaObj.CollectionID)
ops = []clientv3.OpOption{}
} else if metaObj.TaskID != "" {
key = getTaskCollectionPositionPrefixWithTaskID(t.rootPath, metaObj.TaskID)
ops = []clientv3.OpOption{clientv3.WithPrefix()}
}
if txn != nil {
if _, ok := t.txnMap[txn]; !ok {
t.log.Warn("txn not exist")
return nil, errors.New("txn not exist")
}
t.txnMap[txn] = append(t.txnMap[txn], clientv3.OpGet(key, ops...))
return nil, nil
}
getResp, err := t.etcdClient.Get(timeCtx, key, ops...)
if err != nil {
t.log.Warn("fail to get task position", zap.Error(err))
return nil, err
}
var taskPositions []*meta.TaskCollectionPosition
for _, kv := range getResp.Kvs {
taskPositionValue := kv.Value
var taskPosition meta.TaskCollectionPosition
err = json.Unmarshal(taskPositionValue, &taskPosition)
if err != nil {
t.log.Warn("fail to unmarshal task position", zap.Error(err))
return nil, err
}
taskPositions = append(taskPositions, &taskPosition)
}
return taskPositions, nil
}
func (t *TaskCollectionPositionEtcdStore) Delete(ctx context.Context, metaObj *meta.TaskCollectionPosition, txn any) error {
if metaObj.TaskID == "" {
t.log.Warn("task id is empty")
return errors.New("task id is empty")
}
timeCtx, cancel := context.WithTimeout(ctx, EtcdOpTimeout)
defer cancel()
key := getTaskCollectionPositionPrefixWithTaskID(t.rootPath, metaObj.TaskID)
ops := []clientv3.OpOption{clientv3.WithPrefix()}
if metaObj.CollectionID != 0 {
key = getTaskCollectionPositionKey(t.rootPath, metaObj.TaskID, metaObj.CollectionID)
ops = []clientv3.OpOption{}
}
if txn != nil {
if _, ok := t.txnMap[txn]; !ok {
t.log.Warn("txn not exist")
return errors.New("txn not exist")
}
t.txnMap[txn] = append(t.txnMap[txn], clientv3.OpDelete(key, ops...))
return nil
}
_, err := t.etcdClient.Delete(timeCtx, key, ops...)
if err != nil {
t.log.Warn("fail to delete task position", zap.Error(err))
return err
}
return nil
}
func EtcdStatus(ctx context.Context, etcdClient *clientv3.Client) error {
ctx, cancel := context.WithTimeout(ctx, EtcdOpTimeout)
defer cancel()
for _, endpoint := range etcdClient.Endpoints() {
_, err := etcdClient.Status(ctx, endpoint)
if err != nil {
return err
}
}
return nil
}