Skip to content

Instantly share code, notes, and snippets.

@danmux
Created June 3, 2015 10:39
Show Gist options
  • Save danmux/b9711353980dc03357da to your computer and use it in GitHub Desktop.
Save danmux/b9711353980dc03357da to your computer and use it in GitHub Desktop.
Couchbase work out a set of keys that are guaranteed to write to all vBuckets
// from github.com/go-couchbase
// Copyright (c) 2013 Couchbase, Inc.
//
package couchbase
import (
"fmt"
"testing"
"unsafe"
)
func testBucket() Bucket {
b := Bucket{vBucketServerMap: unsafe.Pointer(&VBucketServerMap{
VBucketMap: make([][]int, 2048),
})}
return b
}
/*
key: k0 master: 10.1.7.1:11210 vBucketId: 9 couchApiBase: http://10.1.7.1:8092/default replicas: 10.1.7.2:11210
key: k1 master: 10.1.7.1:11210 vBucketId: 14 couchApiBase: http://10.1.7.1:8092/default replicas: 10.1.7.3:11210
key: k2 master: 10.1.7.1:11210 vBucketId: 7 couchApiBase: http://10.1.7.1:8092/default replicas: 10.1.7.2:11210
key: k3 master: 10.1.7.1:11210 vBucketId: 0 couchApiBase: http://10.1.7.1:8092/default replicas: 10.1.7.2:11210
key: k4 master: 10.1.7.2:11210 vBucketId: 100 couchApiBase: http://10.1.7.2:8092/default replicas: 10.1.7.5:11210
key: k5 master: 10.1.7.2:11210 vBucketId: 99 couchApiBase: http://10.1.7.2:8092/default replicas: 10.1.7.5:11210
*/
func TestVBHash(t *testing.T) {
b := testBucket()
// m := map[string]uint32{
// "k0": 9,
// "k1": 14,
// "k2": 7,
// "k3": 0,
// "k4": 100,
// "k5": 99,
// }
// for k, v := range m {
// assert(t, k, b.VBHash(k), v)
// }
hit := map[uint32]string{}
for n := 0; n < 100000000; n++ {
key := fmt.Sprintf("vbfill-%d", n)
bid := b.VBHash(key)
_, ok := hit[bid]
if !ok {
hit[bid] = key
// fmt.Println("another", key)
if len(hit) == 2048 {
break
}
// fmt.Println("len", len(hit))
}
}
if len(hit) != 2048 {
t.Error("not enough")
}
for _, v := range hit {
fmt.Printf("\"%v\",\n", v)
}
}
@danmux
Copy link
Author

danmux commented Jun 3, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment