|
6 | 6 | import java.time.ZoneOffset; |
7 | 7 | import java.util.List; |
8 | 8 | import java.util.Map; |
9 | | -import java.util.concurrent.atomic.AtomicReference; |
10 | 9 |
|
11 | 10 | /** Portable optimistic Business ID allocator backed by a TeaQLDatabase. */ |
12 | 11 | public final class JdbcBusinessIdAllocator |
@@ -48,47 +47,88 @@ public BusinessIdAllocation allocate(BusinessIdPlan plan) { |
48 | 47 | .toEpochMilli(); |
49 | 48 | RuntimeException lastConflict = null; |
50 | 49 | for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) { |
51 | | - AtomicReference<BusinessIdAllocation> result = new AtomicReference<>(); |
| 50 | + List<Map<String, Object>> rows; |
52 | 51 | try { |
53 | | - database.executeInTransaction(() -> { |
54 | | - List<Map<String, Object>> rows = database.query( |
55 | | - "SELECT current_value, version FROM " + table |
56 | | - + " WHERE scope_key = ?", |
57 | | - new Object[]{scopeKey}); |
58 | | - if (rows == null || rows.isEmpty()) { |
59 | | - database.executeUpdate( |
60 | | - "INSERT INTO " + table |
61 | | - + " (scope_key, current_value, version, updated_at)" |
62 | | - + " VALUES (?, 1, 1, ?)", |
63 | | - new Object[]{scopeKey, updatedAt}); |
64 | | - result.set(new BusinessIdAllocation(plan.scope(), 1)); |
65 | | - return; |
| 52 | + rows = database.query( |
| 53 | + "SELECT current_value, version FROM " + table |
| 54 | + + " WHERE scope_key = ?", |
| 55 | + new Object[]{scopeKey}); |
| 56 | + } catch (RuntimeException failure) { |
| 57 | + throw new IllegalStateException( |
| 58 | + "Cannot read Business ID table " + table |
| 59 | + + "; verify database access and call context.ensureSchema()", |
| 60 | + failure); |
| 61 | + } |
| 62 | + if (rows == null) { |
| 63 | + throw new IllegalStateException( |
| 64 | + "Business ID database returned a null result set for " + scopeKey); |
| 65 | + } |
| 66 | + if (rows.isEmpty()) { |
| 67 | + Integer inserted = null; |
| 68 | + try { |
| 69 | + inserted = database.executeUpdate( |
| 70 | + "INSERT INTO " + table |
| 71 | + + " (scope_key, current_value, version, updated_at)" |
| 72 | + + " VALUES (?, 1, 1, ?)", |
| 73 | + new Object[]{scopeKey, updatedAt}); |
| 74 | + } catch (RuntimeException insertionFailure) { |
| 75 | + lastConflict = insertionFailure; |
| 76 | + // A competing instance may have inserted this key. Do not retry an |
| 77 | + // unrelated write failure unless that exact row is now observable. |
| 78 | + List<Map<String, Object>> observed; |
| 79 | + try { |
| 80 | + observed = database.query( |
| 81 | + "SELECT current_value, version FROM " + table |
| 82 | + + " WHERE scope_key = ?", |
| 83 | + new Object[]{scopeKey}); |
| 84 | + } catch (RuntimeException readFailure) { |
| 85 | + readFailure.addSuppressed(insertionFailure); |
| 86 | + throw new IllegalStateException( |
| 87 | + "Cannot verify Business ID insert for " + scopeKey, |
| 88 | + readFailure); |
66 | 89 | } |
67 | | - Map<String, Object> row = rows.get(0); |
68 | | - long current = number(row, "current_value"); |
69 | | - long version = number(row, "version"); |
70 | | - if (current >= plan.maximumSequence()) { |
71 | | - throw new BusinessIdException( |
72 | | - BusinessIdErrorCode.BUSINESS_ID_RANGE_EXHAUSTED, |
73 | | - "Business ID range exhausted for " + scopeKey); |
| 90 | + if (observed == null || observed.isEmpty()) { |
| 91 | + throw new IllegalStateException( |
| 92 | + "Business ID insert failed without a matching row for " |
| 93 | + + scopeKey, |
| 94 | + insertionFailure); |
74 | 95 | } |
75 | | - long next = current + 1; |
76 | | - int updated = database.executeUpdate( |
77 | | - "UPDATE " + table |
78 | | - + " SET current_value = ?, version = version + 1, updated_at = ?" |
79 | | - + " WHERE scope_key = ? AND version = ?", |
80 | | - new Object[]{next, updatedAt, scopeKey, version}); |
81 | | - if (updated == 1) { |
82 | | - result.set(new BusinessIdAllocation(plan.scope(), next)); |
| 96 | + } |
| 97 | + if (inserted != null) { |
| 98 | + if (inserted == 1) { |
| 99 | + return new BusinessIdAllocation(plan.scope(), 1); |
83 | 100 | } |
84 | | - }); |
85 | | - if (result.get() != null) { |
86 | | - return result.get(); |
| 101 | + throw new IllegalStateException( |
| 102 | + "Expected one inserted Business ID row for " + scopeKey |
| 103 | + + ", inserted " + inserted); |
| 104 | + } |
| 105 | + } else { |
| 106 | + Map<String, Object> row = rows.get(0); |
| 107 | + long current = number(row, "current_value"); |
| 108 | + long version = number(row, "version"); |
| 109 | + if (current < 1 || version < 1) { |
| 110 | + throw new IllegalStateException( |
| 111 | + "Invalid Business ID sequence row for " + scopeKey); |
| 112 | + } |
| 113 | + if (current >= plan.maximumSequence()) { |
| 114 | + throw new BusinessIdException( |
| 115 | + BusinessIdErrorCode.BUSINESS_ID_RANGE_EXHAUSTED, |
| 116 | + "Business ID range exhausted for " + scopeKey); |
| 117 | + } |
| 118 | + long next = current + 1; |
| 119 | + int updated = database.executeUpdate( |
| 120 | + "UPDATE " + table |
| 121 | + + " SET current_value = ?, version = version + 1, updated_at = ?" |
| 122 | + + " WHERE scope_key = ? AND version = ? AND current_value = ?", |
| 123 | + new Object[]{next, updatedAt, scopeKey, version, current}); |
| 124 | + if (updated == 1) { |
| 125 | + return new BusinessIdAllocation(plan.scope(), next); |
| 126 | + } |
| 127 | + if (updated != 0) { |
| 128 | + throw new IllegalStateException( |
| 129 | + "Expected at most one updated Business ID row for " + scopeKey |
| 130 | + + ", updated " + updated); |
87 | 131 | } |
88 | | - } catch (BusinessIdException error) { |
89 | | - throw error; |
90 | | - } catch (RuntimeException conflict) { |
91 | | - lastConflict = conflict; |
92 | 132 | } |
93 | 133 | java.util.concurrent.locks.LockSupport.parkNanos(1_000_000L); |
94 | 134 | } |
|
0 commit comments