client-go icon indicating copy to clipboard operation
client-go copied to clipboard

`CompareAndSwap` will erase the expiration time of the key.

Open wenlive opened this issue 3 years ago • 3 comments

    In go-client V2, the function `CompareAndSwap` will erase the expiration time of the key.

E.g

rawClient.PutWithTTL key1 value1 100
//GetKeyTTL key1 ->*ttl ==100
oldValue :=rawClient.Get key1
previousValue, ok, err := rawClient.CompareAndSwap key1 oldValue newValue
//GetKeyTTL key1 ->*ttl == 0

As far as I know, tikv splices ttl in value and uses it as a compaction filter to delete the key after the life cycle. In tikv go-client, CompareAndSwap does not specify Ttl

        reqArgs := kvrpcpb.RawCASRequest{
		Key:   key,
		Value: newValue,
	}
	if previousValue == nil {
		reqArgs.PreviousNotExist = true
	} else {
		reqArgs.PreviousValue = previousValue
	}

	req := tikvrpc.NewRequest(tikvrpc.CmdRawCompareAndSwap, &reqArgs)
	req.MaxExecutionDurationMs = uint64(client.MaxWriteExecutionTime.Milliseconds())
	resp, _, err := c.sendReq(ctx, key, req, false)

Can I add new ttl to RawCASRequest? Also what can be done to update the value using CAS and keep the historical ttl?@iosmanthus

Originally posted by @wenlive in https://github.com/tikv/client-go/issues/371#issuecomment-1277105165

wenlive avatar Oct 13 '22 08:10 wenlive

RawCASRequest has a TTL parameter, this should be a bug, the new TTL should be attached to the request while the key is put with a TTL. Any bugfix pull requests are welcomed! @wenlive

iosmanthus avatar Oct 26 '22 09:10 iosmanthus

If i'm understanding it correctly, the fix should be introduced in tikv-server side.

disksing avatar Oct 26 '22 09:10 disksing

To keep the "historical ttl", I think it would better to add an argument (e.g., KeepPreviousTtl) to indicate such request, for backward compatibility.

pingyu avatar Oct 26 '22 10:10 pingyu