diff --git a/server/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/server/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index b6206a1e1b6..603dc15f7ce 100644 --- a/server/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/server/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -808,7 +808,7 @@ public abstract class StreamOutput extends OutputStream { if (writer != null) { writer.write(this, value); } else { - throw new IOException("can not write type [" + type + "]"); + throw new IllegalArgumentException("can not write type [" + type + "]"); } } diff --git a/server/src/main/java/org/elasticsearch/indices/IndicesRequestCache.java b/server/src/main/java/org/elasticsearch/indices/IndicesRequestCache.java index 21be46dcf75..78dbde7aa3c 100644 --- a/server/src/main/java/org/elasticsearch/indices/IndicesRequestCache.java +++ b/server/src/main/java/org/elasticsearch/indices/IndicesRequestCache.java @@ -28,6 +28,7 @@ import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.util.Accountable; import org.apache.lucene.util.RamUsageEstimator; +import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; @@ -43,6 +44,7 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import java.io.Closeable; +import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -115,8 +117,8 @@ public final class IndicesRequestCache implements RemovalListener loader, - DirectoryReader reader, BytesReference cacheKey, Supplier cacheKeyRenderer) throws Exception { + BytesReference getOrCompute(CacheEntity cacheEntity, CheckedSupplier loader, + DirectoryReader reader, BytesReference cacheKey, Supplier cacheKeyRenderer) throws Exception { assert reader.getReaderCacheHelper() != null; final Key key = new Key(cacheEntity, reader.getReaderCacheHelper().getKey(), cacheKey); Loader cacheLoader = new Loader(cacheEntity, loader); @@ -157,10 +159,10 @@ public final class IndicesRequestCache implements RemovalListener { private final CacheEntity entity; - private final Supplier loader; + private final CheckedSupplier loader; private boolean loaded; - Loader(CacheEntity entity, Supplier loader) { + Loader(CacheEntity entity, CheckedSupplier loader) { this.entity = entity; this.loader = loader; } diff --git a/server/src/main/java/org/elasticsearch/indices/IndicesService.java b/server/src/main/java/org/elasticsearch/indices/IndicesService.java index 05d816341d1..eb7ce281cd3 100644 --- a/server/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/server/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -45,7 +45,9 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.RecoverySource; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.CheckedFunction; +import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.breaker.CircuitBreaker; import org.elasticsearch.common.bytes.BytesReference; @@ -1376,12 +1378,7 @@ public class IndicesService extends AbstractLifecycleComponent () -> "Shard: " + request.shardId() + "\nSource:\n" + request.source(), out -> { queryPhase.execute(context); - try { - context.queryResult().writeToNoId(out); - - } catch (IOException e) { - throw new AssertionError("Could not serialize response", e); - } + context.queryResult().writeToNoId(out); loadedFromCache[0] = false; }); @@ -1420,9 +1417,9 @@ public class IndicesService extends AbstractLifecycleComponent * @return the contents of the cache or the result of calling the loader */ private BytesReference cacheShardLevelResult(IndexShard shard, DirectoryReader reader, BytesReference cacheKey, - Supplier cacheKeyRenderer, Consumer loader) throws Exception { + Supplier cacheKeyRenderer, CheckedConsumer loader) throws Exception { IndexShardCacheEntity cacheEntity = new IndexShardCacheEntity(shard); - Supplier supplier = () -> { + CheckedSupplier supplier = () -> { /* BytesStreamOutput allows to pass the expected size but by default uses * BigArrays.PAGE_SIZE_IN_BYTES which is 16k. A common cached result ie. * a date histogram with 3 buckets is ~100byte so 16k might be very wasteful diff --git a/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java b/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index 195abb85939..631ab18d8c9 100644 --- a/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java +++ b/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -349,6 +349,13 @@ public class BytesStreamsTests extends ESTestCase { assertThat(jdt.getZonedDateTime().toInstant().toEpochMilli(), equalTo(123456L)); assertThat(jdt.getZonedDateTime().getZone(), equalTo(ZoneId.of("America/Los_Angeles"))); assertEquals(0, in.available()); + IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> out.writeGenericValue(new Object() { + @Override + public String toString() { + return "This object cannot be serialized by writeGeneric method"; + } + })); + assertThat(ex.getMessage(), containsString("can not write type")); in.close(); out.close(); } diff --git a/server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheTests.java b/server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheTests.java index 6575503341c..6f734f55dd3 100644 --- a/server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheTests.java +++ b/server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheTests.java @@ -32,6 +32,7 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.bytes.AbstractBytesReference; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; @@ -49,7 +50,6 @@ import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Arrays; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Supplier; public class IndicesRequestCacheTests extends ESTestCase { @@ -331,7 +331,7 @@ public class IndicesRequestCacheTests extends ESTestCase { StringField.TYPE_STORED)); } - private static class Loader implements Supplier { + private static class Loader implements CheckedSupplier { private final DirectoryReader reader; private final int id;