Search in sources :

Example 1 with CommitLog

use of org.apache.cassandra.db.commitlog.CommitLog in project cassandra by apache.

the class StorageService method drain.

protected synchronized void drain(boolean isFinalShutdown) throws IOException, InterruptedException, ExecutionException {
    ExecutorService counterMutationStage = StageManager.getStage(Stage.COUNTER_MUTATION);
    ExecutorService viewMutationStage = StageManager.getStage(Stage.VIEW_MUTATION);
    ExecutorService mutationStage = StageManager.getStage(Stage.MUTATION);
    if (mutationStage.isTerminated() && counterMutationStage.isTerminated() && viewMutationStage.isTerminated()) {
        if (!isFinalShutdown)
            logger.warn("Cannot drain node (did it already happen?)");
        return;
    }
    assert !isShutdown;
    isShutdown = true;
    Throwable preShutdownHookThrowable = Throwables.perform(null, preShutdownHooks.stream().map(h -> h::run));
    if (preShutdownHookThrowable != null)
        logger.error("Attempting to continue draining after pre-shutdown hooks returned exception", preShutdownHookThrowable);
    try {
        setMode(Mode.DRAINING, "starting drain process", !isFinalShutdown);
        BatchlogManager.instance.shutdown();
        HintsService.instance.pauseDispatch();
        if (daemon != null)
            shutdownClientServers();
        ScheduledExecutors.optionalTasks.shutdown();
        Gossiper.instance.stop();
        if (!isFinalShutdown)
            setMode(Mode.DRAINING, "shutting down MessageService", false);
        // In-progress writes originating here could generate hints to be written, so shut down MessagingService
        // before mutation stage, so we can get all the hints saved before shutting down
        MessagingService.instance().shutdown();
        if (!isFinalShutdown)
            setMode(Mode.DRAINING, "clearing mutation stage", false);
        viewMutationStage.shutdown();
        counterMutationStage.shutdown();
        mutationStage.shutdown();
        viewMutationStage.awaitTermination(3600, TimeUnit.SECONDS);
        counterMutationStage.awaitTermination(3600, TimeUnit.SECONDS);
        mutationStage.awaitTermination(3600, TimeUnit.SECONDS);
        StorageProxy.instance.verifyNoHintsInProgress();
        if (!isFinalShutdown)
            setMode(Mode.DRAINING, "flushing column families", false);
        // disable autocompaction - we don't want to start any new compactions while we are draining
        for (Keyspace keyspace : Keyspace.all()) for (ColumnFamilyStore cfs : keyspace.getColumnFamilyStores()) cfs.disableAutoCompaction();
        // count CFs first, since forceFlush could block for the flushWriter to get a queue slot empty
        totalCFs = 0;
        for (Keyspace keyspace : Keyspace.nonSystem()) totalCFs += keyspace.getColumnFamilyStores().size();
        remainingCFs = totalCFs;
        // flush
        List<Future<?>> flushes = new ArrayList<>();
        for (Keyspace keyspace : Keyspace.nonSystem()) {
            for (ColumnFamilyStore cfs : keyspace.getColumnFamilyStores()) flushes.add(cfs.forceFlush());
        }
        // thus make several short ones "instant" if we wait for them later.
        for (Future f : flushes) {
            try {
                FBUtilities.waitOnFuture(f);
            } catch (Throwable t) {
                JVMStabilityInspector.inspectThrowable(t);
                // don't let this stop us from shutting down the commitlog and other thread pools
                logger.warn("Caught exception while waiting for memtable flushes during shutdown hook", t);
            }
            remainingCFs--;
        }
        // Interrupt ongoing compactions and shutdown CM to prevent further compactions.
        CompactionManager.instance.forceShutdown();
        // Flush the system tables after all other tables are flushed, just in case flushing modifies any system state
        // like CASSANDRA-5151. Don't bother with progress tracking since system data is tiny.
        // Flush system tables after stopping compactions since they modify
        // system tables (for example compactions can obsolete sstables and the tidiers in SSTableReader update
        // system tables, see SSTableReader.GlobalTidy)
        flushes.clear();
        for (Keyspace keyspace : Keyspace.system()) {
            for (ColumnFamilyStore cfs : keyspace.getColumnFamilyStores()) flushes.add(cfs.forceFlush());
        }
        FBUtilities.waitOnFutures(flushes);
        HintsService.instance.shutdownBlocking();
        // Interrupt ongoing compactions and shutdown CM to prevent further compactions.
        CompactionManager.instance.forceShutdown();
        // whilst we've flushed all the CFs, which will have recycled all completed segments, we want to ensure
        // there are no segments to replay, so we force the recycling of any remaining (should be at most one)
        CommitLog.instance.forceRecycleAllSegments();
        CommitLog.instance.shutdownBlocking();
        // wait for miscellaneous tasks like sstable and commitlog segment deletion
        ScheduledExecutors.nonPeriodicTasks.shutdown();
        if (!ScheduledExecutors.nonPeriodicTasks.awaitTermination(1, TimeUnit.MINUTES))
            logger.warn("Failed to wait for non periodic tasks to shutdown");
        ColumnFamilyStore.shutdownPostFlushExecutor();
        setMode(Mode.DRAINED, !isFinalShutdown);
    } catch (Throwable t) {
        logger.error("Caught an exception while draining ", t);
    } finally {
        Throwable postShutdownHookThrowable = Throwables.perform(null, postShutdownHooks.stream().map(h -> h::run));
        if (postShutdownHookThrowable != null)
            logger.error("Post-shutdown hooks returned exception", postShutdownHookThrowable);
    }
}
Also used : TraceKeyspace(org.apache.cassandra.tracing.TraceKeyspace) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) ScheduledExecutors(org.apache.cassandra.concurrent.ScheduledExecutors) StorageMetrics(org.apache.cassandra.metrics.StorageMetrics) org.apache.cassandra.repair(org.apache.cassandra.repair) Stage(org.apache.cassandra.concurrent.Stage) org.apache.cassandra.streaming(org.apache.cassandra.streaming) ProposeVerbHandler(org.apache.cassandra.service.paxos.ProposeVerbHandler) MatchResult(java.util.regex.MatchResult) LoggerFactory(org.slf4j.LoggerFactory) org.apache.cassandra.db(org.apache.cassandra.db) CommitLog(org.apache.cassandra.db.commitlog.CommitLog) org.apache.cassandra.gms(org.apache.cassandra.gms) com.google.common.util.concurrent(com.google.common.util.concurrent) JMXConfiguratorMBean(ch.qos.logback.classic.jmx.JMXConfiguratorMBean) AuthKeyspace(org.apache.cassandra.auth.AuthKeyspace) TabularData(javax.management.openmbean.TabularData) org.apache.cassandra.utils(org.apache.cassandra.utils) StringUtils(org.apache.commons.lang3.StringUtils) ByteBuffer(java.nio.ByteBuffer) TokenFactory(org.apache.cassandra.dht.Token.TokenFactory) InetAddress(java.net.InetAddress) MigrationManager(org.apache.cassandra.schema.MigrationManager) SecondaryIndexManager.isIndexColumnFamily(org.apache.cassandra.index.SecondaryIndexManager.isIndexColumnFamily) SecondaryIndexManager.getIndexName(org.apache.cassandra.index.SecondaryIndexManager.getIndexName) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) HintsService(org.apache.cassandra.hints.HintsService) AuthSchemaChangeListener(org.apache.cassandra.auth.AuthSchemaChangeListener) com.google.common.collect(com.google.common.collect) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) java.util.concurrent(java.util.concurrent) TombstoneOption(org.apache.cassandra.schema.CompactionParams.TombstoneOption) JMXProgressSupport(org.apache.cassandra.utils.progress.jmx.JMXProgressSupport) SchemaVersionVerbHandler(org.apache.cassandra.schema.SchemaVersionVerbHandler) org.apache.cassandra.dht(org.apache.cassandra.dht) ViewMetadata(org.apache.cassandra.schema.ViewMetadata) PrepareVerbHandler(org.apache.cassandra.service.paxos.PrepareVerbHandler) NamedThreadFactory(org.apache.cassandra.concurrent.NamedThreadFactory) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) Predicate(com.google.common.base.Predicate) org.apache.cassandra.exceptions(org.apache.cassandra.exceptions) FileUtils(org.apache.cassandra.io.util.FileUtils) TableMetadataRef(org.apache.cassandra.schema.TableMetadataRef) TabularDataSupport(javax.management.openmbean.TabularDataSupport) BatchRemoveVerbHandler(org.apache.cassandra.batchlog.BatchRemoveVerbHandler) Entry(java.util.Map.Entry) TableMetadata(org.apache.cassandra.schema.TableMetadata) Pattern(java.util.regex.Pattern) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) SchemaConstants(org.apache.cassandra.schema.SchemaConstants) ProgressEvent(org.apache.cassandra.utils.progress.ProgressEvent) HintVerbHandler(org.apache.cassandra.hints.HintVerbHandler) java.util(java.util) CompactionManager(org.apache.cassandra.db.compaction.CompactionManager) SSTableLoader(org.apache.cassandra.io.sstable.SSTableLoader) RepairOption(org.apache.cassandra.repair.messages.RepairOption) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Range(org.apache.cassandra.dht.Range) LoggerContext(ch.qos.logback.classic.LoggerContext) Schema(org.apache.cassandra.schema.Schema) CommitVerbHandler(org.apache.cassandra.service.paxos.CommitVerbHandler) Appender(ch.qos.logback.core.Appender) BatchlogManager(org.apache.cassandra.batchlog.BatchlogManager) StreamSupport(java.util.stream.StreamSupport) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) ManagementFactory(java.lang.management.ManagementFactory) SchemaPushVerbHandler(org.apache.cassandra.schema.SchemaPushVerbHandler) ProgressEventType(org.apache.cassandra.utils.progress.ProgressEventType) Nullable(javax.annotation.Nullable) SchemaPullVerbHandler(org.apache.cassandra.schema.SchemaPullVerbHandler) Logger(org.slf4j.Logger) LegacyJMXProgressSupport(org.apache.cassandra.utils.progress.jmx.LegacyJMXProgressSupport) DelayingShutdownHook(ch.qos.logback.core.hook.DelayingShutdownHook) javax.management(javax.management) UnknownHostException(java.net.UnknownHostException) Collectors.toList(java.util.stream.Collectors.toList) BatchStoreVerbHandler(org.apache.cassandra.batchlog.BatchStoreVerbHandler) java.io(java.io) StageManager(org.apache.cassandra.concurrent.StageManager) org.apache.cassandra.net(org.apache.cassandra.net) VisibleForTesting(com.google.common.annotations.VisibleForTesting) org.apache.cassandra.locator(org.apache.cassandra.locator) TraceKeyspace(org.apache.cassandra.tracing.TraceKeyspace) AuthKeyspace(org.apache.cassandra.auth.AuthKeyspace)

Aggregations

LoggerContext (ch.qos.logback.classic.LoggerContext)1 JMXConfiguratorMBean (ch.qos.logback.classic.jmx.JMXConfiguratorMBean)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 Appender (ch.qos.logback.core.Appender)1 DelayingShutdownHook (ch.qos.logback.core.hook.DelayingShutdownHook)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Predicate (com.google.common.base.Predicate)1 com.google.common.collect (com.google.common.collect)1 com.google.common.util.concurrent (com.google.common.util.concurrent)1 java.io (java.io)1 ManagementFactory (java.lang.management.ManagementFactory)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 ByteBuffer (java.nio.ByteBuffer)1 java.util (java.util)1 Arrays.asList (java.util.Arrays.asList)1 Entry (java.util.Map.Entry)1 java.util.concurrent (java.util.concurrent)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1