-
Notifications
You must be signed in to change notification settings - Fork 1
/
repository.go
356 lines (281 loc) · 11 KB
/
repository.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
package eventsource
import (
"context"
"crypto/rand"
"fmt"
"sync"
"time"
"github.com/oklog/ulid"
"github.com/pkg/errors"
)
var (
// ErrDeleted is returned by Aggregate.On() method to signal that the object has been deleted
ErrDeleted = errors.New("not found (was deleted)")
// ErrNoHistory is returned by Repository.Load() when no history exist for the given aggregate ID
ErrNoHistory = errors.New("no history found")
// ErrNotificationFailed is returned by Commit() if notification service fails
ErrNotificationFailed = errors.New("Failed to send notification")
)
// QueryOption is used for setting store specific options like limit or sorting
// Can be found in any of the stores
type QueryOption func(opt interface{})
// Store is the interface implemented by the data stores that can be used as back end for
// the event source.
type Store interface {
NewTransaction(ctx context.Context, records ...Record) (StoreTransaction, error)
LoadByAggregate(ctx context.Context, aggregateID string, opts ...QueryOption) ([]Record, error)
Load(ctx context.Context, opts ...QueryOption) ([]Record, error)
// Deprecated: Use Load(ctx, store.BySequenceID(...))
LoadBySequenceID(ctx context.Context, sequenceID string, opts ...QueryOption) (record []Record, err error)
// Deprecated: Use Load(ctx, store.BySequenceID(...), store.ByType(...))
LoadBySequenceIDAndType(ctx context.Context, sequenceID string, eventType string, opts ...QueryOption) (records []Record, err error)
// Deprecated: Use Load(ctx, store.ByTimestamp(...))
LoadByTimestamp(ctx context.Context, timestamp int64, opts ...QueryOption) (record []Record, err error)
}
// StoreTransaction encapsulates a write operation to a Store, allowing the caller
// to roll back the operation.
type StoreTransaction interface {
Commit() error
Rollback() error
GetRecords() []Record
}
// Aggregate is an interface representing an object whose state changes can be
// recorded to and replayed from an event source.
type Aggregate interface {
On(ctx context.Context, event Event) error
SetAggregateID(id string)
}
// Serializer is an interface that should be implemented if events need to be saved
// using a new storage format.
type Serializer interface {
Unmarshal(data []byte, eventType string) (event Event, err error)
Marshal(event Event) (data []byte, err error)
}
// NotificationService represents a service which can emit notifications
// when records are saved to the event source
type NotificationService interface {
Send(record Record) error // deprecated use SendWithContext instead
SendWithContext(ctx context.Context, record Record) error
}
// Repository is an interface representing the actual event source.
type Repository interface {
// Return store
Store() Store
// Save one or more events to the repository
Save(ctx context.Context, events ...Event) error
// Save one or more events to the repository, within a transaction
SaveTransaction(ctx context.Context, events ...Event) (StoreTransaction, error)
// Load events from repository for the given aggregate ID. For each event e,
// call aggr.On(e) to update the state of aggr. When done, aggr has been
// "fast forwarded" to the current state.
Load(ctx context.Context, id string, aggr Aggregate) (deleted bool, err error)
// Get all events with query options (definied in the store)
// Query options can be used for filter by sequence ID (see https://github.com/oklog/ulid)
// or options like limit, offset
LoadEvents(ctx context.Context, opts ...QueryOption) (events []Event, err error)
// Deprecated: Use LoadEvents(ctx, store.BySequenceId(...))
// Get all events with sequence ID newer than the given ID (see https://github.com/oklog/ulid)
// Return at most limit records. If limit is 0, don't limit the number of records returned.
GetEventsBySequenceID(ctx context.Context, sequenceID string, opts ...QueryOption) (events []Event, err error)
// Deprecated: Use LoadEvents(ctx, store.BySequenceId(...), store.ByType(...))
// Same as GetEventsBySequenceID, but only returns events of the same type
// as the one provided in the eventType parameter.
GetEventsBySequenceIDAndType(ctx context.Context, sequenceID string, eventType Event, opts ...QueryOption) (events []Event, err error)
// Deprecated: Use LoadEvents(ctx, store.ByTimestamp(...))
// Get all events newer than the given timestamp
// Return at most limit records. If limit is 0, don't limit the number of records returned.
GetEventsByTimestamp(ctx context.Context, timestamp int64, opts ...QueryOption) (events []Event, err error)
// Add notification service
AddNotificationService(service NotificationService)
// Unmarshal records to events using repository
UnmarshalRecords(records []Record) (events []Event, err error)
}
// NewRepository returns a new repository
func NewRepository(store Store, serializer Serializer) Repository {
return &repository{
store: store,
serializer: serializer,
notificationServices: []NotificationService{},
}
}
func (repo *repository) AddNotificationService(service NotificationService) {
repo.notificationServices = append(repo.notificationServices, service)
}
// Record is a store row. The Data field contains the marshalled Event, and
// Type is the type of event retrieved by reflect.TypeOf(event).
type Record struct {
AggregateID string `json:"aggregateId"`
SequenceID string `json:"sequenceId"`
Timestamp int64 `json:"timestamp"`
Type string `json:"type"`
Data []byte `json:"data"`
UserID string `json:"userId"`
}
type repository struct {
store Store
serializer Serializer
notificationServices []NotificationService
}
type transactionWrapper struct {
ctx context.Context
transaction StoreTransaction
notificationServices []NotificationService
}
func newTransactionWrapper(ctx context.Context, store Store, records []Record, ns []NotificationService) (StoreTransaction, error) {
transaction, err := store.NewTransaction(ctx, records...)
if err != nil {
return nil, err
}
return &transactionWrapper{ctx, transaction, ns}, nil
}
// Commit transaction to underlying store and, if configured, publish the records to a
// notification service. If ErrNotificationFailed is returned, the data has been successfully
// committed to the store, but the notification service failed.
func (transWrap *transactionWrapper) Commit() error {
err := transWrap.transaction.Commit()
if err != nil {
return err
}
for _, service := range transWrap.notificationServices {
for _, r := range transWrap.transaction.GetRecords() {
if err = service.SendWithContext(transWrap.ctx, r); err != nil {
return fmt.Errorf("%w: %s", ErrNotificationFailed, err)
}
}
}
return nil
}
func (transWrap *transactionWrapper) Rollback() error {
return transWrap.transaction.Rollback()
}
func (transWrap *transactionWrapper) GetRecords() []Record {
return transWrap.transaction.GetRecords()
}
// See https://godoc.org/github.com/oklog/ulid#example-ULID
var (
entropy = ulid.Monotonic(rand.Reader, 0)
entropyMutex sync.Mutex
)
// NewULID returns a Universally Unique Lexicographically Sortable Identifier
func NewULID() string {
entropyMutex.Lock()
defer entropyMutex.Unlock()
return ulid.MustNew(ulid.Now(), entropy).String()
}
// Return store
func (repo *repository) Store() Store {
return repo.store
}
// Save persists the event to the repo
func (repo *repository) Save(ctx context.Context, events ...Event) error {
tx, err := repo.SaveTransaction(ctx, events...)
if err != nil {
return err
}
if err := tx.Commit(); err != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return errors.Wrapf(err, "rollback error: %+v", rollbackErr)
}
return errors.Wrap(err, "failed to commit transaction")
}
return nil
}
func (repo *repository) SaveTransaction(ctx context.Context, events ...Event) (StoreTransaction, error) {
records := []Record{}
for _, event := range events {
event.SetSequenceID(NewULID())
if event.GetTimestamp() == 0 {
event.SetTimestamp(time.Now().UnixNano())
}
data, err := repo.serializer.Marshal(event)
if err != nil {
return nil, err
}
records = append(records, Record{
AggregateID: event.GetAggregateID(),
SequenceID: event.GetSequenceID(),
Timestamp: event.GetTimestamp(),
Type: GetTypeName(event),
Data: data,
UserID: event.GetUserID(),
})
}
return newTransactionWrapper(ctx, repo.store, records, repo.notificationServices)
}
// Load rehydrates the repo
func (repo repository) Load(ctx context.Context, aggregateID string, aggr Aggregate) (deleted bool, err error) {
history, err := repo.store.LoadByAggregate(ctx, aggregateID)
if err != nil {
return false, err
}
if len(history) == 0 {
return false, ErrNoHistory
}
aggr.SetAggregateID(aggregateID)
for _, record := range history {
var event Event
event, err = repo.serializer.Unmarshal(record.Data, record.Type)
if err != nil {
return false, err
}
// Some older events created with earlier releases did not have timestamp in
// record.Data so in those cases we pick up timestamp from event
if event.GetTimestamp() == int64(0) {
event.SetTimestamp(record.Timestamp)
}
err = aggr.On(ctx, event)
if errors.Is(err, ErrDeleted) {
return true, nil
}
if err != nil {
return false, err
}
}
return false, nil
}
func (repo repository) UnmarshalRecords(records []Record) ([]Event, error) {
return unmarshalRecords(repo.serializer, records)
}
func unmarshalRecords(serializer Serializer, records []Record) (events []Event, err error) {
for _, record := range records {
var event Event
if event, err = serializer.Unmarshal(record.Data, record.Type); err != nil {
err = errors.Wrap(err, "failed to unmarshal record")
return
}
events = append(events, event)
}
return
}
func (repo repository) LoadEvents(ctx context.Context, opts ...QueryOption) (events []Event, err error) {
var records []Record
if records, err = repo.store.Load(ctx, opts...); err != nil {
return
}
return unmarshalRecords(repo.serializer, records)
}
// Deprecated
func (repo repository) GetEventsBySequenceID(ctx context.Context, sequenceID string, opts ...QueryOption) (events []Event, err error) {
var records []Record
if records, err = repo.store.LoadBySequenceID(ctx, sequenceID, opts...); err != nil {
return
}
return unmarshalRecords(repo.serializer, records)
}
// Deprecated
func (repo repository) GetEventsBySequenceIDAndType(ctx context.Context, sequenceID string, eventType Event, opts ...QueryOption) (events []Event, err error) {
var records []Record
if records, err = repo.store.LoadBySequenceIDAndType(ctx, sequenceID, GetTypeName(eventType), opts...); err != nil {
return
}
return unmarshalRecords(repo.serializer, records)
}
// Deprecated
func (repo repository) GetEventsByTimestamp(ctx context.Context, timestamp int64, opts ...QueryOption) (events []Event, err error) {
var records []Record
if records, err = repo.store.LoadByTimestamp(ctx, timestamp, opts...); err != nil {
return
}
return unmarshalRecords(repo.serializer, records)
}