Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.beam.model.pipeline.v1.RunnerApi.ReadPayload;
import org.apache.beam.model.pipeline.v1.RunnerApi.StandardEnvironments;
import org.apache.beam.model.pipeline.v1.RunnerApi.WindowIntoPayload;
import org.apache.beam.sdk.metrics.BeamUrns;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this file was fine before without importing this? : s

import org.apache.beam.sdk.util.common.ReflectHelpers;
import org.apache.beam.vendor.grpc.v1p13p1.com.google.protobuf.ByteString;
import org.apache.beam.vendor.grpc.v1p13p1.com.google.protobuf.InvalidProtocolBufferException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.beam.runners.core.construction;

import static org.apache.beam.runners.core.construction.BeamUrns.getUrn;
import static org.apache.beam.sdk.metrics.BeamUrns.getUrn;
import static org.apache.beam.vendor.guava.v20_0.com.google.common.base.Preconditions.checkArgument;

import com.google.auto.value.AutoValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.beam.runners.core.construction;

import static org.apache.beam.runners.core.construction.BeamUrns.getUrn;
import static org.apache.beam.sdk.metrics.BeamUrns.getUrn;
import static org.apache.beam.vendor.guava.v20_0.com.google.common.base.Preconditions.checkArgument;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.beam.runners.core.construction;

import static org.apache.beam.runners.core.construction.BeamUrns.getUrn;
import static org.apache.beam.sdk.metrics.BeamUrns.getUrn;

import java.io.IOException;
import java.io.Serializable;
Expand All @@ -30,6 +30,7 @@
import org.apache.beam.model.pipeline.v1.StandardWindowFns.GlobalWindowsPayload;
import org.apache.beam.model.pipeline.v1.StandardWindowFns.SessionsPayload;
import org.apache.beam.model.pipeline.v1.StandardWindowFns.SlidingWindowsPayload;
import org.apache.beam.sdk.metrics.BeamUrns;
import org.apache.beam.sdk.transforms.windowing.FixedWindows;
import org.apache.beam.sdk.transforms.windowing.GlobalWindows;
import org.apache.beam.sdk.transforms.windowing.Sessions;
Expand Down Expand Up @@ -354,6 +355,7 @@ public static RunnerApi.WindowingStrategy toProto(
.withOnTimeBehavior(onTimeBehavior);
}

