]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: implement singleton pattern over RGWKeystoneTokenCache.
authorRadoslaw Zarzynski <rzarzynski@mirantis.com>
Wed, 6 Apr 2016 16:01:17 +0000 (18:01 +0200)
committerRadoslaw Zarzynski <rzarzynski@mirantis.com>
Thu, 2 Jun 2016 13:12:10 +0000 (15:12 +0200)
Signed-off-by: Radoslaw Zarzynski <rzarzynski@mirantis.com>
src/rgw/rgw_keystone.cc
src/rgw/rgw_keystone.h
src/rgw/rgw_swift.cc

index 978b70d05fd2a47b6612f6370dbfa8612bde3e1c..4324e639264fc9ddec12f0838b1fa0d2b69f50f0 100644 (file)
@@ -203,6 +203,13 @@ int KeystoneToken::parse(CephContext * const cct,
   return 0;
 }
 
+RGWKeystoneTokenCache& RGWKeystoneTokenCache::get_instance()
+{
+  /* In C++11 this is thread safe. */
+  static RGWKeystoneTokenCache instance;
+  return instance;
+}
+
 bool RGWKeystoneTokenCache::find(const string& token_id, KeystoneToken& token)
 {
   lock.Lock();
index ce41b86149cc9e9cfe053e463676ea35d6ebbd81..18ee53bac85a6ff0220564478ea95ee6364cbd86 100644 (file)
@@ -95,13 +95,14 @@ public:
   void decode_json(JSONObj *access_obj);
 };
 
+
 class RGWKeystoneTokenCache {
   struct token_entry {
     KeystoneToken token;
     list<string>::iterator lru_iter;
   };
 
-  CephContext *cct;
+  CephContext * const cct;
 
   string admin_token_id;
   map<string, token_entry> tokens;
@@ -109,14 +110,18 @@ class RGWKeystoneTokenCache {
 
   Mutex lock;
 
-  size_t max;
+  const size_t max;
 
-public:
-  RGWKeystoneTokenCache(CephContext *_cct, int _max)
-    : cct(_cct),
+  RGWKeystoneTokenCache()
+    : cct(g_ceph_context),
       lock("RGWKeystoneTokenCache", true /* recursive */),
-      max(_max) {
+      max(cct->_conf->rgw_keystone_token_cache_size) {
   }
+public:
+  RGWKeystoneTokenCache(const RGWKeystoneTokenCache&) = delete;
+  void operator=(const RGWKeystoneTokenCache&) = delete;
+
+  static RGWKeystoneTokenCache& get_instance();
 
   bool find(const string& token_id, KeystoneToken& token);
   bool find_admin(KeystoneToken& token);
@@ -125,6 +130,7 @@ public:
   void invalidate(const string& token_id);
 };
 
+
 class KeystoneAdminTokenRequest {
 public:
   virtual ~KeystoneAdminTokenRequest() = default;
index dae77b1f4996a6cb214cfdd5f42066e299be3d70..081357457d3855f344a0f61dcaa1b2f443b5afd8 100644 (file)
@@ -127,8 +127,6 @@ typedef RGWKeystoneHTTPTransceiver RGWValidateKeystoneToken;
 typedef RGWKeystoneHTTPTransceiver RGWGetKeystoneAdminToken;
 typedef RGWKeystoneHTTPTransceiver RGWGetRevokedTokens;
 
-static RGWKeystoneTokenCache *keystone_token_cache = NULL;
-
 int RGWSwift::get_keystone_url(CephContext * const cct,
                                std::string& url)
 {
@@ -173,7 +171,7 @@ int RGWSwift::get_keystone_admin_token(CephContext * const cct,
   KeystoneToken t;
 
   /* Try cache first. */
-  if (keystone_token_cache->find_admin(t)) {
+  if (RGWKeystoneTokenCache::get_instance().find_admin(t)) {
     ldout(cct, 20) << "found cached admin token" << dendl;
     token = t.token.id;
     return 0;
@@ -216,7 +214,7 @@ int RGWSwift::get_keystone_admin_token(CephContext * const cct,
     return -EINVAL;
   }
 
-  keystone_token_cache->add_admin(t);
+  RGWKeystoneTokenCache::get_instance().add_admin(t);
   token = t.token.id;
   return 0;
 }
@@ -314,7 +312,7 @@ int RGWSwift::check_revoked()
     }
 
     string token_id = token->get_data();
-    keystone_token_cache->invalidate(token_id);
+    RGWKeystoneTokenCache::get_instance().invalidate(token_id);
   }
   
   return 0;
@@ -421,7 +419,7 @@ int RGWSwift::validate_keystone_token(RGWRados *store, const string& token,
   ldout(cct, 20) << "token_id=" << token_id << dendl;
 
   /* check cache first */
-  if (keystone_token_cache->find(token_id, t)) {
+  if (RGWKeystoneTokenCache::get_instance().find(token_id, t)) {
     rgw_set_keystone_token_auth_info(t, &info);
 
     ldout(cct, 20) << "cached token.project.id=" << t.get_project_id() << dendl;
@@ -489,7 +487,7 @@ int RGWSwift::validate_keystone_token(RGWRados *store, const string& token,
     return -EPERM;
   }
 
-  keystone_token_cache->add(token_id, t);
+  RGWKeystoneTokenCache::get_instance().add(token_id, t);
 
   ret = update_user_info(store, &info, rgw_user);
   if (ret < 0)
@@ -756,8 +754,6 @@ void RGWSwift::init()
 
 void RGWSwift::init_keystone()
 {
-  keystone_token_cache = new RGWKeystoneTokenCache(cct, cct->_conf->rgw_keystone_token_cache_size);
-
   keystone_revoke_thread = new KeystoneRevokeThread(cct, this);
   keystone_revoke_thread->create("rgw_swift_k_rev");
 }
@@ -771,9 +767,6 @@ void RGWSwift::finalize()
 
 void RGWSwift::finalize_keystone()
 {
-  delete keystone_token_cache;
-  keystone_token_cache = NULL;
-
   down_flag.set(1);
   if (keystone_revoke_thread) {
     keystone_revoke_thread->stop();