Search in sources :

Example 16 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class V1Decoder method decode.

public void decode(final LogEntry log, final byte[] content) {
    // 1-5 type
    final int iType = Bits.getInt(content, 1);
    log.setType(EnumOutter.EntryType.forNumber(iType));
    // 5-13 index
    // 13-21 term
    final long index = Bits.getLong(content, 5);
    final long term = Bits.getLong(content, 13);
    log.setId(new LogId(index, term));
    // 21-25 peer count
    int peerCount = Bits.getInt(content, 21);
    // peers
    int pos = 25;
    if (peerCount > 0) {
        List<PeerId> peers = new ArrayList<>(peerCount);
        while (peerCount-- > 0) {
            final short len = Bits.getShort(content, pos);
            final byte[] bs = new byte[len];
            System.arraycopy(content, pos + 2, bs, 0, len);
            // peer len (short in 2 bytes)
            // peer str
            pos += 2 + len;
            final PeerId peer = new PeerId();
            peer.parse(AsciiStringUtil.unsafeDecode(bs));
            peers.add(peer);
        }
        log.setPeers(peers);
    }
    // old peers
    int oldPeerCount = Bits.getInt(content, pos);
    pos += 4;
    if (oldPeerCount > 0) {
        List<PeerId> oldPeers = new ArrayList<>(oldPeerCount);
        while (oldPeerCount-- > 0) {
            final short len = Bits.getShort(content, pos);
            final byte[] bs = new byte[len];
            System.arraycopy(content, pos + 2, bs, 0, len);
            // peer len (short in 2 bytes)
            // peer str
            pos += 2 + len;
            final PeerId peer = new PeerId();
            peer.parse(AsciiStringUtil.unsafeDecode(bs));
            oldPeers.add(peer);
        }
        log.setOldPeers(oldPeers);
    }
    // data
    if (content.length > pos) {
        final int len = content.length - pos;
        ByteBuffer data = ByteBuffer.allocate(len);
        data.put(content, pos, len);
        data.flip();
        log.setData(data);
    }
}
Also used : ArrayList(java.util.ArrayList) LogId(com.alipay.sofa.jraft.entity.LogId) ByteBuffer(java.nio.ByteBuffer) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 17 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class RocksDBLogStorage method reset.

@Override
public boolean reset(final long nextLogIndex) {
    if (nextLogIndex <= 0) {
        throw new IllegalArgumentException("Invalid next log index.");
    }
    this.writeLock.lock();
    try (final Options opt = new Options()) {
        LogEntry entry = getEntry(nextLogIndex);
        closeDB();
        try {
            RocksDB.destroyDB(this.path, opt);
            onReset(nextLogIndex);
            if (initAndLoad(null)) {
                if (entry == null) {
                    entry = new LogEntry();
                    entry.setType(EntryType.ENTRY_TYPE_NO_OP);
                    entry.setId(new LogId(nextLogIndex, 0));
                    LOG.warn("Entry not found for nextLogIndex {} when reset in data path: {}.", nextLogIndex, this.path);
                }
                return appendEntry(entry);
            } else {
                return false;
            }
        } catch (final RocksDBException e) {
            LOG.error("Fail to reset next log index.", e);
            return false;
        }
    } finally {
        this.writeLock.unlock();
    }
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) ReadOptions(org.rocksdb.ReadOptions) DBOptions(org.rocksdb.DBOptions) WriteOptions(org.rocksdb.WriteOptions) LogStorageOptions(com.alipay.sofa.jraft.option.LogStorageOptions) Options(org.rocksdb.Options) RocksDBException(org.rocksdb.RocksDBException) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 18 with LogId

use of com.alipay.sofa.jraft.entity.LogId 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 19 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class LogManagerImpl method checkConsistency.

