Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, String> 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<Integer, String>) 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<Integer, String>) 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<Integer, String> cache) {
for (LruCacheBase<Integer, String>.CacheLinkedList list :
cache.linkedLists) {
LruCacheBase<Integer, String>.CachedItem previous = null;
LruCacheBase<Integer, String>.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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -421,4 +422,28 @@ 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. 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
*/
@Override
protected void configureEngine(CloudRequestEngine engine)
throws Exception {
if (cacheConfig != null &&
cacheConfig.getCacheBuilder() instanceof PutCacheBuilder) {
((PutCacheBuilder) cacheConfig.getCacheBuilder())
.setUpdateExisting(true);
}
super.configureEngine(engine);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,125 @@ 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);
}

/**
* 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<String> 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.
*/
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<String> processThreeTimes(
CloudRequestEngine engine,
CountDownLatch firstGate) throws Exception {
List<String> 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<String> processTwice(CloudRequestEngine engine)
throws Exception {
List<String> answers = new ArrayList<>();
Expand Down
Loading