Skip to content

Commit

Permalink
kvstore: replace custom createOpPut method with upstream client.OpPut
Browse files Browse the repository at this point in the history
The client.WithLease(0) option is a no op, because 0 is the default
value for the LeaseID. Hence, let's get rid of the custom createOpPut
method, and always use client.OpPut regardless of whether a lease is
set. This brings also consistency with the other Put operations.

Signed-off-by: Marco Iorio <[email protected]>
  • Loading branch information
giorio94 authored and jibi committed Jan 8, 2024
1 parent 6e4e972 commit 5320719
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions pkg/kvstore/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,16 +1190,6 @@ func (e *etcdClient) Delete(ctx context.Context, key string) (err error) {
return Hint(err)
}

func (e *etcdClient) createOpPut(key string, value []byte, leaseID client.LeaseID) *client.Op {
if leaseID != 0 {
op := client.OpPut(key, string(value), client.WithLease(leaseID))
return &op
}

op := client.OpPut(key, string(value))
return &op
}

// UpdateIfLocked updates a key if the client is still holding the given lock.
func (e *etcdClient) UpdateIfLocked(ctx context.Context, key string, value []byte, lease bool, lock KVLocker) (err error) {
defer func() {
Expand Down Expand Up @@ -1353,7 +1343,7 @@ func (e *etcdClient) CreateOnlyIfLocked(ctx context.Context, key string, value [
}
duration := spanstat.Start()

req := e.createOpPut(key, value, leaseID)
req := client.OpPut(key, string(value), client.WithLease(leaseID))
cnds := []client.Cmp{
client.Compare(client.Version(key), "=", 0),
lock.Comparator().(client.Cmp),
Expand All @@ -1364,7 +1354,7 @@ func (e *etcdClient) CreateOnlyIfLocked(ctx context.Context, key string, value [
opGets := []client.Op{
client.OpGet(key),
}
txnresp, err := e.client.Txn(ctx).If(cnds...).Then(*req).Else(opGets...).Commit()
txnresp, err := e.client.Txn(ctx).If(cnds...).Then(req).Else(opGets...).Commit()
increaseMetric(key, metricSet, "CreateOnlyLocked", duration.EndError(err).Total(), err)
if err != nil {
lr.Error(err)
Expand Down Expand Up @@ -1420,10 +1410,10 @@ func (e *etcdClient) CreateOnly(ctx context.Context, key string, value []byte, l
increaseMetric(key, metricSet, "CreateOnly", duration.EndError(err).Total(), err)
}(spanstat.Start())

req := e.createOpPut(key, value, leaseID)
req := client.OpPut(key, string(value), client.WithLease(leaseID))
cond := client.Compare(client.Version(key), "=", 0)

txnresp, err := e.client.Txn(ctx).If(cond).Then(*req).Commit()
txnresp, err := e.client.Txn(ctx).If(cond).Then(req).Commit()

if err != nil {
lr.Error(err)
Expand Down

0 comments on commit 5320719

Please sign in to comment.