Search in sources :

Example 1 with Segment

use of com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.Segment in project sofa-jraft by sofastack.

the class MemoryRawKVStore method doSnapshotLoad.

void doSnapshotLoad(final MemoryKVStoreSnapshotFile snapshotFile, final String snapshotPath) throws Exception {
    final Timer.Context timeCtx = getTimeContext("SNAPSHOT_LOAD");
    try {
        final SequenceDB sequenceDB = snapshotFile.readFromFile(snapshotPath, SEQUENCE_DB, SequenceDB.class);
        final FencingKeyDB fencingKeyDB = snapshotFile.readFromFile(snapshotPath, FENCING_KEY_DB, FencingKeyDB.class);
        final LockerDB lockerDB = snapshotFile.readFromFile(snapshotPath, LOCKER_DB, LockerDB.class);
        this.sequenceDB.putAll(sequenceDB.data());
        this.fencingKeyDB.putAll(fencingKeyDB.data());
        this.lockerDB.putAll(lockerDB.data());
        final TailIndex tailIndex = snapshotFile.readFromFile(snapshotPath, TAIL_INDEX, TailIndex.class);
        final int tail = tailIndex.data();
        final List<Segment> segments = Lists.newArrayListWithCapacity(tail + 1);
        for (int i = 0; i <= tail; i++) {
            final Segment segment = snapshotFile.readFromFile(snapshotPath, SEGMENT + i, Segment.class);
            segments.add(segment);
        }
        for (final Segment segment : segments) {
            for (final Pair<byte[], byte[]> p : segment.data()) {
                this.defaultDB.put(p.getKey(), p.getValue());
            }
        }
    } finally {
        timeCtx.stop();
    }
}
Also used : FencingKeyDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.FencingKeyDB) Timer(com.codahale.metrics.Timer) LockerDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.LockerDB) TailIndex(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.TailIndex) SequenceDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.SequenceDB) Segment(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.Segment)

Example 2 with Segment

use of com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.Segment in project sofa-jraft by sofastack.

the class MemoryRawKVStore method doSnapshotSave.

void doSnapshotSave(final MemoryKVStoreSnapshotFile snapshotFile, final String snapshotPath, final Region region) throws Exception {
    final Timer.Context timeCtx = getTimeContext("SNAPSHOT_SAVE");
    try {
        final String tempPath = snapshotPath + "_temp";
        final File tempFile = new File(tempPath);
        FileUtils.deleteDirectory(tempFile);
        FileUtils.forceMkdir(tempFile);
        snapshotFile.writeToFile(tempPath, SEQUENCE_DB, new SequenceDB(subRangeMap(this.sequenceDB, region)));
        snapshotFile.writeToFile(tempPath, FENCING_KEY_DB, new FencingKeyDB(subRangeMap(this.fencingKeyDB, region)));
        snapshotFile.writeToFile(tempPath, LOCKER_DB, new LockerDB(subRangeMap(this.lockerDB, region)));
        final int size = this.opts.getKeysPerSegment();
        final List<Pair<byte[], byte[]>> segment = Lists.newArrayListWithCapacity(size);
        int index = 0;
        final byte[] realStartKey = BytesUtil.nullToEmpty(region.getStartKey());
        final byte[] endKey = region.getEndKey();
        final NavigableMap<byte[], byte[]> subMap;
        if (endKey == null) {
            subMap = this.defaultDB.tailMap(realStartKey);
        } else {
            subMap = this.defaultDB.subMap(realStartKey, endKey);
        }
        for (final Map.Entry<byte[], byte[]> entry : subMap.entrySet()) {
            segment.add(Pair.of(entry.getKey(), entry.getValue()));
            if (segment.size() >= size) {
                snapshotFile.writeToFile(tempPath, SEGMENT + index++, new Segment(segment));
                segment.clear();
            }
        }
        if (!segment.isEmpty()) {
            snapshotFile.writeToFile(tempPath, SEGMENT + index++, new Segment(segment));
            segment.clear();
        }
        snapshotFile.writeToFile(tempPath, TAIL_INDEX, new TailIndex(--index));
        final File destinationPath = new File(snapshotPath);
        FileUtils.deleteDirectory(destinationPath);
        FileUtils.moveDirectory(tempFile, destinationPath);
    } finally {
        timeCtx.stop();
    }
}
Also used : FencingKeyDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.FencingKeyDB) LockerDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.LockerDB) Segment(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.Segment) Timer(com.codahale.metrics.Timer) TailIndex(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.TailIndex) SequenceDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.SequenceDB) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Pair(com.alipay.sofa.jraft.rhea.util.Pair)

Aggregations

FencingKeyDB (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.FencingKeyDB)2 LockerDB (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.LockerDB)2 Segment (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.Segment)2 SequenceDB (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.SequenceDB)2 TailIndex (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.TailIndex)2 Timer (com.codahale.metrics.Timer)2 Pair (com.alipay.sofa.jraft.rhea.util.Pair)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1