From f814d17d7b6a80856bdf96c34c4557b394746224 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sat, 21 Nov 2026 22:05:11 +0000 Subject: [PATCH 1/2] Let a cloud result replace one the cache will never serve The Cloud Request Engine's result is stored while its request is still running. If that request then fails, or answers with a 51Did, the entry is never served. The cache the engine is given does not replace an existing entry by default, so the next result for the same Evidence was dropped, and every later request for that Evidence went to the cloud service while the dead entry stayed in the cache. The Cloud Request Engine's builder now sets a cache built by a PutCacheBuilder to replace an existing entry, which covers setCacheSize and the cloud pipeline builders' CacheConfiguration made with a size. Two tests make three requests with the same Evidence, the first failing or answering with a 51Did, and expect the third to be served from the cache. --- .../CloudRequestEngineBuilder.java | 23 ++++++ .../CloudRequestEngineLazyCacheTests.java | 81 +++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java index b40f4db91..50630c81d 100644 --- a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java +++ b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java @@ -23,6 +23,7 @@ package fiftyone.pipeline.cloudrequestengine.flowelements; +import fiftyone.caching.PutCacheBuilder; import fiftyone.pipeline.annotations.DefaultValue; import fiftyone.pipeline.annotations.ElementBuilder; import fiftyone.pipeline.cloudrequestengine.Constants.Messages; @@ -421,4 +422,26 @@ public CloudRequestEngineBuilder setCacheSize(int size) { this.cacheConfig = new CacheConfiguration(size); return this; } + + /** + * Where the cache is built by a {@link PutCacheBuilder}, as it is by + * {@link #setCacheSize(int)} and by a {@link CacheConfiguration} made + * with a size, the cache is set to replace an entry when a result is put + * for its key. A result is stored while its cloud request is still + * running, and an entry whose request then fails, or answers with a + * 51Did, is never served, so the next result for that key must take its + * place. + * @param engine the engine to configure + * @throws Exception if the engine could not be configured + */ + @Override + protected void configureEngine(CloudRequestEngine engine) + throws Exception { + if (cacheConfig != null && + cacheConfig.getCacheBuilder() instanceof PutCacheBuilder) { + ((PutCacheBuilder) cacheConfig.getCacheBuilder()) + .setUpdateExisting(true); + } + super.configureEngine(engine); + } } diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java index 7a65dd654..bbcf28862 100644 --- a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java +++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java @@ -355,6 +355,87 @@ public void close() throws java.io.IOException { "The processing task read the Flow Data to cache the result."); } + /** + * After a failed request, the next result for the same Evidence takes + * the failed entry's place, so the request after that is served from the + * cache. + */ + @Test + public void process_ResultAfterAFailureIsCached() throws Exception { + String second = "{'device':{'value':'1'}}"; + CountDownLatch gate = new CountDownLatch(1); + mockService(new CountDownLatch(2), gate, null, second); + CloudRequestEngine engine = buildLazyCachedEngine(); + + assertEquals(Arrays.asList(FAILED, second, second), + processThreeTimes(engine, gate)); + verifyDataRequests(2); + } + + /** + * After a response with a 51Did, the next result for the same Evidence + * takes its place, so the request after that is served from the cache. + * The cache is configured as the cloud pipeline builders configure it. + */ + @Test + public void process_ResultAfterAFodIdIsCached() throws Exception { + String first = "{'device':{'value':'1'},'fodid':'first'}"; + String second = "{'device':{'value':'1'}}"; + CountDownLatch gate = new CountDownLatch(1); + mockService(new CountDownLatch(2), gate, first, second); + CloudRequestEngine engine = + new CloudRequestEngineBuilder(loggerFactory, httpClient) + .setResourceKey("resourcekey") + .setCache(new CacheConfiguration(10)) + .build(); + + assertEquals(Arrays.asList(first, second, second), + processThreeTimes(engine, gate)); + verifyDataRequests(2); + } + + /** + * What processThreeTimes records for a request whose response could not + * be read. + */ + private static final String FAILED = "failed"; + + /** + * Process three Flow Data with the same Evidence and read each response. + * The first request is held back until its process has returned, so its + * result is offered to the cache while the request is still running. + */ + private List processThreeTimes( + CloudRequestEngine engine, + CountDownLatch firstGate) throws Exception { + List answers = new ArrayList<>(); + try (Pipeline pipeline = new PipelineBuilder(loggerFactory) + .addFlowElement(engine).build()) { + for (int i = 0; i < 3; i++) { + try (FlowData data = pipeline.createFlowData()) { + data.addEvidence("query.User-Agent", "iPhone"); + data.process(); + firstGate.countDown(); + try { + answers.add(data.get(CloudRequestData.class) + .getJsonResponse()); + } catch (RuntimeException e) { + answers.add(FAILED); + } + } + } + } + return answers; + } + + private void verifyDataRequests(int count) throws Exception { + verify(httpClient, times(count)).postData( + argThat(c -> c.getURL().getPath().endsWith("properties") == false && + c.getURL().getPath().endsWith("evidencekeys") == false), + any(), + any()); + } + private List processTwice(CloudRequestEngine engine) throws Exception { List answers = new ArrayList<>(); From 7b884b698d5acac7e0b862c98be42a77d0bd7fe1 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sat, 21 Nov 2026 22:42:11 +0000 Subject: [PATCH 2/2] Keep a replaced cache item in the list of the item it replaces With updateExisting set, LruCacheBase.add made the replacement on a list chosen at random and spliced it into the old item's place, which is in the old item's list. With more than one list, the default on any machine with more than one processor, the old list kept the replaced item as its first or last, the new list's first or last pointed into the old list, and the replacement named a list it was not in. Later moves and evictions walked these crossed chains and threw NullPointerException. No main code set updateExisting before the Cloud Request Engine's builder did. The replacement is now made on the old item's list. A new test replaces forty items in a cache with four lists, evicts past its size, and checks that each list runs from its first item to its last through items that belong to it. The Cloud Request Engine's builder javadoc now says the builder is set, not copied, and a further test fills a small cache after a replaced entry. --- .../java/fiftyone/caching/LruCacheBase.java | 7 +- .../fiftyone/caching/LruPutCacheTests.java | 68 +++++++++++++++++++ .../CloudRequestEngineBuilder.java | 4 +- .../CloudRequestEngineLazyCacheTests.java | 38 +++++++++++ 4 files changed, 114 insertions(+), 3 deletions(-) diff --git a/pipeline/pipeline.caching/src/main/java/fiftyone/caching/LruCacheBase.java b/pipeline/pipeline.caching/src/main/java/fiftyone/caching/LruCacheBase.java index df581768e..72d4006bd 100644 --- a/pipeline/pipeline.caching/src/main/java/fiftyone/caching/LruCacheBase.java +++ b/pipeline/pipeline.caching/src/main/java/fiftyone/caching/LruCacheBase.java @@ -165,8 +165,11 @@ protected CachedItem add(K key, V value) { } else { if (updateExisting) { - newNode.list.replace(node, newNode); - node = newNode; + // The replacement takes the existing item's place in the + // existing item's list, so it must belong to that list. + CachedItem replacement = new CachedItem(node.list, key, value); + node.list.replace(node, replacement); + node = replacement; } // The item is in the dictionary. // Move the item to the head of it's LRU list. diff --git a/pipeline/pipeline.caching/src/test/java/fiftyone/caching/LruPutCacheTests.java b/pipeline/pipeline.caching/src/test/java/fiftyone/caching/LruPutCacheTests.java index d7c469d4f..798b15c93 100644 --- a/pipeline/pipeline.caching/src/test/java/fiftyone/caching/LruPutCacheTests.java +++ b/pipeline/pipeline.caching/src/test/java/fiftyone/caching/LruPutCacheTests.java @@ -218,4 +218,72 @@ public void LruPutCache_Replace() { "The existing value was not overwritten in the cache", "replacement", result); } + + /** + * A replaced item stays in the linked list the item it replaces was in, + * so a cache with several lists stays whole through replacements and the + * evictions after them. Each new item is put on a list chosen at random, + * so with four lists and forty replacements, a replacement that joined + * another list is all but certain to break the lists checked here. + */ + @Test + public void LruPutCache_ReplaceWithSeveralLists() { + final int size = 10; + PutCache cache = null; + cache = new LruPutCache.Builder() + .setUpdateExisting(true) + .setConcurrency(4) + .build(cache, size); + for (int i = 0; i < size; i++) { + cache.put(i, "first " + i); + } + for (int round = 0; round < 4; round++) { + for (int i = 0; i < size; i++) { + cache.put(i, "replacement " + round + " " + i); + } + } + assertListsWhole((LruPutCache) cache); + for (int i = 0; i < size; i++) { + assertEquals("replacement 3 " + i, cache.get(i)); + } + + // Put past the cache's size, which removes items from the ends of + // the lists, and read the items back. + for (int i = size; i < size * 10; i++) { + cache.put(i, "value " + i); + cache.get(i); + } + assertListsWhole((LruPutCache) cache); + } + + /** + * More items than any list in LruPutCache_ReplaceWithSeveralLists can + * hold, so a walk that takes more steps has found a loop. + */ + private static final int MAX_LIST_STEPS = 1000; + + /** + * Each linked list runs from its first item to its last through items + * that belong to it, with each item's previous item the one before it. + */ + private static void assertListsWhole(LruPutCache cache) { + for (LruCacheBase.CacheLinkedList list : + cache.linkedLists) { + LruCacheBase.CachedItem previous = null; + LruCacheBase.CachedItem item = list.first; + int steps = 0; + while (item != null) { + assertTrue("A list loops back on itself", + ++steps <= MAX_LIST_STEPS); + assertSame("An item is in a list it does not belong to", + list, item.list); + assertSame("An item's previous item is not the one before it", + previous, item.previous); + previous = item; + item = item.next; + } + assertSame("The list's last item is not the end of its chain", + previous, list.last); + } + } } diff --git a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java index 50630c81d..c458af103 100644 --- a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java +++ b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java @@ -430,7 +430,9 @@ public CloudRequestEngineBuilder setCacheSize(int size) { * for its key. A result is stored while its cloud request is still * running, and an entry whose request then fails, or answers with a * 51Did, is never served, so the next result for that key must take its - * place. + * place. The builder in the {@link CacheConfiguration} is set, not + * copied, so a configuration shared with another engine's builder is set + * for that engine too. * @param engine the engine to configure * @throws Exception if the engine could not be configured */ diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java index bbcf28862..28bc715d3 100644 --- a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java +++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java @@ -394,6 +394,44 @@ public void process_ResultAfterAFodIdIsCached() throws Exception { verifyDataRequests(2); } + /** + * After a failed entry is replaced, requests with other Evidence fill + * the cache past its size, and every request still gets its own + * response. + */ + @Test + public void process_ReplacedEntryThenEvictions() throws Exception { + final int others = 40; + String[] responses = new String[others + 2]; + responses[0] = null; + responses[1] = "{'device':{'value':'replacement'}}"; + for (int i = 0; i < others; i++) { + responses[i + 2] = "{'device':{'value':'" + i + "'}}"; + } + CountDownLatch gate = new CountDownLatch(1); + mockService(new CountDownLatch(1), gate, responses); + CloudRequestEngine engine = + new CloudRequestEngineBuilder(loggerFactory, httpClient) + .setResourceKey("resourcekey") + .setCacheSize(4) + .build(); + + List answers = processThreeTimes(engine, gate); + assertEquals(Arrays.asList(FAILED, responses[1], responses[1]), + answers); + try (Pipeline pipeline = new PipelineBuilder(loggerFactory) + .addFlowElement(engine).build()) { + for (int i = 0; i < others; i++) { + try (FlowData data = pipeline.createFlowData()) { + data.addEvidence("query.User-Agent", "other " + i); + data.process(); + assertEquals(responses[i + 2], + data.get(CloudRequestData.class).getJsonResponse()); + } + } + } + } + /** * What processThreeTimes records for a request whose response could not * be read.