|
184 | 184 | import org.opensearch.index.translog.RemoteFsTranslog; |
185 | 185 | import org.opensearch.index.translog.RemoteTranslogStats; |
186 | 186 | import org.opensearch.index.translog.Translog; |
| 187 | +import org.opensearch.index.translog.Translog.Durability; |
187 | 188 | import org.opensearch.index.translog.TranslogConfig; |
188 | 189 | import org.opensearch.index.translog.TranslogFactory; |
189 | 190 | import org.opensearch.index.translog.TranslogRecoveryRunner; |
@@ -569,6 +570,32 @@ private long getInitialGlobalCheckpointForShard(IndexSettings indexSettings) { |
569 | 570 | return UNASSIGNED_SEQ_NO; |
570 | 571 | } |
571 | 572 |
|
| 573 | + /** |
| 574 | + * Initializes primary term routing for the store directory if supported. |
| 575 | + * This method should be called after the IndexShard is fully constructed |
| 576 | + * to enable primary term-based segment file routing. |
| 577 | + */ |
| 578 | + private void initializePrimaryTermRouting() { |
| 579 | + logger.info("intializing primary term based routing"); |
| 580 | + try { |
| 581 | + Directory storeDirectory = store.directory(); |
| 582 | + logger.info("intializing primary term based routing {}", storeDirectory); |
| 583 | + // if (storeDirectory instanceof org.opensearch.index.store.distributed.PrimaryTermAwareDirectoryWrapper) { |
| 584 | + org.opensearch.index.store.distributed.PrimaryTermAwareDirectoryWrapper wrapper = |
| 585 | + (org.opensearch.index.store.distributed.PrimaryTermAwareDirectoryWrapper) ((FilterDirectory) ((FilterDirectory)storeDirectory).getDelegate()).getDelegate(); |
| 586 | + logger.info("intializing primary term based routing"); |
| 587 | + |
| 588 | + if (!wrapper.isPrimaryTermRoutingEnabled()) { |
| 589 | + wrapper.enablePrimaryTermRouting(this); |
| 590 | + logger.info("Enabled primary term routing for shard {}", shardId); |
| 591 | + } |
| 592 | + // } |
| 593 | + } catch (Exception e) { |
| 594 | + logger.warn("Failed to initialize primary term routing for shard {}", shardId, e); |
| 595 | + // Don't fail shard creation if primary term routing setup fails |
| 596 | + } |
| 597 | + } |
| 598 | + |
572 | 599 | public ThreadPool getThreadPool() { |
573 | 600 | return this.threadPool; |
574 | 601 | } |
@@ -2444,6 +2471,9 @@ public void postRecovery(String reason) throws IndexShardStartedException, Index |
2444 | 2471 | } |
2445 | 2472 | recoveryState.setStage(RecoveryState.Stage.DONE); |
2446 | 2473 | changeState(IndexShardState.POST_RECOVERY, reason); |
| 2474 | + |
| 2475 | + // Initialize primary term routing after recovery is complete |
| 2476 | + initializePrimaryTermRouting(); |
2447 | 2477 | } |
2448 | 2478 | } |
2449 | 2479 | } |
|
0 commit comments