Search in sources :

Example 21 with SQLOperations

use of io.crate.action.sql.SQLOperations in project crate by crate.

the class TableStatsServiceTest method testStatsQueriesCorrectly.

@Test
public void testStatsQueriesCorrectly() {
    SQLOperations sqlOperations = Mockito.mock(SQLOperations.class);
    Session session = Mockito.mock(Session.class);
    Mockito.when(sqlOperations.newSystemSession()).thenReturn(session);
    TableStatsService statsService = new TableStatsService(Settings.EMPTY, THREAD_POOL, clusterService, sqlOperations);
    statsService.run();
    Mockito.verify(session, Mockito.times(1)).quickExec(ArgumentMatchers.eq(TableStatsService.STMT), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any());
}
Also used : SQLOperations(io.crate.action.sql.SQLOperations) Session(io.crate.action.sql.Session) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 22 with SQLOperations

use of io.crate.action.sql.SQLOperations in project crate by crate.

the class TableStatsServiceTest method testNoUpdateIfLocalNodeNotAvailable.

@Test
public void testNoUpdateIfLocalNodeNotAvailable() {
    final ClusterService clusterService = Mockito.mock(ClusterService.class);
    Mockito.when(clusterService.localNode()).thenReturn(null);
    Mockito.when(clusterService.getClusterSettings()).thenReturn(this.clusterService.getClusterSettings());
    SQLOperations sqlOperations = Mockito.mock(SQLOperations.class);
    Session session = Mockito.mock(Session.class);
    Mockito.when(sqlOperations.createSession(ArgumentMatchers.anyString(), ArgumentMatchers.any())).thenReturn(session);
    TableStatsService statsService = new TableStatsService(Settings.EMPTY, THREAD_POOL, clusterService, sqlOperations);
    statsService.run();
    Mockito.verify(session, Mockito.times(0)).sync();
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) SQLOperations(io.crate.action.sql.SQLOperations) Session(io.crate.action.sql.Session) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 23 with SQLOperations

use of io.crate.action.sql.SQLOperations in project crate by crate.

the class PostgresWireProtocolTest method testCrateServerVersionIsReceivedOnStartup.

