/
antidepend.go
154 lines (140 loc) · 4.13 KB
/
antidepend.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
// 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 txnimpl
import (
"fmt"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/objectio"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
)
func readWriteConfilictCheck[T catalog.BaseNode[T]](entry *catalog.BaseEntryImpl[T], ts types.TS) (err error) {
entry.RLock()
defer entry.RUnlock()
needWait, txnToWait := entry.GetLatestNodeLocked().NeedWaitCommitting(ts)
// TODO:
// I don't think we need to wait here any more. `block` and `Object` are
// local metadata and never be involved in a 2PC txn. So a prepared `block`
// will never be rollbacked
if needWait {
entry.RUnlock()
txnToWait.GetTxnState(true)
entry.RLock()
}
if entry.DeleteBefore(ts) {
err = moerr.NewTxnRWConflictNoCtx()
}
return
}
type warChecker struct {
txn txnif.AsyncTxn
catalog *catalog.Catalog
conflictSet map[types.Blockid]bool
readSet map[types.Blockid]*catalog.BlockEntry
cache map[types.Blockid]*catalog.BlockEntry
}
func newWarChecker(txn txnif.AsyncTxn, c *catalog.Catalog) *warChecker {
checker := &warChecker{
txn: txn,
catalog: c,
conflictSet: make(map[types.Blockid]bool),
readSet: make(map[types.Blockid]*catalog.BlockEntry),
cache: make(map[types.Blockid]*catalog.BlockEntry),
}
return checker
}
func (checker *warChecker) CacheGet(
dbID uint64,
tableID uint64,
ObjectID *types.Objectid,
blockID *objectio.Blockid) (block *catalog.BlockEntry, err error) {
block = checker.cacheGet(blockID)
if block != nil {
return
}
db, err := checker.catalog.GetDatabaseByID(dbID)
if err != nil {
return
}
table, err := db.GetTableEntryByID(tableID)
if err != nil {
return
}
Object, err := table.GetObjectByID(ObjectID)
if err != nil {
return
}
block, err = Object.GetBlockEntryByID(blockID)
if err != nil {
return
}
checker.Cache(block)
return
}
func (checker *warChecker) InsertByID(
dbID uint64,
tableID uint64,
ObjectID *types.Objectid,
blockID *objectio.Blockid) {
block, err := checker.CacheGet(dbID, tableID, ObjectID, blockID)
if err != nil {
panic(err)
}
checker.Insert(block)
}
func (checker *warChecker) cacheGet(id *objectio.Blockid) *catalog.BlockEntry {
return checker.cache[*id]
}
func (checker *warChecker) Cache(block *catalog.BlockEntry) {
checker.cache[block.ID] = block
}
func (checker *warChecker) Insert(block *catalog.BlockEntry) {
checker.Cache(block)
if checker.HasConflict(block.ID) {
panic(fmt.Sprintf("cannot add conflicted %s into readset", block.String()))
}
checker.readSet[block.ID] = block
}
func (checker *warChecker) checkOne(id *common.ID, ts types.TS) (err error) {
// defer func() {
// logutil.Infof("checkOne blk=%s ts=%s err=%v", id.BlockString(), ts.ToString(), err)
// }()
if checker.HasConflict(id.BlockID) {
err = moerr.NewTxnRWConflictNoCtx()
return
}
entry := checker.readSet[id.BlockID]
if entry == nil {
return
}
return readWriteConfilictCheck(entry.BaseEntryImpl, ts)
}
func (checker *warChecker) checkAll(ts types.TS) (err error) {
for _, block := range checker.readSet {
if err = readWriteConfilictCheck(block.BaseEntryImpl, ts); err != nil {
return
}
}
return
}
func (checker *warChecker) Delete(id *common.ID) {
checker.conflictSet[id.BlockID] = true
delete(checker.readSet, id.BlockID)
}
func (checker *warChecker) HasConflict(id types.Blockid) (y bool) {
_, y = checker.conflictSet[id]
return
}