Skip to content

Commit

Permalink
Fix integer overflow for remaining index stats (#960) (#965)
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <dblock@amazon.com>
Co-authored-by: Oksana Yakusheva <oksanay@cloudcruiser.com>
  • Loading branch information
dblock and oksanay authored Apr 30, 2024
1 parent 1ef976e commit ab69f34
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed

### Fixed
- Fix integer overflow for variables in indices stats response ([#960](https://github.com/opensearch-project/opensearch-java/pull/960))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@

@JsonpDeserializable
public class QueryCacheStats implements JsonpSerializable {
private final int cacheCount;
private final long cacheCount;

private final int cacheSize;
private final long cacheSize;

private final int evictions;
private final long evictions;

private final int hitCount;
private final long hitCount;

@Nullable
private final String memorySize;

private final long memorySizeInBytes;

private final int missCount;
private final long missCount;

private final int totalCount;
private final long totalCount;

// ---------------------------------------------------------------------------------------------

Expand All @@ -88,28 +88,28 @@ public static QueryCacheStats of(Function<Builder, ObjectBuilder<QueryCacheStats
/**
* Required - API name: {@code cache_count}
*/
public final int cacheCount() {
public final long cacheCount() {
return this.cacheCount;
}

/**
* Required - API name: {@code cache_size}
*/
public final int cacheSize() {
public final long cacheSize() {
return this.cacheSize;
}

/**
* Required - API name: {@code evictions}
*/
public final int evictions() {
public final long evictions() {
return this.evictions;
}

/**
* Required - API name: {@code hit_count}
*/
public final int hitCount() {
public final long hitCount() {
return this.hitCount;
}

Expand All @@ -131,20 +131,21 @@ public final long memorySizeInBytes() {
/**
* Required - API name: {@code miss_count}
*/
public final int missCount() {
public final long missCount() {
return this.missCount;
}

/**
* Required - API name: {@code total_count}
*/
public final int totalCount() {
public final long totalCount() {
return this.totalCount;
}

/**
* Serialize this object to JSON.
*/
@Override
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartObject();
serializeInternal(generator, mapper);
Expand Down Expand Up @@ -188,51 +189,51 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<QueryCacheStats> {
private Integer cacheCount;
private Long cacheCount;

private Integer cacheSize;
private Long cacheSize;

private Integer evictions;
private Long evictions;

private Integer hitCount;
private Long hitCount;

@Nullable
private String memorySize;

private Long memorySizeInBytes;

private Integer missCount;
private Long missCount;

private Integer totalCount;
private Long totalCount;

/**
* Required - API name: {@code cache_count}
*/
public final Builder cacheCount(int value) {
public final Builder cacheCount(long value) {
this.cacheCount = value;
return this;
}

/**
* Required - API name: {@code cache_size}
*/
public final Builder cacheSize(int value) {
public final Builder cacheSize(long value) {
this.cacheSize = value;
return this;
}

/**
* Required - API name: {@code evictions}
*/
public final Builder evictions(int value) {
public final Builder evictions(long value) {
this.evictions = value;
return this;
}

/**
* Required - API name: {@code hit_count}
*/
public final Builder hitCount(int value) {
public final Builder hitCount(long value) {
this.hitCount = value;
return this;
}
Expand All @@ -256,15 +257,15 @@ public final Builder memorySizeInBytes(long value) {
/**
* Required - API name: {@code miss_count}
*/
public final Builder missCount(int value) {
public final Builder missCount(long value) {
this.missCount = value;
return this;
}

/**
* Required - API name: {@code total_count}
*/
public final Builder totalCount(int value) {
public final Builder totalCount(long value) {
this.totalCount = value;
return this;
}
Expand All @@ -275,6 +276,7 @@ public final Builder totalCount(int value) {
* @throws NullPointerException
* if some of the required fields are null.
*/
@Override
public QueryCacheStats build() {
_checkSingleUse();

Expand All @@ -294,14 +296,14 @@ public QueryCacheStats build() {

protected static void setupQueryCacheStatsDeserializer(ObjectDeserializer<QueryCacheStats.Builder> op) {

op.add(Builder::cacheCount, JsonpDeserializer.integerDeserializer(), "cache_count");
op.add(Builder::cacheSize, JsonpDeserializer.integerDeserializer(), "cache_size");
op.add(Builder::evictions, JsonpDeserializer.integerDeserializer(), "evictions");
op.add(Builder::hitCount, JsonpDeserializer.integerDeserializer(), "hit_count");
op.add(Builder::cacheCount, JsonpDeserializer.longDeserializer(), "cache_count");
op.add(Builder::cacheSize, JsonpDeserializer.longDeserializer(), "cache_size");
op.add(Builder::evictions, JsonpDeserializer.longDeserializer(), "evictions");
op.add(Builder::hitCount, JsonpDeserializer.longDeserializer(), "hit_count");
op.add(Builder::memorySize, JsonpDeserializer.stringDeserializer(), "memory_size");
op.add(Builder::memorySizeInBytes, JsonpDeserializer.longDeserializer(), "memory_size_in_bytes");
op.add(Builder::missCount, JsonpDeserializer.integerDeserializer(), "miss_count");
op.add(Builder::totalCount, JsonpDeserializer.integerDeserializer(), "total_count");
op.add(Builder::missCount, JsonpDeserializer.longDeserializer(), "miss_count");
op.add(Builder::totalCount, JsonpDeserializer.longDeserializer(), "total_count");

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.opensearch.client.opensearch._types;

import static org.junit.Assert.assertEquals;

import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import org.junit.Test;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;

public class QueryCacheStatsTest {

@Test
public void testLongSerialization() {
QueryCacheStats expected = new QueryCacheStats.Builder().cacheCount(2757938871l)
.cacheSize(1757938872l)
.evictions(3757938873l)
.hitCount(4757938874l)
.memorySizeInBytes(5757938875l)
.missCount(6757938876l)
.totalCount(7757938877l)
.build();

String jsonString = "{\"cache_count\": 2757938871, \"cache_size\": 1757938872, \"evictions\":"
+ " 3757938873, \"hit_count\": 4757938874, \"memory_size_in_bytes\": 5757938875, \"miss_count\":"
+ " 6757938876, \"total_count\": 7757938877}";

StringReader reader = new StringReader(jsonString);
JacksonJsonpMapper mapper = new JacksonJsonpMapper();
JsonParser parser = mapper.jsonProvider().createParser(reader);
QueryCacheStats actual = QueryCacheStats._DESERIALIZER.deserialize(parser, mapper);
assertEquals(expected.hitCount(), actual.hitCount());
assertEquals(expected.memorySizeInBytes(), actual.memorySizeInBytes());
assertEquals(expected.missCount(), actual.missCount());
assertEquals(expected.cacheCount(), actual.cacheCount());
assertEquals(expected.totalCount(), actual.totalCount());
assertEquals(expected.cacheSize(), actual.cacheSize());
assertEquals(expected.evictions(), actual.evictions());
}
}

0 comments on commit ab69f34

Please sign in to comment.