Skip to content

Commit 53f7d97

Browse files
Throwing exception for any update call for Append only indices (#17177) (#17179)
(cherry picked from commit 7306905) Signed-off-by: RS146BIJAY <rishavsagar4b1@gmail.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent fcf5c35 commit 53f7d97

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

server/src/main/java/org/opensearch/index/engine/InternalEngine.java

+7
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,13 @@ private boolean assertDocDoesNotExist(final Index index, final boolean allowDele
14181418
}
14191419

14201420
private void updateDocs(final Term uid, final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException {
1421+
if (engineConfig.getIndexSettings().getIndexMetadata().isAppendOnlyIndex()) {
1422+
failEngine(
1423+
"Failing shard as update operation is not allowed for append only index ",
1424+
new EngineException(shardId, "Unable to update document as it is an append only index")
1425+
);
1426+
}
1427+
14211428
if (docs.size() > 1) {
14221429
indexWriter.softUpdateDocuments(uid, docs, softDeletesField);
14231430
} else {

server/src/test/java/org/opensearch/index/engine/InternalEngineTests.java

+49
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,55 @@ public void testConcurrentGetAndFlush() throws Exception {
10841084
latestGetResult.get().close();
10851085
}
10861086

1087+
public void testUpdateOperationForAppendOnlyIndex() throws Exception {
1088+
Settings.Builder settings = Settings.builder()
1089+
.put(defaultSettings.getSettings())
1090+
.put(IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey(), "true");
1091+
final IndexMetadata indexMetadata = IndexMetadata.builder(defaultSettings.getIndexMetadata()).settings(settings).build();
1092+
final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(indexMetadata);
1093+
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
1094+
try (
1095+
Store store = createStore();
1096+
InternalEngine engine = createUpdateOnlyEngine(
1097+
config(indexSettings, store, createTempDir(), newMergePolicy(), null, null, globalCheckpoint::get)
1098+
)
1099+
) {
1100+
engine.refresh("warm_up");
1101+
Engine.Searcher searchResult = engine.acquireSearcher("test");
1102+
searchResult.close();
1103+
1104+
final BiFunction<String, Engine.SearcherScope, Engine.Searcher> searcherFactory = engine::acquireSearcher;
1105+
1106+
// create a document
1107+
Document document = testDocumentWithTextField();
1108+
document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
1109+
ParsedDocument doc = testParsedDocument("1", null, document, B_1, null);
1110+
expectThrows(AlreadyClosedException.class, () -> engine.index(indexForDoc(doc)));
1111+
}
1112+
}
1113+
1114+
private InternalEngine createUpdateOnlyEngine(EngineConfig config) throws IOException {
1115+
final Store store = config.getStore();
1116+
final Directory directory = store.directory();
1117+
if (Lucene.indexExists(directory) == false) {
1118+
store.createEmpty(config.getIndexSettings().getIndexVersionCreated().luceneVersion);
1119+
final String translogUuid = Translog.createEmptyTranslog(
1120+
config.getTranslogConfig().getTranslogPath(),
1121+
SequenceNumbers.NO_OPS_PERFORMED,
1122+
shardId,
1123+
primaryTerm.get()
1124+
);
1125+
store.associateIndexWithNewTranslog(translogUuid);
1126+
}
1127+
1128+
return new InternalEngine(config) {
1129+
@Override
1130+
protected IndexingStrategy indexingStrategyForOperation(Index index) throws IOException {
1131+
return IndexingStrategy.processNormally(false, 0, 0);
1132+
}
1133+
};
1134+
}
1135+
10871136
public void testSimpleOperations() throws Exception {
10881137
engine.refresh("warm_up");
10891138
Engine.Searcher searchResult = engine.acquireSearcher("test");

0 commit comments

Comments
 (0)