Skip to content

Conversation

@tlrx
Copy link
Member

@tlrx tlrx commented Mar 12, 2025

Today shard's engine mutation are guarded by an engineMutex object monitor. But we would like to be able to execute one or more operations on an engine instance, without this instance being resetted during the execution of the operation.

In order to do that, this change replaces the engineMutex by a reentrant read/write lock and introduces two new methods IndexShard#withEngine() and IndexShard#withEngineOrNull() that can be used to execute an operation while avoiding the current engine instance to be reset. It does not prevent it to be closed during execution though.

Relates ES-10826

Note: I'm opening this change for further discussion and hand-off.

@tlrx tlrx force-pushed the ES-10826-no-refresh-on-close branch from 782187e to acf2a3a Compare March 12, 2025 12:48
@elasticsearchmachine elasticsearchmachine added the serverless-linked Added by automation, don't add manually label Mar 12, 2025
@tlrx tlrx force-pushed the ES-10826-no-refresh-on-close branch 5 times, most recently from fbea5c1 to 4958122 Compare March 13, 2025 10:58
@tlrx tlrx changed the title Draft ES-10826 (no refresh on engine close) Draft ES-10826 (no write lock held during engine close) Mar 13, 2025
@tlrx tlrx force-pushed the ES-10826-no-refresh-on-close branch 3 times, most recently from 82ada07 to f565ee1 Compare March 13, 2025 13:42
@tlrx tlrx removed the serverless-linked Added by automation, don't add manually label Mar 13, 2025
@tlrx tlrx force-pushed the ES-10826-no-refresh-on-close branch 2 times, most recently from 920c83c to 2256a6f Compare March 13, 2025 17:17
@tlrx tlrx force-pushed the ES-10826-no-refresh-on-close branch from 2256a6f to a2f57d2 Compare March 14, 2025 09:37
@tlrx tlrx changed the title Draft ES-10826 (no write lock held during engine close) Use read/write engine lock to guard operations against resets Mar 14, 2025
@tlrx tlrx added the :Distributed Indexing/Engine Anything around managing Lucene and the Translog in an open shard. label Mar 14, 2025
@tlrx tlrx marked this pull request as ready for review March 14, 2025 11:40
@tlrx tlrx requested a review from fcofdez March 14, 2025 11:40
@elasticsearchmachine elasticsearchmachine added the Team:Distributed Indexing Meta label for Distributed Indexing team label Mar 14, 2025
@tlrx tlrx requested a review from henningandersen March 14, 2025 11:40
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-distributed-indexing (Team:Distributed Indexing)

Copy link
Contributor

@henningandersen henningandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I assume tests pass?

return store.getMetadata(null, true);
engineLock.readLock().lock();
try {
synchronized (closeMutex) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should take the closeMutex first to have same lock acquisition ordering as in close?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I pushed a1b5ff3

Comment on lines +4460 to +4462
// How do we ensure that no indexing operations have been processed since prepareForEngineReset() here? We're not
// blocking all operations when resetting the engine nor we are blocking flushes or force-merges.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the assumption is that we are indeed blocking operations? IIUC, we only hollow under the permit today and unhollow will happen prior to the indexing. I suppose we will get to this issue down the road once we start online hollowing, but would it not be ok to assume that the client has acquired permits/blocked operations?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the relocation also does something to merge and flush.

But you are probably right about the need to protect against changes. Could the IndexEngine do so, given that it knows it is now hollow through a method called from all such mutating methods?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are indeed blocking ingestion when hollowing during the primary relocation.

We are also in general blocking ingestion (with our own ingestion blocker in stateless) when unhollowing.

I think maybe we could add an assertion that either the permits are held, or the ingestion blocker in stateless is installed here. But it might be a bit cumbersome to put the assertion on the ingestion blocker here (since it's in stateless code). I'd leave it to @fcofdez to figure this out (and can help if needed).

There is a small chance a force merge might come through and it might fail, and we created ES-11277 to investigate in the future if it's serious to handle (for the moment we believe not that serious).

@fcofdez
Copy link
Contributor

fcofdez commented Mar 21, 2025

Unrelated failure:

> Task :x-pack:plugin:logsdb:javaRestTest
--
  | java.lang.AssertionError: Source matching failed at document id [13]. Source documents don't match for field [bIfLGBoqaw.GgwIIyZm.xjjfhj]: Error [Values of type [half_float] don't match after normalization

Triggering CI again.

Copy link
Member Author

@tlrx tlrx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fcofdez fcofdez merged commit 4aa7ce5 into elastic:main Mar 25, 2025
17 checks passed
fcofdez added a commit to fcofdez/elasticsearch that referenced this pull request Mar 26, 2025
tlrx added a commit to tlrx/elasticsearch that referenced this pull request Mar 28, 2025
omricohenn pushed a commit to omricohenn/elasticsearch that referenced this pull request Mar 28, 2025
…c#124635)

Today shard's engine mutation are guarded by an engineMutex object monitor. But we would like to be able to execute one or more operations on an engine instance, without this instance being resetted during the execution of the operation.

In order to do that, this change replaces the engineMutex by a reentrant read/write lock and introduces two new methods IndexShard#withEngine() and IndexShard#withEngineOrNull() that can be used to execute an operation while avoiding the current engine instance to be reset. It does not prevent it to be closed during execution though.

Relates ES-10826

Co-authored-by: Francisco Fernández Castaño <[email protected]>
tlrx added a commit that referenced this pull request Mar 31, 2025
tlrx added a commit that referenced this pull request Mar 31, 2025
tlrx added a commit to tlrx/elasticsearch that referenced this pull request Apr 2, 2025
tlrx added a commit to tlrx/elasticsearch that referenced this pull request Apr 3, 2025
tlrx added a commit to tlrx/elasticsearch that referenced this pull request Apr 7, 2025
tlrx added a commit to tlrx/elasticsearch that referenced this pull request Apr 8, 2025
tlrx added a commit that referenced this pull request Apr 10, 2025
…6311)

This change re-introduces the engine read/write lock to guard against engine resets.

It differs from #124635 on the following:
    uses the engineMutex for creating/closing engines
    uses the reentrant r/w lock for retaining engine instances and for resetting the engine
    acquires the reentrant read lock during refreshes to prevent deadlocks during resets
    add tests to ensure no deadlock when re-acquiring read lock in refresh listeners

Relates ES-11447
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Distributed Indexing/Engine Anything around managing Lucene and the Translog in an open shard. >non-issue serverless-linked Added by automation, don't add manually Team:Distributed Indexing Meta label for Distributed Indexing team v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants