From f1aa49b9f80f532307195932e6bf34f4705e6ccb Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 12 Jun 2014 18:42:21 -0500 Subject: [PATCH 1/3] Track metrics at a keyspace level Patch by brandonwilliams, reviewed by yukim for CASSANDRA-6539 --- CHANGES.txt | 1 + src/java/org/apache/cassandra/db/Keyspace.java | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index a2a978e2f4..db94066cee 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -17,6 +17,7 @@ * Reference sstables before populating key cache (CASSANDRA-7234) Merged from 1.2: 1.2.17 + * Track metrics at a keyspace level (CASSANDRA-6539) * Add replace_address_first_boot flag to only replace if not bootstrapped (CASSANDRA-7356) * Enable keepalive for native protocol (CASSANDRA-7380) * Check internal addresses for seeds (CASSANDRA-6523) diff --git a/src/java/org/apache/cassandra/db/Keyspace.java b/src/java/org/apache/cassandra/db/Keyspace.java index 714956a9ea..965179404a 100644 --- a/src/java/org/apache/cassandra/db/Keyspace.java +++ b/src/java/org/apache/cassandra/db/Keyspace.java @@ -43,6 +43,7 @@ import org.apache.cassandra.locator.AbstractReplicationStrategy; import org.apache.cassandra.service.StorageService; import org.apache.cassandra.service.pager.QueryPagers; import org.apache.cassandra.tracing.Tracing; +import org.apache.cassandra.metrics.KeyspaceMetrics; /** * It represents a Keyspace. @@ -76,6 +77,7 @@ public class Keyspace private final ConcurrentMap columnFamilyStores = new ConcurrentHashMap(); private volatile AbstractReplicationStrategy replicationStrategy; public static final Function keyspaceTransformer = new Function() + public final KeyspaceMetrics metric; { public Keyspace apply(String keyspaceName) { @@ -133,6 +135,7 @@ public class Keyspace { for (ColumnFamilyStore cfs : t.getColumnFamilyStores()) t.unloadCf(cfs); + t.metric.release(); } return t; } @@ -265,6 +268,7 @@ public class Keyspace logger.debug("Initializing {}.{}", getName(), cfm.cfName); initCf(cfm.cfId, cfm.cfName, loadSSTables); } + this.metric = new KeyspaceMetrics(this); } public void createReplicationStrategy(KSMetaData ksm) From cecd1da318f19139446db526be035fb25e1c5b12 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 12 Jun 2014 18:42:21 -0500 Subject: [PATCH 2/3] Track metrics at a keyspace level Patch by brandonwilliams, reviewed by yukim for CASSANDRA-6539 --- .../org/apache/cassandra/db/Keyspace.java | 2 +- .../cassandra/metrics/KeyspaceMetrics.java | 202 ++++++++++++++++++ 2 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 src/java/org/apache/cassandra/metrics/KeyspaceMetrics.java diff --git a/src/java/org/apache/cassandra/db/Keyspace.java b/src/java/org/apache/cassandra/db/Keyspace.java index 965179404a..06df066ed5 100644 --- a/src/java/org/apache/cassandra/db/Keyspace.java +++ b/src/java/org/apache/cassandra/db/Keyspace.java @@ -76,8 +76,8 @@ public class Keyspace /* ColumnFamilyStore per column family */ private final ConcurrentMap columnFamilyStores = new ConcurrentHashMap(); private volatile AbstractReplicationStrategy replicationStrategy; - public static final Function keyspaceTransformer = new Function() public final KeyspaceMetrics metric; + public static final Function keyspaceTransformer = new Function() { public Keyspace apply(String keyspaceName) { diff --git a/src/java/org/apache/cassandra/metrics/KeyspaceMetrics.java b/src/java/org/apache/cassandra/metrics/KeyspaceMetrics.java new file mode 100644 index 0000000000..4a0980f384 --- /dev/null +++ b/src/java/org/apache/cassandra/metrics/KeyspaceMetrics.java @@ -0,0 +1,202 @@ +/* + * 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.cassandra.metrics; + +import java.util.ArrayList; +import java.util.List; + +import com.yammer.metrics.Metrics; +import com.yammer.metrics.core.*; +import com.yammer.metrics.stats.Snapshot; + +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.db.Keyspace; + +/** + * Metrics for {@link ColumnFamilyStore}. + */ +public class KeyspaceMetrics +{ + /** Total amount of data stored in the memtable, including column related overhead. */ + public final Gauge memtableDataSize; + /** Total amount of data stored in the memtables (2i and pending flush memtables included). */ + public final Gauge allMemtablesDataSize; + /** Total number of columns present in the memtable. */ + public final Gauge memtableColumnsCount; + /** Number of times flush has resulted in the memtable being switched out. */ + public final Gauge memtableSwitchCount; + /** Estimated number of tasks pending for this column family */ + public final Gauge pendingTasks; + /** Estimate of number of pending compactios for this CF */ + public final Gauge pendingCompactions; + /** Disk space used by SSTables belonging to this CF */ + public final Gauge liveDiskSpaceUsed; + /** Total disk space used by SSTables belonging to this CF, including obsolete ones waiting to be GC'd */ + public final Gauge totalDiskSpaceUsed; + /** Disk space used by bloom filter */ + public final Gauge bloomFilterDiskSpaceUsed; + + private final MetricNameFactory factory; + + /** + * Creates metrics for given {@link ColumnFamilyStore}. + * + * @param ks Keyspace to measure metrics + */ + public KeyspaceMetrics(final Keyspace ks) + { + factory = new KeyspaceMetricNameFactory(ks); + + memtableColumnsCount = Metrics.newGauge(factory.createMetricName("MemtableColumnsCount"), new Gauge() + { + public Long value() + { + long total = 0; + for (ColumnFamilyStore cf : ks.getColumnFamilyStores()) + { + total += cf.metric.memtableColumnsCount.value(); + } + return total; + } + }); + memtableDataSize = Metrics.newGauge(factory.createMetricName("MemtableDataSize"), new Gauge() + { + public Long value() + { + long total = 0; + for (ColumnFamilyStore cf : ks.getColumnFamilyStores()) + { + total += cf.metric.memtableDataSize.value(); + } + return total; + } + }); + allMemtablesDataSize = Metrics.newGauge(factory.createMetricName("AllMemtablesDataSize"), new Gauge() + { + public Long value() + { + long total = 0; + for (ColumnFamilyStore cf : ks.getColumnFamilyStores()) + { + total += cf.metric.allMemtablesDataSize.value(); + } + return total; + } + }); + memtableSwitchCount = Metrics.newGauge(factory.createMetricName("MemtableSwitchCount"), new Gauge() + { + public Long value() + { + long sum = 0; + for (ColumnFamilyStore cf : ks.getColumnFamilyStores()) + sum += cf.metric.memtableSwitchCount.count(); + return sum; + } + }); + pendingCompactions = Metrics.newGauge(factory.createMetricName("PendingCompactions"), new Gauge() + { + public Integer value() + { + int sum = 0; + for (ColumnFamilyStore cf : ks.getColumnFamilyStores()) + { + sum += cf.metric.pendingCompactions.value(); + } + return sum; + } + }); + pendingTasks = Metrics.newGauge(factory.createMetricName("PendingTasks"), new Gauge() + { + public Integer value() + { + return Keyspace.switchLock.getQueueLength(); + } + }); + liveDiskSpaceUsed = Metrics.newGauge(factory.createMetricName("LiveDiskSpaceUsed"), new Gauge() + { + public Long value() + { + long sum = 0; + for (ColumnFamilyStore cf : ks.getColumnFamilyStores()) + { + sum += cf.metric.liveDiskSpaceUsed.count(); + } + return sum; + } + }); + totalDiskSpaceUsed = Metrics.newGauge(factory.createMetricName("TotalDiskSpaceUsed"), new Gauge() + { + public Long value() + { + long sum = 0; + for (ColumnFamilyStore cf : ks.getColumnFamilyStores()) + { + sum += cf.metric.totalDiskSpaceUsed.count(); + } + return sum; + } + }); + bloomFilterDiskSpaceUsed = Metrics.newGauge(factory.createMetricName("BloomFilterDiskSpaceUsed"), new Gauge() + { + public Long value() + { + long total = 0; + for (ColumnFamilyStore cf : ks.getColumnFamilyStores()) + total += cf.metric.bloomFilterDiskSpaceUsed.value(); + return total; + } + }); + } + + /** + * Release all associated metrics. + */ + public void release() + { + Metrics.defaultRegistry().removeMetric(factory.createMetricName("AllMemtablesDataSize")); + Metrics.defaultRegistry().removeMetric(factory.createMetricName("MemtableDataSize")); + Metrics.defaultRegistry().removeMetric(factory.createMetricName("MemtableSwitchCount")); + Metrics.defaultRegistry().removeMetric(factory.createMetricName("PendingTasks")); + Metrics.defaultRegistry().removeMetric(factory.createMetricName("LiveDiskSpaceUsed")); + Metrics.defaultRegistry().removeMetric(factory.createMetricName("TotalDiskSpaceUsed")); + Metrics.defaultRegistry().removeMetric(factory.createMetricName("BloomFilterDiskSpaceUsed")); + } + + class KeyspaceMetricNameFactory implements MetricNameFactory + { + private final String keyspaceName; + + KeyspaceMetricNameFactory(Keyspace ks) + { + this.keyspaceName = ks.getName(); + } + + public MetricName createMetricName(String metricName) + { + String groupName = ColumnFamilyMetrics.class.getPackage().getName(); + + StringBuilder mbeanName = new StringBuilder(); + mbeanName.append(groupName).append(":"); + mbeanName.append("type=Keyspace"); + mbeanName.append(",keyspace=").append(keyspaceName); + mbeanName.append(",name=").append(metricName); + + return new MetricName(groupName, "keyspace", metricName, keyspaceName, mbeanName.toString()); + } + } +} From be79ba500e6e63ade6e337374f935e69a691086e Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Thu, 12 Jun 2014 18:49:09 -0500 Subject: [PATCH 3/3] Track metrics at a keyspace level Patch by brandonwilliams, reviewed by yukim for CASSANDRA-6539 --- CHANGES.txt | 1 - src/java/org/apache/cassandra/db/Keyspace.java | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index db94066cee..e3bd64b06a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -16,7 +16,6 @@ * Fix infinite loop on exception while streaming (CASSANDRA-7330) * Reference sstables before populating key cache (CASSANDRA-7234) Merged from 1.2: -1.2.17 * Track metrics at a keyspace level (CASSANDRA-6539) * Add replace_address_first_boot flag to only replace if not bootstrapped (CASSANDRA-7356) * Enable keepalive for native protocol (CASSANDRA-7380) diff --git a/src/java/org/apache/cassandra/db/Keyspace.java b/src/java/org/apache/cassandra/db/Keyspace.java index 06df066ed5..308d8ef9de 100644 --- a/src/java/org/apache/cassandra/db/Keyspace.java +++ b/src/java/org/apache/cassandra/db/Keyspace.java @@ -27,6 +27,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import com.google.common.base.Function; import com.google.common.collect.Iterables; +import org.apache.cassandra.metrics.KeyspaceMetrics; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,6 +64,8 @@ public class Keyspace */ public static final ReentrantReadWriteLock switchLock = new ReentrantReadWriteLock(); + public final KeyspaceMetrics metric; + // It is possible to call Keyspace.open without a running daemon, so it makes sense to ensure // proper directories here as well as in CassandraDaemon. static @@ -262,6 +265,7 @@ public class Keyspace metadata = Schema.instance.getKSMetaData(keyspaceName); assert metadata != null : "Unknown keyspace " + keyspaceName; createReplicationStrategy(metadata); + metric = new KeyspaceMetrics(this); for (CFMetaData cfm : new ArrayList(metadata.cfMetaData().values())) {