|
| 1 | +package io.github.datacatering.datacaterer.core.sink.memory |
| 2 | + |
| 3 | +import io.github.datacatering.datacaterer.core.util.SparkSuite |
| 4 | +import org.apache.log4j.Logger |
| 5 | +import org.scalatest.matchers.should.Matchers |
| 6 | + |
| 7 | +import java.time.LocalDateTime |
| 8 | +import java.util.concurrent.{CountDownLatch, Executors} |
| 9 | +import java.util.concurrent.atomic.AtomicInteger |
| 10 | +import scala.concurrent.{ExecutionContext, Future, Await} |
| 11 | +import scala.concurrent.duration.DurationInt |
| 12 | + |
| 13 | +/** |
| 14 | + * Unit tests for BatchTimestampTracker thread safety and functionality. |
| 15 | + * |
| 16 | + * Tests verify: |
| 17 | + * - Thread-safe concurrent access to recordTimestamp() |
| 18 | + * - Correct batch window flushing |
| 19 | + * - No record count loss during concurrent window transitions |
| 20 | + * - Proper batch aggregation and metrics generation |
| 21 | + * |
| 22 | + * Critical: Tests for issue #1 - thread safety race condition where |
| 23 | + * concurrent threads entering flush path could lose record counts. |
| 24 | + */ |
| 25 | +class BatchTimestampTrackerTest extends SparkSuite with Matchers { |
| 26 | + |
| 27 | + private val LOGGER = Logger.getLogger(getClass.getName) |
| 28 | + |
| 29 | + test("Single-threaded tracking - verify basic functionality") { |
| 30 | + val tracker = new BatchTimestampTracker(windowMs = 100) |
| 31 | + |
| 32 | + // Record 10 timestamps |
| 33 | + (1 to 10).foreach { _ => |
| 34 | + tracker.recordTimestamp() |
| 35 | + Thread.sleep(5) // Small delay within window |
| 36 | + } |
| 37 | + |
| 38 | + // Should still be in first window (< 100ms total) |
| 39 | + tracker.getBatchCount shouldBe 0 |
| 40 | + tracker.getTotalRecords shouldBe 10 |
| 41 | + |
| 42 | + // Wait for window to expire |
| 43 | + Thread.sleep(150) |
| 44 | + tracker.recordTimestamp() // Trigger flush |
| 45 | + |
| 46 | + // Should have flushed first window and started second |
| 47 | + tracker.getBatchCount shouldBe 1 |
| 48 | + tracker.getTotalRecords shouldBe 11 |
| 49 | + } |
| 50 | + |
| 51 | + test("Multi-threaded concurrent recording - verify no record loss") { |
| 52 | + val tracker = new BatchTimestampTracker(windowMs = 1000) |
| 53 | + val numThreads = 10 |
| 54 | + val recordsPerThread = 100 |
| 55 | + val totalExpectedRecords = numThreads * recordsPerThread |
| 56 | + |
| 57 | + val executor = Executors.newFixedThreadPool(numThreads) |
| 58 | + implicit val ec: ExecutionContext = ExecutionContext.fromExecutor(executor) |
| 59 | + |
| 60 | + try { |
| 61 | + val latch = new CountDownLatch(numThreads) |
| 62 | + |
| 63 | + // Launch multiple threads simultaneously |
| 64 | + val futures = (1 to numThreads).map { threadId => |
| 65 | + Future { |
| 66 | + latch.countDown() |
| 67 | + latch.await() // Wait for all threads to be ready |
| 68 | + |
| 69 | + // Each thread records timestamps |
| 70 | + (1 to recordsPerThread).foreach { _ => |
| 71 | + tracker.recordTimestamp() |
| 72 | + Thread.sleep(1) // Small delay to spread across time |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Wait for all threads to complete |
| 78 | + Await.result(Future.sequence(futures), 30.seconds) |
| 79 | + |
| 80 | + // Verify no records were lost |
| 81 | + val finalRecordCount = tracker.getTotalRecords |
| 82 | + LOGGER.info(s"Multi-threaded test: expected=$totalExpectedRecords, actual=$finalRecordCount") |
| 83 | + |
| 84 | + finalRecordCount shouldBe totalExpectedRecords |
| 85 | + |
| 86 | + } finally { |
| 87 | + executor.shutdown() |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + test("Concurrent window transitions - verify no count loss during flush") { |
| 92 | + // CRITICAL TEST: This tests the fix for issue #1 |
| 93 | + // Without the fix (increment AFTER flush check), concurrent threads |
| 94 | + // entering the flush path would both increment in the NEW window, |
| 95 | + // losing counts from the old window. |
| 96 | + |
| 97 | + val tracker = new BatchTimestampTracker(windowMs = 50) |
| 98 | + val numThreads = 20 |
| 99 | + val recordsPerThread = 50 |
| 100 | + val totalExpectedRecords = numThreads * recordsPerThread |
| 101 | + |
| 102 | + val executor = Executors.newFixedThreadPool(numThreads) |
| 103 | + implicit val ec: ExecutionContext = ExecutionContext.fromExecutor(executor) |
| 104 | + |
| 105 | + try { |
| 106 | + val latch = new CountDownLatch(numThreads) |
| 107 | + |
| 108 | + // Launch threads that will trigger multiple window transitions |
| 109 | + val futures = (1 to numThreads).map { threadId => |
| 110 | + Future { |
| 111 | + latch.countDown() |
| 112 | + latch.await() |
| 113 | + |
| 114 | + (1 to recordsPerThread).foreach { i => |
| 115 | + tracker.recordTimestamp() |
| 116 | + |
| 117 | + // Introduce occasional small delays to trigger window transitions |
| 118 | + if (i % 10 == 0) { |
| 119 | + Thread.sleep(10) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + Await.result(Future.sequence(futures), 30.seconds) |
| 126 | + |
| 127 | + // Force final flush |
| 128 | + Thread.sleep(100) |
| 129 | + val metrics = tracker.finalizeAndGetMetrics( |
| 130 | + LocalDateTime.now(), |
| 131 | + LocalDateTime.now(), |
| 132 | + "test" |
| 133 | + ) |
| 134 | + |
| 135 | + val finalRecordCount = metrics.totalRecords |
| 136 | + LOGGER.info(s"Concurrent window transitions: expected=$totalExpectedRecords, actual=$finalRecordCount, batches=${tracker.getBatchCount}") |
| 137 | + |
| 138 | + // CRITICAL: This should be exact equality |
| 139 | + // Any loss indicates a thread safety bug |
| 140 | + finalRecordCount shouldBe totalExpectedRecords |
| 141 | + |
| 142 | + // Should have created multiple batches due to window transitions |
| 143 | + tracker.getBatchCount should be > 1 |
| 144 | + |
| 145 | + } finally { |
| 146 | + executor.shutdown() |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + test("High-frequency concurrent access - stress test") { |
| 151 | + val tracker = new BatchTimestampTracker(windowMs = 100) |
| 152 | + val numThreads = 50 |
| 153 | + val recordsPerThread = 200 |
| 154 | + val totalExpectedRecords = numThreads * recordsPerThread |
| 155 | + |
| 156 | + val executor = Executors.newFixedThreadPool(numThreads) |
| 157 | + implicit val ec: ExecutionContext = ExecutionContext.fromExecutor(executor) |
| 158 | + |
| 159 | + try { |
| 160 | + val startTime = System.currentTimeMillis() |
| 161 | + |
| 162 | + val futures = (1 to numThreads).map { _ => |
| 163 | + Future { |
| 164 | + (1 to recordsPerThread).foreach { _ => |
| 165 | + tracker.recordTimestamp() |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + Await.result(Future.sequence(futures), 60.seconds) |
| 171 | + |
| 172 | + val duration = System.currentTimeMillis() - startTime |
| 173 | + val finalRecordCount = tracker.getTotalRecords |
| 174 | + |
| 175 | + LOGGER.info(s"Stress test: $totalExpectedRecords records from $numThreads threads in ${duration}ms") |
| 176 | + LOGGER.info(s"Expected=$totalExpectedRecords, actual=$finalRecordCount, batches=${tracker.getBatchCount}") |
| 177 | + |
| 178 | + finalRecordCount shouldBe totalExpectedRecords |
| 179 | + |
| 180 | + } finally { |
| 181 | + executor.shutdown() |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + test("Metrics generation - verify correct batch aggregation") { |
| 186 | + val tracker = new BatchTimestampTracker(windowMs = 100) |
| 187 | + val startTime = LocalDateTime.now() |
| 188 | + |
| 189 | + // Record some timestamps with delays to create multiple windows |
| 190 | + (1 to 10).foreach { _ => |
| 191 | + tracker.recordTimestamp() |
| 192 | + } |
| 193 | + |
| 194 | + Thread.sleep(150) |
| 195 | + |
| 196 | + (1 to 15).foreach { _ => |
| 197 | + tracker.recordTimestamp() |
| 198 | + } |
| 199 | + |
| 200 | + Thread.sleep(150) |
| 201 | + |
| 202 | + (1 to 20).foreach { _ => |
| 203 | + tracker.recordTimestamp() |
| 204 | + } |
| 205 | + |
| 206 | + // Verify tracker has all records before finalization |
| 207 | + val totalBeforeFinalize = tracker.getTotalRecords |
| 208 | + val batchCountBefore = tracker.getBatchCount |
| 209 | + LOGGER.info(s"Before finalization: totalRecords=$totalBeforeFinalize, batches=$batchCountBefore") |
| 210 | + totalBeforeFinalize shouldBe 45 |
| 211 | + |
| 212 | + val endTime = LocalDateTime.now() |
| 213 | + val metrics = tracker.finalizeAndGetMetrics(startTime, endTime, "test") |
| 214 | + |
| 215 | + val batchCountAfter = tracker.getBatchCount |
| 216 | + LOGGER.info(s"After finalization: metrics.totalRecords=${metrics.totalRecords}, batches=$batchCountAfter") |
| 217 | + LOGGER.info(s"recordTimestamps.size=${metrics.recordTimestamps.size}") |
| 218 | + |
| 219 | + metrics.totalRecords shouldBe 45 |
| 220 | + metrics.executionType shouldBe "test" |
| 221 | + } |
| 222 | + |
| 223 | + test("Empty tracker - verify graceful handling") { |
| 224 | + val tracker = new BatchTimestampTracker(windowMs = 100) |
| 225 | + |
| 226 | + tracker.getBatchCount shouldBe 0 |
| 227 | + tracker.getTotalRecords shouldBe 0 |
| 228 | + |
| 229 | + val metrics = tracker.finalizeAndGetMetrics( |
| 230 | + LocalDateTime.now(), |
| 231 | + LocalDateTime.now(), |
| 232 | + "test" |
| 233 | + ) |
| 234 | + |
| 235 | + metrics.totalRecords shouldBe 0 |
| 236 | + } |
| 237 | + |
| 238 | + test("Window size variations - verify different window sizes work") { |
| 239 | + // Test with very small window |
| 240 | + val smallWindow = new BatchTimestampTracker(windowMs = 10) |
| 241 | + (1 to 100).foreach { _ => |
| 242 | + smallWindow.recordTimestamp() |
| 243 | + Thread.sleep(1) |
| 244 | + } |
| 245 | + |
| 246 | + Thread.sleep(20) |
| 247 | + smallWindow.recordTimestamp() |
| 248 | + |
| 249 | + smallWindow.getBatchCount should be > 5 |
| 250 | + smallWindow.getTotalRecords shouldBe 101 |
| 251 | + |
| 252 | + // Test with larger window |
| 253 | + val largeWindow = new BatchTimestampTracker(windowMs = 1000) |
| 254 | + (1 to 100).foreach { _ => |
| 255 | + largeWindow.recordTimestamp() |
| 256 | + Thread.sleep(1) |
| 257 | + } |
| 258 | + |
| 259 | + // Should still be in first window |
| 260 | + largeWindow.getBatchCount shouldBe 0 |
| 261 | + largeWindow.getTotalRecords shouldBe 100 |
| 262 | + } |
| 263 | +} |
0 commit comments