tillrohrmann commented on a change in pull request #18237: URL: https://github.com/apache/flink/pull/18237#discussion_r777410554
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/taskexecutor/slot/FileSlotAllocationSnapshotPersistenceService.java ########## @@ -0,0 +1,139 @@ +/* + * 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.taskexecutor.slot; + +import org.apache.flink.util.FileUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nonnull; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +/** + * File based {@link SlotAllocationSnapshotPersistenceService} that persists the {@link + * SlotAllocationSnapshot} as local files. + */ +public class FileSlotAllocationSnapshotPersistenceService + implements SlotAllocationSnapshotPersistenceService { + private static final Logger LOG = + LoggerFactory.getLogger(FileSlotAllocationSnapshotPersistenceService.class); + + static final String SUFFIX = ".bin"; + private final File slotAllocationSnapshotDirectory; + + public FileSlotAllocationSnapshotPersistenceService(File slotAllocationSnapshotDirectory) { + this.slotAllocationSnapshotDirectory = slotAllocationSnapshotDirectory; + + if (!slotAllocationSnapshotDirectory.exists() + && !slotAllocationSnapshotDirectory.mkdirs()) { + throw new IllegalStateException( + String.format( + "Cannot create the slot allocation snapshot directory %s.", + slotAllocationSnapshotDirectory)); + } + } + + @Override + public void persistAllocationSnapshot(SlotAllocationSnapshot slotAllocationSnapshot) + throws IOException { + // Let's try to write the slot allocations on file + final File slotAllocationSnapshotFile = + slotAllocationFile(slotAllocationSnapshot.getSlotID().getSlotNumber()); + + try (ObjectOutputStream oos = + new ObjectOutputStream(new FileOutputStream(slotAllocationSnapshotFile))) { + oos.writeObject(slotAllocationSnapshot); + + LOG.debug( + "Successfully written allocation state metadata file {} for job {} and allocation {}.", + slotAllocationSnapshotFile.toPath(), + slotAllocationSnapshot.getJobId(), + slotAllocationSnapshot.getAllocationId()); + } + } + + private File slotAllocationFile(int slotIndex) { + return new File( + slotAllocationSnapshotDirectory.getAbsolutePath(), slotIndexToFilename(slotIndex)); + } + + @Nonnull + private String slotIndexToFilename(int slotIndex) { + return slotIndex + SUFFIX; + } + + private int filenameToSlotIndex(String filename) { + return Integer.parseInt(filename.substring(0, filename.length() - SUFFIX.length())); + } + + @Override + public void deleteAllocationSnapshot(int slotIndex) { + // Let's try to write the slot allocations on file + final File slotAllocationSnapshotFile = slotAllocationFile(slotIndex); + try { + FileUtils.deleteFileOrDirectory(slotAllocationSnapshotFile); + LOG.debug( + "Successfully deleted allocation state metadata file {}.", + slotAllocationSnapshotFile.toPath()); + } catch (IOException ioe) { + LOG.debug( Review comment: Yes, this is probably better. -- 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]
