pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0080-schema-cache-Read-schema-instead-of-rewriting-it-whe.patch

403b09
From a9ebd731b23ebf9a56401139fe20dafc50322ff3 Mon Sep 17 00:00:00 2001
403b09
From: David Kupka <dkupka@redhat.com>
403b09
Date: Tue, 16 Aug 2016 15:32:47 +0200
403b09
Subject: [PATCH] schema cache: Read schema instead of rewriting it when
403b09
 SchemaUpToDate
403b09
403b09
https://fedorahosted.org/freeipa/ticket/6048
403b09
403b09
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
403b09
---
403b09
 ipaclient/remote_plugins/schema.py | 46 ++++++++++++++++++++------------------
403b09
 1 file changed, 24 insertions(+), 22 deletions(-)
403b09
403b09
diff --git a/ipaclient/remote_plugins/schema.py b/ipaclient/remote_plugins/schema.py
403b09
index 2fc6cce3eca392447cd4230216900116002934f4..e8b3f61bd8460c60630e8710028da79654819bd1 100644
403b09
--- a/ipaclient/remote_plugins/schema.py
403b09
+++ b/ipaclient/remote_plugins/schema.py
403b09
@@ -17,6 +17,7 @@ import six
403b09
 
403b09
 from ipaclient.frontend import ClientCommand, ClientMethod
403b09
 from ipalib import errors, parameters, plugable
403b09
+from ipalib.errors import SchemaUpToDate
403b09
 from ipalib.frontend import Object
403b09
 from ipalib.output import Output
403b09
 from ipalib.parameters import DefaultFrom, Flag, Password, Str
403b09
@@ -439,15 +440,14 @@ class Schema(object):
403b09
             self._dict[ns] = {}
403b09
             self._namespaces[ns] = _SchemaNameSpace(self, ns)
403b09
 
403b09
-        is_known = False
403b09
-        if not api.env.force_schema_check:
403b09
-            try:
403b09
-                self._fingerprint = server_info['fingerprint']
403b09
-                self._expiration = server_info['expiration']
403b09
-            except KeyError:
403b09
-                pass
403b09
-            else:
403b09
-                is_known = True
403b09
+        try:
403b09
+            self._fingerprint = server_info['fingerprint']
403b09
+            self._expiration = server_info['expiration']
403b09
+        except KeyError:
403b09
+            is_known = False
403b09
+        else:
403b09
+            is_known = (not api.env.force_schema_check and
403b09
+                        self._expiration > time.time())
403b09
 
403b09
         if is_known:
403b09
             try:
403b09
@@ -461,14 +461,15 @@ class Schema(object):
403b09
             self._fetch(client)
403b09
         except NotAvailable:
403b09
             raise
403b09
+        except SchemaUpToDate as e:
403b09
+            self._fingerprint = e.fingerprint
403b09
+            self._expiration = time.time() + e.ttl
403b09
+            self._read_schema()
403b09
         else:
403b09
             self._write_schema()
403b09
-        finally:
403b09
-            try:
403b09
-                server_info['fingerprint'] = self._fingerprint
403b09
-                server_info['expiration'] = self._expiration
403b09
-            except AttributeError:
403b09
-                pass
403b09
+
403b09
+        server_info['fingerprint'] = self._fingerprint
403b09
+        server_info['expiration'] = self._expiration
403b09
 
403b09
     @contextlib.contextmanager
403b09
     def _open(self, filename, mode):
403b09
@@ -501,21 +502,22 @@ class Schema(object):
403b09
             schema = client.forward(u'schema', **kwargs)['result']
403b09
         except errors.CommandError:
403b09
             raise NotAvailable()
403b09
-        except errors.SchemaUpToDate as e:
403b09
-            fp = e.fingerprint
403b09
-            ttl = e.ttl
403b09
-        else:
403b09
+
403b09
+        try:
403b09
             fp = schema['fingerprint']
403b09
-            ttl = schema.pop('ttl', 0)
403b09
-            schema.pop('version', None)
403b09
+            ttl = schema.pop('ttl')
403b09
+            schema.pop('version')
403b09
 
403b09
             for key, value in schema.items():
403b09
                 if key in self.namespaces:
403b09
                     value = {m['full_name']: m for m in value}
403b09
                 self._dict[key] = value
403b09
+        except KeyError as e:
403b09
+            logger.warning("Failed to fetch schema: %s", e)
403b09
+            raise NotAvailable()
403b09
 
403b09
         self._fingerprint = fp
403b09
-        self._expiration = ttl + time.time()
403b09
+        self._expiration = time.time() + ttl
403b09
 
403b09
     def _read_schema(self):
403b09
         self._file.truncate(0)
403b09
-- 
403b09
2.7.4
403b09