forked from cloudspannerecosystem/spanner-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleter.go
142 lines (121 loc) · 4.3 KB
/
deleter.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
//
// 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 (
"context"
"fmt"
"strings"
"time"
"cloud.google.com/go/spanner"
spannerProto "google.golang.org/genproto/googleapis/spanner/v1"
)
// Status is a delete status.
type status int
const (
statusAnalyzing status = iota // Status for calculating the total rows in the table.
statusWaiting // Status for waiting for dependent tables being deleted.
statusDeleting // Status for deleting rows.
statusCascadeDeleting // Status for deleting rows by parent in cascaded way.
statusCompleted // Status for delete completed.
)
// deleter deletes all rows from the table.
type deleter struct {
tableName string
client *spanner.Client
status status
column string
columnValues []string
lower string
upper string
priority int32
// Total rows in the table.
// Once set, we don't update this number even if new rows are added to the table.
totalRows uint64
// Remained rows in the table.
remainedRows uint64
}
// deleteRows deletes rows from the table using PDML.
func (d *deleter) deleteRows(ctx context.Context) error {
d.status = statusDeleting
stmt := spanner.NewStatement(d.getDeleteStatementString())
// low priority https://pkg.go.dev/google.golang.org/genproto/googleapis/spanner/v1#RequestOptions_Priority
options := spanner.QueryOptions{Priority: spannerProto.RequestOptions_Priority(d.priority)}
_, err := d.client.PartitionedUpdateWithOptions(ctx, stmt, options)
return err
}
func (d *deleter) getDeleteStatementString() string {
stmtStr := fmt.Sprintf("DELETE FROM `%s`", d.tableName)
stmtStr += d.getStatementStringSuffix()
return stmtStr
}
func (d *deleter) getStatementStringSuffix() string {
suffix := ""
if len(d.columnValues) > 0 {
suffix = fmt.Sprintf(" WHERE %s IN ('%s')", d.column, strings.Join(d.columnValues, "','"))
return suffix
}
hasLower := d.column != "" && d.lower != ""
hasUpper := d.column != "" && d.upper != ""
if hasLower && hasUpper {
suffix = fmt.Sprintf(" WHERE %s > '%s' AND %s < '%s'", d.column, d.lower, d.column, d.upper)
} else if hasLower {
suffix = fmt.Sprintf(" WHERE %s > '%s'", d.column, d.lower)
} else if hasUpper {
suffix = fmt.Sprintf(" WHERE %s < '%s'", d.column, d.upper)
}
return suffix
}
func (d *deleter) parentDeletionStarted() {
d.status = statusCascadeDeleting
}
// startRowCountUpdater starts periodical row count in another goroutine.
func (d *deleter) startRowCountUpdater(ctx context.Context) {
go func() {
for {
if d.status == statusCompleted {
return
}
begin := time.Now()
// Ignore error as it could be a temporal error.
d.updateRowCount(ctx)
// Sleep for a while to minimize the impact on CPU usage caused by SELECT COUNT(*) queries.
time.Sleep(time.Since(begin) * 10)
}
}()
}
func (d *deleter) updateRowCount(ctx context.Context) error {
stmt := spanner.NewStatement(fmt.Sprintf("SELECT COUNT(*) as count FROM `%s` %s", d.tableName, d.getStatementStringSuffix()))
var count int64
// Use stale read to minimize the impact on the leader replica.
txn := d.client.Single().WithTimestampBound(spanner.ExactStaleness(time.Second))
// low priority https://pkg.go.dev/google.golang.org/genproto/googleapis/spanner/v1#RequestOptions_Priority
options := spanner.QueryOptions{Priority: spannerProto.RequestOptions_Priority(d.priority)}
if err := txn.QueryWithOptions(ctx, stmt, options).Do(func(r *spanner.Row) error {
return r.ColumnByName("count", &count)
}); err != nil {
return err
}
if d.totalRows == 0 {
d.totalRows = uint64(count)
}
d.remainedRows = uint64(count)
if count == 0 {
d.status = statusCompleted
} else if d.status == statusAnalyzing {
d.status = statusWaiting
}
return nil
}