Java Code Examples for org.apache.flink.util.MathUtils#checkedDownCast()

The following examples show how to use org.apache.flink.util.MathUtils#checkedDownCast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SeekableFileChannelInputView.java    From flink with Apache License 2.0 5 votes vote down vote up
public void seek(long position) throws IOException {
	final int block = MathUtils.checkedDownCast(position / segmentSize);
	final int positionInBlock = (int) (position % segmentSize);
	
	if (position < 0 || block >= numBlocksTotal || (block == numBlocksTotal - 1 && positionInBlock > sizeOfLastBlock)) {
		throw new IllegalArgumentException("Position is out of range");
	}
	
	clear();
	if (reader != null) {
		reader.close();
	}
	
	reader = ioManager.createBlockChannelReader(channelId);
	
	if (block > 0) {
		reader.seekToPosition(((long) block) * segmentSize);
	}
	
	this.numBlocksRemaining = this.numBlocksTotal - block;
	this.numRequestsRemaining = numBlocksRemaining;
	
	for (int i = 0; i < memory.size(); i++) {
		sendReadRequest(memory.get(i));
	}
	
	numBlocksRemaining--;
	seekInput(reader.getNextReturnedBlock(), positionInBlock, numBlocksRemaining == 0 ? sizeOfLastBlock : segmentSize);
}
 
Example 2
Source File: FileChannelInputView.java    From flink with Apache License 2.0 5 votes vote down vote up
public FileChannelInputView(BlockChannelReader<MemorySegment> reader, MemoryManager memManager, List<MemorySegment> memory, int sizeOfLastBlock) throws IOException {
	super(0);
	
	checkNotNull(reader);
	checkNotNull(memManager);
	checkNotNull(memory);
	checkArgument(!reader.isClosed());
	checkArgument(memory.size() > 0);
	
	this.reader = reader;
	this.memManager = memManager;
	this.memory = memory;
	this.sizeOfLastBlock = sizeOfLastBlock;
	
	try {
		final long channelLength = reader.getSize();
		final int segmentSize = memManager.getPageSize();
		
		this.numBlocksRemaining = MathUtils.checkedDownCast(channelLength / segmentSize);
		if (channelLength % segmentSize != 0) {
			this.numBlocksRemaining++;
		}
		
		this.numRequestsRemaining = numBlocksRemaining;
		
		for (int i = 0; i < memory.size(); i++) {
			sendReadRequest(memory.get(i));
		}
		
		advance();
	}
	catch (IOException e) {
		memManager.release(memory);
		throw e;
	}
}
 
Example 3
Source File: SeekableFileChannelInputView.java    From flink with Apache License 2.0 5 votes vote down vote up
public void seek(long position) throws IOException {
	final int block = MathUtils.checkedDownCast(position / segmentSize);
	final int positionInBlock = (int) (position % segmentSize);
	
	if (position < 0 || block >= numBlocksTotal || (block == numBlocksTotal - 1 && positionInBlock > sizeOfLastBlock)) {
		throw new IllegalArgumentException("Position is out of range");
	}
	
	clear();
	if (reader != null) {
		reader.close();
	}
	
	reader = ioManager.createBlockChannelReader(channelId);
	
	if (block > 0) {
		reader.seekToPosition(((long) block) * segmentSize);
	}
	
	this.numBlocksRemaining = this.numBlocksTotal - block;
	this.numRequestsRemaining = numBlocksRemaining;
	
	for (int i = 0; i < memory.size(); i++) {
		sendReadRequest(memory.get(i));
	}
	
	numBlocksRemaining--;
	seekInput(reader.getNextReturnedBlock(), positionInBlock, numBlocksRemaining == 0 ? sizeOfLastBlock : segmentSize);
}
 
Example 4
Source File: SeekableFileChannelInputView.java    From flink with Apache License 2.0 5 votes vote down vote up
public SeekableFileChannelInputView(IOManager ioManager, FileIOChannel.ID channelId, MemoryManager memManager, List<MemorySegment> memory, int sizeOfLastBlock) throws IOException {
	super(0);
	
	checkNotNull(ioManager);
	checkNotNull(channelId);
	checkNotNull(memManager);
	checkNotNull(memory);
	
	this.ioManager = ioManager;
	this.channelId = channelId;
	this.memManager = memManager;
	this.memory = memory;
	this.sizeOfLastBlock = sizeOfLastBlock;
	this.segmentSize = memManager.getPageSize();
	
	this.reader = ioManager.createBlockChannelReader(channelId);
	
	try {
		final long channelLength = reader.getSize();
		
		final int blockCount =  MathUtils.checkedDownCast(channelLength / segmentSize);
		this.numBlocksTotal = (channelLength % segmentSize == 0) ? blockCount : blockCount + 1;

		this.numBlocksRemaining = this.numBlocksTotal;
		this.numRequestsRemaining = numBlocksRemaining;
		
		for (int i = 0; i < memory.size(); i++) {
			sendReadRequest(memory.get(i));
		}
		
		advance();
	}
	catch (IOException e) {
		memManager.release(memory);
		throw e;
	}
}
 
