use of herddb.log.LogNotAvailableException in project herddb by diennea.
the class DBManager method handleTableSpace.
private void handleTableSpace(TableSpace tableSpace) throws DataStorageManagerException, LogNotAvailableException, MetadataStorageManagerException, DDLException {
String tableSpaceName = tableSpace.name;
TableSpaceManager actual_manager = tablesSpaces.get(tableSpaceName);
if (actual_manager != null && actual_manager.isFailed()) {
LOGGER.log(Level.INFO, "Tablespace {0} is in 'Failed' status", new Object[] { tableSpaceName, nodeId });
return;
}
if (actual_manager != null && actual_manager.isLeader() && !tableSpace.leaderId.equals(nodeId)) {
LOGGER.log(Level.INFO, "Tablespace {0} leader is no more {1}, it changed to {2}", new Object[] { tableSpaceName, nodeId, tableSpace.leaderId });
stopTableSpace(tableSpaceName, tableSpace.uuid);
return;
}
if (actual_manager != null && !actual_manager.isLeader() && tableSpace.leaderId.equals(nodeId)) {
LOGGER.log(Level.INFO, "Tablespace {0} need to switch to leadership on node {1}", new Object[] { tableSpaceName, nodeId });
stopTableSpace(tableSpaceName, tableSpace.uuid);
return;
}
if (tableSpace.isNodeAssignedToTableSpace(nodeId) && !tablesSpaces.containsKey(tableSpaceName)) {
LOGGER.log(Level.INFO, "Booting tablespace {0} on {1}, uuid {2}", new Object[] { tableSpaceName, nodeId, tableSpace.uuid });
long _start = System.currentTimeMillis();
CommitLog commitLog = commitLogManager.createCommitLog(tableSpace.uuid, tableSpace.name, nodeId);
TableSpaceManager manager = new TableSpaceManager(nodeId, tableSpaceName, tableSpace.uuid, tableSpace.expectedReplicaCount, metadataStorageManager, dataStorageManager, commitLog, this, false);
try {
manager.start();
LOGGER.log(Level.INFO, "Boot success tablespace {0} on {1}, uuid {2}, time {3} ms leader:{4}", new Object[] { tableSpaceName, nodeId, tableSpace.uuid, (System.currentTimeMillis() - _start) + "", manager.isLeader() });
tablesSpaces.put(tableSpaceName, manager);
if (serverConfiguration.getBoolean(ServerConfiguration.PROPERTY_JMX_ENABLE, ServerConfiguration.PROPERTY_JMX_ENABLE_DEFAULT)) {
JMXUtils.registerTableSpaceManagerStatsMXBean(tableSpaceName, manager.getStats());
}
} catch (DataStorageManagerException | LogNotAvailableException | MetadataStorageManagerException | DDLException t) {
LOGGER.log(Level.SEVERE, "Error Booting tablespace {0} on {1}", new Object[] { tableSpaceName, nodeId });
LOGGER.log(Level.SEVERE, "Error", t);
tablesSpaces.remove(tableSpaceName);
try {
manager.close();
} catch (Throwable t2) {
LOGGER.log(Level.SEVERE, "Other Error", t2);
t.addSuppressed(t2);
}
throw t;
}
return;
}
if (tablesSpaces.containsKey(tableSpaceName) && !tableSpace.isNodeAssignedToTableSpace(nodeId)) {
LOGGER.log(Level.INFO, "Tablespace {0} on {1} is not more in replica list {3}, uuid {2}", new Object[] { tableSpaceName, nodeId, tableSpace.uuid, tableSpace.replicas + "" });
stopTableSpace(tableSpaceName, tableSpace.uuid);
return;
}
if (!tableSpace.isNodeAssignedToTableSpace("*") && tableSpace.replicas.size() < tableSpace.expectedReplicaCount) {
List<NodeMetadata> nodes = metadataStorageManager.listNodes();
LOGGER.log(Level.WARNING, "Tablespace {0} is underreplicated expectedReplicaCount={1}, replicas={2}, nodes={3}", new Object[] { tableSpaceName, tableSpace.expectedReplicaCount, tableSpace.replicas, nodes });
List<String> availableOtherNodes = nodes.stream().map(n -> {
return n.nodeId;
}).filter(n -> {
return !tableSpace.replicas.contains(n);
}).collect(Collectors.toList());
Collections.shuffle(availableOtherNodes);
int countMissing = tableSpace.expectedReplicaCount - tableSpace.replicas.size();
LOGGER.log(Level.WARNING, "Tablespace {0} is underreplicated expectedReplicaCount={1}, replicas={2}, missing {3}, availableOtherNodes={4}", new Object[] { tableSpaceName, tableSpace.expectedReplicaCount, tableSpace.replicas, countMissing, availableOtherNodes });
if (!availableOtherNodes.isEmpty()) {
TableSpace.Builder newTableSpaceBuilder = TableSpace.builder().cloning(tableSpace);
while (!availableOtherNodes.isEmpty() && countMissing-- > 0) {
String node = availableOtherNodes.remove(0);
LOGGER.log(Level.WARNING, "Tablespace {0} adding {1} node as replica", new Object[] { tableSpaceName, node });
newTableSpaceBuilder.replica(node);
}
TableSpace newTableSpace = newTableSpaceBuilder.build();
boolean ok = metadataStorageManager.updateTableSpace(newTableSpace, tableSpace);
if (!ok) {
LOGGER.log(Level.SEVERE, "updating tableSpace {0} metadata failed, someone else altered metadata", tableSpaceName);
}
}
}
if (actual_manager != null && !actual_manager.isFailed() && actual_manager.isLeader()) {
actual_manager.metadataUpdated(tableSpace);
}
}
use of herddb.log.LogNotAvailableException in project herddb by diennea.
the class TableSpaceManager method dropIndex.
private StatementExecutionResult dropIndex(DropIndexStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
boolean lockAcquired = false;
if (context.getTableSpaceLock() == 0) {
long lockStamp = acquireWriteLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
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 {
if (lockAcquired) {
releaseWriteLock(context.getTableSpaceLock(), statement);
context.setTableSpaceLock(0);
}
}
}
use of herddb.log.LogNotAvailableException in project herddb by diennea.
the class TableSpaceManager method createTable.
private StatementExecutionResult createTable(CreateTableStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
boolean lockAcquired = false;
if (context.getTableSpaceLock() == 0) {
long lockStamp = acquireWriteLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
try {
if (tables.containsKey(statement.getTableDefinition().name)) {
if (statement.isIfExistsClause()) {
return new DDLStatementExecutionResult(transaction != null ? transaction.transactionId : 0);
}
throw new TableAlreadyExistsException(statement.getTableDefinition().name);
}
for (Index additionalIndex : statement.getAdditionalIndexes()) {
AbstractIndexManager exists = indexes.get(additionalIndex.name);
if (exists != null) {
LOGGER.log(Level.INFO, "Error while creating index " + additionalIndex.name + ", there is already an index " + exists.getIndex().name + " on table " + exists.getIndex().table);
throw new IndexAlreadyExistsException(additionalIndex.name);
}
}
Table table = statement.getTableDefinition();
// validate foreign keys
if (table.foreignKeys != null) {
for (ForeignKeyDef def : table.foreignKeys) {
AbstractTableManager parentTableManager = null;
for (AbstractTableManager ab : tables.values()) {
if (ab.getTable().uuid.equals(def.parentTableId)) {
parentTableManager = ab;
break;
}
}
if (parentTableManager == null) {
throw new StatementExecutionException("Table " + def.parentTableId + " does not exist in tablespace " + tableSpaceName);
}
Table parentTable = parentTableManager.getTable();
int i = 0;
for (String col : def.columns) {
Column column = table.getColumn(col);
Column parentColumn = parentTable.getColumn(def.parentTableColumns[i]);
if (column == null) {
throw new StatementExecutionException("Cannot find column " + col);
}
if (parentColumn == null) {
throw new StatementExecutionException("Cannot find column " + def.parentTableColumns[i]);
}
if (!ColumnTypes.sameRawDataType(column.type, parentColumn.type)) {
throw new StatementExecutionException("Column " + table.name + "." + column.name + " is not the same tyepe of column " + parentTable.name + "." + parentColumn.name);
}
i++;
}
}
}
LogEntry entry = LogEntryFactory.createTable(statement.getTableDefinition(), transaction);
CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
apply(pos, entry, false);
for (Index additionalIndex : statement.getAdditionalIndexes()) {
LogEntry index_entry = LogEntryFactory.createIndex(additionalIndex, transaction);
CommitLogResult index_pos = log.log(index_entry, index_entry.transactionId <= 0);
apply(index_pos, index_entry, false);
}
return new DDLStatementExecutionResult(entry.transactionId);
} catch (DataStorageManagerException | LogNotAvailableException err) {
throw new StatementExecutionException(err);
} finally {
if (lockAcquired) {
releaseWriteLock(context.getTableSpaceLock(), statement);
context.setTableSpaceLock(0);
}
}
}
use of herddb.log.LogNotAvailableException in project herddb by diennea.
the class TableSpaceManager method createIndex.
private StatementExecutionResult createIndex(CreateIndexStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
boolean lockAcquired = false;
if (context.getTableSpaceLock() == 0) {
long lockStamp = acquireWriteLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
try {
AbstractIndexManager exists = indexes.get(statement.getIndexDefinition().name);
if (exists != null) {
LOGGER.log(Level.INFO, "Error while creating index " + statement.getIndexDefinition().name + ", there is already an index " + exists.getIndex().name + " on table " + exists.getIndex().table);
throw new IndexAlreadyExistsException(statement.getIndexDefinition().name);
}
LogEntry entry = LogEntryFactory.createIndex(statement.getIndexDefinition(), 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 {
if (lockAcquired) {
releaseWriteLock(context.getTableSpaceLock(), statement);
context.setTableSpaceLock(0);
}
}
}
use of herddb.log.LogNotAvailableException in project herddb by diennea.
the class FileCommitLog method openNewLedger.
private void openNewLedger() throws LogNotAvailableException {
try {
if (writer != null) {
LOGGER.log(Level.FINEST, "closing actual file {0}", writer.filename);
writer.close();
}
writer = new CommitFileWriter(++currentLedgerId, -1);
newfiles.inc();
} catch (IOException err) {
throw new LogNotAvailableException(err);
}
}
Aggregations