Skip to content

Commit

Permalink
Throwing exception for any update call for Append only indices (#17177)
Browse files Browse the repository at this point in the history
Signed-off-by: RS146BIJAY <rishavsagar4b1@gmail.com>
  • Loading branch information
RS146BIJAY authored Jan 29, 2025
1 parent b9ddef9 commit 7306905
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,13 @@ private boolean assertDocDoesNotExist(final Index index, final boolean allowDele
}

private void updateDocs(final Term uid, final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException {
if (engineConfig.getIndexSettings().getIndexMetadata().isAppendOnlyIndex()) {
failEngine(
"Failing shard as update operation is not allowed for append only index ",
new EngineException(shardId, "Unable to update document as it is an append only index")
);
}

if (docs.size() > 1) {
indexWriter.softUpdateDocuments(uid, docs, softDeletesField);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,55 @@ public void testConcurrentGetAndFlush() throws Exception {
latestGetResult.get().close();
}

public void testUpdateOperationForAppendOnlyIndex() throws Exception {
Settings.Builder settings = Settings.builder()
.put(defaultSettings.getSettings())
.put(IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey(), "true");
final IndexMetadata indexMetadata = IndexMetadata.builder(defaultSettings.getIndexMetadata()).settings(settings).build();
final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(indexMetadata);
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
try (
Store store = createStore();
InternalEngine engine = createUpdateOnlyEngine(
config(indexSettings, store, createTempDir(), newMergePolicy(), null, null, globalCheckpoint::get)
)
) {
engine.refresh("warm_up");
Engine.Searcher searchResult = engine.acquireSearcher("test");
searchResult.close();

final BiFunction<String, Engine.SearcherScope, Engine.Searcher> searcherFactory = engine::acquireSearcher;

// create a document
Document document = testDocumentWithTextField();
document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
ParsedDocument doc = testParsedDocument("1", null, document, B_1, null);
expectThrows(AlreadyClosedException.class, () -> engine.index(indexForDoc(doc)));
}
}

private InternalEngine createUpdateOnlyEngine(EngineConfig config) throws IOException {
final Store store = config.getStore();
final Directory directory = store.directory();
if (Lucene.indexExists(directory) == false) {
store.createEmpty(config.getIndexSettings().getIndexVersionCreated().luceneVersion);
final String translogUuid = Translog.createEmptyTranslog(
config.getTranslogConfig().getTranslogPath(),
SequenceNumbers.NO_OPS_PERFORMED,
shardId,
primaryTerm.get()
);
store.associateIndexWithNewTranslog(translogUuid);
}

return new InternalEngine(config) {
@Override
protected IndexingStrategy indexingStrategyForOperation(Index index) throws IOException {
return IndexingStrategy.processNormally(false, 0, 0);
}
};
}

public void testSimpleOperations() throws Exception {
engine.refresh("warm_up");
Engine.Searcher searchResult = engine.acquireSearcher("test");
Expand Down

0 comments on commit 7306905

Please sign in to comment.