forked from vitessio/vitess
/
keyspace.go
130 lines (112 loc) · 3.52 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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/vt/topo"
"github.com/youtube/vitess/go/zk"
"golang.org/x/net/context"
"launchpad.net/gozk/zookeeper"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
/*
This file contains the Keyspace management code for zktopo.Server
*/
const (
// GlobalKeyspacesPath is the path used to store global
// information in ZK. Exported for tests.
GlobalKeyspacesPath = "/zk/global/vt/keyspaces"
)
// CreateKeyspace is part of the topo.Server interface
func (zkts *Server) CreateKeyspace(ctx context.Context, keyspace string, value *topodatapb.Keyspace) error {
keyspacePath := path.Join(GlobalKeyspacesPath, keyspace)
pathList := []string{
keyspacePath,
path.Join(keyspacePath, "action"),
path.Join(keyspacePath, "actionlog"),
path.Join(keyspacePath, "shards"),
}
data, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
}
alreadyExists := false
for i, zkPath := range pathList {
c := ""
if i == 0 {
c = string(data)
}
_, 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 convertError(err)
}
}
}
if alreadyExists {
return topo.ErrNodeExists
}
return nil
}
// UpdateKeyspace is part of the topo.Server interface
func (zkts *Server) UpdateKeyspace(ctx context.Context, keyspace string, value *topodatapb.Keyspace, existingVersion int64) (int64, error) {
keyspacePath := path.Join(GlobalKeyspacesPath, keyspace)
data, err := json.MarshalIndent(value, "", " ")
if err != nil {
return -1, err
}
stat, err := zkts.zconn.Set(keyspacePath, string(data), int(existingVersion))
if err != nil {
return -1, convertError(err)
}
return int64(stat.Version()), nil
}
// DeleteKeyspace is part of the topo.Server interface.
func (zkts *Server) DeleteKeyspace(ctx context.Context, keyspace string) error {
keyspacePath := path.Join(GlobalKeyspacesPath, keyspace)
err := zk.DeleteRecursive(zkts.zconn, keyspacePath, -1)
if err != nil {
return convertError(err)
}
return nil
}
// GetKeyspace is part of the topo.Server interface
func (zkts *Server) GetKeyspace(ctx context.Context, keyspace string) (*topodatapb.Keyspace, int64, error) {
keyspacePath := path.Join(GlobalKeyspacesPath, keyspace)
data, stat, err := zkts.zconn.Get(keyspacePath)
if err != nil {
return nil, 0, convertError(err)
}
k := &topodatapb.Keyspace{}
if err = json.Unmarshal([]byte(data), k); err != nil {
return nil, 0, fmt.Errorf("bad keyspace data %v", err)
}
return k, int64(stat.Version()), nil
}
// GetKeyspaces is part of the topo.Server interface
func (zkts *Server) GetKeyspaces(ctx context.Context) ([]string, error) {
children, _, err := zkts.zconn.Children(GlobalKeyspacesPath)
if err != nil {
err = convertError(err)
if err == topo.ErrNoNode {
return nil, nil
}
return nil, err
}
sort.Strings(children)
return children, nil
}
// DeleteKeyspaceShards is part of the topo.Server interface
func (zkts *Server) DeleteKeyspaceShards(ctx context.Context, 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 convertError(err)
}
return nil
}