forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
203 lines (178 loc) · 4.82 KB
/
schema.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
// 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 ddl
import (
"github.com/juju/errors"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/tablecodec"
)
func (d *ddl) onCreateSchema(t *meta.Meta, job *model.Job) (ver int64, _ error) {
schemaID := job.SchemaID
dbInfo := &model.DBInfo{}
if err := job.DecodeArgs(dbInfo); err != nil {
// Invalid arguments, cancel this job.
job.State = model.JobCancelled
return ver, errors.Trace(err)
}
dbInfo.ID = schemaID
dbInfo.State = model.StateNone
dbs, err := t.ListDatabases()
if err != nil {
return ver, errors.Trace(err)
}
for _, db := range dbs {
if db.Name.L == dbInfo.Name.L {
if db.ID != schemaID {
// The database already exists, can't create it, we should cancel this job now.
job.State = model.JobCancelled
return ver, infoschema.ErrDatabaseExists.GenByArgs(db.Name)
}
dbInfo = db
}
}
ver, err = updateSchemaVersion(t, job)
if err != nil {
return ver, errors.Trace(err)
}
switch dbInfo.State {
case model.StateNone:
// none -> public
job.SchemaState = model.StatePublic
dbInfo.State = model.StatePublic
err = t.CreateDatabase(dbInfo)
if err != nil {
return ver, errors.Trace(err)
}
// Finish this job.
job.State = model.JobDone
job.BinlogInfo.AddDBInfo(ver, dbInfo)
return ver, nil
default:
// We can't enter here.
return ver, errors.Errorf("invalid db state %v", dbInfo.State)
}
}
func (d *ddl) onDropSchema(t *meta.Meta, job *model.Job) (ver int64, _ error) {
dbInfo, err := t.GetDatabase(job.SchemaID)
if err != nil {
return ver, errors.Trace(err)
}
if dbInfo == nil {
job.State = model.JobCancelled
return ver, infoschema.ErrDatabaseDropExists.GenByArgs("")
}
ver, err = updateSchemaVersion(t, job)
if err != nil {
return ver, errors.Trace(err)
}
switch dbInfo.State {
case model.StatePublic:
// public -> write only
job.SchemaState = model.StateWriteOnly
dbInfo.State = model.StateWriteOnly
err = t.UpdateDatabase(dbInfo)
case model.StateWriteOnly:
// write only -> delete only
job.SchemaState = model.StateDeleteOnly
dbInfo.State = model.StateDeleteOnly
err = t.UpdateDatabase(dbInfo)
case model.StateDeleteOnly:
dbInfo.State = model.StateNone
tables, err := t.ListTables(job.SchemaID)
if err != nil {
return ver, errors.Trace(err)
}
err = t.UpdateDatabase(dbInfo)
if err != nil {
return ver, errors.Trace(err)
}
if err = t.DropDatabase(dbInfo.ID); err != nil {
break
}
// Finish this job.
job.BinlogInfo.AddDBInfo(ver, dbInfo)
if len(tables) > 0 {
job.Args = append(job.Args, getIDs(tables))
}
job.State = model.JobDone
job.SchemaState = model.StateNone
for _, tblInfo := range dbInfo.Tables {
d.asyncNotifyEvent(&Event{Tp: model.ActionDropTable, TableInfo: tblInfo})
}
default:
// We can't enter here.
err = errors.Errorf("invalid db state %v", dbInfo.State)
}
return ver, errors.Trace(err)
}
func getIDs(tables []*model.TableInfo) []int64 {
ids := make([]int64, 0, len(tables))
for _, t := range tables {
ids = append(ids, t.ID)
}
return ids
}
func (d *ddl) delReorgSchema(t *meta.Meta, job *model.Job) error {
var startKey kv.Key
var tableIDs []int64
if err := job.DecodeArgs(&tableIDs, &startKey); err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
isFinished, err := d.dropSchemaData(tableIDs, startKey, job, t)
if err != nil {
return errors.Trace(err)
}
if !isFinished {
return nil
}
// Finish this background job.
job.SchemaState = model.StateNone
job.State = model.JobDone
return nil
}
func (d *ddl) dropSchemaData(tIDs []int64, startKey kv.Key, job *model.Job, m *meta.Meta) (bool, error) {
if len(tIDs) == 0 {
return true, nil
}
isFinished := false
var nextStartKey kv.Key
for i, id := range tIDs {
job.TableID = id
if startKey == nil {
startKey = tablecodec.EncodeTablePrefix(id)
}
delCount, err := d.dropTableData(startKey, job, defaultBatchCnt)
if err != nil {
return false, errors.Trace(err)
}
if delCount == defaultBatchCnt {
isFinished = false
nextStartKey = job.Args[len(job.Args)-1].(kv.Key)
break
}
if i < len(tIDs)-1 {
tIDs = tIDs[i+1:]
} else {
tIDs = nil
}
startKey = nil
isFinished = true
}
job.TableID = 0
job.Args = []interface{}{tIDs, nextStartKey}
return isFinished, nil
}