-
Notifications
You must be signed in to change notification settings - Fork 89
/
nats.go
294 lines (254 loc) · 9.45 KB
/
nats.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
package transport
import (
"context"
"errors"
"fmt"
"strings"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/rs/zerolog/log"
"github.com/bacalhau-project/bacalhau/pkg/compute"
"github.com/bacalhau-project/bacalhau/pkg/lib/validate"
"github.com/bacalhau-project/bacalhau/pkg/model"
"github.com/bacalhau-project/bacalhau/pkg/models"
nats_helper "github.com/bacalhau-project/bacalhau/pkg/nats"
"github.com/bacalhau-project/bacalhau/pkg/nats/proxy"
nats_pubsub "github.com/bacalhau-project/bacalhau/pkg/nats/pubsub"
"github.com/bacalhau-project/bacalhau/pkg/pubsub"
"github.com/bacalhau-project/bacalhau/pkg/routing"
core_transport "github.com/bacalhau-project/bacalhau/pkg/transport"
)
const NodeInfoSubjectPrefix = "node.info."
// reservedChars are the characters that are not allowed in node IDs as nodes
// subscribe to subjects with their node IDs, and these are wildcards
// in NATS subjects that could cause a node to subscribe to unintended subjects.
const reservedChars = ".*>"
type NATSTransportConfig struct {
NodeID string
Port int
AdvertisedAddress string
Orchestrators []string
IsRequesterNode bool
// StoreDir is the directory where the NATS server will store its data
StoreDir string
// AuthSecret is a secret string that clients must use to connect. NATS servers
// must supply this config, while clients can also supply it as the user part
// of their Orchestrator URL.
AuthSecret string
// Cluster config for requester nodes to connect with each other
ClusterName string
ClusterPort int
ClusterAdvertisedAddress string
ClusterPeers []string
}
func (c *NATSTransportConfig) Validate() error {
var mErr error
if validate.IsBlank(c.NodeID) {
mErr = errors.Join(mErr, errors.New("missing node ID"))
} else if validate.ContainsSpaces(c.NodeID) {
mErr = errors.Join(mErr, errors.New("node ID contains a space"))
} else if validate.ContainsNull(c.NodeID) {
mErr = errors.Join(mErr, errors.New("node ID contains a null character"))
} else if strings.ContainsAny(c.NodeID, reservedChars) {
mErr = errors.Join(mErr, fmt.Errorf("node ID '%s' contains one or more reserved characters: %s", c.NodeID, reservedChars))
}
if c.IsRequesterNode {
mErr = errors.Join(mErr, validate.IsGreaterThanZero(c.Port, "port %d must be greater than zero", c.Port))
// if cluster config is set, validate it
if c.ClusterName != "" || c.ClusterPort != 0 || c.ClusterAdvertisedAddress != "" || len(c.ClusterPeers) > 0 {
mErr = errors.Join(mErr,
validate.IsGreaterThanZero(c.ClusterPort, "cluster port %d must be greater than zero", c.ClusterPort))
}
} else {
if validate.IsEmpty(c.Orchestrators) {
mErr = errors.Join(mErr, errors.New("missing orchestrators"))
}
}
return mErr
}
type NATSTransport struct {
Config *NATSTransportConfig
nodeID string
natsServer *nats_helper.ServerManager
natsClient *nats_helper.ClientManager
computeProxy compute.Endpoint
callbackProxy compute.Callback
nodeInfoPubSub pubsub.PubSub[models.NodeState]
nodeInfoDecorator models.NodeInfoDecorator
managementProxy compute.ManagementEndpoint
}
//nolint:funlen
func NewNATSTransport(ctx context.Context,
config *NATSTransportConfig) (*NATSTransport, error) {
log.Debug().Msgf("Creating NATS transport with config: %+v", config)
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("error validating nats transport config. %w", err)
}
var sm *nats_helper.ServerManager
if config.IsRequesterNode {
var err error
// create nats server with servers acting as its cluster peers
serverOpts := &server.Options{
ServerName: config.NodeID,
Port: config.Port,
ClientAdvertise: config.AdvertisedAddress,
Authorization: config.AuthSecret,
Debug: true, // will only be used if log level is debug
JetStream: true,
DisableJetStreamBanner: true,
StoreDir: config.StoreDir,
}
// Only set cluster options if cluster peers are provided. Jetstream doesn't
// like the setting to be present with no values, or with values that are
// a local address (e.g. it can't RAFT to itself).
routes, err := nats_helper.RoutesFromSlice(config.ClusterPeers, false)
if err != nil {
return nil, err
}
if len(config.ClusterPeers) > 0 {
serverOpts.Routes = routes
serverOpts.Cluster = server.ClusterOpts{
Name: config.ClusterName,
Port: config.ClusterPort,
Advertise: config.ClusterAdvertisedAddress,
}
}
log.Debug().Msgf("Creating NATS server with options: %+v", serverOpts)
sm, err = nats_helper.NewServerManager(ctx, nats_helper.ServerManagerParams{
Options: serverOpts,
})
if err != nil {
return nil, err
}
config.Orchestrators = append(config.Orchestrators, sm.Server.ClientURL())
}
nc, err := CreateClient(ctx, config)
if err != nil {
return nil, err
}
// PubSub to publish and consume node info messages
nodeInfoPubSub, err := nats_pubsub.NewPubSub[models.NodeState](nats_pubsub.PubSubParams{
Conn: nc.Client,
Subject: NodeInfoSubjectPrefix + config.NodeID,
SubscriptionSubject: NodeInfoSubjectPrefix + "*",
})
if err != nil {
return nil, err
}
// compute proxy
computeProxy, err := proxy.NewComputeProxy(proxy.ComputeProxyParams{
Conn: nc.Client,
})
if err != nil {
return nil, err
}
// Callback to send compute events (i.e. requester endpoint)
computeCallback := proxy.NewCallbackProxy(proxy.CallbackProxyParams{
Conn: nc.Client,
})
// A proxy to register and unregister compute nodes with the requester
managementProxy := proxy.NewManagementProxy(proxy.ManagementProxyParams{
Conn: nc.Client,
})
return &NATSTransport{
nodeID: config.NodeID,
natsServer: sm,
natsClient: nc,
Config: config,
computeProxy: computeProxy,
callbackProxy: computeCallback,
nodeInfoPubSub: nodeInfoPubSub,
nodeInfoDecorator: models.NoopNodeInfoDecorator{},
managementProxy: managementProxy,
}, nil
}
func CreateClient(ctx context.Context, config *NATSTransportConfig) (*nats_helper.ClientManager, error) {
// create nats client
log.Debug().Msgf("Creating NATS client with servers: %s", strings.Join(config.Orchestrators, ","))
clientOptions := []nats.Option{
nats.Name(config.NodeID),
}
if config.AuthSecret != "" {
clientOptions = append(clientOptions, nats.Token(config.AuthSecret))
}
return nats_helper.NewClientManager(ctx,
strings.Join(config.Orchestrators, ","),
clientOptions...,
)
}
func (t *NATSTransport) RegisterNodeInfoConsumer(ctx context.Context, infostore routing.NodeInfoStore) error {
// subscribe to nodeInfo subject and add nodeInfo to nodeInfoStore
nodeInfoSubscriber := pubsub.NewChainedSubscriber[models.NodeState](true)
nodeInfoSubscriber.Add(pubsub.SubscriberFunc[models.NodeState](infostore.Add))
return t.nodeInfoPubSub.Subscribe(ctx, nodeInfoSubscriber)
}
// RegisterComputeCallback registers a compute callback with the transport layer.
func (t *NATSTransport) RegisterComputeCallback(callback compute.Callback) error {
_, err := proxy.NewCallbackHandler(proxy.CallbackHandlerParams{
Name: t.nodeID,
Conn: t.natsClient.Client,
Callback: callback,
})
return err
}
// RegisterComputeEndpoint registers a compute endpoint with the transport layer.
func (t *NATSTransport) RegisterComputeEndpoint(endpoint compute.Endpoint) error {
_, err := proxy.NewComputeHandler(proxy.ComputeHandlerParams{
Name: t.nodeID,
Conn: t.natsClient.Client,
ComputeEndpoint: endpoint,
})
return err
}
// RegisterManagementEndpoint registers a requester endpoint with the transport layer
func (t *NATSTransport) RegisterManagementEndpoint(endpoint compute.ManagementEndpoint) error {
_, err := proxy.NewManagementHandler(proxy.ManagementHandlerParams{
Conn: t.natsClient.Client,
ManagementEndpoint: endpoint,
})
return err
}
// ComputeProxy returns the compute proxy.
func (t *NATSTransport) ComputeProxy() compute.Endpoint {
return t.computeProxy
}
// CallbackProxy returns the callback proxy.
func (t *NATSTransport) CallbackProxy() compute.Callback {
return t.callbackProxy
}
// RegistrationProxy returns the previoously created registration proxy.
func (t *NATSTransport) ManagementProxy() compute.ManagementEndpoint {
return t.managementProxy
}
// NodeInfoPubSub returns the node info pubsub.
func (t *NATSTransport) NodeInfoPubSub() pubsub.PubSub[models.NodeState] {
return t.nodeInfoPubSub
}
// NodeInfoDecorator returns the node info decorator.
func (t *NATSTransport) NodeInfoDecorator() models.NodeInfoDecorator {
return t.nodeInfoDecorator
}
// DebugInfoProviders returns the debug info of the NATS transport layer
func (t *NATSTransport) DebugInfoProviders() []model.DebugInfoProvider {
var debugInfoProviders []model.DebugInfoProvider
if t.natsServer != nil {
debugInfoProviders = append(debugInfoProviders, t.natsServer)
}
if t.natsClient != nil {
debugInfoProviders = append(debugInfoProviders, t.natsClient)
}
return debugInfoProviders
}
// Close closes the transport layer.
func (t *NATSTransport) Close(ctx context.Context) error {
if t.natsServer != nil {
log.Ctx(ctx).Debug().Msgf("Shutting down server %s", t.natsServer.Server.Name())
t.natsServer.Stop()
}
if t.natsClient != nil {
t.natsClient.Stop()
}
return nil
}
// compile-time interface check
var _ core_transport.TransportLayer = (*NATSTransport)(nil)