Browse Source

[2225_statistics] synchronize when accessing self._disabled

And timer cannot be updated if self._disabled is true.

Due to the review comment.
Naoki Kambe 12 years ago
parent
commit
aa70659de5
1 changed files with 29 additions and 23 deletions
  1. 29 23
      src/lib/python/isc/statistics/counter.py

+ 29 - 23
src/lib/python/isc/statistics/counter.py

@@ -277,11 +277,13 @@ class Counters():
 
 
     def disable(self):
     def disable(self):
         """disables incrementing/decrementing counters"""
         """disables incrementing/decrementing counters"""
-        self._disabled = True
+        with self._rlock:
+            self._disabled = True
 
 
     def enable(self):
     def enable(self):
         """enables incrementing/decrementing counters"""
         """enables incrementing/decrementing counters"""
-        self._disabled = False
+        with self._rlock:
+            self._disabled = False
 
 
     def inc(self, *args):
     def inc(self, *args):
         """A incrementer for per-zone counter. Locks the thread
         """A incrementer for per-zone counter. Locks the thread
@@ -291,8 +293,8 @@ class Counters():
         file."""
         file."""
         identifier = '/'.join(args)
         identifier = '/'.join(args)
         step = 1
         step = 1
-        if self._disabled: return
         with self._rlock:
         with self._rlock:
+            if self._disabled: return
             _inc_counter(self._statistics._data,
             _inc_counter(self._statistics._data,
                          self._statistics._spec,
                          self._statistics._spec,
                          identifier, step)
                          identifier, step)
@@ -305,8 +307,8 @@ class Counters():
         file."""
         file."""
         identifier = '/'.join(args)
         identifier = '/'.join(args)
         step = -1
         step = -1
-        if self._disabled: return
         with self._rlock:
         with self._rlock:
+            if self._disabled: return
             _inc_counter(self._statistics._data,
             _inc_counter(self._statistics._data,
                          self._statistics._spec,
                          self._statistics._spec,
                          identifier, step)
                          identifier, step)
@@ -322,7 +324,9 @@ class Counters():
         """Sets the value returned from _start_timer() as a value of
         """Sets the value returned from _start_timer() as a value of
         the identifier in the self._start_time which is dict-type"""
         the identifier in the self._start_time which is dict-type"""
         identifier = '/'.join(args)
         identifier = '/'.join(args)
-        isc.cc.data.set(self._start_time, identifier, _start_timer())
+        with self._rlock:
+            if self._disabled: return
+            isc.cc.data.set(self._start_time, identifier, _start_timer())
 
 
     def stop(self, *args):
     def stop(self, *args):
         """Sets duration time between corresponding time in
         """Sets duration time between corresponding time in
@@ -333,24 +337,26 @@ class Counters():
         nothing. But in case of stopping the time which isn't defined
         nothing. But in case of stopping the time which isn't defined
         in the spec file, it raises DataNotFoundError"""
         in the spec file, it raises DataNotFoundError"""
         identifier = '/'.join(args)
         identifier = '/'.join(args)
-        try:
-            start_time = isc.cc.data.find(self._start_time,
-                                          identifier)
-        except isc.cc.data.DataNotFoundError:
-            # do not set the end time if the timer isn't started
-            pass
-        else:
-            # set the end time
-            _stop_timer(
-                start_time,
-                self._statistics._data,
-                self._statistics._spec,
-                identifier)
-            # delete the started timer
-            del isc.cc.data.find(
-                self._start_time,
-                '/'.join(identifier.split('/')[0:-1]))\
-                [identifier.split('/')[-1]]
+        with self._rlock:
+            if self._disabled: return
+            try:
+                start_time = isc.cc.data.find(self._start_time,
+                                              identifier)
+            except isc.cc.data.DataNotFoundError:
+                # do not set the end time if the timer isn't started
+                pass
+            else:
+                # set the end time
+                _stop_timer(
+                    start_time,
+                    self._statistics._data,
+                    self._statistics._spec,
+                    identifier)
+                # delete the started timer
+                del isc.cc.data.find(
+                    self._start_time,
+                    '/'.join(identifier.split('/')[0:-1]))\
+                    [identifier.split('/')[-1]]
 
 
     def dump_statistics(self):
     def dump_statistics(self):
         """Calculates an entire server counts, and returns statistics
         """Calculates an entire server counts, and returns statistics