-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathshards.go
141 lines (109 loc) · 3.21 KB
/
shards.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
package provider
import (
"context"
"github.com/pg-sharding/spqr/pkg/shard"
"github.com/pg-sharding/spqr/pkg/txstatus"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/pg-sharding/spqr/coordinator"
"github.com/pg-sharding/spqr/pkg/models/datashards"
protos "github.com/pg-sharding/spqr/pkg/protos"
)
type ShardServer struct {
protos.UnimplementedShardServiceServer
impl coordinator.Coordinator
}
func NewShardServer(impl coordinator.Coordinator) *ShardServer {
return &ShardServer{
impl: impl,
}
}
var _ protos.ShardServiceServer = &ShardServer{}
// TODO : unit tests
func (s *ShardServer) AddDataShard(ctx context.Context, request *protos.AddShardRequest) (*emptypb.Empty, error) {
newShard := request.GetShard()
if err := s.impl.AddDataShard(ctx, datashards.DataShardFromProto(newShard)); err != nil {
return nil, err
}
return nil, nil
}
func (s *ShardServer) AddWorldShard(ctx context.Context, request *protos.AddWorldShardRequest) (*emptypb.Empty, error) {
panic("ShardServer.AddWorldShard not implemented")
}
// TODO : unit tests
// TODO: remove ShardRequest.
func (s *ShardServer) ListShards(ctx context.Context, _ *emptypb.Empty) (*protos.ListShardsReply, error) {
shardList, err := s.impl.ListShards(ctx)
if err != nil {
return nil, err
}
protoShards := make([]*protos.Shard, 0, len(shardList))
for _, sh := range shardList {
protoShards = append(protoShards, datashards.DataShardToProto(sh))
}
return &protos.ListShardsReply{
Shards: protoShards,
}, nil
}
// TODO : unit tests
func (s *ShardServer) GetShard(ctx context.Context, shardRequest *protos.ShardRequest) (*protos.ShardReply, error) {
sh, err := s.impl.GetShard(ctx, shardRequest.Id)
if err != nil {
return nil, err
}
return &protos.ShardReply{
Shard: datashards.DataShardToProto(sh),
}, nil
}
type CoordShardInfo struct {
underlying *protos.BackendConnectionsInfo
router string
}
// RequestData implements shard.Shardinfo.
func (c *CoordShardInfo) RequestData() {
panic("unimplemented")
}
// DataPending implements shard.Shardinfo.
func (c *CoordShardInfo) DataPending() bool {
panic("unimplemented")
}
// Pid implements shard.Shardinfo.
func (c *CoordShardInfo) Pid() uint32 {
return 0
}
// ListPreparedStatements implements shard.Shardinfo.
func (c *CoordShardInfo) ListPreparedStatements() []shard.PreparedStatementsMgrDescriptor {
return nil
}
func NewCoordShardInfo(conn *protos.BackendConnectionsInfo, router string) shard.Shardinfo {
return &CoordShardInfo{
underlying: conn,
router: router,
}
}
func (c *CoordShardInfo) DB() string {
return c.underlying.Dbname
}
func (c *CoordShardInfo) Router() string {
return c.router
}
func (c *CoordShardInfo) Usr() string {
return c.underlying.Dbname
}
func (c *CoordShardInfo) InstanceHostname() string {
return c.underlying.Hostname
}
func (c *CoordShardInfo) ID() uint {
return uint(c.underlying.BackendConnectionId)
}
func (c *CoordShardInfo) ShardKeyName() string {
return c.underlying.ShardKeyName
}
func (c *CoordShardInfo) Sync() int64 {
return c.underlying.Sync
}
func (c *CoordShardInfo) TxServed() int64 {
return c.underlying.TxServed
}
func (c *CoordShardInfo) TxStatus() txstatus.TXStatus {
return txstatus.TXStatus(c.underlying.TxStatus)
}