org.apache.flink.streaming.api.functions.source.FromElementsFunction Java Examples

The following examples show how to use org.apache.flink.streaming.api.functions.source.FromElementsFunction. 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: StreamExecutionEnvironment.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a data stream from the given non-empty collection.
 *
 * <p>Note that this operation will result in a non-parallel data stream source,
 * i.e., a data stream source with parallelism one.
 *
 * @param data
 * 		The collection of elements to create the data stream from
 * @param typeInfo
 * 		The TypeInformation for the produced data stream
 * @param <OUT>
 * 		The type of the returned data stream
 * @return The data stream representing the given collection
 */
public <OUT> DataStreamSource<OUT> fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) {
	Preconditions.checkNotNull(data, "Collection must not be null");

	// must not have null elements and mixed elements
	FromElementsFunction.checkCollection(data, typeInfo.getTypeClass());

	SourceFunction<OUT> function;
	try {
		function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data);
	}
	catch (IOException e) {
		throw new RuntimeException(e.getMessage(), e);
	}
	return addSource(function, "Collection Source", typeInfo).setParallelism(1);
}
 
Example #2
Source File: FromElementsFunctionTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonJavaSerializableType() {
	try {
		MyPojo[] data = { new MyPojo(1, 2), new MyPojo(3, 4), new MyPojo(5, 6) };

		FromElementsFunction<MyPojo> source = new FromElementsFunction<MyPojo>(
				TypeExtractor.getForClass(MyPojo.class).createSerializer(new ExecutionConfig()), data);

		List<MyPojo> result = new ArrayList<MyPojo>();
		source.run(new ListSourceContext<MyPojo>(result));

		assertEquals(Arrays.asList(data), result);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #3
Source File: FromElementsFunctionTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testStrings() {
	try {
		String[] data = { "Oh", "boy", "what", "a", "show", "!"};

		FromElementsFunction<String> source = new FromElementsFunction<String>(
				BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), data);

		List<String> result = new ArrayList<String>();
		source.run(new ListSourceContext<String>(result));

		assertEquals(Arrays.asList(data), result);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #4
Source File: FromElementsFunctionTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonJavaSerializableType() {
	try {
		MyPojo[] data = { new MyPojo(1, 2), new MyPojo(3, 4), new MyPojo(5, 6) };

		FromElementsFunction<MyPojo> source = new FromElementsFunction<MyPojo>(
				TypeExtractor.getForClass(MyPojo.class).createSerializer(new ExecutionConfig()), data);

		List<MyPojo> result = new ArrayList<MyPojo>();
		source.run(new ListSourceContext<MyPojo>(result));

		assertEquals(Arrays.asList(data), result);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #5
Source File: FromElementsFunctionTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testStrings() {
	try {
		String[] data = { "Oh", "boy", "what", "a", "show", "!"};

		FromElementsFunction<String> source = new FromElementsFunction<String>(
				BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), data);

		List<String> result = new ArrayList<String>();
		source.run(new ListSourceContext<String>(result));

		assertEquals(Arrays.asList(data), result);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #6
Source File: StreamExecutionEnvironment.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a data stream from the given non-empty collection.
 *
 * <p>Note that this operation will result in a non-parallel data stream source,
 * i.e., a data stream source with parallelism one.
 *
 * @param data
 * 		The collection of elements to create the data stream from
 * @param typeInfo
 * 		The TypeInformation for the produced data stream
 * @param <OUT>
 * 		The type of the returned data stream
 * @return The data stream representing the given collection
 */
public <OUT> DataStreamSource<OUT> fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) {
	Preconditions.checkNotNull(data, "Collection must not be null");

	// must not have null elements and mixed elements
	FromElementsFunction.checkCollection(data, typeInfo.getTypeClass());

	SourceFunction<OUT> function;
	try {
		function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data);
	}
	catch (IOException e) {
		throw new RuntimeException(e.getMessage(), e);
	}
	return addSource(function, "Collection Source", typeInfo).setParallelism(1);
}
 
Example #7
Source File: FromElementsFunctionTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testStrings() {
	try {
		String[] data = { "Oh", "boy", "what", "a", "show", "!"};

		FromElementsFunction<String> source = new FromElementsFunction<String>(
				BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), data);

		List<String> result = new ArrayList<String>();
		source.run(new ListSourceContext<String>(result));

		assertEquals(Arrays.asList(data), result);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #8
Source File: FromElementsFunctionTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonJavaSerializableType() {
	try {
		MyPojo[] data = { new MyPojo(1, 2), new MyPojo(3, 4), new MyPojo(5, 6) };

		FromElementsFunction<MyPojo> source = new FromElementsFunction<MyPojo>(
				TypeExtractor.getForClass(MyPojo.class).createSerializer(new ExecutionConfig()), data);

		List<MyPojo> result = new ArrayList<MyPojo>();
		source.run(new ListSourceContext<MyPojo>(result));

		assertEquals(Arrays.asList(data), result);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #9
Source File: StreamExecutionEnvironment.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a data stream from the given non-empty collection.
 *
 * <p>Note that this operation will result in a non-parallel data stream source,
 * i.e., a data stream source with parallelism one.
 *
 * @param data
 * 		The collection of elements to create the data stream from
 * @param typeInfo
 * 		The TypeInformation for the produced data stream
 * @param <OUT>
 * 		The type of the returned data stream
 * @return The data stream representing the given collection
 */
public <OUT> DataStreamSource<OUT> fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) {
	Preconditions.checkNotNull(data, "Collection must not be null");

	// must not have null elements and mixed elements
	FromElementsFunction.checkCollection(data, typeInfo.getTypeClass());

	SourceFunction<OUT> function;
	try {
		function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data);
	}
	catch (IOException e) {
		throw new RuntimeException(e.getMessage(), e);
	}
	return addSource(function, "Collection Source", typeInfo).setParallelism(1);
}
 
Example #10
Source File: TestValuesTableFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public ScanRuntimeProvider getScanRuntimeProvider(ScanContext runtimeProviderContext) {
	TypeSerializer<RowData> serializer = (TypeSerializer<RowData>) runtimeProviderContext
		.createTypeInformation(physicalSchema.toRowDataType())
		.createSerializer(new ExecutionConfig());
	DataStructureConverter converter = runtimeProviderContext.createDataStructureConverter(physicalSchema.toRowDataType());
	converter.open(RuntimeConverter.Context.create(TestValuesTableFactory.class.getClassLoader()));
	Collection<RowData> values = convertToRowData(data, projectedFields, converter);

	if (runtimeSource.equals("SourceFunction")) {
		try {
			return SourceFunctionProvider.of(
				new FromElementsFunction<>(serializer, values),
				bounded);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	} else if (runtimeSource.equals("InputFormat")) {
		return InputFormatProvider.of(new CollectionInputFormat<>(values, serializer));
	} else {
		throw new IllegalArgumentException("Unsupported runtime source class: " + runtimeSource);
	}
}
 
Example #11
Source File: SourceFunctionTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void fromElementsTest() throws Exception {
	List<Integer> expectedList = Arrays.asList(1, 2, 3);
	List<Integer> actualList = SourceFunctionUtil.runSourceFunction(CommonTestUtils.createCopySerializable(
			new FromElementsFunction<Integer>(
					IntSerializer.INSTANCE,
					1,
					2,
					3)));
	assertEquals(expectedList, actualList);
}
 
Example #12
Source File: SourceStreamTaskTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testClosingAllOperatorsOnChainProperly() throws Exception {
	final StreamTaskTestHarness<String> testHarness = new StreamTaskTestHarness<>(
		SourceStreamTask::new,
		BasicTypeInfo.STRING_TYPE_INFO);

	testHarness
		.setupOperatorChain(
			new OperatorID(),
			new OutputRecordInCloseTestSource<>(
				"Source0", new FromElementsFunction<>(StringSerializer.INSTANCE, "Hello")))
		.chain(
			new OperatorID(),
			new TestBoundedOneInputStreamOperator("Operator1"),
			BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()))
		.finish();

	StreamConfig streamConfig = testHarness.getStreamConfig();
	streamConfig.setTimeCharacteristic(TimeCharacteristic.ProcessingTime);

	testHarness.invoke();
	testHarness.waitForTaskCompletion();

	ArrayList<StreamRecord<String>> expected = new ArrayList<>();
	Collections.addAll(expected,
		new StreamRecord<>("Hello"),
		new StreamRecord<>("[Source0]: End of input"),
		new StreamRecord<>("[Source0]: Bye"),
		new StreamRecord<>("[Operator1]: End of input"),
		new StreamRecord<>("[Operator1]: Bye"));

	final Object[] output = testHarness.getOutput().toArray();
	assertArrayEquals("Output was not correct.", expected.toArray(), output);
}
 
Example #13
Source File: StreamExecutionEnvironmentTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSources() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	SourceFunction<Integer> srcFun = new SourceFunction<Integer>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void run(SourceContext<Integer> ctx) throws Exception {
		}

		@Override
		public void cancel() {
		}
	};
	DataStreamSource<Integer> src1 = env.addSource(srcFun);
	src1.addSink(new DiscardingSink<Integer>());
	assertEquals(srcFun, getFunctionFromDataSource(src1));

	List<Long> list = Arrays.asList(0L, 1L, 2L);

	DataStreamSource<Long> src2 = env.generateSequence(0, 2);
	assertTrue(getFunctionFromDataSource(src2) instanceof StatefulSequenceSource);

	DataStreamSource<Long> src3 = env.fromElements(0L, 1L, 2L);
	assertTrue(getFunctionFromDataSource(src3) instanceof FromElementsFunction);

	DataStreamSource<Long> src4 = env.fromCollection(list);
	assertTrue(getFunctionFromDataSource(src4) instanceof FromElementsFunction);
}
 
Example #14
Source File: SourceFunctionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void fromCollectionTest() throws Exception {
	List<Integer> expectedList = Arrays.asList(1, 2, 3);
	List<Integer> actualList = SourceFunctionUtil.runSourceFunction(
			CommonTestUtils.createCopySerializable(new FromElementsFunction<Integer>(
					IntSerializer.INSTANCE,
					Arrays.asList(1, 2, 3))));
	assertEquals(expectedList, actualList);
}
 
Example #15
Source File: SourceFunctionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void fromElementsTest() throws Exception {
	List<Integer> expectedList = Arrays.asList(1, 2, 3);
	List<Integer> actualList = SourceFunctionUtil.runSourceFunction(CommonTestUtils.createCopySerializable(
			new FromElementsFunction<Integer>(
					IntSerializer.INSTANCE,
					1,
					2,
					3)));
	assertEquals(expectedList, actualList);
}
 
Example #16
Source File: SavepointTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
private <T> WaitingSource<T> createSource(Collection<T> data) throws Exception {
	T first = data.iterator().next();
	if (first == null) {
		throw new IllegalArgumentException("Collection must not contain null elements");
	}

	TypeInformation<T> typeInfo = TypeExtractor.getForObject(first);
	SourceFunction<T> inner = new FromElementsFunction<>(typeInfo.createSerializer(new ExecutionConfig()), data);
	return new WaitingSource<>(inner, typeInfo);
}
 
Example #17
Source File: FlinkStreamingSpringBootTest.java    From Flink-Streaming-Spring-Boot with MIT License 5 votes vote down vote up
@Autowired
void populateEnv(StreamExecutionEnvironment flinkEnvironment, String outputFileName)
    throws IOException {
    flinkEnvironment
        .addSource(
            new FromElementsFunction<>(new IntSerializer(), 1, 2, 3),
            TypeInformation.of(Integer.class))
        .filter((Integer i) -> i % 2 == 0)
        .writeAsText(outputFileName, FileSystem.WriteMode.OVERWRITE)
        .setParallelism(1);
}
 
Example #18
Source File: SourceStreamTaskTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarkingEndOfInput() throws Exception {
	final StreamTaskTestHarness<String> testHarness = new StreamTaskTestHarness<>(
		SourceStreamTask::new,
		BasicTypeInfo.STRING_TYPE_INFO);

	testHarness
		.setupOperatorChain(
			new OperatorID(),
			new StreamSource<>(new FromElementsFunction<>(
				BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), "Hello")))
		.chain(
			new OperatorID(),
			new TestBoundedOneInputStreamOperator("Operator1"),
			BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()))
		.finish();

	StreamConfig streamConfig = testHarness.getStreamConfig();
	streamConfig.setTimeCharacteristic(TimeCharacteristic.ProcessingTime);

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();

	testHarness.invoke();
	testHarness.waitForTaskCompletion();

	expectedOutput.add(new StreamRecord<>("Hello"));
	expectedOutput.add(new StreamRecord<>("[Operator1]: Bye"));

	TestHarnessUtil.assertOutputEquals("Output was not correct.",
		expectedOutput,
		testHarness.getOutput());
}
 
Example #19
Source File: StreamExecutionEnvironmentTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSources() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	SourceFunction<Integer> srcFun = new SourceFunction<Integer>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void run(SourceContext<Integer> ctx) throws Exception {
		}

		@Override
		public void cancel() {
		}
	};
	DataStreamSource<Integer> src1 = env.addSource(srcFun);
	src1.addSink(new DiscardingSink<Integer>());
	assertEquals(srcFun, getFunctionFromDataSource(src1));

	List<Long> list = Arrays.asList(0L, 1L, 2L);

	DataStreamSource<Long> src2 = env.generateSequence(0, 2);
	assertTrue(getFunctionFromDataSource(src2) instanceof StatefulSequenceSource);

	DataStreamSource<Long> src3 = env.fromElements(0L, 1L, 2L);
	assertTrue(getFunctionFromDataSource(src3) instanceof FromElementsFunction);

	DataStreamSource<Long> src4 = env.fromCollection(list);
	assertTrue(getFunctionFromDataSource(src4) instanceof FromElementsFunction);
}
 
