blob: 20cb34f5c3c98e95ceda5f464e73d94888d24943 [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// Copyright 2016 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package clientv3
16
17import pb "go.etcd.io/etcd/api/v3/etcdserverpb"
18
19type opType int
20
21const (
22 // A default Op has opType 0, which is invalid.
23 tRange opType = iota + 1
24 tPut
25 tDeleteRange
26 tTxn
27)
28
29var noPrefixEnd = []byte{0}
30
31// Op represents an Operation that kv can execute.
32type Op struct {
33 t opType
34 key []byte
35 end []byte
36
37 // for range
38 limit int64
39 sort *SortOption
40 serializable bool
41 keysOnly bool
42 countOnly bool
43 minModRev int64
44 maxModRev int64
45 minCreateRev int64
46 maxCreateRev int64
47
48 // for range, watch
49 rev int64
50
51 // for watch, put, delete
52 prevKV bool
53
54 // for watch
55 // fragmentation should be disabled by default
56 // if true, split watch events when total exceeds
57 // "--max-request-bytes" flag value + 512-byte
58 fragment bool
59
60 // for put
61 ignoreValue bool
62 ignoreLease bool
63
64 // progressNotify is for progress updates.
65 progressNotify bool
66 // createdNotify is for created event
67 createdNotify bool
68 // filters for watchers
69 filterPut bool
70 filterDelete bool
71
72 // for put
73 val []byte
74 leaseID LeaseID
75
76 // txn
77 cmps []Cmp
78 thenOps []Op
79 elseOps []Op
80
81 isOptsWithFromKey bool
82 isOptsWithPrefix bool
83}
84
85// accessors / mutators
86
87// IsTxn returns true if the "Op" type is transaction.
88func (op Op) IsTxn() bool {
89 return op.t == tTxn
90}
91
92// Txn returns the comparison(if) operations, "then" operations, and "else" operations.
93func (op Op) Txn() ([]Cmp, []Op, []Op) {
94 return op.cmps, op.thenOps, op.elseOps
95}
96
97// KeyBytes returns the byte slice holding the Op's key.
98func (op Op) KeyBytes() []byte { return op.key }
99
100// WithKeyBytes sets the byte slice for the Op's key.
101func (op *Op) WithKeyBytes(key []byte) { op.key = key }
102
103// RangeBytes returns the byte slice holding with the Op's range end, if any.
104func (op Op) RangeBytes() []byte { return op.end }
105
106// Rev returns the requested revision, if any.
107func (op Op) Rev() int64 { return op.rev }
108
109// Limit returns limit of the result, if any.
110func (op Op) Limit() int64 { return op.limit }
111
112// IsPut returns true iff the operation is a Put.
113func (op Op) IsPut() bool { return op.t == tPut }
114
115// IsGet returns true iff the operation is a Get.
116func (op Op) IsGet() bool { return op.t == tRange }
117
118// IsDelete returns true iff the operation is a Delete.
119func (op Op) IsDelete() bool { return op.t == tDeleteRange }
120
121// IsSerializable returns true if the serializable field is true.
122func (op Op) IsSerializable() bool { return op.serializable }
123
124// IsKeysOnly returns whether keysOnly is set.
125func (op Op) IsKeysOnly() bool { return op.keysOnly }
126
127// IsCountOnly returns whether countOnly is set.
128func (op Op) IsCountOnly() bool { return op.countOnly }
129
130func (op Op) IsOptsWithFromKey() bool { return op.isOptsWithFromKey }
131
132func (op Op) IsOptsWithPrefix() bool { return op.isOptsWithPrefix }
133
134// MinModRev returns the operation's minimum modify revision.
135func (op Op) MinModRev() int64 { return op.minModRev }
136
137// MaxModRev returns the operation's maximum modify revision.
138func (op Op) MaxModRev() int64 { return op.maxModRev }
139
140// MinCreateRev returns the operation's minimum create revision.
141func (op Op) MinCreateRev() int64 { return op.minCreateRev }
142
143// MaxCreateRev returns the operation's maximum create revision.
144func (op Op) MaxCreateRev() int64 { return op.maxCreateRev }
145
146// WithRangeBytes sets the byte slice for the Op's range end.
147func (op *Op) WithRangeBytes(end []byte) { op.end = end }
148
149// ValueBytes returns the byte slice holding the Op's value, if any.
150func (op Op) ValueBytes() []byte { return op.val }
151
152// WithValueBytes sets the byte slice for the Op's value.
153func (op *Op) WithValueBytes(v []byte) { op.val = v }
154
155func (op Op) toRangeRequest() *pb.RangeRequest {
156 if op.t != tRange {
157 panic("op.t != tRange")
158 }
159 r := &pb.RangeRequest{
160 Key: op.key,
161 RangeEnd: op.end,
162 Limit: op.limit,
163 Revision: op.rev,
164 Serializable: op.serializable,
165 KeysOnly: op.keysOnly,
166 CountOnly: op.countOnly,
167 MinModRevision: op.minModRev,
168 MaxModRevision: op.maxModRev,
169 MinCreateRevision: op.minCreateRev,
170 MaxCreateRevision: op.maxCreateRev,
171 }
172 if op.sort != nil {
173 r.SortOrder = pb.RangeRequest_SortOrder(op.sort.Order)
174 r.SortTarget = pb.RangeRequest_SortTarget(op.sort.Target)
175 }
176 return r
177}
178
179func (op Op) toTxnRequest() *pb.TxnRequest {
180 thenOps := make([]*pb.RequestOp, len(op.thenOps))
181 for i, tOp := range op.thenOps {
182 thenOps[i] = tOp.toRequestOp()
183 }
184 elseOps := make([]*pb.RequestOp, len(op.elseOps))
185 for i, eOp := range op.elseOps {
186 elseOps[i] = eOp.toRequestOp()
187 }
188 cmps := make([]*pb.Compare, len(op.cmps))
189 for i := range op.cmps {
190 cmps[i] = (*pb.Compare)(&op.cmps[i])
191 }
192 return &pb.TxnRequest{Compare: cmps, Success: thenOps, Failure: elseOps}
193}
194
195func (op Op) toRequestOp() *pb.RequestOp {
196 switch op.t {
197 case tRange:
198 return &pb.RequestOp{Request: &pb.RequestOp_RequestRange{RequestRange: op.toRangeRequest()}}
199 case tPut:
200 r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV, IgnoreValue: op.ignoreValue, IgnoreLease: op.ignoreLease}
201 return &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: r}}
202 case tDeleteRange:
203 r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
204 return &pb.RequestOp{Request: &pb.RequestOp_RequestDeleteRange{RequestDeleteRange: r}}
205 case tTxn:
206 return &pb.RequestOp{Request: &pb.RequestOp_RequestTxn{RequestTxn: op.toTxnRequest()}}
207 default:
208 panic("Unknown Op")
209 }
210}
211
212func (op Op) isWrite() bool {
213 if op.t == tTxn {
214 for _, tOp := range op.thenOps {
215 if tOp.isWrite() {
216 return true
217 }
218 }
219 for _, tOp := range op.elseOps {
220 if tOp.isWrite() {
221 return true
222 }
223 }
224 return false
225 }
226 return op.t != tRange
227}
228
229func NewOp() *Op {
230 return &Op{key: []byte("")}
231}
232
233// OpGet returns "get" operation based on given key and operation options.
234func OpGet(key string, opts ...OpOption) Op {
235 // WithPrefix and WithFromKey are not supported together
236 if IsOptsWithPrefix(opts) && IsOptsWithFromKey(opts) {
237 panic("`WithPrefix` and `WithFromKey` cannot be set at the same time, choose one")
238 }
239 ret := Op{t: tRange, key: []byte(key)}
240 ret.applyOpts(opts)
241 return ret
242}
243
244// OpDelete returns "delete" operation based on given key and operation options.
245func OpDelete(key string, opts ...OpOption) Op {
246 // WithPrefix and WithFromKey are not supported together
247 if IsOptsWithPrefix(opts) && IsOptsWithFromKey(opts) {
248 panic("`WithPrefix` and `WithFromKey` cannot be set at the same time, choose one")
249 }
250 ret := Op{t: tDeleteRange, key: []byte(key)}
251 ret.applyOpts(opts)
252 switch {
253 case ret.leaseID != 0:
254 panic("unexpected lease in delete")
255 case ret.limit != 0:
256 panic("unexpected limit in delete")
257 case ret.rev != 0:
258 panic("unexpected revision in delete")
259 case ret.sort != nil:
260 panic("unexpected sort in delete")
261 case ret.serializable:
262 panic("unexpected serializable in delete")
263 case ret.countOnly:
264 panic("unexpected countOnly in delete")
265 case ret.minModRev != 0, ret.maxModRev != 0:
266 panic("unexpected mod revision filter in delete")
267 case ret.minCreateRev != 0, ret.maxCreateRev != 0:
268 panic("unexpected create revision filter in delete")
269 case ret.filterDelete, ret.filterPut:
270 panic("unexpected filter in delete")
271 case ret.createdNotify:
272 panic("unexpected createdNotify in delete")
273 }
274 return ret
275}
276
277// OpPut returns "put" operation based on given key-value and operation options.
278func OpPut(key, val string, opts ...OpOption) Op {
279 ret := Op{t: tPut, key: []byte(key), val: []byte(val)}
280 ret.applyOpts(opts)
281 switch {
282 case ret.end != nil:
283 panic("unexpected range in put")
284 case ret.limit != 0:
285 panic("unexpected limit in put")
286 case ret.rev != 0:
287 panic("unexpected revision in put")
288 case ret.sort != nil:
289 panic("unexpected sort in put")
290 case ret.serializable:
291 panic("unexpected serializable in put")
292 case ret.countOnly:
293 panic("unexpected countOnly in put")
294 case ret.minModRev != 0, ret.maxModRev != 0:
295 panic("unexpected mod revision filter in put")
296 case ret.minCreateRev != 0, ret.maxCreateRev != 0:
297 panic("unexpected create revision filter in put")
298 case ret.filterDelete, ret.filterPut:
299 panic("unexpected filter in put")
300 case ret.createdNotify:
301 panic("unexpected createdNotify in put")
302 }
303 return ret
304}
305
306// OpTxn returns "txn" operation based on given transaction conditions.
307func OpTxn(cmps []Cmp, thenOps []Op, elseOps []Op) Op {
308 return Op{t: tTxn, cmps: cmps, thenOps: thenOps, elseOps: elseOps}
309}
310
311func opWatch(key string, opts ...OpOption) Op {
312 ret := Op{t: tRange, key: []byte(key)}
313 ret.applyOpts(opts)
314 switch {
315 case ret.leaseID != 0:
316 panic("unexpected lease in watch")
317 case ret.limit != 0:
318 panic("unexpected limit in watch")
319 case ret.sort != nil:
320 panic("unexpected sort in watch")
321 case ret.serializable:
322 panic("unexpected serializable in watch")
323 case ret.countOnly:
324 panic("unexpected countOnly in watch")
325 case ret.minModRev != 0, ret.maxModRev != 0:
326 panic("unexpected mod revision filter in watch")
327 case ret.minCreateRev != 0, ret.maxCreateRev != 0:
328 panic("unexpected create revision filter in watch")
329 }
330 return ret
331}
332
333func (op *Op) applyOpts(opts []OpOption) {
334 for _, opt := range opts {
335 opt(op)
336 }
337}
338
339// OpOption configures Operations like Get, Put, Delete.
340type OpOption func(*Op)
341
342// WithLease attaches a lease ID to a key in 'Put' request.
343func WithLease(leaseID LeaseID) OpOption {
344 return func(op *Op) { op.leaseID = leaseID }
345}
346
347// WithLimit limits the number of results to return from 'Get' request.
348// If WithLimit is given a 0 limit, it is treated as no limit.
349func WithLimit(n int64) OpOption { return func(op *Op) { op.limit = n } }
350
351// WithRev specifies the store revision for 'Get' request.
352// Or the start revision of 'Watch' request.
353func WithRev(rev int64) OpOption { return func(op *Op) { op.rev = rev } }
354
355// WithSort specifies the ordering in 'Get' request. It requires
356// 'WithRange' and/or 'WithPrefix' to be specified too.
357// 'target' specifies the target to sort by: key, version, revisions, value.
358// 'order' can be either 'SortNone', 'SortAscend', 'SortDescend'.
359func WithSort(target SortTarget, order SortOrder) OpOption {
360 return func(op *Op) {
361 if target == SortByKey && order == SortAscend {
362 // If order != SortNone, server fetches the entire key-space,
363 // and then applies the sort and limit, if provided.
364 // Since by default the server returns results sorted by keys
365 // in lexicographically ascending order, the client should ignore
366 // SortOrder if the target is SortByKey.
367 order = SortNone
368 }
369 op.sort = &SortOption{target, order}
370 }
371}
372
373// GetPrefixRangeEnd gets the range end of the prefix.
374// 'Get(foo, WithPrefix())' is equal to 'Get(foo, WithRange(GetPrefixRangeEnd(foo))'.
375func GetPrefixRangeEnd(prefix string) string {
376 return string(getPrefix([]byte(prefix)))
377}
378
379func getPrefix(key []byte) []byte {
380 end := make([]byte, len(key))
381 copy(end, key)
382 for i := len(end) - 1; i >= 0; i-- {
383 if end[i] < 0xff {
384 end[i] = end[i] + 1
385 end = end[:i+1]
386 return end
387 }
388 }
389 // next prefix does not exist (e.g., 0xffff);
390 // default to WithFromKey policy
391 return noPrefixEnd
392}
393
394// WithPrefix enables 'Get', 'Delete', or 'Watch' requests to operate
395// on the keys with matching prefix. For example, 'Get(foo, WithPrefix())'
396// can return 'foo1', 'foo2', and so on.
397func WithPrefix() OpOption {
398 return func(op *Op) {
399 op.isOptsWithPrefix = true
400 if len(op.key) == 0 {
401 op.key, op.end = []byte{0}, []byte{0}
402 return
403 }
404 op.end = getPrefix(op.key)
405 }
406}
407
408// WithRange specifies the range of 'Get', 'Delete', 'Watch' requests.
409// For example, 'Get' requests with 'WithRange(end)' returns
410// the keys in the range [key, end).
411// endKey must be lexicographically greater than start key.
412func WithRange(endKey string) OpOption {
413 return func(op *Op) { op.end = []byte(endKey) }
414}
415
416// WithFromKey specifies the range of 'Get', 'Delete', 'Watch' requests
417// to be equal or greater than the key in the argument.
418func WithFromKey() OpOption {
419 return func(op *Op) {
420 if len(op.key) == 0 {
421 op.key = []byte{0}
422 }
423 op.end = []byte("\x00")
424 op.isOptsWithFromKey = true
425 }
426}
427
428// WithSerializable makes `Get` and `MemberList` requests serializable.
429// By default, they are linearizable. Serializable requests are better
430// for lower latency requirement, but users should be aware that they
431// could get stale data with serializable requests.
432//
433// In some situations users may want to use serializable requests. For
434// example, when adding a new member to a one-node cluster, it's reasonable
435// and safe to use serializable request before the new added member gets
436// started.
437func WithSerializable() OpOption {
438 return func(op *Op) { op.serializable = true }
439}
440
441// WithKeysOnly makes the 'Get' request return only the keys and the corresponding
442// values will be omitted.
443func WithKeysOnly() OpOption {
444 return func(op *Op) { op.keysOnly = true }
445}
446
447// WithCountOnly makes the 'Get' request return only the count of keys.
448func WithCountOnly() OpOption {
449 return func(op *Op) { op.countOnly = true }
450}
451
452// WithMinModRev filters out keys for Get with modification revisions less than the given revision.
453func WithMinModRev(rev int64) OpOption { return func(op *Op) { op.minModRev = rev } }
454
455// WithMaxModRev filters out keys for Get with modification revisions greater than the given revision.
456func WithMaxModRev(rev int64) OpOption { return func(op *Op) { op.maxModRev = rev } }
457
458// WithMinCreateRev filters out keys for Get with creation revisions less than the given revision.
459func WithMinCreateRev(rev int64) OpOption { return func(op *Op) { op.minCreateRev = rev } }
460
461// WithMaxCreateRev filters out keys for Get with creation revisions greater than the given revision.
462func WithMaxCreateRev(rev int64) OpOption { return func(op *Op) { op.maxCreateRev = rev } }
463
464// WithFirstCreate gets the key with the oldest creation revision in the request range.
465func WithFirstCreate() []OpOption { return withTop(SortByCreateRevision, SortAscend) }
466
467// WithLastCreate gets the key with the latest creation revision in the request range.
468func WithLastCreate() []OpOption { return withTop(SortByCreateRevision, SortDescend) }
469
470// WithFirstKey gets the lexically first key in the request range.
471func WithFirstKey() []OpOption { return withTop(SortByKey, SortAscend) }
472
473// WithLastKey gets the lexically last key in the request range.
474func WithLastKey() []OpOption { return withTop(SortByKey, SortDescend) }
475
476// WithFirstRev gets the key with the oldest modification revision in the request range.
477func WithFirstRev() []OpOption { return withTop(SortByModRevision, SortAscend) }
478
479// WithLastRev gets the key with the latest modification revision in the request range.
480func WithLastRev() []OpOption { return withTop(SortByModRevision, SortDescend) }
481
482// withTop gets the first key over the get's prefix given a sort order
483func withTop(target SortTarget, order SortOrder) []OpOption {
484 return []OpOption{WithPrefix(), WithSort(target, order), WithLimit(1)}
485}
486
487// WithProgressNotify makes watch server send periodic progress updates
488// every 10 minutes when there is no incoming events.
489// Progress updates have zero events in WatchResponse.
490func WithProgressNotify() OpOption {
491 return func(op *Op) {
492 op.progressNotify = true
493 }
494}
495
496// WithCreatedNotify makes watch server sends the created event.
497func WithCreatedNotify() OpOption {
498 return func(op *Op) {
499 op.createdNotify = true
500 }
501}
502
503// WithFilterPut discards PUT events from the watcher.
504func WithFilterPut() OpOption {
505 return func(op *Op) { op.filterPut = true }
506}
507
508// WithFilterDelete discards DELETE events from the watcher.
509func WithFilterDelete() OpOption {
510 return func(op *Op) { op.filterDelete = true }
511}
512
513// WithPrevKV gets the previous key-value pair before the event happens. If the previous KV is already compacted,
514// nothing will be returned.
515func WithPrevKV() OpOption {
516 return func(op *Op) {
517 op.prevKV = true
518 }
519}
520
521// WithFragment to receive raw watch response with fragmentation.
522// Fragmentation is disabled by default. If fragmentation is enabled,
523// etcd watch server will split watch response before sending to clients
524// when the total size of watch events exceed server-side request limit.
525// The default server-side request limit is 1.5 MiB, which can be configured
526// as "--max-request-bytes" flag value + gRPC-overhead 512 bytes.
527// See "etcdserver/api/v3rpc/watch.go" for more details.
528func WithFragment() OpOption {
529 return func(op *Op) { op.fragment = true }
530}
531
532// WithIgnoreValue updates the key using its current value.
533// This option can not be combined with non-empty values.
534// Returns an error if the key does not exist.
535func WithIgnoreValue() OpOption {
536 return func(op *Op) {
537 op.ignoreValue = true
538 }
539}
540
541// WithIgnoreLease updates the key using its current lease.
542// This option can not be combined with WithLease.
543// Returns an error if the key does not exist.
544func WithIgnoreLease() OpOption {
545 return func(op *Op) {
546 op.ignoreLease = true
547 }
548}
549
550// LeaseOp represents an Operation that lease can execute.
551type LeaseOp struct {
552 id LeaseID
553
554 // for TimeToLive
555 attachedKeys bool
556}
557
558// LeaseOption configures lease operations.
559type LeaseOption func(*LeaseOp)
560
561func (op *LeaseOp) applyOpts(opts []LeaseOption) {
562 for _, opt := range opts {
563 opt(op)
564 }
565}
566
567// WithAttachedKeys makes TimeToLive list the keys attached to the given lease ID.
568func WithAttachedKeys() LeaseOption {
569 return func(op *LeaseOp) { op.attachedKeys = true }
570}
571
572func toLeaseTimeToLiveRequest(id LeaseID, opts ...LeaseOption) *pb.LeaseTimeToLiveRequest {
573 ret := &LeaseOp{id: id}
574 ret.applyOpts(opts)
575 return &pb.LeaseTimeToLiveRequest{ID: int64(id), Keys: ret.attachedKeys}
576}
577
578// IsOptsWithPrefix returns true if WithPrefix option is called in the given opts.
579func IsOptsWithPrefix(opts []OpOption) bool {
580 ret := NewOp()
581 for _, opt := range opts {
582 opt(ret)
583 }
584
585 return ret.isOptsWithPrefix
586}
587
588// IsOptsWithFromKey returns true if WithFromKey option is called in the given opts.
589func IsOptsWithFromKey(opts []OpOption) bool {
590 ret := NewOp()
591 for _, opt := range opts {
592 opt(ret)
593 }
594
595 return ret.isOptsWithFromKey
596}
597
598func (op Op) IsSortOptionValid() bool {
599 if op.sort != nil {
600 sortOrder := int32(op.sort.Order)
601 sortTarget := int32(op.sort.Target)
602
603 if _, ok := pb.RangeRequest_SortOrder_name[sortOrder]; !ok {
604 return false
605 }
606
607 if _, ok := pb.RangeRequest_SortTarget_name[sortTarget]; !ok {
608 return false
609 }
610 }
611 return true
612}