Search in sources :

Example 6 with TableSpace

use of herddb.model.TableSpace in project herddb by diennea.

the class DBManager method manageTableSpaces.

private boolean manageTableSpaces() {
    Collection<String> actualTablesSpaces;
    try {
        // all lowercase names
        actualTablesSpaces = metadataStorageManager.listTableSpaces();
    } catch (MetadataStorageManagerException error) {
        LOGGER.log(Level.SEVERE, "cannot access tablespaces metadata", error);
        return true;
    }
    Map<String, TableSpace> currentTableSpaceMetadata = new HashMap<>();
    generalLock.writeLock().lock();
    try {
        for (String tableSpace : actualTablesSpaces) {
            TableSpace tableSpaceMetadata = metadataStorageManager.describeTableSpace(tableSpace);
            if (tableSpaceMetadata == null) {
                LOGGER.log(Level.INFO, "tablespace {0} does not exist", tableSpace);
                continue;
            }
            currentTableSpaceMetadata.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()) {
                LOGGER.log(Level.SEVERE, "tablespace " + tableSpaceUuid + " failed");
                failedTableSpaces.add(entry.getKey());
            } else if (!entry.getKey().equals(virtualTableSpaceId) && !actualTablesSpaces.contains(entry.getKey().toLowerCase())) {
                LOGGER.log(Level.SEVERE, "tablespace " + tableSpaceUuid + " should not run here");
                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 = currentTableSpaceMetadata.get(tableSpaceUuid);
                if (tableSpaceInfo != null && !tableSpaceInfo.leaderId.equals(nodeId) && tableSpaceInfo.maxLeaderInactivityTime > 0 && !tableSpaceManager.isFailed()) {
                    List<TableSpaceReplicaState> allReplicas = metadataStorageManager.getTableSpaceReplicaState(tableSpaceUuid);
                    TableSpaceReplicaState leaderState = allReplicas.stream().filter(t -> t.nodeId.equals(tableSpaceInfo.leaderId)).findAny().orElse(null);
                    if (leaderState == null) {
                        leaderState = new TableSpaceReplicaState(tableSpaceUuid, tableSpaceInfo.leaderId, tableSpaceInfo.metadataStorageCreationTime, TableSpaceReplicaState.MODE_STOPPED);
                        LOGGER.log(Level.INFO, "Leader for " + tableSpaceUuid + " should be " + tableSpaceInfo.leaderId + ", but it never sent pings or it disappeared," + " considering last activity as tablespace creation time: " + new java.sql.Timestamp(tableSpaceInfo.metadataStorageCreationTime) + " to leave a minimal grace period");
                    }
                    long delta = now - leaderState.timestamp;
                    if (tableSpaceInfo.maxLeaderInactivityTime > delta) {
                        LOGGER.log(Level.FINE, "Leader for " + tableSpaceUuid + " is " + tableSpaceInfo.leaderId + ", last ping " + new java.sql.Timestamp(leaderState.timestamp) + ". leader is healty");
                    } else {
                        LOGGER.log(Level.SEVERE, "Leader for " + tableSpaceUuid + " is " + tableSpaceInfo.leaderId + ", last ping " + new java.sql.Timestamp(leaderState.timestamp) + ". leader is failed.");
                        if (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) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) 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 7 with TableSpace

use of herddb.model.TableSpace 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);
    }
}
Also used : TableSpace(herddb.model.TableSpace) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) 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) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatementExecutionException(herddb.model.StatementExecutionException)

Example 8 with TableSpace

