forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablet.go
96 lines (81 loc) · 2.66 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
96
package memorytopo
import (
"path"
"sort"
"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.Impl.CreateTablet
func (mt *MemoryTopo) CreateTablet(ctx context.Context, tablet *topodatapb.Tablet) error {
data, err := proto.Marshal(tablet)
if err != nil {
return err
}
nodePath := tabletPathForAlias(tablet.Alias)
_, err = mt.Create(ctx, tablet.Alias.Cell, nodePath, data)
return err
}
// UpdateTablet implements topo.Impl.UpdateTablet
func (mt *MemoryTopo) 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 := mt.Update(ctx, tablet.Alias.Cell, nodePath, data, VersionFromInt(existingVersion))
if err != nil {
return 0, err
}
return int64(version.(NodeVersion)), nil
}
// DeleteTablet implements topo.Impl.DeleteTablet
func (mt *MemoryTopo) DeleteTablet(ctx context.Context, alias *topodatapb.TabletAlias) error {
nodePath := tabletPathForAlias(alias)
return mt.Delete(ctx, alias.Cell, nodePath, nil)
}
// GetTablet implements topo.Impl.GetTablet
func (mt *MemoryTopo) GetTablet(ctx context.Context, alias *topodatapb.TabletAlias) (*topodatapb.Tablet, int64, error) {
nodePath := tabletPathForAlias(alias)
data, version, err := mt.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.(NodeVersion)), nil
}
// GetTabletsByCell implements topo.Impl.GetTabletsByCell
func (mt *MemoryTopo) GetTabletsByCell(ctx context.Context, cell string) ([]*topodatapb.TabletAlias, error) {
// Check if the cell exists first. We need to return ErrNoNode if not.
mt.mu.Lock()
if _, ok := mt.cells[cell]; !ok {
mt.mu.Unlock()
return nil, topo.ErrNoNode
}
mt.mu.Unlock()
children, err := mt.ListDir(ctx, cell, tabletsPath)
if err != nil {
if err == topo.ErrNoNode {
return nil, nil
}
return nil, err
}
sort.Strings(children)
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
}