forked from cloudspannerecosystem/spanner-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinator.go
231 lines (207 loc) · 5.85 KB
/
coordinator.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
//
// Copyright 2020 Google LLC
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"cloud.google.com/go/spanner"
"context"
"errors"
"time"
)
// table is an element of the tree which represents inter-table relationships.
type table struct {
tableName string
childTables []*table
parentTableName string
parentOnDeleteAction deleteActionType
referencedBy []*table
deleter *deleter
}
// isDeletable returns true if the table is ready to be deleted.
func (t *table) isDeletable() bool {
for _, child := range t.childTables {
if child.parentOnDeleteAction == deleteActionNoAction && child.deleter.status != statusCompleted {
return false
}
if !child.isDeletable() {
return false
}
}
for _, referencing := range t.referencedBy {
if referencing.deleter.status != statusCompleted {
return false
}
}
return true
}
// constructTableTree creates a table tree which represents inter-table relationships.
func constructTableTree(originals []*table, parentTableName string) []*table {
var tables []*table
for _, original := range originals {
if original.parentTableName == parentTableName {
original.childTables = constructTableTree(originals, original.tableName)
tables = append(tables, original)
}
}
return tables
}
// flattenTables flatten table tree to list of tables.
func flattenTables(tables []*table) []*table {
var flatten []*table
for _, table := range tables {
flatten = append(flatten, table)
childFlatten := flattenTables(table.childTables)
flatten = append(flatten, childFlatten...)
}
return flatten
}
// findDeletableTables returns tables which can be deleted.
func findDeletableTables(tables []*table) []*table {
var deletable []*table
for _, table := range tables {
if s := table.deleter.status; s == statusDeleting || s == statusCompleted {
continue
}
if table.isDeletable() {
deletable = append(deletable, table)
// Parent table will be deleted, so child tables will be also deleted.
continue
}
if len(table.childTables) > 0 {
childDeletables := findDeletableTables(table.childTables)
deletable = append(deletable, childDeletables...)
}
}
return deletable
}
// coordinator initiates deleting rows from tables without violating database constraints.
type coordinator struct {
tables []*table
errChan chan error
}
func newCoordinator(schemas []*tableSchema, client *spanner.Client, column string, columnValues []string, lower string, upper string, priority int32) *coordinator {
var tables []*table
tableMap := map[string]*table{}
for _, schema := range schemas {
t := &table{
tableName: schema.tableName,
parentTableName: schema.parentTableName,
parentOnDeleteAction: schema.parentOnDeleteAction,
deleter: &deleter{
tableName: schema.tableName,
client: client,
column: column,
columnValues: columnValues,
lower: lower,
upper: upper,
priority: priority,
},
referencedBy: []*table{},
}
tables = append(tables, t)
tableMap[schema.tableName] = t
}
// Construct FK reference relationships.
for _, schema := range schemas {
if len(schema.referencedBy) == 0 {
continue
}
table := tableMap[schema.tableName]
for _, referencing := range schema.referencedBy {
table.referencedBy = append(table.referencedBy, tableMap[referencing])
}
}
// Construct Parent-Child relationships.
tables = constructTableTree(tables, "") // root
return &coordinator{
tables: tables,
errChan: make(chan error),
}
}
// start starts coordination in another goroutine.
func (c *coordinator) start(ctx context.Context) {
go func() {
for _, table := range flattenTables(c.tables) {
table.deleter.startRowCountUpdater(ctx)
}
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
tables := findDeletableTables(c.tables)
if len(tables) == 0 {
if !isAllTablesDeleted(c.tables) && !isAnyTableDeleting(c.tables) {
c.errChan <- errors.New("no deletable tables found, probably there is circular dependencies between tables")
}
}
for _, table := range tables {
go func() {
if err := table.deleter.deleteRows(ctx); err != nil {
c.errChan <- err
}
}()
cascadeDelete(table.childTables)
}
case <-ctx.Done():
c.errChan <- ctx.Err()
}
}
}()
}
// waitCompleted blocks until all deletions are completed.
func (c *coordinator) waitCompleted() error {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
if isAllTablesDeleted(c.tables) {
return nil
}
case err := <-c.errChan:
if err != nil {
return err
}
}
}
}
func isAllTablesDeleted(tables []*table) bool {
for _, table := range tables {
if table.deleter.status != statusCompleted {
return false
}
if !isAllTablesDeleted(table.childTables) {
return false
}
}
return true
}
func isAnyTableDeleting(tables []*table) bool {
for _, table := range tables {
if table.deleter.status == statusDeleting || table.deleter.status == statusCascadeDeleting {
return true
}
if isAnyTableDeleting(table.childTables) {
return true
}
}
return false
}
// cascadeDelete marks all of child tables as cascade deleting status.
func cascadeDelete(tables []*table) {
for _, table := range tables {
table.deleter.parentDeletionStarted()
cascadeDelete(table.childTables)
}
}