Java Code Examples for org.apache.flink.runtime.io.network.buffer.Buffer#getSize()

The following examples show how to use org.apache.flink.runtime.io.network.buffer.Buffer#getSize() . 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: SpillingAdaptiveSpanningRecordDeserializer.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void setNextBuffer(Buffer buffer) throws IOException {
	currentBuffer = buffer;

	int offset = buffer.getMemorySegmentOffset();
	MemorySegment segment = buffer.getMemorySegment();
	int numBytes = buffer.getSize();

	// check if some spanning record deserialization is pending
	if (this.spanningWrapper.getNumGatheredBytes() > 0) {
		this.spanningWrapper.addNextChunkFromMemorySegment(segment, offset, numBytes);
	}
	else {
		this.nonSpanningWrapper.initializeFromMemorySegment(segment, offset, numBytes + offset);
	}
}
 
Example 2
Source File: BoundedDataTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
private static void readInts(BoundedData.Reader reader, int numBuffersExpected, int numInts) throws IOException {
	Buffer b;
	int nextValue = 0;
	int numBuffers = 0;

	while ((b = reader.nextBuffer()) != null) {
		final int numIntsInBuffer = b.getSize() / 4;
		BufferBuilderTestUtils.validateBufferWithAscendingInts(b, numIntsInBuffer, nextValue);
		nextValue += numIntsInBuffer;
		numBuffers++;

		b.recycleBuffer();
	}

	assertEquals(numBuffersExpected, numBuffers);
	assertThat(nextValue, Matchers.greaterThanOrEqualTo(numInts));
}
 
Example 3
Source File: BoundedDataTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
private static void readInts(BoundedData.Reader reader, int numBuffersExpected, int numInts) throws IOException {
	Buffer b;
	int nextValue = 0;
	int numBuffers = 0;

	while ((b = reader.nextBuffer()) != null) {
		final int numIntsInBuffer = b.getSize() / 4;
		if (compressionEnabled && b.isCompressed()) {
			Buffer decompressedBuffer = DECOMPRESSOR.decompressToIntermediateBuffer(b);
			BufferBuilderTestUtils.validateBufferWithAscendingInts(decompressedBuffer, numIntsInBuffer, nextValue);
		} else {
			BufferBuilderTestUtils.validateBufferWithAscendingInts(b, numIntsInBuffer, nextValue);
		}
		nextValue += numIntsInBuffer;
		numBuffers++;

		b.recycleBuffer();
	}

	assertEquals(numBuffersExpected, numBuffers);
	assertThat(nextValue, Matchers.greaterThanOrEqualTo(numInts));
}
 
Example 4
Source File: BufferReaderWriterUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
static long writeToByteChannelIfBelowSize(
		FileChannel channel,
		Buffer buffer,
		ByteBuffer[] arrayWithHeaderBuffer,
		long bytesLeft) throws IOException {

	if (bytesLeft >= HEADER_LENGTH + buffer.getSize()) {
		return writeToByteChannel(channel, buffer, arrayWithHeaderBuffer);
	}

	return -1L;
}
 
Example 5
Source File: BufferReaderWriterUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
static boolean writeBuffer(Buffer buffer, ByteBuffer memory) {
	final int bufferSize = buffer.getSize();

	if (memory.remaining() < bufferSize + HEADER_LENGTH) {
		return false;
	}

	memory.putShort(buffer.isBuffer() ? HEADER_VALUE_IS_BUFFER : HEADER_VALUE_IS_EVENT);
	memory.putShort(buffer.isCompressed() ? BUFFER_IS_COMPRESSED : BUFFER_IS_NOT_COMPRESSED);
	memory.putInt(bufferSize);
	memory.put(buffer.getNioBufferReadable());
	return true;
}
 
Example 6
Source File: BufferOrEvent.java    From flink with Apache License 2.0 5 votes vote down vote up
public BufferOrEvent(Buffer buffer, InputChannelInfo channelInfo, boolean moreAvailable) {
	this.buffer = checkNotNull(buffer);
	this.event = null;
	this.channelInfo = channelInfo;
	this.moreAvailable = moreAvailable;
	this.size = buffer.getSize();
}
 
Example 7
Source File: SingleInputGate.java    From flink with Apache License 2.0 5 votes vote down vote up
private BufferOrEvent transformEvent(
		Buffer buffer,
		boolean moreAvailable,
		InputChannel currentChannel) throws IOException, InterruptedException {
	final AbstractEvent event;
	try {
		event = EventSerializer.fromBuffer(buffer, getClass().getClassLoader());
	} finally {
		buffer.recycleBuffer();
	}

	if (event.getClass() == EndOfPartitionEvent.class) {
		channelsWithEndOfPartitionEvents.set(currentChannel.getChannelIndex());

		if (channelsWithEndOfPartitionEvents.cardinality() == numberOfInputChannels) {
			// Because of race condition between:
			// 1. releasing inputChannelsWithData lock in this method and reaching this place
			// 2. empty data notification that re-enqueues a channel
			// we can end up with moreAvailable flag set to true, while we expect no more data.
			checkState(!moreAvailable || !pollNext().isPresent());
			moreAvailable = false;
			hasReceivedAllEndOfPartitionEvents = true;
			markAvailable();
		}

		currentChannel.releaseAllResources();
	}

	return new BufferOrEvent(event, currentChannel.getChannelInfo(), moreAvailable, buffer.getSize());
}
 
Example 8
Source File: BufferFileWriterReaderTest.java    From flink with Apache License 2.0 5 votes vote down vote up
static int verifyBufferFilledWithAscendingNumbers(Buffer buffer, int currentNumber) {
	MemorySegment segment = buffer.getMemorySegment();

	int size = buffer.getSize();

	for (int i = 0; i < size; i += 4) {
		if (segment.getInt(i) != currentNumber++) {
			throw new IllegalStateException("Read unexpected number from buffer.");
		}
	}

	return currentNumber;
}
 
Example 9
Source File: SpillingAdaptiveSpanningRecordDeserializer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void setNextBuffer(Buffer buffer) throws IOException {
	currentBuffer = buffer;

	int offset = buffer.getMemorySegmentOffset();
	MemorySegment segment = buffer.getMemorySegment();
	int numBytes = buffer.getSize();

	// check if some spanning record deserialization is pending
	if (spanningWrapper.getNumGatheredBytes() > 0) {
		spanningWrapper.addNextChunkFromMemorySegment(segment, offset, numBytes);
	} else {
		nonSpanningWrapper.initializeFromMemorySegment(segment, offset, numBytes + offset);
	}
}
 
Example 10
Source File: BufferFileWriterReaderTest.java    From flink with Apache License 2.0 5 votes vote down vote up
static int verifyBufferFilledWithAscendingNumbers(Buffer buffer, int currentNumber) {
	MemorySegment segment = buffer.getMemorySegment();

	int size = buffer.getSize();

	for (int i = 0; i < size; i += 4) {
		if (segment.getInt(i) != currentNumber++) {
			throw new IllegalStateException("Read unexpected number from buffer.");
		}
	}

	return currentNumber;
}
 
Example 11
Source File: BufferReaderWriterUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
static boolean writeBuffer(Buffer buffer, ByteBuffer memory) {
	final int bufferSize = buffer.getSize();

	if (memory.remaining() < bufferSize + HEADER_LENGTH) {
		return false;
	}

	memory.putInt(buffer.isBuffer() ? HEADER_VALUE_IS_BUFFER : HEADER_VALUE_IS_EVENT);
	memory.putInt(bufferSize);
	memory.put(buffer.getNioBufferReadable());
	return true;
}
 
Example 12
Source File: BufferOrEvent.java    From flink with Apache License 2.0 5 votes vote down vote up
public BufferOrEvent(Buffer buffer, int channelIndex, boolean moreAvailable) {
	this.buffer = checkNotNull(buffer);
	this.event = null;
	this.channelIndex = channelIndex;
	this.moreAvailable = moreAvailable;
	this.size = buffer.getSize();
}
 
Example 13
Source File: SingleInputGate.java    From flink with Apache License 2.0 5 votes vote down vote up
private BufferOrEvent transformToBufferOrEvent(
		Buffer buffer,
		boolean moreAvailable,
		InputChannel currentChannel) throws IOException, InterruptedException {
	if (buffer.isBuffer()) {
		return new BufferOrEvent(buffer, currentChannel.getChannelIndex(), moreAvailable);
	}
	else {
		final AbstractEvent event;
		try {
			event = EventSerializer.fromBuffer(buffer, getClass().getClassLoader());
		}
		finally {
			buffer.recycleBuffer();
		}

		if (event.getClass() == EndOfPartitionEvent.class) {
			channelsWithEndOfPartitionEvents.set(currentChannel.getChannelIndex());

			if (channelsWithEndOfPartitionEvents.cardinality() == numberOfInputChannels) {
				// Because of race condition between:
				// 1. releasing inputChannelsWithData lock in this method and reaching this place
				// 2. empty data notification that re-enqueues a channel
				// we can end up with moreAvailable flag set to true, while we expect no more data.
				checkState(!moreAvailable || !pollNext().isPresent());
				moreAvailable = false;
				hasReceivedAllEndOfPartitionEvents = true;
				markAvailable();
			}

			currentChannel.notifySubpartitionConsumed();
			currentChannel.releaseAllResources();
		}

		return new BufferOrEvent(event, currentChannel.getChannelIndex(), moreAvailable, buffer.getSize());
	}
}
 
