|
| 1 | +/* |
| 2 | + * Copyright 2024, AutoMQ HK Limited. |
| 3 | + * |
| 4 | + * The use of this file is governed by the Business Source License, |
| 5 | + * as detailed in the file "/LICENSE.S3Stream" included in this repository. |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +package com.automq.stream.s3.cache.blockcache; |
| 13 | + |
| 14 | +import com.automq.stream.s3.ObjectReader; |
| 15 | +import com.automq.stream.s3.TestUtils; |
| 16 | +import com.automq.stream.s3.cache.ReadDataBlock; |
| 17 | +import com.automq.stream.s3.metadata.S3ObjectMetadata; |
| 18 | +import com.automq.stream.s3.model.StreamRecordBatch; |
| 19 | +import com.automq.stream.s3.objects.ObjectManager; |
| 20 | +import com.automq.stream.s3.operator.MemoryObjectStorage; |
| 21 | +import com.automq.stream.s3.operator.ObjectStorage; |
| 22 | +import com.automq.stream.s3.trace.context.TraceContext; |
| 23 | +import com.automq.stream.utils.MockTime; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Tag; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.concurrent.CompletableFuture; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | + |
| 36 | +import static org.awaitility.Awaitility.await; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 39 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 40 | +import static org.mockito.ArgumentMatchers.eq; |
| 41 | +import static org.mockito.Mockito.mock; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | + |
| 44 | +@Tag("S3Unit") |
| 45 | +public class StreamReadersTest { |
| 46 | + private static final long STREAM_ID_1 = 100L; |
| 47 | + private static final long STREAM_ID_2 = 200L; |
| 48 | + private static final int BLOCK_SIZE_THRESHOLD = 1024; |
| 49 | + |
| 50 | + private Map<Long, MockObject> objects; |
| 51 | + private ObjectManager objectManager; |
| 52 | + private ObjectStorage objectStorage; |
| 53 | + private ObjectReaderFactory objectReaderFactory; |
| 54 | + private StreamReaders streamReaders; |
| 55 | + private MockTime mockTime; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void setup() { |
| 59 | + objects = new HashMap<>(); |
| 60 | + |
| 61 | + // Create mock objects for testing with different offset ranges |
| 62 | + // Object 1: STREAM_ID_1 offset 0-2 |
| 63 | + objects.put(1L, MockObject.builder(1L, BLOCK_SIZE_THRESHOLD).write(STREAM_ID_1, List.of( |
| 64 | + new StreamRecordBatch(STREAM_ID_1, 0, 0, 2, TestUtils.random(100)) |
| 65 | + )).build()); |
| 66 | + // Object 2: STREAM_ID_2 offset 0-1 |
| 67 | + objects.put(2L, MockObject.builder(2L, BLOCK_SIZE_THRESHOLD).write(STREAM_ID_2, List.of( |
| 68 | + new StreamRecordBatch(STREAM_ID_2, 0, 0, 1, TestUtils.random(100)) |
| 69 | + )).build()); |
| 70 | + |
| 71 | + objectManager = mock(ObjectManager.class); |
| 72 | + |
| 73 | + when(objectManager.isObjectExist(anyLong())).thenReturn(true); |
| 74 | + // Mock getObjects method to return appropriate objects based on offset ranges |
| 75 | + // For STREAM_ID_1, use the combined object that covers 0-2 range |
| 76 | + when(objectManager.getObjects(eq(STREAM_ID_1), anyLong(), anyLong(), anyInt())) |
| 77 | + .thenReturn(CompletableFuture.completedFuture(List.of(objects.get(1L).metadata))); |
| 78 | + // STREAM_ID_2 offset 0-1 -> object 3 |
| 79 | + when(objectManager.getObjects(eq(STREAM_ID_2), anyLong(), anyLong(), anyInt())) |
| 80 | + .thenReturn(CompletableFuture.completedFuture(List.of(objects.get(2L).metadata))); |
| 81 | + |
| 82 | + objectStorage = new MemoryObjectStorage(); |
| 83 | + |
| 84 | + objectReaderFactory = new ObjectReaderFactory() { |
| 85 | + @Override |
| 86 | + public ObjectReader get(S3ObjectMetadata metadata) { |
| 87 | + return objects.get(metadata.objectId()).objectReader(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public ObjectStorage getObjectStorage() { |
| 92 | + return objectStorage; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + mockTime = new MockTime(); |
| 97 | + streamReaders = new StreamReaders(Long.MAX_VALUE, objectManager, objectStorage, objectReaderFactory, 2, mockTime); |
| 98 | + } |
| 99 | + |
| 100 | + @AfterEach |
| 101 | + void tearDown() { |
| 102 | + if (streamReaders != null) { |
| 103 | + // Clean up resources |
| 104 | + streamReaders = null; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testStreamReaderCreationAndReuse() throws Exception { |
| 110 | + TraceContext context = TraceContext.DEFAULT; |
| 111 | + |
| 112 | + // Initially no StreamReaders |
| 113 | + assertEquals(0, streamReaders.getActiveStreamReaderCount()); |
| 114 | + |
| 115 | + // Create first StreamReader |
| 116 | + CompletableFuture<ReadDataBlock> readFuture1 = streamReaders.read(context, STREAM_ID_1, 0, 1, Integer.MAX_VALUE); |
| 117 | + ReadDataBlock result1 = readFuture1.get(5, TimeUnit.SECONDS); |
| 118 | + result1.getRecords().forEach(StreamRecordBatch::release); |
| 119 | + |
| 120 | + assertEquals(1, streamReaders.getActiveStreamReaderCount()); |
| 121 | + |
| 122 | + // Read from same stream again - should reuse existing StreamReader |
| 123 | + CompletableFuture<ReadDataBlock> readFuture2 = streamReaders.read(context, STREAM_ID_1, 1, 2, Integer.MAX_VALUE); |
| 124 | + ReadDataBlock result2 = readFuture2.get(5, TimeUnit.SECONDS); |
| 125 | + result2.getRecords().forEach(StreamRecordBatch::release); |
| 126 | + |
| 127 | + // Should still have 1 StreamReader (reused) |
| 128 | + assertEquals(1, streamReaders.getActiveStreamReaderCount()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testCleanupTrigger() throws Exception { |
| 133 | + TraceContext context = TraceContext.DEFAULT; |
| 134 | + |
| 135 | + // Create some StreamReaders |
| 136 | + CompletableFuture<ReadDataBlock> readFuture1 = streamReaders.read(context, STREAM_ID_1, 0, 1, Integer.MAX_VALUE); |
| 137 | + ReadDataBlock result1 = readFuture1.get(5, TimeUnit.SECONDS); |
| 138 | + result1.getRecords().forEach(StreamRecordBatch::release); |
| 139 | + |
| 140 | + CompletableFuture<ReadDataBlock> readFuture2 = streamReaders.read(context, STREAM_ID_2, 0, 1, Integer.MAX_VALUE); |
| 141 | + ReadDataBlock result2 = readFuture2.get(5, TimeUnit.SECONDS); |
| 142 | + result2.getRecords().forEach(StreamRecordBatch::release); |
| 143 | + |
| 144 | + assertEquals(2, streamReaders.getActiveStreamReaderCount()); |
| 145 | + |
| 146 | + // Trigger cleanup - should not affect non-expired readers |
| 147 | + streamReaders.triggerExpiredStreamReaderCleanup(); |
| 148 | + |
| 149 | + // Wait for async cleanup to complete |
| 150 | + await().atMost(1, TimeUnit.SECONDS) |
| 151 | + .pollInterval(100, TimeUnit.MILLISECONDS) |
| 152 | + .until(() -> streamReaders.getActiveStreamReaderCount() == 2); |
| 153 | + |
| 154 | + // StreamReaders should still be there (not expired yet) |
| 155 | + assertEquals(2, streamReaders.getActiveStreamReaderCount()); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testExpiredStreamReaderCleanupExecution() throws Exception { |
| 160 | + TraceContext context = TraceContext.DEFAULT; |
| 161 | + |
| 162 | + // Create a StreamReader |
| 163 | + CompletableFuture<ReadDataBlock> readFuture = streamReaders.read(context, STREAM_ID_1, 0, 1, Integer.MAX_VALUE); |
| 164 | + ReadDataBlock result = readFuture.get(5, TimeUnit.SECONDS); |
| 165 | + result.getRecords().forEach(StreamRecordBatch::release); |
| 166 | + |
| 167 | + assertEquals(1, streamReaders.getActiveStreamReaderCount()); |
| 168 | + |
| 169 | + // Advance mock time to simulate expiration (advance by 2 minutes, expiration is 1 minute) |
| 170 | + mockTime.sleep(TimeUnit.MINUTES.toMillis(2)); |
| 171 | + |
| 172 | + // Trigger cleanup - should now clean up expired StreamReaders |
| 173 | + streamReaders.triggerExpiredStreamReaderCleanup(); |
| 174 | + |
| 175 | + // Wait for async cleanup to complete |
| 176 | + await().atMost(5, TimeUnit.SECONDS) |
| 177 | + .pollInterval(100, TimeUnit.MILLISECONDS) |
| 178 | + .until(() -> streamReaders.getActiveStreamReaderCount() == 0); |
| 179 | + |
| 180 | + // Verify system still works after cleanup |
| 181 | + CompletableFuture<ReadDataBlock> readFuture2 = streamReaders.read(context, STREAM_ID_2, 0, 1, Integer.MAX_VALUE); |
| 182 | + ReadDataBlock result2 = readFuture2.get(5, TimeUnit.SECONDS); |
| 183 | + result2.getRecords().forEach(StreamRecordBatch::release); |
| 184 | + |
| 185 | + assertEquals(1, streamReaders.getActiveStreamReaderCount()); |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | +} |
0 commit comments