This repository was archived by the owner on Sep 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcluster.go
More file actions
338 lines (283 loc) · 10.3 KB
/
cluster.go
File metadata and controls
338 lines (283 loc) · 10.3 KB
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
/*
* Copyright 2025 Hypermode Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2025 Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package actors
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/hypermodeinc/modus/runtime/app"
"github.com/hypermodeinc/modus/runtime/logger"
"github.com/hypermodeinc/modus/runtime/utils"
goakt "github.com/tochemey/goakt/v3/actor"
"github.com/tochemey/goakt/v3/discovery"
"github.com/tochemey/goakt/v3/discovery/kubernetes"
"github.com/tochemey/goakt/v3/discovery/nats"
"github.com/tochemey/goakt/v3/remote"
"github.com/travisjeffery/go-dynaport"
)
func clusterOptions(ctx context.Context) []goakt.Option {
span, ctx := utils.NewSentrySpanForCurrentFunc(ctx)
defer span.Finish()
clusterMode := clusterMode()
if clusterMode == clusterModeNone {
if !app.IsDevEnvironment() {
logger.Warnf("Cluster mode is disabled, which is not recommended for production environments. Set MODUS_CLUSTER_MODE to enable clustering.")
}
return nil
}
discoveryPort, remotingPort, peersPort := clusterPorts()
logger.Info(ctx).
Str("cluster_mode", clusterMode.String()).
Int("discovery_port", discoveryPort).
Int("remoting_port", remotingPort).
Int("peers_port", peersPort).
Msg("Clustering enabled.")
disco, err := newDiscoveryProvider(ctx, clusterMode, discoveryPort)
if err != nil {
logger.Fatal(ctx).Err(err).Msg("Failed to create cluster discovery provider.")
}
return []goakt.Option{
goakt.WithRemote(remote.NewConfig(remotingHost(), remotingPort)),
goakt.WithCluster(goakt.NewClusterConfig().
WithDiscovery(disco).
WithDiscoveryPort(discoveryPort).
WithPeersPort(peersPort).
WithReadTimeout(readTimeout()).
WithWriteTimeout(writeTimeout()).
WithPartitionCount(partitionCount()).
WithClusterStateSyncInterval(nodesSyncInterval()).
WithPeersStateSyncInterval(peerSyncInterval()).
WithKinds(&wasmAgentActor{}, &subscriptionActor{}),
),
}
}
type goaktClusterMode int
const (
clusterModeNone goaktClusterMode = iota
clusterModeNats
clusterModeKubernetes
)
func (c goaktClusterMode) String() string {
switch c {
case clusterModeNone:
return "none"
case clusterModeNats:
return "NATS"
case clusterModeKubernetes:
return "Kubernetes"
default:
return "unknown"
}
}
func parseClusterMode(mode string) goaktClusterMode {
switch strings.ToLower(mode) {
case "none", "":
return clusterModeNone
case "nats":
return clusterModeNats
case "kubernetes", "k8s":
return clusterModeKubernetes
default:
logger.Warnf("Unknown cluster mode: '%s'. Defaulting to 'none'.", mode)
return clusterModeNone
}
}
func clusterMode() goaktClusterMode {
return parseClusterMode(os.Getenv("MODUS_CLUSTER_MODE"))
}
func clusterEnabled() bool {
return clusterMode() != clusterModeNone
}
func clusterNatsUrl() string {
const envVar = "MODUS_CLUSTER_NATS_URL"
const defaultNatsUrl = "nats://localhost:4222"
urlStr := os.Getenv(envVar)
if urlStr == "" {
logger.Warnf("%s not set. Using default: %s", envVar, defaultNatsUrl)
return defaultNatsUrl
}
if _, err := url.Parse(urlStr); err != nil {
logger.Warnf("Invalid URL for %s. Using default: %s", envVar, defaultNatsUrl)
return defaultNatsUrl
}
return urlStr
}
func clusterHost() string {
const envVar = "MODUS_CLUSTER_HOST"
if host := os.Getenv(envVar); host != "" {
if _, err := url.Parse("http://" + host); err != nil {
logger.Fatalf("Invalid value for %s: %s.", envVar, host)
}
return host
}
if app.IsDevEnvironment() {
// Note, forcing IPv4 here avoids memberlist attempting to bind to IPv6 that we're not listening on.
return "127.0.0.1"
} else {
// this hack gets the same IP that the remoting system would bind to by default
rc := remote.NewConfig("0.0.0.0", 0)
_ = rc.Sanitize()
return rc.BindAddr()
}
}
// remotingHost returns the host address to bind the remoting system to.
func remotingHost() string {
// only bind to localhost in development
if app.IsDevEnvironment() {
return "127.0.0.1"
}
// otherwise bind to all interfaces
return "0.0.0.0"
}
// clusterPorts returns the ports used for discovery, remoting, and peer communication in the cluster.
func clusterPorts() (discoveryPort, remotingPort, peersPort int) {
// Get default ports dynamically, but use environment variables if set
ports := dynaport.Get(3)
discoveryPort = getIntFromEnv("MODUS_CLUSTER_DISCOVERY_PORT", ports[0])
remotingPort = getIntFromEnv("MODUS_CLUSTER_REMOTING_PORT", ports[1])
peersPort = getIntFromEnv("MODUS_CLUSTER_PEERS_PORT", ports[2])
return
}
// peerSyncInterval returns the interval at which the actor system will sync its list of actors to other nodes across the cluster.
// We use a tight sync interval of 1 second by default, to ensure quick peer discovery as agents are added or removed.
//
// This value is also used for a sleep both on system startup and when spawning a new agent actor,
// so it needs to be low enough to not be noticed by the user.
func peerSyncInterval() time.Duration {
return getDurationFromEnv("MODUS_CLUSTER_PEER_SYNC_SECONDS", 1, time.Second)
}
// nodesSyncInterval returns the interval at which the cluster forces a resync of the list of active nodes across the cluster.
// This matters only with regard to nodes going down unexpectedly, as other nodes in the cluster will not be aware of the change until the next sync.
// It does not affect anything if a node is gracefully shut down, as that will be communicated immediately during the shutdown process.
//
// On each interval, the node will sync its list of nodes with the cluster, and update its local state accordingly.
// The default is 10 seconds, which is a reasonable balance between responsiveness and network overhead.
func nodesSyncInterval() time.Duration {
return getDurationFromEnv("MODUS_CLUSTER_NODES_SYNC_SECONDS", 10, time.Second)
}
// partitionCount returns the number of partitions the cluster will use for actor distribution.
// It must be a prime number to work properly with the actor system's hashing algorithm.
// It must be greater than the number of nodes in the cluster, but not too large to avoid excessive overhead.
// In testing, 23 is the highest that works well with the other default timing constraints.
// We'll use a slightly lower default of 13, which is still a prime number and should work well for most clusters.
// The GoAkt default is 271, but this has been found to lead to other errors in practice.
func partitionCount() uint64 {
return uint64(getIntFromEnv("MODUS_CLUSTER_PARTITION_COUNT", 13))
}
// readTimeout returns the duration to wait for a cluster read operation before timing out.
// The default is 1 second, which should usually not need to be changed.
func readTimeout() time.Duration {
return getDurationFromEnv("MODUS_CLUSTER_READ_TIMEOUT_SECONDS", 1, time.Second)
}
// writeTimeout returns the duration to wait for a cluster write operation before timing out.
// The default is 1 second, which should usually not need to be changed.
func writeTimeout() time.Duration {
return getDurationFromEnv("MODUS_CLUSTER_WRITE_TIMEOUT_SECONDS", 1, time.Second)
}
func getPodLabels() map[string]string {
// example value: "app.kubernetes.io/name=modus,app.kubernetes.io/component=runtime"
if labels := os.Getenv("MODUS_CLUSTER_POD_LABELS"); labels != "" {
podLabels := make(map[string]string)
for label := range strings.SplitSeq(labels, ",") {
parts := strings.SplitN(label, "=", 2)
if len(parts) == 2 {
podLabels[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
} else {
logger.Warnf("Invalid pod label format: '%s'. Expected 'key=value'.", label)
}
}
return podLabels
}
// defaults
return map[string]string{
"app.kubernetes.io/name": "modus",
"app.kubernetes.io/component": "runtime",
}
}
func newDiscoveryProvider(ctx context.Context, clusterMode goaktClusterMode, discoveryPort int) (discovery.Provider, error) {
span, ctx := utils.NewSentrySpanForCurrentFunc(ctx)
defer span.Finish()
switch clusterMode {
case clusterModeNats:
natsUrl := clusterNatsUrl()
clusterHost := clusterHost()
logger.Info(ctx).
Str("cluster_host", clusterHost).
Str("nats_server", natsUrl).
Msg("Using NATS for node discovery.")
disco := nats.NewDiscovery(&nats.Config{
NatsSubject: "modus-gossip",
NatsServer: natsUrl,
Host: clusterHost,
DiscoveryPort: discoveryPort,
})
return wrapProvider(ctx, disco), nil
case clusterModeKubernetes:
namespace, ok := app.KubernetesNamespace()
if !ok {
return nil, errors.New("Kubernetes cluster mode enabled, but namespace was not found")
}
logger.Info(ctx).
Str("namespace", namespace).
Msg("Using Kubernetes for node discovery.")
disco := kubernetes.NewDiscovery(&kubernetes.Config{
Namespace: namespace,
PodLabels: getPodLabels(),
DiscoveryPortName: "discovery-port",
RemotingPortName: "remoting-port",
PeersPortName: "peers-port",
})
return wrapProvider(ctx, disco), nil
}
return nil, fmt.Errorf("unsupported cluster mode: %s", clusterMode)
}
// wrapProvider wraps a discovery provider to add Sentry tracing to its methods.
func wrapProvider(ctx context.Context, provider discovery.Provider) discovery.Provider {
if provider == nil {
return nil
}
return &providerWrapper{ctx, provider}
}
// providerWrapper is a wrapper around a discovery provider that adds Sentry tracing to its methods.
type providerWrapper struct {
ctx context.Context
provider discovery.Provider
}
func (w *providerWrapper) Close() error {
span, _ := utils.NewSentrySpanForCurrentFunc(w.ctx)
defer span.Finish()
return w.provider.Close()
}
func (w *providerWrapper) Deregister() error {
span, _ := utils.NewSentrySpanForCurrentFunc(w.ctx)
defer span.Finish()
return w.provider.Deregister()
}
func (w *providerWrapper) DiscoverPeers() ([]string, error) {
span, _ := utils.NewSentrySpanForCurrentFunc(w.ctx)
defer span.Finish()
return w.provider.DiscoverPeers()
}
func (w *providerWrapper) ID() string {
return w.provider.ID()
}
func (w *providerWrapper) Initialize() error {
span, _ := utils.NewSentrySpanForCurrentFunc(w.ctx)
defer span.Finish()
return w.provider.Initialize()
}
func (w *providerWrapper) Register() error {
span, _ := utils.NewSentrySpanForCurrentFunc(w.ctx)
defer span.Finish()
return w.provider.Register()
}