|
3 | 3 | import java.util.Map; |
4 | 4 | import java.util.Objects; |
5 | 5 | import java.util.Optional; |
| 6 | +import java.util.concurrent.Callable; |
6 | 7 | import java.util.concurrent.CompletableFuture; |
| 8 | +import java.util.concurrent.CompletionStage; |
7 | 9 | import java.util.concurrent.ConcurrentHashMap; |
8 | 10 | import java.util.concurrent.Executor; |
9 | 11 | import java.util.concurrent.Flow; |
10 | 12 | import java.util.concurrent.TimeUnit; |
| 13 | +import java.util.function.BiConsumer; |
11 | 14 | import java.util.function.Function; |
12 | 15 | import java.util.function.Predicate; |
| 16 | +import java.util.function.Supplier; |
13 | 17 |
|
14 | 18 | import org.infinispan.client.hotrod.RemoteCache; |
15 | 19 | import org.infinispan.client.hotrod.impl.protocol.Codec27; |
16 | 20 | import org.infinispan.commons.util.NullValue; |
17 | | -import org.infinispan.commons.util.concurrent.CompletionStages; |
18 | 21 | import org.reactivestreams.FlowAdapters; |
19 | 22 |
|
20 | 23 | import io.quarkus.arc.Arc; |
@@ -81,78 +84,65 @@ private <T> T decodeNull(Object value) { |
81 | 84 |
|
82 | 85 | @Override |
83 | 86 | public <K, V> Uni<V> get(K key, Function<K, V> valueLoader) { |
84 | | - return Uni.createFrom() |
85 | | - .completionStage(() -> CompletionStages.handleAndCompose(remoteCache.getAsync(key), (v1, ex1) -> { |
86 | | - if (ex1 != null) { |
87 | | - return CompletableFuture.failedFuture(ex1); |
88 | | - } |
| 87 | + Context context = Vertx.currentContext(); |
| 88 | + Executor executor = duplicateContextExecutor(context); |
89 | 89 |
|
| 90 | + return Uni.createFrom().completionStage(new Supplier<CompletionStage<V>>() { |
| 91 | + @Override |
| 92 | + public CompletionStage<V> get() { |
| 93 | + return remoteCache.getAsync(key); |
| 94 | + } |
| 95 | + }) |
| 96 | + .emitOn(executor) |
| 97 | + .flatMap(v1 -> { |
90 | 98 | if (v1 != null) { |
91 | | - return CompletableFuture.completedFuture(decodeNull(v1)); |
| 99 | + return Uni.createFrom() |
| 100 | + .completionStage(new Supplier<CompletionStage<V>>() { |
| 101 | + @Override |
| 102 | + public CompletionStage<V> get() { |
| 103 | + return CompletableFuture.completedFuture(InfinispanCacheImpl.this.decodeNull(v1)); |
| 104 | + } |
| 105 | + }) |
| 106 | + .emitOn(executor); |
92 | 107 | } |
93 | 108 |
|
94 | 109 | CompletableFuture<V> resultAsync = new CompletableFuture<>(); |
95 | 110 | CompletableFuture<V> computedValue = computationResults.putIfAbsent(key, resultAsync); |
| 111 | + |
96 | 112 | if (computedValue != null) { |
97 | | - return computedValue; |
| 113 | + return Uni.createFrom().completionStage(computedValue).emitOn(executor); |
| 114 | + } |
| 115 | + |
| 116 | + if (context != null) { |
| 117 | + return Uni.createFrom().completionStage(new Supplier<CompletionStage<? extends V>>() { |
| 118 | + @Override |
| 119 | + public CompletionStage<? extends V> get() { |
| 120 | + return context.executeBlocking(new Callable<V>() { |
| 121 | + @Override |
| 122 | + public V call() throws Exception { |
| 123 | + return valueLoader.apply(key); |
| 124 | + } |
| 125 | + }).toCompletionStage() |
| 126 | + .thenComposeAsync(newValue -> { |
| 127 | + InfinispanCacheImpl.this.putIfAbsentInInfinispan(key, newValue, resultAsync, |
| 128 | + executor); |
| 129 | + return resultAsync; |
| 130 | + }, executor); |
| 131 | + } |
| 132 | + }); |
98 | 133 | } |
| 134 | + |
99 | 135 | V newValue = valueLoader.apply(key); |
100 | | - remoteCache |
101 | | - .putIfAbsentAsync(key, encodeNull(newValue), lifespan, TimeUnit.MILLISECONDS, maxIdle, |
102 | | - TimeUnit.MILLISECONDS) |
103 | | - .whenComplete((existing, ex2) -> { |
104 | | - if (ex2 != null) { |
105 | | - resultAsync.completeExceptionally((Throwable) ex2); |
106 | | - } else if (existing == null) { |
107 | | - resultAsync.complete(newValue); |
108 | | - } else { |
109 | | - resultAsync.complete(decodeNull(existing)); |
110 | | - } |
111 | | - computationResults.remove(key); |
112 | | - }); |
113 | | - return resultAsync; |
114 | | - })); |
| 136 | + putIfAbsentInInfinispan(key, newValue, resultAsync, executor); |
| 137 | + return Uni.createFrom().completionStage(resultAsync).emitOn(executor); |
| 138 | + }); |
115 | 139 | } |
116 | 140 |
|
117 | 141 | @Override |
118 | 142 | public <K, V> Uni<V> getAsync(K key, Function<K, Uni<V>> valueLoader) { |
119 | 143 | Context context = Vertx.currentContext(); |
120 | | - |
121 | | - return Uni.createFrom().completionStage(CompletionStages.handleAndCompose(remoteCache.getAsync(key), (v1, ex1) -> { |
122 | | - if (ex1 != null) { |
123 | | - return CompletableFuture.failedFuture(ex1); |
124 | | - } |
125 | | - |
126 | | - if (v1 != null) { |
127 | | - return CompletableFuture.completedFuture(decodeNull(v1)); |
128 | | - } |
129 | | - |
130 | | - CompletableFuture<V> resultAsync = new CompletableFuture<>(); |
131 | | - CompletableFuture<V> computedValue = computationResults.putIfAbsent(key, resultAsync); |
132 | | - if (computedValue != null) { |
133 | | - return computedValue; |
134 | | - } |
135 | | - valueLoader.apply(key).convert().toCompletionStage() |
136 | | - .whenComplete((newValue, ex2) -> { |
137 | | - if (ex2 != null) { |
138 | | - resultAsync.completeExceptionally(ex2); |
139 | | - computationResults.remove(key); |
140 | | - } else { |
141 | | - remoteCache.putIfAbsentAsync(key, encodeNull(newValue), lifespan, TimeUnit.MILLISECONDS, maxIdle, |
142 | | - TimeUnit.MILLISECONDS).whenComplete((existing, ex3) -> { |
143 | | - if (ex3 != null) { |
144 | | - resultAsync.completeExceptionally((Throwable) ex3); |
145 | | - } else if (existing == null) { |
146 | | - resultAsync.complete(newValue); |
147 | | - } else { |
148 | | - resultAsync.complete(decodeNull(existing)); |
149 | | - } |
150 | | - computationResults.remove(key); |
151 | | - }); |
152 | | - } |
153 | | - }); |
154 | | - return resultAsync; |
155 | | - })).emitOn(new Executor() { |
| 144 | + Executor executor = duplicateContextExecutor(context); |
| 145 | + return Uni.createFrom().completionStage(getFromInfinispanAsync(key, valueLoader, executor)).emitOn(new Executor() { |
156 | 146 | // We need make sure we go back to the original context when the cache value is computed. |
157 | 147 | // Otherwise, we would always emit on the context having computed the value, which could |
158 | 148 | // break the duplicated context isolation. |
@@ -194,17 +184,105 @@ public void handle(Void ignored) { |
194 | 184 | } |
195 | 185 | } |
196 | 186 | } |
197 | | - }); |
| 187 | + }).emitOn(executor); |
| 188 | + } |
| 189 | + |
| 190 | + private static Executor duplicateContextExecutor(Context context) { |
| 191 | + Executor executor = new Executor() { |
| 192 | + @Override |
| 193 | + public void execute(Runnable r) { |
| 194 | + if (context == null) |
| 195 | + r.run(); |
| 196 | + else |
| 197 | + context.runOnContext(x -> r.run()); |
| 198 | + } |
| 199 | + }; |
| 200 | + return executor; |
| 201 | + } |
| 202 | + |
| 203 | + private <K, V> CompletionStage<V> getFromInfinispanAsync(K key, Function<K, Uni<V>> valueLoader, Executor executor) { |
| 204 | + return remoteCache.getAsync(key) |
| 205 | + .exceptionallyAsync(ex -> ex, executor) |
| 206 | + .thenApplyAsync(new Function() { |
| 207 | + @Override |
| 208 | + public Object apply(Object v1) { |
| 209 | + if (v1 != null) { |
| 210 | + return CompletableFuture.completedFuture(InfinispanCacheImpl.this.decodeNull(v1)); |
| 211 | + } |
| 212 | + |
| 213 | + CompletableFuture<V> resultAsync = new CompletableFuture<>(); |
| 214 | + CompletableFuture<V> computedValue = computationResults.putIfAbsent(key, resultAsync); |
| 215 | + |
| 216 | + if (computedValue != null) { |
| 217 | + return computedValue; |
| 218 | + } |
| 219 | + |
| 220 | + valueLoader.apply(key) |
| 221 | + .convert().toCompletionStage() |
| 222 | + .whenCompleteAsync(new BiConsumer<V, Throwable>() { |
| 223 | + @Override |
| 224 | + public void accept(V newValue, Throwable ex2) { |
| 225 | + if (ex2 != null) { |
| 226 | + resultAsync.completeExceptionally(ex2); |
| 227 | + computationResults.remove(key); |
| 228 | + } else { |
| 229 | + InfinispanCacheImpl.this.putIfAbsentInInfinispan(key, newValue, resultAsync, |
| 230 | + executor); |
| 231 | + } |
| 232 | + } |
| 233 | + }, executor); |
| 234 | + return resultAsync; |
| 235 | + } |
| 236 | + }, executor).thenComposeAsync(new Function() { |
| 237 | + @Override |
| 238 | + public Object apply(Object c) { |
| 239 | + return c; |
| 240 | + } |
| 241 | + }, executor); |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + private <K, V> void putIfAbsentInInfinispan(K key, V newValue, CompletableFuture<V> resultAsync, Executor executor) { |
| 246 | + remoteCache.putIfAbsentAsync( |
| 247 | + key, |
| 248 | + encodeNull(newValue), |
| 249 | + lifespan, TimeUnit.MILLISECONDS, |
| 250 | + maxIdle, TimeUnit.MILLISECONDS).whenCompleteAsync(new BiConsumer<Object, Throwable>() { |
| 251 | + @Override |
| 252 | + public void accept(Object existing, Throwable ex) { |
| 253 | + try { |
| 254 | + if (ex != null) { |
| 255 | + resultAsync.completeExceptionally(ex); |
| 256 | + } else if (existing == null) { |
| 257 | + resultAsync.complete(newValue); |
| 258 | + } else { |
| 259 | + resultAsync.complete(InfinispanCacheImpl.this.decodeNull(existing)); |
| 260 | + } |
| 261 | + } finally { |
| 262 | + computationResults.remove(key); |
| 263 | + } |
| 264 | + } |
| 265 | + }, executor); |
198 | 266 | } |
199 | 267 |
|
200 | 268 | @Override |
201 | 269 | public Uni<Void> invalidate(Object key) { |
202 | | - return Uni.createFrom().completionStage(() -> remoteCache.removeAsync(key)); |
| 270 | + return Uni.createFrom().completionStage(new Supplier<CompletionStage<Void>>() { |
| 271 | + @Override |
| 272 | + public CompletionStage<Void> get() { |
| 273 | + return remoteCache.removeAsync(key); |
| 274 | + } |
| 275 | + }); |
203 | 276 | } |
204 | 277 |
|
205 | 278 | @Override |
206 | 279 | public Uni<Void> invalidateAll() { |
207 | | - return Uni.createFrom().completionStage(() -> remoteCache.clearAsync()); |
| 280 | + return Uni.createFrom().completionStage(new Supplier<CompletionStage<Void>>() { |
| 281 | + @Override |
| 282 | + public CompletionStage<Void> get() { |
| 283 | + return remoteCache.clearAsync(); |
| 284 | + } |
| 285 | + }); |
208 | 286 | } |
209 | 287 |
|
210 | 288 | @Override |
|
0 commit comments