Example 5
Source File: FsStateBackend.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Private constructor that creates a re-configured copy of the state backend.
 *
 * @param original The state backend to re-configure
 * @param configuration The configuration
 */
private FsStateBackend(FsStateBackend original, ReadableConfig configuration, ClassLoader classLoader) {
	super(original.getCheckpointPath(), original.getSavepointPath(), configuration);

	// if asynchronous snapshots were configured, use that setting,
	// else check the configuration
	this.asynchronousSnapshots = original.asynchronousSnapshots.resolveUndefined(
			configuration.get(CheckpointingOptions.ASYNC_SNAPSHOTS));

	if (getValidFileStateThreshold(original.fileStateThreshold) >= 0) {
		this.fileStateThreshold = original.fileStateThreshold;
	} else {
		final int configuredStateThreshold =
			getValidFileStateThreshold(configuration.get(FS_SMALL_FILE_THRESHOLD).getBytes());

		if (configuredStateThreshold >= 0) {
			this.fileStateThreshold = configuredStateThreshold;
		} else {
			this.fileStateThreshold = MathUtils.checkedDownCast(FS_SMALL_FILE_THRESHOLD.defaultValue().getBytes());

			// because this is the only place we (unlikely) ever log, we lazily
			// create the logger here
			LoggerFactory.getLogger(AbstractFileStateBackend.class).warn(
				"Ignoring invalid file size threshold value ({}): {} - using default value {} instead.",
				FS_SMALL_FILE_THRESHOLD.key(), configuration.get(FS_SMALL_FILE_THRESHOLD).getBytes(),
				FS_SMALL_FILE_THRESHOLD.defaultValue());
		}
	}

	final int bufferSize = original.writeBufferSize >= 0 ?
		original.writeBufferSize :
		configuration.get(CheckpointingOptions.FS_WRITE_BUFFER_SIZE);

	this.writeBufferSize = Math.max(bufferSize, this.fileStateThreshold);
}
 
Example 6
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private static String createBigDataChunk(String pattern, long size) {
	final StringBuilder stringBuilder = new StringBuilder();

	int sampleLength = bytesOf(pattern).length;
	int repeats = MathUtils.checkedDownCast(size) / sampleLength + 100;

	for (int i = 0; i < repeats; i++) {
		stringBuilder.append(pattern);
	}
	return stringBuilder.toString();
}
 
Example 7
Source File: S3RecoverableFsDataOutputStreamTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static byte[] readFileContents(RefCountedFSOutputStream file) throws IOException {
	final byte[] content = new byte[MathUtils.checkedDownCast(file.getPos())];
	File inputFile = file.getInputFile();
	long bytesRead = new FileInputStream(inputFile).read(content, 0,  MathUtils.checkedDownCast(inputFile.length()));
	Assert.assertEquals(file.getPos(), bytesRead);
	return content;
}
 
Example 8
Source File: SlidingWindowAssigner.java    From flink with Apache License 2.0 5 votes vote down vote up
protected SlidingWindowAssigner(long size, long slide, long offset, boolean isEventTime) {
	if (size <= 0 || slide <= 0) {
		throw new IllegalArgumentException(
			"SlidingWindowAssigner parameters must satisfy slide > 0 and size > 0");
	}

	this.size = size;
	this.slide = slide;
	this.offset = offset;
	this.isEventTime = isEventTime;
	this.paneSize = ArithmeticUtils.gcd(size, slide);
	this.numPanesPerWindow = MathUtils.checkedDownCast(size / paneSize);
}
 
