org.apache.flink.runtime.io.network.buffer.BufferConsumer Java Examples

The following examples show how to use org.apache.flink.runtime.io.network.buffer.BufferConsumer. 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: LocalInputChannel.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public boolean notifyPriorityEvent(BufferConsumer eventBufferConsumer) throws IOException {
	if (inputGate.getBufferReceivedListener() == null) {
		// in rare cases and very low checkpointing intervals, we may receive the first barrier, before setting
		// up CheckpointedInputGate
		return false;
	}
	Buffer buffer = eventBufferConsumer.build();
	try {
		CheckpointBarrier event = parseCheckpointBarrierOrNull(buffer);
		if (event == null) {
			throw new IllegalStateException("Currently only checkpoint barriers are known priority events");
		} else if (event.isCheckpoint()) {
			inputGate.getBufferReceivedListener().notifyBarrierReceived(event, channelInfo);
		}
	} finally {
		buffer.recycleBuffer();
	}
	// already processed
	return true;
}
 
Example #2
Source File: BoundedBlockingSubpartitionWriteReadTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private static void writeLongs(BoundedBlockingSubpartition partition, long nums) throws IOException {
	final MemorySegment memory = MemorySegmentFactory.allocateUnpooledSegment(BUFFER_SIZE);

	long l = 0;
	while (nums > 0) {
		int pos = 0;
		for (; nums > 0 && pos <= memory.size() - 8; pos += 8) {
			memory.putLongBigEndian(pos, l++);
			nums--;
		}

		partition.add(new BufferConsumer(memory, (ignored) -> {}, pos, Buffer.DataType.DATA_BUFFER));

		// we need to flush after every buffer as long as the add() contract is that
		// buffer are immediately added and can be filled further after that (for low latency
		// streaming data exchanges)
		partition.flush();
	}
}
 
Example #3
Source File: ResultPartitionTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link ResultPartition#addBufferConsumer(BufferConsumer, int)} on a working partition.
 *
 * @param partitionType the result partition type to set up
 */
private void testAddOnPartition(final ResultPartitionType partitionType) throws Exception {
	ResultPartitionConsumableNotifier notifier = mock(ResultPartitionConsumableNotifier.class);
	JobID jobId = new JobID();
	TaskActions taskActions = new NoOpTaskActions();
	ResultPartitionWriter consumableNotifyingPartitionWriter = createConsumableNotifyingResultPartitionWriter(
		partitionType,
		taskActions,
		jobId,
		notifier);
	BufferConsumer bufferConsumer = createFilledBufferConsumer(BufferBuilderTestUtils.BUFFER_SIZE);
	try {
		// partition.add() adds the bufferConsumer without recycling it (if not spilling)
		consumableNotifyingPartitionWriter.addBufferConsumer(bufferConsumer, 0);
		assertFalse("bufferConsumer should not be recycled (still in the queue)", bufferConsumer.isRecycled());
	} finally {
		if (!bufferConsumer.isRecycled()) {
			bufferConsumer.close();
		}
		// should have been notified for pipelined partitions
		if (partitionType.isPipelined()) {
			verify(notifier, times(1))
				.notifyPartitionConsumable(eq(jobId), eq(consumableNotifyingPartitionWriter.getPartitionId()), eq(taskActions));
		}
	}
}
 
