-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutingclient.go
243 lines (197 loc) · 5.76 KB
/
routingclient.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package gocbcoreps
import (
"context"
"crypto/x509"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"net"
"sync"
grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
"go.uber.org/zap/zapgrpc"
"github.com/couchbase/goprotostellar/genproto/view_v1"
"github.com/couchbase/goprotostellar/genproto/admin_search_v1"
"github.com/couchbase/goprotostellar/genproto/admin_query_v1"
"github.com/couchbase/goprotostellar/genproto/admin_bucket_v1"
"github.com/couchbase/goprotostellar/genproto/admin_collection_v1"
"github.com/couchbase/goprotostellar/genproto/analytics_v1"
"github.com/couchbase/goprotostellar/genproto/kv_v1"
"github.com/couchbase/goprotostellar/genproto/query_v1"
"github.com/couchbase/goprotostellar/genproto/routing_v1"
"github.com/couchbase/goprotostellar/genproto/search_v1"
"go.uber.org/zap"
)
type routingClient_Bucket struct {
RefCount uint
Watcher *routingWatcher
}
type RoutingClient struct {
routing *atomicRoutingTable
lock sync.Mutex
buckets map[string]*routingClient_Bucket
logger *zap.Logger
}
// Verify that RoutingClient implements Conn
var _ Conn = (*RoutingClient)(nil)
type DialOptions struct {
ClientCertificate *x509.CertPool
Username string
Password string
Logger *zap.Logger
InsecureSkipVerify bool
PoolSize uint32
TracerProvider trace.TracerProvider
MeterProvider metric.MeterProvider
}
func Dial(target string, opts *DialOptions) (*RoutingClient, error) {
return DialContext(context.Background(), target, opts)
}
func DialContext(ctx context.Context, target string, opts *DialOptions) (*RoutingClient, error) {
// use port 18091 by default
{
_, _, err := net.SplitHostPort(target)
if err != nil {
// if we couldn't split the host/port, assume there is no port
target = target + ":18098"
}
}
logger := opts.Logger
if logger == nil {
logger = zap.NewNop()
}
// Setup grpc level logging, so that we can pipe connection level issues into our logs.
grpc_logsettable.ReplaceGrpcLoggerV2().Set(zapgrpc.NewLogger(logger))
var conns []*routingConn
var poolSize uint32 = 1
if opts.PoolSize > 0 {
poolSize = opts.PoolSize
}
for i := uint32(0); i < poolSize; i++ {
conn, err := dialRoutingConn(ctx, target, &routingConnOptions{
ClientCertificate: opts.ClientCertificate,
Username: opts.Username,
Password: opts.Password,
InsecureSkipVerify: opts.InsecureSkipVerify,
TracerProvider: opts.TracerProvider,
MeterProvider: opts.MeterProvider,
})
if err != nil {
return nil, err
}
conns = append(conns, conn)
}
routing := &atomicRoutingTable{}
routing.Store(&routingTable{
Conns: newRoutingConnPool(conns),
})
return &RoutingClient{
routing: routing,
buckets: make(map[string]*routingClient_Bucket),
logger: logger,
}, nil
}
func (c *RoutingClient) OpenBucket(bucketName string) {
c.lock.Lock()
bucket := c.buckets[bucketName]
if bucket != nil {
bucket.RefCount++
c.lock.Unlock()
return
}
watcher := newRoutingWatcher(&routingWatcherOptions{
RoutingClient: c.RoutingV1(),
BucketName: bucketName,
RoutingTable: c.routing,
})
c.buckets[bucketName] = &routingClient_Bucket{
RefCount: 1,
Watcher: watcher,
}
c.lock.Unlock()
}
func (c *RoutingClient) CloseBucket(bucketName string) {
c.lock.Lock()
bucket := c.buckets[bucketName]
if bucket == nil {
// doesn't exist, thats weird...
c.lock.Unlock()
c.logger.Info("closed an unopened bucket")
return
}
if bucket.RefCount == 0 {
// this shouldn't be possible...
c.lock.Unlock()
c.logger.Info("closed an unreferenced bucket")
return
}
bucket.RefCount--
if bucket.RefCount > 0 {
// there are still references, carry on
c.lock.Unlock()
return
}
bucket.Watcher.Close()
delete(c.buckets, bucketName)
c.lock.Unlock()
}
func (c *RoutingClient) ConnectionState() ConnState {
r := c.routing.Load()
return r.Conns.State()
}
func (c *RoutingClient) fetchConn() *routingConn {
// TODO(brett19): We should probably be more clever here...
r := c.routing.Load()
return r.Conns.Conn()
}
func (c *RoutingClient) fetchConnForBucket(bucketName string) *routingConn {
// TODO(brett19): Implement routing of bucket-specific requests
return c.fetchConn()
}
func (c *RoutingClient) fetchConnForKey(bucketName string, key string) *routingConn {
// TODO(brett19): Implement routing of key-specific requests.
return c.fetchConn()
}
func (c *RoutingClient) RoutingV1() routing_v1.RoutingServiceClient {
return &routingImpl_RoutingV1{c}
}
func (c *RoutingClient) KvV1() kv_v1.KvServiceClient {
return &routingImpl_KvV1{c}
}
func (c *RoutingClient) QueryV1() query_v1.QueryServiceClient {
return &routingImpl_QueryV1{c}
}
func (c *RoutingClient) CollectionV1() admin_collection_v1.CollectionAdminServiceClient {
return &routingImpl_CollectionV1{c}
}
func (c *RoutingClient) BucketV1() admin_bucket_v1.BucketAdminServiceClient {
return &routingImpl_BucketV1{c}
}
func (c *RoutingClient) AnalyticsV1() analytics_v1.AnalyticsServiceClient {
return &routingImpl_AnalyticsV1{c}
}
func (c *RoutingClient) SearchV1() search_v1.SearchServiceClient {
return &routingImpl_SearchV1{c}
}
func (c *RoutingClient) ViewV1() view_v1.ViewServiceClient {
return &routingImpl_ViewV1{c}
}
func (c *RoutingClient) QueryAdminV1() admin_query_v1.QueryAdminServiceClient {
return &routingImpl_QueryAdminV1{c}
}
func (c *RoutingClient) SearchAdminV1() admin_search_v1.SearchAdminServiceClient {
return &routingImpl_SearchAdminV1{c}
}
func (c *RoutingClient) Close() error {
table := c.routing.Load()
if table == nil {
// We're already closed.
return nil
}
c.lock.Lock()
closeErr := table.Conns.Close()
c.routing.Store(nil)
for bucket := range c.buckets {
c.CloseBucket(bucket)
}
c.lock.Unlock()
return closeErr
}