/
mvccchain.go
397 lines (359 loc) · 9.55 KB
/
mvccchain.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright 2021 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package txnbase
import (
"bytes"
"io"
"sync"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
)
type MVCCChain[T txnif.MVCCNode[T]] struct {
*sync.RWMutex
MVCC *common.GenericSortedDList[T]
comparefn func(T, T) int
newnodefn func() T
zero T
}
func NewMVCCChain[T txnif.MVCCNode[T]](comparefn func(T, T) int, newnodefn func() T) *MVCCChain[T] {
return &MVCCChain[T]{
MVCC: common.NewGenericSortedDList(comparefn),
RWMutex: &sync.RWMutex{},
comparefn: comparefn,
newnodefn: newnodefn,
}
}
func (be *MVCCChain[T]) Depth() int {
return be.MVCC.Depth()
}
func (be *MVCCChain[T]) StringLocked() string {
var w bytes.Buffer
it := common.NewGenericSortedDListIt(nil, be.MVCC, false)
for it.Valid() {
version := it.Get().GetPayload()
_, _ = w.WriteString(" -> ")
_, _ = w.WriteString(version.String())
it.Next()
}
return w.String()
}
// for replay
func (be *MVCCChain[T]) GetPrepareTs() types.TS {
return be.GetLatestNodeLocked().GetPrepare()
}
func (be *MVCCChain[T]) GetTxn() txnif.TxnReader { return be.GetLatestNodeLocked().GetTxn() }
func (be *MVCCChain[T]) Insert(vun T) (node *common.GenericDLNode[T]) {
un := vun
node = be.MVCC.Insert(un)
return
}
// [start, end]
// Check whether there is any committed node in between [start, end]
// -----+------+-------+--------+----------+--------->
//
// | | | | | Time
// | start | end |
// commitTs <----- commitTs <--------- commitTs|uncommitted <= MVCCChain Header
// (1) (2) (3)
func (be *MVCCChain[T]) HasCommittedNodeInRange(start, end types.TS) (ok bool) {
be.MVCC.Loop(func(n *common.GenericDLNode[T]) bool {
un := n.GetPayload()
in, before := un.CommittedIn(start, end)
// case (2)
// A committed node is found. Stop the loop.
// Found
if in {
ok = true
return false
}
// case (3)
// Committed after the end ts or uncommitted.
// Go to prev node
// Not found
if !before {
return true
}
// case (1)
// Committed before the start ts. Stop the loop.
// Not found
return false
}, false)
return
}
func (be *MVCCChain[T]) MustOneNodeLocked() (T, bool) {
var nilT T
if be.MVCC.Depth() != 1 {
return nilT, be.MVCC.Depth() == 0
}
return be.MVCC.GetHead().GetPayload(), false
}
// GetLatestNodeLocked gets the latest mvcc node.
// It is useful in making command, apply state(e.g. ApplyCommit),
// check confilct.
func (be *MVCCChain[T]) GetLatestNodeLocked() T {
head := be.MVCC.GetHead()
if head == nil {
return be.zero
}
payload := head.GetPayload()
if payload.IsNil() {
return be.zero
}
entry := payload
return entry
}
// GetLatestCommittedNode gets the latest committed mvcc node.
// It's useful when check whether the catalog/metadata entry is deleted.
func (be *MVCCChain[T]) GetLatestCommittedNode() (node T) {
be.MVCC.Loop(func(n *common.GenericDLNode[T]) bool {
un := n.GetPayload()
if !un.IsActive() && !un.IsCommitting() {
node = un
return false
}
return true
}, false)
return
}
// GetVisibleNode gets mvcc node according to the txnReader.
// It returns the mvcc node in the same txn as the read txn
// or returns the latest mvcc node with commitTS less than the timestamp.
func (be *MVCCChain[T]) GetVisibleNode(txn txnif.TxnReader) (node T) {
be.MVCC.Loop(func(n *common.GenericDLNode[T]) (goNext bool) {
un := n.GetPayload()
var visible bool
if visible = un.IsVisible(txn); visible {
node = un
}
goNext = !visible
return
}, false)
return
}
// GetVisibleNode gets mvcc node according to the timestamp.
// It returns the latest mvcc node with commitTS less than the timestamp.
func (be *MVCCChain[T]) GetVisibleNodeByTS(ts types.TS) (node T) {
be.MVCC.Loop(func(n *common.GenericDLNode[T]) (goNext bool) {
un := n.GetPayload()
var visible bool
if visible = un.IsVisibleByTS(ts); visible {
node = un
}
goNext = !visible
return
}, false)
return
}
// It's only used in replay
func (be *MVCCChain[T]) SearchNode(o T) (node T) {
be.MVCC.Loop(func(n *common.GenericDLNode[T]) bool {
un := n.GetPayload()
compare := be.comparefn(un, o)
if compare == 0 {
node = un
return false
}
// return compare > 0
return true
}, false)
return
}
func (be *MVCCChain[T]) LoopChain(fn func(T) bool) {
be.MVCC.Loop(func(n *common.GenericDLNode[T]) bool {
un := n.GetPayload()
return fn(un)
}, false)
}
func (be *MVCCChain[T]) NeedWaitCommitting(ts types.TS) (bool, txnif.TxnReader) {
un := be.GetLatestNodeLocked()
if un.IsNil() {
return false, nil
}
return un.NeedWaitCommitting(ts)
}
func (be *MVCCChain[T]) HasUncommittedNode() bool {
var found bool
be.LoopChain(func(n T) bool {
if n.IsCommitted() {
return false
} else {
if !n.IsAborted() {
found = true
}
}
return !found
})
return found
}
func (be *MVCCChain[T]) HasCommittedNode() bool {
var found bool
be.LoopChain(func(n T) bool {
if n.IsCommitted() {
found = true
return false
}
return true
})
return found
}
func (be *MVCCChain[T]) IsCreatingOrAborted() bool {
un, empty := be.MustOneNodeLocked()
if empty {
return true
}
if un.IsNil() {
return false
}
return un.IsActive() || un.IsAborted()
}
func (be *MVCCChain[T]) CheckConflict(txn txnif.TxnReader) (err error) {
node := be.GetLatestNodeLocked()
err = node.CheckConflict(txn)
return
}
func (be *MVCCChain[T]) WriteOneNodeTo(w io.Writer) (n int64, err error) {
var n2 int64
n2, err = be.GetLatestNodeLocked().WriteTo(w)
if err != nil {
return
}
n += n2
return
}
func (be *MVCCChain[T]) WriteAllTo(w io.Writer) (n int64, err error) {
depth := uint64(be.MVCC.Depth())
var sn int
if sn, err = w.Write(types.EncodeUint64(&depth)); err != nil {
return
}
n += int64(sn)
be.MVCC.Loop(func(node *common.GenericDLNode[T]) bool {
var n2 int64
n2, err = node.GetPayload().WriteTo(w)
if err != nil {
return false
}
n += n2
return true
}, true)
return
}
func (be *MVCCChain[T]) IsEmpty() bool {
head := be.MVCC.GetHead()
return head == nil
}
func (be *MVCCChain[T]) ApplyRollback() error {
be.Lock()
defer be.Unlock()
return be.GetLatestNodeLocked().ApplyRollback()
}
func (be *MVCCChain[T]) ApplyCommit() error {
be.Lock()
defer be.Unlock()
return be.GetLatestNodeLocked().ApplyCommit()
}
func (be *MVCCChain[T]) Apply1PCCommit() error {
be.Lock()
defer be.Unlock()
return be.GetLatestNodeLocked().ApplyCommit()
}
func (be *MVCCChain[T]) CloneLatestNode() (*MVCCChain[T], T) {
cloned := &MVCCChain[T]{
MVCC: common.NewGenericSortedDList(be.comparefn),
RWMutex: &sync.RWMutex{},
}
un := be.GetLatestNodeLocked()
uncloned := un.CloneData()
cloned.Insert(uncloned)
return cloned, uncloned
}
// In /Catalog, there're three states: Active, Committing and Committed.
// A txn is Active before its CommitTs is allocated.
// It's Committed when its state will never change, i.e. TxnStateCommitted and TxnStateRollbacked.
// It's Committing when it's in any other state, including TxnStateCommitting, TxnStateRollbacking, TxnStatePrepared and so on. When read or write an entry, if the last txn of the entry is Committing, we wait for it. When write on an Entry, if there's an Active txn, we report w-w conflict.
func (be *MVCCChain[T]) IsCommitting() bool {
node := be.GetLatestNodeLocked()
if node.IsNil() {
return false
}
return node.IsCommitting()
}
func (be *MVCCChain[T]) PrepareCommit() error {
be.Lock()
defer be.Unlock()
return be.GetLatestNodeLocked().PrepareCommit()
}
func (be *MVCCChain[T]) PrepareRollback() (bool, error) {
be.Lock()
defer be.Unlock()
node := be.MVCC.GetHead()
_ = node.GetPayload().PrepareRollback()
be.MVCC.Delete(node)
isEmpty := be.IsEmpty()
return isEmpty, nil
}
func (be *MVCCChain[T]) IsCommitted() bool {
un := be.GetLatestNodeLocked()
if un.IsNil() {
return false
}
return un.IsCommitted()
}
func (be *MVCCChain[T]) CloneCommittedInRange(start, end types.TS) (ret *MVCCChain[T]) {
needWait, txn := be.NeedWaitCommitting(end.Next())
if needWait {
be.RUnlock()
txn.GetTxnState(true)
be.RLock()
}
be.MVCC.Loop(func(n *common.GenericDLNode[T]) bool {
un := n.GetPayload()
in, before := un.CommittedIn(start, end)
if in {
if ret == nil {
ret = NewMVCCChain(be.comparefn, be.newnodefn)
}
ret.Insert(un.CloneAll())
} else if !before {
return false
}
return true
}, true)
return
}
// ClonePreparedInRange will collect all txn node prepared in the time window.
// Wait txn to complete committing if it didn't.
func (be *MVCCChain[T]) ClonePreparedInRange(start, end types.TS) (ret []T) {
needWait, txn := be.NeedWaitCommitting(end.Next())
if needWait {
be.RUnlock()
txn.GetTxnState(true)
be.RLock()
}
be.MVCC.Loop(func(n *common.GenericDLNode[T]) bool {
un := n.GetPayload()
in, before := un.PreparedIn(start, end)
if in {
if ret == nil {
ret = make([]T, 0)
}
ret = append(ret, un.CloneAll())
} else if !before {
return false
}
return true
}, true)
return
}