Search in sources :

Example 6 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class RoundRobinLocatorTest method selectNextNode.

@Test
void selectNextNode() {
    Locator locator = new RoundRobinLocator(ServiceType.QUERY, 0);
    QueryRequest request = mock(QueryRequest.class);
    ClusterConfig configMock = mock(ClusterConfig.class);
    when(configMock.hasClusterOrBucketConfig()).thenReturn(true);
    Node node1Mock = mock(Node.class);
    when(node1Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    Node node2Mock = mock(Node.class);
    when(node2Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock));
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, times(1)).send(request);
    verify(node2Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, times(1)).send(request);
    verify(node2Mock, times(1)).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, times(2)).send(request);
    verify(node2Mock, times(1)).send(request);
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) ArrayList(java.util.ArrayList) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Example 7 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class AsyncScope method queryRequest.

/**
 * Helper method to construct the query request. ( copied from Cluster )
 *
 * @param statement the statement of the query.
 * @param options the options.
 * @return the constructed query request.
 */
static QueryRequest queryRequest(final String bucketName, final String scopeName, final String statement, final QueryOptions.Built options, final Core core, final ClusterEnvironment environment) {
    notNullOrEmpty(statement, "Statement", () -> new ReducedQueryErrorContext(statement));
    Duration timeout = options.timeout().orElse(environment.timeoutConfig().queryTimeout());
    RetryStrategy retryStrategy = options.retryStrategy().orElse(environment.retryStrategy());
    final JsonObject query = JsonObject.create();
    query.put("statement", statement);
    query.put("timeout", encodeDurationToMs(timeout));
    String queryContext = QueryRequest.queryContext(bucketName, scopeName);
    query.put("query_context", queryContext);
    options.injectParams(query);
    final byte[] queryBytes = query.toString().getBytes(StandardCharsets.UTF_8);
    final String clientContextId = query.getString("client_context_id");
    final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_QUERY, options.parentSpan().orElse(null));
    QueryRequest request = new QueryRequest(timeout, core.context(), retryStrategy, core.context().authenticator(), statement, queryBytes, options.readonly(), clientContextId, span, bucketName, scopeName, null);
    request.context().clientContext(options.clientContext());
    return request;
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) JsonObject(com.couchbase.client.java.json.JsonObject) Duration(java.time.Duration) ReducedQueryErrorContext(com.couchbase.client.core.error.context.ReducedQueryErrorContext) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 8 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class QueryMessageHandlerBackpressureTest method requestRecordsExplicitly.

/**
 * This test makes sure that even if the server returns a good bunch of data, each individual
 * chunk is requested by the caller explicitly.
 */
@Test
void requestRecordsExplicitly() throws Exception {
    EndpointContext endpointContext = new EndpointContext(core.context(), new HostAndPort("127.0.0.1", 1234), NoopCircuitBreaker.INSTANCE, ServiceType.QUERY, Optional.empty(), Optional.empty(), Optional.empty());
    BaseEndpoint endpoint = mock(BaseEndpoint.class);
    when(endpoint.pipelined()).thenReturn(false);
    Bootstrap client = new Bootstrap().channel(LocalChannel.class).group(new DefaultEventLoopGroup()).remoteAddress(new LocalAddress("s1")).handler(new ChannelInitializer<LocalChannel>() {

        @Override
        protected void initChannel(LocalChannel ch) {
            ch.pipeline().addLast(new HttpClientCodec()).addLast(new QueryMessageHandler(endpoint, endpointContext));
        }
    });
    Channel channel = client.connect().awaitUninterruptibly().channel();
    final List<byte[]> rows = Collections.synchronizedList(new ArrayList<>());
    QueryRequest request = new QueryRequest(Duration.ofSeconds(1), endpointContext, BestEffortRetryStrategy.INSTANCE, endpointContext.authenticator(), "select 1=1", "myquery".getBytes(UTF_8), true, null, null, null, null, null);
    channel.writeAndFlush(request);
    final QueryResponse response = request.response().get();
    assertEquals(0, rows.size());
    StepVerifier.create(response.rows().map(v -> new String(v.data(), UTF_8)), 0).thenRequest(1).expectNext("{\"foo\":1}").thenRequest(1).expectNext("{\"bar\":1}").thenRequest(2).expectNext("{\"faz\":1}", "{\"baz\":1}").thenRequest(4).expectNext("{\"fazz\":1}", "{\"bazz\":1}", "{\"fizz\":1}", "{\"bizz\":1}").expectComplete().verify();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) HttpHeaderNames(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpHeaderNames) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) ServerBootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap) AfterAll(org.junit.jupiter.api.AfterAll) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) ChannelInboundHandlerAdapter(com.couchbase.client.core.deps.io.netty.channel.ChannelInboundHandlerAdapter) BeforeAll(org.junit.jupiter.api.BeforeAll) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) Unpooled(com.couchbase.client.core.deps.io.netty.buffer.Unpooled) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) LocalServerChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) DefaultEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test) List(java.util.List) Optional(java.util.Optional) HostAndPort(com.couchbase.client.core.util.HostAndPort) Mockito.mock(org.mockito.Mockito.mock) QueryResponse(com.couchbase.client.core.msg.query.QueryResponse) HttpVersion(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpVersion) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) SeedNode(com.couchbase.client.core.env.SeedNode) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) HttpServerCodec(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpServerCodec) ArrayList(java.util.ArrayList) LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) LocalChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalChannel) NoopCircuitBreaker(com.couchbase.client.core.endpoint.NoopCircuitBreaker) HttpResponseStatus(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ChannelInitializer(com.couchbase.client.core.deps.io.netty.channel.ChannelInitializer) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Mockito.when(org.mockito.Mockito.when) EventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.EventLoopGroup) TimeUnit(java.util.concurrent.TimeUnit) HttpClientCodec(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpClientCodec) AfterEach(org.junit.jupiter.api.AfterEach) Core(com.couchbase.client.core.Core) Collections(java.util.Collections) Bootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap) LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) LocalChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalChannel) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) LocalServerChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel) LocalChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalChannel) HttpClientCodec(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpClientCodec) DefaultEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup) HostAndPort(com.couchbase.client.core.util.HostAndPort) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) QueryResponse(com.couchbase.client.core.msg.query.QueryResponse) ServerBootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap) Bootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap) Test(org.junit.jupiter.api.Test)

