All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.io.network.partition.external.OptimizedBroadcastMergedPartitionIndices Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 * 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.io.network.partition.external;

import org.apache.flink.core.fs.FSDataInputStream;
import org.apache.flink.core.fs.FileSystem;
import org.apache.flink.core.fs.Path;
import org.apache.flink.core.memory.DataInputView;
import org.apache.flink.core.memory.DataInputViewStreamWrapper;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

import static
	org.apache.flink.runtime.io.network.partition.external.ExternalBlockResultPartitionMeta.ExternalSubpartitionMeta;
import static org.apache.flink.util.Preconditions.checkArgument;

/**
 *
 */
public class OptimizedBroadcastMergedPartitionIndices implements PartitionIndices {
	/** File system of data and index file for this partition. */
	private final FileSystem fileSystem;

	/** The path of result partition index file. */
	private final Path indexFilePath;

	/** The path of result partition data file. */
	private final Path dataFilePath;

	/** The number of subpartitions. */
	private final int subpartitionNum;

	private final AtomicReference subpartitionMetaRef;

	public OptimizedBroadcastMergedPartitionIndices(FileSystem fileSystem, String partitionDir, int spillIdx, int subpartitionNum) {
		this.fileSystem = fileSystem;
		this.indexFilePath = new Path(ExternalBlockShuffleUtils.generateIndexPath(partitionDir, spillIdx));
		this.dataFilePath = new Path(ExternalBlockShuffleUtils.generateDataPath(partitionDir, spillIdx));
		this.subpartitionNum = subpartitionNum;
		this.subpartitionMetaRef = new AtomicReference<>(null);
	}

	@Override
	public void initialize() throws IOException {
		subpartitionMetaRef.set(loadSubpartitionIndex());
	}

	@Override
	public ExternalSubpartitionMeta getSubpartitionMeta(int subpartitionIndex) throws IOException {
		checkArgument(subpartitionIndex >= 0 && subpartitionIndex < subpartitionNum, "Invalid subpartition index.");
		ExternalSubpartitionMeta subpartitionMeta = subpartitionMetaRef.get();
		if (subpartitionMeta == null) {
			subpartitionMeta = loadSubpartitionIndex();
			subpartitionMetaRef.set(subpartitionMeta);
		}
		return subpartitionMeta;
	}

	@Override
	public long shrinkMemoryFootprint() {
		ExternalSubpartitionMeta subpartitionMeta = subpartitionMetaRef.get();
		if (subpartitionMeta != null) {
			subpartitionMetaRef.lazySet(null);
			return 32L; // (64bit for reference) + (2 * 64bit for long) + (64bit for object)
		} else {
			return 0L;
		}
	}

	@Override
	public long getShrinkableMemoryFootprint() {
		ExternalSubpartitionMeta subpartitionMeta = subpartitionMetaRef.get();
		if (subpartitionMeta != null) {
			return 32L; // (64bit for reference) + (2 * 64bit for long) + (64bit for object)
		} else {
			return 0L;
		}
	}

	private ExternalSubpartitionMeta loadSubpartitionIndex() throws IOException {
		// Checks whether index files exist
		if (!fileSystem.exists(indexFilePath)) {
			throw new IOException("Index file doesn't exist, file path: " + indexFilePath.getPath());
		}

		// Loads PartitionIndices from index files
		try (FSDataInputStream indexIn = fileSystem.open(indexFilePath)) {
			DataInputView indexView = new DataInputViewStreamWrapper(indexIn);

			// Gets the number of partitions indices in the index file.
			final int size = indexView.readInt();
			checkArgument(size == subpartitionNum, "number of indices [" + size
				+ "] should be equal to subpartition number [" + subpartitionNum + "].");

			int nextPartitionId = 0;
			long length = 0L;
			for (int i = 0; i < size; i++) {
				int partitionId = indexView.readInt();

				if (nextPartitionId != partitionId) {
					throw new IOException("Got invalid partition id, expected nextPartitionId: "
						+ nextPartitionId + ", real partitionId: " + partitionId);
				}

				long startOffset = indexView.readLong();
				long lengthOfPartition = indexView.readLong();

				if (startOffset != 0) {
					throw new IOException("Offset should be 0 in broadcast mode, partition id: " + partitionId
						+ ", expected nextOffset: " + 0 + ", real startOffset: " + startOffset);
				}

				if (partitionId == 0) {
					length = lengthOfPartition;
				} else if (length != lengthOfPartition) {
					throw new IOException("length of Subpartition [" + partitionId
						+ "] should be equal in broadcast mode, expected length " + length
						+ ", real length " + lengthOfPartition);
				}

				nextPartitionId++;
			}

			return new ExternalSubpartitionMeta(dataFilePath, 0, length);
		} catch (IOException e) {
			throw new IOException("Cannot read index file, file path: " + indexFilePath.getPath(), e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy