-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
replication_graph.go
112 lines (97 loc) · 3.25 KB
/
replication_graph.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
// 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"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/zk"
"golang.org/x/net/context"
"launchpad.net/gozk/zookeeper"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
/*
This file contains the replication graph management code for zktopo.Server
*/
func keyspaceReplicationPath(cell, keyspace string) string {
return path.Join("/zk", cell, "vt", "replication", keyspace)
}
func shardReplicationPath(cell, keyspace, shard string) string {
return path.Join("/zk", cell, "vt", "replication", keyspace, shard)
}
// UpdateShardReplicationFields is part of the topo.Server interface
func (zkts *Server) UpdateShardReplicationFields(ctx context.Context, cell, keyspace, shard string, update func(*pb.ShardReplication) error) error {
// create the parent directory to be sure it's here
zkDir := path.Join("/zk", cell, "vt", "replication", keyspace)
if _, err := zk.CreateRecursive(zkts.zconn, zkDir, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL)); err != nil && !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
return err
}
// now update the data
zkPath := shardReplicationPath(cell, keyspace, shard)
f := func(oldValue string, oldStat zk.Stat) (string, error) {
sr := &pb.ShardReplication{}
if oldValue != "" {
if err := json.Unmarshal([]byte(oldValue), sr); err != nil {
return "", err
}
}
if err := update(sr); err != nil {
return "", err
}
data, err := json.MarshalIndent(sr, "", " ")
if err != nil {
return "", err
}
return string(data), nil
}
err := zkts.zconn.RetryChange(zkPath, 0, zookeeper.WorldACL(zookeeper.PERM_ALL), f)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
return err
}
return nil
}
// GetShardReplication is part of the topo.Server interface
func (zkts *Server) GetShardReplication(ctx context.Context, cell, keyspace, shard string) (*topo.ShardReplicationInfo, error) {
zkPath := shardReplicationPath(cell, keyspace, shard)
data, _, err := zkts.zconn.Get(zkPath)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
return nil, err
}
sr := &pb.ShardReplication{}
if err = json.Unmarshal([]byte(data), sr); err != nil {
return nil, fmt.Errorf("bad ShardReplication data %v", err)
}
return topo.NewShardReplicationInfo(sr, cell, keyspace, shard), nil
}
// DeleteShardReplication is part of the topo.Server interface
func (zkts *Server) DeleteShardReplication(ctx context.Context, cell, keyspace, shard string) error {
zkPath := shardReplicationPath(cell, keyspace, shard)
err := zkts.zconn.Delete(zkPath, -1)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
return err
}
return nil
}
// DeleteKeyspaceReplication is part of the topo.Server interface
func (zkts *Server) DeleteKeyspaceReplication(ctx context.Context, cell, keyspace string) error {
zkPath := keyspaceReplicationPath(cell, keyspace)
err := zkts.zconn.Delete(zkPath, -1)
if err != nil {
if zookeeper.IsError(err, zookeeper.ZNONODE) {
err = topo.ErrNoNode
}
return err
}
return nil
}