Example #20
Source File: SourceFunctionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void fromCollectionTest() throws Exception {
	List<Integer> expectedList = Arrays.asList(1, 2, 3);
	List<Integer> actualList = SourceFunctionUtil.runSourceFunction(
			CommonTestUtils.createCopySerializable(new FromElementsFunction<Integer>(
					IntSerializer.INSTANCE,
					Arrays.asList(1, 2, 3))));
	assertEquals(expectedList, actualList);
}
 
Example #21
Source File: SourceFunctionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void fromElementsTest() throws Exception {
	List<Integer> expectedList = Arrays.asList(1, 2, 3);
	List<Integer> actualList = SourceFunctionUtil.runSourceFunction(CommonTestUtils.createCopySerializable(
			new FromElementsFunction<Integer>(
					IntSerializer.INSTANCE,
					1,
					2,
					3)));
	assertEquals(expectedList, actualList);
}
 
Example #22
Source File: StreamExecutionEnvironmentTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSources() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	SourceFunction<Integer> srcFun = new SourceFunction<Integer>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void run(SourceContext<Integer> ctx) throws Exception {
		}

		@Override
		public void cancel() {
		}
	};
	DataStreamSource<Integer> src1 = env.addSource(srcFun);
	src1.addSink(new DiscardingSink<Integer>());
	assertEquals(srcFun, getFunctionFromDataSource(src1));

	List<Long> list = Arrays.asList(0L, 1L, 2L);

	DataStreamSource<Long> src2 = env.generateSequence(0, 2);
	assertTrue(getFunctionFromDataSource(src2) instanceof StatefulSequenceSource);

	DataStreamSource<Long> src3 = env.fromElements(0L, 1L, 2L);
	assertTrue(getFunctionFromDataSource(src3) instanceof FromElementsFunction);

	DataStreamSource<Long> src4 = env.fromCollection(list);
	assertTrue(getFunctionFromDataSource(src4) instanceof FromElementsFunction);
}
 
