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


##########
modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/mv/AddWriteLinkingWiInvokeClosure.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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 static org.apache.ignite.internal.pagememory.util.PageIdUtils.NULL_LINK;
+
+import java.util.UUID;
+import org.apache.ignite.internal.lang.IgniteInternalCheckedException;
+import org.apache.ignite.internal.pagememory.freelist.FreeList;
+import org.apache.ignite.internal.pagememory.tree.BplusTree;
+import org.apache.ignite.internal.pagememory.tree.IgniteTree.InvokeClosure;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.storage.StorageException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Implementation of {@link InvokeClosure} for {@link 
AbstractPageMemoryMvPartitionStorage#addWrite(RowId, BinaryRow, UUID, int, int)}
+ * which additionally maintains links between write intents.
+ *
+ * <p>See {@link AbstractPageMemoryMvPartitionStorage} about synchronization.
+ *
+ * <p>Operation may throw {@link StorageException} which will cause form 
{@link BplusTree#invoke(Object, Object, InvokeClosure)}.
+ */
+class AddWriteLinkingWiInvokeClosure extends AddWriteInvokeClosure {
+    private final PersistentPageMemoryMvPartitionStorage persistentStorage;
+
+    private final FreeList freeList;
+
+    private long wiListHeadLink;
+    private long newWiListHeadLink;
+
+    AddWriteLinkingWiInvokeClosure(
+            RowId rowId,
+            @Nullable BinaryRow row,
+            UUID txId,
+            int commitZoneId,
+            int commitPartitionId,
+            PersistentPageMemoryMvPartitionStorage storage
+    ) {
+        super(rowId, row, txId, commitZoneId, commitPartitionId, storage);
+
+        persistentStorage = storage;
+
+        freeList = storage.renewableState.freeList();
+    }
+
+    @Override
+    public void call(@Nullable VersionChain oldRow) throws 
IgniteInternalCheckedException {
+        wiListHeadLink = persistentStorage.lockWriteIntentListHead();
+        newWiListHeadLink = wiListHeadLink;
+
+        try {
+            super.call(oldRow);
+        } finally {
+            
persistentStorage.updateWriteIntentListHeadAndUnlock(newWiListHeadLink);
+        }
+    }
+
+    @Override
+    protected RowVersion insertFirstRowVersion() {
+        assert persistentStorage.writeIntentHeadIsLockedByCurrentThread();
+        assert persistentStorage.writeIntentListHead() == wiListHeadLink
+                : "Expected WI list head link " + wiListHeadLink + " but was " 
+ persistentStorage.writeIntentListHead();
+
+        WiLinkableRowVersion newVersion = insertRowVersion(NULL_LINK, 
wiListHeadLink, NULL_LINK);
+
+        newWiListHeadLink = newVersion.link();
+
+        updateWiListLinks(newVersion);
+
+        return newVersion;
+    }
+
+    @Override
+    protected RowVersion insertAnotherRowVersion(VersionChain oldRow, 
@Nullable RowVersion existingWriteIntent) {
+        assert persistentStorage.writeIntentHeadIsLockedByCurrentThread();
+        assert persistentStorage.writeIntentListHead() == wiListHeadLink
+                : "Expected WI list head link " + wiListHeadLink + " but was " 
+ persistentStorage.writeIntentListHead();
+
+        boolean replacingExistingWriteIntent = oldRow.isUncommitted();
+        assert replacingExistingWriteIntent == (existingWriteIntent != null);
+
+        long newNextWiLink;
+        long newPrevWiLink;
+        if (replacingExistingWriteIntent) {
+            newNextWiLink = 
existingWriteIntent.operations().nextWriteIntentLink(wiListHeadLink);
+            newPrevWiLink = 
existingWriteIntent.operations().prevWriteIntentLink();
+        } else {
+            newNextWiLink = wiListHeadLink;
+            newPrevWiLink = NULL_LINK;
+        }
+
+        WiLinkableRowVersion newVersion = 
insertRowVersion(oldRow.newestCommittedLink(), newNextWiLink, newPrevWiLink);

Review Comment:
   What does this method do? If it actually inserts an entire row's payload 
into data pages then I would not recommend doing it while holding an exclusive 
lock. This will lead to potential contention. Operations under lock should be 
as trivial as possible.
   
   What if we insert the row with `NULL/NULL` first, and only then acquire lock 
and update its links? How many changes would that introduce to the code?



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