Skip to content

Commit eefe717

Browse files
Revert "samples: update samples for automatic subscriber assignment (#189)" (#198)
This reverts commit 945f775.
1 parent 945f775 commit eefe717

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

.readme-partials.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ custom_content: |
4040
RetentionConfig.newBuilder()
4141
// How long messages are retained.
4242
.setPeriod(Durations.fromDays(1))
43-
// Set storage per partition to 30 GiB. This must be 30 GiB-10 TiB.
43+
// Set storage per partition to 100 GiB. This must be 30 GiB-10 TiB.
4444
// If the number of bytes stored in any of the topic's partitions grows
4545
// beyond this value, older messages will be dropped to make room for
4646
// newer ones, regardless of the value of `period`.
47-
.setPerPartitionBytes(30 * 1024 * 1024 * 1024L))
47+
.setPerPartitionBytes(100 * 1024 * 1024 * 1024L))
4848
.setName(topicPath.value())
4949
.build();
5050
@@ -210,6 +210,7 @@ custom_content: |
210210
String topicId = "your-topic-id";
211211
// Choose an existing subscription.
212212
String subscriptionId = "your-subscription-id";
213+
List<Integer> partitionNumbers = ImmutableList.of(0);
213214
214215
SubscriptionPath subscriptionPath =
215216
SubscriptionPaths.newBuilder()
@@ -228,6 +229,11 @@ custom_content: |
228229
.setMessagesOutstanding(1000L)
229230
.build();
230231
232+
List<Partition> partitions = new ArrayList<>();
233+
for (Integer num : partitionNumbers) {
234+
partitions.add(Partition.of(num));
235+
}
236+
231237
MessageReceiver receiver =
232238
(PubsubMessage message, AckReplyConsumer consumer) -> {
233239
System.out.println("Id : " + message.getMessageId());
@@ -238,6 +244,7 @@ custom_content: |
238244
SubscriberSettings subscriberSettings =
239245
SubscriberSettings.newBuilder()
240246
.setSubscriptionPath(subscriptionPath)
247+
.setPartitions(partitions)
241248
.setReceiver(receiver)
242249
// Flow control settings are set at the partition level.
243250
.setPerPartitionFlowControlSettings(flowControlSettings)
@@ -251,11 +258,10 @@ custom_content: |
251258
System.out.println("Listening to messages on " + subscriptionPath.value() + "...");
252259
253260
try {
254-
System.out.println(subscriber.state());
255-
// Wait 90 seconds for the subscriber to reach TERMINATED state. If it encounters
261+
// Wait 30 seconds for the subscriber to reach TERMINATED state. If it encounters
256262
// unrecoverable errors before then, its state will change to FAILED and an
257263
// IllegalStateException will be thrown.
258-
subscriber.awaitTerminated(90, TimeUnit.SECONDS);
264+
subscriber.awaitTerminated(30, TimeUnit.SECONDS);
259265
} catch (TimeoutException t) {
260266
// Shut down the subscriber. This will change the state of the subscriber to TERMINATED.
261267
subscriber.stopAsync().awaitTerminated();

samples/snippets/src/main/java/pubsublite/CreateTopicExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void main(String... args) throws Exception {
3838
char zoneId = 'b';
3939
String topicId = "your-topic-id";
4040
long projectNumber = Long.parseLong("123456789");
41-
int partitions = 1;
41+
Integer partitions = 1;
4242

4343
createTopicExample(cloudRegion, zoneId, projectNumber, topicId, partitions);
4444
}
@@ -67,11 +67,11 @@ public static void createTopicExample(
6767
RetentionConfig.newBuilder()
6868
// How long messages are retained.
6969
.setPeriod(Durations.fromDays(1))
70-
// Set storage per partition to 30 GiB. This must be 30 GiB-10 TiB.
70+
// Set storage per partition to 100 GiB. This must be 30 GiB-10 TiB.
7171
// If the number of bytes stored in any of the topic's partitions grows
7272
// beyond this value, older messages will be dropped to make room for
7373
// newer ones, regardless of the value of `period`.
74-
.setPerPartitionBytes(30 * 1024 * 1024 * 1024L))
74+
.setPerPartitionBytes(100 * 1024 * 1024 * 1024L))
7575
.setName(topicPath.value())
7676
.build();
7777

samples/snippets/src/main/java/pubsublite/SubscriberExample.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,19 @@ public static void main(String... args) throws Exception {
4646
// Choose an existing subscription for the subscribe example to work.
4747
String subscriptionId = "your-subscription-id";
4848
long projectNumber = Long.parseLong("123456789");
49+
// List of partitions to subscribe to. It can be all the partitions in a topic or
50+
// a subset of them. A topic of N partitions has partition numbers [0~N-1].
51+
List<Integer> partitionNumbers = ImmutableList.of(0);
4952

50-
subscriberExample(cloudRegion, zoneId, projectNumber, subscriptionId);
53+
subscriberExample(cloudRegion, zoneId, projectNumber, subscriptionId, partitionNumbers);
5154
}
5255

5356
public static void subscriberExample(
5457
String cloudRegion,
5558
char zoneId,
5659
long projectNumber,
57-
String subscriptionId)
60+
String subscriptionId,
61+
List<Integer> partitionNumbers)
5862
throws StatusException {
5963

6064
SubscriptionPath subscriptionPath =
@@ -74,6 +78,11 @@ public static void subscriberExample(
7478
.setMessagesOutstanding(1000L)
7579
.build();
7680

81+
List<Partition> partitions = new ArrayList<>();
82+
for (Integer num : partitionNumbers) {
83+
partitions.add(Partition.of(num));
84+
}
85+
7786
MessageReceiver receiver =
7887
(PubsubMessage message, AckReplyConsumer consumer) -> {
7988
System.out.println("Id : " + message.getMessageId());
@@ -84,6 +93,7 @@ public static void subscriberExample(
8493
SubscriberSettings subscriberSettings =
8594
SubscriberSettings.newBuilder()
8695
.setSubscriptionPath(subscriptionPath)
96+
.setPartitions(partitions)
8797
.setReceiver(receiver)
8898
// Flow control settings are set at the partition level.
8999
.setPerPartitionFlowControlSettings(flowControlSettings)
@@ -97,11 +107,10 @@ public static void subscriberExample(
97107
System.out.println("Listening to messages on " + subscriptionPath.value() + "...");
98108

99109
try {
100-
System.out.println(subscriber.state());
101-
// Wait 90 seconds for the subscriber to reach TERMINATED state. If it encounters
110+
// Wait 30 seconds for the subscriber to reach TERMINATED state. If it encounters
102111
// unrecoverable errors before then, its state will change to FAILED and an
103112
// IllegalStateException will be thrown.
104-
subscriber.awaitTerminated(90, TimeUnit.SECONDS);
113+
subscriber.awaitTerminated(30, TimeUnit.SECONDS);
105114
} catch (TimeoutException t) {
106115
// Shut down the subscriber. This will change the state of the subscriber to TERMINATED.
107116
subscriber.stopAsync().awaitTerminated();

samples/snippets/src/test/java/pubsublite/QuickStartIT.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import com.google.common.collect.ImmutableList;
2323
import java.io.ByteArrayOutputStream;
2424
import java.io.PrintStream;
25-
import java.util.Arrays;
2625
import java.util.List;
27-
import java.util.Random;
2826
import java.util.UUID;
2927
import org.junit.After;
3028
import org.junit.Before;
@@ -37,21 +35,18 @@ public class QuickStartIT {
3735

3836
private ByteArrayOutputStream bout;
3937
private PrintStream out;
40-
private Random rand = new Random();
41-
private List<String> cloudRegions =
42-
Arrays.asList(
43-
"us-central1", "europe-north1", "asia-east1", "australia-southeast1", "asia-northeast2");
4438

4539
private static final String GOOGLE_CLOUD_PROJECT_NUMBER =
4640
System.getenv("GOOGLE_CLOUD_PROJECT_NUMBER");
47-
private String CLOUD_REGION = cloudRegions.get(rand.nextInt(cloudRegions.size()));
41+
private static final String CLOUD_REGION = "us-central1";
4842
private static final char ZONE_ID = 'b';
4943
private static final Long PROJECT_NUMBER = Long.parseLong(GOOGLE_CLOUD_PROJECT_NUMBER);
5044
private static final String SUFFIX = UUID.randomUUID().toString();
5145
private static final String TOPIC_NAME = "lite-topic-" + SUFFIX;
5246
private static final String SUBSCRIPTION_NAME = "lite-subscription-" + SUFFIX;
53-
private static final int PARTITIONS = 2;
54-
private static final int MESSAGE_COUNT = 10;
47+
private static final int PARTITIONS = 1;
48+
private static final int MESSAGE_COUNT = 1;
49+
private static final List<Integer> PARTITION_NOS = ImmutableList.of(0);
5550

5651
private static void requireEnvVar(String varName) {
5752
assertNotNull(
@@ -89,7 +84,7 @@ public void testQuickstart() throws Exception {
8984
// Get a topic.
9085
GetTopicExample.getTopicExample(CLOUD_REGION, ZONE_ID, PROJECT_NUMBER, TOPIC_NAME);
9186
assertThat(bout.toString()).contains(TOPIC_NAME);
92-
assertThat(bout.toString()).contains(String.format("%s partition(s).", PARTITIONS));
87+
assertThat(bout.toString()).contains("1 partition(s).");
9388

9489
bout.reset();
9590
// List topics.
@@ -162,11 +157,9 @@ public void testQuickstart() throws Exception {
162157
bout.reset();
163158
// Subscribe.
164159
SubscriberExample.subscriberExample(
165-
CLOUD_REGION, ZONE_ID, PROJECT_NUMBER, SUBSCRIPTION_NAME);
160+
CLOUD_REGION, ZONE_ID, PROJECT_NUMBER, SUBSCRIPTION_NAME, PARTITION_NOS);
166161
assertThat(bout.toString()).contains("Listening");
167-
for (int i = 0; i < MESSAGE_COUNT; ++i) {
168-
assertThat(bout.toString()).contains(String.format("Data : message-%s", i));
169-
}
162+
assertThat(bout.toString()).contains("Data : message-0");
170163
assertThat(bout.toString()).contains("Subscriber is shut down: TERMINATED");
171164

172165
bout.reset();

0 commit comments

Comments
 (0)