Browse Source

[2225] changed the behavior of the counter class

It doesn't create a counter object twice if it has been already created.  It
just returns created object in that case. The global functors aren't set inside
of the counter classes, but they are set in the init method after the counter
object is firstly created.  It is intended at this change that the init method
is responsible for creation of a counter method and setting of the global
functor.
Naoki Kambe 12 years ago
parent
commit
96edf56fba

+ 21 - 17
src/lib/python/isc/statistics/counter.py

@@ -27,7 +27,14 @@ def init(spec_file_name):
     module_spec = isc.config.module_spec_from_file(spec_file_name)
     module_spec = isc.config.module_spec_from_file(spec_file_name)
     class_name = '%sCounter' % module_spec.get_module_name()
     class_name = '%sCounter' % module_spec.get_module_name()
     global _COUNTER
     global _COUNTER
+    if issubclass(_COUNTER.__class__, Counter):
+        # already loaded
+        return _COUNTER
+    # create an instance once
     _COUNTER = globals()[class_name](module_spec)
     _COUNTER = globals()[class_name](module_spec)
+    # make globals
+    globals().update(_COUNTER._to_global)
+    return _COUNTER
 
 
 # These method are dummies for notify_out in case XfroutCounter is not
 # These method are dummies for notify_out in case XfroutCounter is not
 # loaded.
 # loaded.
@@ -40,15 +47,13 @@ class Counter():
     _statistics_data = {}
     _statistics_data = {}
     _disabled = False
     _disabled = False
     _rlock = threading.RLock()
     _rlock = threading.RLock()
+    _to_global = {}
 
 
     def __init__(self, module_spec):
     def __init__(self, module_spec):
         self._statistics_spec = module_spec.get_statistics_spec()
         self._statistics_spec = module_spec.get_statistics_spec()
-        global clear_counters
-        global disable
-        global enable
-        clear_counters = self.clear_counters
-        enable = self.enable
-        disable = self.disable
+        self._to_global['clear_counters'] = self.clear_counters
+        self._to_global['disable'] = self.disable
+        self._to_global['enable'] = self.enable
 
 
     def clear_counters(self):
     def clear_counters(self):
         """clears all statistics data"""
         """clears all statistics data"""
@@ -108,10 +113,9 @@ class XfroutCounter(Counter):
         self._create_perzone_functors()
         self._create_perzone_functors()
         self._create_xfrrunning_functors()
         self._create_xfrrunning_functors()
         self._create_unixsocket_functors()
         self._create_unixsocket_functors()
-        global dump_default_statistics
-        global dump_statistics
-        dump_default_statistics = self.dump_default_statistics
-        dump_statistics = self.dump_statistics
+        self._to_global['dump_default_statistics'] = \
+            self.dump_default_statistics
+        self._to_global['dump_statistics'] = self.dump_statistics
 
 
     def _create_perzone_functors(self):
     def _create_perzone_functors(self):
         """Creates increment method of each per-zone counter based on
         """Creates increment method of each per-zone counter based on
@@ -142,8 +146,8 @@ class XfroutCounter(Counter):
                                    zone_name,
                                    zone_name,
                                    counter_name )
                                    counter_name )
                     )
                     )
-            globals()['inc_%s' % item] = __incrementer
-            globals()['get_%s' % item] = __getter
+            self._to_global['inc_%s' % item] = __incrementer
+            self._to_global['get_%s' % item] = __getter
 
 
     def _create_xfrrunning_functors(self):
     def _create_xfrrunning_functors(self):
         """Creates increment/decrement method of (a|i)xfr_running
         """Creates increment/decrement method of (a|i)xfr_running
@@ -172,9 +176,9 @@ class XfroutCounter(Counter):
                 """A getter method for xfr_running counters"""
                 """A getter method for xfr_running counters"""
                 return isc.cc.data.find(
                 return isc.cc.data.find(
                         self._statistics_data, counter_name )
                         self._statistics_data, counter_name )
-            globals()['inc_%s' % item] = __incrementer
-            globals()['dec_%s' % item] = __decrementer
-            globals()['get_%s' % item] = __getter
+            self._to_global['inc_%s' % item] = __incrementer
+            self._to_global['dec_%s' % item] = __decrementer
+            self._to_global['get_%s' % item] = __getter
 
 
     def _create_unixsocket_functors(self):
     def _create_unixsocket_functors(self):
         """Creates increment/decrement method of (a|i)xfr_running
         """Creates increment/decrement method of (a|i)xfr_running
@@ -198,8 +202,8 @@ class XfroutCounter(Counter):
                 return isc.cc.data.find(
                 return isc.cc.data.find(
                     self._statistics_data,
                     self._statistics_data,
                     'socket/unixdomain/%s' % counter_name )
                     'socket/unixdomain/%s' % counter_name )
-            globals()['inc_unixsocket_%s' % item] = __incrementer
-            globals()['get_unixsocket_%s' % item] = __getter
+            self._to_global['inc_unixsocket_%s' % item] = __incrementer
+            self._to_global['get_unixsocket_%s' % item] = __getter
 
 
     def _add_perzone_counter(self, zone):
     def _add_perzone_counter(self, zone):
         """Adds a named_set-type counter for each zone name."""
         """Adds a named_set-type counter for each zone name."""

+ 2 - 2
src/lib/python/isc/statistics/tests/counter_test.py

@@ -53,8 +53,8 @@ class TestXfroutCounter(unittest.TestCase):
             xfrout.SPECFILE_LOCATION)
             xfrout.SPECFILE_LOCATION)
         self._statistics_spec = \
         self._statistics_spec = \
             self._module_spec.get_statistics_spec()
             self._module_spec.get_statistics_spec()
-        counter.init(xfrout.SPECFILE_LOCATION)
-        self.xfrout_counter = counter._COUNTER
+        self.xfrout_counter = \
+            counter.init(xfrout.SPECFILE_LOCATION)
         self._entire_server    = self.xfrout_counter._entire_server
         self._entire_server    = self.xfrout_counter._entire_server
         self._perzone_prefix   = self.xfrout_counter._perzone_prefix
         self._perzone_prefix   = self.xfrout_counter._perzone_prefix
         self._xfrrunning_names = self.xfrout_counter._xfrrunning_names
         self._xfrrunning_names = self.xfrout_counter._xfrrunning_names