Search in sources :

Example 6 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker in project crate by crate.

the class SqlHttpHandlerTest method testSessionSettingsArePreservedAcrossRequests.

@Test
public void testSessionSettingsArePreservedAcrossRequests() {
    User dummyUser = User.of("dummy");
    var sessionConext = new SessionContext(dummyUser);
    var mockedSession = mock(Session.class);
    when(mockedSession.sessionContext()).thenReturn(sessionConext);
    var mockedSqlOperations = mock(SQLOperations.class);
    when(mockedSqlOperations.createSession(null, dummyUser)).thenReturn(mockedSession);
    var mockedRequest = mock(FullHttpRequest.class);
    when(mockedRequest.headers()).thenReturn(new DefaultHttpHeaders());
    SqlHttpHandler handler = new SqlHttpHandler(Settings.EMPTY, mockedSqlOperations, (s) -> new NoopCircuitBreaker("dummy"), userName -> dummyUser, sessionContext -> AccessControl.DISABLED, Netty4CorsConfigBuilder.forAnyOrigin().build());
    // 1st call to ensureSession creates a session instance bound to 'dummyUser'
    var session = handler.ensureSession(mockedRequest);
    verify(mockedRequest, atLeast(1)).headers();
    assertThat(session.sessionContext().authenticatedUser(), is(dummyUser));
    assertThat(session.sessionContext().searchPath().currentSchema(), containsString("doc"));
    assertTrue(session.sessionContext().isHashJoinEnabled());
    // modify the session settings
    session.sessionContext().setSearchPath("dummy_path");
    session.sessionContext().setHashJoinEnabled(false);
    // test that the 2nd call to ensureSession will retrieve the session settings modified previously
    session = handler.ensureSession(mockedRequest);
    assertFalse(session.sessionContext().isHashJoinEnabled());
    assertThat(session.sessionContext().searchPath().currentSchema(), containsString("dummy_path"));
}
Also used : User(io.crate.user.User) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) SessionContext(io.crate.action.sql.SessionContext) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) Test(org.junit.Test)

Example 7 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker in project crate by crate.

the class InboundPipelineTests method testDecodeExceptionIsPropagated.

public void testDecodeExceptionIsPropagated() throws IOException {
    BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
    };
    final StatsTracker statsTracker = new StatsTracker();
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
    final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
    final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
    try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
        String actionName = "actionName";
        final Version invalidVersion = Version.V_3_2_0;
        final String value = randomAlphaOfLength(1000);
        final boolean isRequest = randomBoolean();
        final long requestId = randomNonNegativeLong();
        OutboundMessage message;
        if (isRequest) {
            message = new OutboundMessage.Request(new TestRequest(value), invalidVersion, actionName, requestId, false, false);
        } else {
            message = new OutboundMessage.Response(new TestResponse(value), invalidVersion, requestId, false, false);
        }
        final BytesReference reference = message.serialize(streamOutput);
        try (ReleasableBytesReference releasable = ReleasableBytesReference.wrap(reference)) {
            expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), releasable));
        }
        // Pipeline cannot be reused after uncaught exception
        final IllegalStateException ise = expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), ReleasableBytesReference.wrap(BytesArray.EMPTY)));
        assertEquals("Pipeline state corrupted by uncaught exception", ise.getMessage());
    }
}
Also used : Tuple(io.crate.common.collections.Tuple) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) BiConsumer(java.util.function.BiConsumer) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) ESTestCase(org.elasticsearch.test.ESTestCase) Streams(io.crate.common.io.Streams) Releasable(org.elasticsearch.common.lease.Releasable) Predicate(java.util.function.Predicate) PageCacheRecycler(org.elasticsearch.common.util.PageCacheRecycler) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) TestCircuitBreaker(org.elasticsearch.common.breaker.TestCircuitBreaker) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Version(org.elasticsearch.Version) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) TimeValue(io.crate.common.unit.TimeValue) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) TestCircuitBreaker(org.elasticsearch.common.breaker.TestCircuitBreaker) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Version(org.elasticsearch.Version) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) BytesReference(org.elasticsearch.common.bytes.BytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) LongSupplier(java.util.function.LongSupplier)

Example 8 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker in project crate by crate.

the class SqlHttpHandlerTest method testDefaultUserIfHttpHeaderNotPresent.

@Test
public void testDefaultUserIfHttpHeaderNotPresent() {
    SqlHttpHandler handler = new SqlHttpHandler(Settings.EMPTY, mock(SQLOperations.class), (s) -> new NoopCircuitBreaker("dummy"), userName -> {
        if (userName.equals("crate")) {
            return User.CRATE_USER;
        }
        return null;
    }, sessionContext -> AccessControl.DISABLED, Netty4CorsConfigBuilder.forAnyOrigin().build());
    User user = handler.userFromAuthHeader(null);
    assertThat(user, is(User.CRATE_USER));
}
Also used : User(io.crate.user.User) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) SQLOperations(io.crate.action.sql.SQLOperations) Test(org.junit.Test)

Example 9 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker in project crate by crate.

the class SqlHttpHandlerTest method testSettingUserIfHttpHeaderNotPresent.

@Test
public void testSettingUserIfHttpHeaderNotPresent() {
    Settings settings = Settings.builder().put(AuthSettings.AUTH_TRUST_HTTP_DEFAULT_HEADER.getKey(), "trillian").build();
    SqlHttpHandler handler = new SqlHttpHandler(settings, mock(SQLOperations.class), (s) -> new NoopCircuitBreaker("dummy"), User::of, sessionContext -> AccessControl.DISABLED, Netty4CorsConfigBuilder.forAnyOrigin().build());
    User user = handler.userFromAuthHeader(null);
    assertThat(user.name(), is("trillian"));
}
Also used : User(io.crate.user.User) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) SQLOperations(io.crate.action.sql.SQLOperations) AuthSettings(io.crate.auth.AuthSettings) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Example 10 with NoopCircuitBreaker

use of org.elasticsearch.common.breaker.NoopCircuitBreaker in project crate by crate.

the class OutboundHandlerTests method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    channel = new FakeTcpChannel(randomBoolean(), buildNewFakeTransportAddress().address(), buildNewFakeTransportAddress().address());
    TransportAddress transportAddress = buildNewFakeTransportAddress();
    node = new DiscoveryNode("", transportAddress, Version.CURRENT);
    StatsTracker statsTracker = new StatsTracker();
    handler = new OutboundHandler("node", Version.CURRENT, statsTracker, threadPool, BigArrays.NON_RECYCLING_INSTANCE);
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
    final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
    pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, (c, m) -> {
        try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
            Streams.copy(m.openOrGetStreamInput(), streamOutput);
            message.set(new Tuple<>(m.getHeader(), streamOutput.bytes()));
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    });
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) Tuple(io.crate.common.collections.Tuple) LongSupplier(java.util.function.LongSupplier) BigArrays(org.elasticsearch.common.util.BigArrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) BytesArray(org.elasticsearch.common.bytes.BytesArray) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) After(org.junit.After) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) ThreadPool(org.elasticsearch.threadpool.ThreadPool) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) ESTestCase(org.elasticsearch.test.ESTestCase) Streams(io.crate.common.io.Streams) Before(org.junit.Before) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Predicate(java.util.function.Predicate) PageCacheRecycler(org.elasticsearch.common.util.PageCacheRecycler) Test(org.junit.Test) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Version(org.elasticsearch.Version) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TimeValue(io.crate.common.unit.TimeValue) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) ActionListener(org.elasticsearch.action.ActionListener) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) TransportAddress(org.elasticsearch.common.transport.TransportAddress) IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) LongSupplier(java.util.function.LongSupplier) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) Tuple(io.crate.common.collections.Tuple) Before(org.junit.Before)

Aggregations

NoopCircuitBreaker (org.elasticsearch.common.breaker.NoopCircuitBreaker)14 Test (org.junit.Test)10 CircuitBreaker (org.elasticsearch.common.breaker.CircuitBreaker)5 User (io.crate.user.User)4 SQLOperations (io.crate.action.sql.SQLOperations)3 Tuple (io.crate.common.collections.Tuple)3 Streams (io.crate.common.io.Streams)3 TimeValue (io.crate.common.unit.TimeValue)3 TestingRowConsumer (io.crate.testing.TestingRowConsumer)3 IOException (java.io.IOException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 LongSupplier (java.util.function.LongSupplier)3 Predicate (java.util.function.Predicate)3 Supplier (java.util.function.Supplier)3 Version (org.elasticsearch.Version)3 BytesArray (org.elasticsearch.common.bytes.BytesArray)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)3 Row (io.crate.data.Row)2 RowN (io.crate.data.RowN)2