-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
313 lines (272 loc) · 8.19 KB
/
driver.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Package driver provides utilities to control Google's Spanner emulator
// for testing purposes.
package driver
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"cloud.google.com/go/spanner"
database "cloud.google.com/go/spanner/admin/database/apiv1"
instance "cloud.google.com/go/spanner/admin/instance/apiv1"
"github.com/lestrrat-go/spanner-emulator-driver/emulator"
databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1"
instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1"
"google.golang.org/grpc/codes"
)
const (
SPANNER_EMULATOR_HOST = `SPANNER_EMULATOR_HOST`
)
// Driver is the main object to control the spanner emulator.
// The zero value should not be used. Always use the value
// returned from driver.New
type Driver struct {
mu *sync.RWMutex
cond *sync.Cond
dsn string
config *Config
ready bool
setupError error
onClose []func() error
instanceConfig string
}
func New(dsn string) (*Driver, error) {
// Kind of silly, but we parse back the dsn
config, err := ParseDSN(dsn)
if err != nil {
return nil, fmt.Errorf(`failed to parse DSN: %w`, err)
}
mu := &sync.RWMutex{}
return &Driver{
mu: mu,
cond: sync.NewCond(mu),
config: config,
dsn: dsn,
}, nil
}
// Run controls the emulator running in docker. The environment variable
// SPANNER_EMULATOR_HOST will also be set to the appropriate value
func (d *Driver) Run(ctx context.Context, options ...Option) <-chan error {
dropDatabase := true
useEmulator := true
instanceConfig := ""
for _, option := range options {
switch option.Ident() {
case identDropDatabase{}:
dropDatabase = option.Value().(bool)
case identDDLDirectory{}:
ctx = context.WithValue(ctx, identDDLDirectory{}, option.Value().(string))
case identUseEmulator{}:
useEmulator = option.Value().(bool)
case identInstanceConfig{}:
instanceConfig = option.Value().(string)
}
}
d.instanceConfig = instanceConfig
defer d.cond.Broadcast()
// channel to notify readiness to the user
d.mu.Lock()
d.ready = false
d.mu.Unlock()
dropDatabaseFn := func() error {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
adminClient, err := database.NewDatabaseAdminClient(ctx)
if err != nil {
return fmt.Errorf(`failed to create a database admin client to drop the database: %w`, err)
}
fmt.Printf("Dropping database %q\n", d.dsn)
if err := adminClient.DropDatabase(ctx, &databasepb.DropDatabaseRequest{
Database: d.dsn,
}); err != nil {
return err
}
return nil
}
exited := make(chan error, 1)
if !useEmulator {
close(exited) // won't use
} else {
// Setup environment variable
os.Setenv(SPANNER_EMULATOR_HOST, fmt.Sprintf(`localhost:%d`, emulator.DefaultGRPCPort))
// channel to notify _US_ that the emulator is ready
emuReady := make(chan struct{})
emuOptions := []emulator.Option{
emulator.WithNotifyReady(func() { close(emuReady) }),
}
// TODO: currently we don't handle the case where we need to perform
// multiple operations in onExit... in that case we need to fix this
// code to accomodate multiple hooks
if dropDatabase {
emuOptions = append(emuOptions, emulator.WithOnExit(dropDatabaseFn))
}
go func(ctx context.Context) {
defer close(exited)
if err := emulator.Run(ctx, emuOptions...); err != nil {
select {
case <-ctx.Done():
case exited <- err:
}
}
}(ctx)
select {
case <-ctx.Done():
d.notifyReady(fmt.Errorf(`context canceled exited before emulator became ready`))
return exited
case err := <-exited:
// WHAT?!
d.notifyReady(fmt.Errorf(`emulator exited before becoming ready: %w`, err))
return exited
case <-emuReady:
// ready, go on
}
}
// start preparing
if err := d.setup(ctx); err != nil {
d.notifyReady(fmt.Errorf(`failed to setup spanner: %w`, err))
return exited
}
if !useEmulator && dropDatabase {
d.onClose = append(d.onClose, dropDatabaseFn)
}
d.notifyReady(nil)
return exited
}
func (d *Driver) Close() {
for _, fn := range d.onClose {
if err := fn(); err != nil {
log.Printf("onClose callback failed: %s", err)
}
}
}
func (d *Driver) notifyReady(err error) {
d.mu.Lock()
d.ready = true
d.setupError = err
d.cond.Broadcast()
d.mu.Unlock()
}
func (d *Driver) Ready(ctx context.Context) error {
d.cond.L.Lock()
for !d.ready {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
d.cond.Wait()
}
d.cond.L.Unlock()
return d.setupError
}
func (d *Driver) setup(ctx context.Context) error {
if err := d.createSpannerInstance(ctx); err != nil {
return fmt.Errorf(`failed to create spanner instance: %w`, err)
}
if err := d.createSpannerDatabase(ctx); err != nil {
return fmt.Errorf(`failed to create spanner database: %w`, err)
}
return nil
}
func (d *Driver) createSpannerInstance(ctx context.Context) error {
instanceAdminClient, err := instance.NewInstanceAdminClient(ctx)
if err != nil {
return fmt.Errorf(`failed to create instance admin client: %w`, err)
}
defer instanceAdminClient.Close()
name := projectMarker + d.config.Project + instanceMarker + d.config.Instance
log.Printf("Querying %q", name)
if _, err := instanceAdminClient.GetInstance(ctx, &instancepb.GetInstanceRequest{
Name: name,
}); err == nil {
// instance already exists
log.Printf("Instance %q already exists", name)
return nil
}
if err != nil && spanner.ErrCode(err) != codes.NotFound {
return fmt.Errorf(`unexpected error while retrieving instance: %w`, err)
}
if d.instanceConfig == "" {
return fmt.Errorf(`value for InstanceConfig must be specified via driver.WithInstanceCnfig() to create an instance`)
}
if _, err := instanceAdminClient.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
Parent: projectMarker + d.config.Project,
InstanceId: d.config.Instance,
Instance: &instancepb.Instance{
Name: projectMarker + d.config.Project + instanceMarker + d.config.Instance,
Config: d.instanceConfig,
DisplayName: d.config.Instance,
NodeCount: 1,
},
}); err != nil {
return fmt.Errorf(`failed to create instance %q: %w`, name, err)
}
return nil
}
func (d *Driver) createSpannerDatabase(ctx context.Context) error {
adminClient, err := database.NewDatabaseAdminClient(ctx)
if err != nil {
return fmt.Errorf(`failed to create a database admin client: %w`, err)
}
log.Printf("Querying %q", d.dsn)
_, err = adminClient.GetDatabase(ctx, &databasepb.GetDatabaseRequest{
Name: d.dsn,
})
switch {
case err == nil:
fmt.Printf("Database %q already exist\n", d.dsn)
// if the database exists, we just use it
return nil
case err != nil && spanner.ErrCode(err) != codes.NotFound:
return fmt.Errorf(`unexpected error while retrieving database %q: %w`, d.dsn, err)
default:
// no op, go to next
}
var extraStatements []string
// We can load the initial DDLs from the specified directory
var ddlDirectory string
if v := ctx.Value(identDDLDirectory{}); v != nil {
if s, ok := v.(string); ok {
ddlDirectory = s
}
}
if dir := ddlDirectory; dir != "" {
if _, err := os.Stat(dir); err != nil {
return fmt.Errorf(`ddl directory specified in SPANNER_EMULATOR_DDL_DIR does not exist`)
}
entries, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf(`failed to read ddl directory %q: %w`, dir, err)
}
for _, e := range entries {
if e.IsDir() {
continue
}
if !strings.HasSuffix(e.Name(), `.sql`) {
continue
}
fullpath := filepath.Join(dir, e.Name())
content, err := os.ReadFile(fullpath)
if err != nil {
return fmt.Errorf(`failed to read contents of %q: %w`, fullpath, err)
}
extraStatements = append(extraStatements, string(content))
}
}
op, err := adminClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
Parent: projectMarker + d.config.Project + instanceMarker + d.config.Instance,
CreateStatement: fmt.Sprintf("CREATE DATABASE `%s`", d.config.Database),
ExtraStatements: extraStatements,
})
if err != nil {
return fmt.Errorf(`create database call failed: %w`, err)
}
if _, err := op.Wait(ctx); err != nil {
return fmt.Errorf(`create database failed while waiting for the operation to complete: %w`, err)
}
return nil
}