Search in sources :

Example 11 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class LogManagerImpl method appendToStorage.

private LogId appendToStorage(final List<LogEntry> toAppend) {
    LogId lastId = null;
    if (!this.hasError) {
        final long startMs = Utils.monotonicMs();
        final int entriesCount = toAppend.size();
        this.nodeMetrics.recordSize("append-logs-count", entriesCount);
        try {
            int writtenSize = 0;
            for (int i = 0; i < entriesCount; i++) {
                final LogEntry entry = toAppend.get(i);
                writtenSize += entry.getData() != null ? entry.getData().remaining() : 0;
            }
            this.nodeMetrics.recordSize("append-logs-bytes", writtenSize);
            final int nAppent = this.logStorage.appendEntries(toAppend);
            if (nAppent != entriesCount) {
                LOG.error("**Critical error**, fail to appendEntries, nAppent={}, toAppend={}", nAppent, toAppend.size());
                reportError(RaftError.EIO.getNumber(), "Fail to append log entries");
            }
            if (nAppent > 0) {
                lastId = toAppend.get(nAppent - 1).getId();
            }
            toAppend.clear();
        } finally {
            this.nodeMetrics.recordLatency("append-logs", Utils.monotonicMs() - startMs);
        }
    }
    return lastId;
}
Also used : LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 12 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class LogManagerImpl method getTermFromLogStorage.

private long getTermFromLogStorage(final long index) {
    final LogEntry entry = this.logStorage.getEntry(index);
    if (entry != null) {
        if (this.raftOptions.isEnableLogEntryChecksum() && entry.isCorrupted()) {
            // Report error to node and throw exception.
            final String msg = String.format("The log entry is corrupted, index=%d, term=%d, expectedChecksum=%d, realChecksum=%d", entry.getId().getIndex(), entry.getId().getTerm(), entry.getChecksum(), entry.checksum());
            reportError(RaftError.EIO.getNumber(), msg);
            throw new LogEntryCorruptedException(msg);
        }
        return entry.getId().getTerm();
    }
    return 0;
}
Also used : LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) LogEntryCorruptedException(org.apache.ignite.raft.jraft.error.LogEntryCorruptedException)

Example 13 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class LogManagerImpl method descLogsInMemory.

private String descLogsInMemory() {
    final StringBuilder sb = new StringBuilder();
    boolean wasFirst = true;
    for (int i = 0; i < this.logsInMemory.size(); i++) {
        LogEntry logEntry = this.logsInMemory.get(i);
        if (!wasFirst) {
            sb.append(",");
        } else {
            wasFirst = false;
        }
        sb.append("<id:(").append(logEntry.getId().getTerm()).append(",").append(logEntry.getId().getIndex()).append("),type:").append(logEntry.getType()).append(">");
    }
    return sb.toString();
}
Also used : LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 14 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

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(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
                    entry.setId(new LogId(nextLogIndex, 0));
                    LOG.warn("Entry not found for nextLogIndex {} when reset.", nextLogIndex);
                }
                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 : ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) LogStorageOptions(org.apache.ignite.raft.jraft.option.LogStorageOptions) ReadOptions(org.rocksdb.ReadOptions) DBOptions(org.rocksdb.DBOptions) WriteOptions(org.rocksdb.WriteOptions) Options(org.rocksdb.Options) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) RocksDBException(org.rocksdb.RocksDBException) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 15 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class LocalLogStorage method appendEntries.

@Override
public int appendEntries(final List<LogEntry> entries) {
    if (entries == null || entries.isEmpty()) {
        return 0;
    }
    final int entriesCount = entries.size();
    this.readLock.lock();
    try {
        if (!initialized) {
            LOG.warn("DB not initialized or destroyed.");
            return 0;
        }
        for (LogEntry logEntry : entries) {
            log.put(logEntry.getId().getIndex(), logEntry);
        }
        lastLogIndex = log.lastKey();
        firstLogIndex = log.firstKey();
        return entriesCount;
    } catch (Exception e) {
        LOG.error("Fail to append entry.", e);
        return 0;
    } finally {
        this.readLock.unlock();
    }
}
Also used : LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Aggregations

LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)48 LogId (org.apache.ignite.raft.jraft.entity.LogId)26 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)12 Status (org.apache.ignite.raft.jraft.Status)11 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)11 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 LogManager (org.apache.ignite.raft.jraft.storage.LogManager)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)7 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)6 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)6 ByteBuffer (java.nio.ByteBuffer)5 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)5 ConfigurationManager (org.apache.ignite.raft.jraft.conf.ConfigurationManager)4 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)4 EventHandler (com.lmax.disruptor.EventHandler)3 EventTranslator (com.lmax.disruptor.EventTranslator)3 RingBuffer (com.lmax.disruptor.RingBuffer)3 List (java.util.List)3