This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagedAppendSessionDemo.java
More file actions
148 lines (126 loc) · 5.14 KB
/
Copy pathManagedAppendSessionDemo.java
File metadata and controls
148 lines (126 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.example.app;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import s2.channel.ManagedChannelFactory;
import s2.client.StreamClient;
import s2.config.AppendRetryPolicy;
import s2.config.Config;
import s2.config.Endpoints;
import s2.types.AppendInput;
import s2.types.AppendOutput;
import s2.types.AppendRecord;
public class ManagedAppendSessionDemo {
private static final Logger logger =
LoggerFactory.getLogger(ManagedAppendSessionDemo.class.getName());
// 128KiB
private static final Integer TARGET_BATCH_SIZE = 128 * 1024;
public static void main(String[] args) throws Exception {
final var authToken = System.getenv("S2_ACCESS_TOKEN");
final var basinName = System.getenv("S2_BASIN");
final var streamName = System.getenv("S2_STREAM");
if (authToken == null) {
throw new IllegalStateException("S2_ACCESS_TOKEN not set");
}
if (basinName == null) {
throw new IllegalStateException("S2_BASIN not set");
}
if (streamName == null) {
throw new IllegalStateException("S2_STREAM not set");
}
var config =
Config.newBuilder(authToken)
.withEndpoints(Endpoints.fromEnvironment())
.withMaxAppendInflightBytes(1024 * 1024 * 50)
.withAppendRetryPolicy(AppendRetryPolicy.ALL)
.build();
final LinkedBlockingQueue<ListenableFuture<AppendOutput>> pendingAppends =
new LinkedBlockingQueue<>();
try (final var executor =
MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(4));
final var channel = ManagedChannelFactory.forBasinOrStreamService(config, basinName)) {
final var consumer =
executor.submit(
() -> {
try {
while (true) {
var output = pendingAppends.take().get();
if (output == null) {
logger.info("consumer closing");
break;
}
logger.info("consumer got: {}", output);
}
} catch (Exception e) {
logger.error("consumer failed", e);
}
});
final var streamClient =
StreamClient.newBuilder(config, basinName, streamName)
.withExecutor(executor)
.withChannel(channel)
.build();
try (final var futureAppendSession = streamClient.managedAppendSession()) {
for (var i = 0; i < 50_000; i++) {
try {
// Generate a record with approximately 10KiB of random text.
var payload =
RandomASCIIStringGenerator.generateRandomASCIIString(i + " - ", TARGET_BATCH_SIZE);
while (futureAppendSession.remainingBufferCapacityBytes()
< (TARGET_BATCH_SIZE + payload.length())) {
// Crude backpressure mechanism; slow down the rate of payload creation by sleeping
// momentarily
// if we have hit the internal append buffer max size.
Thread.sleep(10);
}
var append =
futureAppendSession.submit(
AppendInput.newBuilder()
.withRecords(
List.of(
AppendRecord.newBuilder()
.withBody(payload.getBytes(StandardCharsets.UTF_8))
.build()))
.build(),
// Duration is how long we are willing to wait to receive a future.
Duration.ofSeconds(10));
pendingAppends.add(append);
} catch (RuntimeException e) {
logger.error("producer failed", e);
pendingAppends.add(Futures.immediateFailedFuture(e));
break;
}
}
logger.info("finished submitting all appends");
// Signal to the consumer that no further appends are happening.
pendingAppends.add(Futures.immediateFuture(null));
}
consumer.get();
}
}
static class RandomASCIIStringGenerator {
private static final String ASCII_PRINTABLE_CHARACTERS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789";
private static final Random RANDOM = new Random();
public static String generateRandomASCIIString(String prefix, int length) {
if (length < 0) {
throw new IllegalArgumentException("Length cannot be negative.");
}
StringBuilder sb = new StringBuilder(length);
sb.append(prefix);
for (int i = 0; i < length - prefix.length(); i++) {
int index = RANDOM.nextInt(ASCII_PRINTABLE_CHARACTERS.length());
sb.append(ASCII_PRINTABLE_CHARACTERS.charAt(index));
}
return sb.toString();
}
}
}