use of herddb.model.TableSpace in project herddb by diennea.
the class SystablespacereplicastateTableManager method buildVirtualRecordList.
@Override
protected Iterable<Record> buildVirtualRecordList(Transaction transaction) throws StatementExecutionException {
try {
Collection<String> names = tableSpaceManager.getMetadataStorageManager().listTableSpaces();
long now = System.currentTimeMillis();
List<Record> result = new ArrayList<>();
for (String name : names) {
TableSpace t = tableSpaceManager.getMetadataStorageManager().describeTableSpace(name);
if (t != null) {
List<TableSpaceReplicaState> tableSpaceReplicaStates = tableSpaceManager.getMetadataStorageManager().getTableSpaceReplicaState(t.uuid);
for (TableSpaceReplicaState state : tableSpaceReplicaStates) {
result.add(RecordSerializer.makeRecord(table, "tablespace_name", t.name, "uuid", t.uuid, "nodeid", state.nodeId, "timestamp", new java.sql.Timestamp(state.timestamp), "maxleaderinactivitytime", t.maxLeaderInactivityTime, "inactivitytime", now - state.timestamp, "mode", TableSpaceReplicaState.modeToSQLString(state.mode)));
}
}
}
return result;
} catch (MetadataStorageManagerException error) {
throw new StatementExecutionException(error);
}
}
use of herddb.model.TableSpace in project herddb by diennea.
the class MemoryMetadataStorageManager method ensureDefaultTableSpace.
@Override
public boolean ensureDefaultTableSpace(String localNodeId, String initialReplicaList, long maxLeaderInactivityTime, int expectedReplicaCount) throws MetadataStorageManagerException {
lock.writeLock().lock();
try {
TableSpace exists = tableSpaces.get(TableSpace.DEFAULT);
if (exists == null) {
TableSpace defaultTableSpace = TableSpace.builder().leader(localNodeId).replica(localNodeId).name(TableSpace.DEFAULT).build();
registerTableSpace(defaultTableSpace);
return true;
} else {
return false;
}
} catch (DDLException err) {
throw new MetadataStorageManagerException(err);
} finally {
lock.writeLock().unlock();
}
}
use of herddb.model.TableSpace in project herddb by diennea.
the class ZookeeperMetadataStorageManagerTest method testSessionExpired.
@Test
public void testSessionExpired() throws Exception {
try (ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(), testEnv.getTimeout(), testEnv.getPath())) {
man.start();
TableSpace tableSpace = TableSpace.builder().leader("test").replica("test").name(TableSpace.DEFAULT).build();
man.registerTableSpace(tableSpace);
assertEquals(1, man.listTableSpaces().size());
ZooKeeper actual = man.getZooKeeper();
long sessionId = actual.getSessionId();
byte[] passwd = actual.getSessionPasswd();
expireZkSession(sessionId, passwd);
for (int i = 0; i < 10; i++) {
try {
man.listTableSpaces();
fail("session should be expired or not connected");
} catch (MetadataStorageManagerException ok) {
System.out.println("ok: " + ok);
assertTrue(ok.getCause() instanceof KeeperException.ConnectionLossException || ok.getCause() instanceof KeeperException.SessionExpiredException);
if (ok.getCause() instanceof KeeperException.SessionExpiredException) {
break;
}
}
Thread.sleep(500);
}
assertNotSame(actual, man.getZooKeeper());
assertEquals(1, man.listTableSpaces().size());
assertNotNull(tableSpace = man.describeTableSpace(TableSpace.DEFAULT));
man.dropTableSpace(TableSpace.DEFAULT, tableSpace);
}
}
use of herddb.model.TableSpace in project herddb by diennea.
the class TableSpaceManager method executeTableAwareStatement.
private CompletableFuture<StatementExecutionResult> executeTableAwareStatement(Statement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
long lockStamp = context.getTableSpaceLock();
boolean lockAcquired = false;
if (lockStamp == 0) {
lockStamp = acquireReadLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
TableAwareStatement st = (TableAwareStatement) statement;
String table = st.getTable();
AbstractTableManager manager = tables.get(table);
CompletableFuture<StatementExecutionResult> res;
if (manager == null) {
res = Futures.exception(new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName));
} else if (manager.getCreatedInTransaction() > 0 && (transaction == null || transaction.transactionId != manager.getCreatedInTransaction())) {
res = Futures.exception(new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName + ". created temporary in transaction " + manager.getCreatedInTransaction()));
} else {
res = manager.executeStatementAsync(statement, transaction, context);
}
if (lockAcquired) {
res = releaseReadLock(res, lockStamp, statement).whenComplete((s, err) -> {
context.setTableSpaceLock(0);
});
}
return res;
}
use of herddb.model.TableSpace in project herddb by diennea.
the class TableSpaceManager method recover.
void recover(TableSpace tableSpaceInfo) throws DataStorageManagerException, LogNotAvailableException, MetadataStorageManagerException {
if (recoveryInProgress) {
throw new HerdDBInternalException("Cannot run recovery twice");
}
recoveryInProgress = true;
LogSequenceNumber logSequenceNumber = dataStorageManager.getLastcheckpointSequenceNumber(tableSpaceUUID);
actualLogSequenceNumber = logSequenceNumber;
LOGGER.log(Level.INFO, "{0} recover {1}, logSequenceNumber from DataStorage: {2}", new Object[] { nodeId, tableSpaceName, logSequenceNumber });
List<Table> tablesAtBoot = dataStorageManager.loadTables(logSequenceNumber, tableSpaceUUID);
List<Index> indexesAtBoot = dataStorageManager.loadIndexes(logSequenceNumber, tableSpaceUUID);
String tableNames = tablesAtBoot.stream().map(t -> {
return t.name;
}).collect(Collectors.joining(","));
String indexNames = indexesAtBoot.stream().map(t -> {
return t.name + " on table " + t.table;
}).collect(Collectors.joining(","));
if (!tableNames.isEmpty()) {
LOGGER.log(Level.INFO, "{0} {1} tablesAtBoot: {2}, indexesAtBoot: {3}", new Object[] { nodeId, tableSpaceName, tableNames, indexNames });
}
for (Table table : tablesAtBoot) {
TableManager tableManager = bootTable(table, 0, null, false);
for (Index index : indexesAtBoot) {
if (index.table.equals(table.name)) {
bootIndex(index, tableManager, false, 0, false, false);
}
}
}
dataStorageManager.loadTransactions(logSequenceNumber, tableSpaceUUID, t -> {
transactions.put(t.transactionId, t);
LOGGER.log(Level.FINER, "{0} {1} tx {2} at boot lsn {3}", new Object[] { nodeId, tableSpaceName, t.transactionId, t.lastSequenceNumber });
try {
if (t.newTables != null) {
for (Table table : t.newTables.values()) {
if (!tables.containsKey(table.name)) {
bootTable(table, t.transactionId, null, false);
}
}
}
if (t.newIndexes != null) {
for (Index index : t.newIndexes.values()) {
if (!indexes.containsKey(index.name)) {
AbstractTableManager tableManager = tables.get(index.table);
bootIndex(index, tableManager, false, t.transactionId, false, false);
}
}
}
} catch (Exception err) {
LOGGER.log(Level.SEVERE, "error while booting tmp tables " + err, err);
throw new RuntimeException(err);
}
});
if (LogSequenceNumber.START_OF_TIME.equals(logSequenceNumber) && dbmanager.getServerConfiguration().getBoolean(ServerConfiguration.PROPERTY_BOOT_FORCE_DOWNLOAD_SNAPSHOT, ServerConfiguration.PROPERTY_BOOT_FORCE_DOWNLOAD_SNAPSHOT_DEFAULT)) {
LOGGER.log(Level.SEVERE, nodeId + " full recovery of data is forced (" + ServerConfiguration.PROPERTY_BOOT_FORCE_DOWNLOAD_SNAPSHOT + "=true) for tableSpace " + tableSpaceName);
downloadTableSpaceData();
log.recovery(actualLogSequenceNumber, new ApplyEntryOnRecovery(), false);
} else {
try {
log.recovery(logSequenceNumber, new ApplyEntryOnRecovery(), false);
} catch (FullRecoveryNeededException fullRecoveryNeeded) {
LOGGER.log(Level.SEVERE, nodeId + " full recovery of data is needed for tableSpace " + tableSpaceName, fullRecoveryNeeded);
downloadTableSpaceData();
log.recovery(actualLogSequenceNumber, new ApplyEntryOnRecovery(), false);
}
}
recoveryInProgress = false;
if (!LogSequenceNumber.START_OF_TIME.equals(actualLogSequenceNumber)) {
LOGGER.log(Level.INFO, "Recovery finished for {0} seqNum {1}", new Object[] { tableSpaceName, actualLogSequenceNumber });
checkpoint(false, false, false);
}
}
Aggregations