forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 10
/
tablet.go
95 lines (79 loc) · 2.63 KB
/
tablet.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
// 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 (
"path"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// tabletPathForAlias converts a tablet alias to the node path.
func tabletPathForAlias(alias *topodatapb.TabletAlias) string {
return path.Join(tabletsPath, topoproto.TabletAliasString(alias), topo.TabletFile)
}
// CreateTablet implements topo.Server.
func (s *Server) CreateTablet(ctx context.Context, tablet *topodatapb.Tablet) error {
data, err := proto.Marshal(tablet)
if err != nil {
return err
}
nodePath := tabletPathForAlias(tablet.Alias)
_, err = s.Create(ctx, tablet.Alias.Cell, nodePath, data)
return err
}
// UpdateTablet implements topo.Server.
func (s *Server) UpdateTablet(ctx context.Context, tablet *topodatapb.Tablet, existingVersion int64) (int64, error) {
data, err := proto.Marshal(tablet)
if err != nil {
return 0, err
}
nodePath := tabletPathForAlias(tablet.Alias)
version, err := s.Update(ctx, tablet.Alias.Cell, nodePath, data, VersionFromInt(existingVersion))
if err != nil {
return 0, err
}
return int64(version.(EtcdVersion)), nil
}
// DeleteTablet implements topo.Server.
func (s *Server) DeleteTablet(ctx context.Context, alias *topodatapb.TabletAlias) error {
nodePath := tabletPathForAlias(alias)
return s.Delete(ctx, alias.Cell, nodePath, nil)
}
// GetTablet implements topo.Server.
func (s *Server) GetTablet(ctx context.Context, alias *topodatapb.TabletAlias) (*topodatapb.Tablet, int64, error) {
nodePath := tabletPathForAlias(alias)
data, version, err := s.Get(ctx, alias.Cell, nodePath)
if err != nil {
return nil, 0, err
}
tablet := &topodatapb.Tablet{}
if err := proto.Unmarshal(data, tablet); err != nil {
return nil, 0, err
}
return tablet, int64(version.(EtcdVersion)), nil
}
// GetTabletsByCell implements topo.Server.
func (s *Server) GetTabletsByCell(ctx context.Context, cell string) ([]*topodatapb.TabletAlias, error) {
// Check if the cell exists first.
if _, err := s.clientForCell(ctx, cell); err != nil {
return nil, err
}
children, err := s.ListDir(ctx, cell, tabletsPath)
if err != nil {
if err == topo.ErrNoNode {
return nil, nil
}
return nil, err
}
result := make([]*topodatapb.TabletAlias, len(children))
for i, child := range children {
result[i], err = topoproto.ParseTabletAlias(child)
if err != nil {
return nil, err
}
}
return result, nil
}