Make SearchStats implement Writeable (#29258)

Moves another class over from Streamable to Writeable. By this,
also some constructors can be removed or made private.
This commit is contained in:
Christoph Büscher 2018-03-27 15:21:11 +02:00 committed by GitHub
parent d2baf4b191
commit 8d6832c5ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 64 deletions

View File

@ -233,7 +233,7 @@ public class CommonStats implements Writeable, ToXContentFragment {
store = in.readOptionalStreamable(StoreStats::new);
indexing = in.readOptionalStreamable(IndexingStats::new);
get = in.readOptionalStreamable(GetStats::new);
search = in.readOptionalStreamable(SearchStats::new);
search = in.readOptionalWriteable(SearchStats::new);
merge = in.readOptionalStreamable(MergeStats::new);
refresh = in.readOptionalStreamable(RefreshStats::new);
flush = in.readOptionalStreamable(FlushStats::new);
@ -253,7 +253,7 @@ public class CommonStats implements Writeable, ToXContentFragment {
out.writeOptionalStreamable(store);
out.writeOptionalStreamable(indexing);
out.writeOptionalStreamable(get);
out.writeOptionalStreamable(search);
out.writeOptionalWriteable(search);
out.writeOptionalStreamable(merge);
out.writeOptionalStreamable(refresh);
out.writeOptionalStreamable(flush);

View File

@ -23,19 +23,20 @@ import org.elasticsearch.common.Nullable;
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.Streamable;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class SearchStats implements Streamable, ToXContentFragment {
public class SearchStats implements Writeable, ToXContentFragment {
public static class Stats implements Streamable, ToXContentFragment {
public static class Stats implements Writeable, ToXContentFragment {
private long queryCount;
private long queryTimeInMillis;
@ -53,8 +54,8 @@ public class SearchStats implements Streamable, ToXContentFragment {
private long suggestTimeInMillis;
private long suggestCurrent;
Stats() {
private Stats() {
// for internal use, initializes all counts to 0
}
public Stats(
@ -78,16 +79,24 @@ public class SearchStats implements Streamable, ToXContentFragment {
this.suggestCount = suggestCount;
this.suggestTimeInMillis = suggestTimeInMillis;
this.suggestCurrent = suggestCurrent;
}
public Stats(Stats stats) {
this(
stats.queryCount, stats.queryTimeInMillis, stats.queryCurrent,
stats.fetchCount, stats.fetchTimeInMillis, stats.fetchCurrent,
stats.scrollCount, stats.scrollTimeInMillis, stats.scrollCurrent,
stats.suggestCount, stats.suggestTimeInMillis, stats.suggestCurrent
);
private Stats(StreamInput in) throws IOException {
queryCount = in.readVLong();
queryTimeInMillis = in.readVLong();
queryCurrent = in.readVLong();
fetchCount = in.readVLong();
fetchTimeInMillis = in.readVLong();
fetchCurrent = in.readVLong();
scrollCount = in.readVLong();
scrollTimeInMillis = in.readVLong();
scrollCurrent = in.readVLong();
suggestCount = in.readVLong();
suggestTimeInMillis = in.readVLong();
suggestCurrent = in.readVLong();
}
public void add(Stats stats) {
@ -173,28 +182,7 @@ public class SearchStats implements Streamable, ToXContentFragment {
}
public static Stats readStats(StreamInput in) throws IOException {
Stats stats = new Stats();
stats.readFrom(in);
return stats;
}
@Override
public void readFrom(StreamInput in) throws IOException {
queryCount = in.readVLong();
queryTimeInMillis = in.readVLong();
queryCurrent = in.readVLong();
fetchCount = in.readVLong();
fetchTimeInMillis = in.readVLong();
fetchCurrent = in.readVLong();
scrollCount = in.readVLong();
scrollTimeInMillis = in.readVLong();
scrollCurrent = in.readVLong();
suggestCount = in.readVLong();
suggestTimeInMillis = in.readVLong();
suggestCurrent = in.readVLong();
return new Stats(in);
}
@Override
@ -238,11 +226,11 @@ public class SearchStats implements Streamable, ToXContentFragment {
}
}
Stats totalStats;
long openContexts;
private final Stats totalStats;
private long openContexts;
@Nullable
Map<String, Stats> groupStats;
private Map<String, Stats> groupStats;
public SearchStats() {
totalStats = new Stats();
@ -254,27 +242,27 @@ public class SearchStats implements Streamable, ToXContentFragment {
this.groupStats = groupStats;
}
public void add(SearchStats searchStats) {
add(searchStats, true);
public SearchStats(StreamInput in) throws IOException {
totalStats = Stats.readStats(in);
openContexts = in.readVLong();
if (in.readBoolean()) {
groupStats = in.readMap(StreamInput::readString, Stats::readStats);
}
}
public void add(SearchStats searchStats, boolean includeTypes) {
public void add(SearchStats searchStats) {
if (searchStats == null) {
return;
}
addTotals(searchStats);
openContexts += searchStats.openContexts;
if (includeTypes && searchStats.groupStats != null && !searchStats.groupStats.isEmpty()) {
if (searchStats.groupStats != null && !searchStats.groupStats.isEmpty()) {
if (groupStats == null) {
groupStats = new HashMap<>(searchStats.groupStats.size());
}
for (Map.Entry<String, Stats> entry : searchStats.groupStats.entrySet()) {
Stats stats = groupStats.get(entry.getKey());
if (stats == null) {
groupStats.put(entry.getKey(), new Stats(entry.getValue()));
} else {
stats.add(entry.getValue());
}
groupStats.putIfAbsent(entry.getKey(), new Stats());
groupStats.get(entry.getKey()).add(entry.getValue());
}
}
}
@ -296,7 +284,7 @@ public class SearchStats implements Streamable, ToXContentFragment {
@Nullable
public Map<String, Stats> getGroupStats() {
return this.groupStats;
return this.groupStats != null ? Collections.unmodifiableMap(this.groupStats) : null;
}
@Override
@ -344,15 +332,6 @@ public class SearchStats implements Streamable, ToXContentFragment {
static final String SUGGEST_CURRENT = "suggest_current";
}
@Override
public void readFrom(StreamInput in) throws IOException {
totalStats = Stats.readStats(in);
openContexts = in.readVLong();
if (in.readBoolean()) {
groupStats = in.readMap(StreamInput::readString, Stats::readStats);
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
totalStats.writeTo(out);

View File

@ -17,16 +17,16 @@
* under the License.
*/
package org.elasticsearch.search.stats;
package org.elasticsearch.index.search.stats;
import org.elasticsearch.index.search.stats.SearchStats;
import org.elasticsearch.index.search.stats.SearchStats.Stats;
import org.elasticsearch.test.ESTestCase;
import java.util.HashMap;
import java.util.Map;
public class SearchStatsUnitTests extends ESTestCase {
public class SearchStatsTests extends ESTestCase {
// https://github.com/elastic/elasticsearch/issues/7644
public void testShardLevelSearchGroupStats() throws Exception {
// let's create two dummy search stats with groups
@ -52,7 +52,7 @@ public class SearchStatsUnitTests extends ESTestCase {
assertStats(groupStats1.get("group1"), 3);
}
private void assertStats(Stats stats, long equalTo) {
private static void assertStats(Stats stats, long equalTo) {
assertEquals(equalTo, stats.getQueryCount());
assertEquals(equalTo, stats.getQueryTimeInMillis());
assertEquals(equalTo, stats.getQueryCurrent());
@ -66,4 +66,5 @@ public class SearchStatsUnitTests extends ESTestCase {
assertEquals(equalTo, stats.getSuggestTimeInMillis());
assertEquals(equalTo, stats.getSuggestCurrent());
}
}