Search in sources :

Example 26 with MockPageCacheRecycler

use of org.opensearch.common.util.MockPageCacheRecycler in project OpenSearch by opensearch-project.

the class AbstractHttpServerTransportTests method setup.

@Before
public void setup() throws Exception {
    networkService = new NetworkService(Collections.emptyList());
    threadPool = new TestThreadPool("test");
    bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) NetworkService(org.opensearch.common.network.NetworkService) MockBigArrays(org.opensearch.common.util.MockBigArrays) TestThreadPool(org.opensearch.threadpool.TestThreadPool) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) Before(org.junit.Before)

Example 27 with MockPageCacheRecycler

use of org.opensearch.common.util.MockPageCacheRecycler in project OpenSearch by opensearch-project.

the class DefaultRestChannelTests method setup.

@Before
public void setup() {
    httpChannel = mock(HttpChannel.class);
    threadPool = new TestThreadPool("test");
    bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) MockBigArrays(org.opensearch.common.util.MockBigArrays) TestThreadPool(org.opensearch.threadpool.TestThreadPool) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) Before(org.junit.Before)

Example 28 with MockPageCacheRecycler

use of org.opensearch.common.util.MockPageCacheRecycler in project OpenSearch by opensearch-project.

the class DefaultRestChannelTests method testCloseOnException.

