Search in sources :

Example 1 with TableSpaceReplicaState

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;
}
Also used : TableSpace(herddb.model.TableSpace) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataStorageManagerException(herddb.storage.DataStorageManagerException) LogNotAvailableException(herddb.log.LogNotAvailableException) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) DataScannerException(herddb.model.DataScannerException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) TableSpaceReplicaState(herddb.model.TableSpaceReplicaState) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DDLException(herddb.model.DDLException) HashSet(java.util.HashSet)

Example 2 with TableSpaceReplicaState

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);
    }
}
Also used : TableSpace(herddb.model.TableSpace) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) StatementExecutionException(herddb.model.StatementExecutionException) DataStorageManagerException(herddb.storage.DataStorageManagerException) LogNotAvailableException(herddb.log.LogNotAvailableException) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) DataScannerException(herddb.model.DataScannerException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) TableSpaceReplicaState(herddb.model.TableSpaceReplicaState) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager)

Example 3 with TableSpaceReplicaState

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);
    }
}
Also used : TableSpace(herddb.model.TableSpace) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) Record(herddb.model.Record) TableSpaceReplicaState(herddb.model.TableSpaceReplicaState)

Example 4 with TableSpaceReplicaState

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);
    }
}
Also used : MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) ArrayList(java.util.ArrayList) TableSpaceReplicaState(herddb.model.TableSpaceReplicaState) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

MetadataStorageManagerException (herddb.metadata.MetadataStorageManagerException)4 TableSpaceReplicaState (herddb.model.TableSpaceReplicaState)4 StatementExecutionException (herddb.model.StatementExecutionException)3 TableSpace (herddb.model.TableSpace)3 ArrayList (java.util.ArrayList)3 LogNotAvailableException (herddb.log.LogNotAvailableException)2 DDLException (herddb.model.DDLException)2 DataScannerException (herddb.model.DataScannerException)2 NotLeaderException (herddb.model.NotLeaderException)2 TableSpaceDoesNotExistException (herddb.model.TableSpaceDoesNotExistException)2 DataStorageManagerException (herddb.storage.DataStorageManagerException)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)1 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)1 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)1 Record (herddb.model.Record)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1