6060import com .google .auto .value .AutoValue ;
6161import java .io .IOException ;
6262import java .io .InputStream ;
63+ import java .net .ServerSocket ;
6364import java .util .ArrayList ;
6465import java .util .Arrays ;
6566import java .util .Collection ;
137138import org .apache .beam .runners .dataflow .worker .windmill .Windmill .WatermarkHold ;
138139import org .apache .beam .runners .dataflow .worker .windmill .Windmill .WorkItemCommitRequest ;
139140import org .apache .beam .runners .dataflow .worker .windmill .Windmill .WorkerMetadataResponse ;
140- import org .apache .beam .runners .dataflow .worker .windmill .WindmillServiceAddress ;
141141import org .apache .beam .runners .dataflow .worker .windmill .client .getdata .FakeGetDataClient ;
142142import org .apache .beam .runners .dataflow .worker .windmill .client .grpc .stubs .WindmillChannels ;
143143import org .apache .beam .runners .dataflow .worker .windmill .testing .FakeWindmillStubFactory ;
190190import org .apache .beam .vendor .grpc .v1p69p0 .com .google .protobuf .ByteString ;
191191import org .apache .beam .vendor .grpc .v1p69p0 .com .google .protobuf .TextFormat ;
192192import org .apache .beam .vendor .grpc .v1p69p0 .io .grpc .Server ;
193- import org .apache .beam .vendor .grpc .v1p69p0 .io .grpc .inprocess . InProcessServerBuilder ;
193+ import org .apache .beam .vendor .grpc .v1p69p0 .io .grpc .ServerBuilder ;
194194import org .apache .beam .vendor .grpc .v1p69p0 .io .grpc .testing .GrpcCleanupRule ;
195195import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .cache .CacheStats ;
196196import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .collect .ImmutableList ;
197197import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .collect .ImmutableMap ;
198198import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .collect .Iterables ;
199199import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .collect .Lists ;
200- import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .net .HostAndPort ;
201200import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .primitives .UnsignedLong ;
202201import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .util .concurrent .ThreadFactoryBuilder ;
203202import org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .util .concurrent .Uninterruptibles ;
@@ -4076,28 +4075,31 @@ public void testSwitchStreamingWorkerHarness() throws Exception {
40764075 return ;
40774076 }
40784077
4079- String serverName = "StreamingDataflowWorkerTestChannel" ;
4078+ int port = -1 ;
4079+ try (ServerSocket socket = new ServerSocket (0 )) {
4080+ port = socket .getLocalPort ();
4081+ }
4082+ String serverEndpoint = "localhost:" + port ;
40804083 Server fakeServer =
40814084 grpcCleanup
40824085 .register (
4083- InProcessServerBuilder . forName ( serverName )
4086+ ServerBuilder . forPort ( port )
40844087 .directExecutor ()
40854088 .addService (new FakeWindmillServer .FakeWindmillMetadataService (server ))
40864089 .addService (
40874090 new CloudWindmillServiceV1Alpha1Grpc
40884091 .CloudWindmillServiceV1Alpha1ImplBase () {})
40894092 .build ())
40904093 .start ();
4091-
40924094 List <ParallelInstruction > instructions =
40934095 Arrays .asList (
40944096 makeSourceInstruction (StringUtf8Coder .of ()),
40954097 makeSinkInstruction (StringUtf8Coder .of (), 0 ));
40964098
4097- // Start with CloudPath .
4099+ // Start with Directpath .
40984100 DataflowWorkerHarnessOptions options =
4099- createTestingPipelineOptions ("--isWindmillServiceDirectPathEnabled=false " );
4100- options .setWindmillServiceEndpoint (serverName );
4101+ createTestingPipelineOptions ("--isWindmillServiceDirectPathEnabled=true " );
4102+ options .setWindmillServiceEndpoint (serverEndpoint );
41014103
41024104 StreamingDataflowWorker worker =
41034105 makeWorker (
@@ -4117,16 +4119,8 @@ public void testSwitchStreamingWorkerHarness() throws Exception {
41174119 List <Consumer <StreamingGlobalConfig >> observers = observerCaptor .getAllValues ();
41184120
41194121 assertTrue (
4120- "Worker should start with SingleSourceWorkerHarness" ,
4121- worker .getStreamingWorkerHarness () instanceof SingleSourceWorkerHarness );
4122-
4123- // Process some work with CloudPath.
4124- server .whenGetWorkCalled ().thenReturn (makeInput (1 , 1000 ));
4125- Map <Long , Windmill .WorkItemCommitRequest > result = server .waitForAndGetCommits (1 );
4126- assertEquals (1 , result .size ());
4127- assertTrue (result .containsKey (1L ));
4128-
4129- ExecutorService harnessSwitchExecutor = worker .getHarnessSwitchExecutor ();
4122+ "Worker should start with FanOutStreamingEngineWorkerHarness" ,
4123+ worker .getStreamingWorkerHarness () instanceof FanOutStreamingEngineWorkerHarness );
41304124
41314125 // Prepare WorkerMetadataResponse
41324126 server .injectWorkerMetadata (
@@ -4135,13 +4129,35 @@ public void testSwitchStreamingWorkerHarness() throws Exception {
41354129 .addWorkEndpoints (
41364130 WorkerMetadataResponse .Endpoint .newBuilder ()
41374131 .setBackendWorkerToken ("workerToken1" )
4138- .setDirectEndpoint (
4139- WindmillServiceAddress .create (HostAndPort .fromParts ("localhost" , 12345 ))
4140- .gcpServiceAddress ()
4141- .toString ())
4132+ .setDirectEndpoint (serverEndpoint )
41424133 .build ())
41434134 .build ());
41444135
4136+ // Switch to Cloudpath.
4137+ StreamingGlobalConfig cloudPathConfig =
4138+ StreamingGlobalConfig .builder ()
4139+ .setUserWorkerJobSettings (
4140+ Windmill .UserWorkerRunnerV1Settings .newBuilder ()
4141+ .setConnectivityType (ConnectivityType .CONNECTIVITY_TYPE_CLOUDPATH )
4142+ .build ())
4143+ .build ();
4144+ for (Consumer <StreamingGlobalConfig > observer : observers ) {
4145+ observer .accept (cloudPathConfig );
4146+ }
4147+
4148+ ExecutorService harnessSwitchExecutor = worker .getHarnessSwitchExecutor ();
4149+ Future <?> cloudPathSwitchFuture = harnessSwitchExecutor .submit (() -> {});
4150+ cloudPathSwitchFuture .get (30 , TimeUnit .SECONDS );
4151+ assertTrue (
4152+ "Worker should switch to SingleSourceWorkerHarness" ,
4153+ worker .getStreamingWorkerHarness () instanceof SingleSourceWorkerHarness );
4154+
4155+ // Process some work with CloudPath.
4156+ server .whenGetWorkCalled ().thenReturn (makeInput (1 , 1000 ));
4157+ Map <Long , Windmill .WorkItemCommitRequest > result = server .waitForAndGetCommits (1 );
4158+ assertEquals (1 , result .size ());
4159+ assertTrue (result .containsKey (1L ));
4160+
41454161 // Switch to Directpath.
41464162 StreamingGlobalConfig directPathConfig =
41474163 StreamingGlobalConfig .builder ()
@@ -4164,8 +4180,8 @@ public void testSwitchStreamingWorkerHarness() throws Exception {
41644180 "Worker should switch to FanOutStreamingEngineWorkerHarness" ,
41654181 worker .getStreamingWorkerHarness () instanceof FanOutStreamingEngineWorkerHarness );
41664182
4167- // Switch to Cloudpath.
4168- StreamingGlobalConfig cloudPathConfig =
4183+ // Switch to Cloudpath again .
4184+ cloudPathConfig =
41694185 StreamingGlobalConfig .builder ()
41704186 .setUserWorkerJobSettings (
41714187 Windmill .UserWorkerRunnerV1Settings .newBuilder ()
@@ -4176,7 +4192,7 @@ public void testSwitchStreamingWorkerHarness() throws Exception {
41764192 observer .accept (cloudPathConfig );
41774193 }
41784194
4179- Future <?> cloudPathSwitchFuture = harnessSwitchExecutor .submit (() -> {});
4195+ cloudPathSwitchFuture = harnessSwitchExecutor .submit (() -> {});
41804196 cloudPathSwitchFuture .get (30 , TimeUnit .SECONDS );
41814197 assertTrue (
41824198 "Worker should switch back to SingleSourceWorkerHarness" ,
0 commit comments