|
| 1 | +package dev.zarr.zarrjava; |
| 2 | + |
| 3 | +import dev.zarr.zarrjava.store.FilesystemStore; |
| 4 | +import dev.zarr.zarrjava.store.StoreHandle; |
| 5 | +import dev.zarr.zarrjava.v3.Array; |
| 6 | +import dev.zarr.zarrjava.v3.DataType; |
| 7 | +import org.junit.jupiter.api.Assertions; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.*; |
| 15 | + |
| 16 | +public class ParallelWriteTest extends ZarrTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testParallelWriteDataSafety() throws IOException, ZarrException { |
| 20 | + // Test internal parallelism of write method (using parallel=true) |
| 21 | + Path path = TESTOUTPUT.resolve("parallel_write_safety"); |
| 22 | + StoreHandle storeHandle = new FilesystemStore(path).resolve(); |
| 23 | + |
| 24 | + int shape = 1000; |
| 25 | + int chunk = 100; |
| 26 | + |
| 27 | + Array array = Array.create(storeHandle, Array.metadataBuilder() |
| 28 | + .withShape(shape, shape) |
| 29 | + .withDataType(DataType.INT32) |
| 30 | + .withChunkShape(chunk, chunk) |
| 31 | + .withFillValue(0) |
| 32 | + .build()); |
| 33 | + |
| 34 | + int[] data = new int[shape * shape]; |
| 35 | + // Fill with some deterministic pattern |
| 36 | + for (int i = 0; i < shape * shape; i++) { |
| 37 | + data[i] = i; |
| 38 | + } |
| 39 | + |
| 40 | + ucar.ma2.Array outputData = ucar.ma2.Array.factory(ucar.ma2.DataType.INT, new int[]{shape, shape}, data); |
| 41 | + |
| 42 | + // Write in parallel |
| 43 | + array.write(outputData, true); |
| 44 | + |
| 45 | + // Read back |
| 46 | + ucar.ma2.Array readData = array.read(); |
| 47 | + int[] readArr = (int[]) readData.get1DJavaArray(ucar.ma2.DataType.INT); |
| 48 | + |
| 49 | + Assertions.assertArrayEquals(data, readArr, "Data read back should match data written in parallel"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testParallelWriteWithSharding() throws IOException, ZarrException { |
| 54 | + // Test internal parallelism with Sharding (nested chunks + shared codec state potential) |
| 55 | + Path path = TESTOUTPUT.resolve("parallel_write_sharding"); |
| 56 | + StoreHandle storeHandle = new FilesystemStore(path).resolve(); |
| 57 | + |
| 58 | + int shape = 128; // 128x128 |
| 59 | + int shardSize = 64; // Shards are 64x64 |
| 60 | + int innerChunk = 32; // Inner chunks 32x32 |
| 61 | + |
| 62 | + // Metadata with sharding |
| 63 | + // With shape 128 and shardSize 64, we have 2x2 = 4 shards. |
| 64 | + // Array.write(parallel=true) will likely process these shards concurrently. |
| 65 | + dev.zarr.zarrjava.v3.ArrayMetadata metadata = Array.metadataBuilder() |
| 66 | + .withShape(shape, shape) |
| 67 | + .withDataType(DataType.INT32) |
| 68 | + .withChunkShape(shardSize, shardSize) // This sets the shard shape (outer chunks) |
| 69 | + .withCodecs(c -> c.withSharding(new int[]{innerChunk, innerChunk}, c2 -> c2.withBytes("LITTLE"))) |
| 70 | + .withFillValue(0) |
| 71 | + .build(); |
| 72 | + |
| 73 | + Array array = Array.create(storeHandle, metadata); |
| 74 | + |
| 75 | + int[] data = new int[shape * shape]; |
| 76 | + for (int i = 0; i < shape * shape; i++) { |
| 77 | + data[i] = i; |
| 78 | + } |
| 79 | + |
| 80 | + ucar.ma2.Array outputData = ucar.ma2.Array.factory(ucar.ma2.DataType.INT, new int[]{shape, shape}, data); |
| 81 | + |
| 82 | + // Write in parallel |
| 83 | + array.write(outputData, true); |
| 84 | + |
| 85 | + ucar.ma2.Array readData = array.read(); |
| 86 | + int[] readArr = (int[]) readData.get1DJavaArray(ucar.ma2.DataType.INT); |
| 87 | + |
| 88 | + Assertions.assertArrayEquals(data, readArr, "Sharded data written in parallel should match"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testConcurrentWritesDifferentChunks() throws IOException, ZarrException, InterruptedException, ExecutionException { |
| 93 | + // Test external parallelism (multiple threads calling write on same Array instance) |
| 94 | + Path path = TESTOUTPUT.resolve("concurrent_write_safety"); |
| 95 | + StoreHandle storeHandle = new FilesystemStore(path).resolve(); |
| 96 | + |
| 97 | + int chunksX = 10; |
| 98 | + int chunksY = 10; |
| 99 | + int chunkSize = 50; |
| 100 | + int shapeX = chunksX * chunkSize; |
| 101 | + int shapeY = chunksY * chunkSize; |
| 102 | + |
| 103 | + Array array = Array.create(storeHandle, Array.metadataBuilder() |
| 104 | + .withShape(shapeX, shapeY) |
| 105 | + .withDataType(DataType.INT32) |
| 106 | + .withChunkShape(chunkSize, chunkSize) |
| 107 | + .withFillValue(-1) |
| 108 | + .build()); |
| 109 | + |
| 110 | + ExecutorService executor = Executors.newFixedThreadPool(8); |
| 111 | + List<Callable<Void>> tasks = new ArrayList<>(); |
| 112 | + |
| 113 | + for (int i = 0; i < chunksX; i++) { |
| 114 | + for (int j = 0; j < chunksY; j++) { |
| 115 | + final int cx = i; |
| 116 | + final int cy = j; |
| 117 | + tasks.add(() -> { |
| 118 | + int[] chunkData = new int[chunkSize * chunkSize]; |
| 119 | + int val = cx * chunksY + cy; // Unique value per chunk |
| 120 | + java.util.Arrays.fill(chunkData, val); |
| 121 | + |
| 122 | + ucar.ma2.Array ucarArray = ucar.ma2.Array.factory(ucar.ma2.DataType.INT, new int[]{chunkSize, chunkSize}, chunkData); |
| 123 | + |
| 124 | + // Write to specific chunk offset |
| 125 | + long[] offset = new long[]{cx * chunkSize, cy * chunkSize}; |
| 126 | + // Use internal parallelism false to isolate external concurrency test mechanism |
| 127 | + array.write(offset, ucarArray, false); |
| 128 | + return null; |
| 129 | + }); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + List<Future<Void>> futures = executor.invokeAll(tasks); |
| 134 | + |
| 135 | + for (Future<Void> f : futures) { |
| 136 | + f.get(); // Check for exceptions |
| 137 | + } |
| 138 | + executor.shutdown(); |
| 139 | + |
| 140 | + // Verification |
| 141 | + ucar.ma2.Array readData = array.read(); |
| 142 | + for (int i = 0; i < chunksX; i++) { |
| 143 | + for (int j = 0; j < chunksY; j++) { |
| 144 | + int expectedVal = i * chunksY + j; |
| 145 | + int originX = i * chunkSize; |
| 146 | + int originY = j * chunkSize; |
| 147 | + |
| 148 | + // Verify a pixel in the chunk |
| 149 | + int val = readData.getInt(readData.getIndex().set(originX, originY)); |
| 150 | + Assertions.assertEquals(expectedVal, val, "Value at chunk " + i + "," + j + " mismatch"); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments