Repository navigation
Expand file tree
/
Copy pathExpExactlyOnce.java
More file actions
279 lines (235 loc) · 13.8 KB
/
Copy pathExpExactlyOnce.java
File metadata and controls
279 lines (235 loc) · 13.8 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
import java.util.*;
/**
* Simulación de exactly-once semantics en Kafka con Java puro.
*
* Niveles de garantía demostrados:
* - At-least-once: puede haber duplicados si el producer hace retry tras un fallo
* - Idempotent producer: el broker deduplicaba por (producerId, partition, sequenceNumber)
* - Exactly-once (transaccional): produce + commitOffset en una transacción atómica
*
* Patrón read-process-write: consume de topicA, procesa, produce a topicB,
* commit de offset — todo atómicamente.
*/
public class ExpExactlyOnce {
// ─────────────────────────────────────────────
// BROKER SIMULADO con deduplicación por sequence
// ─────────────────────────────────────────────
static class Broker {
// topicPartition → lista de mensajes recibidos
private final Map<String, List<String>> topics = new LinkedHashMap<>();
// (producerId, topicPartition) → último sequence aceptado
private final Map<String, Integer> sequenceTracker = new HashMap<>();
// transactionId → estado de la transacción en curso
private final Map<String, String> transacciones = new HashMap<>();
void publicar(String topicPartition, String mensaje) {
topics.computeIfAbsent(topicPartition, k -> new ArrayList<>()).add(mensaje);
}
// Publicación idempotente: el broker rechaza si ya vio este (producerId, seq)
boolean publicarIdempotente(String producerId, String topicPartition,
int sequenceNum, String mensaje) {
String clave = producerId + "|" + topicPartition;
Integer ultimoSeq = sequenceTracker.get(clave);
if (ultimoSeq != null && sequenceNum <= ultimoSeq) {
// Duplicado detectado: ya se procesó este sequence
System.out.printf(" [Broker] DUPLICADO IGNORADO pid='%s' seq=%d (último aceptado=%d) → '%s'%n",
producerId, sequenceNum, ultimoSeq, mensaje);
return false;
}
// Sequence número fuera de orden: gap demasiado grande
if (ultimoSeq != null && sequenceNum > ultimoSeq + 1) {
System.out.printf(" [Broker] ERROR: gap en sequence pid='%s' esperado=%d recibido=%d%n",
producerId, ultimoSeq + 1, sequenceNum);
return false;
}
sequenceTracker.put(clave, sequenceNum);
topics.computeIfAbsent(topicPartition, k -> new ArrayList<>()).add(mensaje);
System.out.printf(" [Broker] ACEPTADO pid='%s' seq=%d → '%s' en '%s'%n",
producerId, sequenceNum, mensaje, topicPartition);
return true;
}
// Operaciones transaccionales
void beginTransaction(String transactionId) {
transacciones.put(transactionId, "OPEN");
System.out.printf(" [Broker] TX '%s' BEGIN%n", transactionId);
}
// Commit: hace efectivas todas las escrituras del batch transaccional
void commitTransaction(String transactionId, List<String[]> writes, Map<String, Integer> offsets) {
if (!"OPEN".equals(transacciones.get(transactionId))) {
throw new IllegalStateException("No hay transacción abierta: " + transactionId);
}
// Aplicar escrituras atómicamente
for (String[] w : writes) {
topics.computeIfAbsent(w[0], k -> new ArrayList<>()).add(w[1]);
System.out.printf(" [Broker] TX WRITE → '%s': %s%n", w[0], w[1]);
}
// Commit de offsets junto con las escrituras (atómico)
offsets.forEach((group, offset) ->
System.out.printf(" [Broker] TX COMMIT OFFSET group='%s' offset=%d%n", group, offset));
transacciones.put(transactionId, "COMMITTED");
System.out.printf(" [Broker] TX '%s' COMMITTED ✓%n", transactionId);
}
void abortTransaction(String transactionId) {
transacciones.put(transactionId, "ABORTED");
System.out.printf(" [Broker] TX '%s' ABORTED — ninguna escritura es visible%n", transactionId);
}
void mostrarTopics() {
System.out.println(" Estado de topics en el broker:");
if (topics.isEmpty()) {
System.out.println(" (vacío)");
return;
}
topics.forEach((topic, msgs) ->
System.out.printf(" %-25s → %s%n", topic, msgs));
}
}
// ─────────────────────────────────────────────
// AT-LEAST-ONCE PRODUCER: simula duplicados en retry
// ─────────────────────────────────────────────
static class AtLeastOnceProducer {
private final Broker broker;
private final String producerId;
AtLeastOnceProducer(Broker broker, String producerId) {
this.broker = broker;
this.producerId = producerId;
}
// Simula crash: el ACK del broker se pierde y el producer reintenta
void enviarConFallo(String topic, String mensaje) {
System.out.printf(" [AtLeastOnce '%s'] Enviando '%s'...%n", producerId, mensaje);
broker.publicar(topic, mensaje); // llega al broker
// Simula: ACK se pierde en la red → producer no sabe si llegó
System.out.printf(" [AtLeastOnce '%s'] ACK perdido — reintentando...%n", producerId);
broker.publicar(topic, mensaje); // DUPLICADO: el broker lo acepta de nuevo
System.out.printf(" [AtLeastOnce '%s'] Reintento enviado (posible duplicado)%n", producerId);
}
}
// ─────────────────────────────────────────────
// IDEMPOTENT PRODUCER: evita duplicados con sequence numbers
// ─────────────────────────────────────────────
static class IdempotentProducer {
private final Broker broker;
private final String producerId;
// partition → próximo sequence number
private final Map<String, Integer> sequences = new HashMap<>();
IdempotentProducer(Broker broker, String producerId) {
this.broker = broker;
this.producerId = producerId;
}
// Asigna sequence number único por (producerId, topicPartition)
void enviar(String topicPartition, String mensaje) {
int seq = sequences.merge(topicPartition, 0, (old, zero) -> old + 1);
System.out.printf(" [Idempotent '%s'] Enviando seq=%d '%s'...%n",
producerId, seq, mensaje);
broker.publicarIdempotente(producerId, topicPartition, seq, mensaje);
}
// Simula crash y retry: mismo mensaje con mismo sequence → broker deduplica
void enviarConFalloYRetry(String topicPartition, String mensaje) {
int seq = sequences.merge(topicPartition, 0, (old, zero) -> old + 1);
System.out.printf(" [Idempotent '%s'] Enviando seq=%d '%s'...%n",
producerId, seq, mensaje);
broker.publicarIdempotente(producerId, topicPartition, seq, mensaje);
// ACK perdido → retry con el MISMO sequence number
System.out.printf(" [Idempotent '%s'] ACK perdido — reintentando con mismo seq=%d...%n",
producerId, seq);
// El broker rechaza: ya procesó este sequence
broker.publicarIdempotente(producerId, topicPartition, seq, mensaje);
}
}
// ─────────────────────────────────────────────
// TRANSACTIONAL PRODUCER: exactly-once con transacciones
// ─────────────────────────────────────────────
static class TransactionalProducer {
private final Broker broker;
private final String transactionId;
private final List<String[]> writes = new ArrayList<>(); // [topic, mensaje]
private final Map<String, Integer> offsetsACommitar = new LinkedHashMap<>();
private boolean txAbierta = false;
TransactionalProducer(Broker broker, String transactionId) {
this.broker = broker;
this.transactionId = transactionId;
}
void beginTransaction() {
writes.clear();
offsetsACommitar.clear();
txAbierta = true;
broker.beginTransaction(transactionId);
}
// Produce dentro de la transacción (buffered, no visible hasta commit)
void send(String topic, String mensaje) {
if (!txAbierta) throw new IllegalStateException("No hay transacción activa");
writes.add(new String[]{topic, mensaje});
System.out.printf(" [Transactional] Buffered → '%s': %s%n", topic, mensaje);
}
// Incluir commit de offset en la misma transacción (read-process-write atómico)
void sendOffsetsToTransaction(String consumerGroup, int offset) {
offsetsACommitar.put(consumerGroup, offset);
System.out.printf(" [Transactional] Buffered offset commit → group='%s' offset=%d%n",
consumerGroup, offset);
}
void commitTransaction() {
broker.commitTransaction(transactionId, writes, offsetsACommitar);
txAbierta = false;
}
void abortTransaction() {
broker.abortTransaction(transactionId);
writes.clear();
offsetsACommitar.clear();
txAbierta = false;
}
}
// ─────────────────────────────────────────────
// MAIN
// ─────────────────────────────────────────────
public static void main(String[] args) {
System.out.println("═".repeat(65));
System.out.println(" EXACTLY-ONCE SEMANTICS — Java puro");
System.out.println("═".repeat(65));
// ── Fase 1: At-least-once — duplicados visibles ────────────────
System.out.println("\n─ FASE 1: At-least-once (duplicados con retry) ─");
Broker broker1 = new Broker();
AtLeastOnceProducer aloProducer = new AtLeastOnceProducer(broker1, "producer-1");
aloProducer.enviarConFallo("pedidos-P0", "pedido-AAA");
aloProducer.enviarConFallo("pedidos-P0", "pedido-BBB");
System.out.println();
broker1.mostrarTopics();
System.out.println(" → pedido-AAA y pedido-BBB aparecen 2 veces cada uno (duplicados)");
// ── Fase 2: Idempotent producer — deduplicación por sequence ──
System.out.println("\n─ FASE 2: Idempotent producer (no hay duplicados) ─");
Broker broker2 = new Broker();
IdempotentProducer idempotentProducer = new IdempotentProducer(broker2, "producer-2");
idempotentProducer.enviar("pedidos-P0", "pedido-CCC");
idempotentProducer.enviarConFalloYRetry("pedidos-P0", "pedido-DDD"); // retry → ignorado
idempotentProducer.enviar("pedidos-P0", "pedido-EEE");
System.out.println();
broker2.mostrarTopics();
System.out.println(" → pedido-DDD aparece exactamente 1 vez (retry deduplicado por broker)");
// ── Fase 3: Transaccional — read-process-write atómico ────────
System.out.println("\n─ FASE 3: Exactly-once transaccional (read-process-write) ─");
Broker broker3 = new Broker();
TransactionalProducer txProducer = new TransactionalProducer(broker3, "tx-producer-1");
// TX exitosa: consume de 'input', procesa, produce a 'output', commit offset
System.out.println(" [TX 1: exitosa]");
txProducer.beginTransaction();
txProducer.send("output-topic-P0", "resultado-de-pedido-FFF");
txProducer.sendOffsetsToTransaction("consumer-group-1", 5);
txProducer.commitTransaction();
// TX abortada: simula crash a mitad — nada es visible
System.out.println("\n [TX 2: abortada por crash a mitad]");
txProducer.beginTransaction();
txProducer.send("output-topic-P0", "resultado-INCOMPLETO-GGG");
txProducer.sendOffsetsToTransaction("consumer-group-1", 6);
txProducer.abortTransaction(); // crash simulado
System.out.println();
broker3.mostrarTopics();
System.out.println(" → resultado-INCOMPLETO-GGG NO aparece: la TX fue abortada");
System.out.println(" → output-topic contiene solo 'resultado-de-pedido-FFF' (TX committed)");
System.out.println("\n" + "═".repeat(65));
System.out.println(" RESUMEN GARANTÍAS");
System.out.println("═".repeat(65));
System.out.println(" At-least-once → duplicados posibles. Requiere idempotencia en consumer.");
System.out.println(" Idempotent → enable.idempotence=true. Deduplica por (pid, seq).");
System.out.println(" Exactly-once → transactional.id + isolation.level=read_committed.");
System.out.println(" produce + commitOffset en una sola transacción atómica.");
System.out.println(" Coste: exactamente-once tiene mayor latencia (2-phase commit en broker).");
System.out.println("═".repeat(65));
}
}