Example 14
Source File: BufferFileWriterReaderTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
static int verifyBufferFilledWithAscendingNumbers(Buffer buffer, int currentNumber) {
	MemorySegment segment = buffer.getMemorySegment();

	int size = buffer.getSize();

	for (int i = 0; i < size; i += 4) {
		if (segment.getInt(i) != currentNumber++) {
			throw new IllegalStateException("Read unexpected number from buffer.");
		}
	}

	return currentNumber;
}
 
Example 15
Source File: BufferReaderWriterUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
static long writeToByteChannelIfBelowSize(
		FileChannel channel,
		Buffer buffer,
		ByteBuffer[] arrayWithHeaderBuffer,
		long bytesLeft) throws IOException {

	if (bytesLeft >= HEADER_LENGTH + buffer.getSize()) {
		return writeToByteChannel(channel, buffer, arrayWithHeaderBuffer);
	}

	return -1L;
}
 
Example 16
Source File: SpillableSubpartition.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
long spillFinishedBufferConsumers(boolean forceFinishRemainingBuffers) throws IOException {
	long spilledBytes = 0;

	while (!buffers.isEmpty()) {
		BufferConsumer bufferConsumer = buffers.getFirst();
		Buffer buffer = bufferConsumer.build();
		updateStatistics(buffer);
		int bufferSize = buffer.getSize();
		spilledBytes += bufferSize;

		// NOTE we may be in the process of finishing the subpartition where any buffer should
		// be treated as if it was finished!
		if (bufferConsumer.isFinished() || forceFinishRemainingBuffers) {
			if (bufferSize > 0) {
				spillWriter.writeBlock(buffer);
			} else {
				// If we skip a buffer for the spill writer, we need to adapt the backlog accordingly
				decreaseBuffersInBacklog(buffer);
				buffer.recycleBuffer();
			}
			bufferConsumer.close();
			buffers.poll();
		} else {
			// If there is already data, we need to spill it anyway, since we do not get this
			// slice from the buffer consumer again during the next build.
			// BEWARE: by doing so, we increase the actual number of buffers in the spill writer!
			if (bufferSize > 0) {
				spillWriter.writeBlock(buffer);
				increaseBuffersInBacklog(bufferConsumer);
			} else {
				buffer.recycleBuffer();
			}

			return spilledBytes;
		}
	}
	return spilledBytes;
}
 
Example 17
Source File: PipelinedSubpartition.java    From flink with Apache License 2.0 4 votes vote down vote up
private void updateStatistics(Buffer buffer) {
	totalNumberOfBytes += buffer.getSize();
}
 
Example 18
Source File: PipelinedSubpartition.java    From flink with Apache License 2.0 4 votes vote down vote up
private void updateStatistics(Buffer buffer) {
	totalNumberOfBytes += buffer.getSize();
}
 
Example 19
Source File: ResultSubpartition.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
protected void updateStatistics(Buffer buffer) {
	totalNumberOfBytes += buffer.getSize();
}
 
Example 20
Source File: SpillableSubpartitionView.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
int releaseMemory() throws IOException {
	synchronized (buffers) {
		if (spilledView != null || nextBuffer == null) {
			// Already spilled or nothing in-memory
			return 0;
		} else {
			// We don't touch next buffer, because a notification has
			// already been sent for it. Only when it is consumed, will
			// it be recycled.

			// Create the spill writer and write all buffers to disk
			BufferFileWriter spillWriter = ioManager.createBufferFileWriter(ioManager.createChannel());

			long spilledBytes = 0;

			int numBuffers = buffers.size();
			for (int i = 0; i < numBuffers; i++) {
				try (BufferConsumer bufferConsumer = buffers.remove()) {
					Buffer buffer = bufferConsumer.build();
					checkState(bufferConsumer.isFinished(), "BufferConsumer must be finished before " +
						"spilling. Otherwise we would not be able to simply remove it from the queue. This should " +
						"be guaranteed by creating ResultSubpartitionView only once Subpartition isFinished.");
					parent.updateStatistics(buffer);
					spilledBytes += buffer.getSize();
					spillWriter.writeBlock(buffer);
				}
			}

			spilledView = new SpilledSubpartitionView(
				parent,
				memorySegmentSize,
				spillWriter,
				numBuffers,
				listener);

			LOG.debug("Spilling {} bytes for sub partition {} of {}.",
				spilledBytes,
				parent.index,
				parent.parent.getPartitionId());

			return numBuffers;
		}
	}
}