74
74
import org .apache .lucene .index .IndexWriterConfig ;
75
75
import org .apache .lucene .index .IndexableField ;
76
76
import org .apache .lucene .index .KeepOnlyLastCommitDeletionPolicy ;
77
+ import org .apache .lucene .index .MergePolicy ;
77
78
import org .apache .lucene .index .PersistentSnapshotDeletionPolicy ;
78
79
import org .apache .lucene .index .SegmentInfos ;
79
80
import org .apache .lucene .index .SimpleMergedSegmentWarmer ;
@@ -183,7 +184,9 @@ public class IndexState implements Closeable {
183
184
/** {@link Bindings} to pass when evaluating expressions. */
184
185
public final Bindings exprBindings = new FieldDefBindings (fields );
185
186
186
- private final Map <Integer ,ShardState > shards = new ConcurrentHashMap <>();
187
+ public final Map <Integer ,ShardState > shards = new ConcurrentHashMap <>();
188
+
189
+ private ShardState writableShard ;
187
190
188
191
/** Built suggest implementations */
189
192
public final Map <String ,Lookup > suggesters = new ConcurrentHashMap <String ,Lookup >();
@@ -362,6 +365,7 @@ public IndexState(GlobalState globalState, String name, Path rootDir, boolean do
362
365
this .globalState = globalState ;
363
366
this .name = name ;
364
367
this .rootDir = rootDir ;
368
+ // nocommit require rootDir != null! no RAMDirectory!
365
369
if (rootDir != null ) {
366
370
if (Files .exists (rootDir ) == false ) {
367
371
Files .createDirectories (rootDir );
@@ -435,7 +439,7 @@ public boolean hasCommit() throws IOException {
435
439
return saveLoadState .getNextWriteGen () != 0 ;
436
440
}
437
441
438
- IndexWriterConfig getIndexWriterConfig (OpenMode openMode , Directory origIndexDir ) throws IOException {
442
+ IndexWriterConfig getIndexWriterConfig (OpenMode openMode , Directory origIndexDir , int shardOrd ) throws IOException {
439
443
IndexWriterConfig iwc = new IndexWriterConfig (indexAnalyzer );
440
444
iwc .setOpenMode (openMode );
441
445
if (getBooleanSetting ("index.verbose" )) {
@@ -452,7 +456,15 @@ IndexWriterConfig getIndexWriterConfig(OpenMode openMode, Directory origIndexDir
452
456
// nocommit in primary case we can't do this?
453
457
iwc .setMergedSegmentWarmer (new SimpleMergedSegmentWarmer (iwc .getInfoStream ()));
454
458
455
- ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler ) iwc .getMergeScheduler ();
459
+ ConcurrentMergeScheduler cms = new ConcurrentMergeScheduler () {
460
+ @ Override
461
+ public synchronized MergeThread getMergeThread (IndexWriter writer , MergePolicy .OneMerge merge ) throws IOException {
462
+ MergeThread thread = super .getMergeThread (writer , merge );
463
+ thread .setName (thread .getName () + " [" + name + ":" + shardOrd + "]" );
464
+ return thread ;
465
+ }
466
+ };
467
+ iwc .setMergeScheduler (cms );
456
468
457
469
if (getBooleanSetting ("index.merge.scheduler.auto_throttle" )) {
458
470
cms .enableAutoIOThrottle ();
@@ -710,8 +722,12 @@ public void setIndexSort(Sort sort, Object saveState) {
710
722
711
723
public void start () throws Exception {
712
724
// start all local shards
725
+
713
726
for (ShardState shard : shards .values ()) {
714
727
shard .start ();
728
+ if (writableShard == null || shard .shardOrd > writableShard .shardOrd ) {
729
+ writableShard = shard ;
730
+ }
715
731
}
716
732
717
733
if (suggesterSettings != null ) {
@@ -1009,10 +1025,36 @@ public ShardState addShard(int shardOrd, boolean doCreate) {
1009
1025
throw new IllegalArgumentException ("shardOrd=" + shardOrd + " already exists in index + \" " + name + "\" " );
1010
1026
}
1011
1027
ShardState shard = new ShardState (this , shardOrd , doCreate );
1028
+ // nocommit fail if there is already a shard here?
1012
1029
shards .put (shardOrd , shard );
1013
1030
return shard ;
1014
1031
}
1015
1032
1033
+ /** Returns the shard that should be used for writing new documents, possibly creating and starting a new shard if the current one is full. */
1034
+ public ShardState getWritableShard () throws Exception {
1035
+ verifyStarted (null );
1036
+
1037
+ synchronized (this ) {
1038
+
1039
+ // nocommit make this tunable:
1040
+ if (writableShard .writer .maxDoc () > 50000000 ) {
1041
+ System .out .println ("NEW SHARD ORD " + (writableShard .shardOrd +1 ));
1042
+ ShardState nextShard = addShard (writableShard .shardOrd +1 , true );
1043
+
1044
+ // nocommit hmm we can't do this now ... we need to get all in-flight ops to finish first
1045
+ writableShard .finishWriting ();
1046
+ // nocommit we should close IW after forceMerge finishes
1047
+
1048
+ nextShard .start ();
1049
+ writableShard = nextShard ;
1050
+ }
1051
+
1052
+ writableShard .startIndexingChunk ();
1053
+ }
1054
+
1055
+ return writableShard ;
1056
+ }
1057
+
1016
1058
public ShardState getShard (int shardOrd ) {
1017
1059
ShardState shardState = shards .get (shardOrd );
1018
1060
if (shardState == null ) {
0 commit comments