/
controller.go
210 lines (178 loc) · 5.82 KB
/
controller.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
// Copyright 2022 Linkall Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate mockgen -source=controller.go -destination=mock_controller.go -package=cluster
package cluster
import (
"context"
stderr "errors"
"strings"
"sync"
"time"
errors2 "github.com/vanus-labs/vanus/api/errors"
"github.com/vanus-labs/vanus/pkg/observability/log"
"google.golang.org/grpc/credentials"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/vanus-labs/vanus/api/cluster/raw_client"
ctrlpb "github.com/vanus-labs/vanus/api/controller"
"github.com/vanus-labs/vanus/api/errors"
metapb "github.com/vanus-labs/vanus/api/meta"
)
var defaultClusterStartTimeout = 3 * time.Minute
type Topology struct {
ControllerLeader string
ControllerURLs []string
Uptime time.Time
}
type Cluster interface {
WaitForControllerReady(createEventbus bool) error
Status() Topology
IsReady(createEventbus bool) bool
NamespaceService() NamespaceService
EventbusService() EventbusService
SegmentService() SegmentService
EventlogService() EventlogService
TriggerService() TriggerService
IDService() IDService
AuthService() AuthService
}
type NamespaceService interface {
RawClient() ctrlpb.NamespaceControllerClient
GetSystemNamespace(ctx context.Context) (*metapb.Namespace, error)
GetDefaultNamespace(ctx context.Context) (*metapb.Namespace, error)
GetNamespace(ctx context.Context, id uint64) (*metapb.Namespace, error)
GetNamespaceByName(ctx context.Context, name string) (*metapb.Namespace, error)
}
type EventbusService interface {
CreateSystemEventbusIfNotExist(ctx context.Context, name string, desc string) (*metapb.Eventbus, error)
Delete(ctx context.Context, id uint64) error
GetSystemEventbusByName(ctx context.Context, name string) (*metapb.Eventbus, error)
GetEventbus(ctx context.Context, id uint64) (*metapb.Eventbus, error)
GetEventbusByName(ctx context.Context, ns, name string) (*metapb.Eventbus, error)
RawClient() ctrlpb.EventbusControllerClient
}
type EventlogService interface {
RawClient() ctrlpb.EventlogControllerClient
}
type TriggerService interface {
RawClient() ctrlpb.TriggerControllerClient
GetSubscription(ctx context.Context, id uint64) (*metapb.Subscription, error)
RegisterHeartbeat(ctx context.Context, interval time.Duration, reqFunc func() interface{}) error
}
type IDService interface {
RawClient() ctrlpb.SnowflakeControllerClient
}
type SegmentService interface {
RegisterHeartbeat(ctx context.Context, interval time.Duration, reqFunc func() interface{}) error
RawClient() ctrlpb.SegmentControllerClient
}
type AuthService interface {
RawClient() ctrlpb.AuthControllerClient
GetUserByToken(ctx context.Context, token string) (string, error)
GetUserRole(ctx context.Context, user string) ([]*metapb.UserRole, error)
}
var (
connCache = map[string]*raw_client.Conn{}
mutex sync.Mutex
)
func NewClusterController(endpoints []string, credentials credentials.TransportCredentials) Cluster {
mutex.Lock()
defer mutex.Unlock()
cc, exist := connCache[strings.Join(endpoints, ",")]
if !exist {
cc = raw_client.NewConnection(endpoints, credentials)
connCache[strings.Join(endpoints, ",")] = cc
}
// single instance
c := &cluster{
cc: cc,
nsSvc: newNamespaceService(cc),
segmentSvc: newSegmentService(cc),
elSvc: newEventlogService(cc),
triggerSvc: newTriggerService(cc),
idSvc: newIDService(cc),
authSvc: newAuthService(cc),
ping: raw_client.NewPingClient(cc),
controllerAddress: endpoints,
}
c.ebSvc = newEventbusService(cc, c.NamespaceService())
return c
}
type cluster struct {
controllerAddress []string
cc *raw_client.Conn
nsSvc NamespaceService
ebSvc EventbusService
elSvc EventlogService
triggerSvc TriggerService
idSvc IDService
segmentSvc SegmentService
ping ctrlpb.PingServerClient
authSvc AuthService
}
func (c *cluster) WaitForControllerReady(createEventbus bool) error {
start := time.Now()
log.Info().Msg("wait for controller is ready")
t := time.NewTicker(defaultClusterStartTimeout)
defer t.Stop()
for !c.IsReady(createEventbus) {
select {
case <-t.C:
return stderr.New("cluster isn't ready")
default:
time.Sleep(time.Second)
}
}
log.Info().
Dur("waiting_time", time.Now().Sub(start)).
Msg("controller is ready")
return nil
}
func (c *cluster) IsReady(createEventbus bool) bool {
res, err := c.ping.Ping(context.Background(), &emptypb.Empty{})
if err != nil {
if !errors.Is(err, errors2.ErrNotLeader) {
log.Warn().Err(err).Msg("failed to ping controller")
}
return false
}
if res.LeaderAddr == "" {
return false
}
return !createEventbus || (createEventbus && res.GetIsEventbusReady())
}
func (c *cluster) Status() Topology {
// TODO(wenfeng)
return Topology{}
}
func (c *cluster) NamespaceService() NamespaceService {
return c.nsSvc
}
func (c *cluster) EventbusService() EventbusService {
return c.ebSvc
}
func (c *cluster) SegmentService() SegmentService {
return c.segmentSvc
}
func (c *cluster) EventlogService() EventlogService {
return c.elSvc
}
func (c *cluster) TriggerService() TriggerService {
return c.triggerSvc
}
func (c *cluster) IDService() IDService {
return c.idSvc
}
func (c *cluster) AuthService() AuthService {
return c.authSvc
}