-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
427 lines (360 loc) · 17.5 KB
/
main.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package main
import (
"context"
_ "embed"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/validator"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/kouhin/envflag"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
v1alpha "github.com/kaskada-ai/kaskada/gen/proto/go/kaskada/kaskada/v1alpha"
v2alpha "github.com/kaskada-ai/kaskada/gen/proto/go/kaskada/kaskada/v2alpha"
"github.com/kaskada-ai/kaskada/wren/auth"
"github.com/kaskada-ai/kaskada/wren/client"
"github.com/kaskada-ai/kaskada/wren/compute"
"github.com/kaskada-ai/kaskada/wren/internal"
"github.com/kaskada-ai/kaskada/wren/service"
"github.com/kaskada-ai/kaskada/wren/store"
"github.com/kaskada-ai/kaskada/wren/telemetry"
"github.com/kaskada-ai/kaskada/wren/utils"
)
var (
// db configuration
dbDialect = flag.String("db-dialect", "sqlite", "database dialect to use. should be `postgres` or `sqlite` (default)")
dbHost = flag.String("db-host", "postgres", "database hostname (only for postgres)")
dbInMemory = flag.Bool("db-in-memory", true, "use an in-memory database (only for sqlite)")
dbName = flag.String("db-name", "wren", "database name (only for postgres)")
dbPath = flag.String("db-path", "~/.cache/kaskada/data/wren.db", "the path database file, when `db-in-memory` is false (only for sqlite)")
dbPass = flag.String("db-pass", "wren123", "database password")
dbPort = flag.Int("db-port", 5432, "database port (only for postgres)")
dbUseSSL = flag.Bool("db-use-ssl", false, "the ssl mode to use when connection to the database (only for postgres)")
dbUser = flag.String("db-user", "wren", "database username")
objectStoreType = flag.String("object-store-type", "local", "the type of object store to use. should be `local` (default), `s3`, `gcs`, or `azure`.")
objectStoreBucket = flag.String("object-store-bucket", "", "the bucket or container to use for storing objects. required if the `object-store-type` is not `local`.")
objectStoreDisableSSL = flag.Bool("object-store-disable-ssl", false, "set true to disable SSL when connecting to the object store")
objectStoreEndpoint = flag.String("object-store-endpoint", "", "the endpoint for accessing the object store. will use the object store default if this is not defined.")
objectStoreForcePathStyle = flag.Bool("object-store-force-path-style", false, "set to true to access the bucket as a path instead via a sub-domain. required when using minio as the object store.")
objectStorePath = flag.String("object-store-path", "~/.cache/kaskada/data", "the path or prefix for storing data objects. can be a relative path if the `object-store-type` is `local`.")
debug = flag.Bool("debug", false, "sets log level to debug")
debugDB = flag.Bool("debug-db", false, "logs database debug info")
debugGrpc = flag.Bool("debug-grpc", false, "logs grpc connection debug info")
defaultClientID = flag.String("default-client-id", "default-client-id", "the default client-id to use when one isn't passed in the request")
env = flag.String("env", "prod", "the environment the service is running in")
grpcHealthPort = flag.Int("grpc-health-port", 6666, "the port of the gRPC health listener")
grpcPort = flag.Int("grpc-port", 50051, "the port of the gRPC listener")
otelEndpoint = flag.String("otel-exporter-otlp-endpoint", "", "the host and port of the grpc otel otlp exporter")
fileServiceHost = flag.String("file-service-host", "localhost", "the hostname of the file service")
fileServicePort = flag.Int("file-service-port", 50052, "the port of the file service")
fileServiceUseTLS = flag.Bool("file-service-use-tls", false, "should TLS be used to connect to the file service")
logFormatJson = flag.Bool("log-format-json", false, "if enabled, logs will be outputted in json")
logHealthCheck = flag.Bool("log-health-check", false, "if enabled, output logs for health-check API calls")
logNoColor = flag.Int("no-color", 0, "set to `1` to disable color output in logs")
prepareParallelizeFactor = flag.Int("prepare-parallelize-factor", 2, "the number of parallel prepare requests that are made per api request")
prepareServiceHost = flag.String("prepare-service-host", "localhost", "the hostname of the prepare service")
prepareServicePort = flag.Int("prepare-service-port", 50052, "the port of the prepare service")
prepareServiceUseTLS = flag.Bool("prepare-service-use-tls", false, "should TLS be used to connect to the prepare service")
promPort = flag.Int("prom-port", 9100, "the port for pulling prometheus metrics")
queryParallelizeFactor = flag.Int("query-parallelize-factor", 1, "the number of parallel query requests that are made per api request")
queryServiceHost = flag.String("query-service-host", "localhost", "the hostname of the query service")
queryServicePort = flag.Int("query-service-port", 50052, "the port of the query service")
queryServiceUseTLS = flag.Bool("query-service-use-tls", false, "should TLS be used to connect to the query service")
restPort = flag.Int("rest-port", 3365, "the port of the REST listener")
safeShutdownSeconds = flag.Int("safe-shutdown-seconds", 120, "the maximum duration in seconds to try to perform a safe shutdown before killing in-flight requests")
license = flag.Bool("license", false, "prints the software's license")
version = flag.Bool("version", false, "prints the software's version")
grpcServer *grpc.Server
grpcHealthServer *grpc.Server
grpcGatewayServer *http.Server
metricsServer *http.Server
fileServiceConfig *client.HostConfig
icebergTableServiceConfig *client.HostConfig
prepareServiceConfig *client.HostConfig
queryServiceConfig *client.HostConfig
)
// ServerConfig contains the config to startup the server
type ServerConfig struct {
GrpcPort int
}
//go:embed .version
var service_version string
//go:embed NOTICE
var service_license string
func main() {
flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "starts the service")
flag.PrintDefaults()
}
if err := envflag.Parse(); err != nil {
log.Fatal().Err(err).Msg("Failed to parse flags")
}
if license != nil && *license {
fmt.Println(service_license)
return
}
if version != nil && *version {
fmt.Println(service_version)
return
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(interrupt)
noColor := *logNoColor == 1
logProvider := telemetry.NewLoggingProvider(*debug, *debugGrpc, *logFormatJson, *logHealthCheck, noColor, *env)
metricsProvider := telemetry.NewMetricsProvider()
traceProvider, shutdownTrace := telemetry.NewTracingProvider(ctx, *otelEndpoint)
defer shutdownTrace()
g, ctx := errgroup.WithContext(ctx)
log.Info().Msg("starting manager")
// setup compute clients
if *fileServiceHost != "" {
fileServiceConfig = &client.HostConfig{
Host: *fileServiceHost,
Port: *fileServicePort,
UseTLS: *fileServiceUseTLS,
}
}
if *prepareServiceHost != "" {
prepareServiceConfig = &client.HostConfig{
Host: *prepareServiceHost,
Port: *prepareServicePort,
UseTLS: *prepareServiceUseTLS,
}
}
if *queryServiceHost != "" {
queryServiceConfig = &client.HostConfig{
Host: *queryServiceHost,
Port: *queryServicePort,
UseTLS: *queryServiceUseTLS,
}
}
entConfig := client.NewEntConfig(*dbDialect, dbName, dbHost, dbInMemory, dbPath, dbPass, dbPort, dbUser, dbUseSSL)
entClient := client.NewEntClient(ctx, entConfig)
if *debugDB {
entClient = entClient.Debug()
}
defer entClient.Close()
objectStoreClient := client.NewObjectStoreClient(*env, *objectStoreType, *objectStoreBucket, *objectStorePath, *objectStoreEndpoint, *objectStoreDisableSSL, *objectStoreForcePathStyle)
ownerClient := internal.NewOwnerClient(entClient)
dataTokenClient := internal.NewDataTokenClient(entClient)
kaskadaTableClient := internal.NewKaskadaTableClient(entClient)
kaskadaViewClient := internal.NewKaskadaViewClient(entClient)
materializationClient := internal.NewMaterializationClient(entClient)
kaskadaQueryClient := internal.NewKaskadaQueryClient(entClient)
prepareJobClient := internal.NewPrepareJobClient(entClient)
computeClients := client.CreateComputeClients(fileServiceConfig, prepareServiceConfig, queryServiceConfig)
// Defines the authorization function as a client ID header
authFunc := auth.ClientIDHeaderAuthFunc(*defaultClientID, ownerClient)
// populate parallize config
// see: https://github.com/kaskada-ai/kaskada/issues/69
var parallelizeConfig *utils.ParallelizeConfig
if entConfig.DriverName() == "sqlite3" {
if *prepareParallelizeFactor > 1 || *queryParallelizeFactor > 1 {
log.Warn().Msg("sqlite backing store currently doesn't support parallel operation. setting all parallization factors to 1.")
}
parallelizeConfig = &utils.ParallelizeConfig{
PrepareFactor: 1,
QueryFactor: 1,
}
} else {
parallelizeConfig = &utils.ParallelizeConfig{
PrepareFactor: *prepareParallelizeFactor,
QueryFactor: *queryParallelizeFactor,
}
}
// connect to stores
tableStore := store.NewTableStore(&objectStoreClient)
compileManager := compute.NewCompileManager(&computeClients, &kaskadaTableClient, &kaskadaViewClient)
prepareManager := compute.NewPrepareManager(&computeClients, &kaskadaTableClient, &objectStoreClient, &prepareJobClient, parallelizeConfig, tableStore)
computeManager := compute.NewComputeManager(g, &compileManager, &computeClients, &dataTokenClient, &kaskadaTableClient, &materializationClient, &objectStoreClient, &prepareManager)
fileManager := compute.NewFileManager(&computeClients)
materializationManager := compute.NewMaterializationManager(&compileManager, &computeClients, &kaskadaTableClient, &materializationClient)
// gRPC Health Server
healthServer := health.NewServer()
g.Go(func() error {
unaryServerChain := grpcmiddleware.WithUnaryServerChain(
logProvider.UnaryPayloadInterceptor,
)
grpcHealthServer = grpc.NewServer(unaryServerChain)
healthpb.RegisterHealthServer(grpcHealthServer, healthServer)
hln, err := net.Listen("tcp", fmt.Sprintf(":%d", *grpcHealthPort))
if err != nil {
log.Fatal().Int("port", *grpcHealthPort).Err(err).Msg("not able to start gRPC-health listener")
}
log.Info().Int("port", *grpcHealthPort).Msg("gRPC-health server is online")
return grpcHealthServer.Serve(hln)
})
// metrics http server
g.Go(func() error {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
// Start HTTP metrics server
metricsServer = &http.Server{
Addr: fmt.Sprintf(":%d", *promPort),
Handler: mux,
}
log.Info().Int("port", *promPort).Msg("metrics is online")
if err := metricsServer.ListenAndServe(); err != http.ErrServerClosed {
log.Error().Err(err).Int("port", *promPort).Msg("not able to start metrics listener")
return err
}
return nil
})
// gRPC server
g.Go(func() error {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *grpcPort))
if err != nil {
log.Fatal().Err(err).Int("grpcPort", *grpcPort).Msg("not able to start gRPC listener")
}
// Unary Interceptors
unaryServerChain := grpcmiddleware.WithUnaryServerChain(
grpc_prometheus.UnaryServerInterceptor,
otelgrpc.UnaryServerInterceptor(),
traceProvider.UnaryInterceptor,
logProvider.UnaryInterceptor,
logProvider.UnaryPayloadInterceptor,
grpc_auth.UnaryServerInterceptor(authFunc),
validator.UnaryServerInterceptor(true),
)
// Stream Interceptors
streamServerChain := grpcmiddleware.WithStreamServerChain(
grpc_prometheus.StreamServerInterceptor,
otelgrpc.StreamServerInterceptor(),
traceProvider.StreamInterceptor,
logProvider.StreamInterceptor,
logProvider.StreamPayloadInterceptor,
grpc_auth.StreamServerInterceptor(authFunc),
validator.StreamServerInterceptor(true),
)
grpcServer = grpc.NewServer(unaryServerChain, streamServerChain)
metricsProvider.RegisterGrpc(grpcServer)
dependencyAnalyzerService := service.NewDependencyAnalyzer(&kaskadaViewClient, &materializationClient)
tableService := service.NewTableService(&computeManager, &fileManager, &kaskadaTableClient, &objectStoreClient, tableStore, &dependencyAnalyzerService)
viewService := service.NewViewService(&compileManager, &kaskadaTableClient, &kaskadaViewClient, &dependencyAnalyzerService)
materializationService := service.NewMaterializationService(&computeManager, &materializationManager, &kaskadaTableClient, &kaskadaViewClient, &dataTokenClient, &materializationClient)
queryV1Service := service.NewQueryV1Service(&computeManager, &dataTokenClient, &kaskadaQueryClient, &kaskadaTableClient, &objectStoreClient, &prepareManager)
// Register the grpc services
v1alpha.RegisterDataTokenServiceServer(grpcServer, service.NewDataTokenService(&dataTokenClient))
v1alpha.RegisterTableServiceServer(grpcServer, tableService)
v1alpha.RegisterViewServiceServer(grpcServer, viewService)
v1alpha.RegisterMaterializationServiceServer(grpcServer, materializationService)
v1alpha.RegisterQueryServiceServer(grpcServer, queryV1Service)
queryV2Service := service.NewQueryV2Service(&computeManager, &dataTokenClient, &kaskadaQueryClient)
v2alpha.RegisterQueryServiceServer(grpcServer, queryV2Service)
// Register reflection service on gRPC server.
reflection.Register(grpcServer)
log.Info().Int("grpcPort", *grpcPort).Msg("gRPC server is online")
healthServer.SetServingStatus("grpc.health.v1.manager", healthpb.HealthCheckResponse_SERVING)
return grpcServer.Serve(lis)
})
// grpc-gateway REST server
g.Go(func() error {
serviceHandlerMap := map[string]func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error{
"materialization": v1alpha.RegisterMaterializationServiceHandlerFromEndpoint,
"query": v2alpha.RegisterQueryServiceHandlerFromEndpoint,
"table": v1alpha.RegisterTableServiceHandlerFromEndpoint,
"view": v1alpha.RegisterViewServiceHandlerFromEndpoint,
}
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
wrenGrpcEndpoint := fmt.Sprintf("localhost:%d", *grpcPort)
for service, handler := range serviceHandlerMap {
if err := handler(ctx, mux, wrenGrpcEndpoint, opts); err != nil {
log.Fatal().Err(err).Msgf("failed to register %s service", service)
}
}
// Start HTTP server (and proxy calls to gRPC server endpoint)
grpcGatewayServer = &http.Server{
Addr: fmt.Sprintf(":%d", *restPort),
Handler: mux,
}
log.Info().Int("port", *restPort).Msg("gRPC-gateway is online")
if err := grpcGatewayServer.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal().Err(err).Int("port", *restPort).Msg("not able to start gRPC-gateway listener")
return err
}
return nil
})
// peridocally reconcile materializations to ensure the ones that are supposed to be running are running
g.Go(func() error {
count := 0
for {
time.Sleep(time.Second)
select {
case <-interrupt:
return nil
case <-ctx.Done():
return nil
default:
}
count++
if count == 60 {
count = 0
if err := materializationManager.ReconcileMaterializations(ctx); err != nil {
return err
}
}
}
})
// wait until shutdown signal occurs
select {
case <-interrupt:
log.Info().Msg("interrupt!")
break
case <-ctx.Done():
log.Info().Msg("context done!")
break
}
log.Info().Msg("received shutdown signal")
cancel()
log.Info().Msg("canceled the main context")
healthServer.SetServingStatus("grpc.health.v1.manager", healthpb.HealthCheckResponse_NOT_SERVING)
log.Info().Msg("set gRPC-health status to not-serving")
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Duration(*safeShutdownSeconds)*time.Second)
defer shutdownCancel()
log.Info().Msg("created a shutdownCancel context")
gracefulShutdownHttpServer(shutdownCtx, grpcGatewayServer, "gRPC-gateway")
gracefulStopGrpcServer(grpcServer, "gRPC")
gracefulShutdownHttpServer(shutdownCtx, metricsServer, "metrics")
gracefulStopGrpcServer(grpcHealthServer, "gRPC-health")
if err := g.Wait(); err != nil {
log.Fatal().Err(err).Msg("manager terminated!")
}
}
func gracefulShutdownHttpServer(ctx context.Context, server *http.Server, name string) {
if server != nil {
log.Info().Msgf("initiating graceful shutdown of the %s server", name)
if err := server.Shutdown(ctx); err != nil {
log.Error().Err(err).Msgf("error shutting down the %s server", name)
} else {
log.Info().Msgf("graceful shutdown of the %s server is complete", name)
}
}
}
func gracefulStopGrpcServer(server *grpc.Server, name string) {
if server != nil {
log.Info().Msgf("initiating graceful stop of the %s server", name)
server.GracefulStop()
log.Info().Msgf("graceful stop of the %s server is complete", name)
}
}