|
29 | 29 | import java.util.TreeMap; |
30 | 30 | import java.util.concurrent.CompletableFuture; |
31 | 31 | import java.util.concurrent.ConcurrentHashMap; |
| 32 | +import java.util.concurrent.ConcurrentSkipListMap; |
32 | 33 | import java.util.concurrent.CopyOnWriteArrayList; |
33 | 34 | import java.util.concurrent.Executors; |
34 | 35 | import java.util.concurrent.ScheduledExecutorService; |
@@ -150,6 +151,7 @@ public synchronized void start() { |
150 | 151 | if (closed || sweepExecutor != null) { |
151 | 152 | return; |
152 | 153 | } |
| 154 | + verifyParallelismConsistency(); |
153 | 155 | this.localLeader = new TcLeader(pulsar.getBrokerId(), pulsar.getBrokerServiceUrl(), |
154 | 156 | pulsar.getBrokerServiceUrlTls(), pulsar.getSafeWebServiceAddress()); |
155 | 157 | for (int partition = 0; partition < partitionCount; partition++) { |
@@ -176,6 +178,52 @@ public synchronized void start() { |
176 | 178 | gcSweepIntervalMs, gcSweepIntervalMs, TimeUnit.MILLISECONDS); |
177 | 179 | } |
178 | 180 |
|
| 181 | + /** |
| 182 | + * Persist this broker's configured parallelism cluster-wide on first start, and verify every |
| 183 | + * subsequent broker agrees. A mismatch means brokers would run different election sets and the |
| 184 | + * coordinator-count encoded in transaction ids would be ambiguous — fatal misconfiguration, so |
| 185 | + * we fail fast rather than start in an inconsistent state. |
| 186 | + */ |
| 187 | + private void verifyParallelismConsistency() { |
| 188 | + var store = pulsar.getLocalMetadataStore(); |
| 189 | + try { |
| 190 | + byte[] value = Integer.toString(partitionCount).getBytes(java.nio.charset.StandardCharsets.UTF_8); |
| 191 | + var existing = store.get(TxnPaths.TXN_TC_PARALLELISM_PATH).get(); |
| 192 | + if (existing.isEmpty()) { |
| 193 | + // First broker to start writes the value (CAS create; lose harmlessly to a racing peer). |
| 194 | + store.put(TxnPaths.TXN_TC_PARALLELISM_PATH, value, java.util.Optional.of(-1L)) |
| 195 | + .get(); |
| 196 | + var after = store.get(TxnPaths.TXN_TC_PARALLELISM_PATH).get(); |
| 197 | + if (after.isPresent()) { |
| 198 | + checkParallelismMatches(after.get().getValue()); |
| 199 | + } |
| 200 | + } else { |
| 201 | + checkParallelismMatches(existing.get().getValue()); |
| 202 | + } |
| 203 | + } catch (IllegalStateException e) { |
| 204 | + throw e; |
| 205 | + } catch (Exception e) { |
| 206 | + // A racing create (BadVersion) or read-after-write resolves by re-reading and comparing. |
| 207 | + try { |
| 208 | + var after = store.get(TxnPaths.TXN_TC_PARALLELISM_PATH).get(); |
| 209 | + after.ifPresent(r -> checkParallelismMatches(r.getValue())); |
| 210 | + } catch (Exception ignore) { |
| 211 | + log.warn().exception(e).log("Could not verify TC parallelism consistency; proceeding"); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + private void checkParallelismMatches(byte[] storedValue) { |
| 217 | + int stored = Integer.parseInt(new String(storedValue, java.nio.charset.StandardCharsets.UTF_8).trim()); |
| 218 | + if (stored != partitionCount) { |
| 219 | + throw new IllegalStateException( |
| 220 | + "transactionCoordinatorScalableTopicsParallelism mismatch: this broker is configured" |
| 221 | + + " with " + partitionCount + " but the cluster was initialized with " + stored |
| 222 | + + ". The value is fixed at cluster bring-up and must be identical on every" |
| 223 | + + " broker."); |
| 224 | + } |
| 225 | + } |
| 226 | + |
179 | 227 | /** Stop the sweeps and release every leader-election lease. Idempotent. */ |
180 | 228 | public synchronized void close() { |
181 | 229 | closed = true; |
@@ -265,18 +313,35 @@ public CompletableFuture<Void> handleClientConnect(TransactionCoordinatorID tcId |
265 | 313 | // ---- Assignment discovery (client watch) ------------------------------ |
266 | 314 |
|
267 | 315 | /** |
268 | | - * Build the current full {@code partition → leader} snapshot from the election state. A |
269 | | - * partition with no currently-known leader (mid-election) is omitted; the client parks any |
270 | | - * transaction routed there until a later snapshot fills it in. Always the complete map — the |
271 | | - * watch protocol sends full snapshots, never diffs. |
| 316 | + * Build the current full {@code partition → leader} snapshot from the election state. Uses the |
| 317 | + * async {@link LeaderElection#getLeaderValue()} (which loads from the metadata store on a cache |
| 318 | + * miss) rather than the cache-only {@code getLeaderValueIfPresent()}: when this broker just |
| 319 | + * transitioned to {@code Following} for a partition, its local cache for the new leader's node |
| 320 | + * may not be repopulated yet, and a cache-only read would silently omit that partition. Loading |
| 321 | + * from the store closes that window so a follower's snapshot is still complete. |
| 322 | + * |
| 323 | + * <p>A partition still genuinely without a leader (no broker elected yet) is omitted; the caller |
| 324 | + * ({@code ServerCnx}) re-pushes shortly after so the client isn't stranded. Always the complete |
| 325 | + * map — the watch protocol sends full snapshots, never diffs. |
| 326 | + * |
| 327 | + * @return a future of the snapshot plus whether it is complete (every partition has a leader) |
272 | 328 | */ |
273 | | - public TcAssignmentsSnapshot buildAssignmentsSnapshot() { |
274 | | - Map<Integer, TcLeader> assignments = new TreeMap<>(); |
| 329 | + public CompletableFuture<TcAssignmentsSnapshot> buildAssignmentsSnapshot() { |
| 330 | + Map<Integer, TcLeader> assignments = new ConcurrentSkipListMap<>(); |
| 331 | + List<CompletableFuture<Void>> loads = new ArrayList<>(elections.size()); |
275 | 332 | for (Map.Entry<Integer, LeaderElection<TcLeader>> e : elections.entrySet()) { |
276 | | - e.getValue().getLeaderValueIfPresent() |
277 | | - .ifPresent(leader -> assignments.put(e.getKey(), leader)); |
| 333 | + int partition = e.getKey(); |
| 334 | + loads.add(e.getValue().getLeaderValue() |
| 335 | + .thenAccept(opt -> opt.ifPresent(leader -> assignments.put(partition, leader))) |
| 336 | + .exceptionally(ex -> { |
| 337 | + // Treat a load error as "leader unknown for now"; the re-push will retry. |
| 338 | + log.debug().attr("partition", partition).exception(ex) |
| 339 | + .log("v5 TC leader-value load failed while building snapshot"); |
| 340 | + return null; |
| 341 | + })); |
278 | 342 | } |
279 | | - return new TcAssignmentsSnapshot(partitionCount, assignments); |
| 343 | + return FutureUtil.waitForAll(loads) |
| 344 | + .thenApply(__ -> new TcAssignmentsSnapshot(partitionCount, new TreeMap<>(assignments))); |
280 | 345 | } |
281 | 346 |
|
282 | 347 | /** |
@@ -291,6 +356,10 @@ public AutoCloseable registerAssignmentChangeListener(Runnable listener) { |
291 | 356 |
|
292 | 357 | /** Immutable full assignment snapshot: partition count + the currently-known leaders. */ |
293 | 358 | public record TcAssignmentsSnapshot(int partitionCount, Map<Integer, TcLeader> assignments) { |
| 359 | + /** @return true if every partition has a known leader (no mid-election gaps). */ |
| 360 | + public boolean isComplete() { |
| 361 | + return assignments.size() == partitionCount; |
| 362 | + } |
294 | 363 | } |
295 | 364 |
|
296 | 365 | // ---- newTransaction --------------------------------------------------- |
|