-
Notifications
You must be signed in to change notification settings - Fork 0
feat(java): proxy sessions with identity dedup and cleanup #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
sdks/java/src/main/java/org/byteveda/taskito/proxies/ProxySession.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package org.byteveda.taskito.proxies; | ||
|
|
||
| import java.lang.System.Logger; | ||
| import java.lang.System.Logger.Level; | ||
| import java.time.Duration; | ||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.HashMap; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.Map; | ||
| import org.byteveda.taskito.errors.ProxyException; | ||
|
|
||
| /** | ||
| * A unit-of-work wrapper over {@link Proxies} adding identity dedup and a | ||
| * cleanup lifecycle. Within one session: | ||
| * | ||
| * <ul> | ||
| * <li>{@link #deconstruct(Object)} memoizes by (instance identity, purpose) — | ||
| * deconstructing the same object again returns the same {@link ProxyRef} | ||
| * without calling the handler twice. The first call's TTL wins per | ||
| * (instance, purpose); use a new session to refresh expiry. | ||
| * <li>{@link #reconstruct(ProxyRef)} memoizes by the ref's signature — every | ||
| * ref to the same underlying resource resolves to the same instance, and | ||
| * the handler reconstructs it once. Signature, expiry, and purpose are | ||
| * re-verified on every call, memo hit or not. | ||
| * <li>{@link #close()} runs {@link ProxyHandler#cleanup} once per unique | ||
| * reconstructed instance, in reverse reconstruction order (LIFO). | ||
| * </ul> | ||
| * | ||
| * <p>Not thread-safe — confine a session to the thread that created it. A | ||
| * session models one producer batch or one task invocation. | ||
| */ | ||
| public final class ProxySession implements AutoCloseable { | ||
| private static final Logger LOG = System.getLogger(ProxySession.class.getName()); | ||
|
|
||
| private final Proxies proxies; | ||
| private final IdentityHashMap<Object, Map<String, ProxyRef>> deconstructed = new IdentityHashMap<>(); | ||
| private final Map<String, Object> reconstructed = new HashMap<>(); | ||
| private final Deque<Runnable> cleanups = new ArrayDeque<>(); | ||
| private boolean closed; | ||
|
|
||
| ProxySession(Proxies proxies) { | ||
| this.proxies = proxies; | ||
| } | ||
|
|
||
| /** Deconstruct {@code value} (no expiry or purpose), deduped by instance identity. */ | ||
| public ProxyRef deconstruct(Object value) { | ||
| return deconstruct(value, null, null); | ||
| } | ||
|
|
||
| /** Deconstruct {@code value} with a TTL, deduped by instance identity. */ | ||
| public ProxyRef deconstruct(Object value, Duration ttl) { | ||
| return deconstruct(value, ttl, null); | ||
| } | ||
|
|
||
| /** | ||
| * Deconstruct {@code value} bound to {@code ttl}/{@code purpose} (both | ||
| * nullable), deduped by (instance identity, purpose). | ||
| */ | ||
| public ProxyRef deconstruct(Object value, Duration ttl, String purpose) { | ||
| ensureOpen(); | ||
| if (value == null) { | ||
| throw new ProxyException("cannot deconstruct null"); | ||
| } | ||
| Map<String, ProxyRef> byPurpose = deconstructed.computeIfAbsent(value, key -> new HashMap<>()); | ||
| ProxyRef cached = byPurpose.get(purpose); | ||
| if (cached != null) { | ||
| return cached; | ||
| } | ||
| ProxyRef ref = proxies.deconstruct(value, ttl, purpose); | ||
| byPurpose.put(purpose, ref); | ||
| return ref; | ||
| } | ||
|
|
||
| /** Verify and reconstruct {@code ref}, deduped by its signature. */ | ||
| public Object reconstruct(ProxyRef ref) { | ||
| return reconstruct(ref, null); | ||
| } | ||
|
|
||
| /** | ||
| * Verify (always — including memo hits, so a ref that expires mid-session | ||
| * stops resolving) and reconstruct {@code ref}, deduped by its signature. | ||
| */ | ||
| public Object reconstruct(ProxyRef ref, String expectedPurpose) { | ||
| ensureOpen(); | ||
| ProxyHandler<Object> handler = proxies.handlerFor(ref.handler()); | ||
| proxies.verify(ref, expectedPurpose); | ||
| String signature = ref.signature(); | ||
| if (reconstructed.containsKey(signature)) { | ||
| return reconstructed.get(signature); | ||
| } | ||
| Object value = handler.reconstruct(ref.reference()); | ||
| reconstructed.put(signature, value); | ||
| cleanups.push(() -> handler.cleanup(value)); | ||
| return value; | ||
| } | ||
|
|
||
| /** {@link #reconstruct(ProxyRef)} cast to the caller's type. */ | ||
| @SuppressWarnings("unchecked") | ||
| public <T> T resolve(ProxyRef ref) { | ||
| return (T) reconstruct(ref); | ||
| } | ||
|
|
||
| /** {@link #reconstruct(ProxyRef, String)} cast to the caller's type. */ | ||
| @SuppressWarnings("unchecked") | ||
| public <T> T resolve(ProxyRef ref, String expectedPurpose) { | ||
| return (T) reconstruct(ref, expectedPurpose); | ||
| } | ||
|
|
||
| /** | ||
| * Run {@link ProxyHandler#cleanup} for every reconstructed instance in | ||
| * reverse order (LIFO), once each. A cleanup failure is logged and never | ||
| * skips the rest; a JVM-level {@link Error} is rethrown only after the | ||
| * remaining cleanups have drained. Idempotent. | ||
| */ | ||
| @Override | ||
| public void close() { | ||
| if (closed) { | ||
| return; | ||
| } | ||
| closed = true; | ||
| Error fatal = null; | ||
| while (!cleanups.isEmpty()) { | ||
| Runnable cleanup = cleanups.pop(); | ||
| try { | ||
| cleanup.run(); | ||
| } catch (RuntimeException e) { | ||
| // Cleanup must never fail the rest of the teardown — record and continue. | ||
| LOG.log(Level.WARNING, "proxy cleanup failed", e); | ||
| } catch (Error e) { | ||
| // Drain the remaining cleanups first, then let the error propagate — | ||
| // aborting here would abandon them for good (closed is already set). | ||
| if (fatal == null) { | ||
| fatal = e; | ||
| } | ||
| LOG.log(Level.WARNING, "proxy cleanup threw an error", e); | ||
| } | ||
| } | ||
| deconstructed.clear(); | ||
| reconstructed.clear(); | ||
| if (fatal != null) { | ||
| throw fatal; | ||
| } | ||
| } | ||
|
|
||
| private void ensureOpen() { | ||
| if (closed) { | ||
| throw new ProxyException("proxy session is closed"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.