fredia commented on code in PR #22590: URL: https://github.com/apache/flink/pull/22590#discussion_r1198501534
########## flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/merging/FileMergingSnapshotManagerTest.java: ########## @@ -0,0 +1,288 @@ +/* + * 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.flink.runtime.checkpoint.merging; + +import org.apache.flink.api.common.TaskInfo; +import org.apache.flink.core.fs.FileSystem; +import org.apache.flink.core.fs.Path; +import org.apache.flink.core.fs.local.LocalFileSystem; +import org.apache.flink.runtime.state.CheckpointedStateScope; +import org.apache.flink.runtime.state.filesystem.AbstractFsCheckpointStorageAccess; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** Tests for {@link FileMergingSnapshotManager}. */ +public class FileMergingSnapshotManagerTest { + + @Rule public final TemporaryFolder tmp = new TemporaryFolder(); Review Comment: We should try to use junit5 and assertJ in new test. ########## flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/merging/FileMergingCheckpointUtils.java: ########## @@ -0,0 +1,87 @@ +/* + * 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.flink.runtime.checkpoint.merging; Review Comment: How about renaming "merging" to "filemerging" ########## flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/merging/FileMergingSnapshotManagerBase.java: ########## @@ -0,0 +1,279 @@ +/* + * 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.flink.runtime.checkpoint.merging; + +import org.apache.flink.core.fs.EntropyInjector; +import org.apache.flink.core.fs.FSDataOutputStream; +import org.apache.flink.core.fs.FileSystem; +import org.apache.flink.core.fs.OutputStreamAndPath; +import org.apache.flink.core.fs.Path; +import org.apache.flink.runtime.checkpoint.merging.FileMergingCheckpointUtils.SnapshotFileSystemInfo; +import org.apache.flink.runtime.checkpoint.merging.LogicalFile.LogicalFileId; +import org.apache.flink.runtime.state.CheckpointedStateScope; +import org.apache.flink.util.Preconditions; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nonnull; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executor; + +/** Base implementation of {@link FileMergingSnapshotManager}. */ +public abstract class FileMergingSnapshotManagerBase implements FileMergingSnapshotManager { + + private static final Logger LOG = LoggerFactory.getLogger(FileMergingSnapshotManager.class); + + private final String id; + + protected final Executor ioExecutor; + + // file system and directories + protected FileSystem fs; + protected Path checkpointDir; + protected Path sharedStateDir; + protected Path taskOwnedStateDir; + + protected int writeBufferSize; + private boolean fileSystemInitiated = false; + + protected boolean syncAfterClosingLogicalFile; + + protected PhysicalFile.PhysicalFileDeleter physicalFileDeleter = this::deletePhysicalFile; + + private final Map<SubtaskKey, Path> managedSharedStateDir = new ConcurrentHashMap<>(); + + protected Path managedExclusiveStateDir; + + public FileMergingSnapshotManagerBase(String id, Executor ioExecutor) { + this.id = id; + this.ioExecutor = ioExecutor; + } + + @Override + public void initFileSystem(SnapshotFileSystemInfo fileSystemInfo) throws IOException { + initFileSystem( + fileSystemInfo.fs, + fileSystemInfo.checkpointBaseDirectory, + fileSystemInfo.sharedStateDirectory, + fileSystemInfo.taskOwnedStateDirectory); + } + + @Override + public void addSubtask(SubtaskKey subtaskKey) { + try { + String managedDirName = subtaskKey.getManagedDirName(); + Path managedPath = new Path(sharedStateDir, managedDirName); + fs.mkdirs(managedPath); + managedSharedStateDir.put(subtaskKey, managedPath); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + // ------------------------------------------------------------------------ + // logical & physical file + // ------------------------------------------------------------------------ + + protected LogicalFile createLogicalFile( + @Nonnull PhysicalFile physicalFile, @Nonnull SubtaskKey subtaskKey) { + LogicalFileId fileID = LogicalFileId.generateRandomId(); + return new LogicalFile(fileID, physicalFile, subtaskKey); + } + + @Nonnull + protected PhysicalFile createPhysicalFile(SubtaskKey subtaskKey, CheckpointedStateScope scope) + throws IOException { + PhysicalFile result; + Exception latestException = null; + + Path dirPath = getManagedDir(subtaskKey, scope); + + if (dirPath == null) { + throw new IOException( + "Could not get " + + scope + + " path for subtask " + + subtaskKey + + ", the directory may have not been created."); + } + + for (int attempt = 0; attempt < 10; attempt++) { + try { + OutputStreamAndPath streamAndPath = + EntropyInjector.createEntropyAware( + fs, + createPhysicalFilePath(dirPath), + FileSystem.WriteMode.NO_OVERWRITE); + FSDataOutputStream outputStream = streamAndPath.stream(); + Path filePath = streamAndPath.path(); + result = new PhysicalFile(outputStream, filePath, this.physicalFileDeleter, scope); + updateFileCreationMetrics(filePath); + return result; + } catch (Exception e) { + latestException = e; + } + } + + throw new IOException( + "Could not open output stream for state file merging.", latestException); + } + + private void updateFileCreationMetrics(Path path) { + // TODO: FLINK- add io metrics + LOG.debug("Create a new physical file {} for checkpoint file merging.", path); + } + + protected Path createPhysicalFilePath(Path dirPath) { + // this must be called after initFileSystem() is called + // so the checkpoint directories must be not null if we reach here + final String fileName = UUID.randomUUID().toString(); + return new Path(dirPath, fileName); + } + + boolean isResponsibleForFile(Path filePath) { Review Comment: This method is not used yet. ########## flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/merging/FileMergingCheckpointUtils.java: ########## @@ -0,0 +1,87 @@ +/* + * 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.flink.runtime.checkpoint.merging; + +import org.apache.flink.core.fs.FileSystem; +import org.apache.flink.core.fs.Path; + +import javax.annotation.Nullable; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** Utilities for file-merging checkpoints. */ +public class FileMergingCheckpointUtils { + + // ------------------------------------------------------------------------ + // SSM initialization + // ------------------------------------------------------------------------ + + /** A class that packs the file system info for snapshot. */ + public static class SnapshotFileSystemInfo { + FileSystem fs; + Path checkpointBaseDirectory; + Path sharedStateDirectory; + Path taskOwnedStateDirectory; + @Nullable Path defaultSavepointDirectory; + + public SnapshotFileSystemInfo( + FileSystem fs, + Path checkpointBaseDirectory, + @Nullable Path sharedStateDirectory, + Path taskOwnedStateDirectory, + @Nullable Path defaultSavepointDirectory) { + this.fs = fs; + this.checkpointBaseDirectory = checkpointBaseDirectory; + this.sharedStateDirectory = sharedStateDirectory; + this.taskOwnedStateDirectory = taskOwnedStateDirectory; + this.defaultSavepointDirectory = sharedStateDirectory; Review Comment: =defaultSavepointDirectory? -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org