Search in sources :

Example 1 with FSError

use of org.apache.cassandra.io.FSError 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 FSError

use of org.apache.cassandra.io.FSError in project cassandra by apache.

the class PerSSTableIndexWriterTest method testSparse.

@Test
public void testSparse() throws Exception {
    final String columnName = "timestamp";
    ColumnFamilyStore cfs = Keyspace.open(KS_NAME).getColumnFamilyStore(CF_NAME);
    ColumnMetadata column = cfs.metadata().getColumn(UTF8Type.instance.decompose(columnName));
    SASIIndex sasi = (SASIIndex) cfs.indexManager.getIndexByName(cfs.name + "_" + columnName);
    File directory = cfs.getDirectories().getDirectoryForNewSSTables();
    Descriptor descriptor = cfs.newSSTableDescriptor(directory);
    PerSSTableIndexWriter indexWriter = (PerSSTableIndexWriter) sasi.getFlushObserver(descriptor, OperationType.FLUSH);
    final long now = System.currentTimeMillis();
    indexWriter.begin();
    indexWriter.indexes.put(column, indexWriter.newIndex(sasi.getIndex()));
    populateSegment(cfs.metadata(), indexWriter.getIndex(column), new HashMap<Long, Set<Integer>>() {

        {
            put(now, new HashSet<>(Arrays.asList(0, 1)));
            put(now + 1, new HashSet<>(Arrays.asList(2, 3)));
            put(now + 2, new HashSet<>(Arrays.asList(4, 5, 6, 7, 8, 9)));
        }
    });
    Callable<OnDiskIndex> segmentBuilder = indexWriter.getIndex(column).scheduleSegmentFlush(false);
    Assert.assertNull(segmentBuilder.call());
    PerSSTableIndexWriter.Index index = indexWriter.getIndex(column);
    Random random = ThreadLocalRandom.current();
    Set<String> segments = new HashSet<>();
    // now let's test multiple correct segments with yield incorrect final segment
    for (int i = 0; i < 3; i++) {
        populateSegment(cfs.metadata(), index, new HashMap<Long, Set<Integer>>() {

            {
                put(now, new HashSet<>(Arrays.asList(random.nextInt(), random.nextInt(), random.nextInt())));
                put(now + 1, new HashSet<>(Arrays.asList(random.nextInt(), random.nextInt(), random.nextInt())));
                put(now + 2, new HashSet<>(Arrays.asList(random.nextInt(), random.nextInt(), random.nextInt())));
            }
        });
        try {
            // flush each of the new segments, they should all succeed
            OnDiskIndex segment = index.scheduleSegmentFlush(false).call();
            index.segments.add(Futures.immediateFuture(segment));
            segments.add(segment.getIndexPath());
        } catch (Exception | FSError e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
    // make sure that all of the segments are present of the filesystem
    for (String segment : segments) Assert.assertTrue(new File(segment).exists());
    indexWriter.complete();
    // make sure that individual segments have been cleaned up
    for (String segment : segments) Assert.assertFalse(new File(segment).exists());
    // and combined index doesn't exist either
    Assert.assertFalse(new File(index.outputFile).exists());
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) FSError(org.apache.cassandra.io.FSError) SASIIndex(org.apache.cassandra.index.sasi.SASIIndex) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Descriptor(org.apache.cassandra.io.sstable.Descriptor) File(java.io.File) Test(org.junit.Test)

Aggregations

ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)2 FSError (org.apache.cassandra.io.FSError)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 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)1 StartupException (org.apache.cassandra.exceptions.StartupException)1 SASIIndex (org.apache.cassandra.index.sasi.SASIIndex)1 CorruptSSTableException (org.apache.cassandra.io.sstable.CorruptSSTableException)1 Descriptor (org.apache.cassandra.io.sstable.Descriptor)1 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)1 TableMetadata (org.apache.cassandra.schema.TableMetadata)1 Test (org.junit.Test)1