forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain.go
292 lines (244 loc) · 7.29 KB
/
domain.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
// Copyright 2015 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"
"sync/atomic"
"time"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/perfschema"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/localstore"
"github.com/pingcap/tidb/terror"
)
var ddlLastReloadSchemaTS = "ddl_last_reload_schema_ts"
// Domain represents a storage space. Different domains can use the same database name.
// Multiple domains can be used in parallel without synchronization.
type Domain struct {
store kv.Storage
infoHandle *infoschema.Handle
ddl ddl.DDL
leaseCh chan time.Duration
lastLeaseTS int64 // nano seconds
m sync.Mutex
}
func (do *Domain) loadInfoSchema(txn kv.Transaction) (err error) {
m := meta.NewMeta(txn)
schemaMetaVersion, err := m.GetSchemaVersion()
if err != nil {
return errors.Trace(err)
}
info := do.infoHandle.Get()
if info != nil && schemaMetaVersion <= info.SchemaMetaVersion() {
// info may be changed by other txn, so here its version may be bigger than schema version,
// so we don't need to reload.
log.Debugf("[ddl] schema version is still %d, no need reload", schemaMetaVersion)
return nil
}
schemas, err := m.ListDatabases()
if err != nil {
return errors.Trace(err)
}
for _, di := range schemas {
if di.State != model.StatePublic {
// schema is not public, can't be used outside.
continue
}
tables, err1 := m.ListTables(di.ID)
if err1 != nil {
return errors.Trace(err1)
}
di.Tables = make([]*model.TableInfo, 0, len(tables))
for _, tbl := range tables {
if tbl.State != model.StatePublic {
// schema is not public, can't be used outsiee.
continue
}
di.Tables = append(di.Tables, tbl)
}
}
log.Infof("[ddl] loadInfoSchema %d", schemaMetaVersion)
err = do.infoHandle.Set(schemas, schemaMetaVersion)
return errors.Trace(err)
}
// InfoSchema gets information schema from domain.
func (do *Domain) InfoSchema() infoschema.InfoSchema {
// try reload if possible.
do.tryReload()
return do.infoHandle.Get()
}
// PerfSchema gets performance schema from domain.
func (do *Domain) PerfSchema() perfschema.PerfSchema {
return do.infoHandle.GetPerfHandle()
}
// DDL gets DDL from domain.
func (do *Domain) DDL() ddl.DDL {
return do.ddl
}
// Store gets KV store from domain.
func (do *Domain) Store() kv.Storage {
return do.store
}
// SetLease will reset the lease time for online DDL change.
func (do *Domain) SetLease(lease time.Duration) {
if lease <= 0 {
log.Warnf("[ddl] set the current lease:%v into a new lease:%v failed, so do nothing",
do.ddl.GetLease(), lease)
return
}
if do.leaseCh == nil {
log.Errorf("[ddl] set the current lease:%v into a new lease:%v failed, so do nothing",
do.ddl.GetLease(), lease)
return
}
do.leaseCh <- lease
// let ddl to reset lease too.
do.ddl.SetLease(lease)
}
// Stats returns the domain statistic.
func (do *Domain) Stats() (map[string]interface{}, error) {
m := make(map[string]interface{})
m[ddlLastReloadSchemaTS] = atomic.LoadInt64(&do.lastLeaseTS) / 1e9
return m, nil
}
// GetScope gets the status variables scope.
func (do *Domain) GetScope(status string) variable.ScopeFlag {
// Now domain status variables scope are all default scope.
return variable.DefaultScopeFlag
}
func (do *Domain) tryReload() {
// if we don't have update the schema for a long time > lease, we must force reloading it.
// Although we try to reload schema every lease time in a goroutine, sometimes it may not
// run accurately, e.g, the machine has a very high load, running the ticker is delayed.
last := atomic.LoadInt64(&do.lastLeaseTS)
lease := do.ddl.GetLease()
// if lease is 0, we use the local store, so no need to reload.
if lease > 0 && time.Now().UnixNano()-last > lease.Nanoseconds() {
do.mustReload()
}
}
const minReloadTimeout = 20 * time.Second
func (do *Domain) reload() error {
// lock here for only once at same time.
do.m.Lock()
defer do.m.Unlock()
timeout := do.ddl.GetLease() / 2
if timeout < minReloadTimeout {
timeout = minReloadTimeout
}
done := make(chan error, 1)
go func() {
var err error
for {
err = kv.RunInNewTxn(do.store, false, do.loadInfoSchema)
// if err is db closed, we will return it directly, otherwise, we will
// check reloading again.
if terror.ErrorEqual(err, localstore.ErrDBClosed) {
break
}
if err != nil {
log.Errorf("[ddl] load schema err %v, retry again", errors.ErrorStack(err))
// TODO: use a backoff algorithm.
time.Sleep(500 * time.Millisecond)
continue
}
atomic.StoreInt64(&do.lastLeaseTS, time.Now().UnixNano())
break
}
done <- err
}()
select {
case err := <-done:
return errors.Trace(err)
case <-time.After(timeout):
return errLoadSchemaTimeOut
}
}
func (do *Domain) mustReload() {
// if reload error, we will terminate whole program to guarantee data safe.
err := do.reload()
if err != nil {
log.Fatalf("[ddl] reload schema err %v", errors.ErrorStack(err))
}
}
// check schema every 300 seconds default.
const defaultLoadTime = 300 * time.Second
func (do *Domain) loadSchemaInLoop(lease time.Duration) {
ticker := time.NewTicker(lease)
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := do.reload()
// we may close store in test, but the domain load schema loop is still checking,
// so we can't panic for ErrDBClosed and just return here.
if terror.ErrorEqual(err, localstore.ErrDBClosed) {
return
} else if err != nil {
log.Fatalf("[ddl] reload schema err %v", errors.ErrorStack(err))
}
case newLease := <-do.leaseCh:
if lease == newLease {
// nothing to do
continue
}
lease = newLease
// reset ticker too.
ticker.Stop()
ticker = time.NewTicker(lease)
}
}
}
type ddlCallback struct {
ddl.BaseCallback
do *Domain
}
func (c *ddlCallback) OnChanged(err error) error {
if err != nil {
return err
}
log.Warnf("[ddl] on DDL change")
c.do.mustReload()
return nil
}
// NewDomain creates a new domain.
func NewDomain(store kv.Storage, lease time.Duration) (d *Domain, err error) {
d = &Domain{store: store}
d.infoHandle, err = infoschema.NewHandle(d.store)
if err != nil {
return nil, errors.Trace(err)
}
d.ddl = ddl.NewDDL(d.store, d.infoHandle, &ddlCallback{do: d}, lease)
d.mustReload()
variable.RegisterStatistics(d)
// Only when the store is local that the lease value is 0.
// If the store is local, it doesn't need loadSchemaInLoop.
if lease > 0 {
d.leaseCh = make(chan time.Duration, 1)
go d.loadSchemaInLoop(lease)
}
return d, nil
}
// Domain error codes.
const (
codeLoadSchemaTimeOut terror.ErrCode = 1
)
var (
errLoadSchemaTimeOut = terror.ClassDomain.New(codeLoadSchemaTimeOut, "reload schema timeout")
)