From 27397dcbdffe631a28b9cc60d7c151787bec079d Mon Sep 17 00:00:00 2001 From: Madhu Venugopal Date: Tue, 22 Sep 2015 10:49:15 -0700 Subject: [PATCH] libnetwork to make use of boltdb ConnectionTimeout to avoid deadlock Signed-off-by: Madhu Venugopal --- libnetwork/store.go | 5 ++++- libnetwork/store_test.go | 29 +++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/libnetwork/store.go b/libnetwork/store.go index 4c2bcd58b7..4e8a98453e 100644 --- a/libnetwork/store.go +++ b/libnetwork/store.go @@ -3,6 +3,7 @@ package libnetwork import ( "encoding/json" "fmt" + "time" log "github.com/Sirupsen/logrus" "github.com/docker/libkv/store" @@ -11,13 +12,15 @@ import ( ) var ( + defaultBoltTimeout = 3 * time.Second defaultLocalStoreConfig = config.DatastoreCfg{ Embedded: true, Client: config.DatastoreClientCfg{ Provider: "boltdb", Address: defaultPrefix + "/boltdb.db", Config: &store.Config{ - Bucket: "libnetwork", + Bucket: "libnetwork", + ConnectionTimeout: defaultBoltTimeout, }, }, } diff --git a/libnetwork/store_test.go b/libnetwork/store_test.go index 0c9ad943cd..d43057010c 100644 --- a/libnetwork/store_test.go +++ b/libnetwork/store_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "testing" + "time" "github.com/docker/libkv/store" "github.com/docker/libnetwork/config" @@ -34,7 +35,9 @@ func TestBoltdbBackend(t *testing.T) { defer os.Remove(defaultLocalStoreConfig.Client.Address) testLocalBackend(t, "", "", nil) defer os.Remove("/tmp/boltdb.db") - testLocalBackend(t, "boltdb", "/tmp/boltdb.db", &store.Config{Bucket: "testBackend"}) + config := &store.Config{Bucket: "testBackend", ConnectionTimeout: 3 * time.Second} + testLocalBackend(t, "boltdb", "/tmp/boltdb.db", config) + } func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) { @@ -91,6 +94,28 @@ func OptionBoltdbWithRandomDBFile() ([]config.Option, error) { cfgOptions := []config.Option{} cfgOptions = append(cfgOptions, config.OptionLocalKVProvider("boltdb")) cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(tmp.Name())) - cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(&store.Config{Bucket: "testBackend"})) + sCfg := &store.Config{Bucket: "testBackend", ConnectionTimeout: 3 * time.Second} + cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(sCfg)) return cfgOptions, nil } + +func TestLocalStoreLockTimeout(t *testing.T) { + cfgOptions, err := OptionBoltdbWithRandomDBFile() + if err != nil { + t.Fatalf("Error getting random boltdb configs %v", err) + } + ctrl, err := New(cfgOptions...) + if err != nil { + t.Fatalf("Error new controller: %v", err) + } + // Use the same boltdb file without closing the previous controller + _, err = New(cfgOptions...) + if err == nil { + t.Fatalf("Multiple boldtdb connection must fail") + } + store := ctrl.(*controller).localStore.KVStore() + if store == nil { + t.Fatalf("Invalid LocalStore access connection") + } + store.Close() +}