flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] dawidwys commented on a change in pull request #7039: [FLINK-8997] Added sliding window aggregation to datastream test job
Date Thu, 08 Nov 2018 14:16:26 GMT
dawidwys 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_r231902404
 
 

 ##########
 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)) {
 
 Review comment:
   It is true that if we see 
   > seqNumber x N times, then all sequenceNumbers y < x
   
   We check that we haven't seen sth less times than `slideFactor` if it is not in the current
window, but it wasn't seen `slideFactor` times before (it is still present in the `eventsSeenSoFar`
variable) that meens we've seen it less times and we log "error" that will fail tests.

----------------------------------------------------------------
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

Mime
View raw message