Search in sources :

Example 1 with SegmentLoadingException

use of com.cinchapi.concourse.server.storage.db.kernel.SegmentLoadingException in project concourse by cinchapi.

the class Database method start.

@Override
public void start() {
    if (!running) {
        Logger.info("Database configured to store data in {}", directory);
        running = true;
        this.writer = new AwaitableExecutorService(Executors.newCachedThreadPool(ThreadFactories.namingThreadFactory("DatabaseWriter")));
        this.segments.clear();
        ArrayBuilder<Runnable> tasks = ArrayBuilder.builder();
        List<Segment> segments = Collections.synchronizedList(this.segments);
        Stream<Path> files = storage.files();
        files.forEach(file -> tasks.add(() -> {
            try {
                Segment segment = Segment.load(file);
                segments.add(segment);
            } catch (SegmentLoadingException e) {
                Logger.error("Error when trying to load Segment {}", file);
                Logger.error("", e);
            }
        }));
        files.close();
        if (tasks.length() > 0) {
            AwaitableExecutorService loader = new AwaitableExecutorService(Executors.newCachedThreadPool(ThreadFactories.namingThreadFactory("DatabaseLoader")));
            try {
                loader.await((task, error) -> Logger.error("Unexpected error when trying to load Database Segments: {}", error), tasks.build());
            } catch (InterruptedException e) {
                Logger.error("The Database was interrupted while starting...", e);
                Thread.currentThread().interrupt();
                return;
            } finally {
                loader.shutdown();
            }
        }
        // Sort the segments in chronological order
        Collections.sort(this.segments, Segment.TEMPORAL_COMPARATOR);
        // Remove segments that overlap. Segments may overlap if they are
        // duplicates resulting from a botched upgrade or reindex or if they
        // were involved in an optimization pass, but garbage collection
        // didn't run before the server shutdown.
        ListIterator<Segment> lit = segments.listIterator();
        while (lit.hasNext()) {
            if (lit.hasPrevious()) {
                Segment previous = lit.previous();
                lit.next();
                Segment current = lit.next();
                if (current.intersects(previous)) {
                    lit.previous();
                    lit.previous();
                    lit.remove();
                    Logger.warn("Segment {} was not loaded because it contains duplicate data. It has been scheduled for garbage collection.", previous);
                // TODO: mark #previous for garbage collection
                }
            } else {
                lit.next();
            }
        }
        rotate(false);
        memory = new CacheState();
        /*
             * If enabled, setup Compaction to run continuously in the
             * background; trying to perform both "full" and "incremental"
             * compaction. Incremental compaction is opportunistic; attempting
             * frequently, but only occurring if no other conflicting work is
             * happening and only trying to compact one "shift". On the other
             * hand,full compaction runs less frequently, but is very
             * aggressive: blocking until any other conflicting work is done
             * and trying every possible shift.
             */
        // @formatter:off
        compactor = ENABLE_COMPACTION ? new SimilarityCompactor(storage) : NoOpCompactor.instance();
        fullCompaction = ENABLE_COMPACTION ? Executors.newScheduledThreadPool(1, createCompactionThreadFactory(tag, "Full")) : NoOpScheduledExecutorService.instance();
        fullCompaction.scheduleWithFixedDelay(() -> compactor.executeFullCompaction(), FULL_COMPACTION_INITIAL_DELAY_IN_SECONDS, FULL_COMPACTION_RUN_FREQUENCY_IN_SECONDS, TimeUnit.SECONDS);
        incrementalCompaction = ENABLE_COMPACTION ? Executors.newScheduledThreadPool(1, createCompactionThreadFactory(tag, "Incremental")) : NoOpScheduledExecutorService.instance();
        incrementalCompaction.scheduleWithFixedDelay(() -> compactor.tryIncrementalCompaction(), INCREMENTAL_COMPACTION_INITIAL_DELAY_IN_SECONDS, INCREMENTAL_COMPACTION_RUN_FREQUENCY_IN_SECONDS, TimeUnit.SECONDS);
        // @formatter:on
        Logger.info("Database is running with compaction {}.", ENABLE_COMPACTION ? "ON" : "OFF");
    }
}
Also used : Path(java.nio.file.Path) SegmentLoadingException(com.cinchapi.concourse.server.storage.db.kernel.SegmentLoadingException) AwaitableExecutorService(com.cinchapi.concourse.server.concurrent.AwaitableExecutorService) SimilarityCompactor(com.cinchapi.concourse.server.storage.db.compaction.similarity.SimilarityCompactor) Segment(com.cinchapi.concourse.server.storage.db.kernel.Segment)

Example 2 with SegmentLoadingException

use of com.cinchapi.concourse.server.storage.db.kernel.SegmentLoadingException in project concourse by cinchapi.

the class StorageFormatV3 method load.

/**
 * Load the {@link Segment Segments} from {@code directory}
 *
 * @param directory
 * @return an {@link Iterable} containing all the loaded {@link Segment
 *         Segments}
 */
public static Iterable<Segment> load(Path directory) {
    /*
         * NOTE: This implementation is copied from Database#start with a few
         * changes:
         * 1) Segments are loaded seriously instead of asynchronously
         * 2) Overlapping Segments are not removed
         */
    List<Segment> segments = Lists.newArrayList();
    Stream<Path> files = FileSystem.ls(directory);
    files.forEach(file -> {
        try {
            Segment segment = Segment.load(file);
            segments.add(segment);
        } catch (SegmentLoadingException e) {
            Logger.error("Error when trying to load Segment {}", file);
            Logger.error("", e);
        }
    });
    files.close();
    // Sort the segments in chronological order
    Collections.sort(segments, Segment.TEMPORAL_COMPARATOR);
    return segments;
}
Also used : Path(java.nio.file.Path) SegmentLoadingException(com.cinchapi.concourse.server.storage.db.kernel.SegmentLoadingException) Segment(com.cinchapi.concourse.server.storage.db.kernel.Segment)

Aggregations

Segment (com.cinchapi.concourse.server.storage.db.kernel.Segment)2 SegmentLoadingException (com.cinchapi.concourse.server.storage.db.kernel.SegmentLoadingException)2 Path (java.nio.file.Path)2 AwaitableExecutorService (com.cinchapi.concourse.server.concurrent.AwaitableExecutorService)1 SimilarityCompactor (com.cinchapi.concourse.server.storage.db.compaction.similarity.SimilarityCompactor)1