forked from pingcap/tiflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ha(dm): support store task config in Etcd (pingcap#3620)
- Loading branch information
Showing
8 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ha | ||
|
||
import ( | ||
"context" | ||
|
||
"go.etcd.io/etcd/clientv3" | ||
"go.etcd.io/etcd/clientv3/clientv3util" | ||
|
||
"github.com/pingcap/ticdc/dm/dm/common" | ||
"github.com/pingcap/ticdc/dm/openapi" | ||
"github.com/pingcap/ticdc/dm/pkg/etcdutil" | ||
"github.com/pingcap/ticdc/dm/pkg/terror" | ||
) | ||
|
||
func openAPITaskFromResp(resp *clientv3.GetResponse) (*openapi.Task, error) { | ||
task := &openapi.Task{} | ||
if resp.Count == 0 { | ||
return nil, nil | ||
} else if resp.Count > 1 { | ||
// this should not happen. | ||
return task, terror.ErrConfigMoreThanOne.Generate(resp.Count, "openapi.Task", "") | ||
} | ||
// we make sure only have one task config. | ||
if err := task.FromJSON(resp.Kvs[0].Value); err != nil { | ||
return task, err | ||
} | ||
return task, nil | ||
} | ||
|
||
// PutOpenAPITaskConfig puts the openapi task config of task-name. | ||
func PutOpenAPITaskConfig(cli *clientv3.Client, task openapi.Task, overWrite bool) error { | ||
ctx, cancel := context.WithTimeout(cli.Ctx(), etcdutil.DefaultRequestTimeout) | ||
defer cancel() | ||
|
||
key := common.OpenAPITaskConfigKeyAdapter.Encode(task.Name) | ||
taskJSON, err := task.ToJSON() | ||
if err != nil { | ||
return err // it should not happen. | ||
} | ||
txn := cli.Txn(ctx) | ||
if !overWrite { | ||
txn = txn.If(clientv3util.KeyMissing(key)) | ||
} | ||
resp, err := txn.Then(clientv3.OpPut(key, string(taskJSON))).Commit() | ||
if err != nil { | ||
return err | ||
} | ||
// user don't want to overwrite and key already exists. | ||
if !overWrite && !resp.Succeeded { | ||
return terror.ErrOpenAPITaskConfigExist.Generate(task.Name) | ||
} | ||
return nil | ||
} | ||
|
||
// UpdateOpenAPITaskConfig updates the openapi task config by task-name. | ||
func UpdateOpenAPITaskConfig(cli *clientv3.Client, task openapi.Task) error { | ||
ctx, cancel := context.WithTimeout(cli.Ctx(), etcdutil.DefaultRequestTimeout) | ||
defer cancel() | ||
|
||
key := common.OpenAPITaskConfigKeyAdapter.Encode(task.Name) | ||
taskJSON, err := task.ToJSON() | ||
if err != nil { | ||
return err // it should not happen. | ||
} | ||
txn := cli.Txn(ctx).If(clientv3util.KeyExists(key)).Then(clientv3.OpPut(key, string(taskJSON))) | ||
resp, err := txn.Commit() | ||
if err != nil { | ||
return err | ||
} | ||
// user want to update a key not exists. | ||
if !resp.Succeeded { | ||
return terror.ErrOpenAPITaskConfigNotExist.Generate(task.Name) | ||
} | ||
return nil | ||
} | ||
|
||
// DeleteOpenAPITaskConfig deletes the openapi task config of task-name. | ||
func DeleteOpenAPITaskConfig(cli *clientv3.Client, taskName string) error { | ||
ctx, cancel := context.WithTimeout(cli.Ctx(), etcdutil.DefaultRequestTimeout) | ||
defer cancel() | ||
if _, err := cli.Delete(ctx, common.OpenAPITaskConfigKeyAdapter.Encode(taskName)); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// GetOpenAPITaskConfig gets the openapi task config of task-name. | ||
func GetOpenAPITaskConfig(cli *clientv3.Client, taskName string) (*openapi.Task, error) { | ||
ctx, cancel := context.WithTimeout(cli.Ctx(), etcdutil.DefaultRequestTimeout) | ||
defer cancel() | ||
|
||
var ( | ||
task *openapi.Task | ||
resp *clientv3.GetResponse | ||
err error | ||
) | ||
resp, err = cli.Get(ctx, common.OpenAPITaskConfigKeyAdapter.Encode(taskName)) | ||
if err != nil { | ||
return task, err | ||
} | ||
return openAPITaskFromResp(resp) | ||
} | ||
|
||
// GetAllOpenAPITaskConfig gets all openapi task config s. | ||
func GetAllOpenAPITaskConfig(cli *clientv3.Client) ([]*openapi.Task, error) { | ||
ctx, cancel := context.WithTimeout(cli.Ctx(), etcdutil.DefaultRequestTimeout) | ||
defer cancel() | ||
|
||
resp, err := cli.Get(ctx, common.OpenAPITaskConfigKeyAdapter.Path(), clientv3.WithPrefix()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tasks := make([]*openapi.Task, resp.Count) | ||
for i, kv := range resp.Kvs { | ||
t := &openapi.Task{} | ||
if err := t.FromJSON(kv.Value); err != nil { | ||
return nil, err | ||
} | ||
tasks[i] = t | ||
} | ||
return tasks, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ha | ||
|
||
import ( | ||
. "github.com/pingcap/check" | ||
|
||
"github.com/pingcap/ticdc/dm/openapi" | ||
"github.com/pingcap/ticdc/dm/openapi/fixtures" | ||
"github.com/pingcap/ticdc/dm/pkg/terror" | ||
) | ||
|
||
func (t *testForEtcd) TestOpenAPITaskConfigEtcd(c *C) { | ||
defer clearTestInfoOperation(c) | ||
|
||
task1, err := fixtures.GenNoShardOpenAPITaskForTest() | ||
task1.Name = "test-1" | ||
c.Assert(err, IsNil) | ||
task2, err := fixtures.GenShardAndFilterOpenAPITaskForTest() | ||
task2.Name = "test-2" | ||
c.Assert(err, IsNil) | ||
|
||
// no openapi task config exist. | ||
task1InEtcd, err := GetOpenAPITaskConfig(etcdTestCli, task1.Name) | ||
c.Assert(err, IsNil) | ||
c.Assert(task1InEtcd, IsNil) | ||
|
||
task2InEtcd, err := GetOpenAPITaskConfig(etcdTestCli, task2.Name) | ||
c.Assert(err, IsNil) | ||
c.Assert(task2InEtcd, IsNil) | ||
|
||
tasks, err := GetAllOpenAPITaskConfig(etcdTestCli) | ||
c.Assert(err, IsNil) | ||
c.Assert(tasks, HasLen, 0) | ||
|
||
// put openapi task config . | ||
c.Assert(PutOpenAPITaskConfig(etcdTestCli, task1, false), IsNil) | ||
c.Assert(PutOpenAPITaskConfig(etcdTestCli, task2, false), IsNil) | ||
|
||
task1InEtcd, err = GetOpenAPITaskConfig(etcdTestCli, task1.Name) | ||
c.Assert(err, IsNil) | ||
c.Assert(*task1InEtcd, DeepEquals, task1) | ||
|
||
task2InEtcd, err = GetOpenAPITaskConfig(etcdTestCli, task2.Name) | ||
c.Assert(err, IsNil) | ||
c.Assert(*task2InEtcd, DeepEquals, task2) | ||
|
||
tasks, err = GetAllOpenAPITaskConfig(etcdTestCli) | ||
c.Assert(err, IsNil) | ||
c.Assert(tasks, HasLen, 2) | ||
|
||
// put openapi task config again without overwrite will fail | ||
c.Assert(terror.ErrOpenAPITaskConfigExist.Equal(PutOpenAPITaskConfig(etcdTestCli, task1, false)), IsTrue) | ||
|
||
// in overwrite mode, it will overwrite the old one. | ||
task1.TaskMode = openapi.TaskTaskModeFull | ||
c.Assert(PutOpenAPITaskConfig(etcdTestCli, task1, true), IsNil) | ||
task1InEtcd, err = GetOpenAPITaskConfig(etcdTestCli, task1.Name) | ||
c.Assert(err, IsNil) | ||
c.Assert(*task1InEtcd, DeepEquals, task1) | ||
|
||
// put task config that not exist will fail | ||
task3, err := fixtures.GenNoShardOpenAPITaskForTest() | ||
c.Assert(err, IsNil) | ||
task3.Name = "test-3" | ||
c.Assert(terror.ErrOpenAPITaskConfigNotExist.Equal(UpdateOpenAPITaskConfig(etcdTestCli, task3)), IsTrue) | ||
|
||
// update exist openapi task config will success | ||
task1.TaskMode = openapi.TaskTaskModeAll | ||
c.Assert(UpdateOpenAPITaskConfig(etcdTestCli, task1), IsNil) | ||
task1InEtcd, err = GetOpenAPITaskConfig(etcdTestCli, task1.Name) | ||
c.Assert(err, IsNil) | ||
c.Assert(*task1InEtcd, DeepEquals, task1) | ||
|
||
// delete task config | ||
c.Assert(DeleteOpenAPITaskConfig(etcdTestCli, task1.Name), IsNil) | ||
tasks, err = GetAllOpenAPITaskConfig(etcdTestCli) | ||
c.Assert(err, IsNil) | ||
c.Assert(tasks, HasLen, 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters