Skip to content

Commit

Permalink
Use external version type for history ops from Lucene (#31568)
Browse files Browse the repository at this point in the history
Today we use INTERNAL version type for the history operations from
Lucene index. However, this does not cover the case in which the
original operation has the version type EXTERNAL and version number 0.
Semantically all operations from translog or Lucene should always use
EXTERNAL version type.
  • Loading branch information
dnhatn authored Jun 28, 2018
1 parent a55f614 commit e4315cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ private Translog.Operation readDocAsOp(int docIndex) throws IOException {
final String type = fields.uid().type();
final Term uid = new Term(IdFieldMapper.NAME, Uid.encodeId(id));
if (isTombstone) {
op = new Translog.Delete(type, id, uid, seqNo, primaryTerm, version, VersionType.INTERNAL);
op = new Translog.Delete(type, id, uid, seqNo, primaryTerm, version, VersionType.EXTERNAL);
assert assertDocSoftDeleted(leaf.reader(), segmentDocID) : "Delete op but soft_deletes field is not set [" + op + "]";
} else {
final BytesReference source = fields.source();
// TODO: pass the latest timestamp from engine.
final long autoGeneratedIdTimestamp = -1;
op = new Translog.Index(type, id, seqNo, primaryTerm, version, VersionType.INTERNAL,
op = new Translog.Index(type, id, seqNo, primaryTerm, version, VersionType.EXTERNAL,
source == null ? null : source.toBytesRef().bytes, fields.routing(), autoGeneratedIdTimestamp);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,9 @@ public String toString() {
", type='" + type + '\'' +
", seqNo=" + seqNo +
", primaryTerm=" + primaryTerm +
", version=" + version +
", versionType=" + versionType +
", autoGeneratedIdTimestamp=" + autoGeneratedIdTimestamp +
'}';
}

Expand Down Expand Up @@ -1340,6 +1343,8 @@ public String toString() {
"uid=" + uid +
", seqNo=" + seqNo +
", primaryTerm=" + primaryTerm +
", version=" + version +
", versionType=" + versionType +
'}';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.VersionType;
Expand Down Expand Up @@ -785,4 +786,23 @@ public void testGCDeletesZero() throws Exception {
.getVersion(),
equalTo(-1L));
}

public void testSpecialVersioning() {
internalCluster().ensureAtLeastNumDataNodes(2);
createIndex("test", Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).build());
IndexResponse doc1 = client().prepareIndex("test", "type", "1").setSource("field", "value1")
.setVersion(0).setVersionType(VersionType.EXTERNAL).execute().actionGet();
assertThat(doc1.getVersion(), equalTo(0L));
IndexResponse doc2 = client().prepareIndex("test", "type", "1").setSource("field", "value2")
.setVersion(Versions.MATCH_ANY).setVersionType(VersionType.INTERNAL).execute().actionGet();
assertThat(doc2.getVersion(), equalTo(1L));
client().prepareDelete("test", "type", "1").get(); //v2
IndexResponse doc3 = client().prepareIndex("test", "type", "1").setSource("field", "value3")
.setVersion(Versions.MATCH_DELETED).setVersionType(VersionType.INTERNAL).execute().actionGet();
assertThat(doc3.getVersion(), equalTo(3L));
// Make sure that these versions are replicated correctly
client().admin().indices().prepareUpdateSettings("test")
.setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)).get();
ensureGreen("test");
}
}

0 comments on commit e4315cc

Please sign in to comment.