Class PartitionKeyedAccumulator<T>
- Type Parameters:
T- the per-partition value type. Must be non-null (ConcurrentHashMapforbids nulls).
- All Implemented Interfaces:
Serializable
AccumulatorV2 that records one value of type T per partition, keyed by partition id with
LAST-WRITE-WINS merge. When the same partition is recorded more than once -- e.g. duplicate
cross-executor computes, or speculative tasks -- the later value replaces the earlier one rather
than aggregating, so each partition contributes exactly once. The key set is the set of recorded
partitions, and callers fold the values (see <A>foldValues(A,scala.Function2<A,T,A>)) to derive de-duplicated aggregates;
a plain summing accumulator would instead over-count under duplicate computes.
add is expected to be called once per task (e.g. from a task completion listener) with that
partition's value, so a partition is recorded even when it produced nothing. Updates from
failed/interrupted tasks are dropped by the accumulator framework (it is not
countFailedValues), so only complete per-partition values are ever merged.
Backed by a ConcurrentHashMap, whose per-entry atomicity is sufficient here: add and the
putAll in merge are last-write-wins per key, and the reads (value,
accumulatedNumPartitions, foldValues) only require thread-safety and eventual consistency
-- they are weakly consistent during concurrent updates but exact once all updates have been
merged. This avoids any explicit locking (and the nested-lock pattern a two-map merge would
otherwise need).
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongNumber of distinct partitions that have been recorded.voidTakes the inputs and accumulates.copy()Creates a new copy of this accumulator.Creates a new copy of this accumulator, which is zero value.<A> AfoldValues(A zero, scala.Function2<A, T, A> op) Folds the per-partition values (each partition counted once) into a single aggregate.booleanisZero()Returns if this accumulator is zero value or not.voidMerges another same-type accumulator into this one and update its state, i.e.voidreset()Resets this accumulator, which is zero value.value()Defines the current value of this accumulatorMethods inherited from class org.apache.spark.util.AccumulatorV2
excludeFromHeartbeat, id, isRegistered, name, toString
-
Constructor Details
-
PartitionKeyedAccumulator
public PartitionKeyedAccumulator()
-
-
Method Details
-
accumulatedNumPartitions
public long accumulatedNumPartitions()Number of distinct partitions that have been recorded. -
add
Description copied from class:AccumulatorV2Takes the inputs and accumulates. -
copy
Description copied from class:AccumulatorV2Creates a new copy of this accumulator. -
copyAndReset
Description copied from class:AccumulatorV2Creates a new copy of this accumulator, which is zero value. i.e. callisZeroon the copy must return true.- Overrides:
copyAndResetin classAccumulatorV2<scala.Tuple2<Object,T>, Map<Object, T>> - Returns:
- (undocumented)
-
foldValues
Folds the per-partition values (each partition counted once) into a single aggregate. -
isZero
public boolean isZero()Description copied from class:AccumulatorV2Returns if this accumulator is zero value or not. e.g. for a counter accumulator, 0 is zero value; for a list accumulator, Nil is zero value. -
merge
Description copied from class:AccumulatorV2Merges another same-type accumulator into this one and update its state, i.e. this should be merge-in-place. -
reset
public void reset()Description copied from class:AccumulatorV2Resets this accumulator, which is zero value. i.e. callisZeromust return true. -
value
Description copied from class:AccumulatorV2Defines the current value of this accumulator
-