1616
1717package com .google .cloud .spanner ;
1818
19- import static io .grpc .Grpc .TRANSPORT_ATTR_REMOTE_ADDR ;
2019import static org .junit .Assert .assertEquals ;
2120import static org .junit .Assert .assertFalse ;
2221import static org .junit .Assert .assertNotEquals ;
3332import com .google .spanner .v1 .BatchCreateSessionsRequest ;
3433import com .google .spanner .v1 .BeginTransactionRequest ;
3534import com .google .spanner .v1 .ExecuteSqlRequest ;
36- import io .grpc .Attributes ;
3735import io .grpc .CallOptions ;
3836import io .grpc .Channel ;
3937import io .grpc .ClientCall ;
4038import io .grpc .ClientInterceptor ;
4139import io .grpc .Context ;
4240import io .grpc .Deadline ;
4341import io .grpc .ManagedChannelBuilder ;
44- import io .grpc .Metadata ;
4542import io .grpc .MethodDescriptor ;
46- import io .grpc .ServerCall ;
47- import io .grpc .ServerCall .Listener ;
48- import io .grpc .ServerCallHandler ;
49- import io .grpc .ServerInterceptor ;
5043import io .grpc .Status ;
51- import java .io .IOException ;
52- import java .net .InetSocketAddress ;
5344import java .time .Duration ;
5445import java .util .HashMap ;
5546import java .util .HashSet ;
7061
7162@ RunWith (JUnit4 .class )
7263public class RetryOnDifferentGrpcChannelMockServerTest extends AbstractMockServerTest {
73- private static final Map <String , Set <InetSocketAddress >> SERVER_ADDRESSES = new HashMap <>();
74-
75- /** Tracks the physical channel IDs from request ID headers (set by grpc-gcp). */
76- private static final Map <String , Set <Long >> CHANNEL_HINTS = new HashMap <>();
77-
7864 /** Tracks the logical affinity keys before grpc-gcp routes the request. */
7965 private static final Map <String , Set <String >> LOGICAL_AFFINITY_KEYS = new HashMap <>();
8066
8167 @ BeforeClass
82- public static void startStaticServer () throws IOException {
68+ public static void setupAndStartServer () throws Exception {
8369 System .setProperty ("spanner.retry_deadline_exceeded_on_different_channel" , "true" );
84- startStaticServer (createServerInterceptor ());
70+ // Call the parent's startStaticServer to set up the mock server
71+ AbstractMockServerTest .startStaticServer ();
8572 }
8673
8774 @ AfterClass
@@ -91,8 +78,6 @@ public static void removeSystemProperty() {
9178
9279 @ After
9380 public void clearRequests () {
94- SERVER_ADDRESSES .clear ();
95- CHANNEL_HINTS .clear ();
9681 LOGICAL_AFFINITY_KEYS .clear ();
9782 mockSpanner .clearRequests ();
9883 mockSpanner .removeAllExecutionTimes ();
@@ -125,53 +110,6 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
125110 });
126111 }
127112
128- static ServerInterceptor createServerInterceptor () {
129- return new ServerInterceptor () {
130- @ Override
131- public <ReqT , RespT > Listener <ReqT > interceptCall (
132- ServerCall <ReqT , RespT > serverCall ,
133- Metadata metadata ,
134- ServerCallHandler <ReqT , RespT > serverCallHandler ) {
135- Attributes attributes = serverCall .getAttributes ();
136- String methodName = serverCall .getMethodDescriptor ().getFullMethodName ();
137- //noinspection unchecked,deprecation
138- Attributes .Key <InetSocketAddress > key =
139- (Attributes .Key <InetSocketAddress >)
140- attributes .keys ().stream ()
141- .filter (k -> k .equals (TRANSPORT_ATTR_REMOTE_ADDR ))
142- .findFirst ()
143- .orElse (null );
144- if (key != null ) {
145- InetSocketAddress address = attributes .get (key );
146- synchronized (SERVER_ADDRESSES ) {
147- Set <InetSocketAddress > addresses =
148- SERVER_ADDRESSES .getOrDefault (methodName , new HashSet <>());
149- addresses .add (address );
150- SERVER_ADDRESSES .putIfAbsent (methodName , addresses );
151- }
152- }
153- String requestId = metadata .get (XGoogSpannerRequestId .REQUEST_ID_HEADER_KEY );
154- if (requestId != null ) {
155- // REQUEST_ID format: version.randProcessId.nthClientId.nthChannelId.nthRequest.attempt
156- String [] parts = requestId .split ("\\ ." );
157- if (parts .length >= 6 ) {
158- try {
159- long channelHint = Long .parseLong (parts [3 ]);
160- synchronized (CHANNEL_HINTS ) {
161- Set <Long > hints = CHANNEL_HINTS .getOrDefault (methodName , new HashSet <>());
162- hints .add (channelHint );
163- CHANNEL_HINTS .putIfAbsent (methodName , hints );
164- }
165- } catch (NumberFormatException ignore ) {
166- // Ignore malformed header values in tests.
167- }
168- }
169- }
170- return serverCallHandler .startCall (serverCall , metadata );
171- }
172- };
173- }
174-
175113 SpannerOptions .Builder createSpannerOptionsBuilder () {
176114 return SpannerOptions .newBuilder ()
177115 .setProjectId ("my-project" )
0 commit comments