Search in sources :

Example 1 with RocksDBSegmentLogStorage

use of com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage in project sofa-jraft by sofastack.

the class LogStorageBenchmark method main.

public static void main(final String[] args) {
    String testPath = Paths.get(SystemPropertyUtil.get("user.dir"), "log_storage").toString();
    System.out.println("Test log storage path: " + testPath);
    int batchSize = 100;
    int logSize = 16 * 1024;
    int totalLogs = 1024 * 1024;
    // LogStorage logStorage = new RocksDBLogStorage(testPath, new RaftOptions());
    LogStorage logStorage = new RocksDBSegmentLogStorage(testPath, new RaftOptions());
    LogStorageOptions opts = new LogStorageOptions();
    opts.setConfigurationManager(new ConfigurationManager());
    opts.setLogEntryCodecFactory(LogEntryV2CodecFactory.getInstance());
    logStorage.init(opts);
    new LogStorageBenchmark(logStorage, logSize, totalLogs, batchSize).doTest();
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) LogStorageOptions(com.alipay.sofa.jraft.option.LogStorageOptions) LogStorage(com.alipay.sofa.jraft.storage.LogStorage) RocksDBSegmentLogStorage(com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage) ConfigurationManager(com.alipay.sofa.jraft.conf.ConfigurationManager) RocksDBSegmentLogStorage(com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage)

Example 2 with RocksDBSegmentLogStorage

use of com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage in project sofa-jraft by sofastack.

the class RocksDBSegmentLogStorageTest method testTruncateChaos.

@Test
public void testTruncateChaos() throws Exception {
    int times = 100;
    int n = 100;
    for (int i = 0; i < times; i++) {
        this.logStorage.shutdown();
        FileUtils.deleteDirectory(new File(this.path));
        this.path = TestUtils.mkTempDir();
        FileUtils.forceMkdir(new File(this.path));
        this.logStorage = new RocksDBSegmentLogStorage(this.path, new RaftOptions(), 32, 256);
        final LogStorageOptions opts = newLogStorageOptions();
        this.logStorage.init(opts);
        for (int j = 0; j < n; j++) {
            this.logStorage.appendEntries(Arrays.asList(TestUtils.mockEntry(j, 1, ThreadLocalRandom.current().nextInt(180))));
        }
        int index = ThreadLocalRandom.current().nextInt(n);
        boolean truncatePrefix = ThreadLocalRandom.current().nextBoolean();
        if (truncatePrefix) {
            this.logStorage.truncatePrefix(index);
            for (int j = 0; j < n; j++) {
                if (j < index) {
                    assertNull(this.logStorage.getEntry(j));
                } else {
                    assertNotNull(this.logStorage.getEntry(j));
                }
            }
        } else {
            this.logStorage.truncateSuffix(index);
            for (int j = 0; j < n; j++) {
                if (j <= index) {
                    assertNotNull(this.logStorage.getEntry(j));
                } else {
                    assertNull(this.logStorage.getEntry(j));
                }
            }
        }
    }
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) LogStorageOptions(com.alipay.sofa.jraft.option.LogStorageOptions) File(java.io.File) RocksDBSegmentLogStorage(com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage) Test(org.junit.Test)

Example 3 with RocksDBSegmentLogStorage

use of com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage in project sofa-jraft by sofastack.

the class RocksDBSegmentLogStorageTest method testTruncateSuffixWithDifferentValueSize.

@Test
public void testTruncateSuffixWithDifferentValueSize() throws Exception {
    // shutdown the old one
    this.logStorage.shutdown();
    // Set value threshold to be 32 bytes.
    this.logStorage = new RocksDBSegmentLogStorage(this.path, new RaftOptions(), 32, 64);
    final LogStorageOptions opts = newLogStorageOptions();
    this.logStorage.init(opts);
    int term = 1;
    for (int i = 0; i < 10; i++) {
        this.logStorage.appendEntries(Arrays.asList(TestUtils.mockEntry(i, term, i)));
    }
    this.logStorage.appendEntries(Arrays.asList(TestUtils.mockEntry(10, term, 64)));
    for (int i = 11; i < 20; i++) {
        this.logStorage.appendEntries(Arrays.asList(TestUtils.mockEntry(i, term, i)));
    }
    for (int i = 0; i < 20; i++) {
        assertNotNull(this.logStorage.getEntry(i));
    }
    assertEquals(((RocksDBSegmentLogStorage) this.logStorage).getLastSegmentFileForRead().getWrotePos(), 179);
    this.logStorage.truncateSuffix(15);
    for (int i = 0; i < 20; i++) {
        if (i <= 15) {
            assertNotNull(this.logStorage.getEntry(i));
        } else {
            assertNull(this.logStorage.getEntry(i));
        }
    }
    assertEquals(((RocksDBSegmentLogStorage) this.logStorage).getLastSegmentFileForRead().getWrotePos(), 102);
    this.logStorage.truncateSuffix(13);
    for (int i = 0; i < 20; i++) {
        if (i <= 13) {
            assertNotNull(this.logStorage.getEntry(i));
        } else {
            assertNull(this.logStorage.getEntry(i));
        }
    }
    assertEquals(((RocksDBSegmentLogStorage) this.logStorage).getLastSegmentFileForRead().getWrotePos(), 102);
    this.logStorage.truncateSuffix(5);
    for (int i = 0; i < 20; i++) {
        if (i <= 5) {
            assertNotNull(this.logStorage.getEntry(i));
        } else {
            assertNull(this.logStorage.getEntry(i));
        }
    }
    assertNull(((RocksDBSegmentLogStorage) this.logStorage).getLastSegmentFileForRead());
    this.logStorage.appendEntries(Arrays.asList(TestUtils.mockEntry(20, term, 10)));
    assertNotNull(this.logStorage.getEntry(20));
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) LogStorageOptions(com.alipay.sofa.jraft.option.LogStorageOptions) RocksDBSegmentLogStorage(com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage) Test(org.junit.Test)

Aggregations

LogStorageOptions (com.alipay.sofa.jraft.option.LogStorageOptions)3 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)3 RocksDBSegmentLogStorage (com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage)3 Test (org.junit.Test)2 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)1 LogStorage (com.alipay.sofa.jraft.storage.LogStorage)1 File (java.io.File)1