From 769b22217365f7eca308c433068565d556794a3c Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Wed, 24 Jun 2015 16:48:56 +0300 Subject: [PATCH 1/2] Adding the CacheServiceMBean and CacheService This adds the CacheServiceMBean and its implementation the CacheService class. The implementation is based on calls to the API that define in cache_service.json Signed-off-by: Amnon Heiman --- .../cassandra/service/CacheService.java | 324 ++++++++++++++++++ .../cassandra/service/CacheServiceMBean.java | 154 +++++++++ 2 files changed, 478 insertions(+) create mode 100644 src/main/java/org/apache/cassandra/service/CacheService.java create mode 100644 src/main/java/org/apache/cassandra/service/CacheServiceMBean.java diff --git a/src/main/java/org/apache/cassandra/service/CacheService.java b/src/main/java/org/apache/cassandra/service/CacheService.java new file mode 100644 index 0000000..08a07ab --- /dev/null +++ b/src/main/java/org/apache/cassandra/service/CacheService.java @@ -0,0 +1,324 @@ +/* + * 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. + */ + +/* + * Copyright 2015 Cloudius Systems + * + * Modified by Cloudius Systems + */ + +package org.apache.cassandra.service; + +import java.lang.management.ManagementFactory; +import java.util.concurrent.ExecutionException; +import javax.management.MBeanServer; +import javax.management.ObjectName; +import javax.ws.rs.core.MultivaluedMap; + +import com.cloudius.urchin.api.APIClient; +import com.sun.jersey.core.util.MultivaluedMapImpl; + +public class CacheService implements CacheServiceMBean { + private static final java.util.logging.Logger logger = java.util.logging.Logger + .getLogger(CacheService.class.getName()); + private APIClient c = new APIClient(); + + public void log(String str) { + logger.info(str); + } + + public static final String MBEAN_NAME = "org.apache.cassandra.db:type=Caches"; + + public final static CacheService instance = new CacheService(); + + public static CacheService getInstance() { + return instance; + } + + private CacheService() { + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + + try { + mbs.registerMBean(this, new ObjectName(MBEAN_NAME)); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + public int getRowCacheSavePeriodInSeconds() { + log(" getRowCacheSavePeriodInSeconds()"); + return c.getIntValue("cache_service/row_cache_save_period"); + } + + public void setRowCacheSavePeriodInSeconds(int rcspis) { + log(" setRowCacheSavePeriodInSeconds(int rcspis)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("period", Integer.toString(rcspis)); + c.post("cache_service/row_cache_save_period", queryParams); + } + + public int getKeyCacheSavePeriodInSeconds() { + log(" getKeyCacheSavePeriodInSeconds()"); + return c.getIntValue("cache_service/key_cache_save_period"); + } + + public void setKeyCacheSavePeriodInSeconds(int kcspis) { + log(" setKeyCacheSavePeriodInSeconds(int kcspis)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("period", Integer.toString(kcspis)); + c.post("cache_service/key_cache_save_period", queryParams); + } + + public int getCounterCacheSavePeriodInSeconds() { + log(" getCounterCacheSavePeriodInSeconds()"); + return c.getIntValue("cache_service/counter_cache_save_period"); + } + + public void setCounterCacheSavePeriodInSeconds(int ccspis) { + log(" setCounterCacheSavePeriodInSeconds(int ccspis)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("ccspis", Integer.toString(ccspis)); + c.post("cache_service/counter_cache_save_period", queryParams); + } + + public int getRowCacheKeysToSave() { + log(" getRowCacheKeysToSave()"); + return c.getIntValue("cache_service/row_cache_keys_to_save"); + } + + public void setRowCacheKeysToSave(int rckts) { + log(" setRowCacheKeysToSave(int rckts)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("rckts", Integer.toString(rckts)); + c.post("cache_service/row_cache_keys_to_save", queryParams); + } + + public int getKeyCacheKeysToSave() { + log(" getKeyCacheKeysToSave()"); + return c.getIntValue("cache_service/key_cache_keys_to_save"); + } + + public void setKeyCacheKeysToSave(int kckts) { + log(" setKeyCacheKeysToSave(int kckts)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("kckts", Integer.toString(kckts)); + c.post("cache_service/key_cache_keys_to_save", queryParams); + } + + public int getCounterCacheKeysToSave() { + log(" getCounterCacheKeysToSave()"); + return c.getIntValue("cache_service/counter_cache_keys_to_save"); + } + + public void setCounterCacheKeysToSave(int cckts) { + log(" setCounterCacheKeysToSave(int cckts)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("cckts", Integer.toString(cckts)); + c.post("cache_service/counter_cache_keys_to_save", queryParams); + } + + /** + * invalidate the key cache; for use after invalidating row cache + */ + public void invalidateKeyCache() { + log(" invalidateKeyCache()"); + c.post("cache_service/invalidate_key_cache"); + } + + /** + * invalidate the row cache; for use after bulk loading via BinaryMemtable + */ + public void invalidateRowCache() { + log(" invalidateRowCache()"); + c.post("cache_service/invalidate_row_cache"); + } + + public void invalidateCounterCache() { + log(" invalidateCounterCache()"); + c.post("cache_service/invalidate_counter_cache"); + } + + public void setRowCacheCapacityInMB(long capacity) { + log(" setRowCacheCapacityInMB(long capacity)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("capacity", Long.toString(capacity)); + c.post("cache_service/row_cache_capacity", queryParams); + } + + public void setKeyCacheCapacityInMB(long capacity) { + log(" setKeyCacheCapacityInMB(long capacity)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("capacity", Long.toString(capacity)); + c.post("cache_service/key_cache_capacity", queryParams); + } + + public void setCounterCacheCapacityInMB(long capacity) { + log(" setCounterCacheCapacityInMB(long capacity)"); + MultivaluedMap queryParams = new MultivaluedMapImpl(); + queryParams.add("capacity", Long.toString(capacity)); + c.post("cache_service/counter_cache_capacity_in_mb", queryParams); + } + + /** + * save row and key caches + * + * @throws ExecutionException + * when attempting to retrieve the result of a task that aborted + * by throwing an exception + * @throws InterruptedException + * when a thread is waiting, sleeping, or otherwise occupied, + * and the thread is interrupted, either before or during the + * activity. + */ + public void saveCaches() throws ExecutionException, InterruptedException { + log(" saveCaches() throws ExecutionException, InterruptedException"); + c.post("cache_service/save_caches"); + } + + // + // remaining methods are provided for backwards compatibility; modern + // clients should use CacheMetrics instead + // + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#hits + */ + @Deprecated + public long getKeyCacheHits() { + log(" getKeyCacheHits()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#hits + */ + @Deprecated + public long getRowCacheHits() { + log(" getRowCacheHits()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#requests + */ + @Deprecated + public long getKeyCacheRequests() { + log(" getKeyCacheRequests()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#requests + */ + @Deprecated + public long getRowCacheRequests() { + log(" getRowCacheRequests()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#hitRate + */ + @Deprecated + public double getKeyCacheRecentHitRate() { + log(" getKeyCacheRecentHitRate()"); + return c.getDoubleValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#hitRate + */ + @Deprecated + public double getRowCacheRecentHitRate() { + log(" getRowCacheRecentHitRate()"); + return c.getDoubleValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#capacity + */ + @Deprecated + public long getRowCacheCapacityInMB() { + log(" getRowCacheCapacityInMB()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#capacity + */ + @Deprecated + public long getRowCacheCapacityInBytes() { + log(" getRowCacheCapacityInBytes()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#capacity + */ + @Deprecated + public long getKeyCacheCapacityInMB() { + log(" getKeyCacheCapacityInMB()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#capacity + */ + @Deprecated + public long getKeyCacheCapacityInBytes() { + log(" getKeyCacheCapacityInBytes()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#size + */ + @Deprecated + public long getRowCacheSize() { + log(" getRowCacheSize()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#entries + */ + @Deprecated + public long getRowCacheEntries() { + log(" getRowCacheEntries()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#size + */ + @Deprecated + public long getKeyCacheSize() { + log(" getKeyCacheSize()"); + return c.getLongValue(""); + } + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#entries + */ + @Deprecated + public long getKeyCacheEntries() { + log(" getKeyCacheEntries()"); + return c.getLongValue(""); + } +} diff --git a/src/main/java/org/apache/cassandra/service/CacheServiceMBean.java b/src/main/java/org/apache/cassandra/service/CacheServiceMBean.java new file mode 100644 index 0000000..44a6119 --- /dev/null +++ b/src/main/java/org/apache/cassandra/service/CacheServiceMBean.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.cassandra.service; + +import java.util.concurrent.ExecutionException; + +public interface CacheServiceMBean +{ + public int getRowCacheSavePeriodInSeconds(); + public void setRowCacheSavePeriodInSeconds(int rcspis); + + public int getKeyCacheSavePeriodInSeconds(); + public void setKeyCacheSavePeriodInSeconds(int kcspis); + + public int getCounterCacheSavePeriodInSeconds(); + public void setCounterCacheSavePeriodInSeconds(int ccspis); + + public int getRowCacheKeysToSave(); + + public void setRowCacheKeysToSave(int rckts); + + public int getKeyCacheKeysToSave(); + public void setKeyCacheKeysToSave(int kckts); + + public int getCounterCacheKeysToSave(); + public void setCounterCacheKeysToSave(int cckts); + + /** + * invalidate the key cache; for use after invalidating row cache + */ + public void invalidateKeyCache(); + + /** + * invalidate the row cache; for use after bulk loading via BinaryMemtable + */ + public void invalidateRowCache(); + + public void invalidateCounterCache(); + + public void setRowCacheCapacityInMB(long capacity); + + public void setKeyCacheCapacityInMB(long capacity); + + public void setCounterCacheCapacityInMB(long capacity); + + /** + * save row and key caches + * + * @throws ExecutionException when attempting to retrieve the result of a task that aborted by throwing an exception + * @throws InterruptedException when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. + */ + public void saveCaches() throws ExecutionException, InterruptedException; + + // + // remaining methods are provided for backwards compatibility; modern clients should use CacheMetrics instead + // + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#hits + */ + @Deprecated + public long getKeyCacheHits(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#hits + */ + @Deprecated + public long getRowCacheHits(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#requests + */ + @Deprecated + public long getKeyCacheRequests(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#requests + */ + @Deprecated + public long getRowCacheRequests(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#hitRate + */ + @Deprecated + public double getKeyCacheRecentHitRate(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#hitRate + */ + @Deprecated + public double getRowCacheRecentHitRate(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#capacity + */ + @Deprecated + public long getRowCacheCapacityInMB(); + /** + * @see org.apache.cassandra.metrics.CacheMetrics#capacity + */ + @Deprecated + public long getRowCacheCapacityInBytes(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#capacity + */ + @Deprecated + public long getKeyCacheCapacityInMB(); + /** + * @see org.apache.cassandra.metrics.CacheMetrics#capacity + */ + @Deprecated + public long getKeyCacheCapacityInBytes(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#size + */ + @Deprecated + public long getRowCacheSize(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#entries + */ + @Deprecated + public long getRowCacheEntries(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#size + */ + @Deprecated + public long getKeyCacheSize(); + + /** + * @see org.apache.cassandra.metrics.CacheMetrics#entries + */ + @Deprecated + public long getKeyCacheEntries(); +} From a7be16ed9957475902057017056f408124e5a951 Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Wed, 24 Jun 2015 16:50:26 +0300 Subject: [PATCH 2/2] Initilizing the CacheService from Main This initilize the CacheService MBean in main Signed-off-by: Amnon Heiman --- src/main/java/com/cloudius/urchin/main/Main.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/cloudius/urchin/main/Main.java b/src/main/java/com/cloudius/urchin/main/Main.java index 0634b70..fada4fd 100644 --- a/src/main/java/com/cloudius/urchin/main/Main.java +++ b/src/main/java/com/cloudius/urchin/main/Main.java @@ -11,6 +11,7 @@ import org.apache.cassandra.gms.Gossiper; import org.apache.cassandra.gms.FailureDetector; import org.apache.cassandra.locator.EndpointSnitchInfo; import org.apache.cassandra.net.MessagingService; +import org.apache.cassandra.service.CacheService; import org.apache.cassandra.service.StorageProxy; import org.apache.cassandra.service.StorageService; @@ -27,6 +28,7 @@ public class Main { EndpointSnitchInfo.getInstance(); FailureDetector.getInstance(); ColumnFamilyStore.register_mbeans(); + CacheService.getInstance(); Thread.sleep(Long.MAX_VALUE); }