blob: e33900fda39a5b5d9aae23e5fe714ba9f829550b [file] [log] [blame]
/*
* 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.nemo.common.ir.vertex.transform;
import org.apache.nemo.common.Pair;
import org.apache.nemo.common.ir.OutputCollector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.BiFunction;
/**
* A {@link Transform} that aggregates statistics generated by the {@link MessageBarrierTransform}.
*
* @param <K> input key type.
* @param <V> input value type.
* @param <O> output type.
*/
public final class MessageAggregatorTransform<K, V, O> extends NoWatermarkEmitTransform<Pair<K, V>, O> {
private static final Logger LOG = LoggerFactory.getLogger(MessageAggregatorTransform.class.getName());
private OutputCollector<O> outputCollector;
private O aggregatedDynOptData;
private final BiFunction<Pair<K, V>, O, O> dynOptDataAggregator;
/**
* Default constructor.
*
* @param aggregatedDynOptData per-stage aggregated dynamic optimization data.
* @param dynOptDataAggregator aggregator to use.
*/
public MessageAggregatorTransform(final O aggregatedDynOptData,
final BiFunction<Pair<K, V>, O, O> dynOptDataAggregator) {
this.aggregatedDynOptData = aggregatedDynOptData;
this.dynOptDataAggregator = dynOptDataAggregator;
}
@Override
public void prepare(final Context context, final OutputCollector<O> oc) {
this.outputCollector = oc;
}
@Override
public void onData(final Pair<K, V> element) {
aggregatedDynOptData = dynOptDataAggregator.apply(element, aggregatedDynOptData);
}
@Override
public void close() {
outputCollector.emit(aggregatedDynOptData);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append(MessageAggregatorTransform.class);
sb.append(":");
sb.append(super.toString());
return sb.toString();
}
}