Example 9 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class QueryMessageHandlerTest method handlesConcurrentInFlightRequest.

@Test
void handlesConcurrentInFlightRequest() {
    BaseEndpoint endpoint = mock(BaseEndpoint.class);
    EmbeddedChannel channel = new EmbeddedChannel(new QueryMessageHandler(endpoint, ENDPOINT_CTX));
    byte[] query = "doesn'tmatter".getBytes(CharsetUtil.UTF_8);
    QueryRequest request1 = new QueryRequest(ENV.timeoutConfig().queryTimeout(), CORE_CTX, FailFastRetryStrategy.INSTANCE, CORE_CTX.authenticator(), "statement", query, true, null, null, null, null, null);
    QueryRequest request2 = new QueryRequest(ENV.timeoutConfig().queryTimeout(), CORE_CTX, FailFastRetryStrategy.INSTANCE, CORE_CTX.authenticator(), "statement", query, true, null, null, null, null, null);
    channel.writeAndFlush(request1);
    channel.writeAndFlush(request2);
    waitUntilCondition(() -> request2.context().retryAttempts() > 0);
    verify(endpoint, times(1)).decrementOutstandingRequests();
    channel.finishAndReleaseAll();
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Example 10 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class RoundRobinLocatorTest method shouldDistributeFairlyUnderMDS.

@Test
void shouldDistributeFairlyUnderMDS() {
    Locator locator = new RoundRobinLocator(ServiceType.QUERY, 0);
    QueryRequest request = mock(QueryRequest.class);
    ClusterConfig configMock = mock(ClusterConfig.class);
    when(configMock.hasClusterOrBucketConfig()).thenReturn(true);
    Node node1Mock = mock(Node.class);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    when(node1Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(false);
    Node node2Mock = mock(Node.class);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    when(node2Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(false);
    Node node3Mock = mock(Node.class);
    when(node3Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.103", 8091));
    when(node3Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    Node node4Mock = mock(Node.class);
    when(node4Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.104", 8091));
    when(node4Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock, node3Mock, node4Mock));
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, never()).send(request);
    verify(node3Mock, times(1)).send(request);
    verify(node4Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, never()).send(request);
    verify(node3Mock, times(1)).send(request);
    verify(node4Mock, times(1)).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, never()).send(request);
    verify(node3Mock, times(2)).send(request);
    verify(node4Mock, times(1)).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, never()).send(request);
    verify(node3Mock, times(2)).send(request);
    verify(node4Mock, times(2)).send(request);
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) ArrayList(java.util.ArrayList) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Aggregations

QueryRequest (com.couchbase.client.core.msg.query.QueryRequest)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)4 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)3 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)3 Duration (java.time.Duration)3 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)2 DefaultHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent)2 DefaultHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse)2 DefaultLastHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent)2 HttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse)2 ReducedQueryErrorContext (com.couchbase.client.core.error.context.ReducedQueryErrorContext)2 QueryResponse (com.couchbase.client.core.msg.query.QueryResponse)2 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)2 Service (com.couchbase.client.core.service.Service)2 ServiceType (com.couchbase.client.core.service.ServiceType)2 JsonObject (com.couchbase.client.java.json.JsonObject)2 Optional (java.util.Optional)2 Core (com.couchbase.client.core.Core)1