Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Change the default value of doc_values in WildcardFieldMapper to true. ([#19796](https://github.com/opensearch-project/OpenSearch/pull/19796))
- Make Engine#loadHistoryUUID() protected and Origin#isFromTranslog() public ([#19753](https://github.com/opensearch-project/OpenSearch/pull/19752))
- Bump opensearch-protobufs dependency to 0.23.0 and update transport-grpc module compatibility ([#19831](https://github.com/opensearch-project/OpenSearch/pull/19831))
- Refactor the DocStats and StoreStats class to use the Builder pattern instead of constructors ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863))

### Fixed
- Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012))
Expand Down Expand Up @@ -74,6 +75,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Deprecated
- Deprecated existing constructors in ThreadPoolStats.Stats in favor of the new Builder ([#19317](https://github.com/opensearch-project/OpenSearch/pull/19317))
- Deprecated existing constructors in IndexingStats.Stats in favor of the new Builder ([#19306](https://github.com/opensearch-project/OpenSearch/pull/19306))
- Deprecated existing constructors in DocStats and StoreStats in favor of the new Builder ([#19863](https://github.com/opensearch-project/OpenSearch/pull/19863))

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testStats() throws IOException {

final long otherStatsBytes = randomLongBetween(0L, Integer.MAX_VALUE);
final long otherStatsReservedBytes = randomBoolean() ? StoreStats.UNKNOWN_RESERVED_BYTES : randomLongBetween(0L, Integer.MAX_VALUE);
stats.add(new StoreStats(otherStatsBytes, otherStatsReservedBytes));
stats.add(new StoreStats.Builder().sizeInBytes(otherStatsBytes).reservedSize(otherStatsReservedBytes).build());
assertEquals(initialStoreSize + otherStatsBytes, stats.getSize().getBytes());
assertEquals(Math.max(reservedBytes, 0L) + Math.max(otherStatsReservedBytes, 0L), stats.getReservedSize().getBytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ protected final DocsStats docsStats(IndexReader indexReader) {
logger.trace(() -> new ParameterizedMessage("failed to get size for [{}]", info.info.name), e);
}
}
return new DocsStats(numDocs, numDeletedDocs, sizeInBytes);
return new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(sizeInBytes).build();
}

/**
Expand Down
51 changes: 51 additions & 0 deletions server/src/main/java/org/opensearch/index/shard/DocsStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,28 @@ public DocsStats() {

}

/**
* Private constructor that takes a builder.
* This is the sole entry point for creating a new DocsStats object.
* @param builder The builder instance containing all the values.
*/
private DocsStats(Builder builder) {
this.count = builder.count;
this.deleted = builder.deleted;
this.totalSizeInBytes = builder.totalSizeInBytes;
}

public DocsStats(StreamInput in) throws IOException {
count = in.readVLong();
deleted = in.readVLong();
totalSizeInBytes = in.readVLong();
}

/**
* This constructor will be deprecated starting in version 3.4.0.
* Use {@link Builder} instead.
*/
@Deprecated
public DocsStats(long count, long deleted, long totalSizeInBytes) {
this.count = count;
this.deleted = deleted;
Expand Down Expand Up @@ -107,6 +123,41 @@ public long getAverageSizeInBytes() {
return totalDocs == 0 ? 0 : totalSizeInBytes / totalDocs;
}

/**
* Builder for the {@link DocsStats} class.
* Provides a fluent API for constructing a DocsStats object.
*/
public static class Builder {
private long count = 0;
private long deleted = 0;
private long totalSizeInBytes = 0;

public Builder() {}

public Builder count(long count) {
this.count = count;
return this;
}

public Builder deleted(long deleted) {
this.deleted = deleted;
return this;
}

public Builder totalSizeInBytes(long totalSizeInBytes) {
this.totalSizeInBytes = totalSizeInBytes;
return this;
}

/**
* Creates a {@link DocsStats} object from the builder's current state.
* @return A new DocsStats instance.
*/
public DocsStats build() {
return new DocsStats(this);
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public CheckIndex.Status checkIndex(PrintStream out) throws IOException {
*/
public StoreStats stats(long reservedBytes) throws IOException {
ensureOpen();
return new StoreStats(directory.estimateSize(), reservedBytes);
return new StoreStats.Builder().sizeInBytes(directory.estimateSize()).reservedSize(reservedBytes).build();
}

/**
Expand Down
53 changes: 50 additions & 3 deletions server/src/main/java/org/opensearch/index/store/StoreStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,26 @@ public StoreStats() {

}

/**
* Private constructor that takes a builder.
* This is the sole entry point for creating a new StoreStats object.
* @param builder The builder instance containing all the values.
*/
private StoreStats(Builder builder) {
this.sizeInBytes = builder.sizeInBytes;
this.reservedSize = builder.reservedSize;
}

public StoreStats(StreamInput in) throws IOException {
sizeInBytes = in.readVLong();
reservedSize = in.readZLong();
}

/**
* @param sizeInBytes the size of the store in bytes
* @param reservedSize a prediction of how much larger the store is expected to grow, or {@link StoreStats#UNKNOWN_RESERVED_BYTES}.
*/
* This constructor will be deprecated starting in version 3.4.0.
* Use {@link Builder} instead.
*/
@Deprecated
public StoreStats(long sizeInBytes, long reservedSize) {
assert reservedSize == UNKNOWN_RESERVED_BYTES || reservedSize >= 0 : reservedSize;
this.sizeInBytes = sizeInBytes;
Expand Down Expand Up @@ -114,6 +125,42 @@ public ByteSizeValue getReservedSize() {
return new ByteSizeValue(reservedSize);
}

/**
* Builder for the {@link StoreStats} class.
* Provides a fluent API for constructing a StoreStats object.
*/
public static class Builder {
private long sizeInBytes = 0;
private long reservedSize = 0;

public Builder() {}

/**
* @param bytes the size of the store in bytes
*/
public Builder sizeInBytes(long bytes) {
this.sizeInBytes = bytes;
return this;
}

/**
* @param size a prediction of how much larger the store is expected to grow, or {@link StoreStats#UNKNOWN_RESERVED_BYTES}.
*/
public Builder reservedSize(long size) {
assert size == UNKNOWN_RESERVED_BYTES || size >= 0 : size;
this.reservedSize = size;
return this;
}

/**
* Creates a {@link StoreStats} object from the builder's current state.
* @return A new StoreStats instance.
*/
public StoreStats build() {
return new StoreStats(this);
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(sizeInBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1147,10 +1147,10 @@ public void testOldVersionNodes() throws IOException {
long numDeletedDocs = randomLongBetween(0, 100);
CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE);

commonStats.docs = new DocsStats(numDocs, numDeletedDocs, 0);
commonStats.store = new StoreStats(100, 0L);
commonStats.docs = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build();
commonStats.store = new StoreStats.Builder().sizeInBytes(100).reservedSize(0L).build();
commonStats.indexing = new IndexingStats();
DocsStats hostDocStats = new DocsStats(numDocs, numDeletedDocs, 0);
DocsStats hostDocStats = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build();

CommonStatsFlags commonStatsFlags = new CommonStatsFlags();
commonStatsFlags.clear();
Expand Down Expand Up @@ -1193,8 +1193,8 @@ public void testNodeIndicesStatsSerialization() throws IOException {
levelParams.add(NodeIndicesStats.StatsLevel.NODE);
CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE);

commonStats.docs = new DocsStats(numDocs, numDeletedDocs, 0);
commonStats.store = new StoreStats(100, 0L);
commonStats.docs = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build();
commonStats.store = new StoreStats.Builder().sizeInBytes(100).reservedSize(0L).build();
commonStats.indexing = new IndexingStats();

CommonStatsFlags commonStatsFlags = new CommonStatsFlags();
Expand Down Expand Up @@ -1244,8 +1244,8 @@ public void testNodeIndicesStatsToXContent() {
levelParams.add(NodeIndicesStats.StatsLevel.NODE);
CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE);

commonStats.docs = new DocsStats(numDocs, numDeletedDocs, 0);
commonStats.store = new StoreStats(100, 0L);
commonStats.docs = new DocsStats.Builder().count(numDocs).deleted(numDeletedDocs).totalSizeInBytes(0).build();
commonStats.store = new StoreStats.Builder().sizeInBytes(100).reservedSize(0L).build();
commonStats.indexing = new IndexingStats();

CommonStatsFlags commonStatsFlags = new CommonStatsFlags();
Expand Down Expand Up @@ -1364,8 +1364,13 @@ public void testNodeIndicesStatsWithAndWithoutAggregations() throws IOException

private CommonStats createRandomCommonStats() {
CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE);
commonStats.docs = new DocsStats(randomLongBetween(0, 10000), randomLongBetween(0, 100), randomLongBetween(0, 1000));
commonStats.store = new StoreStats(randomLongBetween(0, 100), randomLongBetween(0, 1000));
commonStats.docs = new DocsStats.Builder().count(randomLongBetween(0, 10000))
.deleted(randomLongBetween(0, 100))
.totalSizeInBytes(randomLongBetween(0, 1000))
.build();
commonStats.store = new StoreStats.Builder().sizeInBytes(randomLongBetween(0, 100))
.reservedSize(randomLongBetween(0, 1000))
.build();
commonStats.indexing = new IndexingStats();
commonStats.completion = new CompletionStats();
commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,13 @@ private ClusterStatsNodeResponse createClusterStatsNodeResponse(

private CommonStats createRandomCommonStats() {
CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE);
commonStats.docs = new DocsStats(randomLongBetween(0, 10000), randomLongBetween(0, 100), randomLongBetween(0, 1000));
commonStats.store = new StoreStats(randomLongBetween(0, 100), randomLongBetween(0, 1000));
commonStats.docs = new DocsStats.Builder().count(randomLongBetween(0, 10000))
.deleted(randomLongBetween(0, 100))
.totalSizeInBytes(randomLongBetween(0, 1000))
.build();
commonStats.store = new StoreStats.Builder().sizeInBytes(randomLongBetween(0, 100))
.reservedSize(randomLongBetween(0, 1000))
.build();
commonStats.indexing = new IndexingStats();
commonStats.completion = new CompletionStats();
commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,13 @@ private ClusterStatsNodeResponse createClusterStatsNodeResponse(DiscoveryNode no

private CommonStats createRandomCommonStats() {
CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE);
commonStats.docs = new DocsStats(randomLongBetween(0, 10000), randomLongBetween(0, 100), randomLongBetween(0, 1000));
commonStats.store = new StoreStats(randomLongBetween(0, 100), randomLongBetween(0, 1000));
commonStats.docs = new DocsStats.Builder().count(randomLongBetween(0, 10000))
.deleted(randomLongBetween(0, 100))
.totalSizeInBytes(randomLongBetween(0, 1000))
.build();
commonStats.store = new StoreStats.Builder().sizeInBytes(randomLongBetween(0, 100))
.reservedSize(randomLongBetween(0, 1000))
.build();
commonStats.indexing = new IndexingStats();
commonStats.completion = new CompletionStats();
commonStats.flush = new FlushStats(randomLongBetween(0, 100), randomLongBetween(0, 100), randomLongBetween(0, 100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,19 @@ public void testEvaluateConditions() {
final Set<Condition<?>> conditions = Sets.newHashSet(maxDocsCondition, maxAgeCondition, maxSizeCondition);
Map<String, Boolean> results = evaluateConditions(
conditions,
new DocsStats(matchMaxDocs, 0L, ByteSizeUnit.MB.toBytes(120)),
new DocsStats.Builder().count(matchMaxDocs).deleted(0L).totalSizeInBytes(ByteSizeUnit.MB.toBytes(120)).build(),
metadata
);
assertThat(results.size(), equalTo(3));
for (Boolean matched : results.values()) {
assertThat(matched, equalTo(true));
}

results = evaluateConditions(conditions, new DocsStats(notMatchMaxDocs, 0, notMatchMaxSize.getBytes()), metadata);
results = evaluateConditions(
conditions,
new DocsStats.Builder().count(notMatchMaxDocs).deleted(0).totalSizeInBytes(notMatchMaxSize.getBytes()).build(),
metadata
);
assertThat(results.size(), equalTo(3));
for (Map.Entry<String, Boolean> entry : results.entrySet()) {
if (entry.getKey().equals(maxAgeCondition.toString())) {
Expand Down Expand Up @@ -211,7 +215,12 @@ public void testEvaluateWithoutMetadata() {

long matchMaxDocs = randomIntBetween(100, 1000);
final Set<Condition<?>> conditions = Sets.newHashSet(maxDocsCondition, maxAgeCondition, maxSizeCondition);
Map<String, Boolean> results = evaluateConditions(conditions, new DocsStats(matchMaxDocs, 0L, ByteSizeUnit.MB.toBytes(120)), null);

Map<String, Boolean> results = evaluateConditions(
conditions,
new DocsStats.Builder().count(matchMaxDocs).deleted(0L).totalSizeInBytes(ByteSizeUnit.MB.toBytes(120)).build(),
null
);
assertThat(results.size(), equalTo(3));
results.forEach((k, v) -> assertFalse(v));

Expand Down Expand Up @@ -390,10 +399,14 @@ public void testResolveIndices() {

private IndicesStatsResponse createIndicesStatResponse(String indexName, long totalDocs, long primariesDocs) {
final CommonStats primaryStats = mock(CommonStats.class);
when(primaryStats.getDocs()).thenReturn(new DocsStats(primariesDocs, 0, between(1, 10000)));
when(primaryStats.getDocs()).thenReturn(
new DocsStats.Builder().count(primariesDocs).deleted(0).totalSizeInBytes(between(1, 10000)).build()
);

final CommonStats totalStats = mock(CommonStats.class);
when(totalStats.getDocs()).thenReturn(new DocsStats(totalDocs, 0, between(1, 10000)));
when(totalStats.getDocs()).thenReturn(
new DocsStats.Builder().count(totalDocs).deleted(0).totalSizeInBytes(between(1, 10000)).build()
);

final IndicesStatsResponse response = mock(IndicesStatsResponse.class);
when(response.getPrimaries()).thenReturn(primaryStats);
Expand Down Expand Up @@ -422,10 +435,14 @@ private IndicesStatsResponse createAliasToMultipleIndicesStatsResponse(Map<Strin

private IndexStats createIndexStats(long primaries, long total) {
final CommonStats primariesCommonStats = mock(CommonStats.class);
when(primariesCommonStats.getDocs()).thenReturn(new DocsStats(primaries, 0, between(1, 10000)));
when(primariesCommonStats.getDocs()).thenReturn(
new DocsStats.Builder().count(primaries).deleted(0).totalSizeInBytes(between(1, 10000)).build()
);

final CommonStats totalCommonStats = mock(CommonStats.class);
when(totalCommonStats.getDocs()).thenReturn(new DocsStats(total, 0, between(1, 10000)));
when(totalCommonStats.getDocs()).thenReturn(
new DocsStats.Builder().count(total).deleted(0).totalSizeInBytes(between(1, 10000)).build()
);

IndexStats indexStats = mock(IndexStats.class);
when(indexStats.getPrimaries()).thenReturn(primariesCommonStats);
Expand Down
Loading
Loading