1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #1120 from mrjana/store

Remove kvstore backend deps from datastore package
This commit is contained in:
Alessandro Boch 2016-04-15 15:50:46 -07:00
commit 216def1e1b
19 changed files with 237 additions and 79 deletions

View file

@ -205,27 +205,33 @@
}, },
{ {
"ImportPath": "github.com/docker/libkv", "ImportPath": "github.com/docker/libkv",
"Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" "Comment": "v0.1.0-35-g7283ef2",
"Rev": "7283ef27ed32fe267388510a91709b307bb9942c"
}, },
{ {
"ImportPath": "github.com/docker/libkv/store", "ImportPath": "github.com/docker/libkv/store",
"Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" "Comment": "v0.1.0-35-g7283ef2",
"Rev": "7283ef27ed32fe267388510a91709b307bb9942c"
}, },
{ {
"ImportPath": "github.com/docker/libkv/store/boltdb", "ImportPath": "github.com/docker/libkv/store/boltdb",
"Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" "Comment": "v0.1.0-35-g7283ef2",
"Rev": "7283ef27ed32fe267388510a91709b307bb9942c"
}, },
{ {
"ImportPath": "github.com/docker/libkv/store/consul", "ImportPath": "github.com/docker/libkv/store/consul",
"Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" "Comment": "v0.1.0-35-g7283ef2",
"Rev": "7283ef27ed32fe267388510a91709b307bb9942c"
}, },
{ {
"ImportPath": "github.com/docker/libkv/store/etcd", "ImportPath": "github.com/docker/libkv/store/etcd",
"Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" "Comment": "v0.1.0-35-g7283ef2",
"Rev": "7283ef27ed32fe267388510a91709b307bb9942c"
}, },
{ {
"ImportPath": "github.com/docker/libkv/store/zookeeper", "ImportPath": "github.com/docker/libkv/store/zookeeper",
"Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" "Comment": "v0.1.0-35-g7283ef2",
"Rev": "7283ef27ed32fe267388510a91709b307bb9942c"
}, },
{ {
"ImportPath": "github.com/godbus/dbus", "ImportPath": "github.com/godbus/dbus",

View file

@ -1,9 +1,7 @@
language: go language: go
go: go:
- 1.3 - 1.5.3
# - 1.4
# see https://github.com/moovweb/gvm/pull/116 for why Go 1.4 is currently disabled
# let us have speedy Docker-based Travis workers # let us have speedy Docker-based Travis workers
sudo: false sudo: false
@ -11,19 +9,18 @@ sudo: false
before_install: before_install:
# Symlink below is needed for Travis CI to work correctly on personal forks of libkv # Symlink below is needed for Travis CI to work correctly on personal forks of libkv
- ln -s $HOME/gopath/src/github.com/${TRAVIS_REPO_SLUG///libkv/} $HOME/gopath/src/github.com/docker - ln -s $HOME/gopath/src/github.com/${TRAVIS_REPO_SLUG///libkv/} $HOME/gopath/src/github.com/docker
- go get golang.org/x/tools/cmd/vet
- go get golang.org/x/tools/cmd/cover - go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls - go get github.com/mattn/goveralls
- go get github.com/golang/lint/golint - go get github.com/golang/lint/golint
- go get github.com/GeertJohan/fgt - go get github.com/GeertJohan/fgt
before_script: before_script:
- script/travis_consul.sh 0.5.2 - script/travis_consul.sh 0.6.3
- script/travis_etcd.sh 2.2.0 - script/travis_etcd.sh 2.2.5
- script/travis_zk.sh 3.4.6 - script/travis_zk.sh 3.5.1-alpha
script: script:
- ./consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -config-file=./config.json 1>/dev/null & - ./consul agent -server -bootstrap -advertise=127.0.0.1 -data-dir /tmp/consul -config-file=./config.json 1>/dev/null &
- ./etcd/etcd --listen-client-urls 'http://0.0.0.0:4001' --advertise-client-urls 'http://127.0.0.1:4001' >/dev/null 2>&1 & - ./etcd/etcd --listen-client-urls 'http://0.0.0.0:4001' --advertise-client-urls 'http://127.0.0.1:4001' >/dev/null 2>&1 &
- ./zk/bin/zkServer.sh start ./zk/conf/zoo.cfg 1> /dev/null - ./zk/bin/zkServer.sh start ./zk/conf/zoo.cfg 1> /dev/null
- script/validate-gofmt - script/validate-gofmt

View file

@ -176,7 +176,7 @@
END OF TERMS AND CONDITIONS END OF TERMS AND CONDITIONS
Copyright 2014-2015 Docker, Inc. Copyright 2014-2016 Docker, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

View file

@ -0,0 +1,46 @@
# Libkv maintainers file
#
# This file describes who runs the docker/libkv project and how.
# This is a living document - if you see something out of date or missing, speak up!
#
# It is structured to be consumable by both humans and programs.
# To extract its contents programmatically, use any TOML-compliant parser.
#
# This file is compiled into the MAINTAINERS file in docker/opensource.
#
[Org]
[Org."Core maintainers"]
people = [
"abronan",
"aluzzardi",
"sanimej",
"vieux",
]
[people]
# A reference list of all people associated with the project.
# All other sections should refer to people by their canonical key
# in the people section.
# ADD YOURSELF HERE IN ALPHABETICAL ORDER
[people.abronan]
Name = "Alexandre Beslic"
Email = "abronan@docker.com"
GitHub = "abronan"
[people.aluzzardi]
Name = "Andrea Luzzardi"
Email = "al@docker.com"
GitHub = "aluzzardi"
[people.sanimej]
Name = "Santhosh Manohar"
Email = "santhosh@docker.com"
GitHub = "sanimej"
[people.vieux]
Name = "Victor Vieux"
Email = "vieux@docker.com"
GitHub = "vieux"

View file

@ -3,6 +3,7 @@
[![GoDoc](https://godoc.org/github.com/docker/libkv?status.png)](https://godoc.org/github.com/docker/libkv) [![GoDoc](https://godoc.org/github.com/docker/libkv?status.png)](https://godoc.org/github.com/docker/libkv)
[![Build Status](https://travis-ci.org/docker/libkv.svg?branch=master)](https://travis-ci.org/docker/libkv) [![Build Status](https://travis-ci.org/docker/libkv.svg?branch=master)](https://travis-ci.org/docker/libkv)
[![Coverage Status](https://coveralls.io/repos/docker/libkv/badge.svg)](https://coveralls.io/r/docker/libkv) [![Coverage Status](https://coveralls.io/repos/docker/libkv/badge.svg)](https://coveralls.io/r/docker/libkv)
[![Go Report Card](https://goreportcard.com/badge/github.com/docker/libkv)](https://goreportcard.com/report/github.com/docker/libkv)
`libkv` provides a `Go` native library to store metadata. `libkv` provides a `Go` native library to store metadata.
@ -10,7 +11,7 @@ The goal of `libkv` is to abstract common store operations for multiple distribu
For example, you can use it to store your metadata or for service discovery to register machines and endpoints inside your cluster. For example, you can use it to store your metadata or for service discovery to register machines and endpoints inside your cluster.
You can also easily implement a generic *Leader Election* on top of it (see the [swarm/leadership](https://github.com/docker/swarm/tree/master/leadership) package). You can also easily implement a generic *Leader Election* on top of it (see the [docker/leadership](https://github.com/docker/leadership) repository).
As of now, `libkv` offers support for `Consul`, `Etcd`, `Zookeeper` (**Distributed** store) and `BoltDB` (**Local** store). As of now, `libkv` offers support for `Consul`, `Etcd`, `Zookeeper` (**Distributed** store) and `BoltDB` (**Local** store).
@ -30,7 +31,7 @@ You can find examples of usage for `libkv` under in `docs/examples.go`. Optional
`libkv` supports: `libkv` supports:
- Consul versions >= `0.5.1` because it uses Sessions with `Delete` behavior for the use of `TTLs` (mimics zookeeper's Ephemeral node support), If you don't plan to use `TTLs`: you can use Consul version `0.4.0+`. - Consul versions >= `0.5.1` because it uses Sessions with `Delete` behavior for the use of `TTLs` (mimics zookeeper's Ephemeral node support), If you don't plan to use `TTLs`: you can use Consul version `0.4.0+`.
- Etcd versions >= `2.0` because it uses the new `coreos/etcd/client`, this might change in the future as the support for `APIv3` comes along and adds mor capabilities. - Etcd versions >= `2.0` because it uses the new `coreos/etcd/client`, this might change in the future as the support for `APIv3` comes along and adds more capabilities.
- Zookeeper versions >= `3.4.5`. Although this might work with previous version but this remains untested as of now. - Zookeeper versions >= `3.4.5`. Although this might work with previous version but this remains untested as of now.
- Boltdb, which shouldn't be subject to any version dependencies. - Boltdb, which shouldn't be subject to any version dependencies.
@ -83,7 +84,7 @@ Please refer to the `docs/compatibility.md` to see what are the special cases fo
Other than those special cases, you should expect the same experience for basic operations like `Get`/`Put`, etc. Other than those special cases, you should expect the same experience for basic operations like `Get`/`Put`, etc.
Calls like `WatchTree` may return different events (or number of events) depending on the backend (for now, `Etcd` and `Consul` will likely return more events than `Zookeeper` that you should triage properly). Although you should be able to use it successfully to watch on events in an interchangeable way (see the **swarm/leadership** or **swarm/discovery** packages in **docker/swarm**). Calls like `WatchTree` may return different events (or number of events) depending on the backend (for now, `Etcd` and `Consul` will likely return more events than `Zookeeper` that you should triage properly). Although you should be able to use it successfully to watch on events in an interchangeable way (see the **docker/leadership** repository or the **pkg/discovery/kv** package in **docker/docker**).
## TLS ## TLS
@ -103,4 +104,4 @@ Want to hack on libkv? [Docker's contributions guidelines](https://github.com/do
##Copyright and license ##Copyright and license
Copyright © 2014-2015 Docker, Inc. All rights reserved, except as follows. Code is released under the Apache 2.0 license. The README.md file, and files in the "docs" folder are licensed under the Creative Commons Attribution 4.0 International License under the terms and conditions set forth in the file "LICENSE.docs". You may obtain a duplicate copy of the same license, titled CC-BY-SA-4.0, at http://creativecommons.org/licenses/by/4.0/. Copyright © 2014-2016 Docker, Inc. All rights reserved, except as follows. Code is released under the Apache 2.0 license. The README.md file, and files in the "docs" folder are licensed under the Creative Commons Attribution 4.0 International License under the terms and conditions set forth in the file "LICENSE.docs". You may obtain a duplicate copy of the same license, titled CC-BY-SA-4.0, at http://creativecommons.org/licenses/by/4.0/.

View file

@ -25,7 +25,7 @@ var (
}() }()
) )
// NewStore creates a an instance of store // NewStore creates an instance of store
func NewStore(backend store.Backend, addrs []string, options *store.Config) (store.Store, error) { func NewStore(backend store.Backend, addrs []string, options *store.Config) (store.Store, error) {
if init, exists := initializers[backend]; exists { if init, exists := initializers[backend]; exists {
return init(addrs, options) return init(addrs, options)

View file

@ -19,8 +19,6 @@ var (
// ErrMultipleEndpointsUnsupported is thrown when multiple endpoints specified for // ErrMultipleEndpointsUnsupported is thrown when multiple endpoints specified for
// BoltDB. Endpoint has to be a local file path // BoltDB. Endpoint has to be a local file path
ErrMultipleEndpointsUnsupported = errors.New("boltdb supports one endpoint and should be a file path") ErrMultipleEndpointsUnsupported = errors.New("boltdb supports one endpoint and should be a file path")
// ErrBoltBucketNotFound is thrown when specified BoltBD bucket doesn't exist in the DB
ErrBoltBucketNotFound = errors.New("boltdb bucket doesn't exist")
// ErrBoltBucketOptionMissing is thrown when boltBcuket config option is missing // ErrBoltBucketOptionMissing is thrown when boltBcuket config option is missing
ErrBoltBucketOptionMissing = errors.New("boltBucket config option missing") ErrBoltBucketOptionMissing = errors.New("boltBucket config option missing")
) )
@ -141,7 +139,7 @@ func (b *BoltDB) Get(key string) (*store.KVPair, error) {
err = db.View(func(tx *bolt.Tx) error { err = db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(b.boltBucket) bucket := tx.Bucket(b.boltBucket)
if bucket == nil { if bucket == nil {
return ErrBoltBucketNotFound return store.ErrKeyNotFound
} }
v := bucket.Get([]byte(key)) v := bucket.Get([]byte(key))
@ -217,7 +215,7 @@ func (b *BoltDB) Delete(key string) error {
err = db.Update(func(tx *bolt.Tx) error { err = db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(b.boltBucket) bucket := tx.Bucket(b.boltBucket)
if bucket == nil { if bucket == nil {
return ErrBoltBucketNotFound return store.ErrKeyNotFound
} }
err := bucket.Delete([]byte(key)) err := bucket.Delete([]byte(key))
return err return err
@ -243,7 +241,7 @@ func (b *BoltDB) Exists(key string) (bool, error) {
err = db.View(func(tx *bolt.Tx) error { err = db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(b.boltBucket) bucket := tx.Bucket(b.boltBucket)
if bucket == nil { if bucket == nil {
return ErrBoltBucketNotFound return store.ErrKeyNotFound
} }
val = bucket.Get([]byte(key)) val = bucket.Get([]byte(key))
@ -276,7 +274,7 @@ func (b *BoltDB) List(keyPrefix string) ([]*store.KVPair, error) {
err = db.View(func(tx *bolt.Tx) error { err = db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(b.boltBucket) bucket := tx.Bucket(b.boltBucket)
if bucket == nil { if bucket == nil {
return ErrBoltBucketNotFound return store.ErrKeyNotFound
} }
cursor := bucket.Cursor() cursor := bucket.Cursor()
@ -326,7 +324,7 @@ func (b *BoltDB) AtomicDelete(key string, previous *store.KVPair) (bool, error)
err = db.Update(func(tx *bolt.Tx) error { err = db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(b.boltBucket) bucket := tx.Bucket(b.boltBucket)
if bucket == nil { if bucket == nil {
return ErrBoltBucketNotFound return store.ErrKeyNotFound
} }
val = bucket.Get([]byte(key)) val = bucket.Get([]byte(key))
@ -370,7 +368,7 @@ func (b *BoltDB) AtomicPut(key string, value []byte, previous *store.KVPair, opt
bucket := tx.Bucket(b.boltBucket) bucket := tx.Bucket(b.boltBucket)
if bucket == nil { if bucket == nil {
if previous != nil { if previous != nil {
return ErrBoltBucketNotFound return store.ErrKeyNotFound
} }
bucket, err = tx.CreateBucket(b.boltBucket) bucket, err = tx.CreateBucket(b.boltBucket)
if err != nil { if err != nil {
@ -381,7 +379,7 @@ func (b *BoltDB) AtomicPut(key string, value []byte, previous *store.KVPair, opt
// doesn't exist in the DB. // doesn't exist in the DB.
val = bucket.Get([]byte(key)) val = bucket.Get([]byte(key))
if previous == nil && len(val) != 0 { if previous == nil && len(val) != 0 {
return store.ErrKeyModified return store.ErrKeyExists
} }
if previous != nil { if previous != nil {
if len(val) == 0 { if len(val) == 0 {
@ -440,7 +438,7 @@ func (b *BoltDB) DeleteTree(keyPrefix string) error {
err = db.Update(func(tx *bolt.Tx) error { err = db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(b.boltBucket) bucket := tx.Bucket(b.boltBucket)
if bucket == nil { if bucket == nil {
return ErrBoltBucketNotFound return store.ErrKeyNotFound
} }
cursor := bucket.Cursor() cursor := bucket.Cursor()

View file

@ -22,6 +22,14 @@ const (
// RenewSessionRetryMax is the number of time we should try // RenewSessionRetryMax is the number of time we should try
// to renew the session before giving up and throwing an error // to renew the session before giving up and throwing an error
RenewSessionRetryMax = 5 RenewSessionRetryMax = 5
// MaxSessionDestroyAttempts is the maximum times we will try
// to explicitely destroy the session attached to a lock after
// the connectivity to the store has been lost
MaxSessionDestroyAttempts = 5
// defaultLockTTL is the default ttl for the consul lock
defaultLockTTL = 20 * time.Second
) )
var ( var (
@ -186,6 +194,7 @@ func (s *Consul) Put(key string, value []byte, opts *store.WriteOptions) error {
p := &api.KVPair{ p := &api.KVPair{
Key: key, Key: key,
Value: value, Value: value,
Flags: api.LockFlagValue,
} }
if opts != nil && opts.TTL > 0 { if opts != nil && opts.TTL > 0 {
@ -378,44 +387,99 @@ func (s *Consul) NewLock(key string, options *store.LockOptions) (store.Locker,
lock := &consulLock{} lock := &consulLock{}
ttl := defaultLockTTL
if options != nil { if options != nil {
// Set optional TTL on Lock // Set optional TTL on Lock
if options.TTL != 0 { if options.TTL != 0 {
entry := &api.SessionEntry{ ttl = options.TTL
Behavior: api.SessionBehaviorRelease, // Release the lock when the session expires
TTL: (options.TTL / 2).String(), // Consul multiplies the TTL by 2x
LockDelay: 1 * time.Millisecond, // Virtually disable lock delay
}
// Create the key session
session, _, err := s.client.Session().Create(entry, nil)
if err != nil {
return nil, err
}
// Place the session on lock
lockOpts.Session = session
// Renew the session ttl lock periodically
go s.client.Session().RenewPeriodic(entry.TTL, session, nil, options.RenewLock)
lock.renewCh = options.RenewLock
} }
// Set optional value on Lock // Set optional value on Lock
if options.Value != nil { if options.Value != nil {
lockOpts.Value = options.Value lockOpts.Value = options.Value
} }
} }
entry := &api.SessionEntry{
Behavior: api.SessionBehaviorRelease, // Release the lock when the session expires
TTL: (ttl / 2).String(), // Consul multiplies the TTL by 2x
LockDelay: 1 * time.Millisecond, // Virtually disable lock delay
}
// Create the key session
session, _, err := s.client.Session().Create(entry, nil)
if err != nil {
return nil, err
}
// Place the session and renew chan on lock
lockOpts.Session = session
lock.renewCh = options.RenewLock
l, err := s.client.LockOpts(lockOpts) l, err := s.client.LockOpts(lockOpts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Renew the session ttl lock periodically
s.renewLockSession(entry.TTL, session, options.RenewLock)
lock.lock = l lock.lock = l
return lock, nil return lock, nil
} }
// renewLockSession is used to renew a session Lock, it takes
// a stopRenew chan which is used to explicitely stop the session
// renew process. The renew routine never stops until a signal is
// sent to this channel. If deleting the session fails because the
// connection to the store is lost, it keeps trying to delete the
// session periodically until it can contact the store, this ensures
// that the lock is not maintained indefinitely which ensures liveness
// over safety for the lock when the store becomes unavailable.
func (s *Consul) renewLockSession(initialTTL string, id string, stopRenew chan struct{}) {
sessionDestroyAttempts := 0
ttl, err := time.ParseDuration(initialTTL)
if err != nil {
return
}
go func() {
for {
select {
case <-time.After(ttl / 2):
entry, _, err := s.client.Session().Renew(id, nil)
if err != nil {
// If an error occurs, continue until the
// session gets destroyed explicitely or
// the session ttl times out
continue
}
if entry == nil {
return
}
// Handle the server updating the TTL
ttl, _ = time.ParseDuration(entry.TTL)
case <-stopRenew:
// Attempt a session destroy
_, err := s.client.Session().Destroy(id, nil)
if err == nil {
return
}
if sessionDestroyAttempts >= MaxSessionDestroyAttempts {
return
}
// We can't destroy the session because the store
// is unavailable, wait for the session renew period
sessionDestroyAttempts++
time.Sleep(ttl / 2)
}
}
}()
}
// Lock attempts to acquire the lock and blocks while // Lock attempts to acquire the lock and blocks while
// doing so. It returns a channel that is closed if our // doing so. It returns a channel that is closed if our
// lock is lost or if an error occurs // lock is lost or if an error occurs
@ -436,7 +500,7 @@ func (l *consulLock) Unlock() error {
// modified in the meantime, throws an error if this is the case // modified in the meantime, throws an error if this is the case
func (s *Consul) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) { func (s *Consul) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) {
p := &api.KVPair{Key: s.normalize(key), Value: value} p := &api.KVPair{Key: s.normalize(key), Value: value, Flags: api.LockFlagValue}
if previous == nil { if previous == nil {
// Consul interprets ModifyIndex = 0 as new key. // Consul interprets ModifyIndex = 0 as new key.
@ -445,9 +509,14 @@ func (s *Consul) AtomicPut(key string, value []byte, previous *store.KVPair, opt
p.ModifyIndex = previous.LastIndex p.ModifyIndex = previous.LastIndex
} }
if work, _, err := s.client.KV().CAS(p, nil); err != nil { ok, _, err := s.client.KV().CAS(p, nil)
if err != nil {
return false, nil, err return false, nil, err
} else if !work { }
if !ok {
if previous == nil {
return false, nil, store.ErrKeyExists
}
return false, nil, store.ErrKeyModified return false, nil, store.ErrKeyModified
} }
@ -466,7 +535,7 @@ func (s *Consul) AtomicDelete(key string, previous *store.KVPair) (bool, error)
return false, store.ErrPreviousNotSpecified return false, store.ErrPreviousNotSpecified
} }
p := &api.KVPair{Key: s.normalize(key), ModifyIndex: previous.LastIndex} p := &api.KVPair{Key: s.normalize(key), ModifyIndex: previous.LastIndex, Flags: api.LockFlagValue}
// Extra Get operation to check on the key // Extra Get operation to check on the key
_, err := s.Get(key) _, err := s.Get(key)

View file

@ -75,6 +75,9 @@ func New(addrs []string, options *store.Config) (store.Store, error) {
if options.ConnectionTimeout != 0 { if options.ConnectionTimeout != 0 {
setTimeout(cfg, options.ConnectionTimeout) setTimeout(cfg, options.ConnectionTimeout)
} }
if options.Username != "" {
setCredentials(cfg, options.Username, options.Password)
}
} }
c, err := etcd.New(*cfg) c, err := etcd.New(*cfg)
@ -119,6 +122,12 @@ func setTimeout(cfg *etcd.Config, time time.Duration) {
cfg.HeaderTimeoutPerRequest = time cfg.HeaderTimeoutPerRequest = time
} }
// setCredentials sets the username/password credentials for connecting to Etcd
func setCredentials(cfg *etcd.Config, username, password string) {
cfg.Username = username
cfg.Password = password
}
// Normalize the key for usage in Etcd // Normalize the key for usage in Etcd
func (s *Etcd) normalize(key string) string { func (s *Etcd) normalize(key string) string {
key = store.Normalize(key) key = store.Normalize(key)
@ -335,6 +344,10 @@ func (s *Etcd) AtomicPut(key string, value []byte, previous *store.KVPair, opts
if etcdError.Code == etcd.ErrorCodeTestFailed { if etcdError.Code == etcd.ErrorCodeTestFailed {
return false, nil, store.ErrKeyModified return false, nil, store.ErrKeyModified
} }
// Node exists error (when PrevNoExist)
if etcdError.Code == etcd.ErrorCodeNodeExist {
return false, nil, store.ErrKeyExists
}
} }
return false, nil, err return false, nil, err
} }
@ -508,15 +521,15 @@ func (l *etcdLock) Lock(stopChan chan struct{}) (<-chan struct{}, error) {
// Wait for the key to be available or for // Wait for the key to be available or for
// a signal to stop trying to lock the key // a signal to stop trying to lock the key
select { select {
case _ = <-free: case <-free:
break break
case err := <-errorCh: case err := <-errorCh:
return nil, err return nil, err
case _ = <-stopChan: case <-stopChan:
return nil, ErrAbortTryLock return nil, ErrAbortTryLock
} }
// Delete or Expire event occured // Delete or Expire event occurred
// Retry // Retry
} }
} }

View file

@ -35,6 +35,8 @@ var (
ErrKeyNotFound = errors.New("Key not found in store") ErrKeyNotFound = errors.New("Key not found in store")
// ErrPreviousNotSpecified is thrown when the previous value is not specified for an atomic operation // ErrPreviousNotSpecified is thrown when the previous value is not specified for an atomic operation
ErrPreviousNotSpecified = errors.New("Previous K/V pair should be provided for the Atomic operation") ErrPreviousNotSpecified = errors.New("Previous K/V pair should be provided for the Atomic operation")
// ErrKeyExists is thrown when the previous value exists in the case of an AtomicPut
ErrKeyExists = errors.New("Previous K/V pair exists, cannot complete Atomic operation")
) )
// Config contains the options for a storage client // Config contains the options for a storage client
@ -44,6 +46,8 @@ type Config struct {
ConnectionTimeout time.Duration ConnectionTimeout time.Duration
Bucket string Bucket string
PersistConnection bool PersistConnection bool
Username string
Password string
} }
// ClientTLSConfig contains data for a Client TLS configuration in the form // ClientTLSConfig contains data for a Client TLS configuration in the form

View file

@ -291,8 +291,8 @@ func (s *Zookeeper) DeleteTree(directory string) error {
// AtomicPut put a value at "key" if the key has not been // AtomicPut put a value at "key" if the key has not been
// modified in the meantime, throws an error if this is the case // modified in the meantime, throws an error if this is the case
func (s *Zookeeper) AtomicPut(key string, value []byte, previous *store.KVPair, _ *store.WriteOptions) (bool, *store.KVPair, error) { func (s *Zookeeper) AtomicPut(key string, value []byte, previous *store.KVPair, _ *store.WriteOptions) (bool, *store.KVPair, error) {
var lastIndex uint64 var lastIndex uint64
if previous != nil { if previous != nil {
meta, err := s.client.Set(s.normalize(key), value, int32(previous.LastIndex)) meta, err := s.client.Set(s.normalize(key), value, int32(previous.LastIndex))
if err != nil { if err != nil {
@ -307,8 +307,9 @@ func (s *Zookeeper) AtomicPut(key string, value []byte, previous *store.KVPair,
// Interpret previous == nil as create operation. // Interpret previous == nil as create operation.
_, err := s.client.Create(s.normalize(key), value, 0, zk.WorldACL(zk.PermAll)) _, err := s.client.Create(s.normalize(key), value, 0, zk.WorldACL(zk.PermAll))
if err != nil { if err != nil {
// Zookeeper will complain if the directory doesn't exist. // Directory does not exist
if err == zk.ErrNoNode { if err == zk.ErrNoNode {
// Create the directory // Create the directory
parts := store.SplitKey(strings.TrimSuffix(key, "/")) parts := store.SplitKey(strings.TrimSuffix(key, "/"))
parts = parts[:len(parts)-1] parts = parts[:len(parts)-1]
@ -316,11 +317,22 @@ func (s *Zookeeper) AtomicPut(key string, value []byte, previous *store.KVPair,
// Failed to create the directory. // Failed to create the directory.
return false, nil, err return false, nil, err
} }
// Create the node
if _, err := s.client.Create(s.normalize(key), value, 0, zk.WorldACL(zk.PermAll)); err != nil { if _, err := s.client.Create(s.normalize(key), value, 0, zk.WorldACL(zk.PermAll)); err != nil {
// Node exist error (when previous nil)
if err == zk.ErrNodeExists {
return false, nil, store.ErrKeyExists
}
return false, nil, err return false, nil, err
} }
} else { } else {
// Node Exists error (when previous nil)
if err == zk.ErrNodeExists {
return false, nil, store.ErrKeyExists
}
// Unhandled error // Unhandled error
return false, nil, err return false, nil, err
} }

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/docker/libkv/store" "github.com/docker/libkv/store"
"github.com/docker/libkv/store/boltdb"
"github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/datastore"
_ "github.com/docker/libnetwork/testutils" _ "github.com/docker/libnetwork/testutils"
) )
@ -16,6 +17,10 @@ const (
defaultPrefix = "/tmp/libnetwork/test/bitseq" defaultPrefix = "/tmp/libnetwork/test/bitseq"
) )
func init() {
boltdb.Register()
}
func randomLocalStore() (datastore.DataStore, error) { func randomLocalStore() (datastore.DataStore, error) {
tmp, err := ioutil.TempFile("", "libnetwork-") tmp, err := ioutil.TempFile("", "libnetwork-")
if err != nil { if err != nil {

View file

@ -5,7 +5,6 @@ import (
"sync" "sync"
"github.com/docker/libkv/store" "github.com/docker/libkv/store"
"github.com/docker/libkv/store/boltdb"
) )
type kvMap map[string]KVObject type kvMap map[string]KVObject
@ -42,9 +41,7 @@ func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
kvList, err := c.ds.store.List(keyPrefix) kvList, err := c.ds.store.List(keyPrefix)
if err != nil { if err != nil {
// In case of BoltDB it may return ErrBoltBucketNotFound when no writes if err == store.ErrKeyNotFound {
// have ever happened on the db bucket. So check for both err codes
if err == store.ErrKeyNotFound || err == boltdb.ErrBoltBucketNotFound {
// If the store doesn't have anything then there is nothing to // If the store doesn't have anything then there is nothing to
// populate in the cache. Just bail out. // populate in the cache. Just bail out.
goto out goto out

View file

@ -9,10 +9,6 @@ import (
"github.com/docker/libkv" "github.com/docker/libkv"
"github.com/docker/libkv/store" "github.com/docker/libkv/store"
"github.com/docker/libkv/store/boltdb"
"github.com/docker/libkv/store/consul"
"github.com/docker/libkv/store/etcd"
"github.com/docker/libkv/store/zookeeper"
"github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/types" "github.com/docker/libnetwork/types"
) )
@ -148,13 +144,6 @@ func makeDefaultScopes() map[string]*ScopeCfg {
var defaultRootChain = []string{"docker", "network", "v1.0"} var defaultRootChain = []string{"docker", "network", "v1.0"}
var rootChain = defaultRootChain var rootChain = defaultRootChain
func init() {
consul.Register()
zookeeper.Register()
etcd.Register()
boltdb.Register()
}
// DefaultScopes returns a map of default scopes and it's config for clients to use. // DefaultScopes returns a map of default scopes and it's config for clients to use.
func DefaultScopes(dataDir string) map[string]*ScopeCfg { func DefaultScopes(dataDir string) map[string]*ScopeCfg {
if dataDir != "" { if dataDir != "" {
@ -411,6 +400,9 @@ func (ds *datastore) PutObjectAtomic(kvObject KVObject) error {
_, pair, err = ds.store.AtomicPut(Key(kvObject.Key()...), kvObjValue, previous, nil) _, pair, err = ds.store.AtomicPut(Key(kvObject.Key()...), kvObjValue, previous, nil)
if err != nil { if err != nil {
if err == store.ErrKeyExists {
return ErrKeyModified
}
return err return err
} }
@ -571,6 +563,9 @@ func (ds *datastore) DeleteObjectAtomic(kvObject KVObject) error {
} }
if _, err := ds.store.AtomicDelete(Key(kvObject.Key()...), previous); err != nil { if _, err := ds.store.AtomicDelete(Key(kvObject.Key()...), previous); err != nil {
if err == store.ErrKeyExists {
return ErrKeyModified
}
return err return err
} }

View file

@ -6,7 +6,6 @@ import (
"net" "net"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/libkv/store/boltdb"
"github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/netlabel" "github.com/docker/libnetwork/netlabel"
@ -35,7 +34,7 @@ func (d *driver) initStore(option map[string]interface{}) error {
func (d *driver) populateNetworks() error { func (d *driver) populateNetworks() error {
kvol, err := d.store.List(datastore.Key(bridgePrefix), &networkConfiguration{}) kvol, err := d.store.List(datastore.Key(bridgePrefix), &networkConfiguration{})
if err != nil && err != datastore.ErrKeyNotFound && err != boltdb.ErrBoltBucketNotFound { if err != nil && err != datastore.ErrKeyNotFound {
return fmt.Errorf("failed to get bridge network configurations from store: %v", err) return fmt.Errorf("failed to get bridge network configurations from store: %v", err)
} }

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/libkv/store/boltdb"
"github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/netlabel" "github.com/docker/libnetwork/netlabel"
@ -60,7 +59,7 @@ func (d *driver) initStore(option map[string]interface{}) error {
// populateNetworks is invoked at driver init to recreate persistently stored networks // populateNetworks is invoked at driver init to recreate persistently stored networks
func (d *driver) populateNetworks() error { func (d *driver) populateNetworks() error {
kvol, err := d.store.List(datastore.Key(ipvlanPrefix), &configuration{}) kvol, err := d.store.List(datastore.Key(ipvlanPrefix), &configuration{})
if err != nil && err != datastore.ErrKeyNotFound && err != boltdb.ErrBoltBucketNotFound { if err != nil && err != datastore.ErrKeyNotFound {
return fmt.Errorf("failed to get ipvlan network configurations from store: %v", err) return fmt.Errorf("failed to get ipvlan network configurations from store: %v", err)
} }
// If empty it simply means no ipvlan networks have been created yet // If empty it simply means no ipvlan networks have been created yet

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/libkv/store/boltdb"
"github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/netlabel" "github.com/docker/libnetwork/netlabel"
@ -60,7 +59,7 @@ func (d *driver) initStore(option map[string]interface{}) error {
// populateNetworks is invoked at driver init to recreate persistently stored networks // populateNetworks is invoked at driver init to recreate persistently stored networks
func (d *driver) populateNetworks() error { func (d *driver) populateNetworks() error {
kvol, err := d.store.List(datastore.Key(macvlanPrefix), &configuration{}) kvol, err := d.store.List(datastore.Key(macvlanPrefix), &configuration{})
if err != nil && err != datastore.ErrKeyNotFound && err != boltdb.ErrBoltBucketNotFound { if err != nil && err != datastore.ErrKeyNotFound {
return fmt.Errorf("failed to get macvlan network configurations from store: %v", err) return fmt.Errorf("failed to get macvlan network configurations from store: %v", err)
} }
// If empty it simply means no macvlan networks have been created yet // If empty it simply means no macvlan networks have been created yet

View file

@ -6,6 +6,7 @@ import (
"sync" "sync"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/docker/libkv/store/boltdb"
"github.com/docker/libnetwork/bitseq" "github.com/docker/libnetwork/bitseq"
"github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/discoverapi"
@ -25,6 +26,10 @@ const (
dsDataKey = "ipam/" + ipamapi.DefaultIPAM + "/data" dsDataKey = "ipam/" + ipamapi.DefaultIPAM + "/data"
) )
func init() {
boltdb.Register()
}
// Allocator provides per address space ipv4/ipv6 book keeping // Allocator provides per address space ipv4/ipv6 book keeping
type Allocator struct { type Allocator struct {
// Predefined pools for default address spaces // Predefined pools for default address spaces

View file

@ -4,9 +4,20 @@ import (
"fmt" "fmt"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/docker/libkv/store/boltdb"
"github.com/docker/libkv/store/consul"
"github.com/docker/libkv/store/etcd"
"github.com/docker/libkv/store/zookeeper"
"github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/datastore"
) )
func registerKVStores() {
consul.Register()
zookeeper.Register()
etcd.Register()
boltdb.Register()
}
func (c *controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) error { func (c *controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) error {
store, err := datastore.NewDataStore(scope, scfg) store, err := datastore.NewDataStore(scope, scfg)
if err != nil { if err != nil {
@ -20,6 +31,8 @@ func (c *controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) err
} }
func (c *controller) initStores() error { func (c *controller) initStores() error {
registerKVStores()
c.Lock() c.Lock()
if c.cfg == nil { if c.cfg == nil {
c.Unlock() c.Unlock()