forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema_validator.go
200 lines (176 loc) · 5.85 KB
/
schema_validator.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
// Copyright 2016 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package domain
import (
"sync"
"time"
log "github.com/Sirupsen/logrus"
)
type checkResult int
const (
// ResultSucc means schemaValidator's check is passing.
ResultSucc checkResult = iota
// ResultFail means schemaValidator's check is fail.
ResultFail
// ResultUnknown means schemaValidator doesn't know the check would be success or fail.
ResultUnknown
)
// SchemaValidator is the interface for checking the validity of schema version.
type SchemaValidator interface {
// Update the schema validator, add a new item, delete the expired deltaSchemaInfos.
// The latest schemaVer is valid within leaseGrantTime plus lease duration.
// Add the changed table IDs to the new schema information,
// which is produced when the oldSchemaVer is updated to the newSchemaVer.
Update(leaseGrantTime uint64, oldSchemaVer, newSchemaVer int64, changedTableIDs []int64)
// Check is it valid for a transaction to use schemaVer and related tables, at timestamp txnTS.
Check(txnTS uint64, schemaVer int64, relatedTableIDs []int64) checkResult
// Stop stops checking the valid of transaction.
Stop()
// Restart restarts the schema validator after it is stopped.
Restart()
// Reset resets SchemaValidator to initial state.
Reset()
}
type deltaSchemaInfo struct {
schemaVersion int64
relatedTableIDs []int64
}
type schemaValidator struct {
isStarted bool
mux sync.RWMutex
lease time.Duration
latestSchemaVer int64
latestSchemaExpire time.Time
// deltaSchemaInfos is a queue that maintain the history of changes.
deltaSchemaInfos []deltaSchemaInfo
}
// NewSchemaValidator returns a SchemaValidator structure.
func NewSchemaValidator(lease time.Duration) SchemaValidator {
return &schemaValidator{
isStarted: true,
lease: lease,
deltaSchemaInfos: make([]deltaSchemaInfo, 0, maxNumberOfDiffsToLoad),
}
}
func (s *schemaValidator) Stop() {
log.Info("the schema validator stops")
s.mux.Lock()
defer s.mux.Unlock()
s.isStarted = false
s.latestSchemaVer = 0
s.deltaSchemaInfos = make([]deltaSchemaInfo, 0, maxNumberOfDiffsToLoad)
}
func (s *schemaValidator) Restart() {
log.Info("the schema validator restarts")
s.mux.Lock()
defer s.mux.Unlock()
s.isStarted = true
}
func (s *schemaValidator) Reset() {
s.mux.Lock()
defer s.mux.Unlock()
s.isStarted = true
s.latestSchemaVer = 0
s.deltaSchemaInfos = make([]deltaSchemaInfo, 0, maxNumberOfDiffsToLoad)
}
func (s *schemaValidator) Update(leaseGrantTS uint64, oldVer, currVer int64, changedTableIDs []int64) {
s.mux.Lock()
defer s.mux.Unlock()
if !s.isStarted {
log.Infof("the schema validator stopped before updating")
return
}
// Renew the lease.
s.latestSchemaVer = currVer
leaseGrantTime := extractPhysicalTime(leaseGrantTS)
leaseExpire := leaseGrantTime.Add(s.lease - time.Millisecond)
s.latestSchemaExpire = leaseExpire
// Update the schema deltaItem information.
if currVer != oldVer {
log.Debug("update schema validator:", oldVer, currVer, changedTableIDs)
s.enqueue(currVer, changedTableIDs)
}
}
func hasRelatedTableID(relatedTableIDs, updateTableIDs []int64) bool {
for _, tblID := range updateTableIDs {
for _, relatedTblID := range relatedTableIDs {
if tblID == relatedTblID {
return true
}
}
}
return false
}
// isRelatedTablesChanged returns the result whether relatedTableIDs is changed
// from usedVer to the latest schema version.
// NOTE, this function should be called under lock!
func (s *schemaValidator) isRelatedTablesChanged(currVer int64, tableIDs []int64) bool {
if len(s.deltaSchemaInfos) == 0 {
log.Infof("schema change history is empty, checking %d", currVer)
return true
}
newerDeltas := s.findNewerDeltas(currVer)
if len(newerDeltas) == len(s.deltaSchemaInfos) {
log.Infof("the schema version %d is much older than the latest version %d", currVer, s.latestSchemaVer)
return true
}
for _, item := range newerDeltas {
if hasRelatedTableID(item.relatedTableIDs, tableIDs) {
return true
}
}
return false
}
func (s *schemaValidator) findNewerDeltas(currVer int64) []deltaSchemaInfo {
q := s.deltaSchemaInfos
pos := len(q)
for i := len(q) - 1; i >= 0 && q[i].schemaVersion > currVer; i-- {
pos = i
}
return q[pos:]
}
// Check checks schema validity, returns true if use schemaVer and related tables at txnTS is legal.
func (s *schemaValidator) Check(txnTS uint64, schemaVer int64, relatedTableIDs []int64) checkResult {
s.mux.RLock()
defer s.mux.RUnlock()
if !s.isStarted {
log.Infof("the schema validator stopped before checking")
return ResultFail
}
if s.lease == 0 {
return ResultSucc
}
// Schema changed, result decided by whether related tables change.
if schemaVer < s.latestSchemaVer {
if s.isRelatedTablesChanged(schemaVer, relatedTableIDs) {
return ResultFail
}
return ResultSucc
}
// Schema unchanged, maybe success or the schema validator is unavailable.
t := extractPhysicalTime(txnTS)
if t.After(s.latestSchemaExpire) {
return ResultUnknown
}
return ResultSucc
}
func extractPhysicalTime(ts uint64) time.Time {
t := int64(ts >> 18) // 18 for physicalShiftBits
return time.Unix(t/1e3, (t%1e3)*1e6)
}
func (s *schemaValidator) enqueue(schemaVersion int64, relatedTableIDs []int64) {
s.deltaSchemaInfos = append(s.deltaSchemaInfos, deltaSchemaInfo{schemaVersion, relatedTableIDs})
if len(s.deltaSchemaInfos) > maxNumberOfDiffsToLoad {
s.deltaSchemaInfos = s.deltaSchemaInfos[1:]
}
}