Skip to content

Commit

Permalink
Fix array hashCode calculation in ResyncReplicationRequest (#16378) (#…
Browse files Browse the repository at this point in the history
…16389)

* Array, passed to Objects.hash(), should be wrapped into Arrays.hashCode(). Added unit test



* Updated CHANGELOG.md



---------

Signed-off-by: Dmitry Kryukov <[email protected]>
Co-authored-by: Dmitry Kryukov <[email protected]>
  • Loading branch information
dbwiddis and dk2k authored Oct 21, 2024
1 parent 3665b55 commit 41d582e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Streaming Indexing] Fix intermittent 'The bulk request must be terminated by a newline [\n]' failures [#16337](https://github.com/opensearch-project/OpenSearch/pull/16337))
- Fix wrong default value when setting `index.number_of_routing_shards` to null on index creation ([#16331](https://github.com/opensearch-project/OpenSearch/pull/16331))
- [Workload Management] Make query groups persistent across process restarts [#16370](https://github.com/opensearch-project/OpenSearch/pull/16370)
- Fix array hashCode calculation in ResyncReplicationRequest ([#16378](https://github.com/opensearch-project/OpenSearch/pull/16378))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public boolean equals(final Object o) {

@Override
public int hashCode() {
return Objects.hash(trimAboveSeqNo, maxSeenAutoIdTimestampOnPrimary, operations);
return Objects.hash(trimAboveSeqNo, maxSeenAutoIdTimestampOnPrimary, Arrays.hashCode(operations));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.hamcrest.Matchers.equalTo;

public class ResyncReplicationRequestTests extends OpenSearchTestCase {

public void testSerialization() throws IOException {
final byte[] bytes = "{}".getBytes(Charset.forName("UTF-8"));
final byte[] bytes = "{}".getBytes(StandardCharsets.UTF_8);
final Translog.Index index = new Translog.Index("id", 0, randomNonNegativeLong(), randomNonNegativeLong(), bytes, null, -1);
final ShardId shardId = new ShardId(new Index("index", "uuid"), 0);
final ResyncReplicationRequest before = new ResyncReplicationRequest(shardId, 42L, 100, new Translog.Operation[] { index });
Expand All @@ -61,4 +61,18 @@ public void testSerialization() throws IOException {
assertThat(after, equalTo(before));
}

public void testContractBetweenEqualsAndHashCode() {
final byte[] bytes = "{}".getBytes(StandardCharsets.UTF_8);
final Translog.Index index = new Translog.Index("id", 0, 123L, -123L, bytes, null, -1);
final ShardId shardId = new ShardId(new Index("index", "uuid"), 0);
// Both created requests have arrays `operations` with the same content, and we want to verify that
// equals() and hashCode() are following the contract:
// If objects are equal, they have the same hash code
final ResyncReplicationRequest request1 = new ResyncReplicationRequest(shardId, 42L, 100, new Translog.Operation[] { index });
final ResyncReplicationRequest request2 = new ResyncReplicationRequest(shardId, 42L, 100, new Translog.Operation[] { index });

assertEquals(request1, request2);
assertEquals(request1.hashCode(), request2.hashCode());
}

}

0 comments on commit 41d582e

Please sign in to comment.