Skip to content

Commit

Permalink
[FLINK-2318] Union can be used as BroadcastVariable
Browse files Browse the repository at this point in the history
This closes apache#1390
  • Loading branch information
zentol committed Nov 24, 2015
1 parent 49f5a01 commit 25ef324
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,13 @@ private int translateChannel(Channel input, int inputIndex, JobVertex targetVert
// the inputs of the union as well, because the optimizer has a separate union
// node, which does not exist in the JobGraph. Otherwise, this can result in
// deadlocks when closing a branching flow at runtime.
if (input.getDataExchangeMode().equals(DataExchangeMode.BATCH)) {
for (Channel in : inputPlanNode.getInputs()) {
for (Channel in : inputPlanNode.getInputs()) {
if (input.getDataExchangeMode().equals(DataExchangeMode.BATCH)) {
in.setDataExchangeMode(DataExchangeMode.BATCH);
}
if (isBroadcast) {
in.setShipStrategy(ShipStrategyType.BROADCAST, in.getDataExchangeMode());
}
}
}
else if (inputPlanNode instanceof BulkPartialSolutionPlanNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,10 @@ public void addInputToGroup(int groupIndex) {

public void addBroadcastInputToGroup(int groupIndex) {
final String grp = BROADCAST_INPUT_GROUP_SIZE_PREFIX + groupIndex;
if (!this.config.containsKey(grp)) {
this.config.setInteger(NUM_BROADCAST_INPUTS, this.config.getInteger(NUM_BROADCAST_INPUTS, 0) + 1);
}
this.config.setInteger(grp, this.config.getInteger(grp, 0) + 1);
this.config.setInteger(NUM_BROADCAST_INPUTS, this.config.getInteger(NUM_BROADCAST_INPUTS, 0) + 1);
}

public void setInputAsynchronouslyMaterialized(int inputNum, boolean temp) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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:https://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.test.broadcastvars;

import java.util.List;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.test.util.JavaProgramTestBase;
import org.junit.Assert;

public class BroadcastUnionITCase extends JavaProgramTestBase {
private static final String BC_NAME = "bc";

@Override
protected void testProgram() throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(4);

DataSet<Long> input = env.generateSequence(1, 10);
DataSet<Long> bc1 = env.generateSequence(1, 5);
DataSet<Long> bc2 = env.generateSequence(6, 10);

List<Long> result = input
.map(new Mapper())
.withBroadcastSet(bc1.union(bc2), BC_NAME)
.reduce(new Reducer())
.collect();

Assert.assertEquals(result.get(0), Long.valueOf(3025));
}

public static class Mapper extends RichMapFunction<Long, Long> {
private List<Long> values;

@Override
public void open(Configuration config) {
values = getRuntimeContext().getBroadcastVariable(BC_NAME);
}

@Override
public Long map(Long value) throws Exception {
long sum = 0;
for (Long v : values) {
sum += value * v;
}
return sum;
}
}

public static class Reducer implements ReduceFunction<Long> {
@Override
public Long reduce(Long value1, Long value2) throws Exception {
return value1 + value2;
}
}
}

0 comments on commit 25ef324

Please sign in to comment.