From 8bbe80df99d22107a1e4560e32f7086e80a3f617 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Thu, 8 Dec 2016 23:25:10 +0200 Subject: [PATCH] Cache: Use MXS_EXCEPTION_GUARD and maxscale::SpinLockGuard --- server/modules/filter/cache/cachefilter.cc | 19 ++++++------ server/modules/filter/cache/cachefilter.h | 30 ------------------- server/modules/filter/cache/cachemt.cc | 17 ++++++----- server/modules/filter/cache/cachemt.h | 2 +- server/modules/filter/cache/cachept.cc | 3 +- server/modules/filter/cache/cachest.cc | 11 +++---- server/modules/filter/cache/lrustoragemt.cc | 12 ++++---- server/modules/filter/cache/lrustoragemt.h | 2 +- server/modules/filter/cache/lrustoragest.cc | 3 +- .../storage_inmemory/inmemorystoragemt.cc | 10 ++++--- .../storage_inmemory/inmemorystoragemt.h | 2 +- server/modules/filter/cache/storagefactory.cc | 4 +-- 12 files changed, 47 insertions(+), 68 deletions(-) diff --git a/server/modules/filter/cache/cachefilter.cc b/server/modules/filter/cache/cachefilter.cc index fc89ffa2f..5f8be48bb 100644 --- a/server/modules/filter/cache/cachefilter.cc +++ b/server/modules/filter/cache/cachefilter.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include "cachemt.h" #include "cachept.h" #include "cachefiltersession.hh" @@ -191,12 +192,12 @@ static FILTER *createInstance(const char* zName, char** pzOptions, FILTER_PARAME { case CACHE_THREAD_MODEL_MT: MXS_NOTICE("Creating shared cache."); - CPP_GUARD(pFilter->pCache = CacheMT::Create(zName, &pFilter->config)); + MXS_EXCEPTION_GUARD(pFilter->pCache = CacheMT::Create(zName, &pFilter->config)); break; case CACHE_THREAD_MODEL_ST: MXS_NOTICE("Creating thread specific cache."); - CPP_GUARD(pFilter->pCache = CachePT::Create(zName, &pFilter->config)); + MXS_EXCEPTION_GUARD(pFilter->pCache = CachePT::Create(zName, &pFilter->config)); break; default: @@ -229,7 +230,7 @@ static void *newSession(FILTER* pInstance, SESSION* pSession) Cache* pCache = pFilter->pCache; CacheFilterSession* pCacheFilterSession = NULL; - CPP_GUARD(pCacheFilterSession = CacheFilterSession::Create(pCache, pSession)); + MXS_EXCEPTION_GUARD(pCacheFilterSession = CacheFilterSession::Create(pCache, pSession)); return pCacheFilterSession; } @@ -244,7 +245,7 @@ static void closeSession(FILTER* pInstance, void* pSessionData) { CacheFilterSession* pCacheFilterSession = static_cast(pSessionData); - CPP_GUARD(pCacheFilterSession->close()); + MXS_EXCEPTION_GUARD(pCacheFilterSession->close()); } /** @@ -271,7 +272,7 @@ static void setDownstream(FILTER* pInstance, void* pSessionData, DOWNSTREAM* pDo { CacheFilterSession* pCacheFilterSession = static_cast(pSessionData); - CPP_GUARD(pCacheFilterSession->setDownstream(pDownstream)); + MXS_EXCEPTION_GUARD(pCacheFilterSession->setDownstream(pDownstream)); } /** @@ -285,7 +286,7 @@ static void setUpstream(FILTER* pInstance, void* pSessionData, UPSTREAM* pUpstre { CacheFilterSession* pCacheFilterSession = static_cast(pSessionData); - CPP_GUARD(pCacheFilterSession->setUpstream(pUpstream)); + MXS_EXCEPTION_GUARD(pCacheFilterSession->setUpstream(pUpstream)); } /** @@ -300,7 +301,7 @@ static int routeQuery(FILTER* pInstance, void* pSessionData, GWBUF* pPacket) CacheFilterSession* pCacheFilterSession = static_cast(pSessionData); int rv = 0; - CPP_GUARD(rv = pCacheFilterSession->routeQuery(pPacket)); + MXS_EXCEPTION_GUARD(rv = pCacheFilterSession->routeQuery(pPacket)); return rv; } @@ -317,7 +318,7 @@ static int clientReply(FILTER* pInstance, void* pSessionData, GWBUF* pPacket) CacheFilterSession* pCacheFilterSession = static_cast(pSessionData); int rv = 0; - CPP_GUARD(rv = pCacheFilterSession->clientReply(pPacket)); + MXS_EXCEPTION_GUARD(rv = pCacheFilterSession->clientReply(pPacket)); return rv; } @@ -336,7 +337,7 @@ static void diagnostics(FILTER* pInstance, void* pSessionData, DCB* pDcb) { CacheFilterSession* pCacheFilterSession = static_cast(pSessionData); - CPP_GUARD(pCacheFilterSession->diagnostics(pDcb)); + MXS_EXCEPTION_GUARD(pCacheFilterSession->diagnostics(pDcb)); } /** diff --git a/server/modules/filter/cache/cachefilter.h b/server/modules/filter/cache/cachefilter.h index abccbc819..0bdc440fa 100644 --- a/server/modules/filter/cache/cachefilter.h +++ b/server/modules/filter/cache/cachefilter.h @@ -81,34 +81,4 @@ void cache_config_finish(CACHE_CONFIG& config); void cache_config_free(CACHE_CONFIG* pConfig); void cache_config_reset(CACHE_CONFIG& config); -/** - * LockGuard is a RAII class whose constructor acquires a spinlock and - * destructor releases the same spinlock. To be used for locking a spinlock - * in an exceptionsafe manner for the duration of a scope. - */ -class LockGuard -{ -public: - LockGuard(SPINLOCK* plock) - : lock_(*plock) - { - spinlock_acquire(&lock_); - } - ~LockGuard() - { - spinlock_release(&lock_); - } - -private: - LockGuard(const LockGuard&); - LockGuard& operator = (const LockGuard&); - - SPINLOCK& lock_; -}; - -#define CPP_GUARD(statement)\ - do { try { statement; } \ - catch (const std::exception& x) { MXS_ERROR("Caught standard exception: %s", x.what()); }\ - catch (...) { MXS_ERROR("Caught unknown exception."); } } while (false) - #endif diff --git a/server/modules/filter/cache/cachemt.cc b/server/modules/filter/cache/cachemt.cc index 918d312c8..23651356d 100644 --- a/server/modules/filter/cache/cachemt.cc +++ b/server/modules/filter/cache/cachemt.cc @@ -16,6 +16,7 @@ #include "storage.h" #include "storagefactory.h" +using maxscale::SpinLockGuard; using std::tr1::shared_ptr; CacheMT::CacheMT(const std::string& name, @@ -56,21 +57,21 @@ CacheMT* CacheMT::Create(const std::string& name, const CACHE_CONFIG* pConfig) json_t* CacheMT::get_info(uint32_t flags) const { - LockGuard guard(&m_lockPending); + SpinLockGuard guard(m_lockPending); return CacheSimple::do_get_info(flags); } bool CacheMT::must_refresh(const CACHE_KEY& key, const CacheFilterSession* pSession) { - LockGuard guard(&m_lockPending); + SpinLockGuard guard(m_lockPending); return do_must_refresh(key, pSession); } void CacheMT::refreshed(const CACHE_KEY& key, const CacheFilterSession* pSession) { - LockGuard guard(&m_lockPending); + SpinLockGuard guard(m_lockPending); do_refreshed(key, pSession); } @@ -96,11 +97,11 @@ CacheMT* CacheMT::Create(const std::string& name, if (pStorage) { - CPP_GUARD(pCache = new CacheMT(name, - pConfig, - sRules, - sFactory, - pStorage)); + MXS_EXCEPTION_GUARD(pCache = new CacheMT(name, + pConfig, + sRules, + sFactory, + pStorage)); if (!pCache) { diff --git a/server/modules/filter/cache/cachemt.h b/server/modules/filter/cache/cachemt.h index 5feb5a404..70ab558a0 100644 --- a/server/modules/filter/cache/cachemt.h +++ b/server/modules/filter/cache/cachemt.h @@ -13,7 +13,7 @@ */ #include -#include +#include #include "cachesimple.h" class CacheMT : public CacheSimple diff --git a/server/modules/filter/cache/cachept.cc b/server/modules/filter/cache/cachept.cc index ddd959285..c812be273 100644 --- a/server/modules/filter/cache/cachept.cc +++ b/server/modules/filter/cache/cachept.cc @@ -15,6 +15,7 @@ #include "cachept.h" #include #include +#include #include "cachest.h" #include "storagefactory.h" @@ -168,7 +169,7 @@ CachePT* CachePT::Create(const std::string& name, CacheST* pCacheST = 0; - CPP_GUARD(pCacheST = CacheST::Create(namest, sRules, sFactory, pConfig)); + MXS_EXCEPTION_GUARD(pCacheST = CacheST::Create(namest, sRules, sFactory, pConfig)); if (pCacheST) { diff --git a/server/modules/filter/cache/cachest.cc b/server/modules/filter/cache/cachest.cc index 5f4c5193b..9bc4d3e4b 100644 --- a/server/modules/filter/cache/cachest.cc +++ b/server/modules/filter/cache/cachest.cc @@ -13,6 +13,7 @@ #define MXS_MODULE_NAME "cache" #include "cachest.h" +#include #include "storage.h" #include "storagefactory.h" @@ -101,11 +102,11 @@ CacheST* CacheST::Create(const std::string& name, if (pStorage) { - CPP_GUARD(pCache = new CacheST(name, - pConfig, - sRules, - sFactory, - pStorage)); + MXS_EXCEPTION_GUARD(pCache = new CacheST(name, + pConfig, + sRules, + sFactory, + pStorage)); if (!pCache) { diff --git a/server/modules/filter/cache/lrustoragemt.cc b/server/modules/filter/cache/lrustoragemt.cc index 3ee20aa5b..c1233de1d 100644 --- a/server/modules/filter/cache/lrustoragemt.cc +++ b/server/modules/filter/cache/lrustoragemt.cc @@ -14,6 +14,8 @@ #define MXS_MODULE_NAME "cache" #include "lrustoragemt.h" +using maxscale::SpinLockGuard; + LRUStorageMT::LRUStorageMT(Storage* pstorage, size_t max_count, size_t max_size) : LRUStorage(pstorage, max_count, max_size) { @@ -30,7 +32,7 @@ LRUStorageMT* LRUStorageMT::create(Storage* pstorage, size_t max_count, size_t m { LRUStorageMT* plru_storage = NULL; - CPP_GUARD(plru_storage = new LRUStorageMT(pstorage, max_count, max_size)); + MXS_EXCEPTION_GUARD(plru_storage = new LRUStorageMT(pstorage, max_count, max_size)); return plru_storage; } @@ -38,7 +40,7 @@ LRUStorageMT* LRUStorageMT::create(Storage* pstorage, size_t max_count, size_t m cache_result_t LRUStorageMT::get_info(uint32_t what, json_t** ppInfo) const { - LockGuard guard(&lock_); + SpinLockGuard guard(lock_); return LRUStorage::do_get_info(what, ppInfo); } @@ -47,7 +49,7 @@ cache_result_t LRUStorageMT::get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppvalue) { - LockGuard guard(&lock_); + SpinLockGuard guard(lock_); return do_get_value(key, flags, ppvalue); } @@ -55,14 +57,14 @@ cache_result_t LRUStorageMT::get_value(const CACHE_KEY& key, cache_result_t LRUStorageMT::put_value(const CACHE_KEY& key, const GWBUF* pvalue) { - LockGuard guard(&lock_); + SpinLockGuard guard(lock_); return do_put_value(key, pvalue); } cache_result_t LRUStorageMT::del_value(const CACHE_KEY& key) { - LockGuard guard(&lock_); + SpinLockGuard guard(lock_); return do_del_value(key); } diff --git a/server/modules/filter/cache/lrustoragemt.h b/server/modules/filter/cache/lrustoragemt.h index 146ca6901..0ec16d255 100644 --- a/server/modules/filter/cache/lrustoragemt.h +++ b/server/modules/filter/cache/lrustoragemt.h @@ -13,7 +13,7 @@ */ #include -#include +#include #include "lrustorage.h" class LRUStorageMT : public LRUStorage diff --git a/server/modules/filter/cache/lrustoragest.cc b/server/modules/filter/cache/lrustoragest.cc index a53c55376..164676e72 100644 --- a/server/modules/filter/cache/lrustoragest.cc +++ b/server/modules/filter/cache/lrustoragest.cc @@ -13,6 +13,7 @@ #define MXS_MODULE_NAME "cache" #include "lrustoragest.h" +#include LRUStorageST::LRUStorageST(Storage* pstorage, size_t max_count, size_t max_size) : LRUStorage(pstorage, max_count, max_size) @@ -28,7 +29,7 @@ LRUStorageST* LRUStorageST::create(Storage* pstorage, size_t max_count, size_t m { LRUStorageST* plru_storage = NULL; - CPP_GUARD(plru_storage = new LRUStorageST(pstorage, max_count, max_size)); + MXS_EXCEPTION_GUARD(plru_storage = new LRUStorageST(pstorage, max_count, max_size)); return plru_storage; } diff --git a/server/modules/filter/cache/storage/storage_inmemory/inmemorystoragemt.cc b/server/modules/filter/cache/storage/storage_inmemory/inmemorystoragemt.cc index e5ccd183c..26eb05c90 100644 --- a/server/modules/filter/cache/storage/storage_inmemory/inmemorystoragemt.cc +++ b/server/modules/filter/cache/storage/storage_inmemory/inmemorystoragemt.cc @@ -14,6 +14,8 @@ #define MXS_MODULE_NAME "storage_inmemory" #include "inmemorystoragemt.h" +using maxscale::SpinLockGuard; + InMemoryStorageMT::InMemoryStorageMT(const std::string& name, uint32_t ttl) : InMemoryStorage(name, ttl) { @@ -34,28 +36,28 @@ InMemoryStorageMT* InMemoryStorageMT::create(const std::string& name, cache_result_t InMemoryStorageMT::get_info(uint32_t what, json_t** ppInfo) const { - LockGuard guard(&lock_); + SpinLockGuard guard(lock_); return do_get_info(what, ppInfo); } cache_result_t InMemoryStorageMT::get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppresult) { - LockGuard guard(&lock_); + SpinLockGuard guard(lock_); return do_get_value(key, flags, ppresult); } cache_result_t InMemoryStorageMT::put_value(const CACHE_KEY& key, const GWBUF* pvalue) { - LockGuard guard(&lock_); + SpinLockGuard guard(lock_); return do_put_value(key, pvalue); } cache_result_t InMemoryStorageMT::del_value(const CACHE_KEY& key) { - LockGuard guard(&lock_); + SpinLockGuard guard(lock_); return do_del_value(key); } diff --git a/server/modules/filter/cache/storage/storage_inmemory/inmemorystoragemt.h b/server/modules/filter/cache/storage/storage_inmemory/inmemorystoragemt.h index 9cda5bd7e..5917ef325 100644 --- a/server/modules/filter/cache/storage/storage_inmemory/inmemorystoragemt.h +++ b/server/modules/filter/cache/storage/storage_inmemory/inmemorystoragemt.h @@ -13,7 +13,7 @@ */ #include -#include +#include #include "inmemorystorage.h" class InMemoryStorageMT : public InMemoryStorage diff --git a/server/modules/filter/cache/storagefactory.cc b/server/modules/filter/cache/storagefactory.cc index 0e74af99b..eeb994547 100644 --- a/server/modules/filter/cache/storagefactory.cc +++ b/server/modules/filter/cache/storagefactory.cc @@ -130,7 +130,7 @@ StorageFactory* StorageFactory::Open(const char* zName) if (open_cache_storage(zName, &handle, &pApi, &capabilities)) { - CPP_GUARD(pFactory = new StorageFactory(handle, pApi, capabilities)); + MXS_EXCEPTION_GUARD(pFactory = new StorageFactory(handle, pApi, capabilities)); if (!pFactory) { @@ -162,7 +162,7 @@ Storage* StorageFactory::createStorage(cache_thread_model_t model, { StorageReal* pStorageReal = NULL; - CPP_GUARD(pStorageReal = new StorageReal(m_pApi, pRawStorage)); + MXS_EXCEPTION_GUARD(pStorageReal = new StorageReal(m_pApi, pRawStorage)); if (pStorageReal) {