m-trieu commented on code in PR #28835: URL: https://github.com/apache/beam/pull/28835#discussion_r1369242359
########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/DispatcherClient.java: ########## @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.beam.runners.dataflow.worker.windmill; + +import static org.apache.beam.runners.dataflow.worker.windmill.util.WindmillChannelFactory.LOCALHOST; +import static org.apache.beam.runners.dataflow.worker.windmill.util.WindmillChannelFactory.localhostChannel; +import static org.apache.beam.runners.dataflow.worker.windmill.util.WindmillChannelFactory.remoteChannel; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import javax.annotation.concurrent.GuardedBy; +import javax.annotation.concurrent.ThreadSafe; +import org.apache.beam.runners.dataflow.worker.windmill.CloudWindmillServiceV1Alpha1Grpc.CloudWindmillServiceV1Alpha1Stub; +import org.apache.beam.runners.dataflow.worker.windmill.util.WindmillGrpcStubFactory; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableSet; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.net.HostAndPort; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Manages endpoints and stubs for connecting to the Windmill Dispatcher. */ +@ThreadSafe +public class DispatcherClient { + + private static final Logger LOG = LoggerFactory.getLogger(DispatcherClient.class); + private final WindmillGrpcStubFactory windmillGrpcStubFactory; + private final int windmillServiceRpcChannelTimeoutSec; + + @GuardedBy("this") + private final List<CloudWindmillServiceV1Alpha1Stub> dispatcherStubs; + + @GuardedBy("this") + private final Set<HostAndPort> dispatcherEndpoints; + + @GuardedBy("this") + private final Random rand; + + private DispatcherClient( + WindmillGrpcStubFactory windmillGrpcStubFactory, + int windmillServiceRpcChannelTimeoutSec, + List<CloudWindmillServiceV1Alpha1Stub> dispatcherStubs, + Set<HostAndPort> dispatcherEndpoints, + Random rand) { + this.windmillGrpcStubFactory = windmillGrpcStubFactory; + this.windmillServiceRpcChannelTimeoutSec = windmillServiceRpcChannelTimeoutSec; + this.dispatcherStubs = dispatcherStubs; + this.dispatcherEndpoints = dispatcherEndpoints; + this.rand = rand; + } + + public static DispatcherClient create( + WindmillGrpcStubFactory windmillGrpcStubFactory, int windmillServiceRpcChannelTimeoutSec) { + return new DispatcherClient( + windmillGrpcStubFactory, + windmillServiceRpcChannelTimeoutSec, + new ArrayList<>(), + new HashSet<>(), + new Random()); + } + + @VisibleForTesting + static DispatcherClient forTesting( + WindmillGrpcStubFactory windmillGrpcStubFactory, + List<CloudWindmillServiceV1Alpha1Stub> dispatcherStubs, + Set<HostAndPort> dispatcherEndpoints) { + return new DispatcherClient( Review Comment: 1. done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