@Test
public void testCrateServerVersionIsReceivedOnStartup() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    ByteBuf buf = Unpooled.buffer();
    ClientMessages.sendStartupMessage(buf, "doc");
    channel.writeInbound(buf);
    channel.releaseInbound();
    ByteBuf respBuf;
    respBuf = channel.readOutbound();
    try {
        // Auth OK
        assertThat((char) respBuf.readByte(), is('R'));
    } finally {
        respBuf.release();
    }
    respBuf = channel.readOutbound();
    try {
        // ParameterStatus
        assertThat((char) respBuf.readByte(), is('S'));
        // length
        respBuf.readInt();
        String key = PostgresWireProtocol.readCString(respBuf);
        String value = PostgresWireProtocol.readCString(respBuf);
        assertThat(key, is("crate_version"));
        assertThat(value, is(Version.CURRENT.externalNumber()));
    } finally {
        respBuf.release();
    }
}
Also used : Arrays(java.util.Arrays) SessionContext(io.crate.action.sql.SessionContext) PGTypes(io.crate.protocols.postgres.types.PGTypes) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Provider(org.elasticsearch.common.inject.Provider) Unpooled(io.netty.buffer.Unpooled) DependencyCarrier(io.crate.planner.DependencyCarrier) Settings(org.elasticsearch.common.settings.Settings) After(org.junit.After) Map(java.util.Map) SecureString(org.elasticsearch.common.settings.SecureString) SQLOperations(io.crate.action.sql.SQLOperations) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) User(io.crate.user.User) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Session(io.crate.action.sql.Session) Version(org.elasticsearch.Version) DataTypes(io.crate.types.DataTypes) UserManager(io.crate.user.UserManager) Matchers.is(org.hamcrest.Matchers.is) DescribeResult(io.crate.action.sql.DescribeResult) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) AccessControl(io.crate.auth.AccessControl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Authentication(io.crate.auth.Authentication) SQLExecutor(io.crate.testing.SQLExecutor) StubUserManager(io.crate.user.StubUserManager) Nullable(javax.annotation.Nullable) Before(org.junit.Before) ArgumentMatchers.anyChar(org.mockito.ArgumentMatchers.anyChar) JobKilledException(io.crate.exceptions.JobKilledException) AuthenticationMethod(io.crate.auth.AuthenticationMethod) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) ChannelHandler(io.netty.channel.ChannelHandler) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SecureString(org.elasticsearch.common.settings.SecureString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 24 with SQLOperations

use of io.crate.action.sql.SQLOperations in project crate by crate.

the class JobLogIntegrationTest method testJobLogWithEnabledAndDisabledStats.

@Test
// SET extra_float_digits = 3 gets added to the jobs_log
@UseJdbc(0)
public void testJobLogWithEnabledAndDisabledStats() throws Exception {
    String setStmt = "set global transient stats.jobs_log_size=1";
    execute(setStmt);
    // We record the statements in the log **after** we notify the result receivers (see {@link JobsLogsUpdateListener usage).
    // So it might happen that the "set global ..." statement execution is returned to this test but the recording
    // in the log is done AFTER the execution of the below "select name from sys.cluster" statement (because async
    // programming is evil like that). And then this test will fail and people will spend days and days to figure
    // out what's going on.
    // So let's just wait for the "set global ... " statement to be recorded here and then move on with our test.
    assertBusy(() -> {
        boolean setStmtFound = false;
        for (JobsLogService jobsLogService : internalCluster().getDataNodeInstances(JobsLogService.class)) {
            // each node must have received the new jobs_log_size setting change instruction
            assertThat(jobsLogService.jobsLogSize(), is(1));
            JobsLogs jobsLogs = jobsLogService.get();
            Iterator<JobContextLog> iterator = jobsLogs.jobsLog().iterator();
            if (iterator.hasNext()) {
                if (iterator.next().statement().equalsIgnoreCase(setStmt)) {
                    setStmtFound = true;
                }
            }
        }
        // at least one node must have the set statement logged
        assertThat(setStmtFound, is(true));
    });
    // only the latest queries are found in the log.
    for (SQLOperations sqlOperations : internalCluster().getDataNodeInstances(SQLOperations.class)) {
        Session session = sqlOperations.newSystemSession();
        execute("select name from sys.cluster", null, session);
    }
    assertJobLogOnNodesHaveOnlyStatement("select name from sys.cluster");
    for (SQLOperations sqlOperations : internalCluster().getDataNodeInstances(SQLOperations.class)) {
        Session session = sqlOperations.newSystemSession();
        execute("select id from sys.cluster", null, session);
    }
    assertJobLogOnNodesHaveOnlyStatement("select id from sys.cluster");
    execute("set global transient stats.enabled = false");
    for (JobsLogService jobsLogService : internalCluster().getDataNodeInstances(JobsLogService.class)) {
        assertBusy(() -> assertThat(jobsLogService.isEnabled(), is(false)));
    }
    execute("select * from sys.jobs_log");
    assertThat(response.rowCount(), is(0L));
}
Also used : JobContextLog(io.crate.expression.reference.sys.job.JobContextLog) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) SQLOperations(io.crate.action.sql.SQLOperations) JobsLogService(io.crate.execution.engine.collect.stats.JobsLogService) Session(io.crate.action.sql.Session) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test)

Example 25 with SQLOperations

use of io.crate.action.sql.SQLOperations in project crate by crate.

the class GatewayIndexStateIT method testIndexDeletionWhenNodeRejoins.

/**
 * This test ensures that when an index deletion takes place while a node is offline, when that
 * node rejoins the cluster, it deletes the index locally instead of importing it as a dangling index.
 */
@Test
public void testIndexDeletionWhenNodeRejoins() throws Exception {
    final int numNodes = 2;
    final List<String> nodes;
    logger.info("--> starting a cluster with " + numNodes + " nodes");
    nodes = internalCluster().startNodes(numNodes, Settings.builder().put(IndexGraveyard.SETTING_MAX_TOMBSTONES.getKey(), randomIntBetween(10, 100)).build());
    logger.info("--> create an index");
    // createIndex(indexName);
    execute("create table my_schema.test (id int) with (number_of_replicas = 0)");
    var tableName = "my_schema.test";
    logger.info("--> waiting for green status");
    ensureGreen();
    final String indexUUID = resolveIndex(tableName).getUUID();
    logger.info("--> restart a random date node, deleting the index in between stopping and restarting");
    internalCluster().restartRandomDataNode(new RestartCallback() {

        @Override
        public Settings onNodeStopped(final String nodeName) throws Exception {
            nodes.remove(nodeName);
            logger.info("--> stopped node[{}], remaining nodes {}", nodeName, nodes);
            assert nodes.size() > 0;
            final String otherNode = nodes.get(0);
            logger.info("--> delete index and verify it is deleted");
            var clientProvider = new SQLTransportExecutor.ClientProvider() {

                @Override
                public Client client() {
                    return ESIntegTestCase.client(otherNode);
                }

                @Override
                public String pgUrl() {
                    PostgresNetty postgresNetty = internalCluster().getInstance(PostgresNetty.class, otherNode);
                    BoundTransportAddress boundTransportAddress = postgresNetty.boundAddress();
                    if (boundTransportAddress != null) {
                        InetSocketAddress address = boundTransportAddress.publishAddress().address();
                        return String.format(Locale.ENGLISH, "jdbc:postgresql://%s:%d/?ssl=%s&sslmode=%s", address.getHostName(), address.getPort(), false, "disable");
                    }
                    return null;
                }

                @Override
                public SQLOperations sqlOperations() {
                    return internalCluster().getInstance(SQLOperations.class, otherNode);
                }
            };
            var sqlExecutor = new SQLTransportExecutor(clientProvider);
            // client.admin().indices().prepareDelete(indexName).execute().actionGet();
            sqlExecutor.exec("drop table my_schema.test");
            assertThat(response.rowCount(), is(1L));
            // assertFalse(indexExists(indexName, client));
            try {
                sqlExecutor.exec("select * from my_schema.test");
                fail("expecting index to be deleted");
            } catch (Exception e) {
            // pass
            }
            logger.info("--> index deleted");
            return super.onNodeStopped(nodeName);
        }
    });
    logger.info("--> wait until all nodes are back online");
    client().admin().cluster().health(Requests.clusterHealthRequest().waitForEvents(Priority.LANGUID).waitForNodes(Integer.toString(numNodes))).actionGet(REQUEST_TIMEOUT);
    logger.info("--> waiting for green status");
    ensureGreen();
    logger.info("--> verify that the deleted index is removed from the cluster and not reimported as dangling by the restarted node");
    // assertFalse(indexExists(indexName));
    try {
        execute("select * from my_schema.test");
        fail("expecting index to be deleted");
    } catch (Exception e) {
    // pass
    }
    assertBusy(() -> {
        final NodeEnvironment nodeEnv = internalCluster().getInstance(NodeEnvironment.class);
        try {
            assertFalse("index folder " + indexUUID + " should be deleted", nodeEnv.availableIndexFolders().contains(indexUUID));
        } catch (IOException e) {
            logger.error("Unable to retrieve available index folders from the node", e);
            fail("Unable to retrieve available index folders from the node");
        }
    });
}
Also used : NodeEnvironment(org.elasticsearch.env.NodeEnvironment) InetSocketAddress(java.net.InetSocketAddress) SQLTransportExecutor(io.crate.testing.SQLTransportExecutor) IOException(java.io.IOException) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback) IOException(java.io.IOException) PostgresNetty(io.crate.protocols.postgres.PostgresNetty) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Client(org.elasticsearch.client.Client) SQLOperations(io.crate.action.sql.SQLOperations) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Aggregations

SQLOperations (io.crate.action.sql.SQLOperations)26 Test (org.junit.Test)16 Session (io.crate.action.sql.Session)15 Before (org.junit.Before)14 JobsLogs (io.crate.execution.engine.collect.stats.JobsLogs)11 User (io.crate.user.User)11 Settings (org.elasticsearch.common.settings.Settings)11 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)10 SessionContext (io.crate.action.sql.SessionContext)9 DependencyCarrier (io.crate.planner.DependencyCarrier)9 List (java.util.List)9 Map (java.util.Map)9 TimeUnit (java.util.concurrent.TimeUnit)9 Nullable (javax.annotation.Nullable)9 DescribeResult (io.crate.action.sql.DescribeResult)8 AccessControl (io.crate.auth.AccessControl)8 AlwaysOKAuthentication (io.crate.auth.AlwaysOKAuthentication)8 Authentication (io.crate.auth.Authentication)8 AuthenticationMethod (io.crate.auth.AuthenticationMethod)8 SQLExecutor (io.crate.testing.SQLExecutor)8