@@ -684,16 +684,16 @@ enum OpVsLuceneDocStatus {
684
684
/** the op is more recent than the one that last modified the doc found in lucene*/
685
685
OP_NEWER ,
686
686
/** the op is older or the same as the one that last modified the doc found in lucene*/
687
- OP_STALE_OR_EQUAL ,
687
+ OP_STALE_OR_EQUAL ,// 一样,或者更旧的
688
688
/** no doc was found in lucene */
689
689
LUCENE_DOC_NOT_FOUND
690
690
}
691
691
692
692
private static OpVsLuceneDocStatus compareOpToVersionMapOnSeqNo (String id , long seqNo , long primaryTerm , VersionValue versionValue ) {
693
693
Objects .requireNonNull (versionValue );
694
694
if (seqNo > versionValue .seqNo ) {
695
- return OpVsLuceneDocStatus .OP_NEWER ;
696
- } else if (seqNo == versionValue .seqNo ) {
695
+ return OpVsLuceneDocStatus .OP_NEWER ;// 新的
696
+ } else if (seqNo == versionValue .seqNo ) {// 一样
697
697
assert versionValue .term == primaryTerm : "primary term not matched; id="
698
698
+ id
699
699
+ " seq_no="
@@ -711,12 +711,13 @@ private static OpVsLuceneDocStatus compareOpToVersionMapOnSeqNo(String id, long
711
711
private OpVsLuceneDocStatus compareOpToLuceneDocBasedOnSeqNo (final Operation op ) throws IOException {
712
712
assert op .seqNo () != SequenceNumbers .UNASSIGNED_SEQ_NO : "resolving ops based on seq# but no seqNo is found" ;
713
713
final OpVsLuceneDocStatus status ;
714
- VersionValue versionValue = getVersionFromMap (op .uid ().bytes ());
714
+
715
+ VersionValue versionValue = getVersionFromMap (op .uid ().bytes ());// 从maps中找下,看可以找到吗
715
716
assert incrementVersionLookup ();
716
717
boolean segRepEnabled = engineConfig .getIndexSettings ().isSegRepEnabledOrRemoteNode ();
717
- if (versionValue != null ) {
718
+ if (versionValue != null ) {// maps中找到了
718
719
status = compareOpToVersionMapOnSeqNo (op .id (), op .seqNo (), op .primaryTerm (), versionValue );
719
- } else {
720
+ } else {// 没找到
720
721
// load from index
721
722
assert incrementIndexVersionLookup ();
722
723
try (Searcher searcher = acquireSearcher ("load_seq_no" , SearcherScope .INTERNAL )) {
@@ -906,10 +907,11 @@ public IndexResult index(Index index) throws IOException {
906
907
index .getAutoGeneratedIdTimestamp (),
907
908
index .isRetry (),
908
909
index .getIfSeqNo (),
909
- index .getIfPrimaryTerm ()
910
+ index .getIfPrimaryTerm (),
911
+ plan
910
912
);
911
913
912
- final boolean toAppend = plan .indexIntoLucene && plan .useLuceneUpdateDocument == false ;
914
+ final boolean toAppend = plan .indexIntoLucene && plan .useLuceneUpdateDocument == false ;// 不带主键的写入
913
915
if (toAppend == false ) {
914
916
advanceMaxSeqNoOfUpdatesOrDeletesOnPrimary (index .seqNo ());
915
917
}
@@ -922,6 +924,9 @@ public IndexResult index(Index index) throws IOException {
922
924
if (plan .indexIntoLucene || plan .addStaleOpToLucene ) {
923
925
indexResult = indexIntoLucene (index , plan );
924
926
} else {
927
+ if (plan .versionForIndexing != index .indexingStrategy ().versionForIndexing || plan .currentNotFoundOrDeleted != index .indexingStrategy ().currentNotFoundOrDeleted ) {
928
+ throw new RuntimeException ("plan.versionForIndexing != index.indexingStrategy().versionForIndexing || plan.currentNotFoundOrDeleted != index.indexingStrategy().currentNotFoundOrDeleted" );
929
+ }
925
930
indexResult = new IndexResult (
926
931
plan .versionForIndexing ,
927
932
index .primaryTerm (),
@@ -956,6 +961,9 @@ public IndexResult index(Index index) throws IOException {
956
961
index .uid ().bytes (),
957
962
new IndexVersionValue (translogLocation , plan .versionForIndexing , index .seqNo (), index .primaryTerm ())
958
963
);
964
+ if (plan .versionForIndexing != index .indexingStrategy ().versionForIndexing ) {
965
+ System .out .println ("wrong" );
966
+ }
959
967
}
960
968
localCheckpointTracker .markSeqNoAsProcessed (indexResult .getSeqNo ());
961
969
if (indexResult .getTranslogLocation () == null ) {
@@ -987,7 +995,7 @@ protected final IndexingStrategy planIndexingAsNonPrimary(Index index) throws IO
987
995
assert assertNonPrimaryOrigin (index );
988
996
// needs to maintain the auto_id timestamp in case this replica becomes primary
989
997
if (canOptimizeAddDocument (index )) {// 如果不带主键的写入,肯定可以优化
990
- mayHaveBeenIndexedBefore (index );
998
+ mayHaveBeenIndexedBefore (index );// 可能更新maxUnsafeAutoIdTimestamp和必须更新maxSeenAutoIdTimestamp
991
999
}
992
1000
final IndexingStrategy plan ;
993
1001
// unlike the primary, replicas don't really care to about creation status of documents
@@ -1010,19 +1018,24 @@ protected final IndexingStrategy planIndexingAsNonPrimary(Index index) throws IO
1010
1018
} else {
1011
1019
boolean segRepEnabled = engineConfig .getIndexSettings ().isSegRepEnabledOrRemoteNode ();
1012
1020
versionMap .enforceSafeAccess ();
1013
- final OpVsLuceneDocStatus opVsLucene = compareOpToLuceneDocBasedOnSeqNo (index );
1014
- if (opVsLucene == OpVsLuceneDocStatus .OP_STALE_OR_EQUAL ) {
1015
- if (segRepEnabled ) {
1016
- // For segrep based indices, we can't completely rely on localCheckpointTracker
1017
- // as the preserved checkpoint may not have all the operations present in lucene
1018
- // we don't need to index it again as stale op as it would create multiple documents for same seq no
1019
- plan = IndexingStrategy .processButSkipLucene (false , index .version ());
1020
- } else {
1021
- plan = IndexingStrategy .processAsStaleOp (index .version ());
1021
+
1022
+ // if (index.IndexingStrategy() != null && segRepEnabled == false) {
1023
+ // return index.IndexingStrategy();
1024
+ // } else {
1025
+ final OpVsLuceneDocStatus opVsLucene = compareOpToLuceneDocBasedOnSeqNo (index );
1026
+ if (opVsLucene == OpVsLuceneDocStatus .OP_STALE_OR_EQUAL ) {// seqNo小于等于当前已经写入的
1027
+ if (segRepEnabled ) {
1028
+ // For segrep based indices, we can't completely rely on localCheckpointTracker
1029
+ // as the preserved checkpoint may not have all the operations present in lucene
1030
+ // we don't need to index it again as stale op as it would create multiple documents for same seq no
1031
+ plan = IndexingStrategy .processButSkipLucene (false , index .version ());
1032
+ } else {
1033
+ plan = IndexingStrategy .processAsStaleOp (index .version ());
1034
+ }
1035
+ } else {// 更新的,或者没找到
1036
+ plan = IndexingStrategy .processNormally (opVsLucene == OpVsLuceneDocStatus .LUCENE_DOC_NOT_FOUND , index .version (), 0 );
1022
1037
}
1023
- } else {
1024
- plan = IndexingStrategy .processNormally (opVsLucene == OpVsLuceneDocStatus .LUCENE_DOC_NOT_FOUND , index .version (), 0 );
1025
- }
1038
+ // }
1026
1039
}
1027
1040
return plan ;
1028
1041
}
@@ -1111,6 +1124,11 @@ private IndexResult indexIntoLucene(Index index, IndexingStrategy plan) throws I
1111
1124
assert index .seqNo () >= 0 : "ops should have an assigned seq no.; origin: " + index .origin ();
1112
1125
assert plan .versionForIndexing >= 0 : "version must be set. got " + plan .versionForIndexing ;
1113
1126
assert plan .indexIntoLucene || plan .addStaleOpToLucene ;
1127
+ InternalEngine .IndexingStrategy indexingStrategy = index .indexingStrategy ();
1128
+ if (indexingStrategy .versionForIndexing != plan .versionForIndexing || (indexingStrategy .indexIntoLucene || indexingStrategy .addStaleOpToLucene ) == false ) {
1129
+ throw new RuntimeException ("dddddd 0" );
1130
+ }
1131
+
1114
1132
/* Update the document's sequence number and primary term; the sequence number here is derived here from either the sequence
1115
1133
* number service if this is on the primary, or the existing document's sequence number if this is on the replica. The
1116
1134
* primary term here has already been set, see IndexShard#prepareIndex where the Engine$Index operation is created.
@@ -1119,15 +1137,24 @@ private IndexResult indexIntoLucene(Index index, IndexingStrategy plan) throws I
1119
1137
index .parsedDoc ().version ().setLongValue (plan .versionForIndexing );
1120
1138
try {
1121
1139
if (plan .addStaleOpToLucene ) {
1140
+ if (indexingStrategy .addStaleOpToLucene == false ) {
1141
+ throw new RuntimeException ("indexingStrategy.addStaleOpToLucene == false" );
1142
+ }
1122
1143
addStaleDocs (index .docs (), indexWriter );
1123
1144
} else if (plan .useLuceneUpdateDocument ) {
1145
+ if (indexingStrategy .useLuceneUpdateDocument == false ) {
1146
+ throw new RuntimeException ("indexingStrategy.useLuceneUpdateDocument == false" );
1147
+ }
1124
1148
assert assertMaxSeqNoOfUpdatesIsAdvanced (index .uid (), index .seqNo (), true , true );
1125
1149
updateDocs (index .uid (), index .docs (), indexWriter );
1126
1150
} else {
1127
1151
// document does not exists, we can optimize for create, but double check if assertions are running
1128
1152
assert assertDocDoesNotExist (index , canOptimizeAddDocument (index ) == false );
1129
1153
addDocs (index .docs (), indexWriter );
1130
1154
}
1155
+ if (plan .versionForIndexing != indexingStrategy .versionForIndexing || plan .currentNotFoundOrDeleted != indexingStrategy .currentNotFoundOrDeleted ) {
1156
+ throw new RuntimeException ("plan.versionForIndexing != indexingStrategy.versionForIndexing || plan.currentNotFoundOrDeleted != indexingStrategy.currentNotFoundOrDeleted" );
1157
+ }
1131
1158
return new IndexResult (plan .versionForIndexing , index .primaryTerm (), index .seqNo (), plan .currentNotFoundOrDeleted );
1132
1159
} catch (Exception ex ) {
1133
1160
if (ex instanceof AlreadyClosedException == false
0 commit comments