Skip to content

Commit a7f8409

Browse files
Fix Snapshot Shard Status Request Deduplication (#50788)
* Fix Snapshot Shard Status Request Deduplication The request deduplication didn't actually work for these requests since they had no `equals` and `hashCode` so the deduplicator wouldn't actually recognize equal requests.
1 parent 54f5907 commit a7f8409

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

server/src/main/java/org/elasticsearch/snapshots/SnapshotShardsService.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import java.util.Iterator;
7979
import java.util.List;
8080
import java.util.Map;
81+
import java.util.Objects;
8182
import java.util.function.Function;
8283
import java.util.stream.Collectors;
8384

@@ -392,9 +393,9 @@ private void syncShardStatsOnNewMaster(ClusterChangedEvent event) {
392393
* Internal request that is used to send changes in snapshot status to master
393394
*/
394395
public static class UpdateIndexShardSnapshotStatusRequest extends MasterNodeRequest<UpdateIndexShardSnapshotStatusRequest> {
395-
private Snapshot snapshot;
396-
private ShardId shardId;
397-
private ShardSnapshotStatus status;
396+
private final Snapshot snapshot;
397+
private final ShardId shardId;
398+
private final ShardSnapshotStatus status;
398399

399400
public UpdateIndexShardSnapshotStatusRequest(StreamInput in) throws IOException {
400401
super(in);
@@ -440,6 +441,23 @@ public ShardSnapshotStatus status() {
440441
public String toString() {
441442
return snapshot + ", shardId [" + shardId + "], status [" + status.state() + "]";
442443
}
444+
445+
@Override
446+
public boolean equals(final Object o) {
447+
if (this == o) {
448+
return true;
449+
}
450+
if (o == null || getClass() != o.getClass()) {
451+
return false;
452+
}
453+
final UpdateIndexShardSnapshotStatusRequest that = (UpdateIndexShardSnapshotStatusRequest) o;
454+
return snapshot.equals(that.snapshot) && shardId.equals(that.shardId) && status.equals(that.status);
455+
}
456+
457+
@Override
458+
public int hashCode() {
459+
return Objects.hash(snapshot, shardId, status);
460+
}
443461
}
444462

445463
/** Notify the master node that the given shard has been successfully snapshotted **/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.snapshots;
20+
21+
import org.elasticsearch.cluster.SnapshotsInProgress;
22+
import org.elasticsearch.common.UUIDs;
23+
import org.elasticsearch.index.shard.ShardId;
24+
import org.elasticsearch.test.ESTestCase;
25+
import org.elasticsearch.test.EqualsHashCodeTestUtils;
26+
27+
public class SnapshotShardsServiceTests extends ESTestCase {
28+
29+
public void testEqualsAndHashcodeUpdateIndexShardSnapshotStatusRequest() {
30+
EqualsHashCodeTestUtils.checkEqualsAndHashCode(
31+
new SnapshotShardsService.UpdateIndexShardSnapshotStatusRequest(
32+
new Snapshot(randomAlphaOfLength(10),
33+
new SnapshotId(randomAlphaOfLength(10), UUIDs.randomBase64UUID(random()))),
34+
new ShardId(randomAlphaOfLength(10), UUIDs.randomBase64UUID(random()), randomInt(5)),
35+
new SnapshotsInProgress.ShardSnapshotStatus(randomAlphaOfLength(10), UUIDs.randomBase64UUID(random()))),
36+
request ->
37+
new SnapshotShardsService.UpdateIndexShardSnapshotStatusRequest(request.snapshot(), request.shardId(), request.status()),
38+
request -> {
39+
final boolean mutateSnapshot = randomBoolean();
40+
final boolean mutateShardId = randomBoolean();
41+
final boolean mutateStatus = (mutateSnapshot || mutateShardId) == false || randomBoolean();
42+
return new SnapshotShardsService.UpdateIndexShardSnapshotStatusRequest(
43+
mutateSnapshot ? new Snapshot(randomAlphaOfLength(10),
44+
new SnapshotId(randomAlphaOfLength(10), UUIDs.randomBase64UUID(random()))) : request.snapshot(),
45+
mutateShardId ?
46+
new ShardId(randomAlphaOfLength(10), UUIDs.randomBase64UUID(random()), randomInt(5)) : request.shardId(),
47+
mutateStatus ? new SnapshotsInProgress.ShardSnapshotStatus(randomAlphaOfLength(10), UUIDs.randomBase64UUID(random()))
48+
: request.status()
49+
);
50+
});
51+
}
52+
53+
}

0 commit comments

Comments
 (0)