Search in sources :

Example 1 with CorruptSSTableException

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

use of org.apache.cassandra.io.sstable.CorruptSSTableException in project cassandra by apache.

the class CompressionMetadata method readChunkOffsets.

/**
     * Read offsets of the individual chunks from the given input.
     *
     * @param input Source of the data.
     *
     * @return collection of the chunk offsets.
     */
private Memory readChunkOffsets(DataInput input) {
    final int chunkCount;
    try {
        chunkCount = input.readInt();
        if (chunkCount <= 0)
            throw new IOException("Compressed file with 0 chunks encountered: " + input);
    } catch (IOException e) {
        throw new FSReadError(e, indexFilePath);
    }
    @SuppressWarnings("resource") Memory offsets = Memory.allocate(chunkCount * 8L);
    int i = 0;
    try {
        for (i = 0; i < chunkCount; i++) {
            offsets.setLong(i * 8L, input.readLong());
        }
        return offsets;
    } catch (IOException e) {
        if (offsets != null)
            offsets.close();
        if (e instanceof EOFException) {
            String msg = String.format("Corrupted Index File %s: read %d but expected %d chunks.", indexFilePath, i, chunkCount);
            throw new CorruptSSTableException(new IOException(msg, e), indexFilePath);
        }
        throw new FSReadError(e, indexFilePath);
    }
}
Also used : Memory(org.apache.cassandra.io.util.Memory) SafeMemory(org.apache.cassandra.io.util.SafeMemory) FSReadError(org.apache.cassandra.io.FSReadError) EOFException(java.io.EOFException) IOException(java.io.IOException) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException)

Example 3 with CorruptSSTableException

use of org.apache.cassandra.io.sstable.CorruptSSTableException in project cassandra by apache.

the class VerifyTest method testVerifyCorruptRowCorrectDigest.

@Test
public void testVerifyCorruptRowCorrectDigest() throws IOException, WriteTimeoutException {
    CompactionManager.instance.disableAutoCompaction();
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CORRUPT_CF2);
    fillCF(cfs, 2);
    Util.getAll(Util.cmd(cfs).build());
    SSTableReader sstable = cfs.getLiveSSTables().iterator().next();
    // overwrite one row with garbage
    long row0Start = sstable.getPosition(PartitionPosition.ForKey.get(ByteBufferUtil.bytes("0"), cfs.getPartitioner()), SSTableReader.Operator.EQ).position;
    long row1Start = sstable.getPosition(PartitionPosition.ForKey.get(ByteBufferUtil.bytes("1"), cfs.getPartitioner()), SSTableReader.Operator.EQ).position;
    long startPosition = row0Start < row1Start ? row0Start : row1Start;
    long endPosition = row0Start < row1Start ? row1Start : row0Start;
    RandomAccessFile file = new RandomAccessFile(sstable.getFilename(), "rw");
    file.seek(startPosition);
    file.writeBytes(StringUtils.repeat('z', (int) 2));
    file.close();
    if (ChunkCache.instance != null)
        ChunkCache.instance.invalidateFile(sstable.getFilename());
    // Update the Digest to have the right Checksum
    writeChecksum(simpleFullChecksum(sstable.getFilename()), sstable.descriptor.filenameFor(Component.DIGEST));
    try (Verifier verifier = new Verifier(cfs, sstable, false)) {
        // First a simple verify checking digest, which should succeed
        try {
            verifier.verify(false);
        } catch (CorruptSSTableException err) {
            fail("Simple verify should have succeeded as digest matched");
        }
        // Now try extended verify
        try {
            verifier.verify(true);
        } catch (CorruptSSTableException err) {
            return;
        }
        fail("Expected a CorruptSSTableException to be thrown");
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) SchemaLoader.createKeyspace(org.apache.cassandra.SchemaLoader.createKeyspace) Verifier(org.apache.cassandra.db.compaction.Verifier) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException) Test(org.junit.Test)

Example 4 with CorruptSSTableException

use of org.apache.cassandra.io.sstable.CorruptSSTableException in project cassandra by apache.

the class VerifyTest method testVerifyCorrectUncompressed.

@Test
public void testVerifyCorrectUncompressed() throws IOException {
    CompactionManager.instance.disableAutoCompaction();
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF3);
    fillCF(cfs, 2);
    SSTableReader sstable = cfs.getLiveSSTables().iterator().next();
    try (Verifier verifier = new Verifier(cfs, sstable, false)) {
        verifier.verify(false);
    } catch (CorruptSSTableException err) {
        fail("Unexpected CorruptSSTableException");
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) SchemaLoader.createKeyspace(org.apache.cassandra.SchemaLoader.createKeyspace) Verifier(org.apache.cassandra.db.compaction.Verifier) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException) Test(org.junit.Test)

Example 5 with CorruptSSTableException

use of org.apache.cassandra.io.sstable.CorruptSSTableException in project cassandra by apache.

the class VerifyTest method testVerifyCorrect.

@Test
public void testVerifyCorrect() throws IOException {
    CompactionManager.instance.disableAutoCompaction();
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    fillCF(cfs, 2);
    SSTableReader sstable = cfs.getLiveSSTables().iterator().next();
    try (Verifier verifier = new Verifier(cfs, sstable, false)) {
        verifier.verify(false);
    } catch (CorruptSSTableException err) {
        fail("Unexpected CorruptSSTableException");
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) SchemaLoader.createKeyspace(org.apache.cassandra.SchemaLoader.createKeyspace) Verifier(org.apache.cassandra.db.compaction.Verifier) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException) Test(org.junit.Test)

Aggregations

CorruptSSTableException (org.apache.cassandra.io.sstable.CorruptSSTableException)16 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)11 SchemaLoader.createKeyspace (org.apache.cassandra.SchemaLoader.createKeyspace)10 Verifier (org.apache.cassandra.db.compaction.Verifier)10 Test (org.junit.Test)10 IOException (java.io.IOException)4 EOFException (java.io.EOFException)3 FSReadError (org.apache.cassandra.io.FSReadError)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 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 CRC32 (java.util.zip.CRC32)1 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)1 Directories (org.apache.cassandra.db.Directories)1 Keyspace (org.apache.cassandra.db.Keyspace)1 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)1 StartupException (org.apache.cassandra.exceptions.StartupException)1