You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2018/10/16 19:25:19 UTC

[GitHub] zentol closed pull request #1157: [FLINK-2720][storm-compatibility]Add Storm-CountMetric for storm metrics

zentol closed pull request #1157: [FLINK-2720][storm-compatibility]Add Storm-CountMetric for storm metrics
URL: https://github.com/apache/flink/pull/1157
 
 
   

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-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/api/FlinkTopologyContext.java b/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/api/FlinkTopologyContext.java
index a7616170d5b..5d20755339d 100644
--- a/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/api/FlinkTopologyContext.java
+++ b/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/api/FlinkTopologyContext.java
@@ -26,6 +26,9 @@
 import backtype.storm.metric.api.ReducedMetric;
 import backtype.storm.state.ISubscribedState;
 import backtype.storm.task.TopologyContext;
+import org.apache.flink.api.common.accumulators.LongCounter;
+import org.apache.flink.stormcompatibility.api.metrics.FlinkCountMetric;
+import org.apache.flink.streaming.runtime.tasks.StreamingRuntimeContext;
 
 import java.util.Collection;
 import java.util.Map;
@@ -36,6 +39,8 @@
  */
 public class FlinkTopologyContext extends TopologyContext {
 
+	private StreamingRuntimeContext runtimeContext;
+
 	/**
 	 * Instantiates a new {@link FlinkTopologyContext} for a given Storm topology. The context object is instantiated
 	 * for each parallel task
@@ -53,6 +58,10 @@ public FlinkTopologyContext(final StormTopology topology, final Map<Integer, Str
 				null, null);
 	}
 
+	public void setContext(final StreamingRuntimeContext context) {
+		this.runtimeContext = context;
+	}
+
 	/**
 	 * Not supported by Flink.
 	 *
@@ -120,7 +129,19 @@ public ReducedMetric registerMetric(final String name, final IReducer combiner,
 	@SuppressWarnings("unchecked")
 	@Override
 	public IMetric registerMetric(final String name, final IMetric metric, final int timeBucketSizeInSecs) {
-		throw new UnsupportedOperationException("Metrics are not supported by Flink");
+
+		// TODO: There is no use for timeBucketSizeInSecs yet.
+		if (metric instanceof FlinkCountMetric) {
+			LongCounter count = new LongCounter();
+			FlinkCountMetric flinkCount = (FlinkCountMetric)metric;
+			flinkCount.setCounter(count);
+
+			// register accumulator to RuntimeContext
+			this.runtimeContext.addAccumulator(name, count);
+			return metric;
+		} else{
+			throw new UnsupportedOperationException("Metrics are not supported by Flink");
+		}
 	}
 
 	/**
diff --git a/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/api/metrics/FlinkCountMetric.java b/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/api/metrics/FlinkCountMetric.java
new file mode 100644
index 00000000000..a168d0699d1
--- /dev/null
+++ b/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/api/metrics/FlinkCountMetric.java
@@ -0,0 +1,53 @@
+/*
+ * 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.flink.stormcompatibility.api.metrics;
+
+import backtype.storm.metric.api.CountMetric;
+import org.apache.flink.api.common.accumulators.LongCounter;
+
+/**
+ * The wrapper for using {@link backtype.storm.metric.api.CountMetric}
+ */
+public class FlinkCountMetric extends CountMetric {
+
+	private LongCounter longCounter;
+
+	public FlinkCountMetric () {
+	}
+
+	public void setCounter (LongCounter longCounter) {
+		this.longCounter = longCounter;
+	}
+
+	@Override
+	public void incr() {
+		incrBy(1);
+	}
+
+	@Override
+	public void incrBy(long incrementBy) {
+		this.longCounter.add(incrementBy);
+	}
+
+	@Override
+	public Object getValueAndReset() {
+		long ret = this.longCounter.getLocalValue();
+		this.longCounter.resetLocal();
+		return ret;
+	}
+}
diff --git a/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/wrappers/StormWrapperSetupHelper.java b/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/wrappers/StormWrapperSetupHelper.java
index 75ab8e0d8c0..16bc4f53866 100644
--- a/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/wrappers/StormWrapperSetupHelper.java
+++ b/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/main/java/org/apache/flink/stormcompatibility/wrappers/StormWrapperSetupHelper.java
@@ -104,7 +104,10 @@ public static TopologyContext convertToTopologyContext(final StreamingRuntimeCon
 			bolts.put(context.getTaskName(), new Bolt(null, common));
 		}
 
-		return new FlinkTopologyContext(new StormTopology(spoutSpecs, bolts, null), taskToComponents, taskId);
+		FlinkTopologyContext flinkContext = new FlinkTopologyContext(new StormTopology(spoutSpecs, bolts, null), taskToComponents, taskId);
+
+		flinkContext.setContext(context);
+		return flinkContext;
 	}
 
 }
diff --git a/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/test/java/org/apache/flink/stormcompatibility/api/metrics/FlinkCountMetricTest.java b/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/test/java/org/apache/flink/stormcompatibility/api/metrics/FlinkCountMetricTest.java
new file mode 100644
index 00000000000..ce5cc570cb0
--- /dev/null
+++ b/flink-contrib/flink-storm-compatibility/flink-storm-compatibility-core/src/test/java/org/apache/flink/stormcompatibility/api/metrics/FlinkCountMetricTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.flink.stormcompatibility.api.metrics;
+
+import org.apache.flink.api.common.accumulators.LongCounter;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+public class FlinkCountMetricTest {
+
+	@Test
+	public void testAll() {
+		LongCounter longCounter = new LongCounter();
+		FlinkCountMetric flinkCount = new FlinkCountMetric();
+		flinkCount.setCounter(longCounter);
+		flinkCount.incr();
+		assertEquals(1L, flinkCount.getValueAndReset());
+		flinkCount.incrBy(1);
+		assertEquals(1L, flinkCount.getValueAndReset());
+		assertEquals(0L, flinkCount.getValueAndReset());
+	}
+}


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services