/
GceTestEnvConfig.java
229 lines (206 loc) · 9.32 KB
/
GceTestEnvConfig.java
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
/*
* Copyright 2017 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 com.google.cloud.spanner;
import static com.google.common.base.Preconditions.checkState;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.spanner.spi.v1.SpannerInterceptorProvider;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
import io.grpc.Grpc;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.Status;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
/** Configure TestEnv based on configuration provided. */
public class GceTestEnvConfig implements TestEnvConfig {
public static final String GCE_PROJECT_ID = "spanner.gce.config.project_id";
public static final String GCE_SERVER_URL = "spanner.gce.config.server_url";
public static final String GCE_CREDENTIALS_FILE = "spanner.gce.config.credentials_file";
public static final String GCE_STREAM_BROKEN_PROBABILITY =
"spanner.gce.config.stream_broken_probability";
public static final String ATTEMPT_DIRECT_PATH = "spanner.attempt_directpath";
public static final String DIRECT_PATH_TEST_SCENARIO = "spanner.directpath_test_scenario";
// IP address prefixes allocated for DirectPath backends.
public static final String DP_IPV6_PREFIX = "2001:4860:8040";
public static final String DP_IPV4_PREFIX = "34.126";
private static final String DIRECT_PATH_ENDPOINT = "aa423245250f2bbf.sandbox.googleapis.com:443";
private final SpannerOptions options;
public GceTestEnvConfig() {
String projectId = System.getProperty(GCE_PROJECT_ID, "");
String serverUrl = System.getProperty(GCE_SERVER_URL, "");
String credentialsFile = System.getProperty(GCE_CREDENTIALS_FILE, "");
double errorProbability =
Double.parseDouble(System.getProperty(GCE_STREAM_BROKEN_PROBABILITY, "0.0"));
checkState(errorProbability <= 1.0);
boolean attemptDirectPath = Boolean.getBoolean(ATTEMPT_DIRECT_PATH);
String directPathTestScenario = System.getProperty(DIRECT_PATH_TEST_SCENARIO, "");
SpannerOptions.Builder builder =
SpannerOptions.newBuilder()
.setAutoThrottleAdministrativeRequests()
.setTrackTransactionStarter();
InstantiatingGrpcChannelProvider.Builder defaultChannelProviderBuilder =
InstantiatingGrpcChannelProvider.newBuilder();
if (attemptDirectPath) {
builder.setChannelProvider(
defaultChannelProviderBuilder.setEndpoint(DIRECT_PATH_ENDPOINT).build());
}
if (!projectId.isEmpty()) {
builder.setProjectId(projectId);
}
if (!serverUrl.isEmpty()) {
builder.setHost(serverUrl);
}
if (!credentialsFile.isEmpty()) {
try {
builder.setCredentials(GoogleCredentials.fromStream(new FileInputStream(credentialsFile)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
SpannerInterceptorProvider interceptorProvider =
SpannerInterceptorProvider.createDefault().with(new GrpcErrorInjector(errorProbability));
if (attemptDirectPath) {
interceptorProvider =
interceptorProvider.with(new DirectPathAddressCheckInterceptor(directPathTestScenario));
}
builder.setInterceptorProvider(interceptorProvider);
options = builder.build();
}
@Override
public SpannerOptions spannerOptions() {
return options;
}
@Override
public void setUp() {}
@Override
public void tearDown() {}
/** Injects errors in streaming calls to simulate call restarts */
private static class GrpcErrorInjector implements ClientInterceptor {
private final double errorProbability;
private final Random random = new Random();
GrpcErrorInjector(double errorProbability) {
this.errorProbability = errorProbability;
}
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
// Only inject errors in the Cloud Spanner data API.
if (!method.getFullMethodName().startsWith("google.spanner.v1.Spanner")) {
return next.newCall(method, callOptions);
}
final AtomicBoolean errorInjected = new AtomicBoolean();
final ClientCall<ReqT, RespT> clientCall = next.newCall(method, callOptions);
return new SimpleForwardingClientCall<ReqT, RespT>(clientCall) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
super.start(
new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onMessage(RespT message) {
super.onMessage(message);
if (mayInjectError()) {
// Cancel the call after at least one response has been received.
// This will cause the call to terminate, then we can set UNAVAILABLE
// in the onClose() handler to cause a retry.
errorInjected.set(true);
clientCall.cancel("Cancelling call for injected error", null);
}
}
@Override
public void onClose(Status status, Metadata metadata) {
if (errorInjected.get()) {
// UNAVAILABLE error will cause the call to retry.
status = Status.UNAVAILABLE.augmentDescription("INJECTED BY TEST");
}
super.onClose(status, metadata);
}
},
headers);
}
};
}
private boolean mayInjectError() {
return random.nextDouble() < errorProbability;
}
}
/**
* Captures the request attributes "Grpc.TRANSPORT_ATTR_REMOTE_ADDR" when connection is
* established and verifies if the remote address is a DirectPath address. This is only used for
* DirectPath testing. {@link ClientCall#getAttributes()}
*/
private static class DirectPathAddressCheckInterceptor implements ClientInterceptor {
private final String directPathTestScenario;
DirectPathAddressCheckInterceptor(String directPathTestScenario) {
this.directPathTestScenario = directPathTestScenario;
}
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
final ClientCall<ReqT, RespT> clientCall = next.newCall(method, callOptions);
return new SimpleForwardingClientCall<ReqT, RespT>(clientCall) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
super.start(
new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onHeaders(Metadata headers) {
// Check peer IP after connection is established.
SocketAddress remoteAddr =
clientCall.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
if (!verifyRemoteAddress(remoteAddr)) {
throw new RuntimeException(
String.format(
"Synthetically aborting the current request because it did not adhere"
+ " to the test environment's requirement for DirectPath."
+ " Expected test for DirectPath %s scenario,"
+ " but RPC was destined for %s",
directPathTestScenario, remoteAddr.toString()));
}
super.onHeaders(headers);
}
},
headers);
}
};
}
private boolean verifyRemoteAddress(SocketAddress remoteAddr) {
if (remoteAddr instanceof InetSocketAddress) {
InetAddress inetAddress = ((InetSocketAddress) remoteAddr).getAddress();
String addr = inetAddress.getHostAddress();
if (directPathTestScenario.equals("ipv4")) {
// For ipv4-only VM, client should connect to ipv4 DirectPath addresses.
return addr.startsWith(DP_IPV4_PREFIX);
} else if (directPathTestScenario.equals("ipv6")) {
// For ipv6-enabled VM, client could connect to either ipv4 or ipv6 DirectPath addresses.
return addr.startsWith(DP_IPV6_PREFIX) || addr.startsWith(DP_IPV4_PREFIX);
}
}
// For all other scenarios(e.g. fallback), we should allow both DirectPath and CFE addresses.
return true;
}
}
}