diff --git a/libnetwork/Godeps/Godeps.json b/libnetwork/Godeps/Godeps.json index 7414f87243..4e8654b474 100644 --- a/libnetwork/Godeps/Godeps.json +++ b/libnetwork/Godeps/Godeps.json @@ -205,27 +205,33 @@ }, { "ImportPath": "github.com/docker/libkv", - "Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" + "Comment": "v0.1.0-35-g7283ef2", + "Rev": "7283ef27ed32fe267388510a91709b307bb9942c" }, { "ImportPath": "github.com/docker/libkv/store", - "Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" + "Comment": "v0.1.0-35-g7283ef2", + "Rev": "7283ef27ed32fe267388510a91709b307bb9942c" }, { "ImportPath": "github.com/docker/libkv/store/boltdb", - "Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" + "Comment": "v0.1.0-35-g7283ef2", + "Rev": "7283ef27ed32fe267388510a91709b307bb9942c" }, { "ImportPath": "github.com/docker/libkv/store/consul", - "Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" + "Comment": "v0.1.0-35-g7283ef2", + "Rev": "7283ef27ed32fe267388510a91709b307bb9942c" }, { "ImportPath": "github.com/docker/libkv/store/etcd", - "Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" + "Comment": "v0.1.0-35-g7283ef2", + "Rev": "7283ef27ed32fe267388510a91709b307bb9942c" }, { "ImportPath": "github.com/docker/libkv/store/zookeeper", - "Rev": "c2aac5dbbaa5c872211edea7c0f32b3bd67e7410" + "Comment": "v0.1.0-35-g7283ef2", + "Rev": "7283ef27ed32fe267388510a91709b307bb9942c" }, { "ImportPath": "github.com/godbus/dbus", diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/.travis.yml b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/.travis.yml index a9ef7c82bf..f7cecbdf9c 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/.travis.yml +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/.travis.yml @@ -1,9 +1,7 @@ language: go go: - - 1.3 -# - 1.4 -# see https://github.com/moovweb/gvm/pull/116 for why Go 1.4 is currently disabled + - 1.5.3 # let us have speedy Docker-based Travis workers sudo: false @@ -11,19 +9,18 @@ sudo: false before_install: # 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 - - go get golang.org/x/tools/cmd/vet - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls - go get github.com/golang/lint/golint - go get github.com/GeertJohan/fgt before_script: - - script/travis_consul.sh 0.5.2 - - script/travis_etcd.sh 2.2.0 - - script/travis_zk.sh 3.4.6 + - script/travis_consul.sh 0.6.3 + - script/travis_etcd.sh 2.2.5 + - script/travis_zk.sh 3.5.1-alpha 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 & - ./zk/bin/zkServer.sh start ./zk/conf/zoo.cfg 1> /dev/null - script/validate-gofmt diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/LICENSE.code b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/LICENSE.code index 9e4bd4dbee..34c4ea7c50 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/LICENSE.code +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/LICENSE.code @@ -176,7 +176,7 @@ 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"); you may not use this file except in compliance with the License. diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/MAINTAINERS b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/MAINTAINERS new file mode 100644 index 0000000000..4dd59c7e27 --- /dev/null +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/MAINTAINERS @@ -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" diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/README.md b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/README.md index 02cf3fd0ea..baad81b4f8 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/README.md +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/README.md @@ -3,6 +3,7 @@ [![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) [![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. @@ -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. -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). @@ -30,7 +31,7 @@ You can find examples of usage for `libkv` under in `docs/examples.go`. Optional `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+`. -- 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. - 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. -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 @@ -103,4 +104,4 @@ Want to hack on libkv? [Docker's contributions guidelines](https://github.com/do ##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/. diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/libkv.go b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/libkv.go index 2192cd14e8..bdb8c7529f 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/libkv.go +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/libkv.go @@ -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) { if init, exists := initializers[backend]; exists { return init(addrs, options) diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/boltdb/boltdb.go b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/boltdb/boltdb.go index 94d01b4b09..4026e0a20c 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/boltdb/boltdb.go +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/boltdb/boltdb.go @@ -19,8 +19,6 @@ var ( // ErrMultipleEndpointsUnsupported is thrown when multiple endpoints specified for // BoltDB. Endpoint has to be a local 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 = 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 { bucket := tx.Bucket(b.boltBucket) if bucket == nil { - return ErrBoltBucketNotFound + return store.ErrKeyNotFound } v := bucket.Get([]byte(key)) @@ -217,7 +215,7 @@ func (b *BoltDB) Delete(key string) error { err = db.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket(b.boltBucket) if bucket == nil { - return ErrBoltBucketNotFound + return store.ErrKeyNotFound } err := bucket.Delete([]byte(key)) return err @@ -243,7 +241,7 @@ func (b *BoltDB) Exists(key string) (bool, error) { err = db.View(func(tx *bolt.Tx) error { bucket := tx.Bucket(b.boltBucket) if bucket == nil { - return ErrBoltBucketNotFound + return store.ErrKeyNotFound } 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 { bucket := tx.Bucket(b.boltBucket) if bucket == nil { - return ErrBoltBucketNotFound + return store.ErrKeyNotFound } 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 { bucket := tx.Bucket(b.boltBucket) if bucket == nil { - return ErrBoltBucketNotFound + return store.ErrKeyNotFound } 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) if bucket == nil { if previous != nil { - return ErrBoltBucketNotFound + return store.ErrKeyNotFound } bucket, err = tx.CreateBucket(b.boltBucket) 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. val = bucket.Get([]byte(key)) if previous == nil && len(val) != 0 { - return store.ErrKeyModified + return store.ErrKeyExists } if previous != nil { if len(val) == 0 { @@ -440,7 +438,7 @@ func (b *BoltDB) DeleteTree(keyPrefix string) error { err = db.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket(b.boltBucket) if bucket == nil { - return ErrBoltBucketNotFound + return store.ErrKeyNotFound } cursor := bucket.Cursor() diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/consul/consul.go b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/consul/consul.go index c7693ca441..cb64be72d6 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/consul/consul.go +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/consul/consul.go @@ -22,6 +22,14 @@ const ( // RenewSessionRetryMax is the number of time we should try // to renew the session before giving up and throwing an error 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 ( @@ -186,6 +194,7 @@ func (s *Consul) Put(key string, value []byte, opts *store.WriteOptions) error { p := &api.KVPair{ Key: key, Value: value, + Flags: api.LockFlagValue, } if opts != nil && opts.TTL > 0 { @@ -378,44 +387,99 @@ func (s *Consul) NewLock(key string, options *store.LockOptions) (store.Locker, lock := &consulLock{} + ttl := defaultLockTTL + if options != nil { // Set optional TTL on Lock if options.TTL != 0 { - entry := &api.SessionEntry{ - 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 + ttl = options.TTL } - // Set optional value on Lock if options.Value != nil { 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) if err != nil { return nil, err } + // Renew the session ttl lock periodically + s.renewLockSession(entry.TTL, session, options.RenewLock) + lock.lock = l 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 // doing so. It returns a channel that is closed if our // 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 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 { // 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 } - 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 - } else if !work { + } + if !ok { + if previous == nil { + return false, nil, store.ErrKeyExists + } return false, nil, store.ErrKeyModified } @@ -466,7 +535,7 @@ func (s *Consul) AtomicDelete(key string, previous *store.KVPair) (bool, error) 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 _, err := s.Get(key) diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/etcd/etcd.go b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/etcd/etcd.go index 312bb0b65a..c932ca665e 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/etcd/etcd.go +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/etcd/etcd.go @@ -75,6 +75,9 @@ func New(addrs []string, options *store.Config) (store.Store, error) { if options.ConnectionTimeout != 0 { setTimeout(cfg, options.ConnectionTimeout) } + if options.Username != "" { + setCredentials(cfg, options.Username, options.Password) + } } c, err := etcd.New(*cfg) @@ -119,6 +122,12 @@ func setTimeout(cfg *etcd.Config, time time.Duration) { 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 func (s *Etcd) normalize(key string) string { 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 { return false, nil, store.ErrKeyModified } + // Node exists error (when PrevNoExist) + if etcdError.Code == etcd.ErrorCodeNodeExist { + return false, nil, store.ErrKeyExists + } } 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 // a signal to stop trying to lock the key select { - case _ = <-free: + case <-free: break case err := <-errorCh: return nil, err - case _ = <-stopChan: + case <-stopChan: return nil, ErrAbortTryLock } - // Delete or Expire event occured + // Delete or Expire event occurred // Retry } } diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/store.go b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/store.go index 0df01b6c83..7a4850c019 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/store.go +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/store.go @@ -35,6 +35,8 @@ var ( ErrKeyNotFound = errors.New("Key not found in store") // 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") + // 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 @@ -44,6 +46,8 @@ type Config struct { ConnectionTimeout time.Duration Bucket string PersistConnection bool + Username string + Password string } // ClientTLSConfig contains data for a Client TLS configuration in the form diff --git a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/zookeeper/zookeeper.go b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/zookeeper/zookeeper.go index 502b1c6e85..8a44ad318a 100644 --- a/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/zookeeper/zookeeper.go +++ b/libnetwork/Godeps/_workspace/src/github.com/docker/libkv/store/zookeeper/zookeeper.go @@ -291,8 +291,8 @@ func (s *Zookeeper) DeleteTree(directory string) error { // AtomicPut put a value at "key" if the key has not been // 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) { - var lastIndex uint64 + if previous != nil { meta, err := s.client.Set(s.normalize(key), value, int32(previous.LastIndex)) if err != nil { @@ -307,8 +307,9 @@ func (s *Zookeeper) AtomicPut(key string, value []byte, previous *store.KVPair, // Interpret previous == nil as create operation. _, err := s.client.Create(s.normalize(key), value, 0, zk.WorldACL(zk.PermAll)) if err != nil { - // Zookeeper will complain if the directory doesn't exist. + // Directory does not exist if err == zk.ErrNoNode { + // Create the directory parts := store.SplitKey(strings.TrimSuffix(key, "/")) 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. return false, nil, err } + + // Create the node 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 } } else { + // Node Exists error (when previous nil) + if err == zk.ErrNodeExists { + return false, nil, store.ErrKeyExists + } + // Unhandled error return false, nil, err } diff --git a/libnetwork/bitseq/sequence_test.go b/libnetwork/bitseq/sequence_test.go index d435329d88..339f5928ef 100644 --- a/libnetwork/bitseq/sequence_test.go +++ b/libnetwork/bitseq/sequence_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/docker/libkv/store" + "github.com/docker/libkv/store/boltdb" "github.com/docker/libnetwork/datastore" _ "github.com/docker/libnetwork/testutils" ) @@ -16,6 +17,10 @@ const ( defaultPrefix = "/tmp/libnetwork/test/bitseq" ) +func init() { + boltdb.Register() +} + func randomLocalStore() (datastore.DataStore, error) { tmp, err := ioutil.TempFile("", "libnetwork-") if err != nil { diff --git a/libnetwork/datastore/cache.go b/libnetwork/datastore/cache.go index 08c8ac4839..2d00038290 100644 --- a/libnetwork/datastore/cache.go +++ b/libnetwork/datastore/cache.go @@ -5,7 +5,6 @@ import ( "sync" "github.com/docker/libkv/store" - "github.com/docker/libkv/store/boltdb" ) type kvMap map[string]KVObject @@ -42,9 +41,7 @@ func (c *cache) kmap(kvObject KVObject) (kvMap, error) { kvList, err := c.ds.store.List(keyPrefix) if err != nil { - // In case of BoltDB it may return ErrBoltBucketNotFound when no writes - // have ever happened on the db bucket. So check for both err codes - if err == store.ErrKeyNotFound || err == boltdb.ErrBoltBucketNotFound { + if err == store.ErrKeyNotFound { // If the store doesn't have anything then there is nothing to // populate in the cache. Just bail out. goto out diff --git a/libnetwork/datastore/datastore.go b/libnetwork/datastore/datastore.go index c15cd620c1..49affc7883 100644 --- a/libnetwork/datastore/datastore.go +++ b/libnetwork/datastore/datastore.go @@ -9,10 +9,6 @@ import ( "github.com/docker/libkv" "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/types" ) @@ -148,13 +144,6 @@ func makeDefaultScopes() map[string]*ScopeCfg { var defaultRootChain = []string{"docker", "network", "v1.0"} 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. func DefaultScopes(dataDir string) map[string]*ScopeCfg { if dataDir != "" { @@ -411,6 +400,9 @@ func (ds *datastore) PutObjectAtomic(kvObject KVObject) error { _, pair, err = ds.store.AtomicPut(Key(kvObject.Key()...), kvObjValue, previous, nil) if err != nil { + if err == store.ErrKeyExists { + return ErrKeyModified + } 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 == store.ErrKeyExists { + return ErrKeyModified + } return err } diff --git a/libnetwork/drivers/bridge/bridge_store.go b/libnetwork/drivers/bridge/bridge_store.go index eca72bd30b..de9635289a 100644 --- a/libnetwork/drivers/bridge/bridge_store.go +++ b/libnetwork/drivers/bridge/bridge_store.go @@ -6,7 +6,6 @@ import ( "net" "github.com/Sirupsen/logrus" - "github.com/docker/libkv/store/boltdb" "github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/discoverapi" "github.com/docker/libnetwork/netlabel" @@ -35,7 +34,7 @@ func (d *driver) initStore(option map[string]interface{}) error { func (d *driver) populateNetworks() error { 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) } diff --git a/libnetwork/drivers/ipvlan/ipvlan_store.go b/libnetwork/drivers/ipvlan/ipvlan_store.go index f6746da670..c6430835ae 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_store.go +++ b/libnetwork/drivers/ipvlan/ipvlan_store.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/Sirupsen/logrus" - "github.com/docker/libkv/store/boltdb" "github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/discoverapi" "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 func (d *driver) populateNetworks() error { 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) } // If empty it simply means no ipvlan networks have been created yet diff --git a/libnetwork/drivers/macvlan/macvlan_store.go b/libnetwork/drivers/macvlan/macvlan_store.go index 492ea93f71..5f92feadd4 100644 --- a/libnetwork/drivers/macvlan/macvlan_store.go +++ b/libnetwork/drivers/macvlan/macvlan_store.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/Sirupsen/logrus" - "github.com/docker/libkv/store/boltdb" "github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/discoverapi" "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 func (d *driver) populateNetworks() error { 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) } // If empty it simply means no macvlan networks have been created yet diff --git a/libnetwork/ipam/allocator.go b/libnetwork/ipam/allocator.go index 9ff3301baa..255f8932c0 100644 --- a/libnetwork/ipam/allocator.go +++ b/libnetwork/ipam/allocator.go @@ -6,6 +6,7 @@ import ( "sync" log "github.com/Sirupsen/logrus" + "github.com/docker/libkv/store/boltdb" "github.com/docker/libnetwork/bitseq" "github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/discoverapi" @@ -25,6 +26,10 @@ const ( dsDataKey = "ipam/" + ipamapi.DefaultIPAM + "/data" ) +func init() { + boltdb.Register() +} + // Allocator provides per address space ipv4/ipv6 book keeping type Allocator struct { // Predefined pools for default address spaces diff --git a/libnetwork/store.go b/libnetwork/store.go index 2c439dcbd4..0e7ec1921b 100644 --- a/libnetwork/store.go +++ b/libnetwork/store.go @@ -4,9 +4,20 @@ import ( "fmt" 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" ) +func registerKVStores() { + consul.Register() + zookeeper.Register() + etcd.Register() + boltdb.Register() +} + func (c *controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) error { store, err := datastore.NewDataStore(scope, scfg) if err != nil { @@ -20,6 +31,8 @@ func (c *controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) err } func (c *controller) initStores() error { + registerKVStores() + c.Lock() if c.cfg == nil { c.Unlock()