Skip to content

Commit

Permalink
Fix array hashCode calculation in ResyncReplicationRequest (#16378)
Browse files Browse the repository at this point in the history
* Array, passed to Objects.hash(), should be wrapped into Arrays.hashCode(). Added unit test

Signed-off-by: Dmitry Kryukov <[email protected]>

* Updated CHANGELOG.md

Signed-off-by: Dmitry Kryukov <[email protected]>

---------

Signed-off-by: Dmitry Kryukov <[email protected]>
  • Loading branch information
dk2k authored Oct 19, 2024
1 parent f346788 commit 0f7d572
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ 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 inefficient Stream API call chains ending with count() ([#15386](https://github.com/opensearch-project/OpenSearch/pull/15386))
- 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 0f7d572

Please sign in to comment.