|
11 | 11 | import java.util.concurrent.LinkedBlockingQueue; |
12 | 12 | import org.slf4j.Logger; |
13 | 13 | import org.slf4j.LoggerFactory; |
14 | | -import s2.client.Client; |
| 14 | +import s2.channel.ManagedChannelFactory; |
| 15 | +import s2.client.StreamClient; |
15 | 16 | import s2.config.AppendRetryPolicy; |
16 | 17 | import s2.config.Config; |
17 | 18 | import s2.config.Endpoints; |
|
21 | 22 |
|
22 | 23 | public class ManagedAppendSessionDemo { |
23 | 24 |
|
24 | | - static class RandomASCIIStringGenerator { |
25 | | - private static final String ASCII_PRINTABLE_CHARACTERS = |
26 | | - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"; |
27 | | - |
28 | | - private static final Random RANDOM = new Random(); |
29 | | - |
30 | | - public static String generateRandomASCIIString(String prefix, int length) { |
31 | | - if (length < 0) { |
32 | | - throw new IllegalArgumentException("Length cannot be negative."); |
33 | | - } |
34 | | - |
35 | | - StringBuilder sb = new StringBuilder(length); |
36 | | - sb.append(prefix); |
37 | | - for (int i = 0; i < length - prefix.length(); i++) { |
38 | | - int index = RANDOM.nextInt(ASCII_PRINTABLE_CHARACTERS.length()); |
39 | | - sb.append(ASCII_PRINTABLE_CHARACTERS.charAt(index)); |
40 | | - } |
41 | | - return sb.toString(); |
42 | | - } |
43 | | - } |
44 | | - |
45 | 25 | private static final Logger logger = |
46 | 26 | LoggerFactory.getLogger(ManagedAppendSessionDemo.class.getName()); |
47 | 27 |
|
@@ -69,69 +49,88 @@ public static void main(String[] args) throws Exception { |
69 | 49 | final LinkedBlockingQueue<ListenableFuture<AppendOutput>> pendingAppends = |
70 | 50 | new LinkedBlockingQueue<>(); |
71 | 51 |
|
72 | | - var executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadScheduledExecutor()); |
73 | | - var consumer = |
74 | | - executor.submit( |
75 | | - () -> { |
76 | | - try { |
77 | | - while (true) { |
78 | | - var output = pendingAppends.take().get(); |
79 | | - if (output == null) { |
80 | | - logger.info("consumer closing"); |
81 | | - break; |
| 52 | + try (final var executor = |
| 53 | + MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(4)); |
| 54 | + final var channel = ManagedChannelFactory.forBasinOrStreamService(config, basinName)) { |
| 55 | + |
| 56 | + final var consumer = |
| 57 | + executor.submit( |
| 58 | + () -> { |
| 59 | + try { |
| 60 | + while (true) { |
| 61 | + var output = pendingAppends.take().get(); |
| 62 | + if (output == null) { |
| 63 | + logger.info("consumer closing"); |
| 64 | + break; |
| 65 | + } |
| 66 | + logger.info("consumer got: {}", output); |
82 | 67 | } |
83 | | - logger.info("consumer got: {}", output); |
| 68 | + } catch (Exception e) { |
| 69 | + logger.error("consumer failed", e); |
84 | 70 | } |
85 | | - } catch (Exception e) { |
86 | | - logger.error("consumer failed", e); |
87 | | - } |
88 | | - }); |
89 | | - |
90 | | - try (var client = new Client(config)) { |
91 | | - |
92 | | - final var streamClient = client.basinClient(basinName).streamClient(streamName); |
93 | | - final var futureAppendSession = streamClient.managedAppendSession(); |
94 | | - |
95 | | - for (var i = 0; i < 50_000; i++) { |
96 | | - try { |
97 | | - // Generate a record with approximately 10KiB of random text. |
98 | | - var payload = RandomASCIIStringGenerator.generateRandomASCIIString(i + " - ", 1024 * 10); |
99 | | - var append = |
100 | | - futureAppendSession.submit( |
101 | | - AppendInput.newBuilder() |
102 | | - .withRecords( |
103 | | - List.of( |
104 | | - AppendRecord.newBuilder() |
105 | | - .withBody(payload.getBytes(StandardCharsets.UTF_8)) |
106 | | - .build())) |
107 | | - .build(), |
108 | | - // Duration is how long we are willing to wait to receive a future. |
109 | | - Duration.ofSeconds(1)); |
110 | | - |
111 | | - pendingAppends.add(append); |
112 | | - } catch (RuntimeException e) { |
113 | | - logger.error("producer failed", e); |
114 | | - pendingAppends.add(Futures.immediateFailedFuture(e)); |
115 | | - break; |
| 71 | + }); |
| 72 | + |
| 73 | + final var streamClient = |
| 74 | + StreamClient.newBuilder(config, basinName, streamName) |
| 75 | + .withExecutor(executor) |
| 76 | + .withChannel(channel) |
| 77 | + .build(); |
| 78 | + |
| 79 | + try (final var futureAppendSession = streamClient.managedAppendSession()) { |
| 80 | + |
| 81 | + for (var i = 0; i < 50_000; i++) { |
| 82 | + try { |
| 83 | + // Generate a record with approximately 10KiB of random text. |
| 84 | + var payload = |
| 85 | + RandomASCIIStringGenerator.generateRandomASCIIString(i + " - ", 1024 * 10); |
| 86 | + var append = |
| 87 | + futureAppendSession.submit( |
| 88 | + AppendInput.newBuilder() |
| 89 | + .withRecords( |
| 90 | + List.of( |
| 91 | + AppendRecord.newBuilder() |
| 92 | + .withBody(payload.getBytes(StandardCharsets.UTF_8)) |
| 93 | + .build())) |
| 94 | + .build(), |
| 95 | + // Duration is how long we are willing to wait to receive a future. |
| 96 | + Duration.ofSeconds(10)); |
| 97 | + |
| 98 | + pendingAppends.add(append); |
| 99 | + } catch (RuntimeException e) { |
| 100 | + logger.error("producer failed", e); |
| 101 | + pendingAppends.add(Futures.immediateFailedFuture(e)); |
| 102 | + break; |
| 103 | + } |
116 | 104 | } |
| 105 | + |
| 106 | + logger.info("finished submitting all appends"); |
| 107 | + |
| 108 | + // Signal to the consumer that no further appends are happening. |
| 109 | + pendingAppends.add(Futures.immediateFuture(null)); |
117 | 110 | } |
118 | 111 |
|
119 | | - logger.info("finished submitting all appends"); |
| 112 | + consumer.get(); |
| 113 | + } |
| 114 | + } |
120 | 115 |
|
121 | | - // Signal to the consumer that no further appends are happening. |
122 | | - pendingAppends.add(Futures.immediateFuture(null)); |
| 116 | + static class RandomASCIIStringGenerator { |
| 117 | + private static final String ASCII_PRINTABLE_CHARACTERS = |
| 118 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"; |
| 119 | + |
| 120 | + private static final Random RANDOM = new Random(); |
123 | 121 |
|
124 | | - logger.info("starting graceful close"); |
125 | | - try { |
126 | | - futureAppendSession.closeGracefully().get(); |
127 | | - } catch (Exception e) { |
128 | | - logger.error("caught exception during close", e); |
| 122 | + public static String generateRandomASCIIString(String prefix, int length) { |
| 123 | + if (length < 0) { |
| 124 | + throw new IllegalArgumentException("Length cannot be negative."); |
129 | 125 | } |
130 | | - logger.info("finished closing"); |
131 | | - } |
132 | 126 |
|
133 | | - // Await the consumer future. |
134 | | - consumer.get(); |
135 | | - executor.shutdown(); |
| 127 | + StringBuilder sb = new StringBuilder(length); |
| 128 | + sb.append(prefix); |
| 129 | + for (int i = 0; i < length - prefix.length(); i++) { |
| 130 | + int index = RANDOM.nextInt(ASCII_PRINTABLE_CHARACTERS.length()); |
| 131 | + sb.append(ASCII_PRINTABLE_CHARACTERS.charAt(index)); |
| 132 | + } |
| 133 | + return sb.toString(); |
| 134 | + } |
136 | 135 | } |
137 | 136 | } |
0 commit comments