|
9 | 9 | package org.opensearch.plugin.ingestion.fs; |
10 | 10 |
|
11 | 11 | import org.opensearch.action.admin.indices.delete.DeleteIndexRequest; |
| 12 | +import org.opensearch.action.admin.indices.stats.IndexStats; |
| 13 | +import org.opensearch.action.admin.indices.stats.ShardStats; |
12 | 14 | import org.opensearch.action.admin.indices.streamingingestion.pause.PauseIngestionResponse; |
13 | 15 | import org.opensearch.action.admin.indices.streamingingestion.resume.ResumeIngestionRequest; |
14 | 16 | import org.opensearch.action.admin.indices.streamingingestion.resume.ResumeIngestionResponse; |
|
17 | 19 | import org.opensearch.cluster.metadata.IndexMetadata; |
18 | 20 | import org.opensearch.common.settings.Settings; |
19 | 21 | import org.opensearch.index.query.RangeQueryBuilder; |
| 22 | +import org.opensearch.indices.pollingingest.PollingIngestStats; |
20 | 23 | import org.opensearch.plugins.Plugin; |
21 | 24 | import org.opensearch.test.OpenSearchSingleNodeTestCase; |
22 | 25 | import org.opensearch.transport.client.Requests; |
|
31 | 34 | import java.util.Arrays; |
32 | 35 | import java.util.Collection; |
33 | 36 | import java.util.Collections; |
| 37 | +import java.util.concurrent.Callable; |
| 38 | +import java.util.concurrent.TimeUnit; |
34 | 39 |
|
35 | 40 | public class FileBasedIngestionSingleNodeTests extends OpenSearchSingleNodeTestCase { |
36 | 41 | private Path ingestionDir; |
@@ -237,4 +242,167 @@ public void testFileIngestionFromProvidedPointer() throws Exception { |
237 | 242 | // cleanup the test index |
238 | 243 | client().admin().indices().delete(new DeleteIndexRequest(index)).actionGet(); |
239 | 244 | } |
| 245 | + |
| 246 | + public void testPointerBasedLag() throws Exception { |
| 247 | + String mappings = """ |
| 248 | + { |
| 249 | + "properties": { |
| 250 | + "name": { "type": "text" }, |
| 251 | + "age": { "type": "integer" } |
| 252 | + } |
| 253 | + } |
| 254 | + """; |
| 255 | + |
| 256 | + // Create index with empty file (no messages) |
| 257 | + Path streamDir = ingestionDir.resolve(stream); |
| 258 | + Path shardFile = streamDir.resolve("0.ndjson"); |
| 259 | + Files.write(shardFile, new byte[0]); // Empty file |
| 260 | + |
| 261 | + createIndexWithMappingSource( |
| 262 | + index, |
| 263 | + Settings.builder() |
| 264 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 265 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 266 | + .put("ingestion_source.type", "FILE") |
| 267 | + .put("ingestion_source.pointer.init.reset", "earliest") |
| 268 | + .put("ingestion_source.pointer_based_lag_update_interval", "3s") |
| 269 | + .put("ingestion_source.param.stream", stream) |
| 270 | + .put("ingestion_source.param.base_directory", ingestionDir.toString()) |
| 271 | + .put("index.replication.type", "SEGMENT") |
| 272 | + .build(), |
| 273 | + mappings |
| 274 | + ); |
| 275 | + ensureGreen(index); |
| 276 | + |
| 277 | + // Lag should be 0 since there are no messages |
| 278 | + waitForState(() -> { |
| 279 | + PollingIngestStats stats = getPollingIngestStats(index); |
| 280 | + return stats != null && stats.getConsumerStats().pointerBasedLag() == 0L; |
| 281 | + }); |
| 282 | + |
| 283 | + // Add messages to the file |
| 284 | + try ( |
| 285 | + BufferedWriter writer = Files.newBufferedWriter( |
| 286 | + shardFile, |
| 287 | + StandardCharsets.UTF_8, |
| 288 | + StandardOpenOption.WRITE, |
| 289 | + StandardOpenOption.TRUNCATE_EXISTING |
| 290 | + ) |
| 291 | + ) { |
| 292 | + writer.write("{\"_id\":\"1\",\"_version\":\"1\",\"_op_type\":\"index\",\"_source\":{\"name\":\"alice\", \"age\": 30}}\n"); |
| 293 | + writer.write("{\"_id\":\"2\",\"_version\":\"1\",\"_op_type\":\"index\",\"_source\":{\"name\":\"bob\", \"age\": 35}}\n"); |
| 294 | + writer.flush(); |
| 295 | + } |
| 296 | + |
| 297 | + try (FileChannel channel = FileChannel.open(shardFile, StandardOpenOption.READ)) { |
| 298 | + channel.force(true); |
| 299 | + } |
| 300 | + |
| 301 | + // Wait for messages to be processed |
| 302 | + waitForState(() -> { |
| 303 | + SearchResponse response = client().prepareSearch(index).setQuery(new RangeQueryBuilder("age").gte(0)).get(); |
| 304 | + return response.getHits().getTotalHits().value() == 2; |
| 305 | + }); |
| 306 | + |
| 307 | + // Lag should be 0 after all messages are consumed |
| 308 | + waitForState(() -> { |
| 309 | + PollingIngestStats stats = getPollingIngestStats(index); |
| 310 | + return stats != null && stats.getConsumerStats().pointerBasedLag() == 0L; |
| 311 | + }); |
| 312 | + |
| 313 | + // cleanup |
| 314 | + client().admin().indices().delete(new DeleteIndexRequest(index)).actionGet(); |
| 315 | + } |
| 316 | + |
| 317 | + public void testPointerBasedLagAfterPause() throws Exception { |
| 318 | + String mappings = """ |
| 319 | + { |
| 320 | + "properties": { |
| 321 | + "name": { "type": "text" }, |
| 322 | + "age": { "type": "integer" } |
| 323 | + } |
| 324 | + } |
| 325 | + """; |
| 326 | + |
| 327 | + createIndexWithMappingSource( |
| 328 | + index, |
| 329 | + Settings.builder() |
| 330 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 331 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 332 | + .put("ingestion_source.type", "FILE") |
| 333 | + .put("ingestion_source.pointer.init.reset", "earliest") |
| 334 | + .put("ingestion_source.pointer_based_lag_update_interval", "3s") |
| 335 | + .put("ingestion_source.param.stream", stream) |
| 336 | + .put("ingestion_source.param.base_directory", ingestionDir.toString()) |
| 337 | + .put("index.replication.type", "SEGMENT") |
| 338 | + .build(), |
| 339 | + mappings |
| 340 | + ); |
| 341 | + ensureGreen(index); |
| 342 | + |
| 343 | + // Wait for initial messages to be processed |
| 344 | + waitForState(() -> { |
| 345 | + SearchResponse response = client().prepareSearch(index).setQuery(new RangeQueryBuilder("age").gte(0)).get(); |
| 346 | + return response.getHits().getTotalHits().value() == 2; |
| 347 | + }); |
| 348 | + |
| 349 | + // Pause ingestion |
| 350 | + PauseIngestionResponse pauseResponse = client().admin().indices().pauseIngestion(Requests.pauseIngestionRequest(index)).get(); |
| 351 | + assertTrue(pauseResponse.isAcknowledged()); |
| 352 | + assertTrue(pauseResponse.isShardsAcknowledged()); |
| 353 | + |
| 354 | + // Wait for pause to take effect |
| 355 | + waitForState(() -> { |
| 356 | + GetIngestionStateResponse ingestionState = client().admin() |
| 357 | + .indices() |
| 358 | + .getIngestionState(Requests.getIngestionStateRequest(index)) |
| 359 | + .get(); |
| 360 | + return ingestionState.getFailedShards() == 0 |
| 361 | + && Arrays.stream(ingestionState.getShardStates()) |
| 362 | + .allMatch(state -> state.isPollerPaused() && state.getPollerState().equalsIgnoreCase("paused")); |
| 363 | + }); |
| 364 | + |
| 365 | + // Add more messages to the file while paused |
| 366 | + Path streamDir = ingestionDir.resolve(stream); |
| 367 | + Path shardFile = streamDir.resolve("0.ndjson"); |
| 368 | + try (BufferedWriter writer = Files.newBufferedWriter(shardFile, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) { |
| 369 | + writer.write("{\"_id\":\"3\",\"_version\":\"1\",\"_op_type\":\"index\",\"_source\":{\"name\":\"charlie\", \"age\": 40}}\n"); |
| 370 | + writer.write("{\"_id\":\"4\",\"_version\":\"1\",\"_op_type\":\"index\",\"_source\":{\"name\":\"diana\", \"age\": 45}}\n"); |
| 371 | + writer.write("{\"_id\":\"5\",\"_version\":\"1\",\"_op_type\":\"index\",\"_source\":{\"name\":\"eve\", \"age\": 50}}\n"); |
| 372 | + writer.flush(); |
| 373 | + } |
| 374 | + |
| 375 | + try (FileChannel channel = FileChannel.open(shardFile, StandardOpenOption.READ)) { |
| 376 | + channel.force(true); |
| 377 | + } |
| 378 | + |
| 379 | + // Wait for lag to be calculated (lag is updated every 3 seconds in this test) |
| 380 | + waitForState(() -> { |
| 381 | + PollingIngestStats stats = getPollingIngestStats(index); |
| 382 | + return stats != null && stats.getConsumerStats().pointerBasedLag() == 3L; |
| 383 | + }); |
| 384 | + |
| 385 | + // cleanup |
| 386 | + client().admin().indices().delete(new DeleteIndexRequest(index)).actionGet(); |
| 387 | + } |
| 388 | + |
| 389 | + /** |
| 390 | + * Helper method to get polling ingest stats for the index |
| 391 | + */ |
| 392 | + private PollingIngestStats getPollingIngestStats(String indexName) { |
| 393 | + IndexStats indexStats = client().admin().indices().prepareStats(indexName).get().getIndex(indexName); |
| 394 | + ShardStats[] shards = indexStats.getShards(); |
| 395 | + if (shards.length > 0) { |
| 396 | + return shards[0].getPollingIngestStats(); |
| 397 | + } |
| 398 | + return null; |
| 399 | + } |
| 400 | + |
| 401 | + private void waitForState(Callable<Boolean> checkState) throws Exception { |
| 402 | + assertBusy(() -> { |
| 403 | + if (checkState.call() == false) { |
| 404 | + fail("Provided state requirements not met"); |
| 405 | + } |
| 406 | + }, 1, TimeUnit.MINUTES); |
| 407 | + } |
240 | 408 | } |
0 commit comments