-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmain.go
180 lines (154 loc) · 6.3 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
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
//"strings"
"syscall"
"github.com/feast-dev/feast/go/internal/feast"
"github.com/feast-dev/feast/go/internal/feast/registry"
"github.com/feast-dev/feast/go/internal/feast/server"
"github.com/feast-dev/feast/go/internal/feast/server/logging"
"github.com/feast-dev/feast/go/protos/feast/serving"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
//grpctrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/google.golang.org/grpc"
//"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
type ServerStarter interface {
StartHttpServer(fs *feast.FeatureStore, host string, port int, writeLoggedFeaturesCallback logging.OfflineStoreWriteCallback, loggingOpts *logging.LoggingOptions) error
StartGrpcServer(fs *feast.FeatureStore, host string, port int, writeLoggedFeaturesCallback logging.OfflineStoreWriteCallback, loggingOpts *logging.LoggingOptions) error
}
type RealServerStarter struct{}
func (s *RealServerStarter) StartHttpServer(fs *feast.FeatureStore, host string, port int, writeLoggedFeaturesCallback logging.OfflineStoreWriteCallback, loggingOpts *logging.LoggingOptions) error {
return StartHttpServer(fs, host, port, writeLoggedFeaturesCallback, loggingOpts)
}
func (s *RealServerStarter) StartGrpcServer(fs *feast.FeatureStore, host string, port int, writeLoggedFeaturesCallback logging.OfflineStoreWriteCallback, loggingOpts *logging.LoggingOptions) error {
return StartGrpcServer(fs, host, port, writeLoggedFeaturesCallback, loggingOpts)
}
func main() {
// Default values
serverType := "http"
host := ""
port := 8080
server := RealServerStarter{}
// Current Directory
repoPath, err := os.Getwd()
if err != nil {
log.Error().Stack().Err(err).Msg("Failed to get current directory")
}
flag.StringVar(&serverType, "type", serverType, "Specify the server type (http or grpc)")
flag.StringVar(&repoPath, "chdir", repoPath, "Repository path where feature store yaml file is stored")
flag.StringVar(&host, "host", host, "Specify a host for the server")
flag.IntVar(&port, "port", port, "Specify a port for the server")
flag.Parse()
repoConfig, err := registry.NewRepoConfigFromFile(repoPath)
if err != nil {
log.Fatal().Stack().Err(err).Msg("Failed to convert to RepoConfig")
}
fs, err := feast.NewFeatureStore(repoConfig, nil)
if err != nil {
log.Fatal().Stack().Err(err).Msg("Failed to create NewFeatureStore")
}
loggingOptions, err := repoConfig.GetLoggingOptions()
if err != nil {
log.Fatal().Stack().Err(err).Msg("Failed to get LoggingOptions")
}
// TODO: writeLoggedFeaturesCallback is defaulted to nil. write_logged_features functionality needs to be
// implemented in Golang specific to OfflineStoreSink. Python Feature Server doesn't support this.
if serverType == "http" {
err = server.StartHttpServer(fs, host, port, nil, loggingOptions)
} else if serverType == "grpc" {
err = server.StartGrpcServer(fs, host, port, nil, loggingOptions)
} else {
fmt.Println("Unknown server type. Please specify 'http' or 'grpc'.")
}
if err != nil {
log.Fatal().Stack().Err(err).Msg("Failed to start server")
}
}
func constructLoggingService(fs *feast.FeatureStore, writeLoggedFeaturesCallback logging.OfflineStoreWriteCallback, loggingOpts *logging.LoggingOptions) (*logging.LoggingService, error) {
var loggingService *logging.LoggingService = nil
if writeLoggedFeaturesCallback != nil {
sink, err := logging.NewOfflineStoreSink(writeLoggedFeaturesCallback)
if err != nil {
return nil, err
}
loggingService, err = logging.NewLoggingService(fs, sink, logging.LoggingOptions{
ChannelCapacity: loggingOpts.ChannelCapacity,
EmitTimeout: loggingOpts.EmitTimeout,
WriteInterval: loggingOpts.WriteInterval,
FlushInterval: loggingOpts.FlushInterval,
})
if err != nil {
return nil, err
}
}
return loggingService, nil
}
// StartGprcServerWithLogging starts gRPC server with enabled feature logging
func StartGrpcServer(fs *feast.FeatureStore, host string, port int, writeLoggedFeaturesCallback logging.OfflineStoreWriteCallback, loggingOpts *logging.LoggingOptions) error {
// #DD
//if strings.ToLower(os.Getenv("ENABLE_DATADOG_TRACING")) == "true" {
// tracer.Start(tracer.WithRuntimeMetrics())
// defer tracer.Stop()
//}
loggingService, err := constructLoggingService(fs, writeLoggedFeaturesCallback, loggingOpts)
if err != nil {
return err
}
ser := server.NewGrpcServingServiceServer(fs, loggingService)
log.Info().Msgf("Starting a gRPC server on host %s port %d", host, port)
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
return err
}
grpcServer := grpc.NewServer()
serving.RegisterServingServiceServer(grpcServer, ser)
healthService := health.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, healthService)
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
go func() {
// As soon as these signals are received from OS, try to gracefully stop the gRPC server
<-stop
log.Info().Msg("Stopping the gRPC server...")
grpcServer.GracefulStop()
if loggingService != nil {
loggingService.Stop()
}
log.Info().Msg("gRPC server terminated")
}()
return grpcServer.Serve(lis)
}
// StartHttpServerWithLogging starts HTTP server with enabled feature logging
// Go does not allow direct assignment to package-level functions as a way to
// mock them for tests
func StartHttpServer(fs *feast.FeatureStore, host string, port int, writeLoggedFeaturesCallback logging.OfflineStoreWriteCallback, loggingOpts *logging.LoggingOptions) error {
loggingService, err := constructLoggingService(fs, writeLoggedFeaturesCallback, loggingOpts)
if err != nil {
return err
}
ser := server.NewHttpServer(fs, loggingService)
log.Info().Msgf("Starting a HTTP server on host %s, port %d", host, port)
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
go func() {
// As soon as these signals are received from OS, try to gracefully stop the gRPC server
<-stop
log.Info().Msg("Stopping the HTTP server...")
err := ser.Stop()
if err != nil {
log.Error().Err(err).Msg("Error when stopping the HTTP server")
}
if loggingService != nil {
loggingService.Stop()
}
log.Info().Msg("HTTP server terminated")
}()
return ser.Serve(host, port)
}