Skip to content

Commit dd3adac

Browse files
Fix Snapshot Shard Status Request Deduplication (elastic#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 75cb4e0 commit dd3adac

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import java.util.Iterator;
7878
import java.util.List;
7979
import java.util.Map;
80+
import java.util.Objects;
8081
import java.util.function.Function;
8182
import java.util.stream.Collectors;
8283

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

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

444462
/** Notify the master node that the given shard has been successfully snapshotted **/
Lines changed: 53 additions & 0 deletions
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)