forked from vitessio/vitess
/
keyspace.go
122 lines (106 loc) · 3.31 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
// 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"
"sync"
"github.com/coreos/go-etcd/etcd"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/topo"
"golang.org/x/net/context"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// CreateKeyspace implements topo.Server.
func (s *Server) CreateKeyspace(ctx context.Context, keyspace string, value *topodatapb.Keyspace) error {
data, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
}
global := s.getGlobal()
if _, err = global.Create(keyspaceFilePath(keyspace), string(data), 0 /* ttl */); err != nil {
return convertError(err)
}
return nil
}
// UpdateKeyspace implements topo.Server.
func (s *Server) UpdateKeyspace(ctx context.Context, keyspace string, value *topodatapb.Keyspace, existingVersion int64) (int64, error) {
data, err := json.MarshalIndent(value, "", " ")
if err != nil {
return -1, err
}
var resp *etcd.Response
if existingVersion == -1 {
// Set unconditionally.
resp, err = s.getGlobal().Set(keyspaceFilePath(keyspace), string(data), 0 /* ttl */)
} else {
resp, err = s.getGlobal().CompareAndSwap(keyspaceFilePath(keyspace),
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
}
// GetKeyspace implements topo.Server.
func (s *Server) GetKeyspace(ctx context.Context, keyspace string) (*topodatapb.Keyspace, int64, error) {
resp, err := s.getGlobal().Get(keyspaceFilePath(keyspace), false /* sort */, false /* recursive */)
if err != nil {
return nil, 0, convertError(err)
}
if resp.Node == nil {
return nil, 0, ErrBadResponse
}
value := &topodatapb.Keyspace{}
if err := json.Unmarshal([]byte(resp.Node.Value), value); err != nil {
return nil, 0, fmt.Errorf("bad keyspace data (%v): %q", err, resp.Node.Value)
}
return value, int64(resp.Node.ModifiedIndex), nil
}
// GetKeyspaces implements topo.Server.
func (s *Server) GetKeyspaces(ctx context.Context) ([]string, error) {
resp, err := s.getGlobal().Get(keyspacesDirPath, true /* sort */, false /* recursive */)
if err != nil {
err = convertError(err)
if err == topo.ErrNoNode {
return nil, nil
}
return nil, err
}
return getNodeNames(resp)
}
// DeleteKeyspaceShards implements topo.Server.
func (s *Server) DeleteKeyspaceShards(ctx context.Context, keyspace string) error {
shards, err := s.GetShardNames(ctx, keyspace)
if err != nil {
return err
}
wg := sync.WaitGroup{}
rec := concurrency.AllErrorRecorder{}
global := s.getGlobal()
for _, shard := range shards {
wg.Add(1)
go func(shard string) {
defer wg.Done()
_, err := global.Delete(shardDirPath(keyspace, shard), true /* recursive */)
rec.RecordError(convertError(err))
}(shard)
}
wg.Wait()
if err = rec.Error(); err != nil {
return err
}
return nil
}
// DeleteKeyspace implements topo.Server.
func (s *Server) DeleteKeyspace(ctx context.Context, keyspace string) error {
_, err := s.getGlobal().Delete(keyspaceDirPath(keyspace), true /* recursive */)
if err != nil {
return convertError(err)
}
return nil
}