/
plan.go
377 lines (328 loc) · 10.2 KB
/
plan.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) 2018 Huawei Technologies Co., Ltd. All Rights Reserved.
//
// Licensed 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 plan
import (
"errors"
"regexp"
"time"
"encoding/json"
"github.com/globalsign/mgo/bson"
"github.com/micro/go-log"
c "github.com/opensds/multi-cloud/api/pkg/filters/context"
"github.com/opensds/multi-cloud/dataflow/pkg/db"
"github.com/opensds/multi-cloud/dataflow/pkg/kafka"
. "github.com/opensds/multi-cloud/dataflow/pkg/model"
"github.com/opensds/multi-cloud/dataflow/pkg/scheduler/trigger"
"github.com/opensds/multi-cloud/datamover/proto"
)
var tblConnector = "connector"
var tblPolicy = "policy"
var topicMigration = "migration"
const (
BIT_REGION = 1 //00001
BIT_ENDPOINT = 2 //00010
BIT_BUCKETNAME = 4 //00100
BIT_ACCESS = 8 //01000
BIT_SECURITY = 16 //10000
BIT_FULL = 31 //11111
)
func isEqual(src *Connector, dest *Connector) bool {
switch src.StorType {
case STOR_TYPE_OPENSDS:
if dest.StorType == STOR_TYPE_OPENSDS && src.BucketName == dest.BucketName {
return true
} else {
return false
}
default: //TODO: check according to StorType later.
return false
}
}
func checkConnValidation(conn *Connector) error {
var flag int
if conn.StorType == STOR_TYPE_OPENSDS {
//If StorType is opensds-obj, no connector needed.
return nil
}
if conn.StorType == STOR_TYPE_CEPH_S3 {
flag = 1 //region is not a required param for ceph
} else {
flag = 0
}
cfg := conn.ConnConfig
for i := 0; i < len(cfg); i++ {
switch cfg[i].Key {
case "region":
flag = flag | BIT_REGION
case "endpoint":
flag = flag | BIT_ENDPOINT
case "bucketname":
flag = flag | BIT_BUCKETNAME
case "access":
flag = flag | BIT_ACCESS
case "security":
flag = flag | BIT_SECURITY
default:
log.Logf("Uknow key[%s] for connector.\n", cfg[i].Key)
}
}
if flag != BIT_FULL {
log.Logf("Invalid connector, flag=%b\n", flag)
return errors.New("Invalid connector")
}
return nil
}
func Create(ctx *c.Context, plan *Plan) (*Plan, error) {
//Check parameter validity
m, err := regexp.MatchString("[[:alnum:]-_.]+", plan.Name)
if !m {
log.Logf("Invalid plan name[%s], err:%v", plan.Name, err)
return nil, ERR_INVALID_PLAN_NAME
}
plan.Id = ""
plan.LastSchedTime = 0 //set to be 0 as default
if isEqual(&plan.SourceConn, &plan.DestConn) {
log.Log("source connector is the same as destination connector.")
return nil, ERR_DEST_SRC_CONN_EQUAL
}
if checkConnValidation(&plan.SourceConn) != nil {
log.Logf("Source connector is invalid, type=%s\n", plan.SourceConn.StorType)
return nil, ERR_SRC_CONN_NOT_EXIST
}
if checkConnValidation(&plan.DestConn) != nil {
log.Logf("Target connector is invalid, type=%s\n", plan.DestConn.StorType)
return nil, ERR_DEST_CONN_NOT_EXIST
}
//Add to database
plan, err = db.DbAdapter.CreatePlan(ctx, plan)
if err != nil {
log.Logf("create plan in db failed,%v", err)
return nil, err
}
if plan.PolicyId != "" && plan.PolicyEnabled {
if err := trigger.GetTriggerMgr().Add(ctx, plan, NewPlanExecutor(ctx, plan)); err != nil {
log.Logf("Add plan(%s) to trigger failed, %v", plan.Id.Hex(), err)
return nil, err
}
}
return plan, nil
}
func Delete(ctx *c.Context, id string) error {
plan, err := db.DbAdapter.GetPlan(ctx, id)
if err == ERR_PLAN_NOT_EXIST {
log.Logf("specified plan(%s) is not exist, ignore it ", id)
return nil
}
if err != nil {
log.Logf("Delete plan failed, %v", err)
return err
}
if plan.PolicyId != "" {
err = trigger.GetTriggerMgr().Remove(ctx, plan)
if err != nil && err != ERR_PLAN_NOT_EXIST {
log.Logf("Remove plan from triggers failed, %v", err)
return err
}
}
return db.DbAdapter.DeletePlan(ctx, id)
}
//1. cannot update type
func Update(ctx *c.Context, planId string, updateMap map[string]interface{}) (*Plan, error) {
curPlan, err := db.DbAdapter.GetPlan(ctx, planId)
if err != nil {
log.Logf("Update plan failed, err: can not get the plan(%v).\n", err.Error())
return nil, err
}
if v, ok := updateMap["name"]; ok {
name := v.(string)
m, err := regexp.MatchString("[[:alnum:]-_.]+", name)
if !m {
log.Logf("Invalid plan name[%s],err:", name, err) //cannot use all as name
return nil, ERR_INVALID_PLAN_NAME
}
curPlan.Name = name
}
if v, ok := updateMap["sourceConn"]; ok {
b, _ := json.Marshal(v)
curPlan.SourceConn = Connector{}
json.Unmarshal(b, &curPlan.SourceConn)
}
if v, ok := updateMap["destConn"]; ok {
b, _ := json.Marshal(v)
curPlan.DestConn = Connector{}
json.Unmarshal(b, &curPlan.DestConn)
}
if v, ok := updateMap["filter"]; ok {
b, _ := json.Marshal(v)
curPlan.Filter = Filter{Prefix: "/"}
json.Unmarshal(b, &curPlan.Filter)
}
if isEqual(&curPlan.SourceConn, &curPlan.DestConn) {
log.Log("source connector is the same as destination connector.")
return nil, ERR_DEST_SRC_CONN_EQUAL
}
var needUpdateTrigger = false
if v, ok := updateMap["policyEnabled"]; ok {
curPlan.PolicyEnabled = v.(bool)
needUpdateTrigger = true
}
if v, ok := updateMap["policyId"]; ok {
curPlan.PolicyId = v.(string)
needUpdateTrigger = true
}
if v, ok := updateMap["description"]; ok {
curPlan.Description = v.(string)
}
if v, ok := updateMap["remainSource"]; ok {
curPlan.RemainSource = v.(bool)
}
if needUpdateTrigger {
trigger.GetTriggerMgr().Remove(ctx, curPlan)
if curPlan.PolicyId != "" && curPlan.PolicyEnabled {
if err := trigger.GetTriggerMgr().Add(ctx, curPlan, NewPlanExecutor(ctx, curPlan)); err != nil {
log.Logf("Add plan(%s) to trigger failed, %v", curPlan.Id.Hex(), err)
return nil, err
}
}
}
return db.DbAdapter.UpdatePlan(ctx, curPlan)
}
func Get(ctx *c.Context, id string) (*Plan, error) {
return db.DbAdapter.GetPlan(ctx, id)
}
func List(ctx *c.Context, limit int, offset int, filter interface{}) ([]Plan, error) {
return db.DbAdapter.ListPlan(ctx, limit, offset, filter)
}
func getLocation(conn *Connector) (string, error) {
switch conn.StorType {
case STOR_TYPE_OPENSDS:
return conn.BucketName, nil
case STOR_TYPE_HW_OBS, STOR_TYPE_AWS_S3, STOR_TYPE_HW_FUSIONSTORAGE, STOR_TYPE_HW_FUSIONCLOUD,
STOR_TYPE_AZURE_BLOB, STOR_TYPE_CEPH_S3, STOR_TYPE_GCP_S3:
cfg := conn.ConnConfig
for i := 0; i < len(cfg); i++ {
if cfg[i].Key == "bucketname" {
return cfg[i].Value, nil
}
}
return "", errors.New("No bucket provided for sefldefine connector.")
default:
log.Logf("Unsupport cnnector type:%v, return ERR_INNER_ERR\n", conn.StorType)
return "", ERR_INNER_ERR
}
}
func sendJob(req *datamover.RunJobRequest) error {
data, err := json.Marshal(*req)
if err != nil {
log.Logf("Marshal run job request failed, err:%v\n", data)
return err
}
return kafka.ProduceMsg(topicMigration, data)
}
func buildConn(reqConn *datamover.Connector, conn *Connector) {
if conn.StorType == STOR_TYPE_OPENSDS {
reqConn.BucketName = conn.BucketName
} else {
for i := 0; i < len(conn.ConnConfig); i++ {
reqConn.ConnConfig = append(reqConn.ConnConfig, &datamover.KV{Key: conn.ConnConfig[i].Key, Value: conn.ConnConfig[i].Value})
}
}
}
func Run(ctx *c.Context, id string) (bson.ObjectId, error) {
//Get information from database
plan, err := db.DbAdapter.GetPlan(ctx, id)
if err != nil {
return "", err
}
//scheduling must be mutual excluded among several schedulers
//Get Lock
ret := db.DbAdapter.LockSched(string(plan.Id.Hex()))
for i := 0; i < 3; i++ {
if ret == LockSuccess {
//Make sure unlock before return
defer db.DbAdapter.UnlockSched(string(plan.Id.Hex()))
break
} else if ret == LockBusy {
return "", ERR_RUN_PLAN_BUSY
} else {
//Try to lock again, try three times at most
ret = db.DbAdapter.LockSched(string(plan.Id.Hex()))
}
}
//Get source location by source connector
srcLocation, err1 := getLocation(&plan.SourceConn)
if err1 != nil {
log.Logf("Run plan failed, invalid source connector, type=%s\n", plan.SourceConn.StorType)
return "", err1
}
//Get destination location by destination connector
destLocation, err2 := getLocation(&plan.DestConn)
if err2 != nil {
log.Logf("Run plan failed, invalid target connector, type=%s\n", plan.DestConn.StorType)
return "", err2
}
ct := time.Now()
//Create job
job := Job{}
//obId := bson.NewObjectId()
//job.Id = jobId
job.Type = plan.Type
job.PlanId = string(plan.Id.Hex())
job.PlanName = plan.Name
job.SourceLocation = srcLocation
job.DestLocation = destLocation
job.CreateTime = ct
job.Status = JOB_STATUS_PENDING
job.RemainSource = plan.RemainSource
job.StartTime = time.Time{}
//add job to database
_, err = db.DbAdapter.CreateJob(ctx, &job)
if err == nil {
//TODO: change to send job to datamover by kafka
//This way send job is the temporary
filt := datamover.Filter{Prefix: plan.Filter.Prefix}
req := datamover.RunJobRequest{Id: job.Id.Hex(), RemainSource: plan.RemainSource, Filt: &filt}
srcConn := datamover.Connector{Type: plan.SourceConn.StorType}
buildConn(&srcConn, &plan.SourceConn)
req.SourceConn = &srcConn
destConn := datamover.Connector{Type: plan.DestConn.StorType}
buildConn(&destConn, &plan.DestConn)
req.DestConn = &destConn
req.RemainSource = job.RemainSource
go sendJob(&req)
} else {
log.Logf("Add job[id=%s,plan=%s,source_location=%s,dest_location=%s] to database failed.\n", string(job.Id.Hex()),
job.PlanName, job.SourceLocation, job.DestLocation)
}
return job.Id, nil
}
type TriggerExecutor struct {
planId string
tenantId string
ctx *c.Context
}
func NewPlanExecutor(ctx *c.Context, plan *Plan) trigger.Executer {
return &TriggerExecutor{
planId: plan.Id.Hex(),
tenantId: plan.TenantId,
ctx: ctx,
}
}
func (p *TriggerExecutor) Run() {
log.Logf("schudler run plan (%s) is called in dataflow service.", p.planId)
jobId, err := Run(p.ctx, p.planId)
if err != nil {
log.Logf("PlanExcutor run plan(%s) error, jobid:%s, error:%v", p.planId, jobId, err)
}
}