Search in sources :

Example 6 with SchemaOperationException

use of org.apache.ignite.internal.processors.query.schema.SchemaOperationException in project ignite by apache.

the class DynamicIndexAbstractConcurrentSelfTest method testConcurrentOperationsAndCacheStartStopMultithreaded.

/**
     * Multithreaded cache start/stop along with index operations. Nothing should hang.
     *
     * @throws Exception If failed.
     */
public void testConcurrentOperationsAndCacheStartStopMultithreaded() throws Exception {
    // Start complex topology.
    Ignition.start(serverConfiguration(1));
    Ignition.start(serverConfiguration(2));
    Ignition.start(serverConfiguration(3, true));
    Ignite cli = Ignition.start(clientConfiguration(4));
    final AtomicBoolean stopped = new AtomicBoolean();
    // Start cache create/destroy worker.
    IgniteInternalFuture startStopFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            boolean exists = false;
            while (!stopped.get()) {
                Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                if (exists) {
                    destroySqlCache(node);
                    exists = false;
                } else {
                    createSqlCache(node);
                    exists = true;
                }
                Thread.sleep(ThreadLocalRandom.current().nextLong(200L, 400L));
            }
            return null;
        }
    }, 1);
    // Start several threads which will mess around indexes.
    final QueryIndex idx = index(IDX_NAME_1, field(FIELD_NAME_1));
    IgniteInternalFuture idxFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            boolean exists = false;
            while (!stopped.get()) {
                Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                IgniteInternalFuture fut;
                if (exists) {
                    fut = queryProcessor(node).dynamicIndexDrop(CACHE_NAME, CACHE_NAME, IDX_NAME_1, true);
                    exists = false;
                } else {
                    fut = queryProcessor(node).dynamicIndexCreate(CACHE_NAME, CACHE_NAME, TBL_NAME, idx, true);
                    exists = true;
                }
                try {
                    fut.get();
                } catch (SchemaOperationException e) {
                // No-op.
                } catch (Exception e) {
                    fail("Unexpected exception: " + e);
                }
            }
            return null;
        }
    }, 8);
    Thread.sleep(TEST_DUR);
    stopped.set(true);
    // Make sure nothing hanged.
    startStopFut.get();
    idxFut.get();
    // Make sure cache is operational at this point.
    createSqlCache(cli);
    queryProcessor(cli).dynamicIndexDrop(CACHE_NAME, CACHE_NAME, IDX_NAME_1, true).get();
    queryProcessor(cli).dynamicIndexCreate(CACHE_NAME, CACHE_NAME, TBL_NAME, idx, true).get();
    assertIndex(CACHE_NAME, TBL_NAME, IDX_NAME_1, field(FIELD_NAME_1));
    put(cli, 0, KEY_AFTER);
    assertIndexUsed(IDX_NAME_1, SQL_SIMPLE_FIELD_1, SQL_ARG_1);
    assertSqlSimpleData(SQL_SIMPLE_FIELD_1, KEY_AFTER - SQL_ARG_1);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) QueryIndex(org.apache.ignite.cache.QueryIndex) Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException)

Example 7 with SchemaOperationException

use of org.apache.ignite.internal.processors.query.schema.SchemaOperationException in project ignite by apache.

the class DdlStatementsProcessor method runDdlStatement.

/**
     * Execute DDL statement.
     *
     * @param sql SQL.
     * @param stmt H2 statement to parse and execute.
     */
@SuppressWarnings("unchecked")
public FieldsQueryCursor<List<?>> runDdlStatement(String sql, PreparedStatement stmt) throws IgniteCheckedException {
    assert stmt instanceof JdbcPreparedStatement;
    IgniteInternalFuture fut = null;
    try {
        GridSqlStatement stmt0 = new GridSqlQueryParser(false).parse(GridSqlQueryParser.prepared(stmt));
        if (stmt0 instanceof GridSqlCreateIndex) {
            GridSqlCreateIndex cmd = (GridSqlCreateIndex) stmt0;
            GridH2Table tbl = idx.dataTable(cmd.schemaName(), cmd.tableName());
            if (tbl == null)
                throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd.tableName());
            assert tbl.rowDescriptor() != null;
            QueryIndex newIdx = new QueryIndex();
            newIdx.setName(cmd.index().getName());
            newIdx.setIndexType(cmd.index().getIndexType());
            LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>();
            // Let's replace H2's table and property names by those operated by GridQueryProcessor.
            GridQueryTypeDescriptor typeDesc = tbl.rowDescriptor().type();
            for (Map.Entry<String, Boolean> e : cmd.index().getFields().entrySet()) {
                GridQueryProperty prop = typeDesc.property(e.getKey());
                if (prop == null)
                    throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, e.getKey());
                flds.put(prop.name(), e.getValue());
            }
            newIdx.setFields(flds);
            fut = ctx.query().dynamicIndexCreate(tbl.cacheName(), cmd.schemaName(), typeDesc.tableName(), newIdx, cmd.ifNotExists());
        } else if (stmt0 instanceof GridSqlDropIndex) {
            GridSqlDropIndex cmd = (GridSqlDropIndex) stmt0;
            GridH2Table tbl = idx.dataTableForIndex(cmd.schemaName(), cmd.indexName());
            if (tbl != null) {
                fut = ctx.query().dynamicIndexDrop(tbl.cacheName(), cmd.schemaName(), cmd.indexName(), cmd.ifExists());
            } else {
                if (cmd.ifExists())
                    fut = new GridFinishedFuture();
                else
                    throw new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, cmd.indexName());
            }
        } else if (stmt0 instanceof GridSqlCreateTable) {
            GridSqlCreateTable cmd = (GridSqlCreateTable) stmt0;
            if (!F.eq(QueryUtils.DFLT_SCHEMA, cmd.schemaName()))
                throw new SchemaOperationException("CREATE TABLE can only be executed on " + QueryUtils.DFLT_SCHEMA + " schema.");
            GridH2Table tbl = idx.dataTable(cmd.schemaName(), cmd.tableName());
            if (tbl != null) {
                if (!cmd.ifNotExists())
                    throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_EXISTS, cmd.tableName());
            } else {
                ctx.query().dynamicTableCreate(cmd.schemaName(), toQueryEntity(cmd), cmd.templateName(), cmd.atomicityMode(), cmd.backups(), cmd.ifNotExists());
            }
        } else if (stmt0 instanceof GridSqlDropTable) {
            GridSqlDropTable cmd = (GridSqlDropTable) stmt0;
            if (!F.eq(QueryUtils.DFLT_SCHEMA, cmd.schemaName()))
                throw new SchemaOperationException("DROP TABLE can only be executed on " + QueryUtils.DFLT_SCHEMA + " schema.");
            GridH2Table tbl = idx.dataTable(cmd.schemaName(), cmd.tableName());
            if (tbl == null) {
                if (!cmd.ifExists())
                    throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd.tableName());
            } else
                ctx.query().dynamicTableDrop(tbl.cacheName(), cmd.tableName(), cmd.ifExists());
        } else
            throw new IgniteSQLException("Unsupported DDL operation: " + sql, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        if (fut != null)
            fut.get();
        QueryCursorImpl<List<?>> resCur = (QueryCursorImpl<List<?>>) new QueryCursorImpl(Collections.singletonList(Collections.singletonList(0L)), null, false);
        resCur.fieldsMeta(UPDATE_RESULT_META);
        return resCur;
    } catch (SchemaOperationException e) {
        throw convert(e);
    } catch (IgniteSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new IgniteSQLException("Unexpected DLL operation failure: " + e.getMessage(), e);
    }
}
Also used : GridSqlStatement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlStatement) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridSqlDropIndex(org.apache.ignite.internal.processors.query.h2.sql.GridSqlDropIndex) LinkedHashMap(java.util.LinkedHashMap) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridSqlCreateIndex(org.apache.ignite.internal.processors.query.h2.sql.GridSqlCreateIndex) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) QueryIndex(org.apache.ignite.cache.QueryIndex) List(java.util.List) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) GridSqlCreateTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlCreateTable) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) GridSqlQueryParser(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQueryParser) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GridSqlDropTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlDropTable)

Example 8 with SchemaOperationException

use of org.apache.ignite.internal.processors.query.schema.SchemaOperationException in project ignite by apache.

the class DynamicIndexAbstractConcurrentSelfTest method testQueryConsistencyMultithreaded.

/**
     * Make sure that contended operations on the same index from different nodes do not hang when we issue both
     * CREATE/DROP and SELECT statements.
     *
     * @throws Exception If failed.
     */
public void testQueryConsistencyMultithreaded() throws Exception {
    // Start complex topology.
    Ignition.start(serverConfiguration(1));
    Ignition.start(serverConfiguration(2));
    Ignition.start(serverConfiguration(3, true));
    Ignite cli = Ignition.start(clientConfiguration(4));
    createSqlCache(cli);
    put(cli, 0, KEY_AFTER);
    final AtomicBoolean stopped = new AtomicBoolean();
    // Thread which will mess around indexes.
    final QueryIndex idx = index(IDX_NAME_1, field(FIELD_NAME_1));
    IgniteInternalFuture idxFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            boolean exists = false;
            while (!stopped.get()) {
                Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                IgniteInternalFuture fut;
                if (exists) {
                    fut = queryProcessor(node).dynamicIndexDrop(CACHE_NAME, CACHE_NAME, IDX_NAME_1, true);
                    exists = false;
                } else {
                    fut = queryProcessor(node).dynamicIndexCreate(CACHE_NAME, CACHE_NAME, TBL_NAME, idx, true);
                    exists = true;
                }
                try {
                    fut.get();
                } catch (SchemaOperationException e) {
                // No-op.
                } catch (Exception e) {
                    fail("Unexpected exception: " + e);
                }
            }
            return null;
        }
    }, 1);
    IgniteInternalFuture qryFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stopped.get()) {
                Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                assertSqlSimpleData(node, SQL_SIMPLE_FIELD_1, KEY_AFTER - SQL_ARG_1);
            }
            return null;
        }
    }, 8);
    Thread.sleep(TEST_DUR);
    stopped.set(true);
    // Make sure nothing hanged.
    idxFut.get();
    qryFut.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) QueryIndex(org.apache.ignite.cache.QueryIndex) Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException)

Example 9 with SchemaOperationException

use of org.apache.ignite.internal.processors.query.schema.SchemaOperationException in project ignite by apache.

the class DynamicIndexAbstractBasicSelfTest method checkCreateNotCache.

/**
     * Check create when cache doesn't exist.
     *
     * @param mode Mode.
     * @param atomicityMode Atomicity mode.
     * @param near Near flag.
     * @throws Exception If failed.
     */
private void checkCreateNotCache(CacheMode mode, CacheAtomicityMode atomicityMode, boolean near) throws Exception {
    initialize(mode, atomicityMode, near);
    final QueryIndex idx = index(IDX_NAME_1, field(FIELD_NAME_1_ESCAPED));
    try {
        String cacheName = randomString();
        queryProcessor(node()).dynamicIndexCreate(cacheName, cacheName, TBL_NAME, idx, false).get();
    } catch (SchemaOperationException e) {
        assertEquals(SchemaOperationException.CODE_CACHE_NOT_FOUND, e.code());
        assertNoIndex(CACHE_NAME, TBL_NAME, IDX_NAME_1);
        return;
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    fail(SchemaOperationException.class.getSimpleName() + " is not thrown.");
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) QueryIndex(org.apache.ignite.cache.QueryIndex) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) CacheException(javax.cache.CacheException)

Example 10 with SchemaOperationException

use of org.apache.ignite.internal.processors.query.schema.SchemaOperationException in project ignite by apache.

the class DynamicIndexAbstractConcurrentSelfTest method testConcurrentOperationsAndNodeStartStopMultithreaded.

/**
     * Test concurrent node start/stop along with index operations. Nothing should hang.
     *
     * @throws Exception If failed.
     */
public void testConcurrentOperationsAndNodeStartStopMultithreaded() throws Exception {
    // Start several stable nodes.
    Ignition.start(serverConfiguration(1));
    Ignition.start(serverConfiguration(2));
    Ignition.start(serverConfiguration(3, true));
    final Ignite cli = Ignition.start(clientConfiguration(4));
    createSqlCache(cli);
    final AtomicBoolean stopped = new AtomicBoolean();
    // Start node start/stop worker.
    final AtomicInteger nodeIdx = new AtomicInteger(4);
    IgniteInternalFuture startStopFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            boolean exists = false;
            int lastIdx = 0;
            while (!stopped.get()) {
                if (exists) {
                    stopGrid(lastIdx);
                    exists = false;
                } else {
                    lastIdx = nodeIdx.incrementAndGet();
                    IgniteConfiguration cfg;
                    switch(ThreadLocalRandom.current().nextInt(0, 3)) {
                        case 1:
                            cfg = serverConfiguration(lastIdx, false);
                            break;
                        case 2:
                            cfg = serverConfiguration(lastIdx, true);
                            break;
                        default:
                            cfg = clientConfiguration(lastIdx);
                    }
                    Ignition.start(cfg);
                    exists = true;
                }
                Thread.sleep(ThreadLocalRandom.current().nextLong(500L, 1500L));
            }
            return null;
        }
    }, 1);
    // Start several threads which will mess around indexes.
    final QueryIndex idx = index(IDX_NAME_1, field(FIELD_NAME_1));
    IgniteInternalFuture idxFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            boolean exists = false;
            while (!stopped.get()) {
                Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                IgniteInternalFuture fut;
                if (exists) {
                    fut = queryProcessor(node).dynamicIndexDrop(CACHE_NAME, CACHE_NAME, IDX_NAME_1, true);
                    exists = false;
                } else {
                    fut = queryProcessor(node).dynamicIndexCreate(CACHE_NAME, CACHE_NAME, TBL_NAME, idx, true);
                    exists = true;
                }
                try {
                    fut.get();
                } catch (SchemaOperationException e) {
                // No-op.
                } catch (Exception e) {
                    fail("Unexpected exception: " + e);
                }
            }
            return null;
        }
    }, 1);
    Thread.sleep(TEST_DUR);
    stopped.set(true);
    // Make sure nothing hanged.
    startStopFut.get();
    idxFut.get();
    // Make sure cache is operational at this point.
    createSqlCache(cli);
    queryProcessor(cli).dynamicIndexDrop(CACHE_NAME, CACHE_NAME, IDX_NAME_1, true).get();
    queryProcessor(cli).dynamicIndexCreate(CACHE_NAME, CACHE_NAME, TBL_NAME, idx, true).get();
    assertIndex(CACHE_NAME, TBL_NAME, IDX_NAME_1, field(FIELD_NAME_1));
    put(cli, 0, KEY_AFTER);
    assertIndexUsed(IDX_NAME_1, SQL_SIMPLE_FIELD_1, SQL_ARG_1);
    assertSqlSimpleData(SQL_SIMPLE_FIELD_1, KEY_AFTER - SQL_ARG_1);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryIndex(org.apache.ignite.cache.QueryIndex) Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException)

Aggregations

SchemaOperationException (org.apache.ignite.internal.processors.query.schema.SchemaOperationException)15 QueryIndex (org.apache.ignite.cache.QueryIndex)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 IgniteException (org.apache.ignite.IgniteException)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Ignite (org.apache.ignite.Ignite)5 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)5 CacheException (javax.cache.CacheException)3 SchemaOperationClientFuture (org.apache.ignite.internal.processors.query.schema.SchemaOperationClientFuture)3 SchemaIndexCreateOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation)3 SchemaIndexDropOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexDropOperation)3 SQLException (java.sql.SQLException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)2 DynamicCacheDescriptor (org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 SchemaProposeDiscoveryMessage (org.apache.ignite.internal.processors.query.schema.message.SchemaProposeDiscoveryMessage)2 SchemaAbstractOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaAbstractOperation)2