You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2018/11/08 11:25:09 UTC

[GitHub] kl0u commented on a change in pull request #7039: [FLINK-8997] Added sliding window aggregation to datastream test job

kl0u commented on a change in pull request #7039: [FLINK-8997] Added sliding window aggregation to datastream test job
URL: https://github.com/apache/flink/pull/7039#discussion_r231851570
 
 

 ##########
 File path: flink-end-to-end-tests/flink-datastream-allround-test/src/main/java/org/apache/flink/streaming/tests/SlidingWindowCheckMapper.java
 ##########
 @@ -0,0 +1,100 @@
+/*
+ * 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.streaming.tests;
+
+import org.apache.flink.api.common.functions.RichFlatMapFunction;
+import org.apache.flink.api.common.state.ValueState;
+import org.apache.flink.api.common.state.ValueStateDescriptor;
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.typeutils.ListTypeInfo;
+import org.apache.flink.api.java.typeutils.TupleTypeInfo;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.util.Collector;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.BinaryOperator;
+
+/**
+ * This mapper validates sliding event time window. It checks each event belongs to appropriate number of consecutive windows.
+ */
+public class SlidingWindowCheckMapper extends RichFlatMapFunction<Tuple2<Integer, List<Event>>, String> {
+
+	private static final long serialVersionUID = -744070793650644485L;
+
+	/** This value state tracks previously seen events with the number of windows they appeared in. */
+	private transient ValueState<List<Tuple2<Event, Integer>>> previousWindow;
+
+	private final int slideFactor;
+
+	SlidingWindowCheckMapper(int slideFactor) {
+		this.slideFactor = slideFactor;
+	}
+
+	@Override
+	public void open(Configuration parameters) throws Exception {
+		ValueStateDescriptor<List<Tuple2<Event, Integer>>> previousWindowDescriptor =
+			new ValueStateDescriptor<>("previousWindow",
+				new ListTypeInfo<>(new TupleTypeInfo<>(TypeInformation.of(Event.class), BasicTypeInfo.INT_TYPE_INFO)));
+
+		previousWindow = getRuntimeContext().getState(previousWindowDescriptor);
+	}
+
+	@Override
+	public void flatMap(Tuple2<Integer, List<Event>> value, Collector<String> out) throws Exception {
+		List<Tuple2<Event, Integer>> previousWindowValues = Optional.ofNullable(previousWindow.value()).orElseGet(
+			Collections::emptyList);
+
+		List<Event> newValues = value.f1;
+		newValues.stream().reduce(new BinaryOperator<Event>() {
+			@Override
+			public Event apply(Event event, Event event2) {
+				if (event2.getSequenceNumber() - 1 != event.getSequenceNumber()) {
+					out.collect("Alert: events in window out ouf order!");
+				}
+
+				return event2;
+			}
+		});
+
+		List<Tuple2<Event, Integer>> newWindow = new ArrayList<>();
+		for (Tuple2<Event, Integer> windowValue : previousWindowValues) {
+			if (!newValues.contains(windowValue.f0)) {
+				out.collect(String.format("Alert: event %s did not belong to %d consecutive windows. Event seen so far %d times.Current window: %s",
 
 Review comment:
   I would put that in a method name sth like `failTestWithMessage()`...
   				

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services