You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/08/16 14:59:24 UTC

[GitHub] [ignite-3] alievmirza commented on a diff in pull request #939: IGNITE-17334 Basic volatile RAFT log storage

alievmirza commented on code in PR #939:
URL: https://github.com/apache/ignite-3/pull/939#discussion_r946888230


##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/impl/VolatileLogStorage.java:
##########
@@ -0,0 +1,299 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ignite.raft.jraft.storage.impl;
+
+import java.util.List;
+import java.util.NavigableMap;
+import java.util.SortedMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.raft.jraft.entity.EnumOutter;
+import org.apache.ignite.raft.jraft.entity.LogEntry;
+import org.apache.ignite.raft.jraft.entity.LogId;
+import org.apache.ignite.raft.jraft.entity.codec.LogEntryDecoder;
+import org.apache.ignite.raft.jraft.entity.codec.LogEntryEncoder;
+import org.apache.ignite.raft.jraft.option.LogStorageOptions;
+import org.apache.ignite.raft.jraft.storage.LogStorage;
+import org.apache.ignite.raft.jraft.storage.VolatileStorage;
+import org.apache.ignite.raft.jraft.util.Describer;
+import org.apache.ignite.raft.jraft.util.Requires;
+
+/**
+ * Stores RAFT log in memory.
+ */
+public class VolatileLogStorage implements LogStorage, Describer, VolatileStorage {
+    private static final IgniteLogger LOG = Loggers.forClass(VolatileLogStorage.class);
+
+    private final LogStorageBudget budget;
+
+    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    private final Lock readLock = this.readWriteLock.readLock();
+    private final Lock writeLock = this.readWriteLock.writeLock();
+
+    private final NavigableMap<Long, LogEntry> log = new ConcurrentSkipListMap<>();
+
+    private LogEntryEncoder logEntryEncoder;
+    private LogEntryDecoder logEntryDecoder;
+
+    private volatile long firstLogIndex = 1;
+    private volatile long lastLogIndex = 0;
+
+    private volatile boolean initialized = false;
+
+    public VolatileLogStorage(LogStorageBudget budget) {
+        super();
+
+        this.budget = budget;
+    }
+
+    @Override
+    public boolean init(final LogStorageOptions opts) {
+        Requires.requireNonNull(opts.getConfigurationManager(), "Null conf manager");
+        Requires.requireNonNull(opts.getLogEntryCodecFactory(), "Null log entry codec factory");
+
+        this.writeLock.lock();
+
+        try {
+            if (initialized) {
+                LOG.warn("VolatileLogStorage init() was already called.");
+                return true;
+            }
+            this.initialized = true;
+            this.logEntryDecoder = opts.getLogEntryCodecFactory().decoder();
+            this.logEntryEncoder = opts.getLogEntryCodecFactory().encoder();
+            Requires.requireNonNull(this.logEntryDecoder, "Null log entry decoder");
+            Requires.requireNonNull(this.logEntryEncoder, "Null log entry encoder");
+
+            return true;
+        } finally {
+            this.writeLock.unlock();
+        }
+    }
+
+    @Override
+    public void shutdown() {
+        this.writeLock.lock();
+
+        try {
+            this.initialized = false;
+            this.log.clear();
+        } finally {
+            this.writeLock.unlock();
+        }
+    }
+
+    @Override
+    public long getFirstLogIndex() {
+        this.readLock.lock();
+
+        try {
+            return this.firstLogIndex;
+        } finally {
+            this.readLock.unlock();
+        }
+    }
+
+    @Override
+    public long getLastLogIndex() {
+        this.readLock.lock();
+
+        try {
+            return this.lastLogIndex;
+        } finally {
+            this.readLock.unlock();
+        }
+    }
+
+    @Override
+    public LogEntry getEntry(final long index) {
+        this.readLock.lock();
+
+        try {
+            if (index < getFirstLogIndex()) {
+                return null;
+            }
+
+            return log.get(index);
+        } finally {
+            this.readLock.unlock();
+        }
+    }
+
+    @Override
+    public long getTerm(final long index) {
+        final LogEntry entry = getEntry(index);
+        if (entry != null) {
+            return entry.getId().getTerm();
+        }
+        return 0;
+    }
+
+    @Override
+    public boolean appendEntry(final LogEntry entry) {
+        this.readLock.lock();
+
+        try {
+            if (!initialized) {
+                LOG.warn("DB not initialized or destroyed.");
+                return false;
+            }
+
+            this.log.put(entry.getId().getIndex(), entry);
+
+            lastLogIndex = log.lastKey();
+            firstLogIndex = log.firstKey();
+
+            budget.onAppended(entry);
+
+            return true;
+        } finally {
+            this.readLock.unlock();
+        }
+    }
+
+    @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;
+            }
+
+            int appended = 0;
+            for (LogEntry logEntry : entries) {
+
+                log.put(logEntry.getId().getIndex(), logEntry);
+
+                appended++;
+            }
+
+            lastLogIndex = log.lastKey();
+            firstLogIndex = log.firstKey();
+
+            budget.onAppended(entries.subList(0, appended));
+
+            if (appended < entriesCount) {

Review Comment:
   Seems like this code is redundant 



##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/LogManager.java:
##########
@@ -220,5 +220,4 @@ interface NewLogCallback {
      * @return status
      */
     Status checkConsistency();
-
 }

Review Comment:
   EOL



##########
modules/raft/src/test/java/org/apache/ignite/raft/jraft/storage/impl/UnlimitedBudgetTest.java:
##########
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.raft.jraft.storage.impl;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.ignite.raft.jraft.entity.LogEntry;
+import org.junit.jupiter.api.Test;
+
+class UnlimitedBudgetTest {
+    private final UnlimitedBudget budget = new UnlimitedBudget();
+
+    @Test
+    void allowsAppend() {
+        assertTrue(budget.hasRoomFor(new LogEntry()));
+    }
+}

Review Comment:
   EOL



##########
modules/raft/src/test/java/org/apache/ignite/raft/jraft/storage/impl/EntryCountBudgetTest.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.raft.jraft.storage.impl;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import org.apache.ignite.raft.jraft.entity.LogEntry;
+import org.apache.ignite.raft.jraft.entity.LogId;
+import org.junit.jupiter.api.Test;
+
+class EntryCountBudgetTest {
+    @Test
+    void allowsAppendWhenThereIsRoom() {
+        EntryCountBudget budget = new EntryCountBudget(1);
+
+        assertTrue(budget.hasRoomFor(entry(1)));
+    }
+
+    private LogEntry entry(long index) {
+        LogEntry entry = new LogEntry();
+        entry.setId(new LogId(index, 1));
+        return entry;
+    }
+
+    @Test
+    void deniesAppendWhenThereIsNoRoom() {
+        EntryCountBudget budget = new EntryCountBudget(0);
+
+        assertFalse(budget.hasRoomFor(entry(1)));
+    }
+
+    @Test
+    void onAppendedSingleDecreasesRoom() {
+        EntryCountBudget budget = new EntryCountBudget(1);
+
+        budget.onAppended(entry(1));
+
+        assertFalse(budget.hasRoomFor(entry(2)));
+    }
+
+    @Test
+    void onAppendedMultipleDecreasesRoom() {
+        EntryCountBudget budget = new EntryCountBudget(2);
+
+        budget.onAppended(List.of(entry(1), entry(2)));
+
+        assertFalse(budget.hasRoomFor(entry(3)));
+    }
+
+    @Test
+    void onTruncatedPrefixForStoredPrefixIncreasesRoom() {
+        EntryCountBudget budget = new EntryCountBudget(2);
+
+        budget.onAppended(List.of(entry(1), entry(2)));
+
+        budget.onTruncatedPrefix(2);
+
+        assertTrue(budget.hasRoomFor(entry(3)));
+    }
+
+    @Test
+    void onTruncatedPrefixForNonStoredPrefixDoesNotIncreaseRoom() {
+        EntryCountBudget budget = new EntryCountBudget(2);
+
+        budget.onAppended(List.of(entry(1), entry(2)));
+
+        budget.onTruncatedPrefix(2);
+
+        budget.onAppended(List.of(entry(3)));
+
+        budget.onTruncatedPrefix(2);
+
+        assertFalse(budget.hasRoomFor(entry(4)));
+    }
+
+    @Test
+    void onTruncatedSuffixForStoredPrefixIncreasesRoom() {
+        EntryCountBudget budget = new EntryCountBudget(2);
+
+        budget.onAppended(List.of(entry(1), entry(2)));
+
+        budget.onTruncatedSuffix(1);
+
+        assertTrue(budget.hasRoomFor(entry(2)));
+    }
+
+    @Test
+    void onTruncatedSuffixForNonStoredPrefixDoesNotIncreaseRoom() {
+        EntryCountBudget budget = new EntryCountBudget(2);
+
+        budget.onAppended(List.of(entry(1), entry(2)));
+
+        budget.onTruncatedSuffix(2);
+
+        assertFalse(budget.hasRoomFor(entry(3)));
+    }
+}

Review Comment:
   EOL



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org