Skip to content

Commit 09578b5

Browse files
fix: Implement ApiResourceAggregation to deduplicate logic for BackgroundResources. (#301)
1 parent 96ad02c commit 09578b5

File tree

9 files changed

+120
-117
lines changed

9 files changed

+120
-117
lines changed

‎google-cloud-pubsublite/clirr-ignored-differences.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<!-- see http://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html -->
33
<differences>
44
<!-- Blanket ignored files -->
5+
<difference>
6+
<differenceType>5001</differenceType>
7+
<className>com/google/cloud/pubsublite/internal/**</className>
8+
<to>**</to>
9+
</difference>
510
<difference>
611
<differenceType>6000</differenceType>
712
<className>com/google/cloud/pubsublite/internal/**</className>

‎google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/AdminClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
package com.google.cloud.pubsublite;
1818

1919
import com.google.api.core.ApiFuture;
20-
import com.google.api.gax.core.BackgroundResource;
20+
import com.google.cloud.pubsublite.internal.ApiBackgroundResource;
2121
import com.google.cloud.pubsublite.proto.Subscription;
2222
import com.google.cloud.pubsublite.proto.Topic;
2323
import com.google.protobuf.FieldMask;
2424
import io.grpc.StatusException;
2525
import java.util.List;
2626

2727
/** A client for performing Pub/Sub Lite admin operations. */
28-
public interface AdminClient extends BackgroundResource {
28+
public interface AdminClient extends ApiBackgroundResource {
2929
static AdminClient create(AdminClientSettings settings) throws StatusException {
3030
return settings.instantiate();
3131
}

‎google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/internal/AdminClientImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.cloud.pubsublite.internal;
1818

1919
import com.google.api.core.ApiFuture;
20-
import com.google.api.gax.core.BackgroundResourceAggregation;
2120
import com.google.api.gax.core.ExecutorAsBackgroundResource;
2221
import com.google.api.gax.retrying.RetrySettings;
2322
import com.google.api.gax.retrying.RetryingExecutor;
@@ -48,7 +47,7 @@
4847
import java.util.concurrent.Executors;
4948
import java.util.concurrent.ScheduledExecutorService;
5049

51-
public class AdminClientImpl extends BackgroundResourceAggregation implements AdminClient {
50+
public class AdminClientImpl extends ApiResourceAggregation implements AdminClient {
5251
private final CloudRegion region;
5352
private final AdminServiceGrpc.AdminServiceBlockingStub stub;
5453
private final RetryingExecutor<Void> voidRetryingExecutor;
@@ -76,7 +75,7 @@ private AdminClientImpl(
7675
AdminServiceGrpc.AdminServiceBlockingStub stub,
7776
RetrySettings retrySettings,
7877
ScheduledExecutorService executor) {
79-
super(ImmutableList.of(new ExecutorAsBackgroundResource(executor)));
78+
super(new ExecutorAsBackgroundResource(executor));
8079
this.region = region;
8180
this.stub = stub;
8281
this.voidRetryingExecutor = RetryingExecutorUtil.retryingExecutor(retrySettings, executor);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.pubsublite.internal;
18+
19+
import com.google.api.gax.core.BackgroundResource;
20+
import io.grpc.StatusException;
21+
22+
public interface ApiBackgroundResource extends BackgroundResource {
23+
/**
24+
* Tear down this resource.
25+
*
26+
* @throws StatusException on a failure to properly terminate.
27+
*/
28+
@Override
29+
void close() throws StatusException;
30+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.pubsublite.internal;
18+
19+
import static com.google.cloud.pubsublite.internal.ExtractStatus.toCanonical;
20+
21+
import com.google.api.gax.core.BackgroundResource;
22+
import com.google.api.gax.core.BackgroundResourceAggregation;
23+
import com.google.common.collect.ImmutableList;
24+
import io.grpc.StatusException;
25+
import java.util.concurrent.TimeUnit;
26+
27+
public class ApiResourceAggregation implements ApiBackgroundResource {
28+
private final BackgroundResourceAggregation resources;
29+
30+
ApiResourceAggregation(BackgroundResource... resources) {
31+
this.resources = new BackgroundResourceAggregation(ImmutableList.copyOf(resources));
32+
}
33+
34+
@Override
35+
public void close() throws StatusException {
36+
try {
37+
resources.close();
38+
} catch (Throwable t) {
39+
throw toCanonical(t);
40+
}
41+
}
42+
43+
@Override
44+
public void shutdown() {
45+
resources.shutdown();
46+
}
47+
48+
@Override
49+
public boolean isShutdown() {
50+
return resources.isShutdown();
51+
}
52+
53+
@Override
54+
public boolean isTerminated() {
55+
return resources.isTerminated();
56+
}
57+
58+
@Override
59+
public void shutdownNow() {
60+
resources.shutdownNow();
61+
}
62+
63+
@Override
64+
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
65+
return resources.awaitTermination(duration, unit);
66+
}
67+
}

‎google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/internal/CursorClient.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
package com.google.cloud.pubsublite.internal;
1717

1818
import com.google.api.core.ApiFuture;
19-
import com.google.api.gax.core.BackgroundResource;
2019
import com.google.cloud.pubsublite.CloudRegion;
2120
import com.google.cloud.pubsublite.Offset;
2221
import com.google.cloud.pubsublite.Partition;
2322
import com.google.cloud.pubsublite.SubscriptionPath;
2423
import io.grpc.StatusException;
2524
import java.util.Map;
2625

27-
public interface CursorClient extends BackgroundResource {
28-
26+
public interface CursorClient extends ApiBackgroundResource {
2927
static CursorClient create(CursorClientSettings settings) throws StatusException {
3028
return settings.instantiate();
3129
}
@@ -50,12 +48,4 @@ static CursorClient create(CursorClientSettings settings) throws StatusException
5048
* @return A future for the operation's completion.
5149
*/
5250
ApiFuture<Void> commitCursor(SubscriptionPath path, Partition partition, Offset offset);
53-
54-
/**
55-
* Tear down this admin client.
56-
*
57-
* @throws StatusException on a failure to properly terminate.
58-
*/
59-
@Override
60-
void close() throws StatusException;
6151
}

‎google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/internal/CursorClientImpl.java

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.google.cloud.pubsublite.internal;
1717

1818
import com.google.api.core.ApiFuture;
19-
import com.google.api.gax.core.BackgroundResource;
2019
import com.google.api.gax.core.ExecutorAsBackgroundResource;
2120
import com.google.api.gax.retrying.RetrySettings;
2221
import com.google.api.gax.retrying.RetryingExecutor;
@@ -32,14 +31,11 @@
3231
import com.google.cloud.pubsublite.proto.ListPartitionCursorsResponse;
3332
import com.google.cloud.pubsublite.proto.PartitionCursor;
3433
import com.google.common.collect.ImmutableMap;
35-
import io.grpc.StatusException;
3634
import java.util.Map;
3735
import java.util.concurrent.Executors;
3836
import java.util.concurrent.ScheduledExecutorService;
39-
import java.util.concurrent.TimeUnit;
4037

41-
public class CursorClientImpl implements BackgroundResource, CursorClient {
42-
private final ExecutorAsBackgroundResource executorResource;
38+
public class CursorClientImpl extends ApiResourceAggregation implements CursorClient {
4339
private final CloudRegion region;
4440
private final CursorServiceBlockingStub stub;
4541
private final RetryingExecutor<Map<Partition, Offset>> listRetryingExecutor;
@@ -60,7 +56,7 @@ private CursorClientImpl(
6056
CursorServiceBlockingStub stub,
6157
RetrySettings retrySettings,
6258
ScheduledExecutorService executor) {
63-
this.executorResource = new ExecutorAsBackgroundResource(executor);
59+
super(new ExecutorAsBackgroundResource(executor));
6460
this.region = region;
6561
this.stub = stub;
6662
this.listRetryingExecutor = RetryingExecutorUtil.retryingExecutor(retrySettings, executor);
@@ -72,41 +68,6 @@ public CloudRegion region() {
7268
return region;
7369
}
7470

75-
// BackgroundResource implementation.
76-
@Override
77-
public void shutdown() {
78-
executorResource.shutdown();
79-
}
80-
81-
@Override
82-
public boolean isShutdown() {
83-
return executorResource.isShutdown();
84-
}
85-
86-
@Override
87-
public boolean isTerminated() {
88-
return executorResource.isTerminated();
89-
}
90-
91-
@Override
92-
public void shutdownNow() {
93-
executorResource.shutdownNow();
94-
}
95-
96-
@Override
97-
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
98-
return executorResource.awaitTermination(duration, unit);
99-
}
100-
101-
@Override
102-
public void close() throws StatusException {
103-
try {
104-
executorResource.close();
105-
} catch (Exception e) {
106-
throw ExtractStatus.toCanonical(e);
107-
}
108-
}
109-
11071
// CursorClient Implementation
11172
@Override
11273
public ApiFuture<Map<Partition, Offset>> listPartitionCursors(SubscriptionPath path) {

‎google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/internal/TopicStatsClient.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
package com.google.cloud.pubsublite.internal;
1717

1818
import com.google.api.core.ApiFuture;
19-
import com.google.api.gax.core.BackgroundResource;
2019
import com.google.cloud.pubsublite.CloudRegion;
2120
import com.google.cloud.pubsublite.Offset;
2221
import com.google.cloud.pubsublite.Partition;
2322
import com.google.cloud.pubsublite.TopicPath;
2423
import com.google.cloud.pubsublite.proto.ComputeMessageStatsResponse;
2524
import io.grpc.StatusException;
2625

27-
public interface TopicStatsClient extends BackgroundResource {
26+
public interface TopicStatsClient extends ApiBackgroundResource {
2827

2928
static TopicStatsClient create(TopicStatsClientSettings settings) throws StatusException {
3029
return settings.instantiate();
@@ -45,12 +44,4 @@ static TopicStatsClient create(TopicStatsClientSettings settings) throws StatusE
4544
*/
4645
ApiFuture<ComputeMessageStatsResponse> computeMessageStats(
4746
TopicPath path, Partition partition, Offset start, Offset end);
48-
49-
/**
50-
* Tear down this admin client.
51-
*
52-
* @throws StatusException on a failure to properly terminate.
53-
*/
54-
@Override
55-
void close() throws StatusException;
5647
}

‎google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/internal/TopicStatsClientImpl.java

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.google.cloud.pubsublite.internal;
1717

1818
import com.google.api.core.ApiFuture;
19-
import com.google.api.gax.core.BackgroundResource;
2019
import com.google.api.gax.core.ExecutorAsBackgroundResource;
2120
import com.google.api.gax.retrying.RetrySettings;
2221
import com.google.api.gax.retrying.RetryingExecutor;
@@ -29,13 +28,10 @@
2928
import com.google.cloud.pubsublite.proto.ComputeMessageStatsResponse;
3029
import com.google.cloud.pubsublite.proto.Cursor;
3130
import com.google.cloud.pubsublite.proto.TopicStatsServiceGrpc;
32-
import io.grpc.StatusException;
3331
import java.util.concurrent.Executors;
3432
import java.util.concurrent.ScheduledExecutorService;
35-
import java.util.concurrent.TimeUnit;
3633

37-
public class TopicStatsClientImpl implements BackgroundResource, TopicStatsClient {
38-
private final ExecutorAsBackgroundResource executorResource;
34+
public class TopicStatsClientImpl extends ApiResourceAggregation implements TopicStatsClient {
3935
private final CloudRegion region;
4036
private final TopicStatsServiceGrpc.TopicStatsServiceBlockingStub stub;
4137
private final RetryingExecutor<ComputeMessageStatsResponse> retryingExecutor;
@@ -57,7 +53,7 @@ private TopicStatsClientImpl(
5753
TopicStatsServiceGrpc.TopicStatsServiceBlockingStub stub,
5854
RetrySettings retrySettings,
5955
ScheduledExecutorService executor) {
60-
this.executorResource = new ExecutorAsBackgroundResource(executor);
56+
super(new ExecutorAsBackgroundResource(executor));
6157
this.region = region;
6258
this.stub = stub;
6359
this.retryingExecutor = RetryingExecutorUtil.retryingExecutor(retrySettings, executor);
@@ -68,55 +64,19 @@ public CloudRegion region() {
6864
return region;
6965
}
7066

71-
// BackgroundResource implementation.
72-
@Override
73-
public void shutdown() {
74-
executorResource.shutdown();
75-
}
76-
77-
@Override
78-
public boolean isShutdown() {
79-
return executorResource.isShutdown();
80-
}
81-
82-
@Override
83-
public boolean isTerminated() {
84-
return executorResource.isTerminated();
85-
}
86-
87-
@Override
88-
public void shutdownNow() {
89-
executorResource.shutdownNow();
90-
}
91-
92-
@Override
93-
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
94-
return executorResource.awaitTermination(duration, unit);
95-
}
96-
97-
@Override
98-
public void close() throws StatusException {
99-
try {
100-
executorResource.close();
101-
} catch (Exception e) {
102-
throw ExtractStatus.toCanonical(e);
103-
}
104-
}
105-
10667
// TopicStatsClient Implementation
10768
@Override
10869
public ApiFuture<ComputeMessageStatsResponse> computeMessageStats(
10970
TopicPath path, Partition partition, Offset start, Offset end) {
11071
return RetryingExecutorUtil.runWithRetries(
111-
() -> {
112-
return stub.computeMessageStats(
113-
ComputeMessageStatsRequest.newBuilder()
114-
.setTopic(ProjectLookupUtils.toCanonical(path).toString())
115-
.setPartition(partition.value())
116-
.setStartCursor(Cursor.newBuilder().setOffset(start.value()).build())
117-
.setEndCursor(Cursor.newBuilder().setOffset(end.value()).build())
118-
.build());
119-
},
72+
() ->
73+
stub.computeMessageStats(
74+
ComputeMessageStatsRequest.newBuilder()
75+
.setTopic(ProjectLookupUtils.toCanonical(path).toString())
76+
.setPartition(partition.value())
77+
.setStartCursor(Cursor.newBuilder().setOffset(start.value()).build())
78+
.setEndCursor(Cursor.newBuilder().setOffset(end.value()).build())
79+
.build()),
12080
retryingExecutor);
12181
}
12282
}

0 commit comments

Comments
 (0)