[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-19 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r226577921
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,170 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.GuardedBy;
+
+import java.io.Closeable;
+import java.math.BigInteger;
+
+/**
+ * A monitor which pulls {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricMonitor.class);
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final MetricGroup metricGroup;
+
+   private final Object lock;
+
+   @GuardedBy("lock")
+   private RocksDB rocksDB;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB rocksDB,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull MetricGroup metricGroup
+   ) {
+   this.options = options;
+   this.metricGroup = metricGroup;
+   this.rocksDB = rocksDB;
+
+   this.lock = new Object();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   MetricGroup group = metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(handle, property);
+   group.gauge(property, gauge);
+   }
+   }
+
+   /**
+* Updates the value of metricView if the reference is still valid.
+*/
+   private void setProperty(ColumnFamilyHandle handle, String property, 
RocksDBNativeMetricView metricView) {
+   if (metricView.isClosed()) {
 
 Review comment:
   cool, thanks for looking into it. We can leave the code as is.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-17 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225872262
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,170 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.GuardedBy;
+
+import java.io.Closeable;
+import java.math.BigInteger;
+
+/**
+ * A monitor which pulls {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricMonitor.class);
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final MetricGroup metricGroup;
+
+   private final Object lock;
+
+   @GuardedBy("lock")
+   private RocksDB rocksDB;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB rocksDB,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull MetricGroup metricGroup
+   ) {
+   this.options = options;
+   this.metricGroup = metricGroup;
+   this.rocksDB = rocksDB;
+
+   this.lock = new Object();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   MetricGroup group = metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(handle, property);
+   group.gauge(property, gauge);
+   }
+   }
+
+   /**
+* Updates the value of metricView if the reference is still valid.
+*/
+   private void setProperty(ColumnFamilyHandle handle, String property, 
RocksDBNativeMetricView metricView) {
+   if (metricView.isClosed()) {
 
 Review comment:
   @StefanRRichter Do you know anything about the reliability of  
`RocksDB#getLongProperty`?


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-17 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225865415
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,170 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.GuardedBy;
+
+import java.io.Closeable;
+import java.math.BigInteger;
+
+/**
+ * A monitor which pulls {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricMonitor.class);
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final MetricGroup metricGroup;
+
+   private final Object lock;
+
+   @GuardedBy("lock")
+   private RocksDB rocksDB;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB rocksDB,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull MetricGroup metricGroup
+   ) {
+   this.options = options;
+   this.metricGroup = metricGroup;
+   this.rocksDB = rocksDB;
+
+   this.lock = new Object();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   MetricGroup group = metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(handle, property);
+   group.gauge(property, gauge);
+   }
+   }
+
+   /**
+* Updates the value of metricView if the reference is still valid.
+*/
+   private void setProperty(ColumnFamilyHandle handle, String property, 
RocksDBNativeMetricView metricView) {
+   if (metricView.isClosed()) {
 
 Review comment:
   ah. hmm...
   
   Let's say that an exception occurs when retrieving the value from RocksDB. 
How likely is it that this exception happens again? I.e. how reliable are 
failure-cases? Currently, if there is any intermittent failure the metric is 
disabled forever.
   
   If this is more of an all-or-nothing kind of thing we could try it out once 
before registering the metric and skipping the registration if it fails, and 
only skip repeated logging in the metric.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225455306
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitorTest.java
 ##
 @@ -0,0 +1,179 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MetricOptions;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.AbstractMetricGroup;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormats;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+
+/**
+ * validate native metric monitor.
+ */
+public class RocksDBNativeMetricMonitorTest {
+
+   private static final String OPERATOR_NAME = "dummy";
+
+   private static final String COLUMN_FAMILY_NAME = "column-family";
+
+   private static final BigInteger ZERO = new BigInteger(1, new byte[]{0, 
0, 0, 0});
+
+   @Test
+   public void testMetricMonitorLifecycle() throws Throwable {
+   //We use a local variable here to manually control the 
life-cycle.
+   // This allows us to verify that metrics do not try to access
+   // RocksDB after the monitor was closed.
+   RocksDBResource rocksDBResource = new RocksDBResource();
+   rocksDBResource.before();
+
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
+   registry,
+   
UnregisteredMetricGroups.createUnregisteredTaskMetricGroup(),
+   new OperatorID(),
+   OPERATOR_NAME
+   );
+
+   RocksDBNativeMetricOptions options = new 
RocksDBNativeMetricOptions();
+   // always returns a non-zero
+   // value since empty memtables
+   // have overhead.
+   options.enableSizeAllMemTables();
+
+   RocksDBNativeMetricMonitor monitor = new 
RocksDBNativeMetricMonitor(
+   rocksDBResource.getRocksDB(),
+   options,
+   group
+   );
+
+   ColumnFamilyHandle handle = 
rocksDBResource.createNewColumnFamily(COLUMN_FAMILY_NAME);
+   monitor.registerColumnFamily(COLUMN_FAMILY_NAME, handle);
+
+   Assert.assertEquals("Failed to register metrics for column 
family", 1, registry.metrics.size());
+
+   RocksDBNativeMetricMonitor.RocksDBNativeMetricView view = 
registry.metrics.get(0);
+
+   view.update();
+
+   Assert.assertNotEquals("Failed to pull metric from RocksDB", 
ZERO, view.getValue());
+
+   view.setValue(0L);
+
+   //After the monitor is closed no metric should be accessing 
RocksDB anymore.
+   //If they do, then this test will likely fail with a 
segmentation fault.
+   monitor.close();
+
+   rocksDBResource.after();
+
+   view.update();
+
+   Assert.assertEquals("Failed to release RocksDB reference", 
ZERO, view.getValue());
+   }
+
+   @Test
+   public void testReturnsUnsigned() throws Throwable {
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
 
 Review comment:
   could be simplified by using a `GenericMetricGroup`


This is an automated message from the Apache Git Service.
To respond to th

[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225455353
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitorTest.java
 ##
 @@ -0,0 +1,179 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MetricOptions;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.AbstractMetricGroup;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormats;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+
+/**
+ * validate native metric monitor.
+ */
+public class RocksDBNativeMetricMonitorTest {
+
+   private static final String OPERATOR_NAME = "dummy";
+
+   private static final String COLUMN_FAMILY_NAME = "column-family";
+
+   private static final BigInteger ZERO = new BigInteger(1, new byte[]{0, 
0, 0, 0});
+
+   @Test
+   public void testMetricMonitorLifecycle() throws Throwable {
+   //We use a local variable here to manually control the 
life-cycle.
+   // This allows us to verify that metrics do not try to access
+   // RocksDB after the monitor was closed.
+   RocksDBResource rocksDBResource = new RocksDBResource();
+   rocksDBResource.before();
+
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
 
 Review comment:
   could be simplified by using a `GenericMetricGroup`


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225454717
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitorTest.java
 ##
 @@ -0,0 +1,179 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MetricOptions;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.AbstractMetricGroup;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormats;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+
+/**
+ * validate native metric monitor.
+ */
+public class RocksDBNativeMetricMonitorTest {
+
+   private static final String OPERATOR_NAME = "dummy";
+
+   private static final String COLUMN_FAMILY_NAME = "column-family";
+
+   private static final BigInteger ZERO = new BigInteger(1, new byte[]{0, 
0, 0, 0});
+
+   @Test
+   public void testMetricMonitorLifecycle() throws Throwable {
+   //We use a local variable here to manually control the 
life-cycle.
+   // This allows us to verify that metrics do not try to access
+   // RocksDB after the monitor was closed.
+   RocksDBResource rocksDBResource = new RocksDBResource();
+   rocksDBResource.before();
+
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
 
 Review comment:
   this can be simplified to `MetricGroup group = new 
UnregisteredMetricGroups()`.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225454760
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitorTest.java
 ##
 @@ -0,0 +1,179 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MetricOptions;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.AbstractMetricGroup;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormats;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+
+/**
+ * validate native metric monitor.
+ */
+public class RocksDBNativeMetricMonitorTest {
+
+   private static final String OPERATOR_NAME = "dummy";
+
+   private static final String COLUMN_FAMILY_NAME = "column-family";
+
+   private static final BigInteger ZERO = new BigInteger(1, new byte[]{0, 
0, 0, 0});
+
+   @Test
+   public void testMetricMonitorLifecycle() throws Throwable {
+   //We use a local variable here to manually control the 
life-cycle.
+   // This allows us to verify that metrics do not try to access
+   // RocksDB after the monitor was closed.
+   RocksDBResource rocksDBResource = new RocksDBResource();
+   rocksDBResource.before();
+
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
+   registry,
+   
UnregisteredMetricGroups.createUnregisteredTaskMetricGroup(),
+   new OperatorID(),
+   OPERATOR_NAME
+   );
+
+   RocksDBNativeMetricOptions options = new 
RocksDBNativeMetricOptions();
+   // always returns a non-zero
+   // value since empty memtables
+   // have overhead.
+   options.enableSizeAllMemTables();
+
+   RocksDBNativeMetricMonitor monitor = new 
RocksDBNativeMetricMonitor(
+   rocksDBResource.getRocksDB(),
+   options,
+   group
+   );
+
+   ColumnFamilyHandle handle = 
rocksDBResource.createNewColumnFamily(COLUMN_FAMILY_NAME);
+   monitor.registerColumnFamily(COLUMN_FAMILY_NAME, handle);
+
+   Assert.assertEquals("Failed to register metrics for column 
family", 1, registry.metrics.size());
+
+   RocksDBNativeMetricMonitor.RocksDBNativeMetricView view = 
registry.metrics.get(0);
+
+   view.update();
+
+   Assert.assertNotEquals("Failed to pull metric from RocksDB", 
ZERO, view.getValue());
+
+   view.setValue(0L);
+
+   //After the monitor is closed no metric should be accessing 
RocksDB anymore.
+   //If they do, then this test will likely fail with a 
segmentation fault.
+   monitor.close();
+
+   rocksDBResource.after();
+
+   view.update();
+
+   Assert.assertEquals("Failed to release RocksDB reference", 
ZERO, view.getValue());
+   }
+
+   @Test
+   public void testReturnsUnsigned() throws Throwable {
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
 
 Review comment:
   this can be simplified to `MetricGroup group = new 
UnregisteredMetricGroups()`.


This is an automated message from the Apache 

[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225454760
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitorTest.java
 ##
 @@ -0,0 +1,179 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MetricOptions;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.AbstractMetricGroup;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormats;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+
+/**
+ * validate native metric monitor.
+ */
+public class RocksDBNativeMetricMonitorTest {
+
+   private static final String OPERATOR_NAME = "dummy";
+
+   private static final String COLUMN_FAMILY_NAME = "column-family";
+
+   private static final BigInteger ZERO = new BigInteger(1, new byte[]{0, 
0, 0, 0});
+
+   @Test
+   public void testMetricMonitorLifecycle() throws Throwable {
+   //We use a local variable here to manually control the 
life-cycle.
+   // This allows us to verify that metrics do not try to access
+   // RocksDB after the monitor was closed.
+   RocksDBResource rocksDBResource = new RocksDBResource();
+   rocksDBResource.before();
+
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
+   registry,
+   
UnregisteredMetricGroups.createUnregisteredTaskMetricGroup(),
+   new OperatorID(),
+   OPERATOR_NAME
+   );
+
+   RocksDBNativeMetricOptions options = new 
RocksDBNativeMetricOptions();
+   // always returns a non-zero
+   // value since empty memtables
+   // have overhead.
+   options.enableSizeAllMemTables();
+
+   RocksDBNativeMetricMonitor monitor = new 
RocksDBNativeMetricMonitor(
+   rocksDBResource.getRocksDB(),
+   options,
+   group
+   );
+
+   ColumnFamilyHandle handle = 
rocksDBResource.createNewColumnFamily(COLUMN_FAMILY_NAME);
+   monitor.registerColumnFamily(COLUMN_FAMILY_NAME, handle);
+
+   Assert.assertEquals("Failed to register metrics for column 
family", 1, registry.metrics.size());
+
+   RocksDBNativeMetricMonitor.RocksDBNativeMetricView view = 
registry.metrics.get(0);
+
+   view.update();
+
+   Assert.assertNotEquals("Failed to pull metric from RocksDB", 
ZERO, view.getValue());
+
+   view.setValue(0L);
+
+   //After the monitor is closed no metric should be accessing 
RocksDB anymore.
+   //If they do, then this test will likely fail with a 
segmentation fault.
+   monitor.close();
+
+   rocksDBResource.after();
+
+   view.update();
+
+   Assert.assertEquals("Failed to release RocksDB reference", 
ZERO, view.getValue());
+   }
+
+   @Test
+   public void testReturnsUnsigned() throws Throwable {
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
 
 Review comment:
   this can be simplified to `MetricGroup group = new 
UnregisteredMetricGroups()`.


This is an automated message from the Apache 

[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225454717
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitorTest.java
 ##
 @@ -0,0 +1,179 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MetricOptions;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.AbstractMetricGroup;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormats;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+
+/**
+ * validate native metric monitor.
+ */
+public class RocksDBNativeMetricMonitorTest {
+
+   private static final String OPERATOR_NAME = "dummy";
+
+   private static final String COLUMN_FAMILY_NAME = "column-family";
+
+   private static final BigInteger ZERO = new BigInteger(1, new byte[]{0, 
0, 0, 0});
+
+   @Test
+   public void testMetricMonitorLifecycle() throws Throwable {
+   //We use a local variable here to manually control the 
life-cycle.
+   // This allows us to verify that metrics do not try to access
+   // RocksDB after the monitor was closed.
+   RocksDBResource rocksDBResource = new RocksDBResource();
+   rocksDBResource.before();
+
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
 
 Review comment:
   this can be simplified to `MetricGroup group = new 
UnregisteredMetricGroups()`.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225454449
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,154 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.GuardedBy;
+
+import java.io.Closeable;
+import java.math.BigInteger;
+
+/**
+ * A monitor which pulls {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricMonitor.class);
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final MetricGroup metricGroup;
+
+   private final Object lock;
+
+   @GuardedBy("lock")
+   private RocksDB rocksDB;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB rocksDB,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull MetricGroup metricGroup
+   ) {
+   this.options = options;
+   this.metricGroup = metricGroup;
+   this.rocksDB = rocksDB;
+
+   this.lock = new Object();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   MetricGroup group = metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(handle, property);
+   group.gauge(property, gauge);
+   }
+   }
+
+   /**
+* Updates the value of metricView if the reference is still valid.
+*/
+   private void setProperty(ColumnFamilyHandle handle, String property, 
RocksDBNativeMetricView metricView) {
+   try {
+   synchronized (lock) {
+   if (rocksDB != null) {
+   long value = 
rocksDB.getLongProperty(handle, property);
+   metricView.setValue(value);
+   }
+   }
+   } catch (RocksDBException e) {
+   LOG.warn("Failed to read native metric %s from 
RocksDB", property, e);
+   }
+   }
+
+   @Override
+   public void close() {
+   synchronized (lock) {
+   rocksDB = null;
+   }
+   }
+
+   /**
+* A gauge which periodically pull a RocksDB native metric
 
 Review comment:
   pull -> pulls


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225454242
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,154 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.GuardedBy;
+
+import java.io.Closeable;
+import java.math.BigInteger;
+
+/**
+ * A monitor which pulls {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricMonitor.class);
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final MetricGroup metricGroup;
+
+   private final Object lock;
+
+   @GuardedBy("lock")
+   private RocksDB rocksDB;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB rocksDB,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull MetricGroup metricGroup
+   ) {
+   this.options = options;
+   this.metricGroup = metricGroup;
+   this.rocksDB = rocksDB;
+
+   this.lock = new Object();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   MetricGroup group = metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(handle, property);
+   group.gauge(property, gauge);
+   }
+   }
+
+   /**
+* Updates the value of metricView if the reference is still valid.
+*/
+   private void setProperty(ColumnFamilyHandle handle, String property, 
RocksDBNativeMetricView metricView) {
+   try {
+   synchronized (lock) {
+   if (rocksDB != null) {
+   long value = 
rocksDB.getLongProperty(handle, property);
+   metricView.setValue(value);
+   }
+   }
+   } catch (RocksDBException e) {
+   LOG.warn("Failed to read native metric %s from 
RocksDB", property, e);
 
 Review comment:
   We've had issues in the past where the amount of logging messages where 
drowning out other messages.
   If this exception occurs, and if multiple metrics (or god forbid all of 
them) are enabled, then this could lead to a large amount of logging messages.
   
   I suggest to add a flag to each gauge so that this message is only logged 
once for each of them.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225452792
 
 

 ##
 File path: 
flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/StreamTaskStateInitializerImplTest.java
 ##
 @@ -192,7 +196,8 @@ public OperatorStateBackend createOperatorStateBackend(
streamOperator.getClass().getSimpleName(),
streamOperator,
typeSerializer,
-   closeableRegistry);
+   closeableRegistry,
+   
UnregisteredMetricGroups.createUnregisteredOperatorMetricGroup());
 
 Review comment:
   use `new UnregisteredMetricsGroup()` instead. (separate class, more generic 
than the ones from `UnregisteredMetricGroups`. Also applies to other usages.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225452289
 
 

 ##
 File path: 
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/StreamTaskStateInitializer.java
 ##
 @@ -49,5 +51,7 @@ StreamOperatorStateContext streamOperatorStateContext(
@Nonnull String operatorClassName,
@Nonnull KeyContext keyContext,
@Nullable TypeSerializer keySerializer,
-   @Nonnull CloseableRegistry streamTaskCloseableRegistry) throws 
Exception;
+   @Nonnull CloseableRegistry streamTaskCloseableRegistry,
+   @Nonnull MetricGroup metricGroup
+   ) throws Exception;
 
 Review comment:
   indentation is off


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-16 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225451475
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBStateBackend.java
 ##
 @@ -456,7 +456,7 @@ public CheckpointStorage createCheckpointStorage(JobID 
jobId) throws IOException
priorityQueueStateType,
ttlTimeProvider,
getMemoryWatcherOptions(),
-   operatorMetricGroup
+   metricGroup
 
 Review comment:
   indentation is off


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-15 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225188200
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,134 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.io.Closeable;
+
+/**
+ * A monitor which pull {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricMonitor.class);
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final OperatorMetricGroup metricGroup;
+
+   private final Object lock;
+
+   private RocksDB rocksDB;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB rocksDB,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull OperatorMetricGroup metricGroup
+   ) {
+   this.options = options;
+   this.metricGroup = metricGroup;
+   this.rocksDB = rocksDB;
+
+   this.lock = new Object();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   MetricGroup group = metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(handle, property);
+   group.gauge(property, gauge);
+   }
+   }
+
+   /**
+* Updates the value of metricView if the reference is still valid.
+*/
+   private void setProperty(ColumnFamilyHandle handle, String property, 
RocksDBNativeMetricView metricView) {
+   try {
+   synchronized (lock) {
+   if (rocksDB != null) {
+   long value = 
rocksDB.getLongProperty(handle, property);
+   metricView.setValue(value);
+   }
+   }
+   } catch (RocksDBException e) {
+   LOG.warn("Failed to read native metric %s from 
RocksDB", property, e);
+   }
+   }
+
+   @Override
+   public void close() {
+   synchronized (lock) {
+   this.rocksDB = null;
+   }
+   }
+
+   class RocksDBNativeMetricView implements Gauge, View {
+   private final String property;
+
+   private final ColumnFamilyHandle handle;
+
+
+   private long value;
+
+   private RocksDBNativeMetricView(
+   @Nonnull ColumnFamilyHandle handle,
+   @Nonnull String property
+   ) {
+   this.handle = handle;
+   this.property = property;
+   }
+
+   public void setValue(long value) {
+   this.value = value;
+   }
+
+   @Override
+   public String getValue() {
+   return Long.toUnsignedString(value);
 
 Review comment:
   S

[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-15 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225113222
 
 

 ##
 File path: 
flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/StreamTaskStateInitializerImplTest.java
 ##
 @@ -186,13 +191,15 @@ public OperatorStateBackend createOperatorStateBackend(
 
TypeSerializer typeSerializer = new IntSerializer();
CloseableRegistry closeableRegistry = new CloseableRegistry();
+   MetricGroup operatorMetricGroup = mock(MetricGroup.class);
 
 Review comment:
   unused?


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-15 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225112469
 
 

 ##
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/state/StateBackend.java
 ##
 @@ -167,7 +206,9 @@
int numberOfKeyGroups,
KeyGroupRange keyGroupRange,
TaskKvStateRegistry kvStateRegistry,
-   TtlTimeProvider ttlTimeProvider) throws Exception;
+   TtlTimeProvider ttlTimeProvider,
+   OperatorMetricGroup operatorMetricGroup
 
 Review comment:
   can this be a plain `MetricGroup`?


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-15 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225117046
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitorTest.java
 ##
 @@ -0,0 +1,148 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MetricOptions;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.AbstractMetricGroup;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormats;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+
+/**
+ * validate native metric monitor.
+ */
+public class RocksDBNativeMetricMonitorTest {
+
+   private static final String OPERATOR_NAME = "dummy";
+
+   private static final String COLUMN_FAMILY_NAME = "column-family";
+
+   @Test
+   public void testMetricMonitorLifecycle() throws Throwable {
+   //created as a local variable instead of using @Rule
+   //we we can manually control the lifecycle. Using @Rule
+   //but manually calling after causes occasional segmentation
+   //faults.
+   RocksDBResource rocksDBResource = new RocksDBResource();
+   rocksDBResource.before();
+
+   SimpleMetricRegistry registry = new SimpleMetricRegistry();
+   OperatorMetricGroup group = new OperatorMetricGroup(
+   registry,
+   
UnregisteredMetricGroups.createUnregisteredTaskMetricGroup(),
+   new OperatorID(),
+   OPERATOR_NAME
+   );
+
+   RocksDBNativeMetricOptions options = new 
RocksDBNativeMetricOptions();
+   // always returns a non-zero
+   // value since empty memtables
+   // have overhead.
+   options.enableSizeAllMemTables();
+
+   RocksDBNativeMetricMonitor monitor = new 
RocksDBNativeMetricMonitor(
+   rocksDBResource.getRocksDB(),
+   options,
+   group
+   );
+
+   ColumnFamilyHandle handle = 
rocksDBResource.createNewColumnFamily(COLUMN_FAMILY_NAME);
+   monitor.registerColumnFamily(COLUMN_FAMILY_NAME, handle);
+
+   Assert.assertEquals("Failed to register metrics for column 
family", 1, registry.metrics.size());
+
+   RocksDBNativeMetricMonitor.RocksDBNativeMetricView view = 
registry.metrics.get(0);
+
+   view.update();
+
+   Assert.assertNotEquals("Failed to pull metric from RocksDB", 0, 
Long.parseLong(view.getValue()));
+
+   view.setValue(0L);
+
+   //removing this line reliably causes segmentation faults
 
 Review comment:
   re-phrase: `After the monitor is closed no metric should be accessing 
RocksDB anymore. If they do, then this test will likely fail with a 
segmentation fault.`


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-15 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225114941
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,134 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.io.Closeable;
+
+/**
+ * A monitor which pull {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricMonitor.class);
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final OperatorMetricGroup metricGroup;
+
+   private final Object lock;
+
+   private RocksDB rocksDB;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB rocksDB,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull OperatorMetricGroup metricGroup
+   ) {
+   this.options = options;
+   this.metricGroup = metricGroup;
+   this.rocksDB = rocksDB;
+
+   this.lock = new Object();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   MetricGroup group = metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(handle, property);
+   group.gauge(property, gauge);
+   }
+   }
+
+   /**
+* Updates the value of metricView if the reference is still valid.
+*/
+   private void setProperty(ColumnFamilyHandle handle, String property, 
RocksDBNativeMetricView metricView) {
+   try {
+   synchronized (lock) {
+   if (rocksDB != null) {
+   long value = 
rocksDB.getLongProperty(handle, property);
+   metricView.setValue(value);
+   }
+   }
+   } catch (RocksDBException e) {
+   LOG.warn("Failed to read native metric %s from 
RocksDB", property, e);
+   }
+   }
+
+   @Override
+   public void close() {
+   synchronized (lock) {
+   this.rocksDB = null;
+   }
+   }
+
+   class RocksDBNativeMetricView implements Gauge, View {
+   private final String property;
+
+   private final ColumnFamilyHandle handle;
+
+
+   private long value;
+
+   private RocksDBNativeMetricView(
+   @Nonnull ColumnFamilyHandle handle,
+   @Nonnull String property
+   ) {
+   this.handle = handle;
+   this.property = property;
+   }
+
+   public void setValue(long value) {
+   this.value = value;
+   }
+
+   @Override
+   public String getValue() {
+   return Long.toUnsignedString(value);
 
 Review comment:
   w

[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-15 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225116129
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitorTest.java
 ##
 @@ -0,0 +1,148 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MetricOptions;
+import org.apache.flink.metrics.Metric;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.metrics.groups.AbstractMetricGroup;
+import org.apache.flink.runtime.metrics.groups.OperatorMetricGroup;
+import org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups;
+import org.apache.flink.runtime.metrics.scope.ScopeFormats;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+
+/**
+ * validate native metric monitor.
+ */
+public class RocksDBNativeMetricMonitorTest {
+
+   private static final String OPERATOR_NAME = "dummy";
+
+   private static final String COLUMN_FAMILY_NAME = "column-family";
+
+   @Test
+   public void testMetricMonitorLifecycle() throws Throwable {
+   //created as a local variable instead of using @Rule
+   //we we can manually control the lifecycle. Using @Rule
+   //but manually calling after causes occasional segmentation
 
 Review comment:
   This comment raises more questions that it answers.
   
   `We use a local variable here to manually control the life-cycle. This 
allows us to verify that metrics do not try to access RocksDB after the monitor 
was closed.`


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-15 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225113805
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBStateBackend.java
 ##
 @@ -405,14 +413,15 @@ public CheckpointStorage createCheckpointStorage(JobID 
jobId) throws IOException
 
@Override
public  AbstractKeyedStateBackend createKeyedStateBackend(
-   Environment env,
-   JobID jobID,
-   String operatorIdentifier,
-   TypeSerializer keySerializer,
-   int numberOfKeyGroups,
-   KeyGroupRange keyGroupRange,
-   TaskKvStateRegistry kvStateRegistry,
-   TtlTimeProvider ttlTimeProvider) throws IOException {
+   Environment env,
+   JobID jobID,
+   String operatorIdentifier,
 
 Review comment:
   revert indentation change


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-15 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r225112700
 
 

 ##
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/state/memory/MemoryStateBackend.java
 ##
 @@ -302,14 +303,15 @@ public OperatorStateBackend createOperatorStateBackend(
 
@Override
public  AbstractKeyedStateBackend createKeyedStateBackend(
-   Environment env,
-   JobID jobID,
-   String operatorIdentifier,
-   TypeSerializer keySerializer,
-   int numberOfKeyGroups,
-   KeyGroupRange keyGroupRange,
-   TaskKvStateRegistry kvStateRegistry,
-   TtlTimeProvider ttlTimeProvider) {
+   Environment env,
 
 Review comment:
   please revert the indentation change


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-12 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r224761096
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,154 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+import org.apache.flink.util.FlinkRuntimeException;
+import org.apache.flink.util.IOUtils;
+import org.apache.flink.util.ResourceGuard;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * A monitor which pull {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+
+   private final CloseableRegistry registeredGauges;
+
+   private final RocksDB db;
+
+   private final ResourceGuard.Lease lease;
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final MetricGroup metricGroup;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB db,
+   @Nonnull ResourceGuard guard,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull MetricGroup metricGroup
+   ) throws IOException {
+   this.db = db;
+   this.lease = guard.acquireResource();
+   this.options = options;
+   this.metricGroup = metricGroup;
+
+   this.registeredGauges = new CloseableRegistry();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   try {
+   MetricGroup group = 
metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(
+   property,
+   handle,
+   db
+   );
+
+   group.gauge(property, gauge);
+   registeredGauges.registerCloseable(gauge);
+   }
+   } catch (IOException e) {
+   throw new FlinkRuntimeException("Unable to register 
native metrics with RocksDB", e);
+   }
+   }
+
+   @Override
+   public void close() {
+   IOUtils.closeQuietly(registeredGauges);
+   IOUtils.closeQuietly(lease);
+   }
+
+   static class RocksDBNativeMetricView implements Gauge, View, 
Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricView.class);
+
+   private final String property;
+
+   private final ColumnFamilyHandle handle;
+
+   private final RocksDB db;
+
+   private volatile boolean open;
+
+   private long value;
+
+   private RocksDBNativeMetricView(
+   @Nonnull String property,
+   @Nonnull ColumnFamilyHandle handle,
+   @Nonnull RocksDB db
+   ) {
+   this.property = property;
+   this.handle = handle;
+   this.db = db;
+

[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-12 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r224761096
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,154 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.metrics.View;
+import org.apache.flink.util.FlinkRuntimeException;
+import org.apache.flink.util.IOUtils;
+import org.apache.flink.util.ResourceGuard;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * A monitor which pull {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+
+   private final CloseableRegistry registeredGauges;
+
+   private final RocksDB db;
+
+   private final ResourceGuard.Lease lease;
+
+   private final RocksDBNativeMetricOptions options;
+
+   private final MetricGroup metricGroup;
+
+   RocksDBNativeMetricMonitor(
+   @Nonnull RocksDB db,
+   @Nonnull ResourceGuard guard,
+   @Nonnull RocksDBNativeMetricOptions options,
+   @Nonnull MetricGroup metricGroup
+   ) throws IOException {
+   this.db = db;
+   this.lease = guard.acquireResource();
+   this.options = options;
+   this.metricGroup = metricGroup;
+
+   this.registeredGauges = new CloseableRegistry();
+   }
+
+   /**
+* Register gauges to pull native metrics for the column family.
+* @param columnFamilyName group name for the new gauges
+* @param handle native handle to the column family
+*/
+   void registerColumnFamily(String columnFamilyName, ColumnFamilyHandle 
handle) {
+   try {
+   MetricGroup group = 
metricGroup.addGroup(columnFamilyName);
+
+   for (String property : options.getProperties()) {
+   RocksDBNativeMetricView gauge = new 
RocksDBNativeMetricView(
+   property,
+   handle,
+   db
+   );
+
+   group.gauge(property, gauge);
+   registeredGauges.registerCloseable(gauge);
+   }
+   } catch (IOException e) {
+   throw new FlinkRuntimeException("Unable to register 
native metrics with RocksDB", e);
+   }
+   }
+
+   @Override
+   public void close() {
+   IOUtils.closeQuietly(registeredGauges);
+   IOUtils.closeQuietly(lease);
+   }
+
+   static class RocksDBNativeMetricView implements Gauge, View, 
Closeable {
+   private static final Logger LOG = 
LoggerFactory.getLogger(RocksDBNativeMetricView.class);
+
+   private final String property;
+
+   private final ColumnFamilyHandle handle;
+
+   private final RocksDB db;
+
+   private volatile boolean open;
+
+   private long value;
+
+   private RocksDBNativeMetricView(
+   @Nonnull String property,
+   @Nonnull ColumnFamilyHandle handle,
+   @Nonnull RocksDB db
+   ) {
+   this.property = property;
+   this.handle = handle;
+   this.db = db;
+

[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-09 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r223873328
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricMonitor.java
 ##
 @@ -0,0 +1,195 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.util.IOUtils;
+import org.apache.flink.util.ResourceGuard;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+/**
+ * A monitor which pull {{@link RocksDB}} native metrics
+ * and forwards them to Flink's metric group. All metrics are
+ * unsigned longs and are reported at the column family level.
+ */
+@Internal
+public class RocksDBNativeMetricMonitor implements Closeable {
+
+   private final TimerTask task;
+
+   private final Timer timer;
+
+   private final ConcurrentLinkedQueue> 
messageQueue;
+
+   RocksDBNativeMetricMonitor(
 
 Review comment:
   metric polling can be implemented using a `View`, which is periodically 
called to update the current value. This should simplify this class quite a bit.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-09 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r223872847
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBStateBackendTest.java
 ##
 @@ -250,7 +251,10 @@ public void testCorrectMergeOperatorSet() throws 
IOException {
enableIncrementalCheckpointing,
TestLocalRecoveryConfig.disabled(),
RocksDBStateBackend.PriorityQueueStateType.HEAP,
-   TtlTimeProvider.DEFAULT);
+   TtlTimeProvider.DEFAULT,
+   new RocksDBNativeMetricOptions(),
+   Optional.empty()
 
 Review comment:
   If this argument an optional purely to simplify tests? If so, please just 
use an `UnregisteredOperatorMetricGroup` instead.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-09 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r223873851
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricOptions.java
 ##
 @@ -0,0 +1,278 @@
+/*
+ * 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.contrib.streaming.state;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Enable which RocksDB metrics to forward to Flink's metrics reporter.
+ * All metrics report at the column family level and return unsigned long 
values.
+ *
+ * Properties and doc comments are taken from RocksDB documentation. See
+ * https://github.com/facebook/rocksdb/blob/64324e329eb0a9b4e77241a425a1615ff524c7f1/include/rocksdb/db.h#L429";>
+ * db.h for more information.
+ */
+public class RocksDBNativeMetricOptions {
+
+   private static final long TEN_SECONDS = 10 * 1000;
+
+   private Set properties;
+
+   private long frequency = TEN_SECONDS;
+
+   public RocksDBNativeMetricOptions() {
+   this.properties = new HashSet<>();
+   }
+
+   /**
+* Returns number of immutable memtables that have not yet been flushed.
+*/
+   public void enableNumImmutableMemTable() {
+   this.properties.add("rocksdb.num-immutable-mem-table");
 
 Review comment:
   all of this must be configurable via the `flink-conf.yaml` configuration.


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


With regards,
Apache Git Services


[GitHub] zentol commented on a change in pull request #6814: [FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor

2018-10-09 Thread GitBox
zentol commented on a change in pull request #6814: 
[FLINK-10423][rocksdb][metrics] rocksdb native metrics monitor
URL: https://github.com/apache/flink/pull/6814#discussion_r223872647
 
 

 ##
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/test/java/org/apache/flink/contrib/streaming/state/RocksDBNativeMetricTest.java
 ##
 @@ -0,0 +1,160 @@
+/*
+ * 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.contrib.streaming.state;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.util.ResourceGuard;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests that native metrics can be pulled from RocksDB.
+ */
+public class RocksDBNativeMetricTest {
+
+   private static final String COLUMN_FAMILY_NAME = "test-cf";
+
+   private static final String PROPERTY = "property";
+
+   @Rule public RocksDBResource rocksDBResource = new RocksDBResource();
+
+   @Test
+   public void testMessageCollector() throws IOException {
+   ConcurrentLinkedQueue> 
messageQueue = new ConcurrentLinkedQueue<>();
+
+   RocksDBNativeMetricMonitor.RocksDBNativeMetricTask task = new 
RocksDBNativeMetricMonitor.RocksDBNativeMetricTask(
+   mock(RocksDB.class),
+   new ResourceGuard(),
+   new ArrayList<>(),
+   mock(MetricGroup.class),
+   messageQueue
+   );
+
+   messageQueue.add(Tuple2.of("", mock(ColumnFamilyHandle.class)));
+   task.collectNewMessages();
+
+   Assert.assertEquals("failed to removed pending messages from 
queue", 0, messageQueue.size());
+   Assert.assertEquals("failed to add message to kv state", 1, 
task.numberOfColumnFamilies());
+   }
+
+   @Test
+   public void testGaugeRegistration() throws IOException {
+   MetricGroup metricGroup = mock(MetricGroup.class);
+   MetricGroup innerGroup = mock(MetricGroup.class);
 
 Review comment:
   there are various utility classes to not require mocking for metric group 
related stuff. `UnregisteredMetricGroups` can be used to create safe default 
implementations where specific methods can be overridden.
   You can pass a custom MetricRegistry to check for specific metrics instead.


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


With regards,
Apache Git Services