forked from Tochemey/ego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor.go
321 lines (278 loc) · 9.16 KB
/
actor.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
/*
* Copyright (c) 2022-2023 Tochemey
*
* 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"
"fmt"
"sync"
"time"
"github.com/pkg/errors"
"github.com/tochemey/ego/egopb"
"github.com/tochemey/ego/eventstore"
"github.com/tochemey/ego/eventstream"
"github.com/tochemey/ego/internal/telemetry"
"github.com/tochemey/goakt/actors"
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
)
var (
eventsTopic = "topic.events.%d"
)
// actor is an event sourced based actor
type actor[T State] struct {
EntityBehavior[T]
// specifies the events store
eventsStore eventstore.EventsStore
// specifies the current state
currentState T
eventsCounter *atomic.Uint64
lastCommandTime time.Time
mu sync.RWMutex
eventsStream eventstream.Stream
}
// enforce compilation error
var _ actors.Actor = &actor[State]{}
// newActor creates an instance of actor provided the eventSourcedHandler and the events store
func newActor[T State](behavior EntityBehavior[T], eventsStore eventstore.EventsStore, eventsStream eventstream.Stream) *actor[T] {
// create an instance of entity and return it
return &actor[T]{
eventsStore: eventsStore,
EntityBehavior: behavior,
eventsCounter: atomic.NewUint64(0),
mu: sync.RWMutex{},
eventsStream: eventsStream,
}
}
// PreStart pre-starts the actor
// At this stage we connect to the various stores
func (entity *actor[T]) PreStart(ctx context.Context) error {
// add a span context
spanCtx, span := telemetry.SpanContext(ctx, "PreStart")
defer span.End()
// acquire the lock
entity.mu.Lock()
// release lock when done
defer entity.mu.Unlock()
// connect to the various stores
if entity.eventsStore == nil {
return errors.New("events store is not defined")
}
// call the connect method of the journal store
if err := entity.eventsStore.Ping(spanCtx); err != nil {
return fmt.Errorf("failed to connect to the events store: %v", err)
}
// check whether there is a snapshot to recover from
if err := entity.recoverFromSnapshot(spanCtx); err != nil {
return errors.Wrap(err, "failed to recover from snapshot")
}
return nil
}
// Receive processes any message dropped into the actor mailbox.
func (entity *actor[T]) Receive(ctx actors.ReceiveContext) {
// add a span context
_, span := telemetry.SpanContext(ctx.Context(), "Receive")
defer span.End()
// acquire the lock
entity.mu.Lock()
// release lock when done
defer entity.mu.Unlock()
// grab the command sent
switch command := ctx.Message().(type) {
case *egopb.GetStateCommand:
entity.getStateAndReply(ctx)
default:
entity.processCommandAndReply(ctx, command)
}
}
// PostStop prepares the actor to gracefully shutdown
func (entity *actor[T]) PostStop(ctx context.Context) error {
// add a span context
_, span := telemetry.SpanContext(ctx, "PostStop")
defer span.End()
// acquire the lock
entity.mu.Lock()
// release lock when done
defer entity.mu.Unlock()
return nil
}
// recoverFromSnapshot reset the persistent actor to the latest snapshot in case there is one
// this is vital when the entity actor is restarting.
func (entity *actor[T]) recoverFromSnapshot(ctx context.Context) error {
// add a span context
spanCtx, span := telemetry.SpanContext(ctx, "RecoverFromSnapshot")
defer span.End()
// check whether there is a snapshot to recover from
event, err := entity.eventsStore.GetLatestEvent(spanCtx, entity.ID())
// handle the error
if err != nil {
return errors.Wrap(err, "failed to recover the latest journal")
}
// we do have the latest state just recover from it
if event != nil {
// set the current state
currentState := entity.InitialState()
if err := event.GetResultingState().UnmarshalTo(currentState); err != nil {
return errors.Wrap(err, "failed unmarshal the latest state")
}
entity.currentState = currentState
// set the event counter
entity.eventsCounter.Store(event.GetSequenceNumber())
return nil
}
// in case there is no snapshot
entity.currentState = entity.InitialState()
return nil
}
// sendErrorReply sends an error as a reply message
func (entity *actor[T]) sendErrorReply(ctx actors.ReceiveContext, err error) {
// create a new error reply
reply := &egopb.CommandReply{
Reply: &egopb.CommandReply_ErrorReply{
ErrorReply: &egopb.ErrorReply{
Message: err.Error(),
},
},
}
// send the response
ctx.Response(reply)
}
// getStateAndReply returns the current state of the entity
func (entity *actor[T]) getStateAndReply(ctx actors.ReceiveContext) {
// let us fetch the latest journal
latestEvent, err := entity.eventsStore.GetLatestEvent(ctx.Context(), entity.ID())
// handle the error
if err != nil {
entity.sendErrorReply(ctx, err)
return
}
// reply with the state unmarshalled
resultingState := latestEvent.GetResultingState()
reply := &egopb.CommandReply{
Reply: &egopb.CommandReply_StateReply{
StateReply: &egopb.StateReply{
PersistenceId: entity.ID(),
State: resultingState,
SequenceNumber: latestEvent.GetSequenceNumber(),
Timestamp: latestEvent.GetTimestamp(),
},
},
}
// send the response
ctx.Response(reply)
}
// processCommandAndReply processes the incoming command
func (entity *actor[T]) processCommandAndReply(ctx actors.ReceiveContext, command Command) {
// set the go context
goCtx := ctx.Context()
// pass the received command to the command handler
event, err := entity.HandleCommand(goCtx, command, entity.currentState)
// handle the command handler error
if err != nil {
// send an error reply
entity.sendErrorReply(ctx, err)
return
}
// if the event is nil nothing is persisted, and we return no reply
if event == nil {
// get the current state and marshal it
resultingState, _ := anypb.New(entity.currentState)
// create the command reply to send out
reply := &egopb.CommandReply{
Reply: &egopb.CommandReply_StateReply{
StateReply: &egopb.StateReply{
PersistenceId: entity.ID(),
State: resultingState,
SequenceNumber: entity.eventsCounter.Load(),
Timestamp: entity.lastCommandTime.Unix(),
},
},
}
// send the response
ctx.Response(reply)
return
}
// process the event by calling the event handler
resultingState, err := entity.HandleEvent(goCtx, event, entity.currentState)
// handle the event handler error
if err != nil {
// send an error reply
entity.sendErrorReply(ctx, err)
return
}
// increment the event counter
entity.eventsCounter.Inc()
// set the current state for the next command
entity.currentState = resultingState
// marshal the event and the resulting state
marshaledEvent, _ := anypb.New(event)
marshaledState, _ := anypb.New(resultingState)
sequenceNumber := entity.eventsCounter.Load()
timestamp := timestamppb.Now()
entity.lastCommandTime = timestamp.AsTime()
shardNumber := ctx.Self().ActorSystem().GetPartition(entity.ID())
// create the event
envelope := &egopb.Event{
PersistenceId: entity.ID(),
SequenceNumber: sequenceNumber,
IsDeleted: false,
Event: marshaledEvent,
ResultingState: marshaledState,
Timestamp: entity.lastCommandTime.Unix(),
Shard: shardNumber,
}
// create a journal list
journals := []*egopb.Event{envelope}
// define the topic for the given shard
topic := fmt.Sprintf(eventsTopic, shardNumber)
// publish to the event stream and persist the event to the events store
eg, goCtx := errgroup.WithContext(goCtx)
// publish the message to the topic
eg.Go(func() error {
entity.eventsStream.Publish(topic, envelope)
return nil
})
// persist the event to the events store
eg.Go(func() error {
return entity.eventsStore.WriteEvents(goCtx, journals)
})
// handle the persistence error
if err := eg.Wait(); err != nil {
// send an error reply
entity.sendErrorReply(ctx, err)
return
}
// create the command reply to send
reply := &egopb.CommandReply{
Reply: &egopb.CommandReply_StateReply{
StateReply: &egopb.StateReply{
PersistenceId: entity.ID(),
State: marshaledState,
SequenceNumber: sequenceNumber,
Timestamp: entity.lastCommandTime.Unix(),
},
},
}
// send the response
ctx.Response(reply)
}