This is an automated email from the ASF dual-hosted git repository. yanxinyi pushed a commit to branch 4.x in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/4.x by this push: new e3b1568 PHOENIX-6379 Implement a new Metric Type which will be used for Table… (#1139) e3b1568 is described below commit e3b1568dc0693517892b99f4f11ce2ed703628f3 Author: vikas meka <vm...@salesforce.com> AuthorDate: Thu Feb 11 18:27:24 2021 -0800 PHOENIX-6379 Implement a new Metric Type which will be used for Table… (#1139) PHOENIX-6379 Implement a new Metric Type which will be used for TableMetrics --- .../phoenix/monitoring/PhoenixTableMetric.java | 37 ++++++++ .../phoenix/monitoring/PhoenixTableMetricImpl.java | 77 +++++++++++++++++ .../monitoring/PhoenixTableMetricImplTest.java | 98 ++++++++++++++++++++++ 3 files changed, 212 insertions(+) diff --git a/phoenix-core/src/main/java/org/apache/phoenix/monitoring/PhoenixTableMetric.java b/phoenix-core/src/main/java/org/apache/phoenix/monitoring/PhoenixTableMetric.java new file mode 100644 index 0000000..aa8d31d --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/monitoring/PhoenixTableMetric.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.phoenix.monitoring; + +/** + * Class that exposes the various phoenix metrics collected + * at the Table level. Because metrics are dynamic in nature, it is not guaranteed that the + * state exposed will always be in sync with each other. One should use + * these metrics primarily for monitoring and debugging purposes. + */ +public interface PhoenixTableMetric extends Metric { + + /** + * @return Number of samples collected since the last {@link #reset()} call. + */ + public long getNumberOfSamples(); + + /** + * @return Sum of the values of the metric sampled since the last {@link #reset()} call. + */ + public long getTotalSum(); +} diff --git a/phoenix-core/src/main/java/org/apache/phoenix/monitoring/PhoenixTableMetricImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/monitoring/PhoenixTableMetricImpl.java new file mode 100644 index 0000000..2fbf6a7 --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/monitoring/PhoenixTableMetricImpl.java @@ -0,0 +1,77 @@ +/* + * 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.phoenix.monitoring; + +import java.util.concurrent.atomic.AtomicLong; + +public class PhoenixTableMetricImpl implements PhoenixTableMetric { + + private AtomicLong numberOfSamples = new AtomicLong(0); + private Metric metric; + + /** + * Default implementation used when TableLevel Metrics are enabled + */ + public PhoenixTableMetricImpl(MetricType type) { + this.metric = new AtomicMetric(type); + } + + /** + * Reset the internal state. Typically called after metric information has been collected and a new phase of + * collection is being requested for the next interval. + */ + @Override public void reset() { + metric.reset(); + numberOfSamples.set(0); + } + + @Override public long getNumberOfSamples() { + return numberOfSamples.get(); + } + + @Override public long getTotalSum() { + return metric.getValue(); + } + + @Override public void change(long delta) { + metric.change(delta); + numberOfSamples.incrementAndGet(); + } + + @Override public void increment() { + metric.increment(); + numberOfSamples.incrementAndGet(); + } + + @Override public MetricType getMetricType() { + return metric.getMetricType(); + } + + @Override public long getValue() { + return metric.getValue(); + } + + @Override public String getCurrentMetricState() { + return metric.getCurrentMetricState() + ", Number of samples: " + numberOfSamples.get(); + } + + @Override public void decrement() { + metric.decrement(); + numberOfSamples.incrementAndGet(); + } +} diff --git a/phoenix-core/src/test/java/org/apache/phoenix/monitoring/PhoenixTableMetricImplTest.java b/phoenix-core/src/test/java/org/apache/phoenix/monitoring/PhoenixTableMetricImplTest.java new file mode 100644 index 0000000..87eac17 --- /dev/null +++ b/phoenix-core/src/test/java/org/apache/phoenix/monitoring/PhoenixTableMetricImplTest.java @@ -0,0 +1,98 @@ +/* + * 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.phoenix.monitoring; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PhoenixTableMetricImplTest { + + /* + Tests the functionality of the TableMetricImpl methods which are exposed + 1. change() + 2. increment() + 3. decrement() + 4. reset() + 5. getValue() + */ + @Test public void testTableMetricImplAvailableMethods() { + + PhoenixTableMetric metric = new PhoenixTableMetricImpl(MetricType.SELECT_SQL_COUNTER); + + for (int i = 0; i < 10; i++) { + metric.increment(); + } + assertEquals(10, metric.getValue()); + metric.reset(); + assertEquals(0, metric.getValue()); + + for (int i = 0; i < 5; i++) { + metric.change(i); + } + assertEquals(10, metric.getValue()); + metric.reset(); + assertEquals(0, metric.getValue()); + + metric.change(10); + assertEquals(10, metric.getValue()); + for (int i = 0; i < 5; i++) { + metric.decrement(); + } + assertEquals(5, metric.getValue()); + } + + @Test public void testPhoenixImplchange() { + + PhoenixTableMetric metric = new PhoenixTableMetricImpl(MetricType.SELECT_SQL_COUNTER); + for (int i = 0; i < 5; i++) { + metric.change(i); + } + assertEquals(10, metric.getValue()); + } + + @Test public void testPhoenixImplIncrement() { + PhoenixTableMetric metric = new PhoenixTableMetricImpl(MetricType.SELECT_SQL_COUNTER); + for (int i = 0; i < 10; i++) { + metric.increment(); + } + assertEquals(10, metric.getValue()); + } + + @Test public void testPhoenixImplDecrement() { + PhoenixTableMetric metric = new PhoenixTableMetricImpl(MetricType.SELECT_SQL_COUNTER); + metric.change(10); + for (int i = 0; i < 5; i++) { + metric.decrement(); + } + assertEquals(5, metric.getValue()); + } + + @Test public void testPhoenixImplReset() { + PhoenixTableMetric metric = new PhoenixTableMetricImpl(MetricType.SELECT_SQL_COUNTER); + metric.change(10); + metric.reset(); + assertEquals(0, metric.getValue()); + } + + @Test public void testPhoenixImplGetValue() { + PhoenixTableMetric metric = new PhoenixTableMetricImpl(MetricType.SELECT_SQL_COUNTER); + metric.change(10); + assertEquals(10, metric.getValue()); + } +}