@Override
public Status checkConsistency() {
    this.readLock.lock();
    try {
        Requires.requireTrue(this.firstLogIndex > 0);
        Requires.requireTrue(this.lastLogIndex >= 0);
        if (this.lastSnapshotId.equals(new LogId(0, 0))) {
            if (this.firstLogIndex == 1) {
                return Status.OK();
            }
            return new Status(RaftError.EIO, "Missing logs in (0, %d)", this.firstLogIndex);
        } else {
            if (this.lastSnapshotId.getIndex() >= this.firstLogIndex - 1 && this.lastSnapshotId.getIndex() <= this.lastLogIndex) {
                return Status.OK();
            }
            return new Status(RaftError.EIO, "There's a gap between snapshot={%d, %d} and log=[%d, %d] ", this.lastSnapshotId.toString(), this.lastSnapshotId.getTerm(), this.firstLogIndex, this.lastLogIndex);
        }
    } finally {
        this.readLock.unlock();
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) LogId(com.alipay.sofa.jraft.entity.LogId)

Example 20 with LogId

use of com.alipay.sofa.jraft.entity.LogId in project sofa-jraft by sofastack.

the class LogManagerImpl method setSnapshot.

@Override
public void setSnapshot(final SnapshotMeta meta) {
    LOG.debug("set snapshot: {}.", meta);
    boolean doUnlock = true;
    this.writeLock.lock();
    try {
        if (meta.getLastIncludedIndex() <= this.lastSnapshotId.getIndex()) {
            return;
        }
        final Configuration conf = confFromMeta(meta);
        final Configuration oldConf = oldConfFromMeta(meta);
        final ConfigurationEntry entry = new ConfigurationEntry(new LogId(meta.getLastIncludedIndex(), meta.getLastIncludedTerm()), conf, oldConf);
        this.configManager.setSnapshot(entry);
        final long term = unsafeGetTerm(meta.getLastIncludedIndex());
        final long savedLastSnapshotIndex = this.lastSnapshotId.getIndex();
        this.lastSnapshotId.setIndex(meta.getLastIncludedIndex());
        this.lastSnapshotId.setTerm(meta.getLastIncludedTerm());
        if (this.lastSnapshotId.compareTo(this.appliedId) > 0) {
            this.appliedId = this.lastSnapshotId.copy();
        }
        if (term == 0) {
            // last_included_index is larger than last_index
            // FIXME: what if last_included_index is less than first_index?
            doUnlock = false;
            // unlock in truncatePrefix
            truncatePrefix(meta.getLastIncludedIndex() + 1, this.writeLock);
        } else if (term == meta.getLastIncludedTerm()) {
            // TODO if there are still be need?
            if (savedLastSnapshotIndex > 0) {
                doUnlock = false;
                // unlock in truncatePrefix
                truncatePrefix(savedLastSnapshotIndex + 1, this.writeLock);
            }
        } else {
            if (!reset(meta.getLastIncludedIndex() + 1)) {
                LOG.warn("Reset log manager failed, nextLogIndex={}.", meta.getLastIncludedIndex() + 1);
            }
        }
    } finally {
        if (doUnlock) {
            this.writeLock.unlock();
        }
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogId(com.alipay.sofa.jraft.entity.LogId)

Aggregations

LogId (com.alipay.sofa.jraft.entity.LogId)44 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)26 PeerId (com.alipay.sofa.jraft.entity.PeerId)18 Test (org.junit.Test)18 Status (com.alipay.sofa.jraft.Status)10 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)9 ArrayList (java.util.ArrayList)8 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)7 Configuration (com.alipay.sofa.jraft.conf.Configuration)5 ByteBuffer (java.nio.ByteBuffer)5 LogManager (com.alipay.sofa.jraft.storage.LogManager)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)3 EntryType (com.alipay.sofa.jraft.entity.EnumOutter.EntryType)2 BaseLogEntryCodecFactoryTest (com.alipay.sofa.jraft.entity.codec.BaseLogEntryCodecFactoryTest)2 RaftException (com.alipay.sofa.jraft.error.RaftException)2 LogStorageOptions (com.alipay.sofa.jraft.option.LogStorageOptions)2 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)2 DisruptorMetricSet (com.alipay.sofa.jraft.util.DisruptorMetricSet)2