use of herddb.model.DDLStatementExecutionResult in project herddb by diennea.
the class DBManager method createTableSpace.
private StatementExecutionResult createTableSpace(CreateTableSpaceStatement createTableSpaceStatement) throws StatementExecutionException {
TableSpace tableSpace;
try {
tableSpace = TableSpace.builder().leader(createTableSpaceStatement.getLeaderId()).name(createTableSpaceStatement.getTableSpace()).replicas(createTableSpaceStatement.getReplicas()).expectedReplicaCount(createTableSpaceStatement.getExpectedReplicaCount()).maxLeaderInactivityTime(createTableSpaceStatement.getMaxleaderinactivitytime()).build();
} catch (IllegalArgumentException invalid) {
throw new StatementExecutionException("invalid CREATE TABLESPACE statement: " + invalid.getMessage(), invalid);
}
try {
metadataStorageManager.registerTableSpace(tableSpace);
triggerActivator(ActivatorRunRequest.FULL);
if (createTableSpaceStatement.getWaitForTableSpaceTimeout() > 0) {
boolean okWait = false;
int poolTime = 100;
if (metadataStorageManager instanceof MemoryMetadataStorageManager || metadataStorageManager instanceof FileMetadataStorageManager) {
poolTime = 5;
}
LOGGER.log(Level.SEVERE, "waiting for " + tableSpace.name + ", uuid " + tableSpace.uuid + ", to be up withint " + createTableSpaceStatement.getWaitForTableSpaceTimeout() + " ms");
final int timeout = createTableSpaceStatement.getWaitForTableSpaceTimeout();
for (int i = 0; i < timeout; i += poolTime) {
List<TableSpaceReplicaState> replicateStates = metadataStorageManager.getTableSpaceReplicaState(tableSpace.uuid);
for (TableSpaceReplicaState ts : replicateStates) {
LOGGER.log(Level.SEVERE, "waiting for " + tableSpace.name + ", uuid " + tableSpace.uuid + ", to be up, replica state node: " + ts.nodeId + ", state: " + TableSpaceReplicaState.modeToSQLString(ts.mode) + ", ts " + new java.sql.Timestamp(ts.timestamp));
if (ts.mode == TableSpaceReplicaState.MODE_LEADER) {
okWait = true;
break;
}
}
if (okWait) {
break;
}
Thread.sleep(poolTime);
}
if (!okWait) {
throw new StatementExecutionException("tablespace " + tableSpace.name + ", uuid " + tableSpace.uuid + " has been created but leader " + tableSpace.leaderId + " did not start within " + createTableSpaceStatement.getWaitForTableSpaceTimeout() + " ms");
}
}
return new DDLStatementExecutionResult(TransactionContext.NOTRANSACTION_ID);
} catch (StatementExecutionException err) {
throw err;
} catch (Exception err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.DDLStatementExecutionResult in project herddb by diennea.
the class DBManager method dropTableSpace.
private StatementExecutionResult dropTableSpace(DropTableSpaceStatement dropTableSpaceStatement) throws StatementExecutionException {
try {
TableSpace previous = metadataStorageManager.describeTableSpace(dropTableSpaceStatement.getTableSpace());
if (previous == null) {
throw new TableSpaceDoesNotExistException(dropTableSpaceStatement.getTableSpace());
}
metadataStorageManager.dropTableSpace(dropTableSpaceStatement.getTableSpace(), previous);
triggerActivator(ActivatorRunRequest.TABLESPACEMANAGEMENT);
return new DDLStatementExecutionResult(TransactionContext.NOTRANSACTION_ID);
} catch (Exception err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.DDLStatementExecutionResult in project herddb by diennea.
the class TableSpaceManager method alterTable.
private StatementExecutionResult alterTable(AlterTableStatement alterTableStatement, TransactionContext transactionContext) throws TableDoesNotExistException, StatementExecutionException {
generalLock.writeLock().lock();
try {
if (transactionContext.transactionId > 0) {
throw new StatementExecutionException("ALTER TABLE cannot be executed inside a transaction (txid=" + transactionContext.transactionId + ")");
}
AbstractTableManager tableManager = tables.get(alterTableStatement.getTable());
if (tableManager == null) {
throw new TableDoesNotExistException("no table " + alterTableStatement.getTable() + " in tablespace " + tableSpaceName);
}
Table newTable;
try {
newTable = tableManager.getTable().applyAlterTable(alterTableStatement);
} catch (IllegalArgumentException error) {
throw new StatementExecutionException(error);
}
LogEntry entry = LogEntryFactory.alterTable(newTable, null);
try {
CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
apply(pos, entry, false);
} catch (Exception err) {
throw new StatementExecutionException(err);
}
return new DDLStatementExecutionResult(transactionContext.transactionId);
} finally {
generalLock.writeLock().unlock();
}
}
use of herddb.model.DDLStatementExecutionResult in project herddb by diennea.
the class TableSpaceManager method dropIndex.
private StatementExecutionResult dropIndex(DropIndexStatement statement, Transaction transaction) throws StatementExecutionException {
generalLock.writeLock().lock();
try {
if (!indexes.containsKey(statement.getIndexName())) {
if (statement.isIfExists()) {
return new DDLStatementExecutionResult(transaction != null ? transaction.transactionId : 0);
}
throw new IndexDoesNotExistException("index " + statement.getIndexName() + " does not exist " + statement.getIndexName() + " on tableSpace " + statement.getTableSpace());
}
if (transaction != null && transaction.isIndexDropped(statement.getIndexName())) {
if (statement.isIfExists()) {
return new DDLStatementExecutionResult(transaction.transactionId);
}
throw new IndexDoesNotExistException("index does not exist " + statement.getIndexName() + " on tableSpace " + statement.getTableSpace());
}
LogEntry entry = LogEntryFactory.dropIndex(statement.getIndexName(), transaction);
CommitLogResult pos;
try {
pos = log.log(entry, entry.transactionId <= 0);
} catch (LogNotAvailableException ex) {
throw new StatementExecutionException(ex);
}
apply(pos, entry, false);
return new DDLStatementExecutionResult(entry.transactionId);
} catch (DataStorageManagerException err) {
throw new StatementExecutionException(err);
} finally {
generalLock.writeLock().unlock();
}
}
use of herddb.model.DDLStatementExecutionResult in project herddb by diennea.
the class TableSpaceManager method createIndex.
private StatementExecutionResult createIndex(CreateIndexStatement statement, Transaction transaction) throws StatementExecutionException {
generalLock.writeLock().lock();
try {
if (indexes.containsKey(statement.getIndexefinition().name)) {
throw new IndexAlreadyExistsException(statement.getIndexefinition().name);
}
LogEntry entry = LogEntryFactory.createIndex(statement.getIndexefinition(), transaction);
CommitLogResult pos;
try {
pos = log.log(entry, entry.transactionId <= 0);
} catch (LogNotAvailableException ex) {
throw new StatementExecutionException(ex);
}
apply(pos, entry, false);
return new DDLStatementExecutionResult(entry.transactionId);
} catch (DataStorageManagerException err) {
throw new StatementExecutionException(err);
} finally {
generalLock.writeLock().unlock();
}
}
Aggregations