backend.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 main
  15. import (
  16. "encoding/binary"
  17. "fmt"
  18. "path/filepath"
  19. "go.etcd.io/etcd/lease/leasepb"
  20. "go.etcd.io/etcd/mvcc"
  21. "go.etcd.io/etcd/mvcc/backend"
  22. "go.etcd.io/etcd/mvcc/mvccpb"
  23. bolt "go.etcd.io/bbolt"
  24. )
  25. func snapDir(dataDir string) string {
  26. return filepath.Join(dataDir, "member", "snap")
  27. }
  28. func getBuckets(dbPath string) (buckets []string, err error) {
  29. db, derr := bolt.Open(dbPath, 0600, &bolt.Options{Timeout: flockTimeout})
  30. if derr != nil {
  31. return nil, fmt.Errorf("failed to open bolt DB %v", derr)
  32. }
  33. defer db.Close()
  34. err = db.View(func(tx *bolt.Tx) error {
  35. return tx.ForEach(func(b []byte, _ *bolt.Bucket) error {
  36. buckets = append(buckets, string(b))
  37. return nil
  38. })
  39. })
  40. return buckets, err
  41. }
  42. // TODO: import directly from packages, rather than copy&paste
  43. type decoder func(k, v []byte)
  44. var decoders = map[string]decoder{
  45. "key": keyDecoder,
  46. "lease": leaseDecoder,
  47. }
  48. type revision struct {
  49. main int64
  50. sub int64
  51. }
  52. func bytesToRev(bytes []byte) revision {
  53. return revision{
  54. main: int64(binary.BigEndian.Uint64(bytes[0:8])),
  55. sub: int64(binary.BigEndian.Uint64(bytes[9:])),
  56. }
  57. }
  58. func keyDecoder(k, v []byte) {
  59. rev := bytesToRev(k)
  60. var kv mvccpb.KeyValue
  61. if err := kv.Unmarshal(v); err != nil {
  62. panic(err)
  63. }
  64. fmt.Printf("rev=%+v, value=[key %q | val %q | created %d | mod %d | ver %d]\n", rev, string(kv.Key), string(kv.Value), kv.CreateRevision, kv.ModRevision, kv.Version)
  65. }
  66. func bytesToLeaseID(bytes []byte) int64 {
  67. if len(bytes) != 8 {
  68. panic(fmt.Errorf("lease ID must be 8-byte"))
  69. }
  70. return int64(binary.BigEndian.Uint64(bytes))
  71. }
  72. func leaseDecoder(k, v []byte) {
  73. leaseID := bytesToLeaseID(k)
  74. var lpb leasepb.Lease
  75. if err := lpb.Unmarshal(v); err != nil {
  76. panic(err)
  77. }
  78. fmt.Printf("lease ID=%016x, TTL=%ds\n", leaseID, lpb.TTL)
  79. }
  80. func iterateBucket(dbPath, bucket string, limit uint64, decode bool) (err error) {
  81. db, err := bolt.Open(dbPath, 0600, &bolt.Options{Timeout: flockTimeout})
  82. if err != nil {
  83. return fmt.Errorf("failed to open bolt DB %v", err)
  84. }
  85. defer db.Close()
  86. err = db.View(func(tx *bolt.Tx) error {
  87. b := tx.Bucket([]byte(bucket))
  88. if b == nil {
  89. return fmt.Errorf("got nil bucket for %s", bucket)
  90. }
  91. c := b.Cursor()
  92. // iterate in reverse order (use First() and Next() for ascending order)
  93. for k, v := c.Last(); k != nil; k, v = c.Prev() {
  94. // TODO: remove sensitive information
  95. // (https://github.com/etcd-io/etcd/issues/7620)
  96. if dec, ok := decoders[bucket]; decode && ok {
  97. dec(k, v)
  98. } else {
  99. fmt.Printf("key=%q, value=%q\n", k, v)
  100. }
  101. limit--
  102. if limit == 0 {
  103. break
  104. }
  105. }
  106. return nil
  107. })
  108. return err
  109. }
  110. func getHash(dbPath string) (hash uint32, err error) {
  111. b := backend.NewDefaultBackend(dbPath)
  112. return b.Hash(mvcc.DefaultIgnores)
  113. }
  114. // TODO: revert by revision and find specified hash value
  115. // currently, it's hard because lease is in separate bucket
  116. // and does not modify revision