use of herddb.model.TableSpace 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);
    }
}
Also used : ExecutionPlan(herddb.model.ExecutionPlan) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) ServerSidePreparedStatementCache(herddb.server.ServerSidePreparedStatementCache) Table(herddb.model.Table) JMXUtils(herddb.jmx.JMXUtils) ClientConfiguration(herddb.client.ClientConfiguration) MetadataStorageManager(herddb.metadata.MetadataStorageManager) Channel(herddb.network.Channel) ServerConfiguration(herddb.server.ServerConfiguration) FastThreadLocalThread(io.netty.util.concurrent.FastThreadLocalThread) Map(java.util.Map) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) ConnectionsInfoProvider(herddb.core.stats.ConnectionsInfoProvider) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) ThreadFactory(java.util.concurrent.ThreadFactory) Futures(herddb.utils.Futures) Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) DropTableSpaceStatement(herddb.model.commands.DropTableSpaceStatement) ScanResult(herddb.model.ScanResult) LogNotAvailableException(herddb.log.LogNotAvailableException) PduCodec(herddb.proto.PduCodec) NullStatsLogger(org.apache.bookkeeper.stats.NullStatsLogger) DataScanner(herddb.model.DataScanner) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) AbstractSQLPlanner(herddb.sql.AbstractSQLPlanner) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LogSequenceNumber(herddb.log.LogSequenceNumber) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ScanStatement(herddb.model.commands.ScanStatement) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) NotLeaderException(herddb.model.NotLeaderException) GetStatement(herddb.model.commands.GetStatement) NodeMetadata(herddb.model.NodeMetadata) TranslatedQuery(herddb.sql.TranslatedQuery) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) DefaultJVMHalt(herddb.utils.DefaultJVMHalt) DDLStatement(herddb.model.DDLStatement) DataConsistencyStatementResult(herddb.model.DataConsistencyStatementResult) TableSpace(herddb.model.TableSpace) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Statement(herddb.model.Statement) AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) CalcitePlanner(herddb.sql.CalcitePlanner) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) DataScannerException(herddb.model.DataScannerException) MetadataChangeListener(herddb.metadata.MetadataChangeListener) GetResult(herddb.model.GetResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) TableSpaceReplicaState(herddb.model.TableSpaceReplicaState) Pdu(herddb.proto.Pdu) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ByteBuf(io.netty.buffer.ByteBuf) TransactionContext(herddb.model.TransactionContext) CommitLogManager(herddb.log.CommitLogManager) ManagementFactory(java.lang.management.ManagementFactory) ExecutorService(java.util.concurrent.ExecutorService) ReentrantLock(java.util.concurrent.locks.ReentrantLock) StatementExecutionResult(herddb.model.StatementExecutionResult) IOException(java.io.IOException) DMLStatement(herddb.model.DMLStatement) DataStorageManager(herddb.storage.DataStorageManager) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DBManagerStatsMXBean(herddb.jmx.DBManagerStatsMXBean) AtomicLong(java.util.concurrent.atomic.AtomicLong) CommitLog(herddb.log.CommitLog) Condition(java.util.concurrent.locks.Condition) Lock(java.util.concurrent.locks.Lock) TableConsistencyCheckStatement(herddb.model.commands.TableConsistencyCheckStatement) JSQLParserPlanner(herddb.sql.JSQLParserPlanner) StatementEvaluationContext(herddb.model.StatementEvaluationContext) NullSQLPlanner(herddb.sql.NullSQLPlanner) PlansCache(herddb.sql.PlansCache) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) TableSpaceConsistencyCheckStatement(herddb.model.commands.TableSpaceConsistencyCheckStatement) ServerHostData(herddb.network.ServerHostData) DataStorageManagerException(herddb.storage.DataStorageManagerException) TableSpace(herddb.model.TableSpace) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) NodeMetadata(herddb.model.NodeMetadata) CommitLog(herddb.log.CommitLog) DDLException(herddb.model.DDLException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 9 with TableSpace

use of herddb.model.TableSpace in project herddb by diennea.

the class DBManager method waitForBootOfLocalTablespaces.

public boolean waitForBootOfLocalTablespaces(int millis) throws InterruptedException, MetadataStorageManagerException {
    List<String> tableSpacesToWaitFor = new ArrayList<>();
    Collection<String> allTableSpaces = metadataStorageManager.listTableSpaces();
    for (String tableSpaceName : allTableSpaces) {
        TableSpace tableSpace = metadataStorageManager.describeTableSpace(tableSpaceName);
        if (tableSpace.leaderId.equals(nodeId)) {
            tableSpacesToWaitFor.add(tableSpaceName);
        }
    }
    LOGGER.log(Level.INFO, "Waiting (max " + millis + " ms) for boot of local tablespaces: " + tableSpacesToWaitFor);
    for (String tableSpace : tableSpacesToWaitFor) {
        boolean ok = waitForTablespace(tableSpace, millis, true);
        if (!ok) {
            return false;
        }
    }
    return true;
}
Also used : TableSpace(herddb.model.TableSpace) ArrayList(java.util.ArrayList)

Example 10 with TableSpace

use of herddb.model.TableSpace in project herddb by diennea.

the class SystablespacesTableManager method buildVirtualRecordList.

@Override
protected Iterable<Record> buildVirtualRecordList(Transaction transaction) throws StatementExecutionException {
    try {
        Collection<String> names = tableSpaceManager.getMetadataStorageManager().listTableSpaces();
        List<Record> result = new ArrayList<>();
        for (String name : names) {
            TableSpace t = tableSpaceManager.getMetadataStorageManager().describeTableSpace(name);
            if (t != null) {
                result.add(RecordSerializer.makeRecord(table, "tablespace_name", t.name, "uuid", t.uuid, "leader", t.leaderId, "expectedreplicacount", t.expectedReplicaCount, "maxleaderinactivitytime", t.maxLeaderInactivityTime, "replica", t.replicas.stream().collect(Collectors.joining(","))));
            }
        }
        return result;
    } catch (MetadataStorageManagerException error) {
        throw new StatementExecutionException(error);
    }
}
Also used : MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) TableSpace(herddb.model.TableSpace) ArrayList(java.util.ArrayList) Record(herddb.model.Record) StatementExecutionException(herddb.model.StatementExecutionException)

Aggregations

TableSpace (herddb.model.TableSpace)37 MetadataStorageManagerException (herddb.metadata.MetadataStorageManagerException)22 StatementExecutionException (herddb.model.StatementExecutionException)15 DDLException (herddb.model.DDLException)13 IOException (java.io.IOException)13 DataScanner (herddb.model.DataScanner)11 DataScannerException (herddb.model.DataScannerException)11 ArrayList (java.util.ArrayList)11 TableSpaceDoesNotExistException (herddb.model.TableSpaceDoesNotExistException)10 LogNotAvailableException (herddb.log.LogNotAvailableException)9 DataStorageManagerException (herddb.storage.DataStorageManagerException)9 ExecutionException (java.util.concurrent.ExecutionException)9 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)8 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)7 Test (org.junit.Test)6 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)5 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)5 DataAccessor (herddb.utils.DataAccessor)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)4