tkalkirill commented on code in PR #907: URL: https://github.com/apache/ignite-3/pull/907#discussion_r914857080
########## modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/CheckpointDirtyPages.java: ########## @@ -0,0 +1,358 @@ +/* + * 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.pagememory.persistence.checkpoint; + +import static java.util.Collections.binarySearch; +import static java.util.stream.Collectors.toList; +import static org.apache.ignite.internal.pagememory.util.PageIdUtils.pageId; +import static org.apache.ignite.internal.pagememory.util.PageIdUtils.partitionId; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.RandomAccess; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.ignite.internal.pagememory.FullPageId; +import org.apache.ignite.internal.pagememory.persistence.PersistentPageMemory; +import org.apache.ignite.lang.IgniteBiTuple; +import org.jetbrains.annotations.Nullable; + +/** + * Sorted dirty pages from data regions that should be checkpointed. + * + * <p>Dirty pages should be sorted by groupId -> partitionId -> pageIdx. + */ +class CheckpointDirtyPages { + /** Dirty page comparator. */ + static final Comparator<FullPageId> DIRTY_PAGE_COMPARATOR = Comparator + .comparingInt(FullPageId::groupId) + .thenComparingLong(FullPageId::effectivePageId); + + /** Empty checkpoint dirty pages. */ + static final CheckpointDirtyPages EMPTY = new CheckpointDirtyPages(List.of()); + + /** Sorted dirty pages from data regions by groupId -> partitionId -> pageIdx. */ + private final List<IgniteBiTuple<PersistentPageMemory, List<FullPageId>>> dirtyPages; + + /** Total number of dirty pages. */ + private final int dirtyPagesCount; + + /** + * Constructor. + * + * @param dirtyPages Sorted dirty pages from data regions by groupId -> partitionId -> pageIdx. + */ + public CheckpointDirtyPages(Map<PersistentPageMemory, List<FullPageId>> dirtyPages) { + this(dirtyPages.isEmpty() ? List.of() + : dirtyPages.entrySet().stream().map(e -> new IgniteBiTuple<>(e.getKey(), e.getValue())).collect(toList())); + } + + /** + * Constructor. + * + * @param dirtyPages Sorted dirty pages from data regions by groupId -> partitionId -> pageIdx. + */ + public CheckpointDirtyPages(List<IgniteBiTuple<PersistentPageMemory, List<FullPageId>>> dirtyPages) { + assert dirtyPages instanceof RandomAccess : dirtyPages; + + this.dirtyPages = dirtyPages; + + int count = 0; + + for (IgniteBiTuple<PersistentPageMemory, List<FullPageId>> pages : dirtyPages) { + assert !pages.getValue().isEmpty() : pages.getKey(); + assert pages.getValue() instanceof RandomAccess : pages.getValue(); + + count += pages.getValue().size(); + } + + dirtyPagesCount = count; + } + + /** + * Returns total number of dirty pages. + */ + public int dirtyPagesCount() { + return dirtyPagesCount; + } + + /** + * Returns a queue of dirty pages to be written to a checkpoint. + */ + public CheckpointDirtyPagesQueue toQueue() { + return new CheckpointDirtyPagesQueue(); + } + + /** + * Looks for dirty page views for a specific group and partition. + * + * @param grpId Group ID. + * @param partId Partition ID. + */ + public @Nullable CheckpointDirtyPagesView findView(int grpId, int partId) { + if (dirtyPages.isEmpty()) { + return null; + } + + FullPageId startPageId = new FullPageId(pageId(partId, (byte) 0, 0), grpId); + FullPageId endPageId = new FullPageId(pageId(partId + 1, (byte) 0, 0), grpId); + + for (int i = 0; i < dirtyPages.size(); i++) { + List<FullPageId> pageIds = dirtyPages.get(i).getValue(); + + int fromIndex = binarySearch(pageIds, startPageId, DIRTY_PAGE_COMPARATOR); + + fromIndex = fromIndex >= 0 ? fromIndex : Math.min(pageIds.size() - 1, -fromIndex - 1); + + if (!equalsByGroupAndPartition(startPageId, pageIds.get(fromIndex))) { + continue; + } + + int toIndex = binarySearch(pageIds.subList(fromIndex, pageIds.size()), endPageId, DIRTY_PAGE_COMPARATOR); + + toIndex = toIndex > 0 ? toIndex - 1 : -toIndex - 2; + + return new CheckpointDirtyPagesView(i, fromIndex, fromIndex + toIndex); + } + + return null; + } + + /** + * Looks for the next dirty page view from the current one, {@code null} if not found. + * + * @param currentView Current view to dirty pages, {@code null} to get first. + */ + public @Nullable CheckpointDirtyPagesView nextView(@Nullable CheckpointDirtyPagesView currentView) { + assert currentView == null || currentView.owner() == this : currentView; + + if (dirtyPages.isEmpty()) { + return null; + } + + int index; + int fromPosition; + + if (currentView == null) { + index = 0; + fromPosition = 0; + } else { + index = currentView.isToPositionLast() ? currentView.index + 1 : currentView.index; + fromPosition = currentView.isToPositionLast() ? 0 : currentView.toPosition + 1; + } + + if (index >= dirtyPages.size()) { + return null; + } + + List<FullPageId> pageIds = dirtyPages.get(index).getValue(); + + if (fromPosition == pageIds.size() - 1 || !equalsByGroupAndPartition(pageIds.get(fromPosition), pageIds.get(fromPosition + 1))) { + return new CheckpointDirtyPagesView(index, fromPosition, fromPosition); + } + + FullPageId startPageId = pageIds.get(fromPosition); + FullPageId endPageId = new FullPageId(pageId(partitionId(startPageId.pageId()) + 1, (byte) 0, 0), startPageId.groupId()); + + int toPosition = binarySearch(pageIds.subList(fromPosition, pageIds.size()), endPageId, DIRTY_PAGE_COMPARATOR); + + toPosition = toPosition > 0 ? toPosition - 1 : -toPosition - 2; + + return new CheckpointDirtyPagesView(index, fromPosition, fromPosition + toPosition); + } + + /** + * Queue of dirty pages that will need to be written to a checkpoint. + * + * <p>Thread safe. + */ + class CheckpointDirtyPagesQueue { + /** Current position in the queue. */ + private final AtomicInteger position = new AtomicInteger(); + + /** Sizes each element in {@link #dirtyPages} + the previous value in this array. */ + private final int[] sizes; + + /** + * Private constructor. + */ + private CheckpointDirtyPagesQueue() { + int size = 0; + + int[] sizes = new int[dirtyPages.size()]; + + for (int i = 0; i < dirtyPages.size(); i++) { + sizes[i] = size += dirtyPages.get(i).getValue().size(); + } + + this.sizes = sizes; + } + + /** + * Returns {@link true} if the next element of the queue was obtained. + * + * @param result Holder is the result of getting the next dirty page. + */ + public boolean next(QueueResult result) { + int queuePosition = this.position.getAndIncrement(); + + if (queuePosition >= dirtyPagesCount) { + result.owner = null; + + return false; + } + + if (result.owner != this) { + result.owner = this; + result.index = 0; + } + + int index = result.index; + + if (queuePosition >= sizes[index]) { + if (queuePosition == sizes[index]) { + index++; + } else { + index = findDirtyPagesIndex(index, queuePosition); + } + } + + result.index = index; + result.position = index > 0 ? queuePosition - sizes[index - 1] : queuePosition; + + return true; + } + + /** + * Returns {@link true} if the queue is empty. + */ + public boolean isEmpty() { + return position.get() >= dirtyPagesCount; + } + + /** + * Returns the size of the queue. + */ + public int size() { + return dirtyPagesCount - Math.min(dirtyPagesCount, position.get()); + } + + private int findDirtyPagesIndex(int index, int position) { + return Math.abs(Arrays.binarySearch(sizes, index, sizes.length, position) + 1); Review Comment: Let's assume we have 4 regions with the following pages (FullPageId(grpId, partId)): - FullPageId(0, 0) - FullPageId(1, 0) - FullPageId(2, 0), FullPageId(2, 1) - FullPageId(3, 0, 0), FullPageId(3, 0, 1), FullPageId(3, 0, 2) Total size: 7 cumulativeSizes: [1,2,4,7] Let's assume that the first thread got **FullPageId(0, 0)**, did something else, and when `CheckpointDirtyPagesQueue#next` is called, the next position is 5 (let's assume), then the binary search will return -3. Thus, a negative value is possible, but we will not go beyond the **cumulativeSizes** due to the check on total size. -- 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]
