-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducer.java
More file actions
306 lines (267 loc) · 11.4 KB
/
Copy pathProducer.java
File metadata and controls
306 lines (267 loc) · 11.4 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package com.danubemessaging.client;
import com.danubemessaging.client.errors.DanubeClientException;
import com.danubemessaging.client.internal.producer.TopicProducer;
import com.danubemessaging.client.internal.retry.RetryManager;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* Sends messages to a Danube topic.
*
* <p>Obtain an instance via {@link DanubeClient#newProducer()}. Call {@link #create()} before
* sending messages. For partitioned topics, partitions are discovered automatically or set
* explicitly via {@link ProducerBuilder#withPartitions(int)}.
*
* <p>This class is thread-safe.
*/
public final class Producer {
private enum LifecycleState {
NEW,
CREATED,
CLOSED
}
private final DanubeClient client;
private final ProducerOptions options;
private final List<TopicProducer> topicProducers = new ArrayList<>();
private final AtomicInteger nextPartition = new AtomicInteger();
private final AtomicReference<LifecycleState> lifecycleState = new AtomicReference<>(LifecycleState.NEW);
Producer(DanubeClient client, ProducerOptions options) {
this.client = Objects.requireNonNull(client, "client");
this.options = Objects.requireNonNull(options, "options");
}
/** Returns the options this producer was built with. */
public ProducerOptions options() {
return options;
}
/**
* Registers the producer on the broker asynchronously.
* Equivalent to calling {@link #create()} on the IO executor.
*
* @return a future that completes when the producer is registered
*/
public CompletableFuture<Void> createAsync() {
return CompletableFuture.runAsync(this::create, client.ioExecutor());
}
/**
* Registers the producer on the broker.
* Must be called before {@link #send(byte[], java.util.Map)}.
* Idempotent — calling twice on an already-created producer is a no-op.
*
* @throws com.danubemessaging.client.errors.DanubeClientException if registration fails
*/
public synchronized void create() {
ensureOpen();
if (lifecycleState.get() == LifecycleState.CREATED) {
return;
}
List<String> targets;
if (options.partitions() > 0) {
targets = new ArrayList<>(options.partitions());
for (int i = 0; i < options.partitions(); i++) {
targets.add(options.topic() + "-part-" + i);
}
} else {
List<String> partitions = client.lookupService().topicPartitions(client.serviceUri(), options.topic());
targets = partitions.isEmpty() ? List.of(options.topic()) : partitions;
}
SchemaRegistryClient schemaRegistry = options.schemaReference() != null
? client.newSchemaRegistry()
: null;
RetryManager retryManager = hasCustomRetryOptions()
? new RetryManager(options.maxRetries(), options.baseBackoffMs(), options.maxBackoffMs())
: client.retryManager();
try {
for (int i = 0; i < targets.size(); i++) {
String partitionTopic = targets.get(i);
String partitionProducerName = targets.size() == 1
? options.producerName()
: options.producerName() + "-" + i;
TopicProducer topicProducer = new TopicProducer(
client.serviceUri(),
client.connectionManager(),
client.lookupService(),
client.authService(),
client.healthCheckService(),
schemaRegistry,
retryManager,
options,
partitionTopic,
partitionProducerName);
try {
topicProducer.create();
} catch (RuntimeException error) {
notifyProducerError(topicProducer, error, false);
throw error;
}
topicProducers.add(topicProducer);
}
} catch (RuntimeException error) {
topicProducers.forEach(TopicProducer::close);
topicProducers.clear();
throw error;
}
lifecycleState.set(LifecycleState.CREATED);
}
/**
* Sends a message asynchronously.
*
* @param payload message body (may be empty but not null)
* @param attributes optional key-value metadata attached to the message
* @return a future resolving to the broker-assigned message sequence ID
*/
public CompletableFuture<Long> sendAsync(byte[] payload, Map<String, String> attributes) {
byte[] payloadCopy = payload == null ? new byte[0] : payload.clone();
Map<String, String> attr = attributes == null ? Map.of() : Map.copyOf(attributes);
return CompletableFuture.supplyAsync(() -> send(payloadCopy, attr), client.ioExecutor());
}
/**
* Sends a message and blocks until the broker acknowledges receipt.
*
* @param payload message body (may be empty but not null)
* @param attributes optional key-value metadata; pass {@code Map.of()} for none
* @return the broker-assigned message sequence ID
* @throws com.danubemessaging.client.errors.DanubeClientException on unrecoverable error
*/
public long send(byte[] payload, Map<String, String> attributes) {
return sendInternal(payload, attributes, null, selectTopicProducer());
}
/**
* Sends a message with a routing key asynchronously for KEY_SHARED subscriptions.
*
* @param payload message body
* @param attributes optional metadata
* @param routingKey the routing key; all messages with the same key go to the same consumer
* @return a future resolving to the broker-assigned message sequence ID
*/
public CompletableFuture<Long> sendWithKeyAsync(byte[] payload, Map<String, String> attributes,
String routingKey) {
byte[] payloadCopy = payload == null ? new byte[0] : payload.clone();
Map<String, String> attr = attributes == null ? Map.of() : Map.copyOf(attributes);
return CompletableFuture.supplyAsync(() -> sendWithKey(payloadCopy, attr, routingKey), client.ioExecutor());
}
/**
* Sends a message with a routing key for KEY_SHARED subscriptions.
*
* <p>For partitioned topics, hashes the routing key to a specific partition ensuring
* all messages with the same key always go to the same partition. For non-partitioned
* topics, simply tags the routing key on the message.
*
* @param payload message body
* @param attributes optional metadata; pass {@code Map.of()} for none
* @param routingKey the routing key; must not be null
* @return the broker-assigned message sequence ID
* @throws com.danubemessaging.client.errors.DanubeClientException on unrecoverable error
*/
public long sendWithKey(byte[] payload, Map<String, String> attributes, String routingKey) {
ensureOpen();
TopicProducer topicProducer = selectTopicProducerForKey(routingKey);
return sendInternal(payload, attributes, routingKey, topicProducer);
}
private long sendInternal(byte[] payload, Map<String, String> attributes,
String routingKey, TopicProducer topicProducer) {
ensureOpen();
if (lifecycleState.get() != LifecycleState.CREATED) {
create();
}
int attempts = 0;
while (true) {
try {
return topicProducer.send(payload, attributes, routingKey);
} catch (RuntimeException error) {
boolean unrecoverable = client.retryManager().isUnrecoverable(error);
if (unrecoverable) {
notifyProducerError(topicProducer, error, true);
topicProducer.relookupAndCreate();
attempts = 0;
continue;
}
boolean retryable = client.retryManager().isRetryable(error);
if (!retryable) {
notifyProducerError(topicProducer, error, false);
throw error;
}
notifyProducerError(topicProducer, error, true);
attempts++;
if (attempts > client.retryManager().maxRetries()) {
topicProducer.relookupAndCreate();
attempts = 0;
continue;
}
sleepBackoff(client.retryManager().calculateBackoff(attempts - 1));
}
}
}
/**
* Closes this producer and releases all underlying resources.
* Idempotent — safe to call multiple times.
*/
public synchronized void close() {
if (lifecycleState.get() == LifecycleState.CLOSED) {
return;
}
lifecycleState.set(LifecycleState.CLOSED);
topicProducers.forEach(TopicProducer::close);
topicProducers.clear();
}
private void ensureOpen() {
if (lifecycleState.get() == LifecycleState.CLOSED) {
throw new DanubeClientException("Producer is closed");
}
}
private void notifyProducerError(TopicProducer topicProducer, Throwable error, boolean retryable) {
try {
options.eventListener().onProducerError(topicProducer.topic(), topicProducer.producerName(), error,
retryable);
} catch (RuntimeException ignore) {
// Listener errors must never break client flow.
}
}
private TopicProducer selectTopicProducer() {
if (topicProducers.isEmpty()) {
throw new DanubeClientException("Producer is not initialized");
}
if (topicProducers.size() == 1) {
return topicProducers.get(0);
}
int index = Math.floorMod(nextPartition.getAndIncrement(), topicProducers.size());
return topicProducers.get(index);
}
private TopicProducer selectTopicProducerForKey(String routingKey) {
if (topicProducers.isEmpty()) {
throw new DanubeClientException("Producer is not initialized");
}
if (topicProducers.size() == 1) {
return topicProducers.get(0);
}
int index = Math.floorMod((int) fnv1aHash(routingKey), topicProducers.size());
return topicProducers.get(index);
}
/**
* FNV-1a 64-bit hash — must match Rust/Go/Python constants.
*/
static long fnv1aHash(String key) {
long hash = 0xcbf29ce484222325L;
byte[] bytes = key.getBytes(java.nio.charset.StandardCharsets.UTF_8);
for (byte b : bytes) {
hash ^= (b & 0xFF);
hash *= 0x100000001b3L;
}
return hash;
}
private boolean hasCustomRetryOptions() {
return options.maxRetries() > 0 || options.baseBackoffMs() > 0 || options.maxBackoffMs() > 0;
}
private static void sleepBackoff(Duration backoff) {
try {
Thread.sleep(backoff);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
throw new DanubeClientException("Interrupted while backing off for retry", interrupted);
}
}
}