ibessonov commented on code in PR #1405:
URL: https://github.com/apache/ignite-3/pull/1405#discussion_r1047121590


##########
modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/mv/BlobStorage.java:
##########
@@ -0,0 +1,341 @@
+/*
+ * 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.internal.storage.pagememory.mv;
+
+import java.util.Objects;
+import org.apache.ignite.internal.pagememory.PageIdAllocator;
+import org.apache.ignite.internal.pagememory.PageMemory;
+import org.apache.ignite.internal.pagememory.datastructure.DataStructure;
+import org.apache.ignite.internal.pagememory.io.PageIo;
+import org.apache.ignite.internal.pagememory.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagememory.metric.IoStatisticsHolderNoOp;
+import org.apache.ignite.internal.pagememory.reuse.LongListReuseBag;
+import org.apache.ignite.internal.pagememory.reuse.ReuseBag;
+import org.apache.ignite.internal.pagememory.reuse.ReuseList;
+import org.apache.ignite.internal.pagememory.util.PageHandler;
+import org.apache.ignite.internal.pagememory.util.PageLockListenerNoOp;
+import org.apache.ignite.internal.storage.pagememory.mv.io.BlobDataIo;
+import org.apache.ignite.internal.storage.pagememory.mv.io.BlobFirstIo;
+import org.apache.ignite.internal.storage.pagememory.mv.io.BlobIo;
+import org.apache.ignite.lang.IgniteInternalCheckedException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Used to store a limited number of blobs (just byte arrays) per partition. 
Each blob is stored in a sequence
+ * of pages forming a linked list (a previous page links to the next one).
+ *
+ * <p>If a lot of blobs (comparable with the number of rows) needs to be 
stored in a partition, another mechanism
+ * (probably using a {@link 
org.apache.ignite.internal.pagememory.freelist.FreeList}) should be used.
+ */
+public class BlobStorage {

Review Comment:
   This is interesting after all, why didn't you extend the DataStructure 
class? It has a bunch of conveniences



##########
modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/mv/BlobStorage.java:
##########
@@ -0,0 +1,341 @@
+/*
+ * 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.internal.storage.pagememory.mv;
+
+import java.util.Objects;
+import org.apache.ignite.internal.pagememory.PageIdAllocator;
+import org.apache.ignite.internal.pagememory.PageMemory;
+import org.apache.ignite.internal.pagememory.datastructure.DataStructure;
+import org.apache.ignite.internal.pagememory.io.PageIo;
+import org.apache.ignite.internal.pagememory.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.pagememory.metric.IoStatisticsHolderNoOp;
+import org.apache.ignite.internal.pagememory.reuse.LongListReuseBag;
+import org.apache.ignite.internal.pagememory.reuse.ReuseBag;
+import org.apache.ignite.internal.pagememory.reuse.ReuseList;
+import org.apache.ignite.internal.pagememory.util.PageHandler;
+import org.apache.ignite.internal.pagememory.util.PageLockListenerNoOp;
+import org.apache.ignite.internal.storage.pagememory.mv.io.BlobDataIo;
+import org.apache.ignite.internal.storage.pagememory.mv.io.BlobFirstIo;
+import org.apache.ignite.internal.storage.pagememory.mv.io.BlobIo;
+import org.apache.ignite.lang.IgniteInternalCheckedException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Used to store a limited number of blobs (just byte arrays) per partition. 
Each blob is stored in a sequence
+ * of pages forming a linked list (a previous page links to the next one).
+ *
+ * <p>If a lot of blobs (comparable with the number of rows) needs to be 
stored in a partition, another mechanism
+ * (probably using a {@link 
org.apache.ignite.internal.pagememory.freelist.FreeList}) should be used.
+ */
+public class BlobStorage {
+    static final long NO_PAGE_ID = 0;
+
+    private final ReuseList reuseList;
+    private final PageMemory pageMemory;
+
+    private final int groupId;
+    private final int partitionId;
+
+    private final IoStatisticsHolder statisticsHolder;
+
+    private final RecycleAndAddToReuseBag recycleAndAddToReuseBag = new 
RecycleAndAddToReuseBag();
+
+    private final ReadFragment readFragment = new ReadFragment();
+
+    private final WriteFragment writeFragment = new WriteFragment();
+
+    /**
+     * Creates a new instance.
+     */
+    public BlobStorage(ReuseList reuseList, PageMemory pageMemory, int 
groupId, int partitionId, IoStatisticsHolder statisticsHolder) {
+        this.reuseList = reuseList;
+        this.pageMemory = pageMemory;
+        this.groupId = groupId;
+        this.partitionId = partitionId;
+        this.statisticsHolder = statisticsHolder;
+    }
+
+    /**
+     * Reads a blob stored starting at a page with the given ID.
+     *
+     * @param firstPageId ID of first page.
+     * @return Byte array for the blob.
+     * @throws IgniteInternalCheckedException If something goes wrong.
+     */
+    public byte[] readBlob(long firstPageId) throws 
IgniteInternalCheckedException {
+        ReadState readState = new ReadState();
+
+        long pageId = firstPageId;
+
+        while (pageId != NO_PAGE_ID) {
+            Boolean ok = PageHandler.readPage(
+                    pageMemory,
+                    groupId,
+                    pageId,
+                    PageLockListenerNoOp.INSTANCE,
+                    readFragment,
+                    readState,
+                    0,
+                    false,
+                    IoStatisticsHolderNoOp.INSTANCE
+            );
+
+            assert ok : pageId;
+
+            pageId = readState.nextPageId;
+        }
+
+        assert readState.bytes != null;
+
+        return readState.bytes;
+    }
+
+    /**
+     * Adds a new blob to the storage.
+     *
+     * @param bytes Blob bytes.
+     * @return ID of the page starting the chain representing the blob.
+     * @throws IgniteInternalCheckedException If something goes wrong.
+     */
+    public long addBlob(byte[] bytes) throws IgniteInternalCheckedException {
+        return doStore(NO_PAGE_ID, bytes);
+    }
+
+    /**
+     * Updates the blob content.
+     *
+     * @param firstPageId ID of the first page in the chain storing the blob.
+     * @param bytes New blob content.
+     * @throws IgniteInternalCheckedException If something goes wrong.
+     */
+    public void updateBlob(long firstPageId, byte[] bytes) throws 
IgniteInternalCheckedException {
+        doStore(firstPageId, bytes);
+    }
+
+    private long doStore(long maybeFirstPageId, byte[] bytes) throws 
IgniteInternalCheckedException {

Review Comment:
   I agree that we should reuse old pages, it's simpler actually. And I believe 
that for the most part, the amount of pages will stay the same.



##########
modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/mv/PersistentPageMemoryMvPartitionStorage.java:
##########
@@ -226,22 +262,37 @@ public void 
committedGroupConfiguration(RaftGroupConfiguration config) {
         assert checkpointTimeoutLock.checkpointLockIsHeldByThread();
 
         CheckpointProgress lastCheckpoint = 
checkpointManager.lastCheckpointProgress();
-
         UUID lastCheckpointId = lastCheckpoint == null ? null : 
lastCheckpoint.id();
 
-        meta.lastGroupConfig(lastCheckpointId, groupConfigToBytes(config));
+        byte[] raftGroupConfigBytes = raftGroupConfigToBytes(config);
+
+        raftGroupConfigReadWriteLock.writeLock().lock();
+
+        try {
+            if (meta.lastRaftGroupConfigFirstPageId() == 
BlobStorage.NO_PAGE_ID) {
+                long configPageId = blobStorage.addBlob(raftGroupConfigBytes);
+
+                meta.lastRaftGroupConfigFirstPageId(lastCheckpointId, 
configPageId);
+            } else {
+                blobStorage.updateBlob(meta.lastRaftGroupConfigFirstPageId(), 
raftGroupConfigBytes);
+            }
+        } catch (IgniteInternalCheckedException e) {
+            throw new StorageException("Cannot save committed group 
configuration, groupId=" + groupId + ", partitionId=" + groupId, e);
+        } finally {
+            raftGroupConfigReadWriteLock.writeLock().unlock();
+        }
     }
 
     @Nullable
-    private static RaftGroupConfiguration groupConfigFromBytes(byte @Nullable 
[] bytes) {
+    private static RaftGroupConfiguration raftGroupConfigFromBytes(byte 
@Nullable [] bytes) {

Review Comment:
   By the way, why do we have to mention RAFT here? There's a chance of us 
having more replication protocol in the future, can we try and abstract 
ourselves a little bit? At least stop renaming old methods into a new that 
mention RAFT even more.
   
   It would be a good idea to rename RaftGroupConfiguration as well. Or even 
better. I don't understand why it's a storages' task to convert an object into 
a byte array. Looks like a wrong design decision. 



##########
modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/mv/MvPageIoModule.java:
##########
@@ -38,7 +40,9 @@ public Collection<IoVersions<?>> ioVersions() {
                 VersionChainMetaIo.VERSIONS,
                 VersionChainInnerIo.VERSIONS,
                 VersionChainLeafIo.VERSIONS,
-                RowVersionDataIo.VERSIONS
+                RowVersionDataIo.VERSIONS,
+                BlobFirstIo.VERSIONS,

Review Comment:
   I can agree on this one, a single page type could be enough. We could store 
the total length before the payload, for example, that's not a big deal and the 
code complexity won't change too much



##########
modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/index/freelist/io/IndexColumnsDataIo.java:
##########
@@ -48,7 +48,7 @@ protected IndexColumnsDataIo(int ver) {
     protected void writeRowData(long pageAddr, int dataOff, int payloadSize, 
IndexColumns row, boolean newRow) {
         assertPageType(pageAddr);
 
-        putShort(pageAddr, dataOff, (short) payloadSize);
+        putShort(pageAddr, dataOff, narrowIntToShort(payloadSize));

Review Comment:
   Ok, I also think that this method is integrated too early. Here we should 
_assert_ that payloadSize fits the page, not even the short range.
   And, I believe, such assertion must already exist somewhere before calling 
the `writeRowData`



##########
modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/io/AbstractDataPageIo.java:
##########
@@ -1438,6 +1438,22 @@ protected abstract void writeRowData(
             boolean newRow
     ) throws IgniteInternalCheckedException;
 
+
+    /**
+     * Narrows an {@code int} down to {@code short} throwing an exception if 
the value cannot be exactly represented as a {@code short}.
+     *
+     * @param intValue Value to narrow down.
+     * @return Resulting short value.
+     * @throws IllegalArgumentException If the provided value does not fit the 
{@code short} range.
+     */
+    protected static short narrowIntToShort(int intValue) {

Review Comment:
   There's a `java.lang.Math#toIntExact` in Java. Maybe we should borrow the 
naming convention and the exception type? Just saying



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to