Search in sources :

Example 1 with Core

use of com.couchbase.client.core.Core in project couchbase-jdbc-driver by couchbaselabs.

the class AnalyticsProtocol method fetchResult.

@Override
public JsonParser fetchResult(QueryServiceResponse response, SubmitStatementOptions options) throws SQLException {
    int p = response.handle.lastIndexOf("/");
    if (p < 0) {
        throw new SQLNonTransientConnectionException("Protocol error - could not extract deferred ID");
    }
    String handlePath = response.handle.substring(p);
    Core core = connectionHandle.core();
    CoreContext ctx = core.context();
    AnalyticsRequest request = new AnalyticsRequest(getTimeout(options), ctx, ctx.environment().retryStrategy(), ctx.authenticator(), null, AnalyticsRequest.NO_PRIORITY, true, UUID.randomUUID().toString(), "", null, null, null, QUERY_RESULT_ENDPOINT_PATH + handlePath, HttpMethod.GET);
    core.send(request);
    try {
        AnalyticsResponse analyticsResponse = block(request.response());
        PipedOutputStream pos = new PipedOutputStream();
        InputStream is = new PipedInputStream(pos);
        rowExecutor.submit(() -> {
            try {
                final AtomicBoolean first = new AtomicBoolean(true);
                Stream<AnalyticsChunkRow> rows = analyticsResponse.rows().toStream();
                pos.write('[');
                rows.forEach(row -> {
                    try {
                        if (!first.compareAndSet(true, false)) {
                            pos.write(',');
                        }
                        pos.write(row.data());
                    } catch (IOException e) {
                        throw new RuntimeException("Failed to parse JSON row", e);
                    }
                });
                pos.write(']');
            } catch (Exception e) {
                throw new RuntimeException("Failure during streaming rows", e);
            } finally {
                try {
                    pos.close();
                } catch (IOException e) {
                // ignored.
                }
            }
        });
        return driverContext.getGenericObjectReader().getFactory().createParser(is);
    } catch (JsonProcessingException e) {
        throw getErrorReporter().errorInProtocol(e);
    } catch (IOException e) {
        throw getErrorReporter().errorInConnection(e);
    } catch (Exception e) {
        throw getErrorReporter().errorInConnection(e.getMessage());
    }
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) AnalyticsChunkRow(com.couchbase.client.core.msg.analytics.AnalyticsChunkRow) SQLException(java.sql.SQLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) InvalidDefinitionException(com.fasterxml.jackson.databind.exc.InvalidDefinitionException) SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AnalyticsResponse(com.couchbase.client.core.msg.analytics.AnalyticsResponse) AnalyticsRequest(com.couchbase.client.core.msg.analytics.AnalyticsRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Core(com.couchbase.client.core.Core)

Example 2 with Core

use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.

the class DefaultConfigurationProviderIntegrationTest method openBucketFromSecondValidSeed.

/**
 * Bucket config should also be loaded when the first seed in the list is not available.
 */
@Test
void openBucketFromSecondValidSeed() {
    TestNodeConfig cfg = config().firstNodeWith(Services.KV).get();
    Set<SeedNode> seeds = new HashSet<>(Arrays.asList(SeedNode.create("1.2.3.4"), SeedNode.create(cfg.hostname(), Optional.of(cfg.ports().get(Services.KV)), Optional.of(cfg.ports().get(Services.MANAGER)))));
    SimpleEventBus eventBus = new SimpleEventBus(true);
    environment = CoreEnvironment.builder().eventBus(eventBus).build();
    core = Core.create(environment, authenticator(), seeds);
    String bucketName = config().bucketname();
    ConfigurationProvider provider = new DefaultConfigurationProvider(core, seeds);
    openAndClose(bucketName, provider);
    provider.shutdown().block();
    waitUntilCondition(() -> eventBus.publishedEvents().stream().anyMatch(e -> e instanceof EndpointConnectionFailedEvent));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Arrays(java.util.Arrays) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) SeedNode(com.couchbase.client.core.env.SeedNode) ClusterType(com.couchbase.client.test.ClusterType) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ConfigException(com.couchbase.client.core.error.ConfigException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Set(java.util.Set) Event(com.couchbase.client.core.cnc.Event) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) BucketNotFoundDuringLoadException(com.couchbase.client.core.error.BucketNotFoundDuringLoadException) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) Services(com.couchbase.client.test.Services) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) BucketOpenRetriedEvent(com.couchbase.client.core.cnc.events.config.BucketOpenRetriedEvent) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AlreadyShutdownException(com.couchbase.client.core.error.AlreadyShutdownException) Optional(java.util.Optional) Core(com.couchbase.client.core.Core) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) Collections(java.util.Collections) SeedNode(com.couchbase.client.core.env.SeedNode) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) HashSet(java.util.HashSet) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with Core

use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.

