forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
replication_graph.go
144 lines (122 loc) · 4.04 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// 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 etcdtopo
import (
"encoding/json"
"fmt"
"github.com/youtube/vitess/go/vt/topo"
"golang.org/x/net/context"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// UpdateShardReplicationFields implements topo.Server.
func (s *Server) UpdateShardReplicationFields(ctx context.Context, cell, keyspace, shard string, updateFunc func(*topodatapb.ShardReplication) error) error {
var sri *topo.ShardReplicationInfo
var version int64
var err error
for {
if sri, version, err = s.getShardReplication(cell, keyspace, shard); err != nil {
if err == topo.ErrNoNode {
// Pass an empty struct to the update func, as specified in topo.Server.
sri = topo.NewShardReplicationInfo(&topodatapb.ShardReplication{}, cell, keyspace, shard)
version = -1
} else {
return err
}
}
if err = updateFunc(sri.ShardReplication); err != nil {
if err == topo.ErrNoUpdateNeeded {
return nil
}
return err
}
if version == -1 {
if _, err = s.createShardReplication(sri); err != topo.ErrNodeExists {
return err
}
} else {
if _, err = s.updateShardReplication(sri, version); err != topo.ErrBadVersion {
return err
}
}
}
}
func (s *Server) updateShardReplication(sri *topo.ShardReplicationInfo, existingVersion int64) (int64, error) {
cell, err := s.getCell(sri.Cell())
if err != nil {
return -1, err
}
data, err := json.MarshalIndent(sri.ShardReplication, "", " ")
if err != nil {
return -1, err
}
resp, err := cell.CompareAndSwap(shardReplicationFilePath(sri.Keyspace(), sri.Shard()),
string(data), 0 /* ttl */, "" /* prevValue */, uint64(existingVersion))
if err != nil {
return -1, convertError(err)
}
if resp.Node == nil {
return -1, ErrBadResponse
}
return int64(resp.Node.ModifiedIndex), nil
}
func (s *Server) createShardReplication(sri *topo.ShardReplicationInfo) (int64, error) {
cell, err := s.getCell(sri.Cell())
if err != nil {
return -1, err
}
data, err := json.MarshalIndent(sri.ShardReplication, "", " ")
if err != nil {
return -1, err
}
resp, err := cell.Create(shardReplicationFilePath(sri.Keyspace(), sri.Shard()),
string(data), 0 /* ttl */)
if err != nil {
return -1, convertError(err)
}
if resp.Node == nil {
return -1, ErrBadResponse
}
return int64(resp.Node.ModifiedIndex), nil
}
// GetShardReplication implements topo.Server.
func (s *Server) GetShardReplication(ctx context.Context, cell, keyspace, shard string) (*topo.ShardReplicationInfo, error) {
sri, _, err := s.getShardReplication(cell, keyspace, shard)
return sri, err
}
func (s *Server) getShardReplication(cellName, keyspace, shard string) (*topo.ShardReplicationInfo, int64, error) {
cell, err := s.getCell(cellName)
if err != nil {
return nil, -1, err
}
resp, err := cell.Get(shardReplicationFilePath(keyspace, shard), false /* sort */, false /* recursive */)
if err != nil {
return nil, -1, convertError(err)
}
if resp.Node == nil {
return nil, -1, ErrBadResponse
}
value := &topodatapb.ShardReplication{}
if err := json.Unmarshal([]byte(resp.Node.Value), value); err != nil {
return nil, -1, fmt.Errorf("bad shard replication data (%v): %q", err, resp.Node.Value)
}
return topo.NewShardReplicationInfo(value, cellName, keyspace, shard), int64(resp.Node.ModifiedIndex), nil
}
// DeleteShardReplication implements topo.Server.
func (s *Server) DeleteShardReplication(ctx context.Context, cellName, keyspace, shard string) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
_, err = cell.Delete(shardReplicationDirPath(keyspace, shard), true /* recursive */)
return convertError(err)
}
// DeleteKeyspaceReplication implements topo.Server.
func (s *Server) DeleteKeyspaceReplication(ctx context.Context, cellName, keyspace string) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
_, err = cell.Delete(keyspaceReplicationDirPath(keyspace), true /* recursive */)
return convertError(err)
}