Skip to content

Commit f19ae5a

Browse files
committed
[POC] primary term based directory structure
Signed-off-by: Varun Bansal <bansvaru@amazon.com>
1 parent 79c1656 commit f19ae5a

22 files changed

Lines changed: 2820 additions & 2059 deletions

server/src/main/java/org/opensearch/index/shard/IndexShard.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
import org.opensearch.index.translog.RemoteFsTranslog;
185185
import org.opensearch.index.translog.RemoteTranslogStats;
186186
import org.opensearch.index.translog.Translog;
187+
import org.opensearch.index.translog.Translog.Durability;
187188
import org.opensearch.index.translog.TranslogConfig;
188189
import org.opensearch.index.translog.TranslogFactory;
189190
import org.opensearch.index.translog.TranslogRecoveryRunner;
@@ -569,6 +570,32 @@ private long getInitialGlobalCheckpointForShard(IndexSettings indexSettings) {
569570
return UNASSIGNED_SEQ_NO;
570571
}
571572

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+
572599
public ThreadPool getThreadPool() {
573600
return this.threadPool;
574601
}
@@ -2444,6 +2471,9 @@ public void postRecovery(String reason) throws IndexShardStartedException, Index
24442471
}
24452472
recoveryState.setStage(RecoveryState.Stage.DONE);
24462473
changeState(IndexShardState.POST_RECOVERY, reason);
2474+
2475+
// Initialize primary term routing after recovery is complete
2476+
initializePrimaryTermRouting();
24472477
}
24482478
}
24492479
}

server/src/main/java/org/opensearch/index/store/FsDirectoryFactory.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.opensearch.index.IndexSettings;
5151
import org.opensearch.index.shard.ShardPath;
5252
import org.opensearch.index.store.distributed.DistributedSegmentDirectory;
53+
import org.opensearch.index.store.distributed.PrimaryTermAwareDirectoryWrapper;
5354
import org.opensearch.plugins.IndexStorePlugin;
5455

5556
import java.io.IOException;
@@ -97,10 +98,21 @@ protected Directory newFSDirectory(Path location, LockFactory lockFactory, Index
9798
Set<String> preLoadExtensions = new HashSet<>(indexSettings.getValue(IndexModule.INDEX_STORE_PRE_LOAD_SETTING));
9899
switch (type) {
99100
case HYBRIDFS:
100-
// return new DistributedSegmentDirectory(null, location)
101-
// Use Lucene defaults
101+
// Create primary directory
102102
final FSDirectory primaryDirectory = new NIOFSDirectory(location, lockFactory);
103-
return new DistributedSegmentDirectory(primaryDirectory, location);
103+
104+
return new PrimaryTermAwareDirectoryWrapper(primaryDirectory, location);
105+
// // Check if primary term routing should be enabled
106+
// boolean enablePrimaryTermRouting = indexSettings.getSettings()
107+
// .getAsBoolean("index.store.distributed_segment.enable_primary_term_routing", true);
108+
109+
// if (enablePrimaryTermRouting) {
110+
// // Use wrapper that can be configured for primary term routing later
111+
// return new PrimaryTermAwareDirectoryWrapper(primaryDirectory, location);
112+
// } else {
113+
// // Use legacy hash-based routing
114+
// return new DistributedSegmentDirectory(primaryDirectory, location);
115+
// }
104116

105117
// final Set<String> nioExtensions = new HashSet<>(indexSettings.getValue(IndexModule.INDEX_STORE_HYBRID_NIO_EXTENSIONS));
106118
// if (primaryDirectory instanceof MMapDirectory) {

0 commit comments

Comments
 (0)