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


##########
modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/io/PartitionMetaIo.java:
##########
@@ -36,15 +35,19 @@ public class PartitionMetaIo extends PageIo {
 
     private static final int LAST_APPLIED_TERM_OFF = LAST_APPLIED_INDEX_OFF + 
Long.BYTES;
 
-    private static final int ROW_VERSION_FREE_LIST_ROOT_PAGE_ID_OFF = 
LAST_APPLIED_TERM_OFF + Long.BYTES;
+    private static final int LAST_RAFT_GROUP_CONFIG_LINK_OFF = 
LAST_APPLIED_TERM_OFF + Long.BYTES;
+
+    private static final int ROW_VERSION_FREE_LIST_ROOT_PAGE_ID_OFF = 
LAST_RAFT_GROUP_CONFIG_LINK_OFF + Long.BYTES;
 
     private static final int INDEX_COLUMNS_FREE_LIST_ROOT_PAGE_ID_OFF = 
ROW_VERSION_FREE_LIST_ROOT_PAGE_ID_OFF + Long.BYTES;
 
     private static final int VERSION_CHAIN_TREE_ROOT_PAGE_ID_OFF = 
INDEX_COLUMNS_FREE_LIST_ROOT_PAGE_ID_OFF + Long.BYTES;
 
     public static final int INDEX_TREE_META_PAGE_ID_OFF = 
VERSION_CHAIN_TREE_ROOT_PAGE_ID_OFF + Long.BYTES;
 
-    private static final int PAGE_COUNT_OFF = INDEX_TREE_META_PAGE_ID_OFF + 
Long.BYTES;
+    private static final int BLOB_FREE_LIST_ROOT_PAGE_ID_OFF = 
INDEX_TREE_META_PAGE_ID_OFF + Long.BYTES;

Review Comment:
   I think it should be removed.



##########
modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/io/PartitionMetaIo.java:
##########
@@ -36,15 +35,19 @@ public class PartitionMetaIo extends PageIo {
 
     private static final int LAST_APPLIED_TERM_OFF = LAST_APPLIED_INDEX_OFF + 
Long.BYTES;
 
-    private static final int ROW_VERSION_FREE_LIST_ROOT_PAGE_ID_OFF = 
LAST_APPLIED_TERM_OFF + Long.BYTES;
+    private static final int LAST_RAFT_GROUP_CONFIG_LINK_OFF = 
LAST_APPLIED_TERM_OFF + Long.BYTES;

Review Comment:
   ```suggestion
       private static final int LAST_RAFT_GROUP_CONFIG_FIRST_PAGE_ID_OFF = 
LAST_APPLIED_TERM_OFF + Long.BYTES;
   ```



##########
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:
   What about the fact that we always free old pages and create new ones at the 
beginning? seems like a simpler option.



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/BaseMvStoragesTest.java:
##########
@@ -152,7 +152,7 @@ protected static TestValue value(BinaryRow binaryRow) {
         return new IgniteBiTuple<>(key(binaryRow), value(binaryRow));
     }
 
-    protected static List<IgniteBiTuple<TestKey, TestValue>> 
toList(Cursor<ReadResult> cursor) throws Exception {
+    protected static List<IgniteBiTuple<TestKey, TestValue>> 
drainToList(Cursor<ReadResult> cursor) {

Review Comment:
   I think that in tests where it is used, you can remove the Exception.



##########
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 {
+        Objects.requireNonNull(bytes, "bytes is null");
+
+        long firstPageId = allocatePageIfNeeded(maybeFirstPageId, true);
+
+        WriteState state = new WriteState(bytes);
+        state.pageId = firstPageId;
+
+        do {
+            Boolean ok = PageHandler.writePage(
+                    pageMemory,
+                    groupId,
+                    state.pageId,
+                    PageLockListenerNoOp.INSTANCE,
+                    writeFragment,
+                    null,
+                    state,
+                    0,
+                    false,
+                    statisticsHolder
+            );
+
+            assert ok : state.pageId;
+        } while (!state.stop);
+
+        freePagesStartingWith(state.firstPageToFreeId);
+
+        return firstPageId;
+    }
+
+    private long allocatePageIfNeeded(long maybePageId, boolean firstPage) 
throws IgniteInternalCheckedException {
+        long pageId;
+
+        if (maybePageId == NO_PAGE_ID) {
+            pageId = allocatePage();
+
+            PageHandler.initPage(pageMemory, groupId, pageId, 
latestBlobIo(firstPage), PageLockListenerNoOp.INSTANCE, statisticsHolder);
+        } else {
+            pageId = maybePageId;
+        }
+
+        return pageId;
+    }
+
+    private long allocatePage() throws IgniteInternalCheckedException {
+        long pageId = reuseList.takeRecycledPage();
+
+        if (pageId != 0) {
+            pageId = reuseList.initRecycledPage(pageId, 
PageIdAllocator.FLAG_AUX, null);
+        }
+
+        if (pageId == 0) {
+            pageId = pageMemory.allocatePage(groupId, partitionId, 
PageIdAllocator.FLAG_AUX);
+        }
+
+        return pageId;
+    }
+
+    private static BlobIo latestBlobIo(boolean firstPage) {
+        return firstPage ? BlobFirstIo.VERSIONS.latest() : 
BlobDataIo.VERSIONS.latest();
+    }
+
+    private void freePagesStartingWith(long pageId) throws 
IgniteInternalCheckedException {
+        if (pageId != NO_PAGE_ID) {
+            
reuseList.addForRecycle(recycleAndCollectPagesStartingWith(pageId));
+        }
+    }
+
+    private ReuseBag recycleAndCollectPagesStartingWith(long startingPageId) 
throws IgniteInternalCheckedException {
+        ReuseBag reuseBag = new LongListReuseBag();
+
+        long pageId = startingPageId;
+
+        while (pageId != NO_PAGE_ID) {
+            Long nextPageId = PageHandler.writePage(pageMemory, groupId, 
pageId, PageLockListenerNoOp.INSTANCE,
+                    recycleAndAddToReuseBag, null, reuseBag, 0, pageId, 
IoStatisticsHolderNoOp.INSTANCE);
+
+            assert nextPageId != pageId : pageId;
+
+            pageId = nextPageId;
+        }
+
+        return reuseBag;
+    }
+
+    /**
+     * State of a read operation.
+     */
+    private static class ReadState {
+        private byte @Nullable [] bytes;
+
+        private int bytesOffset;
+
+        private long nextPageId = NO_PAGE_ID;
+    }
+
+    /**
+     * Reads a fragment stored in a page.
+     */
+    private static class ReadFragment implements PageHandler<ReadState, 
Boolean> {
+        @Override
+        public Boolean run(int groupId, long pageId, long page, long pageAddr, 
PageIo io, ReadState state, int unused,
+                IoStatisticsHolder statHolder) throws 
IgniteInternalCheckedException {
+            BlobIo blobIo = (BlobIo) io;
+
+            if (state.bytes == null) {
+                assert state.bytesOffset == 0;
+
+                state.bytes = new byte[blobIo.getTotalLength(pageAddr)];
+            }
+
+            int fragmentLength = blobIo.getFragmentLength(pageAddr);
+            blobIo.getFragmentBytes(pageAddr, state.bytes, state.bytesOffset, 
fragmentLength);
+
+            int newBytesOffset = state.bytesOffset + fragmentLength;
+
+            if (newBytesOffset < state.bytes.length) {
+                long nextPageId = blobIo.getNextPageId(pageAddr);
+
+                assert nextPageId != NO_PAGE_ID;
+
+                state.nextPageId = nextPageId;
+            } else {
+                state.nextPageId = NO_PAGE_ID;

Review Comment:
   I think it's worth making sure that there is no nextPageId in BlobIo.



##########
modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/io/PartitionMetaIo.java:
##########
@@ -251,6 +253,7 @@ protected void printPage(long addr, int pageSize, 
IgniteStringBuilder sb) {
         sb.app("TablePartitionMeta [").nl()
                 .app("lastAppliedIndex=").app(getLastAppliedIndex(addr)).nl()
                 .app("lastAppliedTerm=").app(getLastAppliedTerm(addr)).nl()
+                
.app("lastRaftGroupConfigFirstPageId=").app(getLastRaftGroupConfigFirstPageId(addr)).nl()

Review Comment:
   ```suggestion
                   .app(", lastAppliedTerm=").app(getLastAppliedTerm(addr)).nl()
                   .app(", 
lastRaftGroupConfigFirstPageId=").app(getLastRaftGroupConfigFirstPageId(addr)).nl()
   ```



##########
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 think that we could get by with one page, for example `BlobFragmentIo`, 
which will contain: the length of the fragment, the fragment itself (byte[]) 
and the pageId of the next fragment. 
   
   I don't think we'll be reading and writing the config often, so we can copy 
arrays when we read.
   
   As an optimization, we can write the length of the full blob length in the 
first fragment, since the current fragment will be completely filled.



-- 
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