-
Notifications
You must be signed in to change notification settings - Fork 23
/
drop_source_command.go
199 lines (168 loc) · 5.42 KB
/
drop_source_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
package command
import (
log "github.com/sirupsen/logrus"
"github.com/squareup/pranadb/cluster"
"strings"
"sync"
"github.com/squareup/pranadb/command/parser"
"github.com/squareup/pranadb/common"
"github.com/squareup/pranadb/errors"
)
type DropSourceCommand struct {
lock sync.Mutex
e *Executor
schemaName string
sql string
sourceName string
sourceInfo *common.SourceInfo
toDeleteBatch *cluster.ToDeleteBatch
}
func (d *DropSourceCommand) CommandType() DDLCommandType {
return DDLCommandTypeDropSource
}
func (d *DropSourceCommand) SchemaName() string {
return d.schemaName
}
func (d *DropSourceCommand) SQL() string {
return d.sql
}
func (d *DropSourceCommand) TableSequences() []uint64 {
return nil
}
func (d *DropSourceCommand) Cancel() {
}
func NewOriginatingDropSourceCommand(e *Executor, schemaName string, sql string, sourceName string) *DropSourceCommand {
return &DropSourceCommand{
e: e,
schemaName: schemaName,
sql: sql,
sourceName: strings.ToLower(sourceName),
}
}
func NewDropSourceCommand(e *Executor, schemaName string, sql string) *DropSourceCommand {
return &DropSourceCommand{
e: e,
schemaName: schemaName,
sql: sql,
}
}
func (d *DropSourceCommand) Before() error {
d.lock.Lock()
defer d.lock.Unlock()
sourceInfo, err := d.getSourceInfo()
if err != nil {
return errors.WithStack(err)
}
d.sourceInfo = sourceInfo
source, err := d.e.pushEngine.GetSource(sourceInfo.ID)
if err != nil {
return errors.WithStack(err)
}
consuming := source.GetConsumingMVs()
if len(consuming) != 0 {
return errors.NewSourceHasChildrenError(d.sourceInfo.SchemaName, d.sourceInfo.Name, consuming)
}
return nil
}
func (d *DropSourceCommand) OnPhase(phase int32) error {
switch phase {
case 0:
return d.onPhase0()
case 1:
return d.onPhase1()
default:
panic("invalid phase")
}
}
func (d *DropSourceCommand) NumPhases() int {
return 2
}
func (d *DropSourceCommand) onPhase0() error {
d.lock.Lock()
defer d.lock.Unlock()
// Source should be removed from in memory metadata, consumers for the source should be closed
if d.sourceInfo == nil {
sourceInfo, err := d.getSourceInfo()
if err != nil {
return errors.WithStack(err)
}
d.sourceInfo = sourceInfo
}
log.Debugf("drop source command phase 0 %s.%s", d.sourceInfo.SchemaName, d.sourceInfo.Name)
if err := d.e.metaController.UnregisterSource(d.schemaName, d.sourceInfo.Name); err != nil {
return errors.WithStack(err)
}
log.Debugf("drop source command phase 0 %s.%s unregistered from meta controller", d.sourceInfo.SchemaName, d.sourceInfo.Name)
src, err := d.e.pushEngine.GetSource(d.sourceInfo.ID)
if err != nil {
return errors.WithStack(err)
}
// src.Stop() stops the sources consumers, it does not remove it
err = src.Stop()
log.Debugf("drop source command phase 0 %s.%s stopped sourcer", d.sourceInfo.SchemaName, d.sourceInfo.Name)
return err
}
func (d *DropSourceCommand) onPhase1() error {
d.lock.Lock()
defer d.lock.Unlock()
log.Debugf("drop source command phase 1 %s.%s", d.sourceInfo.SchemaName, d.sourceInfo.Name)
// Remove the source from the push engine and delete all it's data
// Deleting the data could take some time
src, err := d.e.pushEngine.RemoveSource(d.sourceInfo)
if err != nil {
return errors.WithStack(err)
}
log.Debugf("drop source command phase 1 %s.%s removed from push engine", d.sourceInfo.SchemaName, d.sourceInfo.Name)
err = src.Drop()
log.Debugf("drop source command phase 1 %s.%s dropped", d.sourceInfo.SchemaName, d.sourceInfo.Name)
return err
}
func (d *DropSourceCommand) AfterPhase(phase int32) error {
d.lock.Lock()
defer d.lock.Unlock()
log.Debugf("drop source command after phase %d %s.%s", phase, d.sourceInfo.SchemaName, d.sourceInfo.Name)
switch phase {
case 0:
// We record prefixes in the to_delete table - this makes sure MV data is deleted on restart if failure occurs
// after this
var err error
d.toDeleteBatch, err = storeToDeleteBatch(d.sourceInfo.ID, d.e.cluster)
if err != nil {
return err
}
log.Debugf("drop source command after phase %d %s.%s stored delete batch", phase, d.sourceInfo.SchemaName, d.sourceInfo.Name)
// Delete the source from the tables table - this must happen before the source data is deleted or we can
// end up with partial source on recovery after failure
err = d.e.metaController.DeleteSource(d.sourceInfo.ID)
log.Debugf("drop source command after phase %d %s.%s deleted source from tables table", phase, d.sourceInfo.SchemaName, d.sourceInfo.Name)
return err
case 1:
// Now delete rows from the to_delete table
err := d.e.cluster.RemoveToDeleteBatch(d.toDeleteBatch)
log.Debugf("drop source command after phase %d %s.%s removed delete batch", phase, d.sourceInfo.SchemaName, d.sourceInfo.Name)
return err
}
return nil
}
func (d *DropSourceCommand) Cleanup() {
}
func (d *DropSourceCommand) getSourceInfo() (*common.SourceInfo, error) {
if d.sourceName == "" {
ast, err := parser.Parse(d.sql)
if err != nil {
return nil, errors.WithStack(err)
}
if ast.Drop == nil && !ast.Drop.Source {
return nil, errors.Errorf("not a drop source command %s", d.sql)
}
d.sourceName = strings.ToLower(ast.Drop.Name)
}
sourceInfo, ok := d.e.metaController.GetSource(d.schemaName, d.sourceName)
if !ok {
return nil, errors.NewUnknownSourceError(d.schemaName, d.sourceName)
}
return sourceInfo, nil
}
func (d *DropSourceCommand) GetExtraData() []byte {
return nil
}