tkalkirill commented on code in PR #800: URL: https://github.com/apache/ignite-3/pull/800#discussion_r870159481
########## modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/CheckpointMarkersStorage.java: ########## @@ -0,0 +1,210 @@ +/* + * 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.nio.file.Files.createDirectories; +import static java.nio.file.Files.createFile; +import static java.nio.file.Files.exists; +import static java.nio.file.Files.isDirectory; +import static java.nio.file.Files.list; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.partitioningBy; +import static java.util.stream.Collectors.toCollection; +import static java.util.stream.Collectors.toList; +import static org.apache.ignite.lang.IgniteSystemProperties.getInteger; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.ignite.lang.IgniteInternalCheckedException; +import org.jetbrains.annotations.Nullable; + +/** + * Abstraction responsible for managing checkpoint markers storage. + */ +// TODO: IGNITE-15818 At the moment, a simple implementation has been made, it may need to be redone, you need to check it +public class CheckpointMarkersStorage { + /** Earliest checkpoint map changes threshold system properties. */ + public static final String IGNITE_CHECKPOINT_MAP_SNAPSHOT_THRESHOLD = "IGNITE_CHECKPOINT_MAP_SNAPSHOT_THRESHOLD"; + + /** Checkpoint start marker. */ + private static final String CHECKPOINT_START_MARKER = "START"; + + /** Checkpoint end marker. */ + private static final String CHECKPOINT_END_MARKER = "END"; + + /** Checkpoint marker file name pattern. */ + private static final Pattern CHECKPOINT_MARKER_FILE_NAME_PATTERN = Pattern.compile("(.*)-(START|END)\\.bin"); + + /** Checkpoint metadata directory ("cp"), contains files with checkpoint start and end markers. */ + private final Path checkpointDir; + + /** Checkpoint IDs. */ + private final Set<UUID> checkpointIds; + + /** Earliest checkpoint map changes threshold. */ + // TODO: IGNITE-16935 Move to config + private final int earliestCheckpointChangesThreshold = getInteger(IGNITE_CHECKPOINT_MAP_SNAPSHOT_THRESHOLD, 5); + + /** + * Constructor. + * + * @param storagePath Storage path. + * @throws IgniteInternalCheckedException If failed. + */ + public CheckpointMarkersStorage( + Path storagePath + ) throws IgniteInternalCheckedException { + checkpointDir = storagePath.resolve("cp"); + + try { + createDirectories(checkpointDir); + } catch (IOException e) { + throw new IgniteInternalCheckedException("Could not create directory for checkpoint metadata: " + checkpointDir, e); + } + + checkCheckpointDir(checkpointDir); + + try { + checkpointIds = list(checkpointDir) + .map(CheckpointMarkersStorage::parseCheckpointIdFromMarkerFile) + .collect(toCollection(ConcurrentHashMap::newKeySet)); + } catch (IOException e) { + throw new IgniteInternalCheckedException("Could not reads checkpoint markers: " + checkpointDir, e); + } + } + + /** + * Callback at the start of the checkpoint. + * + * <p>Creates a start marker for a checkpoint. + * + * @param checkpointId Checkpoint id. + */ + public void onCheckpointBegin(UUID checkpointId) throws IgniteInternalCheckedException { + assert !checkpointIds.contains(checkpointId) : checkpointId; + + Path checkpointStartMarker = checkpointDir.resolve(checkpointMarkerFileName(checkpointId, CHECKPOINT_START_MARKER)); + + try { + createFile(checkpointStartMarker); + } catch (IOException e) { + throw new IgniteInternalCheckedException("Could not create start checkpoint marker: " + checkpointStartMarker, e); + } + + checkpointIds.add(checkpointId); + } + + /** + * Callback at the end of the checkpoint. + * + * <p>Creates an end marker for a checkpoint. + * + * @param checkpointId Checkpoint id. + */ + public void onCheckpointEnd(UUID checkpointId) throws IgniteInternalCheckedException { + assert checkpointIds.contains(checkpointId) : checkpointId; + + Path checkpointEndMarker = checkpointDir.resolve(checkpointMarkerFileName(checkpointId, CHECKPOINT_END_MARKER)); + + try { + createFile(checkpointEndMarker); + } catch (IOException e) { + throw new IgniteInternalCheckedException("Could not create end checkpoint marker: " + checkpointEndMarker, e); + } + + if (checkpointIds.size() >= earliestCheckpointChangesThreshold) { + for (Iterator<UUID> it = checkpointIds.iterator(); it.hasNext(); ) { + UUID id = it.next(); + + if (!id.equals(checkpointId)) { + removeCheckpointMarkers(id); + + it.remove(); + } + } + } + } + + private void removeCheckpointMarkers(UUID checkpointId) { + Path startMarker = checkpointDir.resolve(checkpointMarkerFileName(checkpointId, CHECKPOINT_START_MARKER)); + Path endMarker = checkpointDir.resolve(checkpointMarkerFileName(checkpointId, CHECKPOINT_END_MARKER)); + + if (exists(startMarker)) { + startMarker.toFile().delete(); + } + + if (exists(endMarker)) { + endMarker.toFile().delete(); + } + } + + /** + * Checks that the directory contains only paired (start and end) checkpoint markers. + */ + private static void checkCheckpointDir(Path checkpointDir) throws IgniteInternalCheckedException { + assert isDirectory(checkpointDir) : checkpointDir; + + try { + Map<Boolean, List<Path>> files = list(checkpointDir) Review Comment: No -- 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]