the class GlobalLoaderIntegrationTest method loadGlobalConfigViaCarrierPublication.

/**
 * This is a very simplistic test that makes sure that we can "round trip" in the
 * {@link GlobalLoader} by grabbing a JSON decodable config through the full stack.
 */
@Test
void loadGlobalConfigViaCarrierPublication() {
    TestNodeConfig config = config().firstNodeWith(Services.KV).get();
    Core core = Core.create(env, authenticator(), seedNodes());
    GlobalLoader loader = new GlobalLoader(core);
    ProposedGlobalConfigContext globalConfigContext = loader.load(new NodeIdentifier(config.hostname(), config.ports().get(Services.MANAGER)), config.ports().get(Services.KV)).block();
    assertNotNull(globalConfigContext);
    assertNotNull(globalConfigContext.config());
    core.shutdown().block();
}
Also used : TestNodeConfig(com.couchbase.client.test.TestNodeConfig) ProposedGlobalConfigContext(com.couchbase.client.core.config.ProposedGlobalConfigContext) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) Core(com.couchbase.client.core.Core) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with Core

use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketLoaderIntegrationTest method loadConfigViaCarrierPublication.

/**
 * This is a very simplistic test that makes sure that we can "round trip" in the
 * {@link KeyValueBucketLoader} by grabbing a JSON decodable config through the full stack.
 */
@Test
void loadConfigViaCarrierPublication() {
    TestNodeConfig config = config().firstNodeWith(Services.KV).get();
    Core core = Core.create(env, authenticator(), seedNodes());
    KeyValueBucketLoader loader = new KeyValueBucketLoader(core);
    ProposedBucketConfigContext loaded = loader.load(new NodeIdentifier(config.hostname(), config.ports().get(Services.MANAGER)), config.ports().get(Services.KV), config().bucketname(), Optional.empty()).block();
    assertNotNull(loaded);
    assertEquals(config().bucketname(), loaded.bucketName());
    core.shutdown().block();
}
Also used : TestNodeConfig(com.couchbase.client.test.TestNodeConfig) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) Core(com.couchbase.client.core.Core) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with Core

use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.

the class CoreAnalyticsLinkManager method sendLink.

private CompletableFuture<Void> sendLink(HttpMethod method, Map<String, String> link, CoreCommonOptions options, String tracingId) {
    // ensure mutability, and don't modify caller's map
    link = new HashMap<>(link);
    CoreHttpPath path = getLinkPathAndAdjustMap(link);
    UrlQueryStringBuilder form = newForm();
    link.forEach(form::set);
    return httpClient.newRequest(method, path, options).trace(tracingId).form(form).exec(core).exceptionally(t -> {
        throw translateCompilationFailureToInvalidArgument(t);
    }).thenApply(result -> null);
}
Also used : Builder.newForm(com.couchbase.client.core.endpoint.http.CoreHttpRequest.Builder.newForm) AnalyticsErrorContext(com.couchbase.client.core.error.context.AnalyticsErrorContext) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod) UrlQueryStringBuilder(com.couchbase.client.core.util.UrlQueryStringBuilder) CoreHttpClient(com.couchbase.client.core.endpoint.http.CoreHttpClient) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) RequestTarget(com.couchbase.client.core.msg.RequestTarget) CompilationFailureException(com.couchbase.client.core.error.CompilationFailureException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) CoreHttpPath(com.couchbase.client.core.endpoint.http.CoreHttpPath) CoreCommonOptions(com.couchbase.client.core.endpoint.http.CoreCommonOptions) CbCollections.mapOf(com.couchbase.client.core.util.CbCollections.mapOf) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) CoreHttpResponse(com.couchbase.client.core.endpoint.http.CoreHttpResponse) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) Builder.newQueryString(com.couchbase.client.core.endpoint.http.CoreHttpRequest.Builder.newQueryString) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) CbThrowables(com.couchbase.client.core.util.CbThrowables) Core(com.couchbase.client.core.Core) CoreHttpPath(com.couchbase.client.core.endpoint.http.CoreHttpPath) UrlQueryStringBuilder(com.couchbase.client.core.util.UrlQueryStringBuilder)

Aggregations

Core (com.couchbase.client.core.Core)49 CoreContext (com.couchbase.client.core.CoreContext)31 Test (org.junit.jupiter.api.Test)25 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)17 Duration (java.time.Duration)13 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Stability (com.couchbase.client.core.annotation.Stability)11 Optional (java.util.Optional)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 BucketConfig (com.couchbase.client.core.config.BucketConfig)10 Authenticator (com.couchbase.client.core.env.Authenticator)10 List (java.util.List)10 Flux (reactor.core.publisher.Flux)10 Mono (reactor.core.publisher.Mono)10 Reactor (com.couchbase.client.core.Reactor)9 SeedNode (com.couchbase.client.core.env.SeedNode)9 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)9 HashSet (java.util.HashSet)9