Search in sources :

Example 1 with StartupException

use of org.apache.cassandra.exceptions.StartupException in project cassandra by apache.

the class CassandraDaemon method setup.

/**
     * This is a hook for concrete daemons to initialize themselves suitably.
     *
     * Subclasses should override this to finish the job (listening on ports, etc.)
     */
protected void setup() {
    FileUtils.setFSErrorHandler(new DefaultFSErrorHandler());
    // Delete any failed snapshot deletions on Windows - see CASSANDRA-9658
    if (FBUtilities.isWindows)
        WindowsFailedSnapshotTracker.deleteOldSnapshots();
    maybeInitJmx();
    Mx4jTool.maybeLoad();
    ThreadAwareSecurityManager.install();
    logSystemInfo();
    CLibrary.tryMlockall();
    try {
        startupChecks.verify();
    } catch (StartupException e) {
        exitOrFail(e.returnCode, e.getMessage(), e.getCause());
    }
    // We need to persist this as soon as possible after startup checks.
    // This should be the first write to SystemKeyspace (CASSANDRA-11742)
    SystemKeyspace.persistLocalMetadata();
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        public void uncaughtException(Thread t, Throwable e) {
            StorageMetrics.exceptions.inc();
            logger.error("Exception in thread " + t, e);
            Tracing.trace("Exception in thread {}", t, e);
            for (Throwable e2 = e; e2 != null; e2 = e2.getCause()) {
                JVMStabilityInspector.inspectThrowable(e2);
                if (e2 instanceof FSError) {
                    if (// make sure FSError gets logged exactly once.
                    e2 != e)
                        logger.error("Exception in thread " + t, e2);
                    FileUtils.handleFSError((FSError) e2);
                }
                if (e2 instanceof CorruptSSTableException) {
                    if (e2 != e)
                        logger.error("Exception in thread " + t, e2);
                    FileUtils.handleCorruptSSTable((CorruptSSTableException) e2);
                }
            }
        }
    });
    // Populate token metadata before flushing, for token-aware sstable partitioning (#6696)
    StorageService.instance.populateTokenMetadata();
    // load schema from disk
    Schema.instance.loadFromDisk();
    // clean up debris in the rest of the keyspaces
    for (String keyspaceName : Schema.instance.getKeyspaces()) {
        // Skip system as we've already cleaned it
        if (keyspaceName.equals(SchemaConstants.SYSTEM_KEYSPACE_NAME))
            continue;
        for (TableMetadata cfm : Schema.instance.getTablesAndViews(keyspaceName)) {
            try {
                ColumnFamilyStore.scrubDataDirectories(cfm);
            } catch (StartupException e) {
                exitOrFail(e.returnCode, e.getMessage(), e.getCause());
            }
        }
    }
    Keyspace.setInitialized();
    // initialize keyspaces
    for (String keyspaceName : Schema.instance.getKeyspaces()) {
        if (logger.isDebugEnabled())
            logger.debug("opening keyspace {}", keyspaceName);
        // disable auto compaction until commit log replay ends
        for (ColumnFamilyStore cfs : Keyspace.open(keyspaceName).getColumnFamilyStores()) {
            for (ColumnFamilyStore store : cfs.concatWithIndexes()) {
                store.disableAutoCompaction();
            }
        }
    }
    try {
        loadRowAndKeyCacheAsync().get();
    } catch (Throwable t) {
        JVMStabilityInspector.inspectThrowable(t);
        logger.warn("Error loading key or row cache", t);
    }
    try {
        GCInspector.register();
    } catch (Throwable t) {
        JVMStabilityInspector.inspectThrowable(t);
        logger.warn("Unable to start GCInspector (currently only supported on the Sun JVM)");
    }
    // Replay any CommitLogSegments found on disk
    try {
        CommitLog.instance.recoverSegmentsOnDisk();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    // Re-populate token metadata after commit log recover (new peers might be loaded onto system keyspace #10293)
    StorageService.instance.populateTokenMetadata();
    // enable auto compaction
    for (Keyspace keyspace : Keyspace.all()) {
        for (ColumnFamilyStore cfs : keyspace.getColumnFamilyStores()) {
            for (final ColumnFamilyStore store : cfs.concatWithIndexes()) {
                if (store.getCompactionStrategyManager().shouldBeEnabled())
                    store.enableAutoCompaction();
            }
        }
    }
    SystemKeyspace.finishStartup();
    ActiveRepairService.instance.start();
    // Prepared statements
    QueryProcessor.preloadPreparedStatement();
    // Metrics
    String metricsReporterConfigFile = System.getProperty("cassandra.metricsReporterConfigFile");
    if (metricsReporterConfigFile != null) {
        logger.info("Trying to load metrics-reporter-config from file: {}", metricsReporterConfigFile);
        try {
            // enable metrics provided by metrics-jvm.jar
            CassandraMetricsRegistry.Metrics.register("jvm.buffers.", new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer()));
            CassandraMetricsRegistry.Metrics.register("jvm.gc.", new GarbageCollectorMetricSet());
            CassandraMetricsRegistry.Metrics.register("jvm.memory.", new MemoryUsageGaugeSet());
            CassandraMetricsRegistry.Metrics.register("jvm.fd.usage", new FileDescriptorRatioGauge());
            // initialize metrics-reporter-config from yaml file
            URL resource = CassandraDaemon.class.getClassLoader().getResource(metricsReporterConfigFile);
            if (resource == null) {
                logger.warn("Failed to load metrics-reporter-config, file does not exist: {}", metricsReporterConfigFile);
            } else {
                String reportFileLocation = resource.getFile();
                ReporterConfig.loadFromFile(reportFileLocation).enableAll(CassandraMetricsRegistry.Metrics);
            }
        } catch (Exception e) {
            logger.warn("Failed to load metrics-reporter-config, metric sinks will not be activated", e);
        }
    }
    // start server internals
    StorageService.instance.registerDaemon(this);
    try {
        StorageService.instance.initServer();
    } catch (ConfigurationException e) {
        System.err.println(e.getMessage() + "\nFatal configuration error; unable to start server.  See log for stacktrace.");
        exitOrFail(1, "Fatal configuration error", e);
    }
    // Because we are writing to the system_distributed keyspace, this should happen after that is created, which
    // happens in StorageService.instance.initServer()
    Runnable viewRebuild = () -> {
        for (Keyspace keyspace : Keyspace.all()) {
            keyspace.viewManager.buildAllViews();
        }
        logger.debug("Completed submission of build tasks for any materialized views defined at startup");
    };
    ScheduledExecutors.optionalTasks.schedule(viewRebuild, StorageService.RING_DELAY, TimeUnit.MILLISECONDS);
    if (!FBUtilities.getBroadcastAddress().equals(InetAddress.getLoopbackAddress()))
        Gossiper.waitToSettle();
    // schedule periodic background compaction task submission. this is simply a backstop against compactions stalling
    // due to scheduling errors or race conditions
    ScheduledExecutors.optionalTasks.scheduleWithFixedDelay(ColumnFamilyStore.getBackgroundCompactionTaskSubmitter(), 5, 1, TimeUnit.MINUTES);
    // schedule periodic dumps of table size estimates into SystemKeyspace.SIZE_ESTIMATES_CF
    // set cassandra.size_recorder_interval to 0 to disable
    int sizeRecorderInterval = Integer.getInteger("cassandra.size_recorder_interval", 5 * 60);
    if (sizeRecorderInterval > 0)
        ScheduledExecutors.optionalTasks.scheduleWithFixedDelay(SizeEstimatesRecorder.instance, 30, sizeRecorderInterval, TimeUnit.SECONDS);
    // Native transport
    nativeTransportService = new NativeTransportService();
    completeSetup();
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) BufferPoolMetricSet(com.codahale.metrics.jvm.BufferPoolMetricSet) FileDescriptorRatioGauge(com.codahale.metrics.jvm.FileDescriptorRatioGauge) FSError(org.apache.cassandra.io.FSError) IOException(java.io.IOException) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException) URL(java.net.URL) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException) StartupException(org.apache.cassandra.exceptions.StartupException) StartupException(org.apache.cassandra.exceptions.StartupException) MemoryUsageGaugeSet(com.codahale.metrics.jvm.MemoryUsageGaugeSet) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) GarbageCollectorMetricSet(com.codahale.metrics.jvm.GarbageCollectorMetricSet)

Example 2 with StartupException

use of org.apache.cassandra.exceptions.StartupException in project cassandra by apache.

the class ColumnFamilyStore method scrubDataDirectories.

/**
     * Removes unnecessary files from the cf directory at startup: these include temp files, orphans, zero-length files
     * and compacted sstables. Files that cannot be recognized will be ignored.
     */
public static void scrubDataDirectories(TableMetadata metadata) throws StartupException {
    Directories directories = new Directories(metadata, initialDirectories);
    Set<File> cleanedDirectories = new HashSet<>();
    // clear ephemeral snapshots that were not properly cleared last session (CASSANDRA-7357)
    clearEphemeralSnapshots(directories);
    directories.removeTemporaryDirectories();
    logger.trace("Removing temporary or obsoleted files from unfinished operations for table {}", metadata.name);
    if (!LifecycleTransaction.removeUnfinishedLeftovers(metadata))
        throw new StartupException(StartupException.ERR_WRONG_DISK_STATE, String.format("Cannot remove temporary or obsoleted files for %s due to a problem with transaction " + "log files. Please check records with problems in the log messages above and fix them. " + "Refer to the 3.0 upgrading instructions in NEWS.txt " + "for a description of transaction log files.", metadata.toString()));
    logger.trace("Further extra check for orphan sstable files for {}", metadata.name);
    for (Map.Entry<Descriptor, Set<Component>> sstableFiles : directories.sstableLister(Directories.OnTxnErr.IGNORE).list().entrySet()) {
        Descriptor desc = sstableFiles.getKey();
        File directory = desc.directory;
        Set<Component> components = sstableFiles.getValue();
        if (!cleanedDirectories.contains(directory)) {
            cleanedDirectories.add(directory);
            for (File tmpFile : desc.getTemporaryFiles()) tmpFile.delete();
        }
        File dataFile = new File(desc.filenameFor(Component.DATA));
        if (components.contains(Component.DATA) && dataFile.length() > 0)
            // everything appears to be in order... moving on.
            continue;
        // missing the DATA file! all components are orphaned
        logger.warn("Removing orphans for {}: {}", desc, components);
        for (Component component : components) {
            File file = new File(desc.filenameFor(component));
            if (file.exists())
                FileUtils.deleteWithConfirm(desc.filenameFor(component));
        }
    }
    // cleanup incomplete saved caches
    Pattern tmpCacheFilePattern = Pattern.compile(metadata.keyspace + "-" + metadata.name + "-(Key|Row)Cache.*\\.tmp$");
    File dir = new File(DatabaseDescriptor.getSavedCachesLocation());
    if (dir.exists()) {
        assert dir.isDirectory();
        for (File file : dir.listFiles()) if (tmpCacheFilePattern.matcher(file.getName()).matches())
            if (!file.delete())
                logger.warn("could not delete {}", file.getAbsolutePath());
    }
    // also clean out any index leftovers.
    for (IndexMetadata index : metadata.indexes) if (!index.isCustom()) {
        TableMetadata indexMetadata = CassandraIndex.indexCfsMetadata(metadata, index);
        scrubDataDirectories(indexMetadata);
    }
}
Also used : Pattern(java.util.regex.Pattern) StartupException(org.apache.cassandra.exceptions.StartupException) Descriptor(org.apache.cassandra.io.sstable.Descriptor) Component(org.apache.cassandra.io.sstable.Component) File(java.io.File)

Aggregations

StartupException (org.apache.cassandra.exceptions.StartupException)2 BufferPoolMetricSet (com.codahale.metrics.jvm.BufferPoolMetricSet)1 FileDescriptorRatioGauge (com.codahale.metrics.jvm.FileDescriptorRatioGauge)1 GarbageCollectorMetricSet (com.codahale.metrics.jvm.GarbageCollectorMetricSet)1 MemoryUsageGaugeSet (com.codahale.metrics.jvm.MemoryUsageGaugeSet)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 Pattern (java.util.regex.Pattern)1 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)1 FSError (org.apache.cassandra.io.FSError)1 Component (org.apache.cassandra.io.sstable.Component)1 CorruptSSTableException (org.apache.cassandra.io.sstable.CorruptSSTableException)1 Descriptor (org.apache.cassandra.io.sstable.Descriptor)1 TableMetadata (org.apache.cassandra.schema.TableMetadata)1