-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.go
258 lines (219 loc) · 6.35 KB
/
portal.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package portal
import (
"context"
"embed"
"fmt"
"net/http"
"sync"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"github.com/lachlanorr/rocketcycle/pkg/mgmt"
"github.com/lachlanorr/rocketcycle/pkg/platform"
"github.com/lachlanorr/rocketcycle/pkg/rkcy"
"github.com/lachlanorr/rocketcycle/pkg/rkcypb"
"github.com/lachlanorr/rocketcycle/pkg/telem"
"github.com/lachlanorr/rocketcycle/version"
)
//go:embed static/docs
var gDocsFiles embed.FS
type portalServer struct {
rkcypb.UnimplementedPortalServiceServer
plat *platform.Platform
httpAddr string
grpcAddr string
}
func (srv portalServer) HttpAddr() string {
return srv.httpAddr
}
func (srv portalServer) GrpcAddr() string {
return srv.grpcAddr
}
func (portalServer) StaticFiles() http.FileSystem {
return http.FS(gDocsFiles)
}
func (portalServer) StaticFilesPathPrefix() string {
return "/static/portal/docs"
}
func (srv portalServer) RegisterServer(srvReg grpc.ServiceRegistrar) {
rkcypb.RegisterPortalServiceServer(srvReg, srv)
}
func (portalServer) RegisterHandlerFromEndpoint(
ctx context.Context,
mux *runtime.ServeMux,
endpoint string,
opts []grpc.DialOption,
) (err error) {
return rkcypb.RegisterPortalServiceHandlerFromEndpoint(ctx, mux, endpoint, opts)
}
func (srv portalServer) PlatformDef(ctx context.Context, pa *rkcypb.Void) (*rkcypb.PlatformDef, error) {
if srv.plat.RtPlatformDef != nil {
return srv.plat.RtPlatformDef.PlatformDef, nil
}
return nil, status.Error(codes.FailedPrecondition, "platform not yet initialized")
}
func (srv portalServer) ConfigRead(ctx context.Context, pa *rkcypb.Void) (*rkcypb.ConfigReadResponse, error) {
return srv.plat.ConfigRdr().BuildConfigResponse(), nil
}
func (srv portalServer) DecodeInstance(ctx context.Context, args *rkcypb.DecodeInstanceArgs) (*rkcypb.DecodeResponse, error) {
jsonBytes, err := srv.plat.ClientCode.ConcernHandlers.DecodeInstance64Json(ctx, args.Concern, args.Payload64)
if err != nil {
return nil, err
}
return &rkcypb.DecodeResponse{
Type: args.Concern,
Instance: string(jsonBytes),
}, nil
}
func resultProto2DecodeResponse(resProto *rkcy.ResultProto) (*rkcypb.DecodeResponse, error) {
instJson, err := protojson.Marshal(resProto.Instance)
if err != nil {
return nil, err
}
decResp := &rkcypb.DecodeResponse{
Type: resProto.Type,
Instance: string(instJson),
}
if resProto.Related != nil {
relJson, err := protojson.Marshal(resProto.Related)
if err != nil {
return nil, err
}
decResp.Related = string(relJson)
}
return decResp, nil
}
func (srv portalServer) DecodeArgPayload(ctx context.Context, args *rkcypb.DecodePayloadArgs) (*rkcypb.DecodeResponse, error) {
resProto, _, err := srv.plat.ClientCode.ConcernHandlers.DecodeArgPayload64(ctx, args.Concern, args.System, args.Command, args.Payload64)
if err != nil {
return nil, err
}
return resultProto2DecodeResponse(resProto)
}
func (srv portalServer) DecodeResultPayload(ctx context.Context, args *rkcypb.DecodePayloadArgs) (*rkcypb.DecodeResponse, error) {
resProto, _, err := srv.plat.ClientCode.ConcernHandlers.DecodeResultPayload64(ctx, args.Concern, args.System, args.Command, args.Payload64)
if err != nil {
return nil, err
}
return resultProto2DecodeResponse(resProto)
}
func (srv portalServer) CancelTxn(ctx context.Context, cancelTxn *rkcypb.CancelTxnRequest) (*rkcypb.Void, error) {
ctx, traceId, span := telem.StartRequest(ctx)
defer span.End()
log.Warn().Msgf("CancelTxn %s", cancelTxn.TxnId)
cncAdminDir := &rkcypb.ConcernAdminDirective{
TxnId: cancelTxn.TxnId,
}
wg := &sync.WaitGroup{}
platCh := make(chan *mgmt.PlatformMessage)
mgmt.ConsumePlatformTopic(
ctx,
wg,
srv.plat.StreamProvider(),
srv.plat.Args.Platform,
srv.plat.Args.Environment,
srv.plat.Args.AdminBrokers,
platCh,
nil,
)
platMsg := <-platCh
rtPlat := platMsg.NewRtPlatDef
for _, rtCnc := range rtPlat.Concerns {
if rtCnc.Concern.Type == rkcypb.Concern_APECS {
adminRtTopics, ok := rtCnc.Topics[string(rkcy.ADMIN)]
if !ok {
return nil, fmt.Errorf("No admin topic for concern: %s", rtCnc.Concern.Name)
}
cluster, ok := rtPlat.Clusters[adminRtTopics.Topics.Current.Cluster]
if !ok {
return nil, fmt.Errorf("No brokers (%s) for concern topic: %s.%s", adminRtTopics.Topics.Current.Cluster, rtCnc.Concern.Name, rkcy.ADMIN)
}
log.Info().Msgf("%s - %s", cluster.Brokers, adminRtTopics.CurrentTopic)
prodCh := srv.plat.GetProducerCh(ctx, wg, cluster.Brokers)
msg, err := rkcy.NewKafkaMessage(
&adminRtTopics.CurrentTopic,
0,
cncAdminDir,
rkcypb.Directive_CONCERN_ADMIN_CANCEL_TXN,
traceId,
)
if err != nil {
return nil, err
}
prodCh <- msg
}
}
return &rkcypb.Void{}, nil
}
func serveGrpc(
ctx context.Context,
plat *platform.Platform,
httpAddr string,
grpcAddr string,
) {
srv := portalServer{
plat: plat,
httpAddr: httpAddr,
grpcAddr: grpcAddr,
}
plat.InitConfigMgr(ctx)
ServeGrpcGateway(ctx, srv)
}
func Serve(
ctx context.Context,
plat *platform.Platform,
httpAddr string,
grpcAddr string,
) {
log.Info().
Str("GitCommit", version.GitCommit).
Msg("portal server started")
go serveGrpc(ctx, plat, httpAddr, grpcAddr)
go managePlatform(ctx, plat)
plat.WaitGroup().Add(1)
select {
case <-ctx.Done():
log.Info().
Msg("portal stopped")
plat.WaitGroup().Done()
return
}
}
func managePlatform(
ctx context.Context,
plat *platform.Platform,
) {
platCh := make(chan *mgmt.PlatformMessage)
mgmt.ConsumePlatformTopic(
ctx,
plat.WaitGroup(),
plat.StreamProvider(),
plat.Args.Platform,
plat.Args.Environment,
plat.Args.AdminBrokers,
platCh,
nil,
)
plat.WaitGroup().Add(1)
for {
select {
case <-ctx.Done():
log.Trace().
Msg("managePlatform exiting, ctx.Done()")
plat.WaitGroup().Done()
return
case platMsg := <-platCh:
if (platMsg.Directive & rkcypb.Directive_PLATFORM) != rkcypb.Directive_PLATFORM {
log.Error().Msgf("Invalid directive for PlatformTopic: %s", platMsg.Directive.String())
continue
}
plat.RtPlatformDef = platMsg.NewRtPlatDef
}
}
}