-
Notifications
You must be signed in to change notification settings - Fork 23
/
create_index_command.go
228 lines (198 loc) · 6.07 KB
/
create_index_command.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
package command
import (
"github.com/squareup/pranadb/cluster"
"github.com/squareup/pranadb/interruptor"
"strings"
"sync"
"github.com/squareup/pranadb/command/parser"
"github.com/squareup/pranadb/common"
"github.com/squareup/pranadb/errors"
"github.com/squareup/pranadb/parplan"
)
type CreateIndexCommand struct {
lock sync.Mutex
e *Executor
pl *parplan.Planner
schema *common.Schema
createIndexSQL string
tableSequences []uint64
indexInfo *common.IndexInfo
ast *parser.CreateIndex
toDeleteBatch *cluster.ToDeleteBatch
interruptor interruptor.Interruptor
leadersMap map[uint64]uint64
}
func (c *CreateIndexCommand) CommandType() DDLCommandType {
return DDLCommandTypeCreateIndex
}
func (c *CreateIndexCommand) SchemaName() string {
return c.schema.Name
}
func (c *CreateIndexCommand) SQL() string {
return c.createIndexSQL
}
func (c *CreateIndexCommand) TableSequences() []uint64 {
return c.tableSequences
}
func (c *CreateIndexCommand) Cancel() {
c.interruptor.Interrupt()
}
func NewOriginatingCreateIndexCommand(e *Executor, pl *parplan.Planner, schema *common.Schema, createIndexSQL string,
tableSequences []uint64, ast *parser.CreateIndex) (*CreateIndexCommand, error) {
leadersMap, err := e.cluster.GetLeadersMap()
if err != nil {
return nil, err
}
return &CreateIndexCommand{
e: e,
schema: schema,
pl: pl,
createIndexSQL: createIndexSQL,
tableSequences: tableSequences,
ast: ast,
leadersMap: leadersMap,
}, nil
}
func NewCreateIndexCommand(e *Executor, schemaName string, createIndexSQL string, tableSequences []uint64, extraData []byte) *CreateIndexCommand {
schema := e.metaController.GetOrCreateSchema(schemaName)
pl := parplan.NewPlanner(schema)
return &CreateIndexCommand{
e: e,
schema: schema,
pl: pl,
createIndexSQL: createIndexSQL,
tableSequences: tableSequences,
leadersMap: deserializeLeadersMap(extraData),
}
}
func (c *CreateIndexCommand) OnPhase(phase int32) error {
switch phase {
case 0:
return c.onPhase0()
case 1:
return c.onPhase1()
default:
panic("invalid phase")
}
}
func (c *CreateIndexCommand) NumPhases() int {
return 2
}
func (c *CreateIndexCommand) Before() error {
c.lock.Lock()
defer c.lock.Unlock()
var err error
c.indexInfo, err = c.getIndexInfo(c.ast)
return err
}
func (c *CreateIndexCommand) onPhase0() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.indexInfo == nil {
ast, err := parser.Parse(c.createIndexSQL)
if err != nil {
return err
}
if ast.Create != nil && ast.Create.Index != nil {
c.indexInfo, err = c.getIndexInfo(ast.Create.Index)
if err != nil {
return err
}
} else {
return errors.Errorf("invalid create index statement %s", c.createIndexSQL)
}
}
// We store rows in the to_delete table - if index creation fails (e.g. node crashes) then on restart the index state will
// be cleaned up - we have to add a prefix for each shard as the shard id comes first in the key
var err error
c.toDeleteBatch, err = storeToDeleteBatch(c.indexInfo.ID, c.e.cluster)
if err != nil {
return err
}
var localLeaderShards []uint64
for shardID, nodeID := range c.leadersMap {
if nodeID == uint64(c.e.cluster.GetNodeID()) {
localLeaderShards = append(localLeaderShards, shardID)
}
}
if err := c.e.cluster.RegisterStartFill(c.leadersMap, &c.interruptor); err != nil {
return err
}
// We create the index but we don't register it yet
if err := c.e.pushEngine.CreateIndex(c.indexInfo, true, localLeaderShards, &c.interruptor); err != nil {
return err
}
c.e.cluster.RegisterEndFill()
return nil
}
func (c *CreateIndexCommand) onPhase1() error {
// We can now remove from the to_delete table
if err := c.e.cluster.RemoveToDeleteBatch(c.toDeleteBatch); err != nil {
return err
}
// Now we register the index so it can be used in queries
return c.e.metaController.RegisterIndex(c.indexInfo)
}
func (c *CreateIndexCommand) AfterPhase(phase int32) error {
c.lock.Lock()
defer c.lock.Unlock()
if phase == 0 {
// We persist the index after it's been filled but *before* it's been registered - otherwise in case of
// failure the index can disappear after it has been used
if err := c.e.metaController.PersistIndex(c.indexInfo); err != nil {
return err
}
}
return nil
}
func (c *CreateIndexCommand) Cleanup() {
c.lock.Lock()
defer c.lock.Unlock()
if c.indexInfo == nil {
return
}
if err := c.e.pushEngine.UnattachIndex(c.indexInfo); err != nil {
// Ignore
}
c.e.cluster.RegisterEndFill()
}
func (c *CreateIndexCommand) getIndexInfo(ast *parser.CreateIndex) (*common.IndexInfo, error) {
ast.Name = strings.ToLower(ast.Name)
var tab common.Table
tab, ok := c.e.metaController.GetSource(c.SchemaName(), ast.TableName)
if !ok {
tab, ok = c.e.metaController.GetMaterializedView(c.SchemaName(), ast.TableName)
if !ok {
return nil, errors.NewUnknownTableError(c.SchemaName(), ast.TableName)
}
}
tabInfo := tab.GetTableInfo()
if tabInfo.IndexInfos != nil {
_, ok := tabInfo.IndexInfos[ast.Name]
if ok {
return nil, errors.NewIndexAlreadyExistsError(c.SchemaName(), ast.TableName, ast.Name)
}
}
colMap := make(map[string]int, len(tabInfo.ColumnNames))
for colIndex, colName := range tabInfo.ColumnNames {
colMap[colName] = colIndex
}
indexCols := make([]int, len(ast.ColumnNames))
indexColMap := make(map[int]struct{}, len(ast.ColumnNames))
for i, colName := range ast.ColumnNames {
colIndex, ok := colMap[colName.Name]
if !ok {
return nil, errors.NewPranaErrorf(errors.InvalidStatement, "Unknown column %s in %s.%s",
colName.Name, c.SchemaName(), ast.TableName)
}
indexCols[i] = colIndex
indexColMap[colIndex] = struct{}{}
}
if len(indexColMap) != len(ast.ColumnNames) {
return nil, errors.NewPranaErrorf(errors.InvalidStatement, "Index cannot contain same column multiple times")
}
return common.NewIndexInfo(c.SchemaName(), c.tableSequences[0], ast.TableName, ast.Name, indexCols), nil
}
func (c *CreateIndexCommand) GetExtraData() []byte {
return serializeLeadersMap(c.leadersMap)
}