Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/etcd: return revision when create key #286

Merged
merged 2 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pkg/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,27 @@ func (e *Client) Close() error {
}

// Create guarantees to set a key = value with some options(like ttl)
func (e *Client) Create(ctx context.Context, key string, val string, opts []clientv3.OpOption) error {
func (e *Client) Create(ctx context.Context, key string, val string, opts []clientv3.OpOption) (int64, error) {
key = keyWithPrefix(e.rootPath, key)
txnResp, err := e.client.KV.Txn(ctx).If(
clientv3.Compare(clientv3.ModRevision(key), "=", 0),
).Then(
clientv3.OpPut(key, val, opts...),
).Commit()
if err != nil {
return errors.Trace(err)
return 0, errors.Trace(err)
}

if !txnResp.Succeeded {
return errors.AlreadyExistsf("key %s in etcd", key)
return 0, errors.AlreadyExistsf("key %s in etcd", key)
}

return nil
if txnResp.Header != nil {
return txnResp.Header.Revision, nil
}

// impossible to happen
return 0, errors.New("revision is unknown")
}

// Get returns a key/value matchs the given key
Expand Down
28 changes: 16 additions & 12 deletions pkg/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (t *testEtcdSuite) TestCreate(c *C) {
c.Assert(err, IsNil)
c.Assert(getResp.Kvs, HasLen, 0)

err = etcdCli.Create(ctx, key, obj, nil)
_, err = etcdCli.Create(ctx, key, obj, nil)
c.Assert(err, IsNil)

getResp, err = etcdClient.KV.Get(ctx, key)
Expand All @@ -68,7 +68,7 @@ func (t *testEtcdSuite) TestCreateWithTTL(c *C) {
c.Assert(err, IsNil)
opts := []clientv3.OpOption{clientv3.WithLease(clientv3.LeaseID(lcr.ID))}

err = etcdCli.Create(ctx, key, obj, opts)
_, err = etcdCli.Create(ctx, key, obj, opts)
c.Assert(err, IsNil)

time.Sleep(2 * time.Second)
Expand All @@ -84,7 +84,7 @@ func (t *testEtcdSuite) TestCreateWithKeyExist(c *C) {
_, err := etcdClient.KV.Put(ctx, key, obj, nil...)
c.Assert(err, IsNil)

err = etcdCli.Create(ctx, key, obj, nil)
_, err = etcdCli.Create(ctx, key, obj, nil)
c.Assert(errors.IsAlreadyExists(err), IsTrue)
}

Expand All @@ -97,12 +97,13 @@ func (t *testEtcdSuite) TestUpdate(c *C) {
c.Assert(err, IsNil)

opts := []clientv3.OpOption{clientv3.WithLease(lcr.ID)}
err = etcdCli.Create(ctx, key, obj1, opts)
revision0, err := etcdCli.Create(ctx, key, obj1, opts)
c.Assert(err, IsNil)

res, revision1, err := etcdCli.Get(ctx, key)
c.Assert(err, IsNil)
c.Assert(string(res), Equals, obj1)
c.Assert(revision0, Equals, revision1)

time.Sleep(time.Second)

Expand Down Expand Up @@ -137,36 +138,39 @@ func (t *testEtcdSuite) TestList(c *C) {
k3 := key + "/level3"
k11 := key + "/level1/level1"

err := etcdCli.Create(ctx, k1, k1, nil)
revision1, err := etcdCli.Create(ctx, k1, k1, nil)
c.Assert(err, IsNil)

err = etcdCli.Create(ctx, k2, k2, nil)
revision2, err := etcdCli.Create(ctx, k2, k2, nil)
c.Assert(err, IsNil)
c.Assert(revision2 > revision1, IsTrue)

err = etcdCli.Create(ctx, k3, k3, nil)
revision3, err := etcdCli.Create(ctx, k3, k3, nil)
c.Assert(err, IsNil)
c.Assert(revision3 > revision2, IsTrue)

err = etcdCli.Create(ctx, k11, k11, nil)
revision4, err := etcdCli.Create(ctx, k11, k11, nil)
c.Assert(err, IsNil)
c.Assert(revision4 > revision3, IsTrue)

root, revision1, err := etcdCli.List(ctx, key)
root, revision5, err := etcdCli.List(ctx, key)
c.Assert(err, IsNil)
c.Assert(string(root.Childs["level1"].Value), Equals, k1)
c.Assert(string(root.Childs["level1"].Childs["level1"].Value), Equals, k11)
c.Assert(string(root.Childs["level2"].Value), Equals, k2)
c.Assert(string(root.Childs["level3"].Value), Equals, k3)

// the revision of list should equal to the latest update's revision
_, revision2, err := etcdCli.Get(ctx, k11)
_, revision6, err := etcdCli.Get(ctx, k11)
c.Assert(err, IsNil)
c.Assert(revision1, Equals, revision2)
c.Assert(revision5, Equals, revision6)
}

func (t *testEtcdSuite) TestDelete(c *C) {
key := "binlogdelete/testkey"
keys := []string{key + "/level1", key + "/level2", key + "/level1" + "/level1"}
for _, k := range keys {
err := etcdCli.Create(ctx, k, k, nil)
_, err := etcdCli.Create(ctx, k, k, nil)
c.Assert(err, IsNil)
}

Expand Down
2 changes: 1 addition & 1 deletion tidb-binlog/node/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (r *EtcdRegistry) createNode(ctx context.Context, prefix string, status *St
return errors.Annotatef(err, "error marshal NodeStatus(%v)", status)
}
key := r.prefixed(prefix, status.NodeID)
err = r.client.Create(ctx, key, string(objstr), nil)
_, err = r.client.Create(ctx, key, string(objstr), nil)
return errors.Trace(err)
}

Expand Down