-
Notifications
You must be signed in to change notification settings - Fork 175
/
engine.go
254 lines (218 loc) · 7.91 KB
/
engine.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
package rpc
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"time"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
accessproto "github.com/onflow/flow/protobuf/go/flow/access"
legacyaccessproto "github.com/onflow/flow/protobuf/go/flow/legacy/access"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"github.com/onflow/flow-go/access"
legacyaccess "github.com/onflow/flow-go/access/legacy"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/engine/access/rpc/backend"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
grpcutils "github.com/onflow/flow-go/utils/grpc"
)
// Config defines the configurable options for the access node server
type Config struct {
GRPCListenAddr string // the GRPC server address as ip:port
HTTPListenAddr string // the HTTP web proxy address as ip:port
CollectionAddr string // the address of the upstream collection node
HistoricalAccessAddrs string // the list of all access nodes from previous spork
MaxMsgSize int // GRPC max message size
ExecutionClientTimeout time.Duration // execution API GRPC client timeout
CollectionClientTimeout time.Duration // collection API GRPC client timeout
MaxHeightRange uint // max size of height range requests
PreferredExecutionNodeIDs []string // preferred list of upstream execution node IDs
FixedExecutionNodeIDs []string // fixed list of execution node IDs to choose from if no node node ID can be chosen from the PreferredExecutionNodeIDs
}
// Engine implements a gRPC server with a simplified version of the Observation API.
type Engine struct {
unit *engine.Unit
log zerolog.Logger
backend *backend.Backend // the gRPC service implementation
grpcServer *grpc.Server // the gRPC server
httpServer *http.Server
config Config
grpcAddress net.Addr
}
// New returns a new RPC engine.
func New(log zerolog.Logger,
state protocol.State,
config Config,
collectionRPC accessproto.AccessAPIClient,
historicalAccessNodes []accessproto.AccessAPIClient,
blocks storage.Blocks,
headers storage.Headers,
collections storage.Collections,
transactions storage.Transactions,
executionReceipts storage.ExecutionReceipts,
chainID flow.ChainID,
transactionMetrics module.TransactionMetrics,
collectionGRPCPort uint,
executionGRPCPort uint,
retryEnabled bool,
rpcMetricsEnabled bool,
apiRatelimits map[string]int, // the api rate limit (max calls per second) for each of the Access API e.g. Ping->100, GetTransaction->300
apiBurstLimits map[string]int, // the api burst limit (max calls at the same time) for each of the Access API e.g. Ping->50, GetTransaction->10
) *Engine {
log = log.With().Str("engine", "rpc").Logger()
if config.MaxMsgSize == 0 {
config.MaxMsgSize = grpcutils.DefaultMaxMsgSize
}
// create a GRPC server to serve GRPC clients
grpcOpts := []grpc.ServerOption{
grpc.MaxRecvMsgSize(config.MaxMsgSize),
grpc.MaxSendMsgSize(config.MaxMsgSize),
}
var interceptors []grpc.UnaryServerInterceptor // ordered list of interceptors
// if rpc metrics is enabled, first create the grpc metrics interceptor
if rpcMetricsEnabled {
interceptors = append(interceptors, grpc_prometheus.UnaryServerInterceptor)
}
// add the logging interceptor
interceptors = append(interceptors, loggingInterceptor(log)...)
if len(apiRatelimits) > 0 {
// create a rate limit interceptor
rateLimitInterceptor := NewRateLimiterInterceptor(log, apiRatelimits, apiBurstLimits).unaryServerInterceptor
// append the rate limit interceptor to the list of interceptors
interceptors = append(interceptors, rateLimitInterceptor)
}
if len(interceptors) > 0 {
// create a chained unary interceptor
chainedInterceptors := grpc.ChainUnaryInterceptor(interceptors...)
grpcOpts = append(grpcOpts, chainedInterceptors)
}
grpcServer := grpc.NewServer(grpcOpts...)
// wrap the GRPC server with an HTTP proxy server to serve HTTP clients
httpServer := NewHTTPServer(grpcServer, config.HTTPListenAddr)
connectionFactory := &backend.ConnectionFactoryImpl{
CollectionGRPCPort: collectionGRPCPort,
ExecutionGRPCPort: executionGRPCPort,
CollectionNodeGRPCTimeout: config.CollectionClientTimeout,
ExecutionNodeGRPCTimeout: config.ExecutionClientTimeout,
}
backend := backend.New(
state,
collectionRPC,
historicalAccessNodes,
blocks,
headers,
collections,
transactions,
executionReceipts,
chainID,
transactionMetrics,
connectionFactory,
retryEnabled,
config.MaxHeightRange,
config.PreferredExecutionNodeIDs,
config.FixedExecutionNodeIDs,
log,
)
eng := &Engine{
log: log,
unit: engine.NewUnit(),
backend: backend,
grpcServer: grpcServer,
httpServer: httpServer,
config: config,
}
accessproto.RegisterAccessAPIServer(
eng.grpcServer,
access.NewHandler(backend, chainID.Chain()),
)
if rpcMetricsEnabled {
// Not interested in legacy metrics, so initialize here
grpc_prometheus.EnableHandlingTimeHistogram()
grpc_prometheus.Register(grpcServer)
}
// Register legacy gRPC handlers for backwards compatibility, to be removed at a later date
legacyaccessproto.RegisterAccessAPIServer(
eng.grpcServer,
legacyaccess.NewHandler(backend, chainID.Chain()),
)
return eng
}
// Ready returns a ready channel that is closed once the engine has fully
// started. The RPC engine is ready when the gRPC server has successfully
// started.
func (e *Engine) Ready() <-chan struct{} {
e.unit.Launch(e.serveGRPC)
e.unit.Launch(e.serveGRPCWebProxy)
return e.unit.Ready()
}
// Done returns a done channel that is closed once the engine has fully stopped.
// It sends a signal to stop the gRPC server, then closes the channel.
func (e *Engine) Done() <-chan struct{} {
return e.unit.Done(
e.grpcServer.GracefulStop,
func() {
err := e.httpServer.Shutdown(context.Background())
if err != nil {
e.log.Error().Err(err).Msg("error stopping http server")
}
})
}
// SubmitLocal submits an event originating on the local node.
func (e *Engine) SubmitLocal(event interface{}) {
e.unit.Launch(func() {
err := e.process(event)
if err != nil {
e.log.Error().Err(err).Msg("could not process submitted event")
}
})
}
func (e *Engine) GRPCAddress() net.Addr {
return e.grpcAddress
}
// process processes the given ingestion engine event. Events that are given
// to this function originate within the expulsion engine on the node with the
// given origin ID.
func (e *Engine) process(event interface{}) error {
switch entity := event.(type) {
case *flow.Block:
e.backend.NotifyFinalizedBlockHeight(entity.Header.Height)
return nil
default:
return fmt.Errorf("invalid event type (%T)", event)
}
}
// serveGRPC starts the gRPC server
// When this function returns, the server is considered ready.
func (e *Engine) serveGRPC() {
e.log.Info().Str("grpc_address", e.config.GRPCListenAddr).Msg("starting grpc server on address")
l, err := net.Listen("tcp", e.config.GRPCListenAddr)
if err != nil {
e.log.Err(err).Msg("failed to start the grpc server")
return
}
// save the actual address on which we are listening (may be different from e.config.GRPCListenAddr if not port
// was specified)
e.grpcAddress = l.Addr()
e.log.Debug().Str("grpc_address", e.grpcAddress.String()).Msg("listening on port")
err = e.grpcServer.Serve(l) // blocking call
if err != nil {
e.log.Fatal().Err(err).Msg("fatal error in grpc server")
}
}
// serveGRPCWebProxy starts the gRPC web proxy server
func (e *Engine) serveGRPCWebProxy() {
log := e.log.With().Str("http_proxy_address", e.config.HTTPListenAddr).Logger()
log.Info().Msg("starting http proxy server on address")
err := e.httpServer.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
return
}
if err != nil {
e.log.Err(err).Msg("failed to start the http proxy server")
}
}