-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding shard count to node stats api (#75433)
* Adding shard count to _nodes/stats api Added a shards section to each node returned by the _nodes/stats api. Currently this new section only contains a total count of all shards on the node.
- Loading branch information
Showing
8 changed files
with
241 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
server/src/main/java/org/elasticsearch/index/shard/ShardCountStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.shard; | ||
|
||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.ToXContentFragment; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.core.Nullable; | ||
|
||
import java.io.IOException; | ||
|
||
public class ShardCountStats implements Writeable, ToXContentFragment { | ||
|
||
private final long totalCount; | ||
|
||
public ShardCountStats() { | ||
totalCount = 0; | ||
} | ||
|
||
public ShardCountStats(StreamInput in) throws IOException { | ||
totalCount = in.readVLong(); | ||
} | ||
|
||
public ShardCountStats(long totalCount) { | ||
this.totalCount = totalCount; | ||
} | ||
|
||
public long getTotalCount() { | ||
return this.totalCount; | ||
} | ||
|
||
public ShardCountStats add(@Nullable ShardCountStats other) { | ||
return new ShardCountStats(this.totalCount + (other == null ? 0 : other.totalCount)); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(Fields.SHARDS); | ||
builder.field(Fields.TOTAL_COUNT, totalCount); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeVLong(totalCount); | ||
} | ||
|
||
static final class Fields { | ||
static final String SHARDS = "shards"; | ||
static final String TOTAL_COUNT = "total_count"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return (o instanceof ShardCountStats) && totalCount == ((ShardCountStats) o).totalCount; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Long.hashCode(totalCount); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
server/src/test/java/org/elasticsearch/index/shard/ShardCountStatsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.shard; | ||
|
||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
import org.junit.Assert; | ||
|
||
class ShardCountStatsTest extends AbstractWireSerializingTestCase<ShardCountStats> { | ||
|
||
public void testAdd() { | ||
ShardCountStats shardStats1 = new ShardCountStats(5); | ||
ShardCountStats shardStats2 = new ShardCountStats(8); | ||
ShardCountStats combinedShardStats = shardStats1.add(shardStats2); | ||
Assert.assertEquals(13, combinedShardStats.getTotalCount()); | ||
Assert.assertEquals(5, shardStats1.getTotalCount()); | ||
Assert.assertEquals(8, shardStats2.getTotalCount()); | ||
ShardCountStats noCountGiven = new ShardCountStats(); | ||
Assert.assertEquals(0, noCountGiven.getTotalCount()); | ||
Assert.assertEquals(8, shardStats2.add(null).getTotalCount()); | ||
Assert.assertEquals(8, shardStats2.getTotalCount()); | ||
} | ||
|
||
@Override | ||
protected Writeable.Reader<ShardCountStats> instanceReader() { | ||
return ShardCountStats::new; | ||
} | ||
|
||
@Override | ||
protected ShardCountStats createTestInstance() { | ||
return new ShardCountStats(randomIntBetween(0, Integer.MAX_VALUE)); | ||
} | ||
} |