Cache: Use MXS_EXCEPTION_GUARD and maxscale::SpinLockGuard

This commit is contained in:
Johan Wikman
2016-12-08 23:25:10 +02:00
parent c9bc7a6e76
commit 8bbe80df99
12 changed files with 47 additions and 68 deletions

View File

@ -19,6 +19,7 @@
#include <maxscale/filter.h> #include <maxscale/filter.h>
#include <maxscale/gwdirs.h> #include <maxscale/gwdirs.h>
#include <maxscale/modulecmd.h> #include <maxscale/modulecmd.h>
#include <maxscale/cpp.hh>
#include "cachemt.h" #include "cachemt.h"
#include "cachept.h" #include "cachept.h"
#include "cachefiltersession.hh" #include "cachefiltersession.hh"
@ -191,12 +192,12 @@ static FILTER *createInstance(const char* zName, char** pzOptions, FILTER_PARAME
{ {
case CACHE_THREAD_MODEL_MT: case CACHE_THREAD_MODEL_MT:
MXS_NOTICE("Creating shared cache."); 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; break;
case CACHE_THREAD_MODEL_ST: case CACHE_THREAD_MODEL_ST:
MXS_NOTICE("Creating thread specific cache."); 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; break;
default: default:
@ -229,7 +230,7 @@ static void *newSession(FILTER* pInstance, SESSION* pSession)
Cache* pCache = pFilter->pCache; Cache* pCache = pFilter->pCache;
CacheFilterSession* pCacheFilterSession = NULL; CacheFilterSession* pCacheFilterSession = NULL;
CPP_GUARD(pCacheFilterSession = CacheFilterSession::Create(pCache, pSession)); MXS_EXCEPTION_GUARD(pCacheFilterSession = CacheFilterSession::Create(pCache, pSession));
return pCacheFilterSession; return pCacheFilterSession;
} }
@ -244,7 +245,7 @@ static void closeSession(FILTER* pInstance, void* pSessionData)
{ {
CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(pSessionData); CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(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<CacheFilterSession*>(pSessionData); CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(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<CacheFilterSession*>(pSessionData); CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(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<CacheFilterSession*>(pSessionData); CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(pSessionData);
int rv = 0; int rv = 0;
CPP_GUARD(rv = pCacheFilterSession->routeQuery(pPacket)); MXS_EXCEPTION_GUARD(rv = pCacheFilterSession->routeQuery(pPacket));
return rv; return rv;
} }
@ -317,7 +318,7 @@ static int clientReply(FILTER* pInstance, void* pSessionData, GWBUF* pPacket)
CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(pSessionData); CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(pSessionData);
int rv = 0; int rv = 0;
CPP_GUARD(rv = pCacheFilterSession->clientReply(pPacket)); MXS_EXCEPTION_GUARD(rv = pCacheFilterSession->clientReply(pPacket));
return rv; return rv;
} }
@ -336,7 +337,7 @@ static void diagnostics(FILTER* pInstance, void* pSessionData, DCB* pDcb)
{ {
CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(pSessionData); CacheFilterSession* pCacheFilterSession = static_cast<CacheFilterSession*>(pSessionData);
CPP_GUARD(pCacheFilterSession->diagnostics(pDcb)); MXS_EXCEPTION_GUARD(pCacheFilterSession->diagnostics(pDcb));
} }
/** /**

View File

@ -81,34 +81,4 @@ void cache_config_finish(CACHE_CONFIG& config);
void cache_config_free(CACHE_CONFIG* pConfig); void cache_config_free(CACHE_CONFIG* pConfig);
void cache_config_reset(CACHE_CONFIG& config); 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 #endif

View File

@ -16,6 +16,7 @@
#include "storage.h" #include "storage.h"
#include "storagefactory.h" #include "storagefactory.h"
using maxscale::SpinLockGuard;
using std::tr1::shared_ptr; using std::tr1::shared_ptr;
CacheMT::CacheMT(const std::string& name, 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 json_t* CacheMT::get_info(uint32_t flags) const
{ {
LockGuard guard(&m_lockPending); SpinLockGuard guard(m_lockPending);
return CacheSimple::do_get_info(flags); return CacheSimple::do_get_info(flags);
} }
bool CacheMT::must_refresh(const CACHE_KEY& key, const CacheFilterSession* pSession) 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); return do_must_refresh(key, pSession);
} }
void CacheMT::refreshed(const CACHE_KEY& key, const CacheFilterSession* pSession) void CacheMT::refreshed(const CACHE_KEY& key, const CacheFilterSession* pSession)
{ {
LockGuard guard(&m_lockPending); SpinLockGuard guard(m_lockPending);
do_refreshed(key, pSession); do_refreshed(key, pSession);
} }
@ -96,7 +97,7 @@ CacheMT* CacheMT::Create(const std::string& name,
if (pStorage) if (pStorage)
{ {
CPP_GUARD(pCache = new CacheMT(name, MXS_EXCEPTION_GUARD(pCache = new CacheMT(name,
pConfig, pConfig,
sRules, sRules,
sFactory, sFactory,

View File

@ -13,7 +13,7 @@
*/ */
#include <maxscale/cdefs.h> #include <maxscale/cdefs.h>
#include <maxscale/spinlock.h> #include <maxscale/spinlock.hh>
#include "cachesimple.h" #include "cachesimple.h"
class CacheMT : public CacheSimple class CacheMT : public CacheSimple

View File

@ -15,6 +15,7 @@
#include "cachept.h" #include "cachept.h"
#include <maxscale/atomic.h> #include <maxscale/atomic.h>
#include <maxscale/platform.h> #include <maxscale/platform.h>
#include <maxscale/cpp.hh>
#include "cachest.h" #include "cachest.h"
#include "storagefactory.h" #include "storagefactory.h"
@ -168,7 +169,7 @@ CachePT* CachePT::Create(const std::string& name,
CacheST* pCacheST = 0; CacheST* pCacheST = 0;
CPP_GUARD(pCacheST = CacheST::Create(namest, sRules, sFactory, pConfig)); MXS_EXCEPTION_GUARD(pCacheST = CacheST::Create(namest, sRules, sFactory, pConfig));
if (pCacheST) if (pCacheST)
{ {

View File

@ -13,6 +13,7 @@
#define MXS_MODULE_NAME "cache" #define MXS_MODULE_NAME "cache"
#include "cachest.h" #include "cachest.h"
#include <maxscale/cpp.hh>
#include "storage.h" #include "storage.h"
#include "storagefactory.h" #include "storagefactory.h"
@ -101,7 +102,7 @@ CacheST* CacheST::Create(const std::string& name,
if (pStorage) if (pStorage)
{ {
CPP_GUARD(pCache = new CacheST(name, MXS_EXCEPTION_GUARD(pCache = new CacheST(name,
pConfig, pConfig,
sRules, sRules,
sFactory, sFactory,

View File

@ -14,6 +14,8 @@
#define MXS_MODULE_NAME "cache" #define MXS_MODULE_NAME "cache"
#include "lrustoragemt.h" #include "lrustoragemt.h"
using maxscale::SpinLockGuard;
LRUStorageMT::LRUStorageMT(Storage* pstorage, size_t max_count, size_t max_size) LRUStorageMT::LRUStorageMT(Storage* pstorage, size_t max_count, size_t max_size)
: LRUStorage(pstorage, max_count, 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; 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; 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, cache_result_t LRUStorageMT::get_info(uint32_t what,
json_t** ppInfo) const json_t** ppInfo) const
{ {
LockGuard guard(&lock_); SpinLockGuard guard(lock_);
return LRUStorage::do_get_info(what, ppInfo); return LRUStorage::do_get_info(what, ppInfo);
} }
@ -47,7 +49,7 @@ cache_result_t LRUStorageMT::get_value(const CACHE_KEY& key,
uint32_t flags, uint32_t flags,
GWBUF** ppvalue) GWBUF** ppvalue)
{ {
LockGuard guard(&lock_); SpinLockGuard guard(lock_);
return do_get_value(key, flags, ppvalue); 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, cache_result_t LRUStorageMT::put_value(const CACHE_KEY& key,
const GWBUF* pvalue) const GWBUF* pvalue)
{ {
LockGuard guard(&lock_); SpinLockGuard guard(lock_);
return do_put_value(key, pvalue); return do_put_value(key, pvalue);
} }
cache_result_t LRUStorageMT::del_value(const CACHE_KEY& key) cache_result_t LRUStorageMT::del_value(const CACHE_KEY& key)
{ {
LockGuard guard(&lock_); SpinLockGuard guard(lock_);
return do_del_value(key); return do_del_value(key);
} }

View File

@ -13,7 +13,7 @@
*/ */
#include <maxscale/cdefs.h> #include <maxscale/cdefs.h>
#include <maxscale/spinlock.h> #include <maxscale/spinlock.hh>
#include "lrustorage.h" #include "lrustorage.h"
class LRUStorageMT : public LRUStorage class LRUStorageMT : public LRUStorage

View File

@ -13,6 +13,7 @@
#define MXS_MODULE_NAME "cache" #define MXS_MODULE_NAME "cache"
#include "lrustoragest.h" #include "lrustoragest.h"
#include <maxscale/cpp.hh>
LRUStorageST::LRUStorageST(Storage* pstorage, size_t max_count, size_t max_size) LRUStorageST::LRUStorageST(Storage* pstorage, size_t max_count, size_t max_size)
: LRUStorage(pstorage, max_count, 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; 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; return plru_storage;
} }

View File

@ -14,6 +14,8 @@
#define MXS_MODULE_NAME "storage_inmemory" #define MXS_MODULE_NAME "storage_inmemory"
#include "inmemorystoragemt.h" #include "inmemorystoragemt.h"
using maxscale::SpinLockGuard;
InMemoryStorageMT::InMemoryStorageMT(const std::string& name, uint32_t ttl) InMemoryStorageMT::InMemoryStorageMT(const std::string& name, uint32_t ttl)
: InMemoryStorage(name, 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 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); return do_get_info(what, ppInfo);
} }
cache_result_t InMemoryStorageMT::get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppresult) 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); return do_get_value(key, flags, ppresult);
} }
cache_result_t InMemoryStorageMT::put_value(const CACHE_KEY& key, const GWBUF* pvalue) 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); return do_put_value(key, pvalue);
} }
cache_result_t InMemoryStorageMT::del_value(const CACHE_KEY& key) cache_result_t InMemoryStorageMT::del_value(const CACHE_KEY& key)
{ {
LockGuard guard(&lock_); SpinLockGuard guard(lock_);
return do_del_value(key); return do_del_value(key);
} }

View File

@ -13,7 +13,7 @@
*/ */
#include <maxscale/cdefs.h> #include <maxscale/cdefs.h>
#include <maxscale/spinlock.h> #include <maxscale/spinlock.hh>
#include "inmemorystorage.h" #include "inmemorystorage.h"
class InMemoryStorageMT : public InMemoryStorage class InMemoryStorageMT : public InMemoryStorage

View File

@ -130,7 +130,7 @@ StorageFactory* StorageFactory::Open(const char* zName)
if (open_cache_storage(zName, &handle, &pApi, &capabilities)) 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) if (!pFactory)
{ {
@ -162,7 +162,7 @@ Storage* StorageFactory::createStorage(cache_thread_model_t model,
{ {
StorageReal* pStorageReal = NULL; StorageReal* pStorageReal = NULL;
CPP_GUARD(pStorageReal = new StorageReal(m_pApi, pRawStorage)); MXS_EXCEPTION_GUARD(pStorageReal = new StorageReal(m_pApi, pRawStorage));
if (pStorageReal) if (pStorageReal)
{ {