op.go 18 KB

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