Example #4
Source File: BroadcastRecordWriter.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * The request could be from broadcast or non-broadcast modes like {@link #randomEmit(IOReadableWritable)}.
 *
 * <p>For non-broadcast, the created {@link BufferConsumer} is only for the target channel.
 *
 * <p>For broadcast, all the channels share the same requested {@link BufferBuilder} and the created
 * {@link BufferConsumer} is copied for every channel.
 */
@Override
public BufferBuilder requestNewBufferBuilder(int targetChannel) throws IOException, InterruptedException {
	checkState(bufferBuilder == null || bufferBuilder.isFinished());

	BufferBuilder builder = super.requestNewBufferBuilder(targetChannel);
	if (randomTriggered) {
		addBufferConsumer(randomTriggeredConsumer = builder.createBufferConsumer(), targetChannel);
	} else {
		try (BufferConsumer bufferConsumer = builder.createBufferConsumer()) {
			for (int channel = 0; channel < numberOfChannels; channel++) {
				addBufferConsumer(bufferConsumer.copy(), channel);
			}
		}
	}

	bufferBuilder = builder;
	return builder;
}
 
Example #5
Source File: AbstractCollectingResultPartitionWriter.java    From flink with Apache License 2.0 6 votes vote down vote up
private void processBufferConsumers() throws IOException {
	while (!bufferConsumers.isEmpty()) {
		BufferConsumer bufferConsumer = bufferConsumers.peek();
		Buffer buffer = bufferConsumer.build();
		try {
			deserializeBuffer(buffer);
			if (!bufferConsumer.isFinished()) {
				break;
			}
			bufferConsumers.pop().close();
		}
		finally {
			buffer.recycleBuffer();
		}
	}
}
 
Example #6
Source File: SubpartitionTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddAfterRelease() throws Exception {
	final ResultSubpartition subpartition = createSubpartition();

	try {
		subpartition.release();

		BufferConsumer bufferConsumer = createFilledFinishedBufferConsumer(4096);

		assertFalse(subpartition.add(bufferConsumer));
		assertTrue(bufferConsumer.isRecycled());

	} finally {
		if (subpartition != null) {
			subpartition.release();
		}
	}
}
 
Example #7
Source File: SpillableSubpartitionTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link SpillableSubpartition#add(BufferConsumer)} with a released partition.
 *
 * @param spilled
 * 		whether the partition should be spilled to disk (<tt>true</tt>) or not (<tt>false</tt>,
 * 		spillable).
 */
private void testAddOnReleasedPartition(boolean spilled) throws Exception {
	SpillableSubpartition partition = createSubpartition();
	partition.release();
	if (spilled) {
		assertEquals(0, partition.releaseMemory());
	}

	BufferConsumer buffer = createFilledBufferConsumer(BUFFER_DATA_SIZE, BUFFER_DATA_SIZE);
	boolean bufferRecycled;
	try {
		partition.add(buffer);
	} finally {
		bufferRecycled = buffer.isRecycled();
		if (!bufferRecycled) {
			buffer.close();
		}
	}
	if (!bufferRecycled) {
		Assert.fail("buffer not recycled");
	}
	assertEquals(0, partition.getTotalNumberOfBuffers());
	assertEquals(0, partition.getTotalNumberOfBytes());
}
 
Example #8
Source File: EventSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testToBufferConsumer() throws IOException {
	for (AbstractEvent evt : events) {
		BufferConsumer bufferConsumer = EventSerializer.toBufferConsumer(evt);

		assertFalse(bufferConsumer.isBuffer());
		assertTrue(bufferConsumer.isFinished());
		assertTrue(bufferConsumer.isDataAvailable());
		assertFalse(bufferConsumer.isRecycled());

		if (evt instanceof CheckpointBarrier) {
			assertTrue(bufferConsumer.build().getDataType().isBlockingUpstream());
		} else {
			assertEquals(Buffer.DataType.EVENT_BUFFER, bufferConsumer.build().getDataType());
		}
	}
}
 
Example #9
Source File: PipelinedSubpartition.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private boolean add(BufferConsumer bufferConsumer, boolean finish) {
	checkNotNull(bufferConsumer);

	final boolean notifyDataAvailable;
	synchronized (buffers) {
		if (isFinished || isReleased) {
			bufferConsumer.close();
			return false;
		}

		// Add the bufferConsumer and update the stats
		buffers.add(bufferConsumer);
		updateStatistics(bufferConsumer);
		increaseBuffersInBacklog(bufferConsumer);
		notifyDataAvailable = shouldNotifyDataAvailable() || finish;

		isFinished |= finish;
	}

	if (notifyDataAvailable) {
		notifyDataAvailable();
	}

	return true;
}
 
Example #10
Source File: SubpartitionTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddAfterFinish() throws Exception {
	final ResultSubpartition subpartition = createSubpartition();

	try {
		subpartition.finish();
		assertEquals(1, subpartition.getTotalNumberOfBuffers());
		assertEquals(0, subpartition.getBuffersInBacklog());

		BufferConsumer bufferConsumer = createFilledBufferConsumer(4096, 4096);

		assertFalse(subpartition.add(bufferConsumer));
		assertTrue(bufferConsumer.isRecycled());

		assertEquals(1, subpartition.getTotalNumberOfBuffers());
		assertEquals(0, subpartition.getBuffersInBacklog());
	} finally {
		if (subpartition != null) {
			subpartition.release();
		}
	}
}
 
Example #11
Source File: PipelinedSubpartition.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean add(BufferConsumer bufferConsumer, boolean finish) {
	checkNotNull(bufferConsumer);

	final boolean notifyDataAvailable;
	synchronized (buffers) {
		if (isFinished || isReleased) {
			bufferConsumer.close();
			return false;
		}

		// Add the bufferConsumer and update the stats
		buffers.add(bufferConsumer);
		updateStatistics(bufferConsumer);
		increaseBuffersInBacklog(bufferConsumer);
		notifyDataAvailable = shouldNotifyDataAvailable() || finish;

		isFinished |= finish;
	}

	if (notifyDataAvailable) {
		notifyDataAvailable();
	}

	return true;
}
 
Example #12
Source File: ResultPartitionTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link ResultPartition#addBufferConsumer} on a partition which has already been released.
 *
 * @param pipelined the result partition type to set up
 */
protected void testAddOnReleasedPartition(final ResultPartitionType pipelined)
	throws Exception {
	BufferConsumer bufferConsumer = createFilledBufferConsumer(BufferBuilderTestUtils.BUFFER_SIZE);
	ResultPartitionConsumableNotifier notifier = mock(ResultPartitionConsumableNotifier.class);
	try {
		ResultPartition partition = createPartition(notifier, pipelined, true);
		partition.release();
		// partition.add() silently drops the bufferConsumer but recycles it
		partition.addBufferConsumer(bufferConsumer, 0);
		assertTrue(partition.isReleased());
	} finally {
		if (!bufferConsumer.isRecycled()) {
			bufferConsumer.close();
			Assert.fail("bufferConsumer not recycled");
		}
		// should not have notified either
		verify(notifier, never()).notifyPartitionConsumable(any(JobID.class), any(ResultPartitionID.class), any(TaskActions.class));
	}
}
 
Example #13
Source File: PipelinedSubpartition.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean add(BufferConsumer bufferConsumer, boolean finish, boolean insertAsHead) {
	checkNotNull(bufferConsumer);

	final boolean notifyDataAvailable;
	synchronized (buffers) {
		if (isFinished || isReleased) {
			bufferConsumer.close();
			return false;
		}

		// Add the bufferConsumer and update the stats
		handleAddingBarrier(bufferConsumer, insertAsHead);
		updateStatistics(bufferConsumer);
		increaseBuffersInBacklog(bufferConsumer);
		notifyDataAvailable = insertAsHead || finish || shouldNotifyDataAvailable();

		isFinished |= finish;
	}

	if (notifyDataAvailable) {
		notifyDataAvailable();
	}

	return true;
}
 
Example #14
Source File: RecordWriterTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private void verifyBroadcastBufferOrEventIndependence(boolean broadcastEvent) throws Exception {
	@SuppressWarnings("unchecked")
	ArrayDeque<BufferConsumer>[] queues = new ArrayDeque[]{new ArrayDeque(), new ArrayDeque()};

	ResultPartitionWriter partition =
		new CollectingPartitionWriter(queues, new TestPooledBufferProvider(Integer.MAX_VALUE));
	RecordWriter<IntValue> writer = new RecordWriterBuilder().build(partition);

	if (broadcastEvent) {
		writer.broadcastEvent(EndOfPartitionEvent.INSTANCE);
	} else {
		writer.broadcastEmit(new IntValue(0));
	}

	// verify added to all queues
	assertEquals(1, queues[0].size());
	assertEquals(1, queues[1].size());

	// these two buffers may share the memory but not the indices!
	Buffer buffer1 = buildSingleBuffer(queues[0].remove());
	Buffer buffer2 = buildSingleBuffer(queues[1].remove());
	assertEquals(0, buffer1.getReaderIndex());
	assertEquals(0, buffer2.getReaderIndex());
	buffer1.setReaderIndex(1);
	assertEquals("Buffer 2 shares the same reader index as buffer 1", 0, buffer2.getReaderIndex());
}
 
Example #15
Source File: SpillableSubpartition.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private boolean add(BufferConsumer bufferConsumer, boolean forceFinishRemainingBuffers)
		throws IOException {
	checkNotNull(bufferConsumer);

	synchronized (buffers) {
		if (isFinished || isReleased) {
			bufferConsumer.close();
			return false;
		}

		buffers.add(bufferConsumer);
		// The number of buffers are needed later when creating
		// the read views. If you ever remove this line here,
		// make sure to still count the number of buffers.
		updateStatistics(bufferConsumer);
		increaseBuffersInBacklog(bufferConsumer);

		if (spillWriter != null) {
			spillFinishedBufferConsumers(forceFinishRemainingBuffers);
		}
	}
	return true;
}
 
Example #16
Source File: ResultPartitionTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link ResultPartition#addBufferConsumer} on a partition which has already finished.
 *
 * @param pipelined the result partition type to set up
 */
protected void testAddOnFinishedPartition(final ResultPartitionType pipelined)
	throws Exception {
	BufferConsumer bufferConsumer = createFilledBufferConsumer(BufferBuilderTestUtils.BUFFER_SIZE);
	ResultPartitionConsumableNotifier notifier = mock(ResultPartitionConsumableNotifier.class);
	try {
		ResultPartition partition = createPartition(notifier, pipelined, true);
		partition.finish();
		reset(notifier);
		// partition.add() should fail
		partition.addBufferConsumer(bufferConsumer, 0);
		Assert.fail("exception expected");
	} catch (IllegalStateException e) {
		// expected => ignored
	} finally {
		if (!bufferConsumer.isRecycled()) {
			bufferConsumer.close();
			Assert.fail("bufferConsumer not recycled");
		}
		// should not have notified either
		verify(notifier, never()).notifyPartitionConsumable(any(JobID.class), any(ResultPartitionID.class), any(TaskActions.class));
	}
}
 
Example #17
Source File: SubpartitionTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void verifyViewReleasedAfterParentRelease(ResultSubpartition partition) throws Exception {
	// Add a bufferConsumer
	BufferConsumer bufferConsumer = createFilledBufferConsumer(BufferBuilderTestUtils.BUFFER_SIZE);
	partition.add(bufferConsumer);
	partition.finish();

	// Create the view
	BufferAvailabilityListener listener = mock(BufferAvailabilityListener.class);
	ResultSubpartitionView view = partition.createReadView(listener);

	// The added bufferConsumer and end-of-partition event
	assertNotNull(view.getNextBuffer());
	assertNotNull(view.getNextBuffer());

	// Release the parent
	assertFalse(view.isReleased());
	partition.release();

	// Verify that parent release is reflected at partition view
	assertTrue(view.isReleased());
}
 
Example #18
Source File: ChannelStateSerializerImplTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadToBufferBuilder() throws IOException {
	byte[] data = generateData(100);
	BufferBuilder bufferBuilder = new BufferBuilder(HeapMemorySegment.FACTORY.allocateUnpooledSegment(data.length, null), FreeingBufferRecycler.INSTANCE);
	BufferConsumer bufferConsumer = bufferBuilder.createBufferConsumer();

	new ChannelStateSerializerImpl().readData(new ByteArrayInputStream(data), wrap(bufferBuilder), Integer.MAX_VALUE);

	assertFalse(bufferBuilder.isFinished());

	bufferBuilder.finish();
	Buffer buffer = bufferConsumer.build();

	assertEquals(data.length, buffer.readableBytes());
	byte[] actual = new byte[buffer.readableBytes()];
	buffer.asByteBuf().readBytes(actual);
	assertArrayEquals(data, actual);
}
 
Example #19
Source File: RecordWriterTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static BufferOrEvent parseBuffer(BufferConsumer bufferConsumer, int targetChannel) throws IOException {
	Buffer buffer = buildSingleBuffer(bufferConsumer);
	if (buffer.isBuffer()) {
		return new BufferOrEvent(buffer, targetChannel);
	} else {
		// is event:
		AbstractEvent event = EventSerializer.fromBuffer(buffer, RecordWriterTest.class.getClassLoader());
		buffer.recycleBuffer(); // the buffer is not needed anymore
		return new BufferOrEvent(event, targetChannel);
	}
}
 
Example #20
Source File: LocalInputChannelTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoNotifyOnSavepoint() throws IOException {
	TestBufferReceivedListener listener = new TestBufferReceivedListener();
	SingleInputGate inputGate = new SingleInputGateBuilder().build();
	inputGate.registerBufferReceivedListener(listener);
	LocalInputChannel channel = InputChannelBuilder.newBuilder().buildLocalChannel(inputGate);
	CheckpointBarrier barrier = new CheckpointBarrier(123L, 123L, new CheckpointOptions(SAVEPOINT, CheckpointStorageLocationReference.getDefault()));
	channel.notifyPriorityEvent(new BufferConsumer(toBuffer(barrier).getMemorySegment(), FreeingBufferRecycler.INSTANCE, getDataType(barrier)));
	channel.checkError();
	assertTrue(listener.notifiedOnBarriers.isEmpty());
}
 
Example #21
Source File: InputGateConcurrentTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void go() throws Exception {
	final BufferConsumer bufferConsumer = BufferBuilderTestUtils.createFilledBufferConsumer(100);
	int nextYield = numTotal - yieldAfter;

	for (int i = numTotal; i > 0;) {
		final int nextChannel = rnd.nextInt(sources.length);
		final int chunk = Math.min(i, rnd.nextInt(maxChunk) + 1);

		final Source next = sources[nextChannel];

		for (int k = chunk; k > 0; --k) {
			next.addBufferConsumer(bufferConsumer.copy());
		}

		i -= chunk;

		if (i <= nextYield) {
			nextYield -= yieldAfter;
			//noinspection CallToThreadYield
			Thread.yield();
		}
	}

	for (Source source : sources) {
		source.flush();
	}
}
 
Example #22
Source File: RecordWriterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that event buffers are properly recycled when broadcasting events
 * to multiple channels.
 */
@Test
public void testBroadcastEventBufferReferenceCounting() throws Exception {

	@SuppressWarnings("unchecked")
	ArrayDeque<BufferConsumer>[] queues = new ArrayDeque[] { new ArrayDeque(), new ArrayDeque() };

	ResultPartitionWriter partition =
		new CollectingPartitionWriter(queues, new TestPooledBufferProvider(Integer.MAX_VALUE));
	RecordWriter<?> writer = new RecordWriterBuilder().build(partition);

	writer.broadcastEvent(EndOfPartitionEvent.INSTANCE);

	// Verify added to all queues
	assertEquals(1, queues[0].size());
	assertEquals(1, queues[1].size());

	// get references to buffer consumers (copies from the original event buffer consumer)
	BufferConsumer bufferConsumer1 = queues[0].getFirst();
	BufferConsumer bufferConsumer2 = queues[1].getFirst();

	// process all collected events (recycles the buffer)
	for (int i = 0; i < queues.length; i++) {
		assertTrue(parseBuffer(queues[i].remove(), i).isEvent());
	}

	assertTrue(bufferConsumer1.isRecycled());
	assertTrue(bufferConsumer2.isRecycled());
}
 
Example #23
Source File: BoundedBlockingSubpartition.java    From flink with Apache License 2.0 5 votes vote down vote up
private void writeAndCloseBufferConsumer(BufferConsumer bufferConsumer) throws IOException {
	try {
		final Buffer buffer = bufferConsumer.build();
		try {
			if (canBeCompressed(buffer)) {
				final Buffer compressedBuffer = parent.bufferCompressor.compressToIntermediateBuffer(buffer);
				data.writeBuffer(compressedBuffer);
				if (compressedBuffer != buffer) {
					compressedBuffer.recycleBuffer();
				}
			} else {
				data.writeBuffer(buffer);
			}

			numBuffersAndEventsWritten++;
			if (buffer.isBuffer()) {
				numDataBuffersWritten++;
			}
		}
		finally {
			buffer.recycleBuffer();
		}
	}
	finally {
		bufferConsumer.close();
	}
}
 
Example #24
Source File: AbstractCollectingResultPartitionWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean addBufferConsumer(
		BufferConsumer bufferConsumer,
		int targetChannel,
		boolean isPriorityEvent) throws IOException {
	checkState(targetChannel < getNumberOfSubpartitions());
	bufferConsumers.add(bufferConsumer);
	processBufferConsumers();
	return true;
}
 
Example #25
Source File: ResultPartitionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link ResultPartition#addBufferConsumer} on a partition which has already finished.
 *
 * @param partitionType the result partition type to set up
 */
private void testAddOnFinishedPartition(final ResultPartitionType partitionType) throws Exception {
	BufferConsumer bufferConsumer = createFilledFinishedBufferConsumer(BufferBuilderTestUtils.BUFFER_SIZE);
	ResultPartitionConsumableNotifier notifier = mock(ResultPartitionConsumableNotifier.class);
	JobID jobId = new JobID();
	TaskActions taskActions = new NoOpTaskActions();
	ResultPartitionWriter consumableNotifyingPartitionWriter = createConsumableNotifyingResultPartitionWriter(
		partitionType,
		taskActions,
		jobId,
		notifier);
	try {
		consumableNotifyingPartitionWriter.finish();
		reset(notifier);
		// partition.add() should fail
		consumableNotifyingPartitionWriter.addBufferConsumer(bufferConsumer, 0);
		Assert.fail("exception expected");
	} catch (IllegalStateException e) {
		// expected => ignored
	} finally {
		if (!bufferConsumer.isRecycled()) {
			bufferConsumer.close();
			Assert.fail("bufferConsumer not recycled");
		}
		// should not have notified either
		verify(notifier, never()).notifyPartitionConsumable(
			eq(jobId),
			eq(consumableNotifyingPartitionWriter.getPartitionId()),
			eq(taskActions));
	}
}
 
Example #26
Source File: SpanningRecordSerializationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static BufferAndSerializerResult setNextBufferForSerializer(
		RecordSerializer<SerializationTestType> serializer,
		int segmentSize) throws IOException {
	// create a bufferBuilder with some random starting offset to properly test handling buffer slices in the
	// deserialization code.
	int startingOffset = segmentSize > 2 ? RANDOM.nextInt(segmentSize / 2) : 0;
	BufferBuilder bufferBuilder = createFilledBufferBuilder(segmentSize + startingOffset, startingOffset);
	BufferConsumer bufferConsumer = bufferBuilder.createBufferConsumer();
	bufferConsumer.build().recycleBuffer();

	return new BufferAndSerializerResult(
		bufferBuilder,
		bufferConsumer,
		serializer.copyToBufferBuilder(bufferBuilder));
}
 
Example #27
Source File: SpanningRecordSerializationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
public BufferAndSerializerResult(
		BufferBuilder bufferBuilder,
		BufferConsumer bufferConsumer,
		RecordSerializer.SerializationResult serializationResult) {
	this.bufferBuilder = bufferBuilder;
	this.bufferConsumer = bufferConsumer;
	this.serializationResult = serializationResult;
}
 
Example #28
Source File: PipelinedSubpartition.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean add(BufferConsumer bufferConsumer, boolean isPriorityEvent) throws IOException {
	if (isPriorityEvent) {
		// TODO: use readView.notifyPriorityEvent for local channels
		return add(bufferConsumer, false, true);
	}
	return add(bufferConsumer, false, false);
}
 
Example #29
Source File: RecordWriterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that records are broadcast via {@link RecordWriter#broadcastEmit(IOReadableWritable)}.
 */
@Test
public void testBroadcastEmitRecord() throws Exception {
	final int numberOfChannels = 4;
	final int bufferSize = 32;
	final int numValues = 8;
	final int serializationLength = 4;

	@SuppressWarnings("unchecked")
	final Queue<BufferConsumer>[] queues = new Queue[numberOfChannels];
	for (int i = 0; i < numberOfChannels; i++) {
		queues[i] = new ArrayDeque<>();
	}

	final TestPooledBufferProvider bufferProvider = new TestPooledBufferProvider(Integer.MAX_VALUE, bufferSize);
	final ResultPartitionWriter partitionWriter = new CollectingPartitionWriter(queues, bufferProvider);
	final RecordWriter<SerializationTestType> writer = createRecordWriter(partitionWriter);
	final RecordDeserializer<SerializationTestType> deserializer = new SpillingAdaptiveSpanningRecordDeserializer<>(
		new String[]{ tempFolder.getRoot().getAbsolutePath() });

	final ArrayDeque<SerializationTestType> serializedRecords = new ArrayDeque<>();
	final Iterable<SerializationTestType> records = Util.randomRecords(numValues, SerializationTestTypeFactory.INT);
	for (SerializationTestType record : records) {
		serializedRecords.add(record);
		writer.broadcastEmit(record);
	}

	final int numRequiredBuffers = numValues / (bufferSize / (4 + serializationLength));
	if (isBroadcastWriter) {
		assertEquals(numRequiredBuffers, bufferProvider.getNumberOfCreatedBuffers());
	} else {
		assertEquals(numRequiredBuffers * numberOfChannels, bufferProvider.getNumberOfCreatedBuffers());
	}

	for (int i = 0; i < numberOfChannels; i++) {
		assertEquals(numRequiredBuffers, queues[i].size());
		verifyDeserializationResults(queues[i], deserializer, serializedRecords.clone(), numRequiredBuffers, numValues);
	}
}
 
Example #30
Source File: PipelinedSubpartitionWithReadViewTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testBarrierOvertaking() throws Exception {
	subpartition.add(createFilledFinishedBufferConsumer(1));
	assertEquals(0, availablityListener.getNumNotifications());
	assertEquals(0, availablityListener.getNumPriorityEvents());

	subpartition.add(createFilledFinishedBufferConsumer(2));
	assertEquals(1, availablityListener.getNumNotifications());
	assertEquals(0, availablityListener.getNumPriorityEvents());

	BufferConsumer eventBuffer = EventSerializer.toBufferConsumer(EndOfSuperstepEvent.INSTANCE);
	subpartition.add(eventBuffer);
	assertEquals(1, availablityListener.getNumNotifications());
	assertEquals(0, availablityListener.getNumPriorityEvents());

	subpartition.add(createFilledFinishedBufferConsumer(4));
	assertEquals(1, availablityListener.getNumNotifications());
	assertEquals(0, availablityListener.getNumPriorityEvents());

	CheckpointOptions options = new CheckpointOptions(
		CheckpointType.CHECKPOINT,
		new CheckpointStorageLocationReference(new byte[]{0, 1, 2}),
		true,
		true);
	BufferConsumer barrierBuffer = EventSerializer.toBufferConsumer(new CheckpointBarrier(0, 0, options));
	subpartition.add(barrierBuffer, true);
	assertEquals(2, availablityListener.getNumNotifications());
	assertEquals(0, availablityListener.getNumPriorityEvents());

	List<Buffer> inflight = subpartition.requestInflightBufferSnapshot();
	assertEquals(Arrays.asList(1, 2, 4), inflight.stream().map(Buffer::getSize).collect(Collectors.toList()));
	inflight.forEach(Buffer::recycleBuffer);

	assertNextEvent(readView, barrierBuffer.getWrittenBytes(), CheckpointBarrier.class, true, 2, false, true);
	assertNextBuffer(readView, 1, true, 1, false, true);
	assertNextBuffer(readView, 2, true, 0, true, true);
	assertNextEvent(readView, eventBuffer.getWrittenBytes(), EndOfSuperstepEvent.class, false, 0, false, true);
	assertNextBuffer(readView, 4, false, 0, false, true);
	assertNoNextBuffer(readView);
}