-
Notifications
You must be signed in to change notification settings - Fork 23
/
drop_mv_command.go
178 lines (154 loc) · 4.13 KB
/
drop_mv_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
package command
import (
"github.com/squareup/pranadb/cluster"
"strings"
"sync"
"github.com/squareup/pranadb/command/parser"
"github.com/squareup/pranadb/common"
"github.com/squareup/pranadb/errors"
"github.com/squareup/pranadb/push"
)
type DropMVCommand struct {
lock sync.Mutex
e *Executor
schemaName string
sql string
mvName string
mv *push.MaterializedView
schema *common.Schema
toDeleteBatch *cluster.ToDeleteBatch
}
func (d *DropMVCommand) CommandType() DDLCommandType {
return DDLCommandTypeDropMV
}
func (d *DropMVCommand) SchemaName() string {
return d.schemaName
}
func (d *DropMVCommand) SQL() string {
return d.sql
}
func (d *DropMVCommand) TableSequences() []uint64 {
return nil
}
func (d *DropMVCommand) Cancel() {
}
func NewOriginatingDropMVCommand(e *Executor, schemaName string, sql string, mvName string) *DropMVCommand {
return &DropMVCommand{
e: e,
schemaName: schemaName,
sql: sql,
mvName: strings.ToLower(mvName),
}
}
func NewDropMVCommand(e *Executor, schemaName string, sql string) *DropMVCommand {
return &DropMVCommand{
e: e,
schemaName: schemaName,
sql: sql,
}
}
func (d *DropMVCommand) Before() error {
d.lock.Lock()
defer d.lock.Unlock()
mv, err := d.getMV()
if err != nil {
return errors.WithStack(err)
}
d.mv = mv
consuming := d.mv.GetConsumingMVs()
if len(consuming) != 0 {
return errors.NewMaterializedViewHasChildrenError(mv.Info.SchemaName, mv.Info.Name, consuming)
}
return nil
}
func (d *DropMVCommand) OnPhase(phase int32) error {
switch phase {
case 0:
return d.onPhase0()
case 1:
return d.onPhase1()
default:
panic("invalid phase")
}
}
func (d *DropMVCommand) NumPhases() int {
return 2
}
func (d *DropMVCommand) onPhase0() error {
d.lock.Lock()
defer d.lock.Unlock()
// MV should be removed from in memory metadata and disconnected from it's sources and
// any aggregate tables disconnected as remote receivers
if d.mv == nil {
mv, err := d.getMV()
if err != nil {
return errors.WithStack(err)
}
d.mv = mv
}
var itNames []string
for _, it := range d.mv.InternalTables {
itNames = append(itNames, it.Name)
}
if err := d.e.metaController.UnregisterMaterializedView(d.schemaName, d.mv.Info.Name, itNames); err != nil {
return errors.WithStack(err)
}
schema, ok := d.e.metaController.GetSchema(d.schemaName)
if !ok {
return errors.Errorf("no such schema %s", d.schemaName)
}
d.schema = schema
return d.mv.Disconnect()
}
func (d *DropMVCommand) onPhase1() error {
d.lock.Lock()
defer d.lock.Unlock()
// Remove the mv from the push engine and delete all it's data
// Deleting the data could take some time
if err := d.e.pushEngine.RemoveMV(d.mv.Info.ID); err != nil {
return errors.WithStack(err)
}
return d.mv.Drop()
}
func (d *DropMVCommand) AfterPhase(phase int32) error {
d.lock.Lock()
defer d.lock.Unlock()
switch phase {
case 0:
var err error
d.toDeleteBatch, err = storeToDeleteBatch(d.mv.Info.ID, d.e.cluster)
if err != nil {
return err
}
// Delete the mv from the tables table - we must do this before we delete the data for the MV otherwise we can
// end up with a partial MV on restart after failure
return d.e.metaController.DeleteMaterializedView(d.mv.Info, d.mv.InternalTables)
case 1:
// Now delete rows from the to_delete table
return d.e.cluster.RemoveToDeleteBatch(d.toDeleteBatch)
}
return nil
}
func (d *DropMVCommand) Cleanup() {
}
func (d *DropMVCommand) getMV() (*push.MaterializedView, error) {
if d.mvName == "" {
ast, err := parser.Parse(d.sql)
if err != nil {
return nil, errors.WithStack(err)
}
if ast.Drop == nil && !ast.Drop.MaterializedView {
return nil, errors.Errorf("not a drop materialized view command %s", d.sql)
}
d.mvName = strings.ToLower(ast.Drop.Name)
}
mvInfo, ok := d.e.metaController.GetMaterializedView(d.schemaName, d.mvName)
if !ok {
return nil, errors.NewUnknownMaterializedViewError(d.schemaName, d.mvName)
}
mv, err := d.e.pushEngine.GetMaterializedView(mvInfo.ID)
return mv, errors.WithStack(err)
}
func (d *DropMVCommand) GetExtraData() []byte {
return nil
}