[asterisk-scf-commits] asterisk-scf/integration/ice-util-cpp.git branch "retry_deux" updated.
Commits to the Asterisk SCF project code repositories
asterisk-scf-commits at lists.digium.com
Mon Mar 5 19:20:56 CST 2012
branch "retry_deux" has been updated
via 1907e2be4808a8afbae6b6492ce79cb173b075ff (commit)
via a8d4b01e6acdb983ad91e739230f0757e2e174b2 (commit)
from a52ed86d1b09804dd3ad6133ddd2e3930a93e3aa (commit)
Summary of changes:
.../Discovery/LocatorRegistrationWrapper.h | 100 ++-----------------
.../AsteriskSCF/Operations/OperationContextCache.h | 38 +++++---
src/Operations/OperationContextCache.cpp | 104 ++++++++++++--------
3 files changed, 94 insertions(+), 148 deletions(-)
- Log -----------------------------------------------------------------
commit 1907e2be4808a8afbae6b6492ce79cb173b075ff
Merge: a8d4b01 a52ed86
Author: Ken Hunt <ken.hunt at digium.com>
Date: Mon Mar 5 18:57:58 2012 -0600
Merged.
diff --cc include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
index c9dfda3,56eb5df..5cef1c1
--- a/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
+++ b/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
@@@ -134,7 -155,67 +134,6 @@@ public
}
private:
--
- void addLocatorParams(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsPtr& params,
- const std::string& comparatorGUID)
- {
- mServiceManagement->addLocatorParams(mAddParamsOpContext, params, comparatorGUID);
- }
-
- /**
- * Retries adding the locator params for the service.
- * @return true Successful
- */
- bool retryAddLocatorParams(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsPtr& params,
- const std::string& comparatorGUID)
- {
- AsteriskSCF::RetryPolicy retryPolicy(5,500);
-
- while(retryPolicy.canRetry())
- {
- try
- {
- addLocatorParams(params, comparatorGUID);
- return true;
- }
- catch(const Ice::ConnectionLostException&)
- {
- // For this exception, we'll just loop and rety as long as policy allows.
- }
- }
- return false;
- }
-
- void addService(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorManagementPrx& management)
- {
- mServiceManagement =
- AsteriskSCF::Core::Discovery::V1::ServiceManagementPrx::uncheckedCast(
- management->addService(mAddServiceOpContext, mService, mName));
- }
-
- /**
- * Retries adding the service to the ServiceLocator.
- * @return true Successful
- */
- bool retryAddService(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorManagementPrx& management)
- {
- AsteriskSCF::RetryPolicy retryPolicy(5,500);
-
- while(retryPolicy.canRetry())
- {
- try
- {
- addService(management);
- return true;
- }
- catch(const Ice::ConnectionLostException&)
- {
- // For this exception, we'll just loop and rety as long as policy allows.
- }
- }
- return false;
- }
-
//
// This template doesn't use boost locking simply because it already has a physical dependency
// to Ice runtime, so avoiding adding a second seemed reasonable.
commit a8d4b01e6acdb983ad91e739230f0757e2e174b2
Author: Ken Hunt <ken.hunt at digium.com>
Date: Mon Mar 5 18:53:38 2012 -0600
- Reverted LocatorRegistrationWrapper so that it doesn't do manual retries.
- Integrated feedback on OperationContextCache.
diff --git a/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h b/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
index 8dd0ba6..c9dfda3 100644
--- a/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
+++ b/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
@@ -56,9 +56,7 @@ public:
mService(service),
mName(name),
mParameters(params),
- mComparatorGUID(comparatorGUID),
- mAddServiceOpContext(AsteriskSCF::Operations::createContext()),
- mAddParamsOpContext(AsteriskSCF::Operations::createContext())
+ mComparatorGUID(comparatorGUID)
{
}
@@ -70,37 +68,18 @@ public:
bool registerService()
{
AsteriskSCF::Core::Discovery::V1::ServiceLocatorManagementPrx management =
- AsteriskSCF::Core::Discovery::V1::ServiceLocatorManagementPrx::checkedCast(
- mCommunicator->stringToProxy(mProxyString));
+ AsteriskSCF::Core::Discovery::V1::ServiceLocatorManagementPrx::checkedCast(
+ mCommunicator->stringToProxy(mProxyString));
+
if (management)
{
IceUtil::Mutex::Lock lock(mLock);
-
- try
- {
- addService(management);
- }
- catch(const Ice::ConnectionLostException&)
- {
- if (!retryAddService(management))
- {
- return false;
- }
- }
-
+ mServiceManagement =
+ AsteriskSCF::Core::Discovery::V1::ServiceManagementPrx::uncheckedCast(
+ management->addService(AsteriskSCF::Operations::createContext(), mService, mName));
if (mServiceManagement)
{
- try
- {
- addLocatorParams(mParameters, mComparatorGUID);
- }
- catch(const Ice::ConnectionLostException&)
- {
- if (!retryAddLocatorParams(mParameters, mComparatorGUID))
- {
- return false;
- }
- }
+ mServiceManagement->addLocatorParams(AsteriskSCF::Operations::createContext(), mParameters, mComparatorGUID);
return true;
}
}
@@ -156,82 +135,6 @@ public:
private:
- void addLocatorParams(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsPtr& params,
- const std::string& comparatorGUID)
- {
- mServiceManagement->addLocatorParams(mAddParamsOpContext, params, comparatorGUID);
- }
-
- /**
- * Retries adding the locator params for the service.
- * @return true Successful
- */
- bool retryAddLocatorParams(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsPtr& params,
- const std::string& comparatorGUID)
- {
- AsteriskSCF::RetryPolicy retryPolicy(5,500);
-
- while(retryPolicy.canRetry())
- {
- try
- {
- addLocatorParams(params, comparatorGUID);
- return true;
- }
- catch(const Ice::ConnectionLostException&)
- {
- // For this exception, we'll just loop and rety as long as policy allows.
- }
- catch(AsteriskSCF::System::V1::OperationCallCancelledException& cancelled)
- {
- if (cancelled.reason == AsteriskSCF::System::V1::Duplicate)
- {
- return true;
- }
- return false;
- }
- }
- return false;
- }
-
- void addService(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorManagementPrx& management)
- {
- mServiceManagement =
- AsteriskSCF::Core::Discovery::V1::ServiceManagementPrx::uncheckedCast(
- management->addService(mAddServiceOpContext, mService, mName));
- }
-
- /**
- * Retries adding the service to the ServiceLocator.
- * @return true Successful
- */
- bool retryAddService(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorManagementPrx& management)
- {
- AsteriskSCF::RetryPolicy retryPolicy(5,500);
-
- while(retryPolicy.canRetry())
- {
- try
- {
- addService(management);
- return true;
- }
- catch(const Ice::ConnectionLostException&)
- {
- // For this exception, we'll just loop and rety as long as policy allows.
- }
- catch(AsteriskSCF::System::V1::OperationCallCancelledException& cancelled)
- {
- if (cancelled.reason == AsteriskSCF::System::V1::Duplicate)
- {
- return true;
- }
- return false;
- }
- }
- return false;
- }
-
//
// This template doesn't use boost locking simply because it already has a physical dependency
// to Ice runtime, so avoiding adding a second seemed reasonable.
@@ -244,8 +147,6 @@ private:
AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsPtr mParameters;
AsteriskSCF::Core::Discovery::V1::ServiceManagementPrx mServiceManagement;
std::string mComparatorGUID;
- AsteriskSCF::System::V1::OperationContextPtr mAddServiceOpContext;
- AsteriskSCF::System::V1::OperationContextPtr mAddParamsOpContext;
};
typedef ASTSCF_DLL_EXPORT IceUtil::Handle<LocatorRegistrationWrapper> LocatorRegistrationWrapperPtr;
diff --git a/include/AsteriskSCF/Operations/OperationContextCache.h b/include/AsteriskSCF/Operations/OperationContextCache.h
index 77d7433..90611e7 100644
--- a/include/AsteriskSCF/Operations/OperationContextCache.h
+++ b/include/AsteriskSCF/Operations/OperationContextCache.h
@@ -16,6 +16,7 @@
#pragma once
#include <boost/thread/shared_mutex.hpp>
+#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <IceUtil/Timer.h>
@@ -27,43 +28,47 @@ namespace AsteriskSCF
namespace Operations
{
-class OperationContextCachEntry;
-typedef ASTSCF_DLL_EXPORT boost::shared_ptr<OperationContextCachEntry> OperationContextCachEntryPtr;
+class OperationContextCacheEntry;
+typedef ASTSCF_DLL_EXPORT boost::shared_ptr<OperationContextCacheEntry> OperationContextCacheEntryPtr;
class OperationContextPruner;
typedef IceUtil::Handle<OperationContextPruner> OperationContextPrunerPtr;
class ASTSCF_DLL_EXPORT OperationContextCookie
{
+public:
+ virtual ~OperationContextCookie() {}
};
-
typedef ASTSCF_DLL_EXPORT boost::shared_ptr<OperationContextCookie> OperationContextCookiePtr;
+class OperationContextCache;
+typedef boost::shared_ptr<OperationContextCache> OperationContextCachePtr;
+
/**
* Utiltity class that provides a queryable cache of OperationContext objects.
*/
-class ASTSCF_DLL_EXPORT OperationContextCache : public IceUtil::Shared
+class ASTSCF_DLL_EXPORT OperationContextCache : public IceUtil::Shared
{
public:
/**
- * ctor
+ * Factory method for non-logging cache.
* @param ttlSeconds The time-to-live for the OperationContexts being cached.
* Entries will remain in the cache for at least the provided value, but can
* remain in cache longer.
*/
- OperationContextCache(int ttlSeconds);
+ static OperationContextCachePtr create(int ttlSeconds);
/**
- * ctor for logging.
+ * Factory method for logging cache.
* @param ttlSeconds The time-to-live for the OperationContexts being cached.
* Entries will remain in the cache for at least the provided value, but can
* remain in cache longer.
* @param logger The logger to log to.
* @param label Label to apply when logging to identify this cache.
*/
- OperationContextCache(int ttlSeconds,
- const AsteriskSCF::System::Logging::Logger& logger,
- const std::string& label);
+ static OperationContextCachePtr create(int ttlSeconds,
+ const AsteriskSCF::System::Logging::Logger& logger,
+ const std::string& label);
~OperationContextCache();
@@ -121,9 +126,15 @@ public:
std::size_t size();
private:
- bool containsImpl(const AsteriskSCF::System::V1::OperationContextPtr& operationContext,
- OperationContextCachEntryPtr& cacheEntry);
+ OperationContextCache(int ttlSeconds);
+
+ OperationContextCache(int ttlSeconds,
+ const AsteriskSCF::System::Logging::Logger& logger,
+ const std::string& label);
+
+ OperationContextCacheEntryPtr get(const AsteriskSCF::System::V1::OperationContextPtr& operationContext);
void logStaleList(std::vector<std::string>& staleList);
+ void setPruner(const OperationContextPrunerPtr& pruner);
AsteriskSCF::System::Logging::Logger mLogger;
bool mLoggingEnabled;
@@ -131,9 +142,8 @@ private:
boost::shared_mutex mLock;
OperationContextPrunerPtr mPruner;
IceUtil::Time mTTL;
- std::map<std::string, OperationContextCachEntryPtr> mCache;
+ std::map<std::string, OperationContextCacheEntryPtr> mCache;
};
-typedef IceUtil::Handle<OperationContextCache> OperationContextCachePtr;
} // Operations
} // AsteriskSCF
diff --git a/src/Operations/OperationContextCache.cpp b/src/Operations/OperationContextCache.cpp
index c1eac70..28ace80 100644
--- a/src/Operations/OperationContextCache.cpp
+++ b/src/Operations/OperationContextCache.cpp
@@ -22,7 +22,7 @@ using namespace AsteriskSCF::System::Logging;
namespace
{
-Logger lg = AsteriskSCF::System::Logging::getLoggerFactory().getLogger("AsteriskSCF.Helpers");
+Logger lg = AsteriskSCF::System::Logging::getLoggerFactory().getLogger("AsteriskSCF.Operations");
}
namespace AsteriskSCF
@@ -33,7 +33,7 @@ namespace Operations
/**
* Wrapper to hold OperationContext with a timestamp in the cache.
*/
-class OperationContextCachEntry : public IceUtil::Shared
+class OperationContextCacheEntry : public IceUtil::Shared
{
public:
/**
@@ -41,14 +41,14 @@ public:
* @param context The context to wrap.
* @param ttl The time-to-live to use in isStale() tests.
*/
- OperationContextCachEntry(const OperationContextPtr& context, const IceUtil::Time& ttl) :
+ OperationContextCacheEntry(const OperationContextPtr& context, const IceUtil::Time& ttl) :
mContext(context),
mTimestamp(IceUtil::Time::now()),
mTTL(ttl)
{
}
- OperationContextCachEntry(const OperationContextPtr& context, const OperationContextCookiePtr& cookie, const IceUtil::Time& ttl) :
+ OperationContextCacheEntry(const OperationContextPtr& context, const OperationContextCookiePtr& cookie, const IceUtil::Time& ttl) :
mContext(context),
mCookie(cookie),
mTimestamp(IceUtil::Time::now()),
@@ -94,7 +94,7 @@ private:
class OperationContextPruner : public IceUtil::TimerTask
{
public:
- OperationContextPruner(OperationContextCache* cache, int ttlSeconds) :
+ OperationContextPruner(const OperationContextCachePtr& cache, int ttlSeconds) :
mCache(cache),
mTimer(new IceUtil::Timer)
{
@@ -106,28 +106,58 @@ public:
*/
void runTimerTask()
{
- if (mCache == 0)
+ if (boost::shared_ptr<OperationContextCache> cache = mCache.lock())
{
- return;
+ cache->prune();
}
-
- mCache->prune();
}
void cancel()
{
mTimer->destroy();
- mTimer = 0;
- mCache = 0;
}
private:
- OperationContextCache* mCache; // Raw pointer to avoid circular refs
+ boost::weak_ptr<OperationContextCache> mCache; // Weak pointer to avoid circular refs
IceUtil::TimerPtr mTimer;
};
/**
- * Constructor.
+ * Factory method for non-logging cache.
+ */
+OperationContextCachePtr OperationContextCache::create(int ttlSeconds)
+{
+ OperationContextCachePtr cache(new OperationContextCache(ttlSeconds));
+
+ OperationContextPrunerPtr pruner = new OperationContextPruner(cache, ttlSeconds);
+
+ cache->setPruner(pruner);
+
+ return cache;
+}
+
+/**
+ * Factory method for logging cache.
+ */
+OperationContextCachePtr OperationContextCache::create(int ttlSeconds,
+ const AsteriskSCF::System::Logging::Logger& logger,
+ const std::string& label)
+{
+ OperationContextCachePtr cache(new OperationContextCache(ttlSeconds, logger, label));
+
+ OperationContextPrunerPtr pruner = new OperationContextPruner(cache, ttlSeconds);
+
+ cache->setPruner(pruner);
+
+ return cache;
+}
+
+void OperationContextCache::setPruner(const OperationContextPrunerPtr& pruner)
+{
+ mPruner = pruner;
+}
+
+/**
* @param ttlSeconds The time to live for the cache, specified in seconds.
* This is a minimum time for an OperationContext to be cached. They
* may be cached longer.
@@ -136,7 +166,6 @@ OperationContextCache::OperationContextCache(int ttlSeconds)
: mLogger(lg),
mLoggingEnabled(false),
mLoggingLabel(""),
- mPruner(new OperationContextPruner(this, ttlSeconds)),
mTTL(IceUtil::Time::seconds(ttlSeconds))
{
}
@@ -155,7 +184,6 @@ OperationContextCache::OperationContextCache(int ttlSeconds,
: mLogger(logger),
mLoggingEnabled(true),
mLoggingLabel(label),
- mPruner(new OperationContextPruner(this, ttlSeconds)),
mTTL(IceUtil::Time::seconds(ttlSeconds))
{
}
@@ -168,22 +196,19 @@ OperationContextCache::~OperationContextCache()
/**
* Non-locking contains() operation for code sharing.
*/
-bool OperationContextCache::containsImpl(
- const OperationContextPtr& operationContext,
- OperationContextCachEntryPtr& cacheEntry)
+OperationContextCacheEntryPtr OperationContextCache::get(const OperationContextPtr& operationContext)
{
- std::map<std::string, OperationContextCachEntryPtr>::iterator entry = mCache.find(operationContext->id);
+ std::map<std::string, OperationContextCacheEntryPtr>::iterator entry = mCache.find(operationContext->id);
if (entry == mCache.end())
{
- return false;
+ return OperationContextCacheEntryPtr(); // null
}
- cacheEntry = entry->second;
- return true;
+ return entry->second;
}
/**
- * Caches the specified context if it isnt' already in the cache.
+ * Caches the specified context if it isn't already in the cache.
*
* @param operationContext The context to add to the cache.
* @return true The context was added, which means it wasn't already in the cache.
@@ -193,22 +218,22 @@ bool OperationContextCache::containsImpl(
*/
bool OperationContextCache::addOperationContext(const OperationContextPtr& operationContext)
{
- boost::shared_lock<boost::shared_mutex> lock(mLock);
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
- OperationContextCachEntryPtr existingEntry;
- if (containsImpl(operationContext, existingEntry))
+ OperationContextCacheEntryPtr existingEntry = get(operationContext);
+ if (!existingEntry)
{
return false;
}
- OperationContextCachEntryPtr entry(new OperationContextCachEntry(operationContext, mTTL));
+ OperationContextCacheEntryPtr entry(new OperationContextCacheEntry(operationContext, mTTL));
mCache[operationContext->id] = entry;
return true;
}
/**
- * Caches the specified context if it isnt' already in the cache, and associate a cookie with it.
+ * Caches the specified context if it isn't already in the cache, and associate a cookie with it.
*
* @param operationContext The context to add to the cache.
* @param inCookie A cookie object to associate with this entry in the cache.
@@ -224,16 +249,16 @@ bool OperationContextCache::addOperationContext(
const OperationContextCookiePtr& inCookie,
OperationContextCookiePtr& existingCookie)
{
- boost::shared_lock<boost::shared_mutex> lock(mLock);
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
- OperationContextCachEntryPtr existingEntry;
- if (containsImpl(operationContext, existingEntry))
+ OperationContextCacheEntryPtr existingEntry = get(operationContext);
+ if (existingEntry)
{
existingCookie = existingEntry->getCookie();
return false;
}
- OperationContextCachEntryPtr entry(new OperationContextCachEntry(operationContext, inCookie, mTTL));
+ OperationContextCacheEntryPtr entry(new OperationContextCacheEntry(operationContext, inCookie, mTTL));
mCache[operationContext->id] = entry;
return true;
@@ -243,12 +268,7 @@ void OperationContextCache::removeOperationContext(const OperationContextPtr& op
{
boost::unique_lock<boost::shared_mutex> lock(mLock);
- std::map<std::string, OperationContextCachEntryPtr>::iterator entryIter = mCache.find(operationContext->id);
- if (entryIter == mCache.end())
- {
- return;
- }
- mCache.erase(entryIter);
+ mCache.erase(operationContext->id);
}
/**
@@ -259,8 +279,8 @@ bool OperationContextCache::contains(const OperationContextPtr& operationContext
{
boost::shared_lock<boost::shared_mutex> lock(mLock);
- OperationContextCachEntryPtr entry;
- return containsImpl(operationContext, entry);
+ OperationContextCacheEntryPtr entry = get(operationContext);
+ return entry;
}
void OperationContextCache::logStaleList(std::vector<std::string>& staleList)
@@ -283,7 +303,7 @@ void OperationContextCache::prune()
{ // lock scope
boost::unique_lock<boost::shared_mutex> lock(mLock);
- std::map<std::string, OperationContextCachEntryPtr>::const_iterator cacheIter;
+ std::map<std::string, OperationContextCacheEntryPtr>::const_iterator cacheIter;
IceUtil::Time now(IceUtil::Time::now());
@@ -316,5 +336,5 @@ std::size_t OperationContextCache::size()
return mCache.size();
}
-} // namespace Helpers
+} // namespace Operations
} // namespace AsteriskSCF
-----------------------------------------------------------------------
--
asterisk-scf/integration/ice-util-cpp.git
More information about the asterisk-scf-commits
mailing list