/** Convert an {@link SdkFunctionSpec} to a {@link WindowFn}. */
public static WindowFn<?, ?> windowFnFromProto(SdkFunctionSpec windowFnSpec) {
try {
String s = windowFnSpec.getSpec().getUrn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.beam.runners.core.construction.graph;

import static org.apache.beam.runners.core.construction.BeamUrns.getUrn;
import static org.apache.beam.sdk.metrics.BeamUrns.getUrn;
import static org.apache.beam.vendor.guava.v20_0.com.google.common.base.Preconditions.checkArgument;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.beam.runners.core.construction;

import static org.apache.beam.runners.core.construction.BeamUrns.getUrn;
import static org.apache.beam.sdk.metrics.BeamUrns.getUrn;
import static org.apache.beam.vendor.guava.v20_0.com.google.common.base.MoreObjects.firstNonNull;
import static org.apache.beam.vendor.guava.v20_0.com.google.common.base.Preconditions.checkNotNull;
import static org.hamcrest.Matchers.equalTo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.io.CountingSource;
import org.apache.beam.sdk.io.Read;
import org.apache.beam.sdk.metrics.BeamUrns;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.DoFnSchemaInformation;
import org.apache.beam.sdk.transforms.ParDo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.beam.sdk.metrics.Counter;
import org.apache.beam.sdk.metrics.DelegatingCounter;
import org.apache.beam.sdk.metrics.MetricName;

/**
* Define a metric on the current MetricContainer with a specific URN and a set of labels. This is a
Expand All @@ -29,7 +30,7 @@ public class LabeledMetrics {
/**
* Create a metric that can be incremented and decremented, and is aggregated by taking the sum.
*/
public static Counter counter(MonitoringInfoMetricName metricName) {
public static Counter counter(MetricName metricName) {
return new DelegatingCounter(metricName);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo;
import org.apache.beam.runners.core.metrics.MetricUpdates.MetricUpdate;
Expand All @@ -32,6 +31,7 @@
import org.apache.beam.sdk.metrics.MetricKey;
import org.apache.beam.sdk.metrics.MetricName;
import org.apache.beam.sdk.metrics.MetricsContainer;
import org.apache.beam.sdk.metrics.labels.MetricLabels;
import org.apache.beam.vendor.guava.v20_0.com.google.common.collect.ImmutableList;

/**
Expand All @@ -50,7 +50,7 @@
@Experimental(Kind.METRICS)
public class MetricsContainerImpl implements Serializable, MetricsContainer {

@Nullable private final String stepName;
private final MetricLabels labels;

private MetricsMap<MetricName, CounterCell> counters = new MetricsMap<>(CounterCell::new);

Expand All @@ -59,9 +59,17 @@ public class MetricsContainerImpl implements Serializable, MetricsContainer {

private MetricsMap<MetricName, GaugeCell> gauges = new MetricsMap<>(GaugeCell::new);

public static MetricsContainerImpl ptransform(String ptransform) {
return new MetricsContainerImpl(MetricLabels.ptransform(ptransform));
}

public static MetricsContainerImpl pcollection(String pcollection) {
return new MetricsContainerImpl(MetricLabels.pcollection(pcollection));
}

/** Create a new {@link MetricsContainerImpl} associated with the given {@code stepName}. */
public MetricsContainerImpl(@Nullable String stepName) {
this.stepName = stepName;
public MetricsContainerImpl(MetricLabels labels) {
this.labels = labels;
}

/**
Expand Down Expand Up @@ -124,7 +132,7 @@ ImmutableList<MetricUpdate<UpdateT>> extractUpdates(MetricsMap<MetricName, CellT
if (cell.getValue().getDirty().beforeCommit()) {
updates.add(
MetricUpdate.create(
MetricKey.create(stepName, cell.getKey()), cell.getValue().getCumulative()));
MetricKey.of(cell.getKey(), labels), cell.getValue().getCumulative()));
}
}
return updates.build();
Expand All @@ -145,30 +153,11 @@ public MetricUpdates getUpdates() {
*/
@Nullable
private MonitoringInfo counterUpdateToMonitoringInfo(MetricUpdate<Long> metricUpdate) {
SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder(true);
MetricName metricName = metricUpdate.getKey().metricName();
if (metricName instanceof MonitoringInfoMetricName) {
MonitoringInfoMetricName monitoringInfoName = (MonitoringInfoMetricName) metricName;
// Represents a specific MonitoringInfo for a specific URN.
builder.setUrn(monitoringInfoName.getUrn());
for (Entry<String, String> e : monitoringInfoName.getLabels().entrySet()) {
builder.setLabel(e.getKey(), e.getValue());
}
} else { // Note: (metricName instanceof MetricName) is always True.
// Represents a user counter.
builder.setUrnForUserMetric(
metricUpdate.getKey().metricName().getNamespace(),
metricUpdate.getKey().metricName().getName());
// Drop if the stepname is not set. All user counters must be
// defined for a PTransform. They must be defined on a container bound to a step.
if (this.stepName == null) {
return null;
}
builder.setPTransformLabel(metricUpdate.getKey().stepName());
}
builder.setInt64Value(metricUpdate.getUpdate());
builder.setTimestampToNow();
return builder.build();
return new SimpleMonitoringInfoBuilder(true)
.setLabelsAndUrn(metricUpdate.getKey())
.setInt64Value(metricUpdate.getUpdate())
.setTimestampToNow()
.build();
}

/** Return the cumulative values for any metrics in this container as MonitoringInfos. */
Expand Down Expand Up @@ -207,7 +196,7 @@ ImmutableList<MetricUpdate<UpdateT>> extractCumulatives(MetricsMap<MetricName, C
ImmutableList.Builder<MetricUpdate<UpdateT>> updates = ImmutableList.builder();
for (Map.Entry<MetricName, CellT> cell : cells.entries()) {
UpdateT update = checkNotNull(cell.getValue().getCumulative());
updates.add(MetricUpdate.create(MetricKey.create(stepName, cell.getKey()), update));
updates.add(MetricUpdate.create(MetricKey.of(cell.getKey(), labels), update));
}
return updates.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
*/
package org.apache.beam.runners.core.metrics;

import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toList;
import static org.apache.beam.vendor.guava.v20_0.com.google.common.collect.Iterables.concat;

import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -32,55 +30,53 @@
import org.apache.beam.sdk.metrics.MetricKey;
import org.apache.beam.sdk.metrics.MetricResult;
import org.apache.beam.sdk.metrics.MetricResults;
import org.apache.beam.sdk.metrics.labels.MetricLabels;

/**
* Metrics containers by step.
*
* <p>This class is not thread-safe.
*/
public class MetricsContainerStepMap implements Serializable {

private Map<String, MetricsContainerImpl> metricsContainers;
private MetricsContainerImpl unboundContainer = new MetricsContainerImpl(null);
private Map<MetricLabels, MetricsContainerImpl> metricsContainers;

public MetricsContainerStepMap() {
this.metricsContainers = new ConcurrentHashMap<>();
}

/* Returns the container that is not bound to any step name. */
public MetricsContainerImpl getUnboundContainer() {
return this.unboundContainer;
public MetricsContainerImpl ptransformContainer(String ptransform) {
return getContainer(MetricLabels.ptransform(ptransform));
}

public MetricsContainerImpl pcollectionContainer(String pcollection) {
return getContainer(MetricLabels.pcollection(pcollection));
}

/** Returns the container for the given step name. */
public MetricsContainerImpl getContainer(String stepName) {
if (stepName == null) {
// TODO(BEAM-6538): Disallow this in the future, some tests rely on an empty step name today.
return getUnboundContainer();
}
if (!metricsContainers.containsKey(stepName)) {
metricsContainers.put(stepName, new MetricsContainerImpl(stepName));
public MetricsContainerImpl getContainer(MetricLabels labels) {
if (!metricsContainers.containsKey(labels)) {

@pabloem pabloem Mar 16, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesnt seem like MetricLabels implements a hashCode or an equals function. I may be missing something, but perhaps we need a test that checks that getContainer(MetricLabels.ptransform("A")) will work (creating a new metriclabels), and that MetricLabels.ptransform("A").equals(MetricLabels.ptransform("A"))?

metricsContainers.put(labels, new MetricsContainerImpl(labels));
}
return metricsContainers.get(stepName);
return metricsContainers.get(labels);
}

/**
* Update this {@link MetricsContainerStepMap} with all values from given {@link
* MetricsContainerStepMap}.
*/
public void updateAll(MetricsContainerStepMap other) {
for (Map.Entry<String, MetricsContainerImpl> container : other.metricsContainers.entrySet()) {
for (Map.Entry<MetricLabels, MetricsContainerImpl> container :
other.metricsContainers.entrySet()) {
getContainer(container.getKey()).update(container.getValue());
}
getUnboundContainer().update(other.getUnboundContainer());
}

/**
* Update {@link MetricsContainerImpl} for given step in this map with all values from given
* {@link MetricsContainerImpl}.
*/
public void update(String step, MetricsContainerImpl container) {
getContainer(step).update(container);
public void update(MetricLabels labels, MetricsContainerImpl container) {
getContainer(labels).update(container);
}

@Override
Expand Down Expand Up @@ -172,7 +168,7 @@ public String toString() {
}

private Iterable<MetricsContainerImpl> getMetricsContainers() {
return concat(metricsContainers.values(), singleton(unboundContainer));
return metricsContainers.values();
}

@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.beam.sdk.annotations.Experimental.Kind;
import org.apache.beam.sdk.metrics.MetricKey;
import org.apache.beam.sdk.metrics.MetricName;
import org.apache.beam.sdk.metrics.labels.MetricLabels;
import org.apache.beam.vendor.guava.v20_0.com.google.common.cache.CacheBuilder;
import org.apache.beam.vendor.guava.v20_0.com.google.common.cache.CacheLoader;
import org.apache.beam.vendor.guava.v20_0.com.google.common.cache.LoadingCache;
Expand All @@ -36,6 +37,7 @@ public abstract class MetricsTranslation {

private MetricsTranslation() {}

/** Convert some proto user-metric updates to {@link MetricUpdates}. */
public static MetricUpdates metricUpdatesFromProto(
String ptransformName, Collection<BeamFnApi.Metrics.User> userMetricUpdates) {
List<MetricUpdates.MetricUpdate<Long>> counterUpdates = new ArrayList<>();
Expand All @@ -44,7 +46,9 @@ public static MetricUpdates metricUpdatesFromProto(

for (BeamFnApi.Metrics.User userMetricUpdate : userMetricUpdates) {
MetricKey metricKey =
MetricKey.create(ptransformName, metricNameFromProto(userMetricUpdate.getMetricName()));
MetricKey.of(
metricNameFromProto(userMetricUpdate.getMetricName()),
MetricLabels.ptransform(ptransformName));
switch (userMetricUpdate.getDataCase()) {
case COUNTER_DATA:
counterUpdates.add(
Expand Down Expand Up @@ -73,6 +77,7 @@ public static MetricUpdates metricUpdatesFromProto(
return MetricUpdates.create(counterUpdates, distributionUpdates, gaugeUpdates);
}

/** Convert a {@link MetricUpdates} to user-metric update protos. */
public static Map<String, Collection<BeamFnApi.Metrics.User>> metricUpdatesToProto(
MetricUpdates metricUpdates) {
LoadingCache<String, Collection<BeamFnApi.Metrics.User>> fnMetrics =
Expand Down
Loading