[
https://issues.apache.org/jira/browse/FLINK-8532?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16577786#comment-16577786
]
ASF GitHub Bot commented on FLINK-8532:
---------------------------------------
Guibo-Pan closed pull request #6544: [FLINK-8532] [Streaming] modify
RebalancePartitioner to use a random partition as its first partition
URL: https://github.com/apache/flink/pull/6544
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/partitioner/RebalancePartitioner.java
b/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/partitioner/RebalancePartitioner.java
index bb88d17cf9c..c57000fbe59 100644
---
a/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/partitioner/RebalancePartitioner.java
+++
b/flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/partitioner/RebalancePartitioner.java
@@ -21,6 +21,8 @@
import org.apache.flink.runtime.plugable.SerializationDelegate;
import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import java.util.Random;
+
/**
* Partitioner that distributes the data equally by cycling through the output
* channels.
@@ -36,9 +38,13 @@
@Override
public int[] selectChannels(SerializationDelegate<StreamRecord<T>>
record,
int numberOfOutputChannels) {
- int newChannel = ++this.returnArray[0];
- if (newChannel >= numberOfOutputChannels) {
- this.returnArray[0] = 0;
+ if (this.returnArray[0] < 0) {
+ this.returnArray[0] = new
Random().nextInt(numberOfOutputChannels);
+ } else {
+ int newChannel = ++this.returnArray[0];
+ if (newChannel >= numberOfOutputChannels) {
+ this.returnArray[0] = 0;
+ }
}
return this.returnArray;
}
diff --git
a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/partitioner/RebalancePartitionerTest.java
b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/partitioner/RebalancePartitionerTest.java
index 85410f3de98..0cf022b3f27 100644
---
a/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/partitioner/RebalancePartitionerTest.java
+++
b/flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/partitioner/RebalancePartitionerTest.java
@@ -25,6 +25,7 @@
import org.junit.Test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
/**
* Tests for {@link RebalancePartitioner}.
@@ -52,9 +53,11 @@ public void testSelectChannelsLength() {
@Test
public void testSelectChannelsInterval() {
sd.setInstance(streamRecord);
- assertEquals(0, distributePartitioner.selectChannels(sd, 3)[0]);
- assertEquals(1, distributePartitioner.selectChannels(sd, 3)[0]);
- assertEquals(2, distributePartitioner.selectChannels(sd, 3)[0]);
- assertEquals(0, distributePartitioner.selectChannels(sd, 3)[0]);
+ int initialChannel = distributePartitioner.selectChannels(sd,
3)[0];
+ assertTrue(0 <= initialChannel);
+ assertTrue(3 > initialChannel);
+ assertEquals((initialChannel + 1) % 3,
distributePartitioner.selectChannels(sd, 3)[0]);
+ assertEquals((initialChannel + 2) % 3,
distributePartitioner.selectChannels(sd, 3)[0]);
+ assertEquals((initialChannel + 3) % 3,
distributePartitioner.selectChannels(sd, 3)[0]);
}
}
diff --git
a/flink-tests/src/test/java/org/apache/flink/test/streaming/runtime/PartitionerITCase.java
b/flink-tests/src/test/java/org/apache/flink/test/streaming/runtime/PartitionerITCase.java
index 23fc2eb3df1..4c5c0a9f0ee 100644
---
a/flink-tests/src/test/java/org/apache/flink/test/streaming/runtime/PartitionerITCase.java
+++
b/flink-tests/src/test/java/org/apache/flink/test/streaming/runtime/PartitionerITCase.java
@@ -39,6 +39,7 @@
import java.util.Objects;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
@@ -219,7 +220,7 @@ private static void
verifyBroadcastPartitioning(List<Tuple2<Integer, String>> br
}
private static void verifyRebalancePartitioning(List<Tuple2<Integer,
String>> rebalancePartitionResult) {
- List<Tuple2<Integer, String>> expected = Arrays.asList(
+ List<Tuple2<Integer, String>> expected0 = Arrays.asList(
new Tuple2<Integer, String>(0, "a"),
new Tuple2<Integer, String>(1, "b"),
new Tuple2<Integer, String>(2, "b"),
@@ -228,9 +229,29 @@ private static void
verifyRebalancePartitioning(List<Tuple2<Integer, String>> re
new Tuple2<Integer, String>(2, "c"),
new Tuple2<Integer, String>(0, "a"));
- assertEquals(
- new HashSet<Tuple2<Integer, String>>(expected),
- new HashSet<Tuple2<Integer,
String>>(rebalancePartitionResult));
+ List<Tuple2<Integer, String>> expected1 = Arrays.asList(
+ new Tuple2<Integer, String>(1, "a"),
+ new Tuple2<Integer, String>(2, "b"),
+ new Tuple2<Integer, String>(0, "b"),
+ new Tuple2<Integer, String>(1, "a"),
+ new Tuple2<Integer, String>(2, "a"),
+ new Tuple2<Integer, String>(0, "c"),
+ new Tuple2<Integer, String>(1, "a"));
+
+ List<Tuple2<Integer, String>> expected2 = Arrays.asList(
+ new Tuple2<Integer, String>(2, "a"),
+ new Tuple2<Integer, String>(0, "b"),
+ new Tuple2<Integer, String>(1, "b"),
+ new Tuple2<Integer, String>(2, "a"),
+ new Tuple2<Integer, String>(0, "a"),
+ new Tuple2<Integer, String>(1, "c"),
+ new Tuple2<Integer, String>(2, "a"));
+
+ assertTrue(
+ new HashSet<Tuple2<Integer,
String>>(expected0).equals(new HashSet<Tuple2<Integer,
String>>(rebalancePartitionResult)) ||
+ new HashSet<Tuple2<Integer,
String>>(expected1).equals(new HashSet<Tuple2<Integer,
String>>(rebalancePartitionResult)) ||
+ new HashSet<Tuple2<Integer,
String>>(expected2).equals(new HashSet<Tuple2<Integer,
String>>(rebalancePartitionResult))
+ );
}
private static void verifyGlobalPartitioning(List<Tuple2<Integer,
String>> globalPartitionResult) {
----------------------------------------------------------------
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:
[email protected]
> RebalancePartitioner should use Random value for its first partition
> --------------------------------------------------------------------
>
> Key: FLINK-8532
> URL: https://issues.apache.org/jira/browse/FLINK-8532
> Project: Flink
> Issue Type: Improvement
> Components: DataStream API
> Reporter: Yuta Morisawa
> Priority: Minor
> Labels: pull-request-available
>
> In some conditions, RebalancePartitioner doesn't balance data correctly
> because it use the same value for selecting next operators.
> RebalancePartitioner initializes its partition id using the same value in
> every threads, so it indeed balances data, but at one moment the amount of
> data in each operator is skew.
> Particularly, when the data rate of former operators is equal , data skew
> becomes severe.
>
>
> Example:
> Consider a simple operator chain.
> -> map1 -> rebalance -> map2 ->
> Each map operator(map1, map2) contains three subtasks(subtask 1, 2, 3, 4, 5,
> 6).
> map1 map2
> st1 st4
> st2 st5
> st3 st6
>
> At the beginning, every subtasks in map1 sends data to st4 in map2 because
> they use the same initial parition id.
> Next time the map1 receive data st1,2,3 send data to st5 because they
> increment its partition id when they processed former data.
> In my environment, it takes twice the time to process data when I use
> RebalancePartitioner as long as I use other partitioners(rescale, keyby).
>
> To solve this problem, in my opinion, RebalancePartitioner should use its own
> operator id for the initial value.
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)