Skip to content

Commit

Permalink
Remote: gRPC load balancing. (Part 1)
Browse files Browse the repository at this point in the history
Defines ConnectionFactory interfaces used to create gRPC connections. This change abstracts away the details of connection creation and allows us applying connection pooling and load balancing without changing client code.

PiperOrigin-RevId: 357575236
  • Loading branch information
Googler authored and Copybara-Service committed Feb 15, 2021
1 parent 97bc48d commit 97963c5
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/remote/BUILD
Expand Up @@ -10,6 +10,7 @@ filegroup(
"//src/main/java/com/google/devtools/build/lib/remote/common:srcs",
"//src/main/java/com/google/devtools/build/lib/remote/downloader:srcs",
"//src/main/java/com/google/devtools/build/lib/remote/disk:srcs",
"//src/main/java/com/google/devtools/build/lib/remote/grpc:srcs",
"//src/main/java/com/google/devtools/build/lib/remote/http:srcs",
"//src/main/java/com/google/devtools/build/lib/remote/logging:srcs",
"//src/main/java/com/google/devtools/build/lib/remote/merkletree:srcs",
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/google/devtools/build/lib/remote/grpc/BUILD
@@ -0,0 +1,22 @@
load("@rules_java//java:defs.bzl", "java_library")

package(
default_visibility = ["//src:__subpackages__"],
)

licenses(["notice"])

filegroup(
name = "srcs",
srcs = glob(["*"]),
visibility = ["//src:__subpackages__"],
)

java_library(
name = "grpc",
srcs = glob(["*.java"]),
deps = [
"//third_party:rxjava3",
"//third_party/grpc:grpc-jar",
],
)
@@ -0,0 +1,37 @@
// Copyright 2021 The Bazel Authors. All rights reserved.
//
// 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.devtools.build.lib.remote.grpc;

import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.MethodDescriptor;
import java.io.Closeable;
import java.io.IOException;

/**
* A single connection to a server. RPCs are executed within the context of a connection. A {@link
* Connection} object can consist of any number of transport connections.
*
* <p>Connections must be closed to ensure proper resource disposal.
*/
public interface Connection extends Closeable {

/** Creates a new {@link ClientCall} for issuing RPC. */
<ReqT, RespT> ClientCall<ReqT, RespT> call(
MethodDescriptor<ReqT, RespT> method, CallOptions options);

/** Releases any resources held by the {@link Connection}. */
@Override
void close() throws IOException;
}
@@ -0,0 +1,32 @@
// Copyright 2021 The Bazel Authors. All rights reserved.
//
// 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.devtools.build.lib.remote.grpc;

import io.reactivex.rxjava3.core.Single;

/**
* A {@link ConnectionFactory} represents a resource factory for connection creation. It may create
* connections by itself, wrap a {@link ConnectionFactory}, or apply connection pooling on top of a
* {@link ConnectionFactory}.
*
* <p>A {@link ConnectionFactory} uses deferred initialization and should initiate connection
* resource allocation after subscription.
*
* <p>Connection creation must be cancellable. Canceling connection creation must release (“close”)
* the connection and all associated resources.
*/
public interface ConnectionFactory {
/** Creates a new {@link Connection}. */
Single<? extends Connection> create();
}
@@ -0,0 +1,30 @@
// Copyright 2021 The Bazel Authors. All rights reserved.
//
// 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.devtools.build.lib.remote.grpc;

import java.io.Closeable;
import java.io.IOException;

/**
* A {@link ConnectionFactory} that apply connection pooling. Connections created by {@link
* ConnectionPool} will not be closed by {@link Connection#close()}, use {@link
* ConnectionPool#close()} instead to close all the connections in the pool.
*
* <p>Connections must be closed with {@link Connection#close()} in order to be reused later.
*/
public interface ConnectionPool extends ConnectionFactory, Closeable {
/** Closes the connection pool and closes all the underlying connections */
@Override
void close() throws IOException;
}

0 comments on commit 97963c5

Please sign in to comment.