-
Notifications
You must be signed in to change notification settings - Fork 5
/
pool.go
194 lines (177 loc) · 5.22 KB
/
pool.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
package spool
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"strings"
"time"
"cloud.google.com/go/spanner"
admin "cloud.google.com/go/spanner/admin/database/apiv1"
"github.com/cloudspannerecosystem/spool/model"
databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1"
)
// State represents a state of the database.
type State int64
const (
// StateIdle represents a idle state.
StateIdle State = iota
// StateBusy represents a busy state.
StateBusy
)
// Int64 returns s as int64.
func (s State) Int64() int64 {
return int64(s)
}
// String returns a string representing the state.
func (s State) String() string {
switch s {
case StateIdle:
return "idle"
case StateBusy:
return "busy"
}
return "unknown"
}
// Pool represents a Spanner database pool.
type Pool struct {
client *spanner.Client
adminClient *admin.DatabaseAdminClient
conf *Config
ddlStatements []string
checksum string
}
// NewPool creates a new Pool.
func NewPool(ctx context.Context, conf *Config, ddl []byte) (*Pool, error) {
client, err := spanner.NewClient(ctx, conf.Database(), conf.ClientOptions()...)
if err != nil {
return nil, err
}
adminClient, err := admin.NewDatabaseAdminClient(ctx, conf.ClientOptions()...)
if err != nil {
return nil, err
}
pool := &Pool{
client: client,
adminClient: adminClient,
conf: conf,
ddlStatements: ddlToStatements(ddl),
checksum: checksum(ddl),
}
return pool, nil
}
func ddlToStatements(ddl []byte) []string {
ddls := bytes.Split(ddl, []byte(";"))
ddlStatements := make([]string, 0, len(ddls))
for _, s := range ddls {
if stmt := strings.TrimSpace(string(s)); stmt != "" {
ddlStatements = append(ddlStatements, stmt)
}
}
return ddlStatements
}
func checksum(ddl []byte) string {
return fmt.Sprintf("%x", sha256.Sum256(ddl))
}
// Create creates a new database and adds to the pool.
func (p *Pool) Create(ctx context.Context, dbNamePrefix string) (*model.SpoolDatabase, error) {
dbName := fmt.Sprintf("%s-%d", dbNamePrefix, time.Now().Unix())
sdb := &model.SpoolDatabase{
DatabaseName: dbName,
Checksum: p.checksum,
State: StateIdle.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
return p.create(ctx, sdb)
}
func (p *Pool) create(ctx context.Context, sdb *model.SpoolDatabase) (*model.SpoolDatabase, error) {
op, err := p.adminClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
Parent: p.conf.Instance(),
CreateStatement: fmt.Sprintf("CREATE DATABASE `%s`", sdb.DatabaseName),
ExtraStatements: p.ddlStatements,
})
if err != nil {
return nil, err
}
if _, err := op.Wait(ctx); err != nil {
return nil, err
}
ts, err := p.client.Apply(ctx, []*spanner.Mutation{sdb.Insert(ctx)})
if err != nil {
_ = dropDatabase(ctx, p.conf.WithDatabaseID(sdb.DatabaseName))
return nil, err
}
sdb.CreatedAt = ts
sdb.UpdatedAt = ts
return sdb, nil
}
// Get gets a idle database from the pool.
func (p *Pool) Get(ctx context.Context) (*model.SpoolDatabase, error) {
var sdb *model.SpoolDatabase
if _, err := p.client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
var err error
sdb, err = model.FindSpoolDatabaseByChecksumState(ctx, txn, p.checksum, StateIdle.Int64())
if err != nil {
return err
}
sdb.ChangeState(StateBusy.Int64())
if err := txn.BufferWrite([]*spanner.Mutation{sdb.Update(ctx)}); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
return sdb, nil
}
// GetOrCreate gets a idle database or creates a new database.
func (p *Pool) GetOrCreate(ctx context.Context, dbNamePrefix string) (*model.SpoolDatabase, error) {
sdb, err := p.Get(ctx)
if err == nil {
return sdb, nil
}
if !isErrNotFound(err) {
return nil, err
}
dbName := fmt.Sprintf("%s-%d", dbNamePrefix, time.Now().Unix())
sdb = &model.SpoolDatabase{
DatabaseName: dbName,
Checksum: p.checksum,
State: StateBusy.Int64(),
CreatedAt: spanner.CommitTimestamp,
UpdatedAt: spanner.CommitTimestamp,
}
return p.create(ctx, sdb)
}
// List gets all databases from the pool.
func (p *Pool) List(ctx context.Context) ([]*model.SpoolDatabase, error) {
return model.FindSpoolDatabasesByChecksum(ctx, p.client.ReadOnlyTransaction(), p.checksum)
}
// Put adds a database to the pool.
func (p *Pool) Put(ctx context.Context, dbName string) error {
if _, err := p.client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
sdb, err := model.FindSpoolDatabase(ctx, txn, dbName)
if err != nil {
return err
}
sdb.ChangeState(StateIdle.Int64())
if err := txn.BufferWrite([]*spanner.Mutation{sdb.Update(ctx)}); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
// Clean removes all idle databases.
func (p *Pool) Clean(ctx context.Context, filters ...func(sdb *model.SpoolDatabase) bool) error {
return clean(ctx, p.client, p.conf, func(ctx context.Context, txn *spanner.ReadWriteTransaction) ([]*model.SpoolDatabase, error) {
sdbs, err := model.FindSpoolDatabasesByChecksumState(ctx, txn, p.checksum, StateIdle.Int64())
if err != nil {
return nil, err
}
return filter(sdbs, filters...), nil
})
}