forked from vitessio/vitess
/
serving_graph.go
90 lines (78 loc) · 2.79 KB
/
serving_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
// Copyright 2013, 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 zk2topo
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"
vschemapb "github.com/youtube/vitess/go/vt/proto/vschema"
)
// This file contains the serving graph management code of zktopo.Server.
// Eventually, this code will move to go/vt/topo.
// GetSrvKeyspaceNames is part of the topo.Server interface.
func (zs *Server) GetSrvKeyspaceNames(ctx context.Context, cell string) ([]string, error) {
children, err := zs.ListDir(ctx, cell, keyspacesPath)
switch err {
case nil:
return children, nil
case topo.ErrNoNode:
return nil, nil
default:
return nil, err
}
}
// UpdateSrvKeyspace is part of the topo.Server interface.
func (zs *Server) UpdateSrvKeyspace(ctx context.Context, cell, keyspace string, srvKeyspace *topodatapb.SrvKeyspace) error {
zkPath := path.Join(keyspacesPath, keyspace, topo.SrvKeyspaceFile)
data, err := proto.Marshal(srvKeyspace)
if err != nil {
return err
}
_, err = zs.Update(ctx, cell, zkPath, data, nil)
return err
}
// DeleteSrvKeyspace is part of the topo.Server interface.
func (zs *Server) DeleteSrvKeyspace(ctx context.Context, cell, keyspace string) error {
zkPath := path.Join(keyspacesPath, keyspace, topo.SrvKeyspaceFile)
return zs.Delete(ctx, cell, zkPath, nil)
}
// GetSrvKeyspace is part of the topo.Server interface.
func (zs *Server) GetSrvKeyspace(ctx context.Context, cell, keyspace string) (*topodatapb.SrvKeyspace, error) {
zkPath := path.Join(keyspacesPath, keyspace, topo.SrvKeyspaceFile)
data, _, err := zs.Get(ctx, cell, zkPath)
if err != nil {
return nil, err
}
srvKeyspace := &topodatapb.SrvKeyspace{}
if err := proto.Unmarshal(data, srvKeyspace); err != nil {
return nil, fmt.Errorf("SrvKeyspace unmarshal failed: %v %v", data, err)
}
return srvKeyspace, nil
}
// UpdateSrvVSchema is part of the topo.Server interface
func (zs *Server) UpdateSrvVSchema(ctx context.Context, cell string, srvVSchema *vschemapb.SrvVSchema) error {
zkPath := topo.SrvVSchemaFile
data, err := proto.Marshal(srvVSchema)
if err != nil {
return err
}
_, err = zs.Update(ctx, cell, zkPath, data, nil)
return err
}
// GetSrvVSchema is part of the topo.Server interface
func (zs *Server) GetSrvVSchema(ctx context.Context, cell string) (*vschemapb.SrvVSchema, error) {
zkPath := topo.SrvVSchemaFile
data, _, err := zs.Get(ctx, cell, zkPath)
if err != nil {
return nil, err
}
srvVSchema := &vschemapb.SrvVSchema{}
if err := proto.Unmarshal(data, srvVSchema); err != nil {
return nil, fmt.Errorf("SrvVSchema unmarshal failed: %v %v", data, err)
}
return srvVSchema, nil
}