Example 9
Source File: FileChannelInputView.java    From flink with Apache License 2.0 5 votes vote down vote up
public FileChannelInputView(BlockChannelReader<MemorySegment> reader, MemoryManager memManager, List<MemorySegment> memory, int sizeOfLastBlock) throws IOException {
	super(0);
	
	checkNotNull(reader);
	checkNotNull(memManager);
	checkNotNull(memory);
	checkArgument(!reader.isClosed());
	checkArgument(memory.size() > 0);
	
	this.reader = reader;
	this.memManager = memManager;
	this.memory = memory;
	this.sizeOfLastBlock = sizeOfLastBlock;
	
	try {
		final long channelLength = reader.getSize();
		final int segmentSize = memManager.getPageSize();
		
		this.numBlocksRemaining = MathUtils.checkedDownCast(channelLength / segmentSize);
		if (channelLength % segmentSize != 0) {
			this.numBlocksRemaining++;
		}
		
		this.numRequestsRemaining = numBlocksRemaining;
		
		for (int i = 0; i < memory.size(); i++) {
			sendReadRequest(memory.get(i));
		}
		
		advance();
	}
	catch (IOException e) {
		memManager.release(memory);
		throw e;
	}
}
 
Example 10
Source File: S3RecoverableFsDataOutputStreamTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static byte[] readFileContents(RefCountedFSOutputStream file) throws IOException {
	final byte[] content = new byte[MathUtils.checkedDownCast(file.getPos())];
	File inputFile = file.getInputFile();
	long bytesRead = new FileInputStream(inputFile).read(content, 0,  MathUtils.checkedDownCast(inputFile.length()));
	Assert.assertEquals(file.getPos(), bytesRead);
	return content;
}
 
Example 11
Source File: SeekableFileChannelInputView.java    From flink with Apache License 2.0 5 votes vote down vote up
public SeekableFileChannelInputView(IOManager ioManager, FileIOChannel.ID channelId, MemoryManager memManager, List<MemorySegment> memory, int sizeOfLastBlock) throws IOException {
	super(0);
	
	checkNotNull(ioManager);
	checkNotNull(channelId);
	checkNotNull(memManager);
	checkNotNull(memory);
	
	this.ioManager = ioManager;
	this.channelId = channelId;
	this.memManager = memManager;
	this.memory = memory;
	this.sizeOfLastBlock = sizeOfLastBlock;
	this.segmentSize = memManager.getPageSize();
	
	this.reader = ioManager.createBlockChannelReader(channelId);
	
	try {
		final long channelLength = reader.getSize();
		
		final int blockCount =  MathUtils.checkedDownCast(channelLength / segmentSize);
		this.numBlocksTotal = (channelLength % segmentSize == 0) ? blockCount : blockCount + 1;

		this.numBlocksRemaining = this.numBlocksTotal;
		this.numRequestsRemaining = numBlocksRemaining;
		
		for (int i = 0; i < memory.size(); i++) {
			sendReadRequest(memory.get(i));
		}
		
		advance();
	}
	catch (IOException e) {
		memManager.release(memory);
		throw e;
	}
}
 
Example 12
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private static String createBigDataChunk(String pattern, long size) {
	final StringBuilder stringBuilder = new StringBuilder();

	int sampleLength = bytesOf(pattern).length;
	int repeats = MathUtils.checkedDownCast(size) / sampleLength + 100;

	for (int i = 0; i < repeats; i++) {
		stringBuilder.append(pattern);
	}
	return stringBuilder.toString();
}
 
Example 13
Source File: S3RecoverableFsDataOutputStreamTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static byte[] readFileContents(RefCountedFSOutputStream file) throws IOException {
	final byte[] content = new byte[MathUtils.checkedDownCast(file.getPos())];
	File inputFile = file.getInputFile();
	long bytesRead = new FileInputStream(inputFile).read(content, 0,  MathUtils.checkedDownCast(inputFile.length()));
	Assert.assertEquals(file.getPos(), bytesRead);
	return content;
}
 
Example 14
Source File: SlidingWindowAssigner.java    From flink with Apache License 2.0 5 votes vote down vote up
protected SlidingWindowAssigner(long size, long slide, long offset, boolean isEventTime) {
	if (size <= 0 || slide <= 0) {
		throw new IllegalArgumentException(
			"SlidingWindowAssigner parameters must satisfy slide > 0 and size > 0");
	}

	this.size = size;
	this.slide = slide;
	this.offset = offset;
	this.isEventTime = isEventTime;
	this.paneSize = ArithmeticUtils.gcd(size, slide);
	this.numPanesPerWindow = MathUtils.checkedDownCast(size / paneSize);
}
 
