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;
}
}
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();
}
}
}
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"));
}
}
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());
}
}
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);
}
}
}
}
Aggregations