1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
package datastore
import (
"fmt"
"strings"
"sync"
"time"
store "github.com/docker/docker/libnetwork/internal/kvstore"
"github.com/docker/docker/libnetwork/internal/kvstore/boltdb"
"github.com/docker/docker/libnetwork/types"
)
// ErrKeyModified is raised for an atomic update when the update is working on a stale state
var (
ErrKeyModified = store.ErrKeyModified
ErrKeyNotFound = store.ErrKeyNotFound
)
type Store struct {
mu sync.Mutex
store store.Store
cache *cache
}
// KVObject is Key/Value interface used by objects to be part of the Store.
type KVObject interface {
// Key method lets an object provide the Key to be used in KV Store
Key() []string
// KeyPrefix method lets an object return immediate parent key that can be used for tree walk
KeyPrefix() []string
// Value method lets an object marshal its content to be stored in the KV store
Value() []byte
// SetValue is used by the datastore to set the object's value when loaded from the data store.
SetValue([]byte) error
// Index method returns the latest DB Index as seen by the object
Index() uint64
// SetIndex method allows the datastore to store the latest DB Index into the object
SetIndex(uint64)
// Exists returns true if the object exists in the datastore, false if it hasn't been stored yet.
// When SetIndex() is called, the object has been stored.
Exists() bool
// Skip provides a way for a KV Object to avoid persisting it in the KV Store
Skip() bool
// New returns a new object which is created based on the
// source object
New() KVObject
// CopyTo deep copies the contents of the implementing object
// to the passed destination object
CopyTo(KVObject) error
}
// ScopeCfg represents Datastore configuration.
type ScopeCfg struct {
Client ScopeClientCfg
}
// ScopeClientCfg represents Datastore Client-only mode configuration
type ScopeClientCfg struct {
Provider string
Address string
Config *store.Config
}
const (
// NetworkKeyPrefix is the prefix for network key in the kv store
NetworkKeyPrefix = "network"
// EndpointKeyPrefix is the prefix for endpoint key in the kv store
EndpointKeyPrefix = "endpoint"
)
var (
defaultRootChain = []string{"docker", "network", "v1.0"}
rootChain = defaultRootChain
)
const defaultPrefix = "/var/lib/docker/network/files"
// DefaultScope returns a default scope config for clients to use.
func DefaultScope(dataDir string) ScopeCfg {
var dbpath string
if dataDir == "" {
dbpath = defaultPrefix + "/local-kv.db"
} else {
dbpath = dataDir + "/network/files/local-kv.db"
}
return ScopeCfg{
Client: ScopeClientCfg{
Provider: string(store.BOLTDB),
Address: dbpath,
Config: &store.Config{
Bucket: "libnetwork",
ConnectionTimeout: time.Minute,
},
},
}
}
// IsValid checks if the scope config has valid configuration.
func (cfg *ScopeCfg) IsValid() bool {
if cfg == nil || strings.TrimSpace(cfg.Client.Provider) == "" || strings.TrimSpace(cfg.Client.Address) == "" {
return false
}
return true
}
// Key provides convenient method to create a Key
func Key(key ...string) string {
var b strings.Builder
for _, parts := range [][]string{rootChain, key} {
for _, part := range parts {
b.WriteString(part)
b.WriteString("/")
}
}
return b.String()
}
// newClient used to connect to KV Store
func newClient(kv string, addr string, config *store.Config) (*Store, error) {
if kv != string(store.BOLTDB) {
return nil, fmt.Errorf("unsupported KV store")
}
if config == nil {
config = &store.Config{}
}
s, err := boltdb.New(addr, config)
if err != nil {
return nil, err
}
return &Store{store: s, cache: newCache(s)}, nil
}
// New creates a new Store instance.
func New(cfg ScopeCfg) (*Store, error) {
if cfg.Client.Provider == "" || cfg.Client.Address == "" {
cfg = DefaultScope("")
}
return newClient(cfg.Client.Provider, cfg.Client.Address, cfg.Client.Config)
}
// Close closes the data store.
func (ds *Store) Close() {
ds.store.Close()
}
// PutObjectAtomic provides an atomic add and update operation for a Record.
func (ds *Store) PutObjectAtomic(kvObject KVObject) error {
ds.mu.Lock()
defer ds.mu.Unlock()
if kvObject == nil {
return types.InvalidParameterErrorf("invalid KV Object: nil")
}
kvObjValue := kvObject.Value()
if kvObjValue == nil {
return types.InvalidParameterErrorf("invalid KV Object with a nil Value for key %s", Key(kvObject.Key()...))
}
if !kvObject.Skip() {
var previous *store.KVPair
if kvObject.Exists() {
previous = &store.KVPair{Key: Key(kvObject.Key()...), LastIndex: kvObject.Index()}
}
pair, err := ds.store.AtomicPut(Key(kvObject.Key()...), kvObjValue, previous)
if err != nil {
if err == store.ErrKeyExists {
return ErrKeyModified
}
return err
}
kvObject.SetIndex(pair.LastIndex)
}
// If persistent store is skipped, sequencing needs to
// happen in cache.
return ds.cache.add(kvObject, kvObject.Skip())
}
// GetObject gets data from the store and unmarshals to the specified object.
func (ds *Store) GetObject(o KVObject) error {
ds.mu.Lock()
defer ds.mu.Unlock()
return ds.cache.get(o)
}
func (ds *Store) ensureParent(parent string) error {
exists, err := ds.store.Exists(parent)
if err != nil {
return err
}
if exists {
return nil
}
return ds.store.Put(parent, []byte{})
}
// List returns of a list of KVObjects belonging to the parent key. The caller
// must pass a KVObject of the same type as the objects that need to be listed.
func (ds *Store) List(kvObject KVObject) ([]KVObject, error) {
ds.mu.Lock()
defer ds.mu.Unlock()
return ds.cache.list(kvObject)
}
func (ds *Store) iterateKVPairsFromStore(key string, ctor KVObject, callback func(string, KVObject)) error {
// Make sure the parent key exists
if err := ds.ensureParent(key); err != nil {
return err
}
kvList, err := ds.store.List(key)
if err != nil {
return err
}
for _, kvPair := range kvList {
if len(kvPair.Value) == 0 {
continue
}
dstO := ctor.New()
if err := dstO.SetValue(kvPair.Value); err != nil {
return err
}
// Make sure the object has a correct view of the DB index in
// case we need to modify it and update the DB.
dstO.SetIndex(kvPair.LastIndex)
callback(kvPair.Key, dstO)
}
return nil
}
// Map returns a Map of KVObjects.
func (ds *Store) Map(key string, kvObject KVObject) (map[string]KVObject, error) {
ds.mu.Lock()
defer ds.mu.Unlock()
results := map[string]KVObject{}
err := ds.iterateKVPairsFromStore(key, kvObject, func(key string, val KVObject) {
// Trim the leading & trailing "/" to make it consistent across all stores
results[strings.Trim(key, "/")] = val
})
if err != nil {
return nil, err
}
return results, nil
}
// DeleteObject deletes a kvObject from the on-disk DB and the in-memory cache.
// Unlike DeleteObjectAtomic, it doesn't check the optimistic lock of the
// passed kvObject.
func (ds *Store) DeleteObject(kvObject KVObject) error {
ds.mu.Lock()
defer ds.mu.Unlock()
if kvObject == nil {
return types.InvalidParameterErrorf("invalid KV Object: nil")
}
if !kvObject.Skip() {
if err := ds.store.Delete(Key(kvObject.Key()...)); err != nil {
return err
}
}
// cleanup the cache only if AtomicDelete went through successfully
// If persistent store is skipped, sequencing needs to
// happen in cache.
return ds.cache.del(kvObject, false)
}
// DeleteObjectAtomic performs atomic delete on a record.
func (ds *Store) DeleteObjectAtomic(kvObject KVObject) error {
ds.mu.Lock()
defer ds.mu.Unlock()
if kvObject == nil {
return types.InvalidParameterErrorf("invalid KV Object: nil")
}
previous := &store.KVPair{Key: Key(kvObject.Key()...), LastIndex: kvObject.Index()}
if !kvObject.Skip() {
if err := ds.store.AtomicDelete(Key(kvObject.Key()...), previous); err != nil {
if err == store.ErrKeyExists {
return ErrKeyModified
}
return err
}
}
// cleanup the cache only if AtomicDelete went through successfully
// If persistent store is skipped, sequencing needs to
// happen in cache.
return ds.cache.del(kvObject, kvObject.Skip())
}
|