use of herddb.model.TableSpaceReplicaState in project herddb by diennea.
the class DBManager method manageTableSpaces.
private boolean manageTableSpaces() {
Collection<String> actualTablesSpaces;
try {
actualTablesSpaces = metadataStorageManager.listTableSpaces();
} catch (MetadataStorageManagerException error) {
LOGGER.log(Level.SEVERE, "cannot access tablespaces metadata", error);
return true;
}
Map<String, TableSpace> actualTableSpaceMetadata = new HashMap<>();
generalLock.writeLock().lock();
try {
for (String tableSpace : actualTablesSpaces) {
TableSpace tableSpaceMetadata = metadataStorageManager.describeTableSpace(tableSpace);
actualTableSpaceMetadata.put(tableSpaceMetadata.uuid, tableSpaceMetadata);
try {
handleTableSpace(tableSpaceMetadata);
} catch (Exception err) {
LOGGER.log(Level.SEVERE, "cannot handle tablespace " + tableSpace, err);
if (haltOnTableSpaceBootError && haltProcedure != null) {
err.printStackTrace();
haltProcedure.run();
}
}
}
} catch (MetadataStorageManagerException error) {
LOGGER.log(Level.SEVERE, "cannot access tablespaces metadata", error);
return true;
} finally {
generalLock.writeLock().unlock();
}
List<TableSpaceManager> followingActiveTableSpaces = new ArrayList<>();
Set<String> failedTableSpaces = new HashSet<>();
for (Map.Entry<String, TableSpaceManager> entry : tablesSpaces.entrySet()) {
try {
String tableSpaceUuid = entry.getValue().getTableSpaceUUID();
if (entry.getValue().isFailed()) {
failedTableSpaces.add(entry.getKey());
} else if (!entry.getKey().equals(virtualTableSpaceId) && !actualTablesSpaces.contains(entry.getKey())) {
failedTableSpaces.add(entry.getKey());
} else if (entry.getValue().isLeader()) {
metadataStorageManager.updateTableSpaceReplicaState(TableSpaceReplicaState.builder().mode(TableSpaceReplicaState.MODE_LEADER).nodeId(nodeId).uuid(tableSpaceUuid).timestamp(System.currentTimeMillis()).build());
} else {
metadataStorageManager.updateTableSpaceReplicaState(TableSpaceReplicaState.builder().mode(TableSpaceReplicaState.MODE_FOLLOWER).nodeId(nodeId).uuid(tableSpaceUuid).timestamp(System.currentTimeMillis()).build());
followingActiveTableSpaces.add(entry.getValue());
}
} catch (MetadataStorageManagerException error) {
LOGGER.log(Level.SEVERE, "cannot access tablespace " + entry.getKey() + " metadata", error);
return true;
}
}
if (!failedTableSpaces.isEmpty()) {
generalLock.writeLock().lock();
try {
for (String tableSpace : failedTableSpaces) {
stopTableSpace(tableSpace, null);
}
} catch (MetadataStorageManagerException error) {
LOGGER.log(Level.SEVERE, "cannot access tablespace metadata", error);
return true;
} finally {
generalLock.writeLock().unlock();
}
}
if (!followingActiveTableSpaces.isEmpty()) {
long now = System.currentTimeMillis();
try {
for (TableSpaceManager tableSpaceManager : followingActiveTableSpaces) {
String tableSpaceUuid = tableSpaceManager.getTableSpaceUUID();
TableSpace tableSpaceInfo = actualTableSpaceMetadata.get(tableSpaceUuid);
if (tableSpaceInfo != null && tableSpaceInfo.maxLeaderInactivityTime > 0 && !tableSpaceManager.isFailed()) {
List<TableSpaceReplicaState> allReplicas = metadataStorageManager.getTableSpaceReplicaState(tableSpaceUuid);
TableSpaceReplicaState leaderState = allReplicas.stream().filter(t -> t.mode == TableSpaceReplicaState.MODE_LEADER && !t.nodeId.equals(nodeId)).findAny().orElse(null);
if (leaderState == null) {
LOGGER.log(Level.SEVERE, "Leader for " + tableSpaceUuid + " should be " + tableSpaceInfo.leaderId + ", but it never sent pings or it disappeared");
tryBecomeLeaderFor(tableSpaceInfo);
} else {
long delta = now - leaderState.timestamp;
if (tableSpaceInfo.maxLeaderInactivityTime > delta) {
LOGGER.log(Level.FINER, "Leader for " + tableSpaceUuid + " is " + leaderState.nodeId + ", last ping " + new java.sql.Timestamp(leaderState.timestamp) + ". leader is healty");
} else {
LOGGER.log(Level.SEVERE, "Leader for " + tableSpaceUuid + " is " + leaderState.nodeId + ", last ping " + new java.sql.Timestamp(leaderState.timestamp) + ". leader is failed. trying to take leadership");
tryBecomeLeaderFor(tableSpaceInfo);
// only one change at a time
break;
}
}
}
}
} catch (MetadataStorageManagerException | DDLException error) {
LOGGER.log(Level.SEVERE, "cannot access tablespace metadata", error);
return true;
}
}
return false;
}
use of herddb.model.TableSpaceReplicaState 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.TableSpaceReplicaState in project herddb by diennea.
the class SystablespacereplicastateTableManager method buildVirtualRecordList.
@Override
protected Iterable<Record> buildVirtualRecordList() 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.TableSpaceReplicaState in project herddb by diennea.
the class ZookeeperMetadataStorageManager method getTableSpaceReplicaState.
@Override
public List<TableSpaceReplicaState> getTableSpaceReplicaState(String tableSpaceUuid) throws MetadataStorageManagerException {
try {
List<String> children;
try {
children = ensureZooKeeper().getChildren(tableSpacesReplicasPath + "/" + tableSpaceUuid, false);
} catch (KeeperException.NoNodeException err) {
return Collections.emptyList();
}
List<TableSpaceReplicaState> result = new ArrayList<>();
for (String child : children) {
String path = tableSpacesReplicasPath + "/" + tableSpaceUuid + "/" + child;
try {
byte[] data = ensureZooKeeper().getData(path, false, null);
TableSpaceReplicaState nodeMetadata = TableSpaceReplicaState.deserialize(data);
result.add(nodeMetadata);
} catch (IOException deserializeError) {
LOGGER.log(Level.SEVERE, "error reading " + path, deserializeError);
}
}
return result;
} catch (KeeperException | InterruptedException | IOException err) {
handleSessionExpiredError(err);
throw new MetadataStorageManagerException(err);
}
}
Aggregations