public void testCloseOnException() {
    final boolean close = randomBoolean();
    final HttpRequest.HttpVersion httpVersion = close ? HttpRequest.HttpVersion.HTTP_1_0 : HttpRequest.HttpVersion.HTTP_1_1;
    final String httpConnectionHeaderValue = close ? DefaultRestChannel.CLOSE : DefaultRestChannel.KEEP_ALIVE;
    final RestRequest request = RestRequest.request(xContentRegistry(), new TestHttpRequest(httpVersion, null, "/") {

        @Override
        public HttpResponse createResponse(RestStatus status, BytesReference content) {
            throw new IllegalArgumentException("test");
        }
    }, httpChannel);
    request.getHttpRequest().getHeaders().put(DefaultRestChannel.CONNECTION, Collections.singletonList(httpConnectionHeaderValue));
    DefaultRestChannel channel = new DefaultRestChannel(httpChannel, request.getHttpRequest(), request, bigArrays, HttpHandlingSettings.fromSettings(Settings.EMPTY), threadPool.getThreadContext(), CorsHandler.fromSettings(Settings.EMPTY), null);
    // OpenSearchTestCase#after will invoke ensureAllArraysAreReleased which will fail if the response content was not released
    final BigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
    final ByteArray byteArray = bigArrays.newByteArray(0, false);
    final BytesReference content = new ReleasableBytesReference(BytesReference.fromByteArray(byteArray, 0), byteArray);
    expectThrows(IllegalArgumentException.class, () -> channel.sendResponse(new TestRestResponse(RestStatus.OK, content)));
    if (close) {
        verify(httpChannel, times(1)).close();
    } else {
        verify(httpChannel, times(0)).close();
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) MockBigArrays(org.opensearch.common.util.MockBigArrays) MockBigArrays(org.opensearch.common.util.MockBigArrays) BigArrays(org.opensearch.common.util.BigArrays) RestRequest(org.opensearch.rest.RestRequest) RestStatus(org.opensearch.rest.RestStatus) ByteArray(org.opensearch.common.util.ByteArray) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 29 with MockPageCacheRecycler

use of org.opensearch.common.util.MockPageCacheRecycler in project OpenSearch by opensearch-project.

the class DefaultRestChannelTests method testUnsupportedHttpMethod.

public void testUnsupportedHttpMethod() {
    final boolean close = randomBoolean();
    final HttpRequest.HttpVersion httpVersion = close ? HttpRequest.HttpVersion.HTTP_1_0 : HttpRequest.HttpVersion.HTTP_1_1;
    final String httpConnectionHeaderValue = close ? DefaultRestChannel.CLOSE : DefaultRestChannel.KEEP_ALIVE;
    final RestRequest request = RestRequest.request(xContentRegistry(), new TestHttpRequest(httpVersion, null, "/") {

        @Override
        public RestRequest.Method method() {
            throw new IllegalArgumentException("test");
        }
    }, httpChannel);
    request.getHttpRequest().getHeaders().put(DefaultRestChannel.CONNECTION, Collections.singletonList(httpConnectionHeaderValue));
    DefaultRestChannel channel = new DefaultRestChannel(httpChannel, request.getHttpRequest(), request, bigArrays, HttpHandlingSettings.fromSettings(Settings.EMPTY), threadPool.getThreadContext(), CorsHandler.fromSettings(Settings.EMPTY), null);
    // OpenSearchTestCase#after will invoke ensureAllArraysAreReleased which will fail if the response content was not released
    final BigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
    final ByteArray byteArray = bigArrays.newByteArray(0, false);
    final BytesReference content = new ReleasableBytesReference(BytesReference.fromByteArray(byteArray, 0), byteArray);
    channel.sendResponse(new TestRestResponse(RestStatus.METHOD_NOT_ALLOWED, content));
    Class<ActionListener<Void>> listenerClass = (Class<ActionListener<Void>>) (Class) ActionListener.class;
    ArgumentCaptor<ActionListener<Void>> listenerCaptor = ArgumentCaptor.forClass(listenerClass);
    verify(httpChannel).sendResponse(any(), listenerCaptor.capture());
    ActionListener<Void> listener = listenerCaptor.getValue();
    if (randomBoolean()) {
        listener.onResponse(null);
    } else {
        listener.onFailure(new ClosedChannelException());
    }
    if (close) {
        verify(httpChannel, times(1)).close();
    } else {
        verify(httpChannel, times(0)).close();
    }
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) MockBigArrays(org.opensearch.common.util.MockBigArrays) ByteArray(org.opensearch.common.util.ByteArray) BytesReference(org.opensearch.common.bytes.BytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ClosedChannelException(java.nio.channels.ClosedChannelException) MockBigArrays(org.opensearch.common.util.MockBigArrays) BigArrays(org.opensearch.common.util.BigArrays) RestRequest(org.opensearch.rest.RestRequest) ActionListener(org.opensearch.action.ActionListener) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 30 with MockPageCacheRecycler

use of org.opensearch.common.util.MockPageCacheRecycler in project OpenSearch by opensearch-project.

the class InternalVariableWidthHistogramTests method testOverlappingReduceResult.

public void testOverlappingReduceResult() {
    InternalVariableWidthHistogram dummy_histogram = createEmptyTestInstance();
    List<InternalVariableWidthHistogram.Bucket> buckets = new ArrayList<>();
    for (long value : new long[] { 1, 2, 4, 10 }) {
        InternalVariableWidthHistogram.Bucket.BucketBounds bounds = new InternalVariableWidthHistogram.Bucket.BucketBounds(value, value + 3);
        InternalVariableWidthHistogram.Bucket bucket = new InternalVariableWidthHistogram.Bucket(value, bounds, 4, format, InternalAggregations.EMPTY);
        buckets.add(bucket);
    }
    InternalVariableWidthHistogram histogram = dummy_histogram.create(buckets);
    MockBigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
    ScriptService mockScriptService = mockScriptService();
    MultiBucketConsumerService.MultiBucketConsumer bucketConsumer = new MultiBucketConsumerService.MultiBucketConsumer(DEFAULT_MAX_BUCKETS, new NoneCircuitBreakerService().getBreaker(CircuitBreaker.REQUEST));
    InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forFinalReduction(bigArrays, mockScriptService, bucketConsumer, PipelineAggregator.PipelineTree.EMPTY);
    ArrayList<InternalAggregation> aggs = new ArrayList<>();
    aggs.add(histogram);
    List<InternalVariableWidthHistogram.Bucket> reduced_buckets = ((InternalVariableWidthHistogram) histogram.reduce(aggs, context)).getBuckets();
    // Expected clusters: [ (1, 2), (4), 10) ]
    // Expected centroids: [ 1.5, 4, 10 ]
    // Expected cluster (min, max): [ (1, 5), (4, 7), (10, 13) ]
    // Expected keys: [ 1, 4.5, 10 ]
    // Expected doc counts: [8, 4, 4]
    double double_error = 1d / 10000d;
    assertEquals(1d, reduced_buckets.get(0).min(), double_error);
    assertEquals(1.5, (double) reduced_buckets.get(0).getKey(), double_error);
    assertEquals(8, reduced_buckets.get(0).getDocCount());
    assertEquals(4.5, reduced_buckets.get(1).min(), double_error);
    assertEquals(4d, (double) reduced_buckets.get(1).getKey(), double_error);
    assertEquals(4, reduced_buckets.get(1).getDocCount());
    assertEquals(10d, reduced_buckets.get(2).min(), double_error);
    assertEquals(10d, (double) reduced_buckets.get(2).getKey(), double_error);
    assertEquals(4, reduced_buckets.get(2).getDocCount());
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ArrayList(java.util.ArrayList) MockBigArrays(org.opensearch.common.util.MockBigArrays) ScriptService(org.opensearch.script.ScriptService) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) MultiBucketConsumerService(org.opensearch.search.aggregations.MultiBucketConsumerService) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Aggregations

MockPageCacheRecycler (org.opensearch.common.util.MockPageCacheRecycler)31 NoneCircuitBreakerService (org.opensearch.indices.breaker.NoneCircuitBreakerService)30 MockBigArrays (org.opensearch.common.util.MockBigArrays)25 NetworkService (org.opensearch.common.network.NetworkService)11 TestThreadPool (org.opensearch.threadpool.TestThreadPool)9 ArrayList (java.util.ArrayList)8 InternalAggregation (org.opensearch.search.aggregations.InternalAggregation)8 Before (org.junit.Before)7 ScriptService (org.opensearch.script.ScriptService)7 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)5 BigArrays (org.opensearch.common.util.BigArrays)5 MultiBucketConsumerService (org.opensearch.search.aggregations.MultiBucketConsumerService)5 IndexReader (org.apache.lucene.index.IndexReader)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 Directory (org.apache.lucene.store.Directory)4 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 QueryCachingPolicy (org.apache.lucene.search.QueryCachingPolicy)3 Mockito.anyString (org.mockito.Mockito.anyString)3 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)3 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)3