/
gateway.go
175 lines (155 loc) · 5.06 KB
/
gateway.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
//
// 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 gateway implements a REST gateway for the emulator GRPC service.
package gateway
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"time"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
instancepb "cloud.google.com/go/spanner/admin/instance/apiv1/instancepb"
lrgw "cloud_spanner_emulator/gateway/longrunning_operations_gateway"
dagw "cloud_spanner_emulator/gateway/spanner_admin_database_gateway"
spgw "cloud_spanner_emulator/gateway/spanner_gateway"
iagw "cloud_spanner_emulator/gateway/spanner_admin_instance_gateway"
)
// Options encapsulates options for the emulator gateway.
type Options struct {
GatewayAddress string
FrontendBinary string
FrontendAddress string
CopyEmulatorStdout bool
CopyEmulatorStderr bool
LogRequests bool
EnableFaultInjection bool
DisableQueryNullFilteredIndexCheck bool
}
// Gateway implements the emulator gateway server.
type Gateway struct {
opts Options
}
// New returns a new gateway.
func New(opts Options) *Gateway {
return &Gateway{opts}
}
// Run starts the emulator gateway server.
func (gw *Gateway) Run() {
// Start the emulator grpc server and redirect its output.
emulatorArgs := []string{
"--host_port", gw.opts.FrontendAddress,
}
if gw.opts.LogRequests {
emulatorArgs = append(emulatorArgs, "--log_requests")
}
if gw.opts.EnableFaultInjection {
emulatorArgs = append(emulatorArgs, "--enable_fault_injection")
}
if gw.opts.DisableQueryNullFilteredIndexCheck {
emulatorArgs = append(emulatorArgs, "--disable_query_null_filtered_index_check")
}
cmd := exec.Command(gw.opts.FrontendBinary, emulatorArgs...)
// Proxy emulator log to gateway log.
if gw.opts.CopyEmulatorStdout {
cmd.Stdout = os.Stdout
}
if gw.opts.CopyEmulatorStderr {
cmd.Stderr = os.Stderr
}
// Start the grpc server but won't block for the grpc server to be up.
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
// Terminate the grpc server if the gateway server is terminated.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
// Release resources e.g., network ports associated with the process.
// This is required since gateway may receive an interrupt signal for
// shutdown before Wait() returns.
cmd.Process.Release()
cmd.Process.Kill()
os.Exit(0)
}()
// Terminate the gateway server if the grpc server is terminated.
go func() {
cmd.Wait()
log.Println("Shutting down gateway server since grpc server is terminated.")
os.Exit(cmd.ProcessState.ExitCode())
}()
// Wait for the grpc server to be up.
ctx := context.Background()
addr := gw.opts.FrontendAddress
if err = waitForReady(ctx, addr); err != nil {
log.Fatal(fmt.Errorf("Error waiting for emulator to start: %v", err))
}
// Setup the gateway services.
mux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{}))
opts := []grpc.DialOption{grpc.WithInsecure()}
err = spgw.RegisterSpannerHandlerFromEndpoint(ctx, mux, addr, opts)
if err != nil {
log.Fatal(err)
}
err = iagw.RegisterInstanceAdminHandlerFromEndpoint(ctx, mux, addr, opts)
if err != nil {
log.Fatal(err)
}
err = dagw.RegisterDatabaseAdminHandlerFromEndpoint(ctx, mux, addr, opts)
if err != nil {
log.Fatal(err)
}
err = lrgw.RegisterOperationsHandlerFromEndpoint(ctx, mux, addr, opts)
if err != nil {
log.Fatal(err)
}
// Start the gateway http server.
log.Println("Cloud Spanner emulator running.")
log.Println("REST server listening at", gw.opts.GatewayAddress)
log.Println("gRPC server listening at", gw.opts.FrontendAddress)
err = http.ListenAndServe(gw.opts.GatewayAddress, mux)
if err != nil {
log.Fatal(err)
}
}
func waitForReady(ctx context.Context, endpoint string) error {
timeout := 30 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
conn, err := grpc.Dial(endpoint, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return err
}
defer conn.Close()
// To test whether the server is up, wait for ListInstanceConfigs to respond
// for a dummy project.
instanceAdminClient := instancepb.NewInstanceAdminClient(conn)
if _, err = instanceAdminClient.ListInstanceConfigs(ctx,
&instancepb.ListInstanceConfigsRequest{
Parent: "projects/test-project",
}); err != nil {
return fmt.Errorf("emulator failed to come up at %v within %v deadline: %v",
endpoint, timeout.String(), err)
}
return nil
}