Refactor complex admin and storage methods
This commit is contained in:
1 parent
5304dfc047
commit
3c739deb09
13 files changed
+1434
-950
No files matched your search
@@ -0,0 +1,137 @@
|
||||
package cloud.lunarsky.store;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public final class AdminBulkJobsTest {
|
||||
private static final String BUCKET = "test-bucket";
|
||||
private static final ObjectStorage.Metadata METADATA = new ObjectStorage.Metadata(
|
||||
4, 0, "etag", new byte[32], BUCKET, "", "text/plain");
|
||||
|
||||
@FunctionalInterface
|
||||
private interface Listing {
|
||||
ObjectStorage.ListPage get(int count, String after) throws Exception;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface Deletion {
|
||||
void delete(String key) throws Exception;
|
||||
}
|
||||
|
||||
private static void check(boolean value, String message) {
|
||||
if (!value) throw new AssertionError(message);
|
||||
}
|
||||
|
||||
private static ObjectStorage storage(Listing listing, Deletion deletion) {
|
||||
return (ObjectStorage) Proxy.newProxyInstance(ObjectStorage.class.getClassLoader(),
|
||||
new Class<?>[]{ObjectStorage.class}, (proxy, method, arguments) -> switch (method.getName()) {
|
||||
case "bucket" -> new ObjectStorage.Bucket(BUCKET, 0);
|
||||
case "list" -> listing.get((int) arguments[3], (String) arguments[4]);
|
||||
case "delete" -> {
|
||||
deletion.delete((String) arguments[1]);
|
||||
yield null;
|
||||
}
|
||||
default -> throw new AssertionError("Unexpected storage operation: " + method.getName());
|
||||
});
|
||||
}
|
||||
|
||||
private static ObjectStorage.ListPage page(int start, int end, String next, boolean truncated) {
|
||||
List<ObjectStorage.ListedObject> objects = IntStream.range(start, end)
|
||||
.mapToObj(index -> new ObjectStorage.ListedObject(Integer.toString(index), METADATA)).toList();
|
||||
return new ObjectStorage.ListPage(objects, List.of(), next, truncated);
|
||||
}
|
||||
|
||||
private static AdminBulkJobs.Spec spec(String operation) {
|
||||
return new AdminBulkJobs.Spec(operation, BUCKET, "", "", "", "", "");
|
||||
}
|
||||
|
||||
private static AdminBulkJobs.Snapshot finished(ArrayBlockingQueue<AdminBulkJobs.Snapshot> results)
|
||||
throws InterruptedException {
|
||||
AdminBulkJobs.Snapshot result = results.poll(10, TimeUnit.SECONDS);
|
||||
if (result == null) throw new AssertionError("Bulk job did not finish");
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void itemLimit(int total, boolean oversizedPage) throws Exception {
|
||||
ObjectStorage store = storage((count, after) -> {
|
||||
int start = after == null ? 0 : Integer.parseInt(after) + 1;
|
||||
int end = oversizedPage ? total : Math.min(start + count, total);
|
||||
return page(start, end, Integer.toString(end - 1), end < total);
|
||||
}, key -> { });
|
||||
var results = new ArrayBlockingQueue<AdminBulkJobs.Snapshot>(2);
|
||||
AdminBulkJobs jobs = new AdminBulkJobs(store, results::add);
|
||||
jobs.start(spec("inventory"));
|
||||
AdminBulkJobs.Snapshot result = finished(results);
|
||||
boolean capped = total > AdminBulkJobs.MAX_ITEMS;
|
||||
check(result.capped() == capped, "Incorrect cap at the item boundary");
|
||||
check(result.state().equals(capped ? "Capped" : "Completed"), "Incorrect completion state");
|
||||
check(result.processed() == AdminBulkJobs.MAX_ITEMS && result.succeeded() == result.processed(),
|
||||
"Item limit lost or overran entries");
|
||||
check(!jobs.hasActiveJob(), "Finished job is still active");
|
||||
}
|
||||
|
||||
private static void cancellation() throws Exception {
|
||||
CountDownLatch entered = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
AtomicInteger deletes = new AtomicInteger();
|
||||
ObjectStorage store = storage((count, after) -> page(0, 3, null, false), key -> {
|
||||
deletes.incrementAndGet();
|
||||
entered.countDown();
|
||||
if (!release.await(10, TimeUnit.SECONDS)) throw new AssertionError("Delete was not released");
|
||||
});
|
||||
var results = new ArrayBlockingQueue<AdminBulkJobs.Snapshot>(2);
|
||||
AdminBulkJobs jobs = new AdminBulkJobs(store, results::add);
|
||||
AdminBulkJobs.Snapshot started = jobs.start(spec("delete"));
|
||||
try {
|
||||
check(entered.await(10, TimeUnit.SECONDS), "Delete did not start");
|
||||
jobs.cancel(started.id());
|
||||
} finally {
|
||||
release.countDown();
|
||||
}
|
||||
AdminBulkJobs.Snapshot result = finished(results);
|
||||
check(result.state().equals("Cancelled") && result.processed() == 1 && result.succeeded() == 1,
|
||||
"Cancellation did not finish the in-progress entry");
|
||||
check(deletes.get() == 1, "Cancellation started another entry");
|
||||
}
|
||||
|
||||
private static void stalledPagination() throws Exception {
|
||||
ObjectStorage store = storage((count, after) -> page(0, 1, "0", true), key -> { });
|
||||
var results = new ArrayBlockingQueue<AdminBulkJobs.Snapshot>(2);
|
||||
AdminBulkJobs jobs = new AdminBulkJobs(store, results::add);
|
||||
jobs.start(spec("inventory"));
|
||||
AdminBulkJobs.Snapshot result = finished(results);
|
||||
check(result.state().equals("Failed") && result.processed() == 2,
|
||||
"A repeated pagination marker did not stop the job");
|
||||
check(jobs.report(result.id()).contains("IOException"), "Listing failure missing from report");
|
||||
}
|
||||
|
||||
private static void entryFailure() throws Exception {
|
||||
ObjectStorage store = storage((count, after) -> page(0, 3, null, false), key -> {
|
||||
if (key.equals("1")) throw new StoreException(403, "AccessDenied", "x".repeat(300));
|
||||
});
|
||||
var results = new ArrayBlockingQueue<AdminBulkJobs.Snapshot>(2);
|
||||
AdminBulkJobs jobs = new AdminBulkJobs(store, results::add);
|
||||
jobs.start(spec("delete"));
|
||||
AdminBulkJobs.Snapshot result = finished(results);
|
||||
check(result.state().equals("Completed") && result.processed() == 3 &&
|
||||
result.succeeded() == 2 && result.failed() == 1, "Entry failure aborted or miscounted the job");
|
||||
String report = jobs.report(result.id());
|
||||
check(report.contains("AccessDenied: ") && !report.contains("x".repeat(201)),
|
||||
"Entry failure detail was missing or unbounded");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
itemLimit(AdminBulkJobs.MAX_ITEMS, false);
|
||||
itemLimit(AdminBulkJobs.MAX_ITEMS + 1, false);
|
||||
itemLimit(AdminBulkJobs.MAX_ITEMS + 1, true);
|
||||
cancellation();
|
||||
stalledPagination();
|
||||
entryFailure();
|
||||
System.out.println("Admin bulk job tests passed: cap, cancellation, pagination, errors");
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -3,6 +3,14 @@ package cloud.lunarsky.store;
|
||||
public final class RequestDiagnosticsTest {
|
||||
public static void main(String[] args) {
|
||||
RequestDiagnostics diagnostics = new RequestDiagnostics();
|
||||
rollingCounts(diagnostics);
|
||||
boundedFailures(diagnostics);
|
||||
keyCounters();
|
||||
failureLabelsAndLatency();
|
||||
System.out.println("Request diagnostics tests passed");
|
||||
}
|
||||
|
||||
private static void rollingCounts(RequestDiagnostics diagnostics) {
|
||||
diagnostics.record("request-1", "Object GET", "photos", 200, 4_000_000);
|
||||
diagnostics.record("request-2", "Object PUT", "photos", 403, 16_000_000);
|
||||
diagnostics.record("request-3", "Object GET", "a".repeat(200), 503, 700_000_000);
|
||||
@@ -14,6 +22,9 @@ public final class RequestDiagnosticsTest {
|
||||
!result.recentFailures().get(0).bucket().equals("—") ||
|
||||
!result.recentFailures().get(1).bucket().equals("photos"))
|
||||
throw new AssertionError("Rolling request diagnostics changed");
|
||||
}
|
||||
|
||||
private static void boundedFailures(RequestDiagnostics diagnostics) {
|
||||
for (int index = 0; index < 70; index++)
|
||||
diagnostics.record("bounded-" + index, "Object GET", "photos", 404, 1_000_000);
|
||||
RequestDiagnostics.Snapshot bounded = diagnostics.snapshot();
|
||||
@@ -21,6 +32,33 @@ public final class RequestDiagnosticsTest {
|
||||
!bounded.recentFailures().get(0).requestId().equals("bounded-69") ||
|
||||
!bounded.recentFailures().get(63).requestId().equals("bounded-6"))
|
||||
throw new AssertionError("Recent failure history is not bounded");
|
||||
System.out.println("Request diagnostics tests passed");
|
||||
}
|
||||
|
||||
private static void keyCounters() {
|
||||
RequestDiagnostics diagnostics = new RequestDiagnostics();
|
||||
diagnostics.record("key-ok", "Object GET", "photos", 200, 1_000_000, "reader");
|
||||
diagnostics.record("key-error", "Object GET", "photos", 403, 1_000_000, "reader");
|
||||
diagnostics.record("anonymous", "Object GET", "photos", 200, 1_000_000, null);
|
||||
diagnostics.record("invalid-key", "Object GET", "photos", 403, 1_000_000, "x".repeat(129));
|
||||
var activity = diagnostics.keyActivity();
|
||||
if (activity.size() != 1) throw new AssertionError("Invalid principals entered key counters");
|
||||
var reader = activity.getFirst();
|
||||
if (!reader.principal().equals("reader") || reader.requests() != 2 ||
|
||||
reader.failures() != 1 || reader.lastSeenMillis() <= 0)
|
||||
throw new AssertionError("Authenticated key activity changed");
|
||||
}
|
||||
|
||||
private static void failureLabelsAndLatency() {
|
||||
RequestDiagnostics diagnostics = new RequestDiagnostics();
|
||||
diagnostics.record("missing-label", null, "invalid/bucket", 404, -1);
|
||||
diagnostics.record("long-label", "x".repeat(33), "photos", 503, 10_001_000_000L);
|
||||
var result = diagnostics.snapshot();
|
||||
var newest = result.recentFailures().getFirst();
|
||||
var oldest = result.recentFailures().getLast();
|
||||
if (!newest.operation().equals("Other") || !newest.bucket().equals("photos") ||
|
||||
!oldest.operation().equals("Service") || !oldest.bucket().equals("—"))
|
||||
throw new AssertionError("Failure labels were not normalized");
|
||||
if (result.p50Millis() != 1 || result.p95Millis() != -1 || result.p99Millis() != -1)
|
||||
throw new AssertionError("Latency underflow or overflow bucket changed");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user