store.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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/mvcc/mvccpb"
  25. "github.com/coreos/etcd/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. dir = true
  348. if !recursive {
  349. return s.deleteEmptyDir(nodePath)
  350. }
  351. dels := make([]clientv3.Op, maxPathDepth+1)
  352. dels[0] = clientv3.OpDelete(s.mkPath(nodePath)+"/", clientv3.WithPrevKV())
  353. for i := 1; i < maxPathDepth; i++ {
  354. dels[i] = clientv3.OpDelete(s.mkPathDepth(nodePath, i), clientv3.WithPrefix())
  355. }
  356. dels[maxPathDepth] = clientv3.OpPut(s.mkActionKey(), store.Delete)
  357. resp, err := s.c.Txn(s.ctx).If(
  358. clientv3.Compare(clientv3.Version(s.mkPath(nodePath)+"/"), ">", 0),
  359. clientv3.Compare(clientv3.Version(s.mkPathDepth(nodePath, maxPathDepth)+"/"), "=", 0),
  360. ).Then(
  361. dels...,
  362. ).Commit()
  363. if err != nil {
  364. return nil, err
  365. }
  366. if !resp.Succeeded {
  367. return nil, etcdErr.NewError(etcdErr.EcodeNodeExist, nodePath, mkV2Rev(resp.Header.Revision))
  368. }
  369. dresp := resp.Responses[0].GetResponseDeleteRange()
  370. return &store.Event{
  371. Action: store.Delete,
  372. PrevNode: s.mkV2Node(dresp.PrevKvs[0]),
  373. EtcdIndex: mkV2Rev(resp.Header.Revision),
  374. }, nil
  375. }
  376. func (s *v2v3Store) deleteEmptyDir(nodePath string) (*store.Event, error) {
  377. resp, err := s.c.Txn(s.ctx).If(
  378. clientv3.Compare(clientv3.Version(s.mkPathDepth(nodePath, 1)), "=", 0).WithPrefix(),
  379. ).Then(
  380. clientv3.OpDelete(s.mkPath(nodePath)+"/", clientv3.WithPrevKV()),
  381. clientv3.OpPut(s.mkActionKey(), store.Delete),
  382. ).Commit()
  383. if err != nil {
  384. return nil, err
  385. }
  386. if !resp.Succeeded {
  387. return nil, etcdErr.NewError(etcdErr.EcodeDirNotEmpty, nodePath, mkV2Rev(resp.Header.Revision))
  388. }
  389. dresp := resp.Responses[0].GetResponseDeleteRange()
  390. if len(dresp.PrevKvs) == 0 {
  391. return nil, etcdErr.NewError(etcdErr.EcodeNodeExist, nodePath, mkV2Rev(resp.Header.Revision))
  392. }
  393. return &store.Event{
  394. Action: store.Delete,
  395. PrevNode: s.mkV2Node(dresp.PrevKvs[0]),
  396. EtcdIndex: mkV2Rev(resp.Header.Revision),
  397. }, nil
  398. }
  399. func (s *v2v3Store) deleteNode(nodePath string) (*store.Event, error) {
  400. resp, err := s.c.Txn(s.ctx).If(
  401. clientv3.Compare(clientv3.Version(s.mkPath(nodePath)+"/"), "=", 0),
  402. ).Then(
  403. clientv3.OpDelete(s.mkPath(nodePath), clientv3.WithPrevKV()),
  404. clientv3.OpPut(s.mkActionKey(), store.Delete),
  405. ).Commit()
  406. if err != nil {
  407. return nil, err
  408. }
  409. if !resp.Succeeded {
  410. return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, mkV2Rev(resp.Header.Revision))
  411. }
  412. pkvs := resp.Responses[0].GetResponseDeleteRange().PrevKvs
  413. if len(pkvs) == 0 {
  414. return nil, etcdErr.NewError(etcdErr.EcodeKeyNotFound, nodePath, mkV2Rev(resp.Header.Revision))
  415. }
  416. pkv := pkvs[0]
  417. return &store.Event{
  418. Action: store.Delete,
  419. Node: &store.NodeExtern{
  420. Key: nodePath,
  421. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  422. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  423. },
  424. PrevNode: s.mkV2Node(pkv),
  425. EtcdIndex: mkV2Rev(resp.Header.Revision),
  426. }, nil
  427. }
  428. func (s *v2v3Store) CompareAndDelete(nodePath, prevValue string, prevIndex uint64) (*store.Event, error) {
  429. if isRoot(nodePath) {
  430. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, nodePath, 0)
  431. }
  432. key := s.mkPath(nodePath)
  433. resp, err := s.c.Txn(s.ctx).If(
  434. s.mkCompare(nodePath, prevValue, prevIndex)...,
  435. ).Then(
  436. clientv3.OpDelete(key, clientv3.WithPrevKV()),
  437. clientv3.OpPut(s.mkActionKey(), store.CompareAndDelete),
  438. ).Else(
  439. clientv3.OpGet(key),
  440. clientv3.OpGet(key+"/"),
  441. ).Commit()
  442. if err != nil {
  443. return nil, err
  444. }
  445. if !resp.Succeeded {
  446. return nil, compareFail(nodePath, prevValue, prevIndex, resp)
  447. }
  448. // len(pkvs) > 1 since txn only succeeds when key exists
  449. pkv := resp.Responses[0].GetResponseDeleteRange().PrevKvs[0]
  450. return &store.Event{
  451. Action: store.CompareAndDelete,
  452. Node: &store.NodeExtern{
  453. Key: nodePath,
  454. CreatedIndex: mkV2Rev(pkv.CreateRevision),
  455. ModifiedIndex: mkV2Rev(resp.Header.Revision),
  456. },
  457. PrevNode: s.mkV2Node(pkv),
  458. EtcdIndex: mkV2Rev(resp.Header.Revision),
  459. }, nil
  460. }
  461. func compareFail(nodePath, prevValue string, prevIndex uint64, resp *clientv3.TxnResponse) error {
  462. if dkvs := resp.Responses[1].GetResponseRange().Kvs; len(dkvs) > 0 {
  463. return etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, mkV2Rev(resp.Header.Revision))
  464. }
  465. kvs := resp.Responses[0].GetResponseRange().Kvs
  466. if len(kvs) == 0 {
  467. return etcdErr.NewError(etcdErr.EcodeKeyNotFound, nodePath, mkV2Rev(resp.Header.Revision))
  468. }
  469. kv := kvs[0]
  470. indexMatch := (prevIndex == 0 || kv.ModRevision == int64(prevIndex))
  471. valueMatch := (prevValue == "" || string(kv.Value) == prevValue)
  472. cause := ""
  473. switch {
  474. case indexMatch && !valueMatch:
  475. cause = fmt.Sprintf("[%v != %v]", prevValue, string(kv.Value))
  476. case valueMatch && !indexMatch:
  477. cause = fmt.Sprintf("[%v != %v]", prevIndex, kv.ModRevision)
  478. default:
  479. cause = fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, string(kv.Value), prevIndex, kv.ModRevision)
  480. }
  481. return etcdErr.NewError(etcdErr.EcodeTestFailed, cause, mkV2Rev(resp.Header.Revision))
  482. }
  483. func (s *v2v3Store) mkCompare(nodePath, prevValue string, prevIndex uint64) []clientv3.Cmp {
  484. key := s.mkPath(nodePath)
  485. cmps := []clientv3.Cmp{clientv3.Compare(clientv3.Version(key), ">", 0)}
  486. if prevIndex != 0 {
  487. cmps = append(cmps, clientv3.Compare(clientv3.ModRevision(key), "=", mkV3Rev(prevIndex)))
  488. }
  489. if prevValue != "" {
  490. cmps = append(cmps, clientv3.Compare(clientv3.Value(key), "=", prevValue))
  491. }
  492. return cmps
  493. }
  494. func (s *v2v3Store) JsonStats() []byte { panic("STUB") }
  495. func (s *v2v3Store) DeleteExpiredKeys(cutoff time.Time) { panic("STUB") }
  496. func (s *v2v3Store) Version() int { return 2 }
  497. // TODO: move this out of the Store interface?
  498. func (s *v2v3Store) Save() ([]byte, error) { panic("STUB") }
  499. func (s *v2v3Store) Recovery(state []byte) error { panic("STUB") }
  500. func (s *v2v3Store) Clone() store.Store { panic("STUB") }
  501. func (s *v2v3Store) SaveNoCopy() ([]byte, error) { panic("STUB") }
  502. func (s *v2v3Store) HasTTLKeys() bool { panic("STUB") }
  503. func (s *v2v3Store) mkPath(nodePath string) string { return s.mkPathDepth(nodePath, 0) }
  504. func (s *v2v3Store) mkNodePath(p string) string {
  505. return path.Clean(p[len(s.pfx)+len("/k/000/"):])
  506. }
  507. // mkPathDepth makes a path to a key that encodes its directory depth
  508. // for fast directory listing. If a depth is provided, it is added
  509. // to the computed depth.
  510. func (s *v2v3Store) mkPathDepth(nodePath string, depth int) string {
  511. normalForm := path.Clean(path.Join("/", nodePath))
  512. n := strings.Count(normalForm, "/") + depth
  513. return fmt.Sprintf("%s/%03d/k/%s", s.pfx, n, normalForm)
  514. }
  515. func (s *v2v3Store) mkActionKey() string { return s.pfx + "/act" }
  516. func isRoot(s string) bool { return len(s) == 0 || s == "/" || s == "/0" || s == "/1" }
  517. func mkV2Rev(v3Rev int64) uint64 {
  518. if v3Rev == 0 {
  519. return 0
  520. }
  521. return uint64(v3Rev - 1)
  522. }
  523. func mkV3Rev(v2Rev uint64) int64 {
  524. if v2Rev == 0 {
  525. return 0
  526. }
  527. return int64(v2Rev + 1)
  528. }
  529. // mkV2Node creates a V2 NodeExtern from a V3 KeyValue
  530. func (s *v2v3Store) mkV2Node(kv *mvccpb.KeyValue) *store.NodeExtern {
  531. if kv == nil {
  532. return nil
  533. }
  534. n := &store.NodeExtern{
  535. Key: string(s.mkNodePath(string(kv.Key))),
  536. Dir: kv.Key[len(kv.Key)-1] == '/',
  537. CreatedIndex: mkV2Rev(kv.CreateRevision),
  538. ModifiedIndex: mkV2Rev(kv.ModRevision),
  539. }
  540. if !n.Dir {
  541. v := string(kv.Value)
  542. n.Value = &v
  543. }
  544. return n
  545. }
  546. // prevKeyFromPuts gets the prev key that is being put; ignores
  547. // the put action response.
  548. func prevKeyFromPuts(resp *clientv3.TxnResponse) *mvccpb.KeyValue {
  549. for _, r := range resp.Responses {
  550. pkv := r.GetResponsePut().PrevKv
  551. if pkv != nil && pkv.CreateRevision > 0 {
  552. return pkv
  553. }
  554. }
  555. return nil
  556. }
  557. func (s *v2v3Store) newSTM(applyf func(concurrency.STM) error) (*clientv3.TxnResponse, error) {
  558. return concurrency.NewSTM(s.c, applyf, concurrency.WithIsolation(concurrency.Serializable))
  559. }