• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 23396903466

22 Mar 2026 05:56AM UTC coverage: 16.411% (+0.02%) from 16.387%
23396903466

push

github

tkuhn
fix: improve resilience to API outages and reduce log noise

Preserve stale cached data when API refresh fails instead of invalidating
it, return cached latest-version IDs when re-fetch fails, and demote
per-request preferences logging from INFO to DEBUG.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

743 of 5597 branches covered (13.27%)

Branch coverage included in aggregate %.

1892 of 10459 relevant lines covered (18.09%)

2.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.12
src/main/java/com/knowledgepixels/nanodash/ApiCache.java
1
package com.knowledgepixels.nanodash;
2

3
import com.google.common.cache.Cache;
4
import com.google.common.cache.CacheBuilder;
5
import org.eclipse.rdf4j.model.Model;
6
import org.nanopub.extra.services.*;
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9

10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
13
import java.util.Random;
14
import java.util.concurrent.ConcurrentHashMap;
15
import java.util.concurrent.ConcurrentMap;
16
import java.util.concurrent.TimeUnit;
17

18
/**
19
 * A utility class for caching API responses and maps to reduce redundant API calls.
20
 * This class is thread-safe and ensures that cached data is refreshed periodically.
21
 */
22
public class ApiCache {
23

24
    private ApiCache() {
25
    } // no instances allowed
26

27
    private static final int MAX_CACHE_ENTRIES = 10_000;
28

29
    private static final Cache<String, ApiResponse> cachedResponses = CacheBuilder.newBuilder()
6✔
30
        .maximumSize(MAX_CACHE_ENTRIES)
9✔
31
        .expireAfterAccess(24, TimeUnit.HOURS)
6✔
32
        .removalListener(n -> cleanupMetadata(n.getKey().toString()))
18✔
33
        .build();
6✔
34
    private static final Cache<String, Model> cachedRdfModels = CacheBuilder.newBuilder()
6✔
35
        .maximumSize(MAX_CACHE_ENTRIES)
9✔
36
        .expireAfterAccess(24, TimeUnit.HOURS)
6✔
37
        .removalListener(n -> cleanupMetadata(n.getKey().toString()))
3✔
38
        .build();
6✔
39
    private transient static ConcurrentMap<String, Integer> failed = new ConcurrentHashMap<>();
12✔
40
    private static final Cache<String, Map<String, String>> cachedMaps = CacheBuilder.newBuilder()
6✔
41
        .maximumSize(MAX_CACHE_ENTRIES)
9✔
42
        .expireAfterAccess(24, TimeUnit.HOURS)
6✔
43
        .removalListener(n -> cleanupMetadata(n.getKey().toString()))
3✔
44
        .build();
6✔
45
    private transient static ConcurrentMap<String, Long> lastRefresh = new ConcurrentHashMap<>();
12✔
46
    private transient static ConcurrentMap<String, Long> refreshStart = new ConcurrentHashMap<>();
12✔
47
    private transient static ConcurrentMap<String, Long> runAfter = new ConcurrentHashMap<>();
12✔
48
    private static final Logger logger = LoggerFactory.getLogger(ApiCache.class);
12✔
49

50
    private static void cleanupMetadata(String cacheId) {
51
        lastRefresh.remove(cacheId);
12✔
52
        failed.remove(cacheId);
12✔
53
        runAfter.remove(cacheId);
12✔
54
    }
3✔
55

56
    /**
57
     * Checks if a cache refresh is currently running for the given cache ID.
58
     *
59
     * @param cacheId The unique identifier for the cache.
60
     * @return True if a refresh is running, false otherwise.
61
     */
62
    private static boolean isRunning(String cacheId) {
63
        if (!refreshStart.containsKey(cacheId)) return false;
18✔
64
        return System.currentTimeMillis() - refreshStart.get(cacheId) < 60 * 1000;
42✔
65
    }
66

67
    /**
68
     * Checks if a cache refresh is currently running for the given QueryRef.
69
     *
70
     * @param queryRef The query reference
71
     * @return True if a refresh is running, false otherwise.
72
     */
73
    public static boolean isRunning(QueryRef queryRef) {
74
        return isRunning(queryRef.getAsUrlString());
12✔
75
    }
76

77
    /**
78
     * Updates the cached API response for a specific query reference.
79
     *
80
     * @param queryRef The query reference
81
     * @throws FailedApiCallException If the API call fails.
82
     */
83
    private static void updateResponse(QueryRef queryRef, boolean forced) throws FailedApiCallException, APINotReachableException, NotEnoughAPIInstancesException {
84
        ApiResponse response;
85
        if (forced) {
6✔
86
            response = QueryApiAccess.forcedGet(queryRef);
12✔
87
        } else {
88
            response = QueryApiAccess.get(queryRef);
9✔
89
        }
90
        String cacheId = queryRef.getAsUrlString();
9✔
91
        logger.info("Updating cached API response for {}", cacheId);
12✔
92
        cachedResponses.put(cacheId, response);
12✔
93
        lastRefresh.put(cacheId, System.currentTimeMillis());
18✔
94
    }
3✔
95

96
    public static ApiResponse retrieveResponseSync(QueryRef queryRef, boolean forced) {
97
        long timeNow = System.currentTimeMillis();
6✔
98
        String cacheId = queryRef.getAsUrlString();
9✔
99
        logger.info("Retrieving cached API response synchronously for {}", cacheId);
12✔
100
        boolean needsRefresh = true;
6✔
101
        if (cachedResponses.getIfPresent(cacheId) != null) {
12✔
102
            long cacheAge = timeNow - lastRefresh.get(cacheId);
24✔
103
            needsRefresh = cacheAge > 60 * 1000;
24✔
104
        }
105
        if (failed.get(cacheId) != null && failed.get(cacheId) > 2) {
33!
106
            failed.remove(cacheId);
12✔
107
            throw new RuntimeException("Query failed: " + cacheId);
18✔
108
        }
109
        if ((needsRefresh || forced) && !isRunning(cacheId)) {
21!
110
            logger.info("Refreshing cache for {}", cacheId);
12✔
111
            refreshStart.put(cacheId, timeNow);
18✔
112
            try {
113
                if (runAfter.containsKey(cacheId)) {
12!
114
                    while (System.currentTimeMillis() < runAfter.get(cacheId)) {
×
115
                        Thread.sleep(100);
×
116
                    }
117
                    runAfter.remove(cacheId);
×
118
                }
119
                if (failed.get(cacheId) != null) {
12!
120
                    // 1 second pause between failed attempts;
121
                    Thread.sleep(1000);
×
122
                }
123
                Thread.sleep(100 + new Random().nextLong(400));
24✔
124
            } catch (InterruptedException ex) {
×
125
                logger.error("Interrupted while waiting to refresh cache: {}", ex.getMessage());
×
126
            }
3✔
127
            try {
128
                ApiCache.updateResponse(queryRef, forced);
9✔
129
                failed.remove(cacheId);
12✔
130
            } catch (Exception ex) {
3✔
131
                logger.error("Failed to update cache for {}: {}", cacheId, ex.getMessage());
18✔
132
                // Keep stale cached data if available, only invalidate if nothing was cached
133
                if (cachedResponses.getIfPresent(cacheId) == null) {
12!
134
                    failed.merge(cacheId, 1, Integer::sum);
21✔
135
                }
136
                lastRefresh.put(cacheId, System.currentTimeMillis());
18✔
137
            } finally {
138
                refreshStart.remove(cacheId);
12✔
139
            }
140
        }
141
        return cachedResponses.getIfPresent(cacheId);
15✔
142
    }
143

144
    /**
145
     * Retrieves a cached API response for a specific QueryRef.
146
     *
147
     * @param queryRef The QueryRef object containing the query name and parameters.
148
     * @return The cached API response, or null if not cached.
149
     */
150
    public static ApiResponse retrieveResponseAsync(QueryRef queryRef) {
151
        long timeNow = System.currentTimeMillis();
6✔
152
        String cacheId = queryRef.getAsUrlString();
9✔
153
        logger.info("Retrieving cached API response asynchronously for {}", cacheId);
12✔
154
        boolean isCached = false;
6✔
155
        boolean needsRefresh = true;
6✔
156
        if (cachedResponses.getIfPresent(cacheId) != null) {
12✔
157
            long cacheAge = timeNow - lastRefresh.get(cacheId);
24✔
158
            isCached = cacheAge < 24 * 60 * 60 * 1000;
21!
159
            needsRefresh = cacheAge > 60 * 1000;
18!
160
        }
161
        if (failed.get(cacheId) != null && failed.get(cacheId) > 2) {
12!
162
            failed.remove(cacheId);
×
163
            throw new RuntimeException("Query failed: " + cacheId);
×
164
        }
165
        if (needsRefresh && !isRunning(cacheId)) {
15!
166
            refreshStart.put(cacheId, timeNow);
18✔
167
            NanodashThreadPool.submit(() -> {
15✔
168
                try {
169
                    if (runAfter.containsKey(cacheId)) {
12!
170
                        while (System.currentTimeMillis() < runAfter.get(cacheId)) {
×
171
                            Thread.sleep(100);
×
172
                        }
173
                        runAfter.remove(cacheId);
×
174
                    }
175
                    if (failed.get(cacheId) != null) {
12!
176
                        // 1 second pause between failed attempts;
177
                        Thread.sleep(1000);
×
178
                    }
179
                    Thread.sleep(100 + new Random().nextLong(400));
24✔
180
                } catch (InterruptedException ex) {
×
181
                    logger.error("Interrupted while waiting to refresh cache: {}", ex.getMessage());
×
182
                }
3✔
183
                try {
184
                    ApiCache.updateResponse(queryRef, false);
9✔
185
                    failed.remove(cacheId);
12✔
186
                } catch (Exception ex) {
×
187
                    logger.error("Failed to update cache for {}: {}", cacheId, ex.getMessage());
×
188
                    if (cachedResponses.getIfPresent(cacheId) == null) {
×
189
                        failed.merge(cacheId, 1, Integer::sum);
×
190
                    }
191
                    lastRefresh.put(cacheId, System.currentTimeMillis());
×
192
                } finally {
193
                    refreshStart.remove(cacheId);
12✔
194
                }
195
            });
3✔
196
        }
197
        if (isCached) {
6✔
198
            return cachedResponses.getIfPresent(cacheId);
15✔
199
        } else {
200
            return null;
6✔
201
        }
202
    }
203

204
    /**
205
     * Updates the cached map for a specific query reference.
206
     *
207
     * @param queryRef The query reference
208
     * @throws FailedApiCallException If the API call fails.
209
     */
210
    private static void updateMap(QueryRef queryRef) throws FailedApiCallException, APINotReachableException, NotEnoughAPIInstancesException {
211
        Map<String, String> map = new HashMap<>();
×
212
        List<ApiResponseEntry> respList = QueryApiAccess.get(queryRef).getData();
×
213
        while (respList != null && !respList.isEmpty()) {
×
214
            ApiResponseEntry resultEntry = respList.removeFirst();
×
215
            map.put(resultEntry.get("key"), resultEntry.get("value"));
×
216
        }
×
217
        String cacheId = queryRef.getAsUrlString();
×
218
        cachedMaps.put(cacheId, map);
×
219
        lastRefresh.put(cacheId, System.currentTimeMillis());
×
220
    }
×
221

222
    /**
223
     * Retrieves a cached map for a specific query reference.
224
     * If the cache is stale, it triggers a background refresh.
225
     *
226
     * @param queryRef The query reference
227
     * @return The cached map, or null if not cached.
228
     */
229
    public static synchronized Map<String, String> retrieveMap(QueryRef queryRef) {
230
        long timeNow = System.currentTimeMillis();
×
231
        String cacheId = queryRef.getAsUrlString();
×
232
        boolean isCached = false;
×
233
        boolean needsRefresh = true;
×
234
        if (cachedMaps.getIfPresent(cacheId) != null) {
×
235
            long cacheAge = timeNow - lastRefresh.get(cacheId);
×
236
            isCached = cacheAge < 24 * 60 * 60 * 1000;
×
237
            needsRefresh = cacheAge > 60 * 1000;
×
238
        }
239
        if (needsRefresh && !isRunning(cacheId)) {
×
240
            refreshStart.put(cacheId, timeNow);
×
241
            NanodashThreadPool.submit(() -> {
×
242
                try {
243
                    if (runAfter.containsKey(cacheId)) {
×
244
                        while (System.currentTimeMillis() < runAfter.get(cacheId)) {
×
245
                            Thread.sleep(100);
×
246
                        }
247
                        runAfter.remove(cacheId);
×
248
                    }
249
                    Thread.sleep(100 + new Random().nextLong(400));
×
250
                } catch (InterruptedException ex) {
×
251
                    logger.error("Interrupted while waiting to refresh cache: {}", ex.getMessage());
×
252
                }
×
253
                try {
254
                    ApiCache.updateMap(queryRef);
×
255
                } catch (Exception ex) {
×
256
                    logger.error("Failed to update cache for {}: {}", cacheId, ex.getMessage());
×
257
                    cachedResponses.invalidate(cacheId);
×
258
                    lastRefresh.put(cacheId, System.currentTimeMillis());
×
259
                }  finally {
260
                    refreshStart.remove(cacheId);
×
261
                }
262
            });
×
263
        }
264
        if (isCached) {
×
265
            if (cachedResponses.getIfPresent(cacheId) == null) {
×
266
                cachedResponses.invalidate(cacheId);
×
267
                throw new RuntimeException("Query failed: " + cacheId);
×
268
            }
269
            return cachedMaps.getIfPresent(cacheId);
×
270
        } else {
271
            return null;
×
272
        }
273
    }
274

275
    private static void updateRdfModel(QueryRef queryRef) throws FailedApiCallException, APINotReachableException, NotEnoughAPIInstancesException {
276
        final Model[] modelRef = new Model[1];
×
277
        QueryAccess qa = new QueryAccess() {
×
278
            @Override
279
            protected void processHeader(String[] line) {}
×
280
            @Override
281
            protected void processLine(String[] line) {}
×
282
            @Override
283
            protected void processRdfContent(Model model) {
284
                modelRef[0] = model;
×
285
            }
×
286
        };
287
        qa.call(queryRef);
×
288
        if (modelRef[0] == null) {
×
289
            throw new FailedApiCallException(new Exception("No RDF content in response for query: " + queryRef.getQueryId()));
×
290
        }
291
        String cacheId = queryRef.getAsUrlString();
×
292
        logger.info("Updating cached RDF model for {}", cacheId);
×
293
        cachedRdfModels.put(cacheId, modelRef[0]);
×
294
        lastRefresh.put(cacheId, System.currentTimeMillis());
×
295
    }
×
296

297
    /**
298
     * Retrieves a cached RDF model for a CONSTRUCT query, triggering a background fetch if needed.
299
     *
300
     * @param queryRef The QueryRef for the CONSTRUCT query.
301
     * @return The cached RDF Model, or null if not yet available.
302
     */
303
    public static Model retrieveRdfModelAsync(QueryRef queryRef) {
304
        long timeNow = System.currentTimeMillis();
×
305
        String cacheId = queryRef.getAsUrlString();
×
306
        logger.info("Retrieving cached RDF model asynchronously for {}", cacheId);
×
307
        boolean isCached = false;
×
308
        boolean needsRefresh = true;
×
309
        if (cachedRdfModels.getIfPresent(cacheId) != null) {
×
310
            long cacheAge = timeNow - lastRefresh.get(cacheId);
×
311
            isCached = cacheAge < 24 * 60 * 60 * 1000;
×
312
            needsRefresh = cacheAge > 60 * 1000;
×
313
        }
314
        if (failed.get(cacheId) != null && failed.get(cacheId) > 2) {
×
315
            failed.remove(cacheId);
×
316
            throw new RuntimeException("Query failed: " + cacheId);
×
317
        }
318
        if (needsRefresh && !isRunning(cacheId)) {
×
319
            refreshStart.put(cacheId, timeNow);
×
320
            NanodashThreadPool.submit(() -> {
×
321
                try {
322
                    if (runAfter.containsKey(cacheId)) {
×
323
                        while (System.currentTimeMillis() < runAfter.get(cacheId)) {
×
324
                            Thread.sleep(100);
×
325
                        }
326
                        runAfter.remove(cacheId);
×
327
                    }
328
                    if (failed.get(cacheId) != null) {
×
329
                        Thread.sleep(1000);
×
330
                    }
331
                    Thread.sleep(100 + new Random().nextLong(400));
×
332
                } catch (InterruptedException ex) {
×
333
                    logger.error("Interrupted while waiting to refresh RDF cache: {}", ex.getMessage());
×
334
                }
×
335
                try {
336
                    updateRdfModel(queryRef);
×
337
                    failed.remove(cacheId);
×
338
                } catch (Exception ex) {
×
339
                    logger.error("Failed to update RDF cache for {}: {}", cacheId, ex.getMessage());
×
340
                    if (cachedRdfModels.getIfPresent(cacheId) == null) {
×
341
                        failed.merge(cacheId, 1, Integer::sum);
×
342
                    }
343
                    lastRefresh.put(cacheId, System.currentTimeMillis());
×
344
                } finally {
345
                    refreshStart.remove(cacheId);
×
346
                }
347
            });
×
348
        }
349
        if (isCached) {
×
350
            return cachedRdfModels.getIfPresent(cacheId);
×
351
        } else {
352
            return null;
×
353
        }
354
    }
355

356
    /**
357
     * Clears the cached response for a specific query reference and sets a delay before the next refresh can occur.
358
     *
359
     * @param queryRef   The query reference for which to clear the cache.
360
     * @param waitMillis The amount of time in milliseconds to wait before allowing the cache to be refreshed again.
361
     */
362
    public static void clearCache(QueryRef queryRef, long waitMillis) {
363
        if (waitMillis < 0) {
12✔
364
            throw new IllegalArgumentException("waitMillis must be non-negative");
15✔
365
        }
366
        cachedResponses.invalidate(queryRef.getAsUrlString());
12✔
367
        runAfter.put(queryRef.getAsUrlString(), System.currentTimeMillis() + waitMillis);
27✔
368
    }
3✔
369

370
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc