Search in sources :

Example 41 with TableSpaceManager

use of herddb.core.TableSpaceManager in project herddb by diennea.

the class JSQLParserPlanner method planerInsertOrUpsert.

private ExecutionPlan planerInsertOrUpsert(String defaultTableSpace, net.sf.jsqlparser.schema.Table table, List<net.sf.jsqlparser.schema.Column> columns, ItemsList itemsList, boolean returnValues, boolean upsert) throws StatementExecutionException, StatementNotSupportedException {
    OpSchema inputSchema = getTableSchema(defaultTableSpace, table);
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(inputSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(inputSchema.name);
    Table tableImpl = tableManager.getTable();
    List<CompiledSQLExpression> keyValueExpression = new ArrayList<>();
    List<String> keyExpressionToColumn = new ArrayList<>();
    List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
    List<String> valuesColumns = new ArrayList<>();
    boolean invalid = false;
    int index = 0;
    if (columns == null) {
        // INSERT INTO TABLE VALUES (xxxx) (no column list)
        columns = new ArrayList<>();
        for (Column c : tableImpl.getColumns()) {
            columns.add(new net.sf.jsqlparser.schema.Column(c.name));
        }
    }
    if (itemsList instanceof ExpressionList) {
        List<Expression> values = ((ExpressionList) itemsList).getExpressions();
        for (net.sf.jsqlparser.schema.Column column : columns) {
            CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(values.get(index), inputSchema);
            String columnName = fixMySqlBackTicks(column.getColumnName()).toLowerCase();
            if (exp instanceof ConstantExpression || exp instanceof JdbcParameterExpression || exp instanceof TypedJdbcParameterExpression || exp instanceof CompiledFunction) {
                boolean isAlwaysNull = (exp instanceof ConstantExpression) && ((ConstantExpression) exp).isNull();
                if (!isAlwaysNull) {
                    if (tableImpl.isPrimaryKeyColumn(columnName)) {
                        keyExpressionToColumn.add(columnName);
                        keyValueExpression.add(exp);
                    }
                    Column columnFromSchema = tableImpl.getColumn(columnName);
                    if (columnFromSchema == null) {
                        throw new StatementExecutionException("Column '" + columnName + "' not found in table " + tableImpl.name);
                    }
                    valuesColumns.add(columnFromSchema.name);
                    valuesExpressions.add(exp);
                }
                index++;
            } else {
                checkSupported(false, "Unsupported expression type " + exp.getClass().getName());
                break;
            }
        }
        // handle default values
        for (Column col : tableImpl.getColumns()) {
            if (!valuesColumns.contains(col.name)) {
                if (col.defaultValue != null) {
                    valuesColumns.add(col.name);
                    CompiledSQLExpression defaultValueExpression = makeDefaultValue(col);
                    valuesExpressions.add(defaultValueExpression);
                } else if (ColumnTypes.isNotNullDataType(col.type) && !tableImpl.auto_increment) {
                    throw new StatementExecutionException("Column '" + col.name + "' has no default value and does not allow NULLs");
                }
            }
        }
        DMLStatement statement;
        if (!invalid) {
            RecordFunction keyfunction;
            if (keyValueExpression.isEmpty() && tableImpl.auto_increment) {
                keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
            } else {
                if (keyValueExpression.size() != tableImpl.primaryKey.length) {
                    throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
                }
                keyfunction = new SQLRecordKeyFunction(keyExpressionToColumn, keyValueExpression, tableImpl);
            }
            RecordFunction valuesfunction = new SQLRecordFunction(valuesColumns, tableImpl, valuesExpressions);
            statement = new InsertStatement(inputSchema.tableSpace, inputSchema.name, keyfunction, valuesfunction, upsert).setReturnValues(returnValues);
        } else {
            throw new StatementNotSupportedException();
        }
        PlannerOp op = new SimpleInsertOp(statement.setReturnValues(returnValues));
        return optimizePlan(op);
    } else if (itemsList instanceof MultiExpressionList) {
        List<ExpressionList> records = ((MultiExpressionList) itemsList).getExprList();
        ValuesOp values = planValuesForInsertOp(columns, tableImpl, inputSchema, records);
        InsertOp op = new InsertOp(tableImpl.tablespace, tableImpl.name, values, returnValues, upsert);
        return optimizePlan(op);
    } else {
        checkSupported(false);
        return null;
    }
}
Also used : ConstantExpression(herddb.sql.expressions.ConstantExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) ValuesOp(herddb.model.planner.ValuesOp) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) Column(herddb.model.Column) TableSpaceManager(herddb.core.TableSpaceManager) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) List(java.util.List) SetOperationList(net.sf.jsqlparser.statement.select.SetOperationList) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) CompiledFunction(herddb.sql.expressions.CompiledFunction) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlannerOp(herddb.model.planner.PlannerOp) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) AbstractTableManager(herddb.core.AbstractTableManager) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) Expression(net.sf.jsqlparser.expression.Expression) CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) ConstantExpression(herddb.sql.expressions.ConstantExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) DMLStatement(herddb.model.DMLStatement) OpSchema(herddb.sql.expressions.OpSchema) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) InsertOp(herddb.model.planner.InsertOp)

Example 42 with TableSpaceManager

use of herddb.core.TableSpaceManager in project herddb by diennea.

the class AbstractSQLPlanner method getTableSpaceManager.

protected final TableSpaceManager getTableSpaceManager(String tableSpace) {
    long startTs = System.currentTimeMillis();
    while (true) {
        TableSpaceManager result = manager.getTableSpaceManager(tableSpace);
        if (result != null) {
            return result;
        }
        long delta = System.currentTimeMillis() - startTs;
        LOG.log(Level.FINE, "schema {0} not available yet, after waiting {1}/{2} ms", new Object[] { tableSpace, delta, waitForSchemaTimeout });
        if (delta >= waitForSchemaTimeout) {
            return null;
        }
        clearCache();
        try {
            Thread.sleep(100);
        } catch (InterruptedException err) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : TableSpaceManager(herddb.core.TableSpaceManager)

Example 43 with TableSpaceManager

use of herddb.core.TableSpaceManager in project herddb by diennea.

the class ShowCreateTableCalculator method calculateShowCreateTable.

public static TranslatedQuery calculateShowCreateTable(String query, String defaultTablespace, List<Object> parameters, DBManager manager) {
    String[] items = { "SHOW", "CREATE", "TABLE" };
    if (Arrays.stream(items).allMatch(query::contains)) {
        query = query.substring(Arrays.stream(items).collect(Collectors.joining(" ")).length()).trim();
        String tableSpace = defaultTablespace;
        String tableName;
        boolean showCreateIndex = query.contains("WITH INDEXES");
        if (showCreateIndex) {
            query = query.substring(0, query.indexOf("WITH INDEXES"));
        }
        if (query.contains(".")) {
            String[] tokens = query.split("\\.");
            tableSpace = tokens[0].trim();
            tableName = tokens[1].trim();
        } else {
            tableName = query.trim();
        }
        tableName = tableName.toLowerCase();
        TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
        if (tableSpaceManager == null) {
            throw new TableSpaceDoesNotExistException(String.format("Tablespace %s does not exist.", tableSpace));
        }
        AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
        if (tableManager == null || tableManager.getCreatedInTransaction() > 0) {
            throw new TableDoesNotExistException(String.format("Table %s does not exist.", tableName));
        }
        String showCreateResult = calculate(showCreateIndex, tableName, tableSpace, tableManager);
        ValuesOp values = new ValuesOp(manager.getNodeId(), new String[] { "tabledef" }, new Column[] { column("tabledef", ColumnTypes.STRING) }, Arrays.asList(Arrays.asList(new ConstantExpression(showCreateResult, ColumnTypes.NOTNULL_STRING))));
        ExecutionPlan executionPlan = ExecutionPlan.simple(new SQLPlannedOperationStatement(values), values);
        return new TranslatedQuery(executionPlan, new SQLStatementEvaluationContext(query, parameters, false, false));
    } else {
        throw new StatementExecutionException(String.format("Incorrect Syntax for SHOW CREATE TABLE tablespace.tablename"));
    }
}
Also used : TranslatedQuery(herddb.sql.TranslatedQuery) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) ConstantExpression(herddb.sql.expressions.ConstantExpression) SQLStatementEvaluationContext(herddb.sql.SQLStatementEvaluationContext) ValuesOp(herddb.model.planner.ValuesOp) StatementExecutionException(herddb.model.StatementExecutionException) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) TableDoesNotExistException(herddb.model.TableDoesNotExistException) ExecutionPlan(herddb.model.ExecutionPlan) AbstractTableManager(herddb.core.AbstractTableManager) TableSpaceManager(herddb.core.TableSpaceManager)

Example 44 with TableSpaceManager

use of herddb.core.TableSpaceManager in project herddb by diennea.

the class MaxLeaderInactivityTest method test_auto_heal1.

@Test
public void test_auto_heal1() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            TestUtils.execute(server_1.getManager(), "CREATE TABLESPACE 'ttt','leader:" + server_2.getNodeId() + "','expectedreplicacount:2'", Collections.emptyList());
            // wait for server_2 to wake up
            for (int i = 0; i < 40; i++) {
                TableSpaceManager tableSpaceManager2 = server_2.getManager().getTableSpaceManager("ttt");
                if (tableSpaceManager2 != null && tableSpaceManager2.isLeader()) {
                    break;
                }
                Thread.sleep(500);
            }
            assertTrue(server_2.getManager().getTableSpaceManager("ttt") != null && server_2.getManager().getTableSpaceManager("ttt").isLeader());
            TestUtils.execute(server_1.getManager(), "ALTER TABLESPACE 'ttt','maxLeaderInactivityTime:5000'", Collections.emptyList());
            try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM SYSTABLESPACEREPLICASTATE where tablespace_name='ttt' and nodeId='" + server_2.getNodeId() + "'", Collections.emptyList())) {
                List<DataAccessor> tuples = scan.consume();
                // for (Tuple t : tuples) {
                // System.out.println("tuple:" + t);
                // }
                assertEquals(1, tuples.size());
            }
            // wait for server_1 to announce as follower
            for (int i = 0; i < 100; i++) {
                try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM SYSTABLESPACEREPLICASTATE where tablespace_name='ttt' and nodeId='" + server_1.getNodeId() + "'", Collections.emptyList())) {
                    List<DataAccessor> tuples = scan.consume();
                    // for (Tuple t : tuples) {
                    // System.out.println("tuple:" + t);
                    // }
                    assertEquals(1, tuples.size());
                    if (tuples.get(0).get("mode").equals("follower")) {
                        break;
                    }
                }
                Thread.sleep(1000);
            }
        // server_2 dies
        }
        // wait for server_1 to announce as leader
        for (int i = 0; i < 100; i++) {
            try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM SYSTABLESPACEREPLICASTATE " + "where tablespace_name='ttt' and nodeId='" + server_1.getNodeId() + "'", Collections.emptyList())) {
                List<DataAccessor> tuples = scan.consume();
                for (DataAccessor t : tuples) {
                    System.out.println("tuple2:" + t);
                }
                assertEquals(1, tuples.size());
                if (tuples.get(0).get("mode").equals("leader")) {
                    break;
                }
            }
            Thread.sleep(1000);
        }
        assertTrue(server_1.getManager().getTableSpaceManager("ttt").isLeader());
    }
}
Also used : DataScanner(herddb.model.DataScanner) DataAccessor(herddb.utils.DataAccessor) TableSpaceManager(herddb.core.TableSpaceManager) Test(org.junit.Test)

Example 45 with TableSpaceManager

use of herddb.core.TableSpaceManager in project herddb by diennea.

the class MaxLeaderInactivityTest method test_auto_no_heal.

@Test
public void test_auto_no_heal() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            TestUtils.execute(server_1.getManager(), "CREATE TABLESPACE 'ttt','leader:" + server_2.getNodeId() + "','expectedreplicacount:2','wait:60000'", Collections.emptyList());
            // wait for server_2 to wake up
            for (int i = 0; i < 100; i++) {
                if (server_2.getManager().getTableSpaceManager("ttt") != null && server_2.getManager().getTableSpaceManager("ttt").isLeader()) {
                    break;
                }
                Thread.sleep(500);
            }
            assertTrue(server_2.getManager().getTableSpaceManager("ttt") != null && server_2.getManager().getTableSpaceManager("ttt").isLeader());
            TestUtils.execute(server_1.getManager(), "ALTER TABLESPACE 'ttt','maxLeaderInactivityTime:10000'", Collections.emptyList());
            try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM SYSTABLESPACEREPLICASTATE where " + "tablespace_name='ttt' and nodeId='" + server_2.getNodeId() + "'", Collections.emptyList())) {
                List<DataAccessor> tuples = scan.consume();
                // for (Tuple t : tuples) {
                // System.out.println("tuple:" + t);
                // }
                assertEquals(1, tuples.size());
            }
            // we want server 2 to be leader forever
            for (int i = 0; i < 20; i++) {
                try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM SYSTABLESPACEREPLICASTATE where tablespace_name='ttt'", Collections.emptyList())) {
                    List<DataAccessor> tuples = scan.consume();
                    // for (Tuple t : tuples) {
                    // System.out.println("tuple:" + t);
                    // }
                    assertEquals(2, tuples.size());
                }
                TableSpaceManager tableSpaceManager_1 = server_1.getManager().getTableSpaceManager("ttt");
                if (tableSpaceManager_1 != null) {
                    assertTrue(!tableSpaceManager_1.isLeader());
                }
                TableSpaceManager tableSpaceManager_2 = server_2.getManager().getTableSpaceManager("ttt");
                assertNotNull(tableSpaceManager_2);
                assertTrue(tableSpaceManager_2.isLeader());
                Thread.sleep(1000);
            }
        }
    }
}
Also used : DataScanner(herddb.model.DataScanner) DataAccessor(herddb.utils.DataAccessor) TableSpaceManager(herddb.core.TableSpaceManager) Test(org.junit.Test)

Aggregations

TableSpaceManager (herddb.core.TableSpaceManager)46 Table (herddb.model.Table)33 Test (org.junit.Test)23 InsertStatement (herddb.model.commands.InsertStatement)22 StatementExecutionException (herddb.model.StatementExecutionException)20 CreateTableStatement (herddb.model.commands.CreateTableStatement)20 AbstractTableManager (herddb.core.AbstractTableManager)16 Server (herddb.server.Server)15 ServerConfiguration (herddb.server.ServerConfiguration)15 DataScanner (herddb.model.DataScanner)13 BookkeeperCommitLog (herddb.cluster.BookkeeperCommitLog)11 AlterTableSpaceStatement (herddb.model.commands.AlterTableSpaceStatement)11 HashSet (java.util.HashSet)11 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)11 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)9 ArrayList (java.util.ArrayList)9 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)9 ClientConfiguration (herddb.client.ClientConfiguration)8 HDBClient (herddb.client.HDBClient)8 HDBConnection (herddb.client.HDBConnection)8