Skip to content

Commit 7306905

Browse files
authored
Throwing exception for any update call for Append only indices (#17177)
Signed-off-by: RS146BIJAY <rishavsagar4b1@gmail.com>
1 parent b9ddef9 commit 7306905

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
@@ -1327,6 +1327,13 @@ private boolean assertDocDoesNotExist(final Index index, final boolean allowDele
13271327
}
13281328

13291329
private void updateDocs(final Term uid, final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException {
1330+
if (engineConfig.getIndexSettings().getIndexMetadata().isAppendOnlyIndex()) {
1331+
failEngine(
1332+
"Failing shard as update operation is not allowed for append only index ",
1333+
new EngineException(shardId, "Unable to update document as it is an append only index")
1334+
);
1335+
}
1336+
13301337
if (docs.size() > 1) {
13311338
indexWriter.softUpdateDocuments(uid, docs, softDeletesField);
13321339
} else {

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

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

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

0 commit comments

Comments
 (0)