Skip to content

Commit 0ee60eb

Browse files
fix: Move settings for the topic backlog reader to TopicBacklogReaderSettings (#254)
* Add an independent TopicBacklogReaderSettings * Delete Example.java
1 parent 0e20d80 commit 0ee60eb

File tree

4 files changed

+102
-32
lines changed

4 files changed

+102
-32
lines changed

pubsublite-beam-io/src/main/java/com/google/cloud/pubsublite/beam/PubsubLiteUnboundedSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public UnboundedReader<SequencedMessage> createReader(
102102
return new PubsubLiteUnboundedReader(
103103
this,
104104
statesBuilder.build(),
105-
subscriberOptions.topicBacklogReader(),
105+
TopicBacklogReader.create(subscriberOptions.topicBacklogReaderSettings()),
106106
Ticker.systemTicker());
107107
} catch (StatusException e) {
108108
throw new IOException(e);

pubsublite-beam-io/src/main/java/com/google/cloud/pubsublite/beam/SubscriberOptions.java

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@
1717
package com.google.cloud.pubsublite.beam;
1818

1919
import com.google.auto.value.AutoValue;
20-
import com.google.cloud.pubsublite.AdminClient;
21-
import com.google.cloud.pubsublite.AdminClientSettings;
2220
import com.google.cloud.pubsublite.Partition;
2321
import com.google.cloud.pubsublite.PartitionLookupUtils;
2422
import com.google.cloud.pubsublite.SubscriptionPath;
25-
import com.google.cloud.pubsublite.TopicPath;
2623
import com.google.cloud.pubsublite.cloudpubsub.FlowControlSettings;
27-
import com.google.cloud.pubsublite.internal.ExtractStatus;
28-
import com.google.cloud.pubsublite.internal.TopicStatsClient;
29-
import com.google.cloud.pubsublite.internal.TopicStatsClientSettings;
3024
import com.google.cloud.pubsublite.internal.wire.Committer;
3125
import com.google.cloud.pubsublite.internal.wire.CommitterBuilder;
3226
import com.google.cloud.pubsublite.internal.wire.PubsubContext;
@@ -40,7 +34,6 @@
4034
import com.google.common.collect.ImmutableSet;
4135
import io.grpc.StatusException;
4236
import java.io.Serializable;
43-
import java.util.concurrent.ExecutionException;
4437

4538
@AutoValue
4639
public abstract class SubscriberOptions implements Serializable {
@@ -57,8 +50,8 @@ public abstract class SubscriberOptions implements Serializable {
5750
/** A set of partitions. If empty, retrieve the set of partitions using an admin client. */
5851
public abstract ImmutableSet<Partition> partitions();
5952

60-
/** The class used to read backlog for the subscription described by subscriptionPath() */
61-
public abstract TopicBacklogReader topicBacklogReader();
53+
/** The class used to read backlog for the subscription described by subscriptionPath(). */
54+
public abstract TopicBacklogReaderSettings topicBacklogReaderSettings();
6255

6356
/** A supplier for the subscriber stub to be used. */
6457
public abstract Optional<SerializableSupplier<SubscriberServiceStub>> subscriberStubSupplier();
@@ -143,7 +136,8 @@ public abstract Builder setSubscriberStubSupplier(
143136
public abstract Builder setCommitterStubSupplier(
144137
SerializableSupplier<CursorServiceStub> stubSupplier);
145138

146-
public abstract Builder setTopicBacklogReader(TopicBacklogReader topicBacklogReader);
139+
public abstract Builder setTopicBacklogReaderSettings(
140+
TopicBacklogReaderSettings topicBacklogReaderSettings);
147141

148142
// Used in unit tests
149143
abstract Builder setSubscriberFactory(SubscriberFactory subscriberFactory);
@@ -155,41 +149,29 @@ public abstract Builder setCommitterStubSupplier(
155149

156150
abstract ImmutableSet<Partition> partitions();
157151

158-
abstract Optional<TopicBacklogReader> topicBacklogReader();
152+
abstract Optional<TopicBacklogReaderSettings> topicBacklogReaderSettings();
159153

160154
abstract SubscriberOptions autoBuild();
161155

162156
public SubscriberOptions build() throws StatusException {
163-
if (!partitions().isEmpty() && topicBacklogReader().isPresent()) {
157+
if (!partitions().isEmpty() && topicBacklogReaderSettings().isPresent()) {
164158
return autoBuild();
165159
}
166-
TopicPath path;
167-
try (AdminClient adminClient =
168-
AdminClient.create(
169-
AdminClientSettings.newBuilder()
170-
.setRegion(subscriptionPath().location().region())
171-
.build())) {
172-
path = TopicPath.parse(adminClient.getSubscription(subscriptionPath()).get().getTopic());
173-
} catch (ExecutionException e) {
174-
throw ExtractStatus.toCanonical(e.getCause());
175-
} catch (Throwable t) {
176-
throw ExtractStatus.toCanonical(t);
177-
}
178160

179161
if (partitions().isEmpty()) {
180-
int partition_count = PartitionLookupUtils.numPartitions(path);
162+
int partitionCount = PartitionLookupUtils.numPartitions(subscriptionPath());
181163
ImmutableSet.Builder<Partition> partitions = ImmutableSet.builder();
182-
for (int i = 0; i < partition_count; i++) {
164+
for (int i = 0; i < partitionCount; i++) {
183165
partitions.add(Partition.of(i));
184166
}
185167
setPartitions(partitions.build());
186168
}
187-
if (!topicBacklogReader().isPresent()) {
188-
setTopicBacklogReader(
189-
new TopicBacklogReaderImpl(
190-
TopicStatsClient.create(TopicStatsClientSettings.newBuilder().build()), path));
169+
if (!topicBacklogReaderSettings().isPresent()) {
170+
setTopicBacklogReaderSettings(
171+
TopicBacklogReaderSettings.newBuilder()
172+
.setTopicPathFromSubscriptionPath(subscriptionPath())
173+
.build());
191174
}
192-
193175
return autoBuild();
194176
}
195177
}

pubsublite-beam-io/src/main/java/com/google/cloud/pubsublite/beam/TopicBacklogReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.cloud.pubsublite.Offset;
2020
import com.google.cloud.pubsublite.Partition;
2121
import com.google.cloud.pubsublite.proto.ComputeMessageStatsResponse;
22+
import io.grpc.StatusException;
2223
import java.util.Map;
2324

2425
/**
@@ -27,6 +28,10 @@
2728
* partitions within a subscription.
2829
*/
2930
public interface TopicBacklogReader {
31+
/** Create a TopicBacklogReader from settings. */
32+
static TopicBacklogReader create(TopicBacklogReaderSettings settings) throws StatusException {
33+
return settings.instantiate();
34+
}
3035
/**
3136
* Compute and aggregate message statistics for message between the provided start offset and HEAD
3237
* for each partition.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.beam;
18+
19+
import com.google.auto.value.AutoValue;
20+
import com.google.cloud.pubsublite.AdminClient;
21+
import com.google.cloud.pubsublite.AdminClientSettings;
22+
import com.google.cloud.pubsublite.SubscriptionPath;
23+
import com.google.cloud.pubsublite.TopicPath;
24+
import com.google.cloud.pubsublite.internal.ExtractStatus;
25+
import com.google.cloud.pubsublite.internal.TopicStatsClient;
26+
import com.google.cloud.pubsublite.internal.TopicStatsClientSettings;
27+
import com.google.cloud.pubsublite.proto.TopicStatsServiceGrpc.TopicStatsServiceBlockingStub;
28+
import com.google.common.base.Optional;
29+
import io.grpc.StatusException;
30+
import java.io.Serializable;
31+
import java.util.concurrent.ExecutionException;
32+
33+
@AutoValue
34+
public abstract class TopicBacklogReaderSettings implements Serializable {
35+
/**
36+
* The topic path for this backlog reader. Either topicPath or subscriptionPath must be set. If
37+
* both are set, subscriptionPath will be ignored.
38+
*/
39+
abstract TopicPath topicPath();
40+
41+
// Optional parameters
42+
abstract Optional<SerializableSupplier<TopicStatsServiceBlockingStub>> stub();
43+
44+
public static Builder newBuilder() {
45+
return new AutoValue_TopicBacklogReaderSettings.Builder();
46+
}
47+
48+
@AutoValue.Builder
49+
public abstract static class Builder {
50+
// Required parameters.
51+
public abstract Builder setTopicPath(TopicPath topicPath);
52+
53+
public Builder setTopicPathFromSubscriptionPath(SubscriptionPath subscriptionPath)
54+
throws StatusException {
55+
try (AdminClient adminClient =
56+
AdminClient.create(
57+
AdminClientSettings.newBuilder()
58+
.setRegion(subscriptionPath.location().region())
59+
.build())) {
60+
setTopicPath(
61+
TopicPath.parse(adminClient.getSubscription(subscriptionPath).get().getTopic()));
62+
return this;
63+
} catch (ExecutionException e) {
64+
throw ExtractStatus.toCanonical(e.getCause());
65+
} catch (Throwable t) {
66+
throw ExtractStatus.toCanonical(t);
67+
}
68+
}
69+
70+
public abstract Builder setStub(SerializableSupplier<TopicStatsServiceBlockingStub> stub);
71+
72+
public abstract TopicBacklogReaderSettings build();
73+
}
74+
75+
TopicBacklogReader instantiate() throws StatusException {
76+
TopicStatsClientSettings.Builder builder = TopicStatsClientSettings.newBuilder();
77+
if (stub().isPresent()) {
78+
builder.setStub(stub().get().get());
79+
}
80+
builder.setRegion(topicPath().location().region());
81+
return new TopicBacklogReaderImpl(TopicStatsClient.create(builder.build()), topicPath());
82+
}
83+
}

0 commit comments

Comments
 (0)