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

View File

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

View File

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