本文整理汇总了Golang中github.com/portworx/kvdb.Instance函数的典型用法代码示例。如果您正苦于以下问题:Golang Instance函数的具体用法?Golang Instance怎么用?Golang Instance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Instance函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Init
func Init(params volume.DriverParams) (volume.VolumeDriver, error) {
inst := &driver{
IoNotSupported: &volume.IoNotSupported{},
DefaultEnumerator: volume.NewDefaultEnumerator(Name, kvdb.Instance()),
}
inst.buseDevices = make(map[string]*buseDev)
if err := os.MkdirAll(BuseMountPath, 0744); err != nil {
return nil, err
}
volumeInfo, err := inst.DefaultEnumerator.Enumerate(
&api.VolumeLocator{},
nil,
)
if err == nil {
for _, info := range volumeInfo {
if info.Status == api.VolumeStatus_VOLUME_STATUS_NONE {
info.Status = api.VolumeStatus_VOLUME_STATUS_UP
inst.UpdateVol(info)
}
}
} else {
dlog.Println("Could not enumerate Volumes, ", err)
}
c, err := cluster.Inst()
if err != nil {
dlog.Println("BUSE initializing in single node mode")
} else {
dlog.Println("BUSE initializing in clustered mode")
c.AddEventListener(inst)
}
dlog.Println("BUSE initialized and driver mounted at: ", BuseMountPath)
return inst, nil
}
示例2: watchTree
func watchTree(t *testing.T) {
fmt.Println("watchTree")
kv := kvdb.Instance()
assert.NotNil(t, kv, "Default KVDB is not set")
tree := "tree"
watchData := watchData{
t: t,
key: tree + "/key",
stop: "stop",
iterations: 2,
}
kv.Delete(watchData.key)
time.Sleep(time.Second)
err := kv.WatchTree(tree, 0, &watchData, watchFn)
if err != nil {
fmt.Printf("Cannot test watchKey: %v\n", err)
return
}
go watchUpdate(&watchData)
for watchData.watchStopped == false {
time.Sleep(time.Millisecond * 100)
}
}
示例3: deleteTree
func deleteTree(t *testing.T) {
fmt.Println("deleteTree")
kv := kvdb.Instance()
assert.NotNil(t, kv, "Default KVDB is not set")
prefix := "tree"
keys := map[string]string{
prefix + "/1cbc9a98-072a-4793-8608-01ab43db96c8": "bar",
prefix + "/foo": "baz",
}
for key, val := range keys {
_, err := kv.Put(key, []byte(val), 10)
assert.NoError(t, err, "Unexpected error on Put")
}
for key, _ := range keys {
_, err := kv.Get(key)
assert.NoError(t, err, "Unexpected error on Get")
}
err := kv.DeleteTree(prefix)
assert.NoError(t, err, "Unexpected error on DeleteTree")
for key, _ := range keys {
_, err := kv.Get(key)
assert.Error(t, err, "Get should fail on all keys after DeleteTree")
}
}
示例4: TestRaiseWithGenerateIdAndErase
func TestRaiseWithGenerateIdAndErase(t *testing.T) {
// RaiseWithGenerateId api.Alerts Id : 1
raiseAlerts, err := kva.RaiseWithGenerateId(api.Alerts{Resource: api.ResourceType_VOLUMES, Severity: api.SeverityType_NOTIFY}, mockGenerateId)
require.NoError(t, err, "Failed in raising an alert")
kv := kvdb.Instance()
var alert api.Alerts
_, err = kv.GetVal(getResourceKey(api.ResourceType_VOLUMES)+strconv.FormatInt(raiseAlerts.Id, 10), &alert)
require.NoError(t, err, "Failed to retrieve alert from kvdb")
require.NotNil(t, alert, "api.Alerts object null in kvdb")
require.Equal(t, raiseAlerts.Id, alert.Id, "api.Alerts Id mismatch")
require.Equal(t, api.ResourceType_VOLUMES, alert.Resource, "api.Alerts Resource mismatch")
require.Equal(t, api.SeverityType_NOTIFY, alert.Severity, "api.Alerts Severity mismatch")
// RaiseWithGenerateId api.Alerts with no Resource
_, err = kva.RaiseWithGenerateId(api.Alerts{Severity: api.SeverityType_NOTIFY}, mockGenerateId)
require.Error(t, err, "An error was expected")
require.Equal(t, ErrResourceNotFound, err, "Error mismatch")
// Erase api.Alerts Id : 1
err = kva.Erase(api.ResourceType_VOLUMES, raiseAlerts.Id)
require.NoError(t, err, "Failed to erase an alert")
_, err = kv.GetVal(getResourceKey(api.ResourceType_VOLUMES)+"1", &alert)
require.Error(t, err, "api.Alerts not erased from kvdb")
}
示例5: update
func update(t *testing.T) {
fmt.Println("update")
kv := kvdb.Instance()
assert.NotNil(t, kv, "Default KVDB is not set")
key := "update/foo"
kv.Delete(key)
kvp, err := kv.Update(key, []byte("bar"), 0)
assert.Error(t, err, "Update should error on non-existent key")
defer func() {
kv.Delete(key)
}()
kvp, err = kv.Create(key, []byte("bar"), 0)
assert.NoError(t, err, "Unexpected error on create")
kvp, err = kv.Update(key, []byte("bar"), 0)
assert.NoError(t, err, "Unexpected error on update")
assert.Equal(t, kvp.Action, kvdb.KVSet,
"Expected action KVSet, actual %v", kvp.Action)
}
示例6: cas
func cas(t *testing.T) {
fmt.Println("cas")
kv := kvdb.Instance()
assert.NotNil(t, kv, "Default KVDB is not set")
key := "foo/docker"
val := "great"
defer func() {
kv.Delete(key)
}()
kvPair, err := kv.Put(key, []byte(val), 10)
assert.NoError(t, err, "Unxpected error in Put")
kvPair, err = kv.Get(key)
assert.NoError(t, err, "Failed in Get")
_, err = kv.CompareAndSet(kvPair, kvdb.KVFlags(0), []byte("badval"))
assert.Error(t, err, "CompareAndSet should fail on an incorrect previous value")
kvPair.ModifiedIndex++
_, err = kv.CompareAndSet(kvPair, kvdb.KVModifiedIndex, nil)
assert.Error(t, err, "CompareAndSet should fail on an incorrect modified index")
kvPair.ModifiedIndex--
kvPair, err = kv.CompareAndSet(kvPair, kvdb.KVModifiedIndex, nil)
assert.NoError(t, err, "CompareAndSet should succeed on an correct modified index")
kvPair, err = kv.CompareAndSet(kvPair, kvdb.KVFlags(0), []byte(val))
assert.NoError(t, err, "CompareAndSet should succeed on an correct value")
kvPair, err = kv.CompareAndSet(kvPair, kvdb.KVModifiedIndex, []byte(val))
assert.NoError(t, err, "CompareAndSet should succeed on an correct value and modified index")
}
示例7: lock
func lock(t *testing.T) {
fmt.Println("lock")
kv := kvdb.Instance()
assert.NotNil(t, kv, "Default KVDB is not set")
key := "locktest"
kvPair, err := kv.Lock(key, 100)
assert.NoError(t, err, "Unexpected error in lock")
stash := *kvPair
stash.Value = []byte("hoohah")
err = kv.Unlock(&stash)
assert.Error(t, err, "Unlock should fail for bad KVPair")
err = kv.Unlock(kvPair)
assert.NoError(t, err, "Unexpected error from Unlock")
kvPair, err = kv.Lock(key, 20)
assert.NoError(t, err, "Failed to lock after unlock")
err = kv.Unlock(kvPair)
assert.NoError(t, err, "Unexpected error from Unlock")
}
示例8: readDatabase
func readDatabase() (Database, error) {
kvdb := kv.Instance()
db := Database{Status: api.StatusInit,
NodeEntries: make(map[string]NodeEntry)}
kv, err := kvdb.Get("cluster/database")
if err != nil && !strings.Contains(err.Error(), "Key not found") {
log.Warn("Warning, could not read cluster database")
goto done
}
if kv == nil || bytes.Compare(kv.Value, []byte("{}")) == 0 {
log.Info("Cluster is uninitialized...")
err = nil
goto done
} else {
err = json.Unmarshal(kv.Value, &db)
if err != nil {
log.Warn("Fatal, Could not parse cluster database ", kv)
goto done
}
}
done:
return db, err
}
示例9: Watch
// Watch on all alerts
func (kva *KvAlerts) Watch(alertsWatcherFunc AlertsWatcherFunc) error {
prefix := alertsKey
alertsWatcher = watcher{status: watchBootstrap, cb: alertsWatcherFunc, kvcb: kvdbWatch}
if err := subscribeWatch(); err != nil {
return err
}
// Subscribe for a watch is in a goroutine. Bootsrap by writing to the key and waiting for an update
retries := 0
kv := kvdb.Instance()
for alertsWatcher.status == watchBootstrap {
_, err := kv.Put(prefix+bootstrap, time.Now(), 1)
if err != nil {
return err
}
if alertsWatcher.status == watchBootstrap {
retries++
time.Sleep(time.Millisecond * 100)
}
}
if alertsWatcher.status != watchReady {
return fmt.Errorf("Failed to watch on %v", prefix)
}
return nil
}
示例10: getInterface
func getInterface(t *testing.T) {
fmt.Println("getInterface")
expected := struct {
N int
S string
}{
N: 10,
S: "Ten",
}
actual := expected
actual.N = 0
actual.S = "zero"
kv := kvdb.Instance()
assert.NotNil(t, kv, "Default KVDB is not set")
key := "DEADBEEF"
_, err := kv.Delete(key)
_, err = kv.Put(key, &expected, 0)
assert.NoError(t, err, "Failed in Put")
_, err = kv.GetVal(key, &actual)
assert.NoError(t, err, "Failed in Get")
assert.Equal(t, expected, actual, "Expected %#v but got %#v",
expected, actual)
}
示例11: readDatabase
func readDatabase() (Database, error) {
kvdb := kvdb.Instance()
db := Database{
Status: api.Status_STATUS_INIT,
NodeEntries: make(map[string]NodeEntry),
}
kv, err := kvdb.Get(ClusterDBKey)
if err != nil && !strings.Contains(err.Error(), "Key not found") {
dlog.Warnln("Warning, could not read cluster database")
return db, err
}
if kv == nil || bytes.Compare(kv.Value, []byte("{}")) == 0 {
dlog.Infoln("Cluster is uninitialized...")
return db, nil
}
if err := json.Unmarshal(kv.Value, &db); err != nil {
dlog.Warnln("Fatal, Could not parse cluster database ", kv)
return db, err
}
return db, nil
}
示例12: Erase
// Erase erases an alert
func (kva *KvAlerts) Erase(resourceType api.ResourceType, alertId int64) error {
kv := kvdb.Instance()
if resourceType == api.ResourceType_UNKNOWN_RESOURCE {
return ErrResourceNotFound
}
var err error
_, err = kv.Delete(getResourceKey(resourceType) + strconv.FormatInt(alertId, 10))
return err
}
示例13: subscribeWatch
func subscribeWatch() error {
watchIndex := alertsWatchIndex
if watchIndex != 0 {
watchIndex = alertsWatchIndex + 1
}
kv := kvdb.Instance()
if err := kv.WatchTree(alertsKey, watchIndex, nil, alertsWatcher.kvcb); err != nil {
return err
}
return nil
}
示例14: Init
func Init(params volume.DriverParams) (volume.VolumeDriver, error) {
restUrl, ok := params["restUrl"]
if !ok {
return nil, fmt.Errorf("rest api 'url' configuration parameter must be set")
}
user, ok := params["user"]
if !ok {
return nil, fmt.Errorf("rest auth 'user' must be set")
}
pass, ok := params["password"]
if !ok {
return nil, fmt.Errorf("rest auth 'password' must be set")
}
consistency_group, ok := params["consistency_group"]
if !ok {
return nil, fmt.Errorf("'consistency_group' configuration parameter must be set")
}
project, ok := params["project"]
if !ok {
return nil, fmt.Errorf("'project' configuration parameter must be set")
}
varray, ok := params["varray"]
if !ok {
return nil, fmt.Errorf("'varray' configuration parameter must be set")
}
vpool, ok := params["vpool"]
if !ok {
return nil, fmt.Errorf("'vpool' configuration parameter must be set")
}
d := &driver{
DefaultEnumerator: volume.NewDefaultEnumerator(Name, kvdb.Instance()),
consistency_group: consistency_group,
project: project,
varray: varray,
vpool: vpool,
url: restUrl,
creds: url.UserPassword(user, pass),
httpClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
}
return d, nil
}
示例15: Init
func Init(params volume.DriverParams) (volume.VolumeDriver, error) {
root, ok := params[RootParam]
if !ok {
return nil, fmt.Errorf("Root directory should be specified with key %q", RootParam)
}
home := path.Join(root, Volumes)
d, err := btrfs.Init(home, nil)
if err != nil {
return nil, err
}
s := volume.NewDefaultEnumerator(Name, kvdb.Instance())
return &driver{btrfs: d, root: root, DefaultEnumerator: s}, nil
}