Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/134198.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 134198
summary: Improve `ShardLockObtainFailedException` message
area: Store
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -1009,12 +1009,16 @@ void acquire(long timeoutInMillis, final String details) throws ShardLockObtainF
setDetails(details);
} else {
final Tuple<Long, String> lockDetails = this.lockDetails; // single volatile read
final var stateAge = TimeValue.timeValueNanos(System.nanoTime() - lockDetails.v1());
final var message = format(
"obtaining shard lock for [%s] timed out after [%dms], lock already held for [%s] with age [%dms]",
"""
obtaining shard lock for [%s] timed out after [%dms]; \
this shard lock is still held by a different instance of the shard and has been in state [%s] for [%s/%dms]""",
Copy link
Member

Choose a reason for hiding this comment

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

different instance

I think this should be true. Just want to explicitly confirm it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In practice yes. If it were the same instance of the shard, it wouldn't be trying to acquire a fresh shard lock.

Technically it might not be an IndexShard instance that holds this lock, it could also be one of the processes that deletes unused shard data, but we don't see that happen here and IMO the phrase instance of the shard is sufficiently loosely defined to include those processes too.

details,
timeoutInMillis,
lockDetails.v2(),
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - lockDetails.v1())
stateAge,
stateAge.millis()
);
maybeLogThreadDump(shardId, message);
throw new ShardLockObtainFailedException(shardId, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.startsWith;

@LuceneTestCase.SuppressFileSystems("ExtrasFS") // TODO: fix test to allow extras
Expand Down Expand Up @@ -134,25 +135,28 @@ public void testShardLock() throws Exception {

try (var mockLog = MockLog.capture(NodeEnvironment.class); var lock = env.shardLock(new ShardId(index, 0), "1")) {
mockLog.addExpectation(
new MockLog.SeenEventExpectation(
"hot threads logging",
NODE_ENVIRONMENT_LOGGER_NAME,
Level.DEBUG,
"hot threads while failing to obtain shard lock for [foo][0]: obtaining shard lock for [2] timed out after *"
)
new MockLog.SeenEventExpectation("hot threads logging", NODE_ENVIRONMENT_LOGGER_NAME, Level.DEBUG, """
hot threads while failing to obtain shard lock for [foo][0]: obtaining shard lock for [2] timed out after [*ms]; \
this shard lock is still held by a different instance of the shard and has been in state [1] for [*/*ms]*""")
);
mockLog.addExpectation(
new MockLog.UnseenEventExpectation(
"second attempt should be suppressed due to throttling",
NODE_ENVIRONMENT_LOGGER_NAME,
Level.DEBUG,
"hot threads while failing to obtain shard lock for [foo][0]: obtaining shard lock for [3] timed out after *"
"*obtaining shard lock for [3] timed out*"
)
);

assertEquals(new ShardId(index, 0), lock.getShardId());

expectThrows(ShardLockObtainFailedException.class, () -> env.shardLock(new ShardId(index, 0), "2"));
assertThat(
expectThrows(ShardLockObtainFailedException.class, () -> env.shardLock(new ShardId(index, 0), "2")).getMessage(),
matchesPattern("""
\\[foo]\\[0]: obtaining shard lock for \\[2] timed out after \\[0ms]; \
this shard lock is still held by a different instance of the shard \
and has been in state \\[1] for \\[.*/[0-9]+ms]""")
);

for (Path path : env.indexPaths(index)) {
Files.createDirectories(path.resolve("0"));
Expand Down