From 208b4980887fa4e977f60a99804d4df4036d03ed Mon Sep 17 00:00:00 2001 From: nea Date: Mon, 6 Feb 2023 03:03:15 +0100 Subject: ApiUtil: Add cache with per request timeout and per class histogram --- .../commands/dev/DevTestCommand.java | 20 +++ .../options/customtypes/NEUDebugFlag.java | 5 + .../moulberry/notenoughupdates/util/ApiUtil.java | 33 ++++- .../moulberry/notenoughupdates/util/ApiCache.kt | 138 +++++++++++++++++++++ 4 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt (limited to 'src') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.java b/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.java index 35474ff3..8dda864a 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.java @@ -33,11 +33,13 @@ import io.github.moulberry.notenoughupdates.miscfeatures.customblockzones.Locati import io.github.moulberry.notenoughupdates.miscfeatures.customblockzones.SpecialBlockZone; import io.github.moulberry.notenoughupdates.miscgui.GuiPriceGraph; import io.github.moulberry.notenoughupdates.miscgui.minionhelper.MinionHelperManager; +import io.github.moulberry.notenoughupdates.util.ApiCache; import io.github.moulberry.notenoughupdates.util.PronounDB; import io.github.moulberry.notenoughupdates.util.SBInfo; import io.github.moulberry.notenoughupdates.util.TabListUtils; import io.github.moulberry.notenoughupdates.util.Utils; import io.github.moulberry.notenoughupdates.util.hypixelapi.ProfileCollectionInfo; +import lombok.var; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; import net.minecraft.command.CommandException; @@ -126,6 +128,24 @@ public class DevTestCommand extends ClientCommandBase { Utils.addChatMessage(EnumChatFormatting.RED + DEV_FAIL_STRINGS[devFailIndex++]); return; } + if (args.length == 1 && args[0].equalsIgnoreCase("dumpapihistogram")) { + synchronized (ApiCache.INSTANCE) { + Utils.addChatMessage("§e[NEU] API Request Histogram"); + Utils.addChatMessage("§e[NEU] §bClass Name§e: §aCached§e/§cNonCached§e/§dTotal"); + ApiCache.INSTANCE.getHistogramTotalRequests().forEach((className, totalRequests) -> { + var nonCachedRequests = ApiCache.INSTANCE.getHistogramNonCachedRequests().getOrDefault(className, 0); + var cachedRequests = totalRequests - nonCachedRequests; + Utils.addChatMessage( + String.format( + "§e[NEU] §b%s §a%d§e/§c%d§e/§d%d", + className, + cachedRequests, + nonCachedRequests, + totalRequests + )); + }); + } + } if (args.length == 1 && args[0].equalsIgnoreCase("testprofile")) { NotEnoughUpdates.INSTANCE.manager.apiUtils.newHypixelApiRequest("skyblock/profiles") .queryArgument( diff --git a/src/main/java/io/github/moulberry/notenoughupdates/options/customtypes/NEUDebugFlag.java b/src/main/java/io/github/moulberry/notenoughupdates/options/customtypes/NEUDebugFlag.java index 50f459c0..90ef93bb 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/options/customtypes/NEUDebugFlag.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/options/customtypes/NEUDebugFlag.java @@ -31,6 +31,7 @@ public enum NEUDebugFlag { WISHING("Wishing Compass Solver"), MAP("Dungeon Map Player Information"), SEARCH("SearchString Matches"), + API_CACHE("Api Cache"), ; private final String description; @@ -43,6 +44,10 @@ public enum NEUDebugFlag { return description; } + public void log(String message) { + NEUDebugLogger.log(this, message); + } + public boolean isSet() { return NEUDebugLogger.isFlagEnabled(this); } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java index 023be060..9ace59fc 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java @@ -48,8 +48,10 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; +import java.time.Duration; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -60,6 +62,11 @@ import java.util.zip.GZIPInputStream; public class ApiUtil { private static final Gson gson = new Gson(); + + private static final Comparator nameValuePairComparator = Comparator + .comparing(NameValuePair::getName) + .thenComparing(NameValuePair::getValue); + private static final ExecutorService executorService = Executors.newFixedThreadPool(3); private static String getUserAgent() { if (NotEnoughUpdates.INSTANCE.config.hidden.customUserAgent != null) { @@ -110,6 +117,7 @@ public class ApiUtil { private final List queryArguments = new ArrayList<>(); private String baseUrl = null; private boolean shouldGunzip = false; + private Duration cacheTimeout = Duration.ofSeconds(500); private String method = "GET"; private String postData = null; private String postContentType = null; @@ -119,6 +127,15 @@ public class ApiUtil { return this; } + /** + * Specify a cache timeout of {@code null} to signify an uncacheable request. + * Non {@code GET} requests are always uncacheable. + */ + public Request cacheTimeout(Duration cacheTimeout) { + this.cacheTimeout = cacheTimeout; + return this; + } + public Request url(String baseUrl) { this.baseUrl = baseUrl; return this; @@ -160,7 +177,17 @@ public class ApiUtil { return fut; } - public CompletableFuture requestString() { + public String getBaseUrl() { + return baseUrl; + } + + private ApiCache.CacheKey getCacheKey() { + if (!"GET".equals(method)) return null; + queryArguments.sort(nameValuePairComparator); + return new ApiCache.CacheKey(baseUrl, queryArguments, shouldGunzip); + } + + private CompletableFuture requestString0() { return buildUrl().thenApplyAsync(url -> { try { InputStream inputStream = null; @@ -216,6 +243,10 @@ public class ApiUtil { }, executorService); } + public CompletableFuture requestString() { + return ApiCache.INSTANCE.cacheRequest(this, getCacheKey(), this::requestString0, cacheTimeout); + } + public CompletableFuture requestJson() { return requestJson(JsonObject.class); } diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt new file mode 100644 index 00000000..49a79ae9 --- /dev/null +++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2023 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see . + */ + +package io.github.moulberry.notenoughupdates.util + +import io.github.moulberry.notenoughupdates.NotEnoughUpdates +import io.github.moulberry.notenoughupdates.options.customtypes.NEUDebugFlag +import io.github.moulberry.notenoughupdates.util.ApiUtil.Request +import org.apache.http.NameValuePair +import java.time.Duration +import java.util.* +import java.util.concurrent.CompletableFuture +import java.util.function.Supplier +import kotlin.time.Duration.Companion.hours +import kotlin.time.Duration.Companion.seconds +import kotlin.time.ExperimentalTime +import kotlin.time.TimeSource +import kotlin.time.toKotlinDuration + +@OptIn(ExperimentalTime::class) + +object ApiCache { + data class CacheKey( + val baseUrl: String, + val requestParameters: List, + val shouldGunzip: Boolean, + ) + + data class CacheResult( + val future: CompletableFuture, + val firedAt: TimeSource.Monotonic.ValueTimeMark, + ) + + val cachedRequests = mutableMapOf() + val histogramTotalRequests: MutableMap = mutableMapOf() + val histogramNonCachedRequests: MutableMap = mutableMapOf() + val timeout = 10.seconds + val maxCacheAge = 1.hours + + fun log(message: String) { + NEUDebugFlag.API_CACHE.log(message) + } + + fun traceApiRequest( + request: Request, + failReason: String?, + ) { + if (!NotEnoughUpdates.INSTANCE.config.hidden.dev) return + val callingClass = Thread.currentThread().stackTrace + .find { + !it.className.startsWith("java.") && + !it.className.startsWith("kotlin.") && + it.className != ApiCache::class.java.name && + it.className != ApiUtil::class.java.name && + it.className != Request::class.java.name + } + val callingClassText = callingClass?.let { + "${it.className}.${it.methodName} (${it.fileName}:${it.lineNumber})" + } ?: "no calling class found" + callingClass?.className?.let { + histogramTotalRequests[it] = (histogramTotalRequests[it] ?: 0) + 1 + if (failReason != null) + histogramNonCachedRequests[it] = (histogramNonCachedRequests[it] ?: 0) + 1 + } + if (failReason != null) { + log("Executing api request for url ${request.baseUrl} by $callingClassText: $failReason") + } else { + log("Cache hit for api request for url ${request.baseUrl} by $callingClassText.") + } + + } + + fun evictCache() { + synchronized(this) { + val it = cachedRequests.iterator() + while (it.hasNext()) { + if (it.next().value.firedAt.elapsedNow() >= maxCacheAge) + it.remove() + } + } + } + + fun cacheRequest( + request: Request, + cacheKey: CacheKey?, + futureSupplier: Supplier>, + maxAge: Duration? + ): CompletableFuture { + evictCache() + if (cacheKey == null) { + traceApiRequest(request, "uncacheable request (probably POST)") + return futureSupplier.get() + } + if (maxAge == null) { + traceApiRequest(request, "manually specified as uncacheable") + return futureSupplier.get() + } + fun recache(): CompletableFuture { + return futureSupplier.get().also { + cachedRequests[cacheKey] = CacheResult(it, TimeSource.Monotonic.markNow()) + } + } + synchronized(this) { + val cachedRequest = cachedRequests[cacheKey] + if (cachedRequest == null) { + traceApiRequest(request, "no cache found") + return recache() + } + if (cachedRequest.future.isDone && cachedRequest.firedAt.elapsedNow() > maxAge.toKotlinDuration()) { + traceApiRequest(request, "outdated cache") + return recache() + } + if (!cachedRequest.future.isDone && cachedRequest.firedAt.elapsedNow() > timeout) { + traceApiRequest(request, "suspiciously slow api response") + return recache() + } + traceApiRequest(request, null) + return cachedRequest.future + } + } + +} -- cgit