store.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package store
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "time"
  6. )
  7. // Backend represents a KV Store Backend
  8. type Backend string
  9. const (
  10. // CONSUL backend
  11. CONSUL Backend = "consul"
  12. // ETCD backend
  13. ETCD Backend = "etcd"
  14. // ZK backend
  15. ZK Backend = "zk"
  16. // BOLTDB backend
  17. BOLTDB Backend = "boltdb"
  18. )
  19. var (
  20. // ErrBackendNotSupported is thrown when the backend k/v store is not supported by libkv
  21. ErrBackendNotSupported = errors.New("Backend storage not supported yet, please choose one of")
  22. // ErrCallNotSupported is thrown when a method is not implemented/supported by the current backend
  23. ErrCallNotSupported = errors.New("The current call is not supported with this backend")
  24. // ErrNotReachable is thrown when the API cannot be reached for issuing common store operations
  25. ErrNotReachable = errors.New("Api not reachable")
  26. // ErrCannotLock is thrown when there is an error acquiring a lock on a key
  27. ErrCannotLock = errors.New("Error acquiring the lock")
  28. // ErrKeyModified is thrown during an atomic operation if the index does not match the one in the store
  29. ErrKeyModified = errors.New("Unable to complete atomic operation, key modified")
  30. // ErrKeyNotFound is thrown when the key is not found in the store during a Get operation
  31. ErrKeyNotFound = errors.New("Key not found in store")
  32. // ErrPreviousNotSpecified is thrown when the previous value is not specified for an atomic operation
  33. ErrPreviousNotSpecified = errors.New("Previous K/V pair should be provided for the Atomic operation")
  34. // ErrKeyExists is thrown when the previous value exists in the case of an AtomicPut
  35. ErrKeyExists = errors.New("Previous K/V pair exists, cannot complete Atomic operation")
  36. )
  37. // Config contains the options for a storage client
  38. type Config struct {
  39. ClientTLS *ClientTLSConfig
  40. TLS *tls.Config
  41. ConnectionTimeout time.Duration
  42. Bucket string
  43. PersistConnection bool
  44. Username string
  45. Password string
  46. }
  47. // ClientTLSConfig contains data for a Client TLS configuration in the form
  48. // the etcd client wants it. Eventually we'll adapt it for ZK and Consul.
  49. type ClientTLSConfig struct {
  50. CertFile string
  51. KeyFile string
  52. CACertFile string
  53. }
  54. // Store represents the backend K/V storage
  55. // Each store should support every call listed
  56. // here. Or it couldn't be implemented as a K/V
  57. // backend for libkv
  58. type Store interface {
  59. // Put a value at the specified key
  60. Put(key string, value []byte, options *WriteOptions) error
  61. // Get a value given its key
  62. Get(key string) (*KVPair, error)
  63. // Delete the value at the specified key
  64. Delete(key string) error
  65. // Verify if a Key exists in the store
  66. Exists(key string) (bool, error)
  67. // Watch for changes on a key
  68. Watch(key string, stopCh <-chan struct{}) (<-chan *KVPair, error)
  69. // WatchTree watches for changes on child nodes under
  70. // a given directory
  71. WatchTree(directory string, stopCh <-chan struct{}) (<-chan []*KVPair, error)
  72. // NewLock creates a lock for a given key.
  73. // The returned Locker is not held and must be acquired
  74. // with `.Lock`. The Value is optional.
  75. NewLock(key string, options *LockOptions) (Locker, error)
  76. // List the content of a given prefix
  77. List(directory string) ([]*KVPair, error)
  78. // DeleteTree deletes a range of keys under a given directory
  79. DeleteTree(directory string) error
  80. // Atomic CAS operation on a single value.
  81. // Pass previous = nil to create a new key.
  82. AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, *KVPair, error)
  83. // Atomic delete of a single value
  84. AtomicDelete(key string, previous *KVPair) (bool, error)
  85. // Close the store connection
  86. Close()
  87. }
  88. // KVPair represents {Key, Value, Lastindex} tuple
  89. type KVPair struct {
  90. Key string
  91. Value []byte
  92. LastIndex uint64
  93. }
  94. // WriteOptions contains optional request parameters
  95. type WriteOptions struct {
  96. IsDir bool
  97. TTL time.Duration
  98. }
  99. // LockOptions contains optional request parameters
  100. type LockOptions struct {
  101. Value []byte // Optional, value to associate with the lock
  102. TTL time.Duration // Optional, expiration ttl associated with the lock
  103. RenewLock chan struct{} // Optional, chan used to control and stop the session ttl renewal for the lock
  104. }
  105. // Locker provides locking mechanism on top of the store.
  106. // Similar to `sync.Lock` except it may return errors.
  107. type Locker interface {
  108. Lock(stopChan chan struct{}) (<-chan struct{}, error)
  109. Unlock() error
  110. }