store.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. // Copyright 2017 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 v2v3
  15. import (
  16. "context"
  17. "fmt"
  18. "path"
  19. "strings"
  20. "time"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/clientv3/concurrency"
  23. etcdErr "github.com/coreos/etcd/error"
  24. "github.com/coreos/etcd/internal/mvcc/mvccpb"
  25. "github.com/coreos/etcd/internal/store"
  26. )
  27. // store implements the Store interface for V2 using
  28. // a v3 client.
  29. type v2v3Store struct {
  30. c *clientv3.Client
  31. // pfx is the v3 prefix where keys should be stored.
  32. pfx string
  33. ctx context.Context
  34. }
  35. const maxPathDepth = 63
  36. var errUnsupported = fmt.Errorf("TTLs are unsupported")
  37. func NewStore(c *clientv3.Client, pfx string) store.Store { return newStore(c, pfx) }
  38. func newStore(c *clientv3.Client, pfx string) *v2v3Store { return &v2v3Store{c, pfx, c.Ctx()} }
  39. func (s *v2v3Store) Index() uint64 { panic("STUB") }
  40. func (s *v2v3Store) Get(nodePath string, recursive, sorted bool) (*store.Event, error) {
  41. key := s.mkPath(nodePath)
  42. resp, err := s.c.Txn(s.ctx).Then(
  43. clientv3.OpGet(key+"/"),
  44. clientv3.OpGet(key),
  45. ).Commit()
  46. if err != nil {
  47. return nil, err
  48. }
  49. if kvs := resp.Responses[0].GetResponseRange().Kvs; len(kvs) != 0 || isRoot(nodePath) {
  50. nodes, err := s.getDir(nodePath, recursive, sorted, resp.Header.Revision)
  51. if err != nil {
  52. return nil, err
  53. }
  54. cidx, midx := uint64(0), uint64(0)
  55. if len(kvs) > 0 {
  56. cidx, midx = mkV2Rev(kvs[0].CreateRevision), mkV2Rev(kvs[0].ModRevision)
  57. }
  58. return &store.Event{
  59. Action: store.Get,
  60. Node: &store.NodeExtern{
  61. Key: nodePath,
  62. Dir: true,
  63. Nodes: nodes,
  64. CreatedIndex: cidx,
  65. ModifiedIndex: midx,
  66. },
  67. EtcdIndex: mkV2Rev(resp.Header.Revision),
  68. }, nil
  69. }
  70. kvs := resp.Responses[1].GetResponseRange().Kvs
  71. if len(kvs) == 0 {
  72. return nil, etcdErr.NewError(etcdErr.EcodeKeyNotFound, nodePath, mkV2Rev(resp.Header.Revision))
  73. }
  74. return &store.Event{
  75. Action: store.Get,
  76. Node: s.mkV2Node(kvs[0]),
  77. EtcdIndex: mkV2Rev(resp.Header.Revision),
  78. }, nil
  79. }
  80. func (s *v2v3Store) getDir(nodePath string, recursive, sorted bool, rev int64) ([]*store.NodeExtern, error) {
  81. rootNodes, err := s.getDirDepth(nodePath, 1, rev)
  82. if err != nil || !recursive {
  83. return rootNodes, err
  84. }
  85. nextNodes := rootNodes
  86. nodes := make(map[string]*store.NodeExtern)
  87. // Breadth walk the subdirectories
  88. for i := 2; len(nextNodes) > 0; i++ {
  89. for _, n := range nextNodes {
  90. nodes[n.Key] = n
  91. if parent := nodes[path.Dir(n.Key)]; parent != nil {
  92. parent.Nodes = append(parent.Nodes, n)
  93. }
  94. }
  95. if nextNodes, err = s.getDirDepth(nodePath, i, rev); err != nil {
  96. return nil, err
  97. }
  98. }
  99. return rootNodes, nil
  100. }
  101. func (s *v2v3Store) getDirDepth(nodePath string, depth int, rev int64) ([]*store.NodeExtern, error) {
  102. pd := s.mkPathDepth(nodePath, depth)
  103. resp, err := s.c.Get(s.ctx, pd, clientv3.WithPrefix(), clientv3.WithRev(rev))
  104. if err != nil {
  105. return nil, err
  106. }
  107. nodes := make([]*store.NodeExtern, len(resp.Kvs))
  108. for i, kv := range resp.Kvs {
  109. nodes[i] = s.mkV2Node(kv)
  110. }
  111. return nodes, nil
  112. }
  113. func (s *v2v3Store) Set(
  114. nodePath string,
  115. dir bool,
  116. value string,
  117. expireOpts store.TTLOptionSet,
  118. ) (*store.Event, error) {
  119. if expireOpts.Refresh || !expireOpts.ExpireTime.IsZero() {
  120. return nil, errUnsupported
  121. }
  122. if isRoot(nodePath) {
  123. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, nodePath, 0)
  124. }
  125. ecode := 0
  126. applyf := func(stm concurrency.STM) error {
  127. parent := path.Dir(nodePath)
  128. if !isRoot(parent) && stm.Rev(s.mkPath(parent)+"/") == 0 {
  129. ecode = etcdErr.EcodeKeyNotFound
  130. return nil
  131. }
  132. key := s.mkPath(nodePath)
  133. if dir {
  134. if stm.Rev(key) != 0 {
  135. // exists as non-dir
  136. ecode = etcdErr.EcodeNotDir
  137. return nil
  138. }
  139. key = key + "/"
  140. } else if stm.Rev(key+"/") != 0 {
  141. ecode = etcdErr.EcodeNotFile
  142. return nil
  143. }
  144. stm.Put(key, value, clientv3.WithPrevKV())
  145. stm.Put(s.mkActionKey(), store.Set)
  146. return nil
  147. }
  148. resp, err := s.newSTM(applyf)
  149. if err != nil {
  150. return nil, err
  151. }
  152. if ecode != 0 {
  153. return nil, etcdErr.NewError(ecode, nodePath, mkV2Rev(resp.Header.Revision))
  154. }
  155. createRev := resp.Header.Revision
  156. var pn *store.NodeExtern
  157. if pkv := prevKeyFromPuts(resp); pkv != nil {
  158. pn = s.mkV2Node(pkv)
  159. createRev = pkv.CreateRevision
  160. }
  161. vp := &value
  162. if dir {
  163. vp = nil
  164. }
  165. return &store.Event{
  166. Action: store.Set,
  167. Node: &store.NodeExtern{
  168. Key: nodePath,
  169. Value: vp,
  170. Dir: dir,
  171. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  172. CreatedIndex: mkV2Rev(createRev),
  173. },
  174. PrevNode: pn,
  175. EtcdIndex: mkV2Rev(resp.Header.Revision),
  176. }, nil
  177. }
  178. func (s *v2v3Store) Update(nodePath, newValue string, expireOpts store.TTLOptionSet) (*store.Event, error) {
  179. if isRoot(nodePath) {
  180. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, nodePath, 0)
  181. }
  182. if expireOpts.Refresh || !expireOpts.ExpireTime.IsZero() {
  183. return nil, errUnsupported
  184. }
  185. key := s.mkPath(nodePath)
  186. ecode := 0
  187. applyf := func(stm concurrency.STM) error {
  188. if rev := stm.Rev(key + "/"); rev != 0 {
  189. ecode = etcdErr.EcodeNotFile
  190. return nil
  191. }
  192. if rev := stm.Rev(key); rev == 0 {
  193. ecode = etcdErr.EcodeKeyNotFound
  194. return nil
  195. }
  196. stm.Put(key, newValue, clientv3.WithPrevKV())
  197. stm.Put(s.mkActionKey(), store.Update)
  198. return nil
  199. }
  200. resp, err := s.newSTM(applyf)
  201. if err != nil {
  202. return nil, err
  203. }
  204. if ecode != 0 {
  205. return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, mkV2Rev(resp.Header.Revision))
  206. }
  207. pkv := prevKeyFromPuts(resp)
  208. return &store.Event{
  209. Action: store.Update,
  210. Node: &store.NodeExtern{
  211. Key: nodePath,
  212. Value: &newValue,
  213. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  214. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  215. },
  216. PrevNode: s.mkV2Node(pkv),
  217. EtcdIndex: mkV2Rev(resp.Header.Revision),
  218. }, nil
  219. }
  220. func (s *v2v3Store) Create(
  221. nodePath string,
  222. dir bool,
  223. value string,
  224. unique bool,
  225. expireOpts store.TTLOptionSet,
  226. ) (*store.Event, error) {
  227. if isRoot(nodePath) {
  228. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, nodePath, 0)
  229. }
  230. if expireOpts.Refresh || !expireOpts.ExpireTime.IsZero() {
  231. return nil, errUnsupported
  232. }
  233. ecode := 0
  234. applyf := func(stm concurrency.STM) error {
  235. ecode = 0
  236. key := s.mkPath(nodePath)
  237. if unique {
  238. // append unique item under the node path
  239. for {
  240. key = nodePath + "/" + fmt.Sprintf("%020s", time.Now())
  241. key = path.Clean(path.Join("/", key))
  242. key = s.mkPath(key)
  243. if stm.Rev(key) == 0 {
  244. break
  245. }
  246. }
  247. }
  248. if stm.Rev(key) > 0 || stm.Rev(key+"/") > 0 {
  249. ecode = etcdErr.EcodeNodeExist
  250. return nil
  251. }
  252. // build path if any directories in path do not exist
  253. dirs := []string{}
  254. for p := path.Dir(nodePath); !isRoot(p); p = path.Dir(p) {
  255. pp := s.mkPath(p)
  256. if stm.Rev(pp) > 0 {
  257. ecode = etcdErr.EcodeNotDir
  258. return nil
  259. }
  260. if stm.Rev(pp+"/") == 0 {
  261. dirs = append(dirs, pp+"/")
  262. }
  263. }
  264. for _, d := range dirs {
  265. stm.Put(d, "")
  266. }
  267. if dir {
  268. // directories marked with extra slash in key name
  269. key += "/"
  270. }
  271. stm.Put(key, value)
  272. stm.Put(s.mkActionKey(), store.Create)
  273. return nil
  274. }
  275. resp, err := s.newSTM(applyf)
  276. if err != nil {
  277. return nil, err
  278. }
  279. if ecode != 0 {
  280. return nil, etcdErr.NewError(ecode, nodePath, mkV2Rev(resp.Header.Revision))
  281. }
  282. var v *string
  283. if !dir {
  284. v = &value
  285. }
  286. return &store.Event{
  287. Action: store.Create,
  288. Node: &store.NodeExtern{
  289. Key: nodePath,
  290. Value: v,
  291. Dir: dir,
  292. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  293. CreatedIndex: mkV2Rev(resp.Header.Revision),
  294. },
  295. EtcdIndex: mkV2Rev(resp.Header.Revision),
  296. }, nil
  297. }
  298. func (s *v2v3Store) CompareAndSwap(
  299. nodePath string,
  300. prevValue string,
  301. prevIndex uint64,
  302. value string,
  303. expireOpts store.TTLOptionSet,
  304. ) (*store.Event, error) {
  305. if isRoot(nodePath) {
  306. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, nodePath, 0)
  307. }
  308. if expireOpts.Refresh || !expireOpts.ExpireTime.IsZero() {
  309. return nil, errUnsupported
  310. }
  311. key := s.mkPath(nodePath)
  312. resp, err := s.c.Txn(s.ctx).If(
  313. s.mkCompare(nodePath, prevValue, prevIndex)...,
  314. ).Then(
  315. clientv3.OpPut(key, value, clientv3.WithPrevKV()),
  316. clientv3.OpPut(s.mkActionKey(), store.CompareAndSwap),
  317. ).Else(
  318. clientv3.OpGet(key),
  319. clientv3.OpGet(key+"/"),
  320. ).Commit()
  321. if err != nil {
  322. return nil, err
  323. }
  324. if !resp.Succeeded {
  325. return nil, compareFail(nodePath, prevValue, prevIndex, resp)
  326. }
  327. pkv := resp.Responses[0].GetResponsePut().PrevKv
  328. return &store.Event{
  329. Action: store.CompareAndSwap,
  330. Node: &store.NodeExtern{
  331. Key: nodePath,
  332. Value: &value,
  333. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  334. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  335. },
  336. PrevNode: s.mkV2Node(pkv),
  337. EtcdIndex: mkV2Rev(resp.Header.Revision),
  338. }, nil
  339. }
  340. func (s *v2v3Store) Delete(nodePath string, dir, recursive bool) (*store.Event, error) {
  341. if isRoot(nodePath) {
  342. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, nodePath, 0)
  343. }
  344. if !dir && !recursive {
  345. return s.deleteNode(nodePath)
  346. }
  347. if !recursive {
  348. return s.deleteEmptyDir(nodePath)
  349. }
  350. dels := make([]clientv3.Op, maxPathDepth+1)
  351. dels[0] = clientv3.OpDelete(s.mkPath(nodePath)+"/", clientv3.WithPrevKV())
  352. for i := 1; i < maxPathDepth; i++ {
  353. dels[i] = clientv3.OpDelete(s.mkPathDepth(nodePath, i), clientv3.WithPrefix())
  354. }
  355. dels[maxPathDepth] = clientv3.OpPut(s.mkActionKey(), store.Delete)
  356. resp, err := s.c.Txn(s.ctx).If(
  357. clientv3.Compare(clientv3.Version(s.mkPath(nodePath)+"/"), ">", 0),
  358. clientv3.Compare(clientv3.Version(s.mkPathDepth(nodePath, maxPathDepth)+"/"), "=", 0),
  359. ).Then(
  360. dels...,
  361. ).Commit()
  362. if err != nil {
  363. return nil, err
  364. }
  365. if !resp.Succeeded {
  366. return nil, etcdErr.NewError(etcdErr.EcodeNodeExist, nodePath, mkV2Rev(resp.Header.Revision))
  367. }
  368. dresp := resp.Responses[0].GetResponseDeleteRange()
  369. return &store.Event{
  370. Action: store.Delete,
  371. PrevNode: s.mkV2Node(dresp.PrevKvs[0]),
  372. EtcdIndex: mkV2Rev(resp.Header.Revision),
  373. }, nil
  374. }
  375. func (s *v2v3Store) deleteEmptyDir(nodePath string) (*store.Event, error) {
  376. resp, err := s.c.Txn(s.ctx).If(
  377. clientv3.Compare(clientv3.Version(s.mkPathDepth(nodePath, 1)), "=", 0).WithPrefix(),
  378. ).Then(
  379. clientv3.OpDelete(s.mkPath(nodePath)+"/", clientv3.WithPrevKV()),
  380. clientv3.OpPut(s.mkActionKey(), store.Delete),
  381. ).Commit()
  382. if err != nil {
  383. return nil, err
  384. }
  385. if !resp.Succeeded {
  386. return nil, etcdErr.NewError(etcdErr.EcodeDirNotEmpty, nodePath, mkV2Rev(resp.Header.Revision))
  387. }
  388. dresp := resp.Responses[0].GetResponseDeleteRange()
  389. if len(dresp.PrevKvs) == 0 {
  390. return nil, etcdErr.NewError(etcdErr.EcodeNodeExist, nodePath, mkV2Rev(resp.Header.Revision))
  391. }
  392. return &store.Event{
  393. Action: store.Delete,
  394. PrevNode: s.mkV2Node(dresp.PrevKvs[0]),
  395. EtcdIndex: mkV2Rev(resp.Header.Revision),
  396. }, nil
  397. }
  398. func (s *v2v3Store) deleteNode(nodePath string) (*store.Event, error) {
  399. resp, err := s.c.Txn(s.ctx).If(
  400. clientv3.Compare(clientv3.Version(s.mkPath(nodePath)+"/"), "=", 0),
  401. ).Then(
  402. clientv3.OpDelete(s.mkPath(nodePath), clientv3.WithPrevKV()),
  403. clientv3.OpPut(s.mkActionKey(), store.Delete),
  404. ).Commit()
  405. if err != nil {
  406. return nil, err
  407. }
  408. if !resp.Succeeded {
  409. return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, mkV2Rev(resp.Header.Revision))
  410. }
  411. pkvs := resp.Responses[0].GetResponseDeleteRange().PrevKvs
  412. if len(pkvs) == 0 {
  413. return nil, etcdErr.NewError(etcdErr.EcodeKeyNotFound, nodePath, mkV2Rev(resp.Header.Revision))
  414. }
  415. pkv := pkvs[0]
  416. return &store.Event{
  417. Action: store.Delete,
  418. Node: &store.NodeExtern{
  419. Key: nodePath,
  420. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  421. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  422. },
  423. PrevNode: s.mkV2Node(pkv),
  424. EtcdIndex: mkV2Rev(resp.Header.Revision),
  425. }, nil
  426. }
  427. func (s *v2v3Store) CompareAndDelete(nodePath, prevValue string, prevIndex uint64) (*store.Event, error) {
  428. if isRoot(nodePath) {
  429. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, nodePath, 0)
  430. }
  431. key := s.mkPath(nodePath)
  432. resp, err := s.c.Txn(s.ctx).If(
  433. s.mkCompare(nodePath, prevValue, prevIndex)...,
  434. ).Then(
  435. clientv3.OpDelete(key, clientv3.WithPrevKV()),
  436. clientv3.OpPut(s.mkActionKey(), store.CompareAndDelete),
  437. ).Else(
  438. clientv3.OpGet(key),
  439. clientv3.OpGet(key+"/"),
  440. ).Commit()
  441. if err != nil {
  442. return nil, err
  443. }
  444. if !resp.Succeeded {
  445. return nil, compareFail(nodePath, prevValue, prevIndex, resp)
  446. }
  447. // len(pkvs) > 1 since txn only succeeds when key exists
  448. pkv := resp.Responses[0].GetResponseDeleteRange().PrevKvs[0]
  449. return &store.Event{
  450. Action: store.CompareAndDelete,
  451. Node: &store.NodeExtern{
  452. Key: nodePath,
  453. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  454. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  455. },
  456. PrevNode: s.mkV2Node(pkv),
  457. EtcdIndex: mkV2Rev(resp.Header.Revision),
  458. }, nil
  459. }
  460. func compareFail(nodePath, prevValue string, prevIndex uint64, resp *clientv3.TxnResponse) error {
  461. if dkvs := resp.Responses[1].GetResponseRange().Kvs; len(dkvs) > 0 {
  462. return etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, mkV2Rev(resp.Header.Revision))
  463. }
  464. kvs := resp.Responses[0].GetResponseRange().Kvs
  465. if len(kvs) == 0 {
  466. return etcdErr.NewError(etcdErr.EcodeKeyNotFound, nodePath, mkV2Rev(resp.Header.Revision))
  467. }
  468. kv := kvs[0]
  469. indexMatch := (prevIndex == 0 || kv.ModRevision == int64(prevIndex))
  470. valueMatch := (prevValue == "" || string(kv.Value) == prevValue)
  471. var cause string
  472. switch {
  473. case indexMatch && !valueMatch:
  474. cause = fmt.Sprintf("[%v != %v]", prevValue, string(kv.Value))
  475. case valueMatch && !indexMatch:
  476. cause = fmt.Sprintf("[%v != %v]", prevIndex, kv.ModRevision)
  477. default:
  478. cause = fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, string(kv.Value), prevIndex, kv.ModRevision)
  479. }
  480. return etcdErr.NewError(etcdErr.EcodeTestFailed, cause, mkV2Rev(resp.Header.Revision))
  481. }
  482. func (s *v2v3Store) mkCompare(nodePath, prevValue string, prevIndex uint64) []clientv3.Cmp {
  483. key := s.mkPath(nodePath)
  484. cmps := []clientv3.Cmp{clientv3.Compare(clientv3.Version(key), ">", 0)}
  485. if prevIndex != 0 {
  486. cmps = append(cmps, clientv3.Compare(clientv3.ModRevision(key), "=", mkV3Rev(prevIndex)))
  487. }
  488. if prevValue != "" {
  489. cmps = append(cmps, clientv3.Compare(clientv3.Value(key), "=", prevValue))
  490. }
  491. return cmps
  492. }
  493. func (s *v2v3Store) JsonStats() []byte { panic("STUB") }
  494. func (s *v2v3Store) DeleteExpiredKeys(cutoff time.Time) { panic("STUB") }
  495. func (s *v2v3Store) Version() int { return 2 }
  496. // TODO: move this out of the Store interface?
  497. func (s *v2v3Store) Save() ([]byte, error) { panic("STUB") }
  498. func (s *v2v3Store) Recovery(state []byte) error { panic("STUB") }
  499. func (s *v2v3Store) Clone() store.Store { panic("STUB") }
  500. func (s *v2v3Store) SaveNoCopy() ([]byte, error) { panic("STUB") }
  501. func (s *v2v3Store) HasTTLKeys() bool { panic("STUB") }
  502. func (s *v2v3Store) mkPath(nodePath string) string { return s.mkPathDepth(nodePath, 0) }
  503. func (s *v2v3Store) mkNodePath(p string) string {
  504. return path.Clean(p[len(s.pfx)+len("/k/000/"):])
  505. }
  506. // mkPathDepth makes a path to a key that encodes its directory depth
  507. // for fast directory listing. If a depth is provided, it is added
  508. // to the computed depth.
  509. func (s *v2v3Store) mkPathDepth(nodePath string, depth int) string {
  510. normalForm := path.Clean(path.Join("/", nodePath))
  511. n := strings.Count(normalForm, "/") + depth
  512. return fmt.Sprintf("%s/%03d/k/%s", s.pfx, n, normalForm)
  513. }
  514. func (s *v2v3Store) mkActionKey() string { return s.pfx + "/act" }
  515. func isRoot(s string) bool { return len(s) == 0 || s == "/" || s == "/0" || s == "/1" }
  516. func mkV2Rev(v3Rev int64) uint64 {
  517. if v3Rev == 0 {
  518. return 0
  519. }
  520. return uint64(v3Rev - 1)
  521. }
  522. func mkV3Rev(v2Rev uint64) int64 {
  523. if v2Rev == 0 {
  524. return 0
  525. }
  526. return int64(v2Rev + 1)
  527. }
  528. // mkV2Node creates a V2 NodeExtern from a V3 KeyValue
  529. func (s *v2v3Store) mkV2Node(kv *mvccpb.KeyValue) *store.NodeExtern {
  530. if kv == nil {
  531. return nil
  532. }
  533. n := &store.NodeExtern{
  534. Key: string(s.mkNodePath(string(kv.Key))),
  535. Dir: kv.Key[len(kv.Key)-1] == '/',
  536. CreatedIndex: mkV2Rev(kv.CreateRevision),
  537. ModifiedIndex: mkV2Rev(kv.ModRevision),
  538. }
  539. if !n.Dir {
  540. v := string(kv.Value)
  541. n.Value = &v
  542. }
  543. return n
  544. }
  545. // prevKeyFromPuts gets the prev key that is being put; ignores
  546. // the put action response.
  547. func prevKeyFromPuts(resp *clientv3.TxnResponse) *mvccpb.KeyValue {
  548. for _, r := range resp.Responses {
  549. pkv := r.GetResponsePut().PrevKv
  550. if pkv != nil && pkv.CreateRevision > 0 {
  551. return pkv
  552. }
  553. }
  554. return nil
  555. }
  556. func (s *v2v3Store) newSTM(applyf func(concurrency.STM) error) (*clientv3.TxnResponse, error) {
  557. return concurrency.NewSTM(s.c, applyf, concurrency.WithIsolation(concurrency.Serializable))
  558. }