-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathengine.go
378 lines (322 loc) · 12.2 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
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
/*
* MIT License
*
* Copyright (c) 2022-2025 Arsene Tochemey Gandote
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package ego
import (
"context"
"errors"
"fmt"
"os"
"sync"
"time"
"github.com/tochemey/goakt/v2/address"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/tochemey/goakt/v2/actors"
"github.com/tochemey/goakt/v2/discovery"
"github.com/tochemey/goakt/v2/log"
"github.com/tochemey/ego/v3/egopb"
"github.com/tochemey/ego/v3/eventstream"
"github.com/tochemey/ego/v3/offsetstore"
"github.com/tochemey/ego/v3/persistence"
"github.com/tochemey/ego/v3/projection"
)
var (
// ErrEngineNotStarted is returned when the eGo engine has not started
ErrEngineNotStarted = errors.New("eGo engine has not started")
// ErrUndefinedEntityID is returned when sending a command to an undefined entity
ErrUndefinedEntityID = errors.New("eGo entity id is not defined")
// ErrCommandReplyUnmarshalling is returned when unmarshalling command reply failed
ErrCommandReplyUnmarshalling = errors.New("failed to parse command reply")
// ErrDurableStateStoreRequired is returned when the eGo engine durable store is not set
ErrDurableStateStoreRequired = errors.New("durable state store is required")
)
// Engine represents the engine that empowers the various entities
type Engine struct {
name string // name is the application name
eventsStore persistence.EventsStore // eventsStore is the events store
stateStore persistence.StateStore // stateStore is the durable state store
enableCluster *atomic.Bool // enableCluster enable/disable cluster mode
actorSystem actors.ActorSystem // actorSystem is the underlying actor system
logger log.Logger // logger is the logging engine to use
discoveryProvider discovery.Provider // discoveryProvider is the discovery provider for clustering
partitionsCount uint64 // partitionsCount specifies the number of partitions
started atomic.Bool
hostName string
peersPort int
gossipPort int
remotingPort int
minimumPeersQuorum uint16
eventStream eventstream.Stream
mutex *sync.Mutex
remoting *actors.Remoting
}
// NewEngine creates an instance of Engine
func NewEngine(name string, eventsStore persistence.EventsStore, opts ...Option) *Engine {
e := &Engine{
name: name,
eventsStore: eventsStore,
enableCluster: atomic.NewBool(false),
logger: log.New(log.ErrorLevel, os.Stderr),
eventStream: eventstream.New(),
mutex: &sync.Mutex{},
remoting: actors.NewRemoting(),
}
for _, opt := range opts {
opt.Apply(e)
}
e.started.Store(false)
return e
}
// Start starts the ego engine
func (engine *Engine) Start(ctx context.Context) error {
opts := []actors.Option{
actors.WithLogger(engine.logger),
actors.WithPassivationDisabled(),
actors.WithActorInitMaxRetries(1),
}
if engine.enableCluster.Load() {
if engine.hostName == "" {
engine.hostName, _ = os.Hostname()
}
replicaCount := 1
if engine.minimumPeersQuorum > 1 {
replicaCount = 2
}
clusterConfig := actors.
NewClusterConfig().
WithDiscovery(engine.discoveryProvider).
WithDiscoveryPort(engine.gossipPort).
WithPeersPort(engine.peersPort).
WithMinimumPeersQuorum(uint32(engine.minimumPeersQuorum)).
WithReplicaCount(uint32(replicaCount)).
WithPartitionCount(engine.partitionsCount).
WithKinds(
new(eventSourcedActor),
new(durableStateActor),
)
opts = append(opts,
actors.WithCluster(clusterConfig),
actors.WithRemoting(engine.hostName, int32(engine.remotingPort)))
}
var err error
engine.actorSystem, err = actors.NewActorSystem(engine.name, opts...)
if err != nil {
return fmt.Errorf("failed to create the ego actor system: %w", err)
}
if err := engine.actorSystem.Start(ctx); err != nil {
return err
}
engine.started.Store(true)
return nil
}
// AddProjection add a projection to the running eGo engine and starts it
func (engine *Engine) AddProjection(ctx context.Context, name string, handler projection.Handler, offsetStore offsetstore.OffsetStore, opts ...projection.Option) error {
if !engine.Started() {
return ErrEngineNotStarted
}
actor := projection.New(name, handler, engine.eventsStore, offsetStore, opts...)
engine.mutex.Lock()
actorSystem := engine.actorSystem
engine.mutex.Unlock()
if _, err := actorSystem.Spawn(ctx, name, actor); err != nil {
return fmt.Errorf("failed to register the projection=(%s): %w", name, err)
}
return nil
}
// RemoveProjection stops and removes a given projection from the engine
func (engine *Engine) RemoveProjection(ctx context.Context, name string) error {
if !engine.Started() {
return ErrEngineNotStarted
}
engine.mutex.Lock()
actorSystem := engine.actorSystem
engine.mutex.Unlock()
return actorSystem.Kill(ctx, name)
}
// IsProjectionRunning returns true when the projection is active and running
// One needs to check the error to see whether this function does not return a false negative
func (engine *Engine) IsProjectionRunning(ctx context.Context, name string) (bool, error) {
if !engine.Started() {
return false, ErrEngineNotStarted
}
engine.mutex.Lock()
actorSystem := engine.actorSystem
engine.mutex.Unlock()
addr, pid, err := actorSystem.ActorOf(ctx, name)
if err != nil {
return false, fmt.Errorf("failed to get projection %s: %w", name, err)
}
if pid != nil {
return pid.IsRunning(), nil
}
return addr.Equals(address.NoSender()), nil
}
// Stop stops the ego engine
func (engine *Engine) Stop(ctx context.Context) error {
engine.started.Store(false)
engine.eventStream.Close()
return engine.actorSystem.Stop(ctx)
}
// Started returns true when the eGo engine has started
func (engine *Engine) Started() bool {
return engine.started.Load()
}
// Subscribe creates an events subscriber
func (engine *Engine) Subscribe() (eventstream.Subscriber, error) {
if !engine.Started() {
return nil, ErrEngineNotStarted
}
engine.mutex.Lock()
eventStream := engine.eventStream
engine.mutex.Unlock()
subscriber := eventStream.AddSubscriber()
for i := 0; i < int(engine.partitionsCount); i++ {
topic := fmt.Sprintf(eventsTopic, i)
engine.eventStream.Subscribe(subscriber, topic)
topic = fmt.Sprintf(statesTopic, i)
engine.eventStream.Subscribe(subscriber, topic)
}
return subscriber, nil
}
// Entity creates an event sourced entity.
// Entity persists its full state into an events store that tracks the history based upon events that occurred.
// An event sourced entity receives a (non-persistent) command which is first validated if it can be applied to the current state.
// Here validation can mean anything, from simple inspection of a command message’s fields up to a conversation with several external services, for instance.
// If validation succeeds, events are generated from the command, representing the outcome of the command.
// These events are then persisted and, after successful persistence, used to change the actor’s state.
// When the event sourced entity needs to be recovered, only the persisted events are replayed of which we know that they can be successfully applied.
// In other words, events cannot fail when being replayed to a persistent actor, in contrast to commands.
// When there are no events to persist the event sourced entity will return the current state of the entity.
// One can use the SendCommand to send a command a durable state entity.
func (engine *Engine) Entity(ctx context.Context, behavior EventSourcedBehavior) error {
if !engine.Started() {
return ErrEngineNotStarted
}
engine.mutex.Lock()
actorSystem := engine.actorSystem
eventsStore := engine.eventsStore
eventStream := engine.eventStream
engine.mutex.Unlock()
_, err := actorSystem.Spawn(ctx,
behavior.ID(),
newEventSourcedActor(behavior, eventsStore, eventStream))
if err != nil {
return err
}
return nil
}
// DurableStateEntity creates a durable state entity.
// A DurableStateEntity persists its full state into a durable store without any history of the state evolution.
// A durable state entity receives a (non-persistent) command which is first validated if it can be applied to the current state.
// Here validation can mean anything, from simple inspection of a command message’s fields up to a conversation with several external services, for instance.
// If validation succeeds, a new state is generated from the command, representing the outcome of the command.
// The new state is persisted and, after successful persistence, used to change the actor’s state.
// During a normal shutdown process, it will persist its current state to the durable store prior to shutting down.
// One can use the SendCommand to send a command a durable state entity.
func (engine *Engine) DurableStateEntity(ctx context.Context, behavior DurableStateBehavior) error {
if !engine.Started() {
return ErrEngineNotStarted
}
engine.mutex.Lock()
actorSystem := engine.actorSystem
durableStateStore := engine.stateStore
eventStream := engine.eventStream
engine.mutex.Unlock()
if durableStateStore == nil {
return ErrDurableStateStoreRequired
}
_, err := actorSystem.Spawn(ctx,
behavior.ID(),
newDurableStateActor(behavior, durableStateStore, eventStream))
if err != nil {
return err
}
return nil
}
// SendCommand sends command to a given entity ref.
// This will return:
// 1. the resulting state after the command has been handled and the emitted event/durable state persisted
// 2. nil when there is no resulting state or no event persisted
// 3. an error in case of error
func (engine *Engine) SendCommand(ctx context.Context, entityID string, cmd Command, timeout time.Duration) (resultingState State, revision uint64, err error) {
if !engine.Started() {
return nil, 0, ErrEngineNotStarted
}
// entityID is not defined
if entityID == "" {
return nil, 0, ErrUndefinedEntityID
}
engine.mutex.Lock()
actorSystem := engine.actorSystem
engine.mutex.Unlock()
// locate the given actor
addr, pid, err := actorSystem.ActorOf(ctx, entityID)
if err != nil {
return nil, 0, err
}
var reply proto.Message
switch {
case pid != nil:
reply, err = actors.Ask(ctx, pid, cmd, timeout)
case addr != nil:
res, err := engine.remoting.RemoteAsk(ctx, address.NoSender(), addr, cmd, timeout)
if err == nil {
// let us unmarshal the response
reply, err = res.UnmarshalNew()
}
}
if err != nil {
return nil, 0, err
}
// cast the reply as it supposes
commandReply, ok := reply.(*egopb.CommandReply)
if ok {
return parseCommandReply(commandReply)
}
return nil, 0, ErrCommandReplyUnmarshalling
}
// parseCommandReply parses the command reply
func parseCommandReply(reply *egopb.CommandReply) (State, uint64, error) {
var (
state State
err error
)
switch r := reply.GetReply().(type) {
case *egopb.CommandReply_StateReply:
msg, err := r.StateReply.GetState().UnmarshalNew()
if err != nil {
return state, 0, err
}
switch v := msg.(type) {
case State:
return v, r.StateReply.GetSequenceNumber(), nil
default:
return state, 0, fmt.Errorf("got %s", r.StateReply.GetState().GetTypeUrl())
}
case *egopb.CommandReply_ErrorReply:
err = errors.New(r.ErrorReply.GetMessage())
return state, 0, err
}
return state, 0, errors.New("no state received")
}