This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
197 lines (174 loc) · 5.04 KB
/
run.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
package limestone
import (
"context"
"fmt"
"reflect"
"github.com/ridge/limestone/tlog"
"github.com/ridge/limestone/typeddb"
"github.com/ridge/limestone/wire"
"github.com/ridge/parallel"
"go.uber.org/zap"
)
// Run executes the Limestone reading pump.
// Call it once and use the context to signal shutdown.
//
// Always returns a non-nil error: either a context cancelation reason, or
// client.ErrContinuityBroken.
func (db *DB) Run(ctx context.Context) error {
return parallel.Run(ctx, func(ctx context.Context, spawn parallel.SpawnFn) error {
ch := make(chan *wire.IncomingTransaction)
spawn("connection", parallel.Fail, func(ctx context.Context) error {
return db.connection.Run(ctx, ch)
})
spawn("incoming", parallel.Fail, func(ctx context.Context) error {
return db.process(ctx, ch)
})
return nil
})
}
func (db *DB) process(ctx context.Context, ch <-chan *wire.IncomingTransaction) error {
logger := tlog.Get(ctx)
var txn Transaction
var tc typeddb.TransactionControl
defer func() {
if txn != nil {
tc.Cancel()
}
}()
defer db.readyCancel()
defer db.scheduler.Clear()
attention := map[typeddb.EID]bool{}
caughtUpCount := 0
var lastPos wire.Position
for {
var incoming *wire.IncomingTransaction
select {
case incoming = <-ch:
if incoming == nil {
logger.Debug("Reached hot end", zap.Any("position", lastPos))
}
case <-db.scheduler.Wait():
for _, key := range db.scheduler.Get() {
logger.Debug("Alarm", zap.Stringer("eid", key))
attention[key] = true
}
case <-ctx.Done():
return ctx.Err()
}
if incoming != nil {
lastPos = incoming.Position
if db.source != nil && incoming.Source == *db.source && incoming.Session == db.session {
continue // ignoring echoed transaction
}
if db.ready {
logger.Debug("Handling transaction", zap.Object("txn", incoming))
} else {
caughtUpCount++
}
if txn == nil {
txn, tc = db.incomingTransaction()
}
for k, byID := range incoming.Changes {
kind := db.kinds[k]
if kind == nil {
continue
}
idName := kind.Identity().DBName
for id, diff := range byID {
key := typeddb.EID{Kind: kind, ID: id}
before := tc.GetByEID(key)
if before == nil && diff[idName] == nil {
continue // An update for a pruned entity
}
after, err := wire.Decode(kind.Struct, before, diff, func(index int, v1, v2 reflect.Value) error {
producers := kind.Fields[index].Producers
switch {
case incoming.Source.Producer == "":
return nil // Empty producer means admin action: any update is accepted
case len(producers) == 0:
return fmt.Errorf("writing by %s is denied", incoming.Source)
case producers[incoming.Source.Producer]:
return nil // OK
default:
return fmt.Errorf("writing by %s is denied (allowed for %s)", incoming.Source, producers)
}
})
if err != nil {
panic(fmt.Errorf("failed to decode incoming transaction at %s: %w", incoming.Position, err))
}
if after == nil {
continue
}
err = kind.ValidateRequired(after)
if err != nil {
panic(fmt.Errorf("failed to validate incoming transaction at %s: %w", incoming.Position, err))
}
if s, ok := after.(withSurvive); ok && tc.GetBeforeByEID(key) == nil && !s.Survive() {
if before != nil {
tc.Prune(key)
delete(attention, key)
}
continue
}
tc.SetByEID(key, after)
attention[key] = true
}
}
if len(attention) == 0 { // no relevant changes
tc.Cancel()
txn = nil
}
} else {
// we can get here because we received nil from the client (hot end reached),
// or because the scheduler has fired while waiting at the hot end (incoming == nil already)
if len(attention) != 0 {
if txn == nil {
txn, tc = db.incomingTransaction()
} else {
tc.Reset()
}
if db.wakeUp != nil {
entities := make([]any, 0, len(attention))
var eids []string
if db.ready {
eids = make([]string, 0, len(attention))
}
for key := range attention {
entities = append(entities, tc.GetByEID(key))
if db.ready {
eids = append(eids, key.String())
}
}
if db.ready {
logger.Debug("Waking up", zap.Int("entities", len(entities)), zap.Strings("eids", eids))
} else {
logger.Debug("Waking up for the first time", zap.Int("entities", len(entities)))
}
if err := db.submit(ctx, tc); err != nil {
return err
}
for key := range tc.Changes() {
attention[key] = true
}
}
if db.debugTap != nil {
db.debugTap <- txn.Snapshot()
}
for key := range attention {
db.postProcess(tc, key, tc.GetByEID(key), txn.Time())
}
attention = map[typeddb.EID]bool{}
tc.Commit()
txn = nil
}
if !db.ready {
logger.Info("Limestone ready", zap.Int("transactions", caughtUpCount), zap.Any("position", lastPos))
db.ready = true
db.readyCancel()
}
}
}
}
func (db *DB) incomingTransaction() (Transaction, typeddb.TransactionControl) {
return db.tdb.Transaction()
}