Skip to content

Commit 97963c5

Browse files
Googlercopybara-github
Googler
authored andcommitted
Remote: gRPC load balancing. (Part 1)
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
1 parent 97bc48d commit 97963c5

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

src/main/java/com/google/devtools/build/lib/remote/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ filegroup(
1010
"//src/main/java/com/google/devtools/build/lib/remote/common:srcs",
1111
"//src/main/java/com/google/devtools/build/lib/remote/downloader:srcs",
1212
"//src/main/java/com/google/devtools/build/lib/remote/disk:srcs",
13+
"//src/main/java/com/google/devtools/build/lib/remote/grpc:srcs",
1314
"//src/main/java/com/google/devtools/build/lib/remote/http:srcs",
1415
"//src/main/java/com/google/devtools/build/lib/remote/logging:srcs",
1516
"//src/main/java/com/google/devtools/build/lib/remote/merkletree:srcs",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("@rules_java//java:defs.bzl", "java_library")
2+
3+
package(
4+
default_visibility = ["//src:__subpackages__"],
5+
)
6+
7+
licenses(["notice"])
8+
9+
filegroup(
10+
name = "srcs",
11+
srcs = glob(["*"]),
12+
visibility = ["//src:__subpackages__"],
13+
)
14+
15+
java_library(
16+
name = "grpc",
17+
srcs = glob(["*.java"]),
18+
deps = [
19+
"//third_party:rxjava3",
20+
"//third_party/grpc:grpc-jar",
21+
],
22+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2021 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.devtools.build.lib.remote.grpc;
15+
16+
import io.grpc.CallOptions;
17+
import io.grpc.ClientCall;
18+
import io.grpc.MethodDescriptor;
19+
import java.io.Closeable;
20+
import java.io.IOException;
21+
22+
/**
23+
* A single connection to a server. RPCs are executed within the context of a connection. A {@link
24+
* Connection} object can consist of any number of transport connections.
25+
*
26+
* <p>Connections must be closed to ensure proper resource disposal.
27+
*/
28+
public interface Connection extends Closeable {
29+
30+
/** Creates a new {@link ClientCall} for issuing RPC. */
31+
<ReqT, RespT> ClientCall<ReqT, RespT> call(
32+
MethodDescriptor<ReqT, RespT> method, CallOptions options);
33+
34+
/** Releases any resources held by the {@link Connection}. */
35+
@Override
36+
void close() throws IOException;
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2021 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.devtools.build.lib.remote.grpc;
15+
16+
import io.reactivex.rxjava3.core.Single;
17+
18+
/**
19+
* A {@link ConnectionFactory} represents a resource factory for connection creation. It may create
20+
* connections by itself, wrap a {@link ConnectionFactory}, or apply connection pooling on top of a
21+
* {@link ConnectionFactory}.
22+
*
23+
* <p>A {@link ConnectionFactory} uses deferred initialization and should initiate connection
24+
* resource allocation after subscription.
25+
*
26+
* <p>Connection creation must be cancellable. Canceling connection creation must release (“close”)
27+
* the connection and all associated resources.
28+
*/
29+
public interface ConnectionFactory {
30+
/** Creates a new {@link Connection}. */
31+
Single<? extends Connection> create();
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2021 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.devtools.build.lib.remote.grpc;
15+
16+
import java.io.Closeable;
17+
import java.io.IOException;
18+
19+
/**
20+
* A {@link ConnectionFactory} that apply connection pooling. Connections created by {@link
21+
* ConnectionPool} will not be closed by {@link Connection#close()}, use {@link
22+
* ConnectionPool#close()} instead to close all the connections in the pool.
23+
*
24+
* <p>Connections must be closed with {@link Connection#close()} in order to be reused later.
25+
*/
26+
public interface ConnectionPool extends ConnectionFactory, Closeable {
27+
/** Closes the connection pool and closes all the underlying connections */
28+
@Override
29+
void close() throws IOException;
30+
}

0 commit comments

Comments
 (0)