Skip to content

Commit

Permalink
Implement 'bootstrap_on' connection string option
Browse files Browse the repository at this point in the history
This will allow the forcing of bootstrapping methods

Change-Id: Ia78497222a58f38f2bb4a3b05734ecddcc603f4f
Reviewed-on: http://review.couchbase.org/59530
Reviewed-by: Brett Lawson <brett19@gmail.com>
Tested-by: Mark Nunberg <mark.nunberg@couchbase.com>
  • Loading branch information
mnunberg authored and Mark Nunberg committed Feb 13, 2016
1 parent 5c25588 commit 2694c60
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
68 changes: 68 additions & 0 deletions bucket_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package gocb

// Get the mock first!
import (
"fmt"
"testing"
)

func TestBadConnstr(t *testing.T) {
_, err := Connect(fmt.Sprintf("couchbase://host.com/bucket"))
if err == nil {
t.Fatal("Explicit bucket should fail for Connect")
}

_, err = Connect("blah://bad_conn_str")
if err == nil {
t.Fatal("Connection should fail with bad scheme!")
}
_, err = Connect("couchbase://host.com?bootstrap_on=dummy")
if err == nil {
t.Fatal("Connection should fail with bad 'bootstrap_on' option")
}
}

func TestBootstrapOn(t *testing.T) {
connstr := "couchbase://foo.com,bar.com,baz.com"
c, err := Connect(connstr)
if err != nil {
t.Fatalf("Multi-host connection string failed: %v", err)
}
if len(c.spec.HttpHosts) != 3 || len(c.spec.MemcachedHosts) != 3 {
t.Fatal("Wrong number of hosts for http/memcached")
}

// Use http only
c, err = Connect("couchbase://foo.com,bar.com,baz.com?bootstrap_on=http")
if err != nil {
t.Fatalf("bootstrap_on=http: %v", err)
}
if len(c.spec.HttpHosts) != 3 {
t.Fatalf("HttpHosts is not 3 (%v)", c.spec.HttpHosts)
}
if len(c.spec.MemcachedHosts) != 0 {
t.Fatalf("MemcachedHosts is not 0: %v", c.spec.MemcachedHosts)
}

c, err = Connect("couchbase://foo.com,bar.com,baz.com?bootstrap_on=cccp")
if err != nil {
t.Fatalf("bootstrap_on=cccp: %v", err)
}
if len(c.spec.MemcachedHosts) != 3 {
t.Fatalf("Expected 3 hosts in memcached: %v", c.spec.MemcachedHosts)
}
if len(c.spec.HttpHosts) != 0 {
t.Fatalf("Expected 0 hosts in http: %v", c.spec.HttpHosts)
}

// Should fail if there are no hosts
c, err = Connect("couchbase://foo.com:12000?bootstrap_on=http")
if err == nil {
t.Fatal("Expected failure with explicit http without http hosts")
}
c, err = Connect("http://foo.com:9000?bootstrap_on=cccp")
if err == nil {
t.Fatal("Expected failure with explicit cccp without cccp hosts")
}

}
21 changes: 21 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ func Connect(connSpecStr string) (*Cluster, error) {
}

csResolveDnsSrv(&spec)

// Get bootstrap_on option to determine which, if any, of the bootstrap nodes should be cleared
switch spec.Options.Get("bootstrap_on") {
case "http":
spec.MemcachedHosts = nil
if len(spec.HttpHosts) == 0 {
return nil, errors.New("bootstrap_on=http but no HTTP hosts in connection string")
}
case "cccp":
spec.HttpHosts = nil
if len(spec.MemcachedHosts) == 0 {
return nil, errors.New("bootstrap_on=cccp but no CCCP/Memcached hosts in connection string")
}
case "both":
case "":
// Do nothing
break
default:
return nil, errors.New("bootstrap_on={http,cccp,both}")
}

cluster := &Cluster{
spec: spec,
connectTimeout: 60000 * time.Millisecond,
Expand Down

0 comments on commit 2694c60

Please sign in to comment.