2015-05-08 09:26:35 -04:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2015-06-05 16:31:12 -04:00
|
|
|
"reflect"
|
2015-05-08 09:26:35 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-06-11 08:32:15 -04:00
|
|
|
"github.com/docker/libkv"
|
|
|
|
"github.com/docker/libkv/store"
|
2015-05-23 00:52:02 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-06-05 16:31:12 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-08 09:26:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
//DataStore exported
|
|
|
|
type DataStore interface {
|
2015-06-01 00:19:10 -04:00
|
|
|
// GetObject gets data from datastore and unmarshals to the specified object
|
2015-06-18 18:13:38 -04:00
|
|
|
GetObject(key string, o KV) error
|
2015-05-08 09:26:35 -04:00
|
|
|
// PutObject adds a new Record based on an object into the datastore
|
|
|
|
PutObject(kvObject KV) error
|
|
|
|
// PutObjectAtomic provides an atomic add and update operation for a Record
|
|
|
|
PutObjectAtomic(kvObject KV) error
|
2015-06-01 12:43:24 -04:00
|
|
|
// DeleteObject deletes a record
|
|
|
|
DeleteObject(kvObject KV) error
|
|
|
|
// DeleteObjectAtomic performs an atomic delete operation
|
|
|
|
DeleteObjectAtomic(kvObject KV) error
|
|
|
|
// DeleteTree deletes a record
|
|
|
|
DeleteTree(kvObject KV) error
|
2015-05-08 09:26:35 -04:00
|
|
|
// KVStore returns access to the KV Store
|
|
|
|
KVStore() store.Store
|
|
|
|
}
|
|
|
|
|
2015-06-11 08:32:15 -04:00
|
|
|
// ErrKeyModified is raised for an atomic update when the update is working on a stale state
|
2015-06-17 12:13:31 -04:00
|
|
|
var (
|
|
|
|
ErrKeyModified = store.ErrKeyModified
|
|
|
|
ErrKeyNotFound = store.ErrKeyNotFound
|
|
|
|
)
|
2015-06-11 08:32:15 -04:00
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
type datastore struct {
|
2015-05-23 00:52:02 -04:00
|
|
|
store store.Store
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//KV Key Value interface used by objects to be part of the DataStore
|
|
|
|
type KV interface {
|
2015-05-15 18:23:59 -04:00
|
|
|
// Key method lets an object to provide the Key to be used in KV Store
|
2015-05-08 09:26:35 -04:00
|
|
|
Key() []string
|
2015-06-01 12:43:24 -04:00
|
|
|
// KeyPrefix method lets an object to return immediate parent key that can be used for tree walk
|
|
|
|
KeyPrefix() []string
|
2015-05-15 18:23:59 -04:00
|
|
|
// Value method lets an object to marshal its content to be stored in the KV store
|
2015-05-08 09:26:35 -04:00
|
|
|
Value() []byte
|
2015-06-18 18:13:38 -04:00
|
|
|
// SetValue is used by the datastore to set the object's value when loaded from the data store.
|
|
|
|
SetValue([]byte) error
|
2015-05-15 18:23:59 -04:00
|
|
|
// Index method returns the latest DB Index as seen by the object
|
2015-05-08 09:26:35 -04:00
|
|
|
Index() uint64
|
2015-05-15 18:23:59 -04:00
|
|
|
// SetIndex method allows the datastore to store the latest DB Index into the object
|
2015-05-08 09:26:35 -04:00
|
|
|
SetIndex(uint64)
|
2015-06-18 18:13:38 -04:00
|
|
|
// 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
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-15 18:23:59 -04:00
|
|
|
const (
|
|
|
|
// NetworkKeyPrefix is the prefix for network key in the kv store
|
|
|
|
NetworkKeyPrefix = "network"
|
2015-05-31 14:49:11 -04:00
|
|
|
// EndpointKeyPrefix is the prefix for endpoint key in the kv store
|
|
|
|
EndpointKeyPrefix = "endpoint"
|
2015-05-15 18:23:59 -04:00
|
|
|
)
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
var rootChain = []string{"docker", "libnetwork"}
|
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
//Key provides convenient method to create a Key
|
|
|
|
func Key(key ...string) string {
|
2015-06-05 16:31:12 -04:00
|
|
|
keychain := append(rootChain, key...)
|
2015-05-08 09:26:35 -04:00
|
|
|
str := strings.Join(keychain, "/")
|
|
|
|
return str + "/"
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
//ParseKey provides convenient method to unpack the key to complement the Key function
|
|
|
|
func ParseKey(key string) ([]string, error) {
|
|
|
|
chain := strings.Split(strings.Trim(key, "/"), "/")
|
|
|
|
|
|
|
|
// The key must atleast be equal to the rootChain in order to be considered as valid
|
|
|
|
if len(chain) <= len(rootChain) || !reflect.DeepEqual(chain[0:len(rootChain)], rootChain) {
|
|
|
|
return nil, types.BadRequestErrorf("invalid Key : %s", key)
|
|
|
|
}
|
|
|
|
return chain[len(rootChain):], nil
|
|
|
|
}
|
2015-05-08 09:26:35 -04:00
|
|
|
|
|
|
|
// newClient used to connect to KV Store
|
2015-05-23 00:52:02 -04:00
|
|
|
func newClient(kv string, addrs string) (DataStore, error) {
|
2015-06-11 08:32:15 -04:00
|
|
|
store, err := libkv.NewStore(store.Backend(kv), []string{addrs}, &store.Config{})
|
2015-05-08 09:26:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ds := &datastore{store: store}
|
|
|
|
return ds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDataStore creates a new instance of LibKV data store
|
2015-05-23 00:52:02 -04:00
|
|
|
func NewDataStore(cfg *config.DatastoreCfg) (DataStore, error) {
|
|
|
|
if cfg == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return nil, types.BadRequestErrorf("invalid configuration passed to datastore")
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-05-23 00:52:02 -04:00
|
|
|
// TODO : cfg.Embedded case
|
|
|
|
return newClient(cfg.Client.Provider, cfg.Client.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCustomDataStore can be used by clients to plugin cusom datatore that adhers to store.Store
|
|
|
|
func NewCustomDataStore(customStore store.Store) DataStore {
|
|
|
|
return &datastore{store: customStore}
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *datastore) KVStore() store.Store {
|
|
|
|
return ds.store
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectAtomic adds a new Record based on an object into the datastore
|
|
|
|
func (ds *datastore) PutObjectAtomic(kvObject KV) error {
|
|
|
|
if kvObject == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object : nil")
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
kvObjValue := kvObject.Value()
|
|
|
|
|
|
|
|
if kvObjValue == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object with a nil Value for key %s", Key(kvObject.Key()...))
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 18:13:38 -04:00
|
|
|
var previous *store.KVPair
|
|
|
|
if kvObject.Exists() {
|
|
|
|
previous = &store.KVPair{Key: Key(kvObject.Key()...), LastIndex: kvObject.Index()}
|
|
|
|
} else {
|
|
|
|
previous = nil
|
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
_, pair, err := ds.store.AtomicPut(Key(kvObject.Key()...), kvObjValue, previous, nil)
|
2015-05-08 09:26:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
|
|
|
|
kvObject.SetIndex(pair.LastIndex)
|
2015-05-08 09:26:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObject adds a new Record based on an object into the datastore
|
|
|
|
func (ds *datastore) PutObject(kvObject KV) error {
|
|
|
|
if kvObject == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object : nil")
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
|
|
|
return ds.putObjectWithKey(kvObject, kvObject.Key()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *datastore) putObjectWithKey(kvObject KV, key ...string) error {
|
|
|
|
kvObjValue := kvObject.Value()
|
|
|
|
|
|
|
|
if kvObjValue == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object with a nil Value for key %s", Key(kvObject.Key()...))
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
return ds.store.Put(Key(key...), kvObjValue, nil)
|
2015-05-08 09:26:35 -04:00
|
|
|
}
|
2015-06-01 00:19:10 -04:00
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
// GetObject returns a record matching the key
|
2015-06-18 18:13:38 -04:00
|
|
|
func (ds *datastore) GetObject(key string, o KV) error {
|
2015-06-01 00:19:10 -04:00
|
|
|
kvPair, err := ds.store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-18 18:13:38 -04:00
|
|
|
err = o.SetValue(kvPair.Value)
|
2015-06-16 17:46:51 -04:00
|
|
|
if err != nil {
|
2015-06-18 18:13:38 -04:00
|
|
|
return err
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-06-18 18:13:38 -04:00
|
|
|
|
|
|
|
// Make sure the object has a correct view of the DB index in case we need to modify it
|
|
|
|
// and update the DB.
|
|
|
|
o.SetIndex(kvPair.LastIndex)
|
|
|
|
return nil
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
// DeleteObject unconditionally deletes a record from the store
|
|
|
|
func (ds *datastore) DeleteObject(kvObject KV) error {
|
|
|
|
return ds.store.Delete(Key(kvObject.Key()...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObjectAtomic performs atomic delete on a record
|
|
|
|
func (ds *datastore) DeleteObjectAtomic(kvObject KV) error {
|
|
|
|
if kvObject == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return types.BadRequestErrorf("invalid KV Object : nil")
|
2015-06-01 12:43:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
previous := &store.KVPair{Key: Key(kvObject.Key()...), LastIndex: kvObject.Index()}
|
|
|
|
_, err := ds.store.AtomicDelete(Key(kvObject.Key()...), previous)
|
2015-06-05 16:31:12 -04:00
|
|
|
return err
|
2015-06-01 12:43:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTree unconditionally deletes a record from the store
|
|
|
|
func (ds *datastore) DeleteTree(kvObject KV) error {
|
|
|
|
return ds.store.DeleteTree(Key(kvObject.KeyPrefix()...))
|
|
|
|
}
|