forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard.go
123 lines (108 loc) · 3.06 KB
/
shard.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
// 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 shard management code for zktopo.Server
*/
func (zkts *Server) CreateShard(keyspace, shard string, value *topo.Shard) error {
shardPath := path.Join(globalKeyspacesPath, keyspace, "shards", shard)
pathList := []string{
shardPath,
path.Join(shardPath, "action"),
path.Join(shardPath, "actionlog"),
}
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 shard: %v %v", zkPath, err)
}
}
}
if alreadyExists {
return topo.ErrNodeExists
}
return nil
}
func (zkts *Server) UpdateShard(si *topo.ShardInfo) error {
shardPath := path.Join(globalKeyspacesPath, si.Keyspace(), "shards", si.ShardName())
_, err := zkts.zconn.Set(shardPath, jscfg.ToJson(si.Shard), -1)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
}
return err
}
func (zkts *Server) ValidateShard(keyspace, shard string) error {
shardPath := path.Join(globalKeyspacesPath, keyspace, "shards", shard)
zkPaths := []string{
path.Join(shardPath, "action"),
path.Join(shardPath, "actionlog"),
}
for _, zkPath := range zkPaths {
_, _, err := zkts.zconn.Get(zkPath)
if err != nil {
return err
}
}
return nil
}
func (zkts *Server) GetShard(keyspace, shard string) (*topo.ShardInfo, error) {
shardPath := path.Join(globalKeyspacesPath, keyspace, "shards", shard)
data, _, err := zkts.zconn.Get(shardPath)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
return nil, err
}
s := &topo.Shard{}
if err = json.Unmarshal([]byte(data), s); err != nil {
return nil, fmt.Errorf("bad shard data %v", err)
}
return topo.NewShardInfo(keyspace, shard, s), nil
}
func (zkts *Server) GetShardCritical(keyspace, shard string) (*topo.ShardInfo, error) {
return zkts.GetShard(keyspace, shard)
}
func (zkts *Server) GetShardNames(keyspace string) ([]string, error) {
shardsPath := path.Join(globalKeyspacesPath, keyspace, "shards")
children, _, err := zkts.zconn.Children(shardsPath)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
return nil, err
}
sort.Strings(children)
return children, nil
}
func (zkts *Server) DeleteShard(keyspace, shard string) error {
shardPath := path.Join(globalKeyspacesPath, keyspace, "shards", shard)
err := zk.DeleteRecursive(zkts.zconn, shardPath, -1)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
}
return err
}