forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyspace.go
115 lines (101 loc) · 2.92 KB
/
keyspace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2013, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zktopo
import (
"encoding/json"
"fmt"
"path"
"sort"
"github.com/youtube/vitess/go/jscfg"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/zk"
"launchpad.net/gozk/zookeeper"
)
/*
This file contains the Keyspace management code for zktopo.Server
*/
const (
globalKeyspacesPath = "/zk/global/vt/keyspaces"
)
func (zkts *Server) CreateKeyspace(keyspace string, value *topo.Keyspace) error {
keyspacePath := path.Join(globalKeyspacesPath, keyspace)
pathList := []string{
keyspacePath,
path.Join(keyspacePath, "action"),
path.Join(keyspacePath, "actionlog"),
path.Join(keyspacePath, "shards"),
}
alreadyExists := false
for i, zkPath := range pathList {
c := ""
if i == 0 {
c = jscfg.ToJson(value)
}
_, err := zk.CreateRecursive(zkts.zconn, zkPath, c, 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
alreadyExists = true
} else {
return fmt.Errorf("error creating keyspace: %v %v", zkPath, err)
}
}
}
if alreadyExists {
return topo.ErrNodeExists
}
return nil
}
func (zkts *Server) UpdateKeyspace(ki *topo.KeyspaceInfo) error {
keyspacePath := path.Join(globalKeyspacesPath, ki.KeyspaceName())
_, err := zkts.zconn.Set(keyspacePath, jscfg.ToJson(ki.Keyspace), -1)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
// The code should be:
// err = topo.ErrNoNode
// Temporary code until we have Keyspace object
// everywhere:
_, err = zkts.zconn.Create(keyspacePath, jscfg.ToJson(ki.Keyspace), 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
// the directory doesn't even exist
err = topo.ErrNoNode
}
}
}
}
return err
}
func (zkts *Server) GetKeyspace(keyspace string) (*topo.KeyspaceInfo, error) {
keyspacePath := path.Join(globalKeyspacesPath, keyspace)
data, _, err := zkts.zconn.Get(keyspacePath)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
return nil, err
}
k := &topo.Keyspace{}
if err = json.Unmarshal([]byte(data), k); err != nil {
return nil, fmt.Errorf("bad keyspace data %v", err)
}
return topo.NewKeyspaceInfo(keyspace, k), nil
}
func (zkts *Server) GetKeyspaces() ([]string, error) {
children, _, err := zkts.zconn.Children(globalKeyspacesPath)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
return nil, nil
}
return nil, err
}
sort.Strings(children)
return children, nil
}
func (zkts *Server) DeleteKeyspaceShards(keyspace string) error {
shardsPath := path.Join(globalKeyspacesPath, keyspace, "shards")
if err := zk.DeleteRecursive(zkts.zconn, shardsPath, -1); err != nil && !zookeeper.IsError(err, zookeeper.ZNONODE) {
return err
}
return nil
}