Example #23
Source File: SourceFunctionTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void fromCollectionTest() throws Exception {
	List<Integer> expectedList = Arrays.asList(1, 2, 3);
	List<Integer> actualList = SourceFunctionUtil.runSourceFunction(
			CommonTestUtils.createCopySerializable(new FromElementsFunction<Integer>(
					IntSerializer.INSTANCE,
					Arrays.asList(1, 2, 3))));
	assertEquals(expectedList, actualList);
}
 
Example #24
Source File: FromElementsFunctionTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testCheckpointAndRestore() {
	try {
		final int numElements = 10000;

		List<Integer> data = new ArrayList<Integer>(numElements);
		List<Integer> result = new ArrayList<Integer>(numElements);

		for (int i = 0; i < numElements; i++) {
			data.add(i);
		}

		final FromElementsFunction<Integer> source = new FromElementsFunction<>(IntSerializer.INSTANCE, data);
		StreamSource<Integer, FromElementsFunction<Integer>> src = new StreamSource<>(source);
		AbstractStreamOperatorTestHarness<Integer> testHarness =
			new AbstractStreamOperatorTestHarness<>(src, 1, 1, 0);
		testHarness.open();

		final SourceFunction.SourceContext<Integer> ctx = new ListSourceContext<Integer>(result, 2L);

		final Throwable[] error = new Throwable[1];

		// run the source asynchronously
		Thread runner = new Thread() {
			@Override
			public void run() {
				try {
					source.run(ctx);
				}
				catch (Throwable t) {
					error[0] = t;
				}
			}
		};
		runner.start();

		// wait for a bit
		Thread.sleep(1000);

		// make a checkpoint
		List<Integer> checkpointData = new ArrayList<>(numElements);
		OperatorSubtaskState handles = null;
		synchronized (ctx.getCheckpointLock()) {
			handles = testHarness.snapshot(566, System.currentTimeMillis());
			checkpointData.addAll(result);
		}

		// cancel the source
		source.cancel();
		runner.join();

		// check for errors
		if (error[0] != null) {
			System.err.println("Error in asynchronous source runner");
			error[0].printStackTrace();
			fail("Error in asynchronous source runner");
		}

		final FromElementsFunction<Integer> sourceCopy = new FromElementsFunction<>(IntSerializer.INSTANCE, data);
		StreamSource<Integer, FromElementsFunction<Integer>> srcCopy = new StreamSource<>(sourceCopy);
		AbstractStreamOperatorTestHarness<Integer> testHarnessCopy =
			new AbstractStreamOperatorTestHarness<>(srcCopy, 1, 1, 0);
		testHarnessCopy.setup();
		testHarnessCopy.initializeState(handles);
		testHarnessCopy.open();

		// recovery run
		SourceFunction.SourceContext<Integer> newCtx = new ListSourceContext<>(checkpointData);

		sourceCopy.run(newCtx);

		assertEquals(data, checkpointData);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #25
Source File: FromElementsFunctionTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testCheckpointAndRestore() {
	try {
		final int numElements = 10000;

		List<Integer> data = new ArrayList<Integer>(numElements);
		List<Integer> result = new ArrayList<Integer>(numElements);

		for (int i = 0; i < numElements; i++) {
			data.add(i);
		}

		final FromElementsFunction<Integer> source = new FromElementsFunction<>(IntSerializer.INSTANCE, data);
		StreamSource<Integer, FromElementsFunction<Integer>> src = new StreamSource<>(source);
		AbstractStreamOperatorTestHarness<Integer> testHarness =
			new AbstractStreamOperatorTestHarness<>(src, 1, 1, 0);
		testHarness.open();

		final SourceFunction.SourceContext<Integer> ctx = new ListSourceContext<Integer>(result, 2L);

		final Throwable[] error = new Throwable[1];

		// run the source asynchronously
		Thread runner = new Thread() {
			@Override
			public void run() {
				try {
					source.run(ctx);
				}
				catch (Throwable t) {
					error[0] = t;
				}
			}
		};
		runner.start();

		// wait for a bit
		Thread.sleep(1000);

		// make a checkpoint
		List<Integer> checkpointData = new ArrayList<>(numElements);
		OperatorSubtaskState handles = null;
		synchronized (ctx.getCheckpointLock()) {
			handles = testHarness.snapshot(566, System.currentTimeMillis());
			checkpointData.addAll(result);
		}

		// cancel the source
		source.cancel();
		runner.join();

		// check for errors
		if (error[0] != null) {
			System.err.println("Error in asynchronous source runner");
			error[0].printStackTrace();
			fail("Error in asynchronous source runner");
		}

		final FromElementsFunction<Integer> sourceCopy = new FromElementsFunction<>(IntSerializer.INSTANCE, data);
		StreamSource<Integer, FromElementsFunction<Integer>> srcCopy = new StreamSource<>(sourceCopy);
		AbstractStreamOperatorTestHarness<Integer> testHarnessCopy =
			new AbstractStreamOperatorTestHarness<>(srcCopy, 1, 1, 0);
		testHarnessCopy.setup();
		testHarnessCopy.initializeState(handles);
		testHarnessCopy.open();

		// recovery run
		SourceFunction.SourceContext<Integer> newCtx = new ListSourceContext<>(checkpointData);

		sourceCopy.run(newCtx);

		assertEquals(data, checkpointData);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #26
Source File: FromElementsFunctionTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testCheckpointAndRestore() {
	try {
		final int numElements = 10000;

		List<Integer> data = new ArrayList<Integer>(numElements);
		List<Integer> result = new ArrayList<Integer>(numElements);

		for (int i = 0; i < numElements; i++) {
			data.add(i);
		}

		final FromElementsFunction<Integer> source = new FromElementsFunction<>(IntSerializer.INSTANCE, data);
		StreamSource<Integer, FromElementsFunction<Integer>> src = new StreamSource<>(source);
		AbstractStreamOperatorTestHarness<Integer> testHarness =
			new AbstractStreamOperatorTestHarness<>(src, 1, 1, 0);
		testHarness.open();

		final SourceFunction.SourceContext<Integer> ctx = new ListSourceContext<Integer>(result, 2L);

		final Throwable[] error = new Throwable[1];

		// run the source asynchronously
		Thread runner = new Thread() {
			@Override
			public void run() {
				try {
					source.run(ctx);
				}
				catch (Throwable t) {
					error[0] = t;
				}
			}
		};
		runner.start();

		// wait for a bit
		Thread.sleep(1000);

		// make a checkpoint
		List<Integer> checkpointData = new ArrayList<>(numElements);
		OperatorSubtaskState handles = null;
		synchronized (ctx.getCheckpointLock()) {
			handles = testHarness.snapshot(566, System.currentTimeMillis());
			checkpointData.addAll(result);
		}

		// cancel the source
		source.cancel();
		runner.join();

		// check for errors
		if (error[0] != null) {
			System.err.println("Error in asynchronous source runner");
			error[0].printStackTrace();
			fail("Error in asynchronous source runner");
		}

		final FromElementsFunction<Integer> sourceCopy = new FromElementsFunction<>(IntSerializer.INSTANCE, data);
		StreamSource<Integer, FromElementsFunction<Integer>> srcCopy = new StreamSource<>(sourceCopy);
		AbstractStreamOperatorTestHarness<Integer> testHarnessCopy =
			new AbstractStreamOperatorTestHarness<>(srcCopy, 1, 1, 0);
		testHarnessCopy.setup();
		testHarnessCopy.initializeState(handles);
		testHarnessCopy.open();

		// recovery run
		SourceFunction.SourceContext<Integer> newCtx = new ListSourceContext<>(checkpointData);

		sourceCopy.run(newCtx);

		assertEquals(data, checkpointData);
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}