Example 15
Source File: FileChannelInputView.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public FileChannelInputView(BlockChannelReader<MemorySegment> reader, MemoryManager memManager, List<MemorySegment> memory, int sizeOfLastBlock) throws IOException {
	super(0);
	
	checkNotNull(reader);
	checkNotNull(memManager);
	checkNotNull(memory);
	checkArgument(!reader.isClosed());
	checkArgument(memory.size() > 0);
	
	this.reader = reader;
	this.memManager = memManager;
	this.memory = memory;
	this.sizeOfLastBlock = sizeOfLastBlock;
	
	try {
		final long channelLength = reader.getSize();
		final int segmentSize = memManager.getPageSize();
		
		this.numBlocksRemaining = MathUtils.checkedDownCast(channelLength / segmentSize);
		if (channelLength % segmentSize != 0) {
			this.numBlocksRemaining++;
		}
		
		this.numRequestsRemaining = numBlocksRemaining;
		
		for (int i = 0; i < memory.size(); i++) {
			sendReadRequest(memory.get(i));
		}
		
		advance();
	}
	catch (IOException e) {
		memManager.release(memory);
		throw e;
	}
}
 
Example 16
Source File: SeekableFileChannelInputView.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public void seek(long position) throws IOException {
	final int block = MathUtils.checkedDownCast(position / segmentSize);
	final int positionInBlock = (int) (position % segmentSize);
	
	if (position < 0 || block >= numBlocksTotal || (block == numBlocksTotal - 1 && positionInBlock > sizeOfLastBlock)) {
		throw new IllegalArgumentException("Position is out of range");
	}
	
	clear();
	if (reader != null) {
		reader.close();
	}
	
	reader = ioManager.createBlockChannelReader(channelId);
	
	if (block > 0) {
		reader.seekToPosition(((long) block) * segmentSize);
	}
	
	this.numBlocksRemaining = this.numBlocksTotal - block;
	this.numRequestsRemaining = numBlocksRemaining;
	
	for (int i = 0; i < memory.size(); i++) {
		sendReadRequest(memory.get(i));
	}
	
	numBlocksRemaining--;
	seekInput(reader.getNextReturnedBlock(), positionInBlock, numBlocksRemaining == 0 ? sizeOfLastBlock : segmentSize);
}
 
Example 17
Source File: SeekableFileChannelInputView.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public SeekableFileChannelInputView(IOManager ioManager, FileIOChannel.ID channelId, MemoryManager memManager, List<MemorySegment> memory, int sizeOfLastBlock) throws IOException {
	super(0);
	
	checkNotNull(ioManager);
	checkNotNull(channelId);
	checkNotNull(memManager);
	checkNotNull(memory);
	
	this.ioManager = ioManager;
	this.channelId = channelId;
	this.memManager = memManager;
	this.memory = memory;
	this.sizeOfLastBlock = sizeOfLastBlock;
	this.segmentSize = memManager.getPageSize();
	
	this.reader = ioManager.createBlockChannelReader(channelId);
	
	try {
		final long channelLength = reader.getSize();
		
		final int blockCount =  MathUtils.checkedDownCast(channelLength / segmentSize);
		this.numBlocksTotal = (channelLength % segmentSize == 0) ? blockCount : blockCount + 1;

		this.numBlocksRemaining = this.numBlocksTotal;
		this.numRequestsRemaining = numBlocksRemaining;
		
		for (int i = 0; i < memory.size(); i++) {
			sendReadRequest(memory.get(i));
		}
		
		advance();
	}
	catch (IOException e) {
		memManager.release(memory);
		throw e;
	}
}
 
Example 18
Source File: HadoopS3RecoverableWriterITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static String createBigDataChunk(String pattern, long size) {
	final StringBuilder stringBuilder = new StringBuilder();

	int sampleLength = bytesOf(pattern).length;
	int repeats = MathUtils.checkedDownCast(size) / sampleLength + 100;

	for (int i = 0; i < repeats; i++) {
		stringBuilder.append(pattern);
	}
	return stringBuilder.toString();
}
 
Example 19
Source File: FsStateBackend.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the threshold below which state is stored as part of the metadata, rather than in files.
 * This threshold ensures that the backend does not create a large amount of very small files,
 * where potentially the file pointers are larger than the state itself.
 *
 * <p>If not explicitly configured, this is the default value of
 * {@link CheckpointingOptions#FS_SMALL_FILE_THRESHOLD}.
 *
 * @return The file size threshold, in bytes.
 */
public int getMinFileSizeThreshold() {
	return fileStateThreshold >= 0 ?
			fileStateThreshold :
			MathUtils.checkedDownCast(FS_SMALL_FILE_THRESHOLD.defaultValue().getBytes());
}