forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 19
/
replication_graph.go
100 lines (85 loc) · 2.71 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
// Copyright 2014, 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 etcd2topo
import (
"fmt"
"path"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/topo"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// UpdateShardReplicationFields implements topo.Server.
func (s *Server) UpdateShardReplicationFields(ctx context.Context, cell, keyspace, shard string, update func(*topodatapb.ShardReplication) error) error {
p := path.Join(keyspacesPath, keyspace, shardsPath, shard, topo.ShardReplicationFile)
for {
data, version, err := s.Get(ctx, cell, p)
sr := &topodatapb.ShardReplication{}
switch err {
case topo.ErrNoNode:
// Empty node, version is nil
case nil:
// Use any data we got.
if err = proto.Unmarshal(data, sr); err != nil {
return fmt.Errorf("bad ShardReplication data %v", err)
}
default:
return err
}
err = update(sr)
switch err {
case topo.ErrNoUpdateNeeded:
return nil
case nil:
// keep going
default:
return err
}
// marshall and save
data, err = proto.Marshal(sr)
if err != nil {
return err
}
if version == nil {
// We have to create, and we catch ErrNodeExists.
_, err = s.Create(ctx, cell, p, data)
if err == topo.ErrNodeExists {
// Node was created by another process, try
// again.
continue
}
return err
}
// We have to update, and we catch ErrBadVersion.
_, err = s.Update(ctx, cell, p, data, version)
if err == topo.ErrBadVersion {
// Node was updated by another process, try again.
continue
}
return err
}
}
// GetShardReplication implements topo.Server.
func (s *Server) GetShardReplication(ctx context.Context, cell, keyspace, shard string) (*topo.ShardReplicationInfo, error) {
p := path.Join(keyspacesPath, keyspace, shardsPath, shard, topo.ShardReplicationFile)
data, _, err := s.Get(ctx, cell, p)
if err != nil {
return nil, err
}
sr := &topodatapb.ShardReplication{}
if err = proto.Unmarshal(data, sr); err != nil {
return nil, fmt.Errorf("bad ShardReplication data %v", err)
}
return topo.NewShardReplicationInfo(sr, cell, keyspace, shard), nil
}
// DeleteShardReplication implements topo.Server.
func (s *Server) DeleteShardReplication(ctx context.Context, cell, keyspace, shard string) error {
p := path.Join(keyspacesPath, keyspace, shardsPath, shard, topo.ShardReplicationFile)
return s.Delete(ctx, cell, p, nil)
}
// DeleteKeyspaceReplication implements topo.Server.
func (s *Server) DeleteKeyspaceReplication(ctx context.Context, cell, keyspace string) error {
p := path.Join(keyspacesPath, keyspace)
return s.Delete(ctx, cell, p, nil)
}