Skip to content

Commit

Permalink
[Refactor] InternalEngine to always use soft deletes (#1933)
Browse files Browse the repository at this point in the history
Soft Deletes have been enabled by default since Legacy version 7.0 and made
mandatory in Version 2.0.0. This commit refactors the InternalEngine to always
use soft-deletes. It is a follow on to making soft deletes mandatory in 2.0.0.

Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize authored Jan 26, 2022
1 parent 56ae7fa commit 57ac788
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,12 @@ public void testRecovery() throws Exception {
* or not we have one. */
shouldHaveTranslog = randomBoolean();

Settings.Builder settings = Settings.builder();
if (minimumNodeVersion().before(Version.V_2_0_0) && randomBoolean()) {
settings.put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), randomBoolean());
}
createIndex(index, settings.build());

indexRandomDocuments(count, true, true, i -> jsonBuilder().startObject().field("field", "value").endObject());

// make sure all recoveries are done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,9 +1248,7 @@ public void testFilterCacheStats() throws Exception {
client().prepareIndex("index", "type", "1").setSource("foo", "bar"),
client().prepareIndex("index", "type", "2").setSource("foo", "baz")
);
if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(settings)) {
persistGlobalCheckpoint("index"); // Need to persist the global checkpoint for the soft-deletes retention MP.
}
persistGlobalCheckpoint("index"); // Need to persist the global checkpoint for the soft-deletes retention MP.
refresh();
ensureGreen();

Expand Down Expand Up @@ -1287,22 +1285,20 @@ public void testFilterCacheStats() throws Exception {
// Here we are testing that a fully deleted segment should be dropped and its cached is evicted.
// In order to instruct the merge policy not to keep a fully deleted segment,
// we need to flush and make that commit safe so that the SoftDeletesPolicy can drop everything.
if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(settings)) {
persistGlobalCheckpoint("index");
assertBusy(() -> {
for (final ShardStats shardStats : client().admin().indices().prepareStats("index").get().getIndex("index").getShards()) {
final long maxSeqNo = shardStats.getSeqNoStats().getMaxSeqNo();
assertTrue(
shardStats.getRetentionLeaseStats()
.retentionLeases()
.leases()
.stream()
.allMatch(retentionLease -> retentionLease.retainingSequenceNumber() == maxSeqNo + 1)
);
}
});
flush("index");
}
persistGlobalCheckpoint("index");
assertBusy(() -> {
for (final ShardStats shardStats : client().admin().indices().prepareStats("index").get().getIndex("index").getShards()) {
final long maxSeqNo = shardStats.getSeqNoStats().getMaxSeqNo();
assertTrue(
shardStats.getRetentionLeaseStats()
.retentionLeases()
.leases()
.stream()
.allMatch(retentionLease -> retentionLease.retainingSequenceNumber() == maxSeqNo + 1)
);
}
});
flush("index");
logger.info("--> force merging to a single segment");
ForceMergeResponse forceMergeResponse = client().admin()
.indices()
Expand Down
138 changes: 53 additions & 85 deletions server/src/main/java/org/opensearch/index/engine/InternalEngine.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,13 @@ protected final OpenSearchDirectoryReader wrapReader(
DirectoryReader reader,
Function<DirectoryReader, DirectoryReader> readerWrapperFunction
) throws IOException {
if (engineConfig.getIndexSettings().isSoftDeleteEnabled()) {
reader = new SoftDeletesDirectoryReaderWrapper(reader, Lucene.SOFT_DELETES_FIELD);
}
reader = readerWrapperFunction.apply(reader);
return OpenSearchDirectoryReader.wrap(reader, engineConfig.getShardId());
}

protected DirectoryReader open(IndexCommit commit) throws IOException {
assert Transports.assertNotTransportThread("opening index commit of a read-only engine");
return DirectoryReader.open(commit);
return new SoftDeletesDirectoryReaderWrapper(DirectoryReader.open(commit), Lucene.SOFT_DELETES_FIELD);
}

@Override
Expand Down Expand Up @@ -337,10 +334,7 @@ public Translog.Snapshot newChangesSnapshot(
long fromSeqNo,
long toSeqNo,
boolean requiredFullRange
) throws IOException {
if (engineConfig.getIndexSettings().isSoftDeleteEnabled() == false) {
throw new IllegalStateException("accessing changes snapshot requires soft-deletes enabled");
}
) {
return newEmptySnapshot();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void preParse(ParseContext context) throws IOException {
context.doc().add(new StoredField(fieldType().name(), ref.bytes, ref.offset, ref.length));
}

if (originalSource != null && adaptedSource != originalSource && context.indexSettings().isSoftDeleteEnabled()) {
if (originalSource != null && adaptedSource != originalSource) {
// if we omitted source or modified it we add the _recovery_source to ensure we have it for ops based recovery
BytesRef ref = originalSource.toBytesRef();
context.doc().add(new StoredField(RECOVERY_SOURCE_NAME, ref.bytes, ref.offset, ref.length));
Expand Down
Loading

0 comments on commit 57ac788

Please sign in to comment.