208 lines
9.7 KiB
Java
208 lines
9.7 KiB
Java
package cloud.lunarsky.store;
|
|
|
|
import com.sun.net.httpserver.HttpServer;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.InputStream;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.URI;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpResponse;
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.channels.FileChannel;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.time.Clock;
|
|
import java.util.Arrays;
|
|
import java.util.Comparator;
|
|
import java.util.Map;
|
|
import java.util.concurrent.Executors;
|
|
|
|
public final class LargeObjectTest {
|
|
private static final long LARGE_LENGTH = 3L * 1024 * 1024 * 1024 + 17;
|
|
private static final byte[] TAIL = "last bytes past the integer boundary".getBytes(
|
|
java.nio.charset.StandardCharsets.US_ASCII);
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
configuration();
|
|
Path root = Files.createTempDirectory("large-object-test-");
|
|
try {
|
|
boundedUploads(root.resolve("uploads"));
|
|
sparseReads(root.resolve("sparse"));
|
|
} finally {
|
|
try (var files = Files.walk(root)) {
|
|
for (Path file : files.sorted(Comparator.reverseOrder()).toList()) Files.delete(file);
|
|
}
|
|
}
|
|
System.out.println("Large object limits, bounded uploads and sparse range tests passed");
|
|
}
|
|
|
|
private static void configuration() {
|
|
var defaults = StorageLimits.fromEnvironment(Map.of());
|
|
check(defaults.maxObjectBytes() == 134217728L, "Object default changed");
|
|
check(defaults.maxTotalBytes() == 2147483648L, "Total default changed");
|
|
long maximum = StorageLimits.MAX_OBJECT_BYTES;
|
|
check(limits(maximum, maximum).maxObjectBytes() == maximum, "64 GiB ceiling rejected");
|
|
check(limits(LARGE_LENGTH, Long.MAX_VALUE).maxObjectBytes() == LARGE_LENGTH,
|
|
"Object size was narrowed to an integer");
|
|
invalidLimits(0, maximum);
|
|
invalidLimits(-1, maximum);
|
|
invalidLimits(maximum + 1, maximum + 1);
|
|
invalidLimits(maximum, maximum - 1);
|
|
invalidLimits(1, -1);
|
|
try {
|
|
StorageLimits.fromEnvironment(Map.of("MAX_OBJECT_BYTES", "not-a-size"));
|
|
throw new AssertionError("Non-numeric limit accepted");
|
|
} catch (IllegalArgumentException expected) {
|
|
check(expected.getMessage().contains("MAX_OBJECT_BYTES"), "Missing setting in error");
|
|
}
|
|
}
|
|
|
|
private static ObjectStorage.Limits limits(long object, long total) {
|
|
return StorageLimits.fromEnvironment(Map.of("MAX_OBJECT_BYTES", Long.toString(object),
|
|
"MAX_TOTAL_BYTES", Long.toString(total)));
|
|
}
|
|
|
|
private static void invalidLimits(long object, long total) {
|
|
try {
|
|
limits(object, total);
|
|
throw new AssertionError("Invalid storage limits accepted");
|
|
} catch (IllegalArgumentException expected) { }
|
|
}
|
|
|
|
private static void boundedUploads(Path root) throws Exception {
|
|
try (var store = new DiskStore(root, StorageLimits.MAX_OBJECT_BYTES,
|
|
StorageLimits.MAX_OBJECT_BYTES * 2)) {
|
|
store.ensureBucket("objects");
|
|
int length = 8 * 1024 * 1024 + 17;
|
|
var body = new BoundedInput(length);
|
|
var metadata = store.put("objects", "stream", body, length, null, null, false,
|
|
"application/octet-stream");
|
|
check(metadata.length() == length && body.remaining == 0, "Upload truncated");
|
|
try (var opened = store.open("objects", "stream")) {
|
|
check(opened.stream().transferTo(java.io.OutputStream.nullOutputStream()) == length,
|
|
"Streamed download truncated");
|
|
}
|
|
StoreTest.fails(400, () -> store.put("objects", "incomplete", new BoundedInput(17),
|
|
LARGE_LENGTH, null, null, false, "application/octet-stream"));
|
|
StoreTest.fails(413, () -> store.put("objects", "too-large", unreadable(),
|
|
StorageLimits.MAX_OBJECT_BYTES + 1, null, null, false, "application/octet-stream"));
|
|
multipartBoundaries(store);
|
|
check(store.usedBytes() == length, "Failed upload changed accounted bytes");
|
|
try (var pending = Files.list(root.resolve("pending"))) {
|
|
check(pending.findAny().isEmpty(), "Failed upload left a staged file");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void multipartBoundaries(DiskStore store) throws Exception {
|
|
var multipart = new MultipartStore(store);
|
|
String id = multipart.create("objects", "multipart", "application/octet-stream",
|
|
Map.of(), Map.of(), Map.of());
|
|
StoreTest.fails(400, () -> multipart.putPart(id, "objects", "multipart", 1,
|
|
new BoundedInput(17), LARGE_LENGTH, null, null));
|
|
StoreTest.fails(413, () -> multipart.putPart(id, "objects", "multipart", 1,
|
|
unreadable(), StorageLimits.MAX_OBJECT_BYTES + 1, null, null));
|
|
check(multipart.stagedBytes() == 0, "Failed part changed accounted bytes");
|
|
multipart.abort(id, "objects", "multipart");
|
|
}
|
|
|
|
private static InputStream unreadable() {
|
|
return new InputStream() {
|
|
@Override public int read() { throw new AssertionError("Rejected payload was read"); }
|
|
};
|
|
}
|
|
|
|
private static final class BoundedInput extends InputStream {
|
|
private long remaining;
|
|
|
|
BoundedInput(long remaining) { this.remaining = remaining; }
|
|
|
|
@Override public int read() {
|
|
if (remaining == 0) return -1;
|
|
remaining--;
|
|
return 71;
|
|
}
|
|
|
|
@Override public int read(byte[] bytes, int offset, int length) {
|
|
check(bytes.length <= 65536, "Upload allocated a payload-sized buffer");
|
|
if (length == 0) return 0;
|
|
if (remaining == 0) return -1;
|
|
int count = (int) Math.min(length, remaining);
|
|
Arrays.fill(bytes, offset, offset + count, (byte) 71);
|
|
remaining -= count;
|
|
return count;
|
|
}
|
|
}
|
|
|
|
private static void sparseReads(Path root) throws Exception {
|
|
sparseFixture(root);
|
|
try (var store = new DiskStore(root, StorageLimits.MAX_OBJECT_BYTES,
|
|
StorageLimits.MAX_OBJECT_BYTES * 2)) {
|
|
check(store.usedBytes() == LARGE_LENGTH, "Restart truncated the object length");
|
|
var executor = Executors.newVirtualThreadPerTaskExecutor();
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 8);
|
|
try (HttpClient client = HttpClient.newHttpClient()) {
|
|
var app = new Main(store, new SigV4("TESTACCESSKEY123",
|
|
"test-secret-key-that-is-at-least-32-characters", "us-east-1", Clock.systemUTC()),
|
|
"objects");
|
|
server.setExecutor(executor);
|
|
server.createContext("/", app::handle);
|
|
server.start();
|
|
URI uri = URI.create("http://127.0.0.1:" + server.getAddress().getPort() + "/objects/sparse");
|
|
readRequests(client, uri);
|
|
} finally {
|
|
server.stop(0);
|
|
executor.close();
|
|
}
|
|
store.delete("objects", "sparse");
|
|
check(store.usedBytes() == 0, "Deleting a large object left accounted bytes");
|
|
}
|
|
}
|
|
|
|
private static void readRequests(HttpClient client, URI uri) throws Exception {
|
|
var head = client.send(HttpTest.signedUri(uri, "HEAD", new byte[0], Map.of()),
|
|
HttpResponse.BodyHandlers.ofByteArray());
|
|
check(head.statusCode() == 200, "Large object HEAD failed");
|
|
check(head.headers().firstValueAsLong("Content-Length").orElse(-1) == LARGE_LENGTH,
|
|
"HEAD narrowed Content-Length");
|
|
String start = Long.toString(LARGE_LENGTH - TAIL.length);
|
|
for (String range : new String[] {"bytes=" + start + "-", "bytes=-" + TAIL.length}) {
|
|
var response = client.send(HttpTest.signedUri(uri, "GET", new byte[0], Map.of("range", range)),
|
|
HttpResponse.BodyHandlers.ofByteArray());
|
|
check(response.statusCode() == 206 && Arrays.equals(response.body(), TAIL),
|
|
"Range beyond 2 GiB returned incorrect data");
|
|
check(response.headers().firstValue("Content-Range").orElse("").equals(
|
|
"bytes " + start + "-" + (LARGE_LENGTH - 1) + "/" + LARGE_LENGTH),
|
|
"Range metadata narrowed the object length");
|
|
}
|
|
}
|
|
|
|
/** Builds a sparse length fixture; it does not write or hash gigabytes of data. */
|
|
private static void sparseFixture(Path root) throws Exception {
|
|
try (var store = new DiskStore(root, StorageLimits.MAX_OBJECT_BYTES,
|
|
StorageLimits.MAX_OBJECT_BYTES * 2)) {
|
|
store.ensureBucket("objects");
|
|
store.put("objects", "sparse", new ByteArrayInputStream(TAIL), TAIL.length, null, null,
|
|
false, "application/octet-stream");
|
|
}
|
|
Path record;
|
|
try (var files = Files.walk(root.resolve("objects"))) {
|
|
record = files.filter(Files::isRegularFile).findFirst().orElseThrow();
|
|
}
|
|
long headerLength = Files.size(record) - TAIL.length;
|
|
try (FileChannel file = FileChannel.open(record, StandardOpenOption.WRITE)) {
|
|
write(file, ByteBuffer.allocate(Long.BYTES).putLong(LARGE_LENGTH).flip(), Long.BYTES);
|
|
write(file, ByteBuffer.wrap(TAIL), headerLength + LARGE_LENGTH - TAIL.length);
|
|
}
|
|
}
|
|
|
|
private static void write(FileChannel file, ByteBuffer bytes, long position) throws Exception {
|
|
while (bytes.hasRemaining()) position += file.write(bytes, position);
|
|
}
|
|
|
|
private static void check(boolean condition, String message) {
|
|
if (!condition) throw new AssertionError(message);
|
|
}
|
|
}
|