The Identity, Policy and Audit system
CentOS Sources
2016-11-03 403b09ab980c02ef36095973349a13e0181c794a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
From a9ebd731b23ebf9a56401139fe20dafc50322ff3 Mon Sep 17 00:00:00 2001
From: David Kupka <dkupka@redhat.com>
Date: Tue, 16 Aug 2016 15:32:47 +0200
Subject: [PATCH] schema cache: Read schema instead of rewriting it when
 SchemaUpToDate
 
https://fedorahosted.org/freeipa/ticket/6048
 
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
---
 ipaclient/remote_plugins/schema.py | 46 ++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
 
diff --git a/ipaclient/remote_plugins/schema.py b/ipaclient/remote_plugins/schema.py
index 2fc6cce3eca392447cd4230216900116002934f4..e8b3f61bd8460c60630e8710028da79654819bd1 100644
--- a/ipaclient/remote_plugins/schema.py
+++ b/ipaclient/remote_plugins/schema.py
@@ -17,6 +17,7 @@ import six
 
 from ipaclient.frontend import ClientCommand, ClientMethod
 from ipalib import errors, parameters, plugable
+from ipalib.errors import SchemaUpToDate
 from ipalib.frontend import Object
 from ipalib.output import Output
 from ipalib.parameters import DefaultFrom, Flag, Password, Str
@@ -439,15 +440,14 @@ class Schema(object):
             self._dict[ns] = {}
             self._namespaces[ns] = _SchemaNameSpace(self, ns)
 
-        is_known = False
-        if not api.env.force_schema_check:
-            try:
-                self._fingerprint = server_info['fingerprint']
-                self._expiration = server_info['expiration']
-            except KeyError:
-                pass
-            else:
-                is_known = True
+        try:
+            self._fingerprint = server_info['fingerprint']
+            self._expiration = server_info['expiration']
+        except KeyError:
+            is_known = False
+        else:
+            is_known = (not api.env.force_schema_check and
+                        self._expiration > time.time())
 
         if is_known:
             try:
@@ -461,14 +461,15 @@ class Schema(object):
             self._fetch(client)
         except NotAvailable:
             raise
+        except SchemaUpToDate as e:
+            self._fingerprint = e.fingerprint
+            self._expiration = time.time() + e.ttl
+            self._read_schema()
         else:
             self._write_schema()
-        finally:
-            try:
-                server_info['fingerprint'] = self._fingerprint
-                server_info['expiration'] = self._expiration
-            except AttributeError:
-                pass
+
+        server_info['fingerprint'] = self._fingerprint
+        server_info['expiration'] = self._expiration
 
     @contextlib.contextmanager
     def _open(self, filename, mode):
@@ -501,21 +502,22 @@ class Schema(object):
             schema = client.forward(u'schema', **kwargs)['result']
         except errors.CommandError:
             raise NotAvailable()
-        except errors.SchemaUpToDate as e:
-            fp = e.fingerprint
-            ttl = e.ttl
-        else:
+
+        try:
             fp = schema['fingerprint']
-            ttl = schema.pop('ttl', 0)
-            schema.pop('version', None)
+            ttl = schema.pop('ttl')
+            schema.pop('version')
 
             for key, value in schema.items():
                 if key in self.namespaces:
                     value = {m['full_name']: m for m in value}
                 self._dict[key] = value
+        except KeyError as e:
+            logger.warning("Failed to fetch schema: %s", e)
+            raise NotAvailable()
 
         self._fingerprint = fp
-        self._expiration = ttl + time.time()
+        self._expiration = time.time() + ttl
 
     def _read_schema(self):
         self._file.truncate(0)
-- 
2.7.4