Search in sources :

Example 1 with LogStorageOptions

use of com.alipay.sofa.jraft.option.LogStorageOptions in project sofa-jraft by sofastack.

the class BaseLogStorageTest method setup.

@Override
@Before
public void setup() throws Exception {
    super.setup();
    this.confManager = new ConfigurationManager();
    this.logEntryCodecFactory = LogEntryV2CodecFactory.getInstance();
    this.logStorage = newLogStorage();
    final LogStorageOptions opts = newLogStorageOptions();
    this.logStorage.init(opts);
}
Also used : LogStorageOptions(com.alipay.sofa.jraft.option.LogStorageOptions) ConfigurationManager(com.alipay.sofa.jraft.conf.ConfigurationManager) Before(org.junit.Before)

Example 2 with LogStorageOptions

use of com.alipay.sofa.jraft.option.LogStorageOptions in project sofa-jraft by sofastack.

the class LogManagerImpl method init.

@Override
public boolean init(final LogManagerOptions opts) {
    this.writeLock.lock();
    try {
        if (opts.getLogStorage() == null) {
            LOG.error("Fail to init log manager, log storage is null");
            return false;
        }
        this.raftOptions = opts.getRaftOptions();
        this.nodeMetrics = opts.getNodeMetrics();
        this.logStorage = opts.getLogStorage();
        this.configManager = opts.getConfigurationManager();
        LogStorageOptions lsOpts = new LogStorageOptions();
        lsOpts.setConfigurationManager(this.configManager);
        lsOpts.setLogEntryCodecFactory(opts.getLogEntryCodecFactory());
        if (!this.logStorage.init(lsOpts)) {
            LOG.error("Fail to init logStorage");
            return false;
        }
        this.firstLogIndex = this.logStorage.getFirstLogIndex();
        this.lastLogIndex = this.logStorage.getLastLogIndex();
        this.diskId = new LogId(this.lastLogIndex, getTermFromLogStorage(this.lastLogIndex));
        this.fsmCaller = opts.getFsmCaller();
        this.disruptor = // 
        DisruptorBuilder.<StableClosureEvent>newInstance().setEventFactory(// 
        new StableClosureEventFactory()).setRingBufferSize(// 
        opts.getDisruptorBufferSize()).setThreadFactory(// 
        new NamedThreadFactory("JRaft-LogManager-Disruptor-", true)).setProducerType(// 
        ProducerType.MULTI).setWaitStrategy(new TimeoutBlockingWaitStrategy(this.raftOptions.getDisruptorPublishEventWaitTimeoutSecs(), // 
        TimeUnit.SECONDS)).build();
        this.disruptor.handleEventsWith(new StableClosureEventHandler());
        this.disruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(this.getClass().getSimpleName(), (event, ex) -> reportError(-1, "LogManager handle event error")));
        this.diskQueue = this.disruptor.start();
        if (this.nodeMetrics.getMetricRegistry() != null) {
            this.nodeMetrics.getMetricRegistry().register("jraft-log-manager-disruptor", new DisruptorMetricSet(this.diskQueue));
        }
    } finally {
        this.writeLock.unlock();
    }
    return true;
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) LoggerFactory(org.slf4j.LoggerFactory) NodeMetrics(com.alipay.sofa.jraft.core.NodeMetrics) EntryType(com.alipay.sofa.jraft.entity.EnumOutter.EntryType) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Utils(com.alipay.sofa.jraft.util.Utils) ArrayList(java.util.ArrayList) LogManagerOptions(com.alipay.sofa.jraft.option.LogManagerOptions) LogManager(com.alipay.sofa.jraft.storage.LogManager) Map(java.util.Map) TimeoutBlockingWaitStrategy(com.lmax.disruptor.TimeoutBlockingWaitStrategy) RaftError(com.alipay.sofa.jraft.error.RaftError) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) ArrayDeque(com.alipay.sofa.jraft.util.ArrayDeque) EventHandler(com.lmax.disruptor.EventHandler) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ConfigurationManager(com.alipay.sofa.jraft.conf.ConfigurationManager) LogExceptionHandler(com.alipay.sofa.jraft.util.LogExceptionHandler) Logger(org.slf4j.Logger) DisruptorMetricSet(com.alipay.sofa.jraft.util.DisruptorMetricSet) PeerId(com.alipay.sofa.jraft.entity.PeerId) Configuration(com.alipay.sofa.jraft.conf.Configuration) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) RingBuffer(com.lmax.disruptor.RingBuffer) DisruptorBuilder(com.alipay.sofa.jraft.util.DisruptorBuilder) ProducerType(com.lmax.disruptor.dsl.ProducerType) Status(com.alipay.sofa.jraft.Status) LogStorage(com.alipay.sofa.jraft.storage.LogStorage) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Lock(java.util.concurrent.locks.Lock) LogEntryCorruptedException(com.alipay.sofa.jraft.error.LogEntryCorruptedException) LogStorageOptions(com.alipay.sofa.jraft.option.LogStorageOptions) FSMCaller(com.alipay.sofa.jraft.FSMCaller) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) ErrorType(com.alipay.sofa.jraft.entity.EnumOutter.ErrorType) EventFactory(com.lmax.disruptor.EventFactory) RaftException(com.alipay.sofa.jraft.error.RaftException) Requires(com.alipay.sofa.jraft.util.Requires) Disruptor(com.lmax.disruptor.dsl.Disruptor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) SegmentList(com.alipay.sofa.jraft.util.SegmentList) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) DisruptorMetricSet(com.alipay.sofa.jraft.util.DisruptorMetricSet) LogStorageOptions(com.alipay.sofa.jraft.option.LogStorageOptions) LogId(com.alipay.sofa.jraft.entity.LogId) TimeoutBlockingWaitStrategy(com.lmax.disruptor.TimeoutBlockingWaitStrategy)

Example 3 with LogStorageOptions

use of com.alipay.sofa.jraft.option.LogStorageOptions in project sofa-jraft by sofastack.

the class BaseLogStorageTest method newLogStorageOptions.

protected LogStorageOptions newLogStorageOptions() {
    final LogStorageOptions opts = new LogStorageOptions();
    opts.setConfigurationManager(this.confManager);
    opts.setLogEntryCodecFactory(this.logEntryCodecFactory);
    return opts;
}
Also used : LogStorageOptions(com.alipay.sofa.jraft.option.LogStorageOptions)

Example 4 with LogStorageOptions

use of com.alipay.sofa.jraft.option.LogStorageOptions 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 5 with LogStorageOptions

use of com.alipay.sofa.jraft.option.LogStorageOptions 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)

Aggregations

LogStorageOptions (com.alipay.sofa.jraft.option.LogStorageOptions)6 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)4 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)3 RocksDBSegmentLogStorage (com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage)3 LogStorage (com.alipay.sofa.jraft.storage.LogStorage)2 Test (org.junit.Test)2 FSMCaller (com.alipay.sofa.jraft.FSMCaller)1 Status (com.alipay.sofa.jraft.Status)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)1 NodeMetrics (com.alipay.sofa.jraft.core.NodeMetrics)1 EntryType (com.alipay.sofa.jraft.entity.EnumOutter.EntryType)1 ErrorType (com.alipay.sofa.jraft.entity.EnumOutter.ErrorType)1 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)1 LogId (com.alipay.sofa.jraft.entity.LogId)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 SnapshotMeta (com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta)1 LogEntryCorruptedException (com.alipay.sofa.jraft.error.LogEntryCorruptedException)1 RaftError (com.alipay.sofa.jraft.error.RaftError)1 RaftException (com.alipay.sofa.jraft.error.RaftException)1