Parcourir la source

fix a minor typo and add some code comments

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac289@2635 e5f2f494-b856-4b98-b285-d166d9295462
Jerry il y a 14 ans
Parent
commit
20355243ac
2 fichiers modifiés avec 48 ajouts et 37 suppressions
  1. 17 8
      src/bin/zonemgr/tests/zonemgr_test.py
  2. 31 29
      src/bin/zonemgr/zonemgr.py.in

+ 17 - 8
src/bin/zonemgr/tests/zonemgr_test.py

@@ -23,6 +23,8 @@ from zonemgr import *
 
 ZONE_NAME_CLASS1 = ("sd.cn.", "IN")
 ZONE_NAME_CLASS2 = ("tw.cn", "IN")
+ZONE_NAME_CLASS3 = ("example.com", "IN")
+
 class ZonemgrTestException(Exception):
     pass
 
@@ -37,7 +39,6 @@ class MySession():
 class MyZoneMgrRefreshInfo(ZoneMgrRefreshInfo):
     def __init__(self):
         self._cc = MySession()
-        self._sock_file = UNIX_SOCKET_FILE
         self._db_file = "initdb.file"
         self._zonemgr_refresh_info = { 
          ('sd.cn.', 'IN'): {
@@ -98,7 +99,6 @@ class TestZoneMgrRefreshInfo(unittest.TestCase):
     def test_zone_not_exist(self):
         self.assertFalse(self.zoneinfo._zone_not_exist(ZONE_NAME_CLASS1))
         self.assertFalse(self.zoneinfo._zone_not_exist(ZONE_NAME_CLASS2))
-        ZONE_NAME_CLASS3 = ("example.com", "IN")
         self.assertTrue(self.zoneinfo._zone_not_exist(ZONE_NAME_CLASS3))
 
     def test_set_zone_notify_timer(self):
@@ -240,8 +240,10 @@ class TestZoneMgrRefreshInfo(unittest.TestCase):
         zone_timeout = float(self.zoneinfo._zonemgr_refresh_info[ZONE_NAME_CLASS1]["next_refresh_time"])
         current_time = time.time()
         self.assertTrue(zone_timeout <= current_time)
-        self.assertRaises(ZonemgrException, self.zoneinfo.zone_handle_notify,
+        self.assertRaises(ZonemgrException, self.zoneinfo.zone_handle_notify,\
                           "org.cn.", "127.0.0.1")
+        self.assertRaises(ZonemgrException, self.zoneinfo.zone_handle_notify,\
+                          ZONE_NAME_CLASS3, "127.0.0.1")
 
     def test_zone_refresh_success(self):
         soa_rdata = 'a.dns.cn. root.cnnic.cn. 2009073106 1800 900 2419200 21600'
@@ -263,6 +265,7 @@ class TestZoneMgrRefreshInfo(unittest.TestCase):
         self.assertTrue(time1 <= last_refresh_time)
         self.assertTrue(last_refresh_time <= time2)
         self.assertRaises(ZonemgrException, self.zoneinfo.zone_refresh_success, "org.cn.")
+        self.assertRaises(ZonemgrException, self.zoneinfo.zone_refresh_success, ZONE_NAME_CLASS3) 
 
     def test_zone_refresh_fail(self):
         soa_rdata = 'a.dns.cn. root.cnnic.cn. 2009073105 7200 3600 2419200 21600' 
@@ -276,9 +279,10 @@ class TestZoneMgrRefreshInfo(unittest.TestCase):
         self.assertTrue((time1 + 3 * 3600 / 4) <= next_refresh_time)
         self.assertTrue(next_refresh_time <= time2 + 3600)
         self.assertEqual(ZONE_OK, self.zoneinfo._zonemgr_refresh_info[ZONE_NAME_CLASS1]["zone_state"])
-        self.assertRaises(ZonemgrException, self.zoneinfo.zone_refresh_success, "org.cn.")
+        self.assertRaises(ZonemgrException, self.zoneinfo.zone_refresh_fail, "org.cn.")
+        self.assertRaises(ZonemgrException, self.zoneinfo.zone_refresh_fail, ZONE_NAME_CLASS3) 
 
-    def test_find_minimum_timeout_zone(self):
+    def test_find_need_do_refresh_zone(self):
         time1 = time.time()
         self.zoneinfo._zonemgr_refresh_info = { 
                 ("sd.cn.","IN"):{
@@ -374,9 +378,12 @@ class TestZonemgr(unittest.TestCase):
         self.zonemgr = MyZonemgr()
 
     def test_config_handler(self):
-        config_data = {"zone_name" : "sd.cn.", "zone_class" : "IN", "master" : "192.168.1.1"}
-        self.zonemgr.config_handler(config_data)
-        self.assertEqual(config_data, self.zonemgr._config_data)
+        config_data1 = {"zone_name" : "sd.cn.", "zone_class" : "IN", "master" : "192.168.1.1"}
+        self.zonemgr.config_handler(config_data1)
+        self.assertEqual(config_data1, self.zonemgr._config_data)
+        config_data2 = {"zone_name" : "sd.cn.", "port" : "53", "master" : "192.168.1.1"}
+        self.zonemgr.config_handler(config_data2)
+        self.assertEqual(config_data1, self.zonemgr._config_data)
 
     def test_get_db_file(self):
         self.assertEqual("initdb.file", self.zonemgr.get_db_file())
@@ -389,6 +396,8 @@ class TestZonemgr(unittest.TestCase):
         answer2 = ("org.cn", "IN")
         self.assertEqual(answer2, self.zonemgr._parse_cmd_params(params2, ZONE_XFRIN_SUCCESS_COMMAND))
         self.assertRaises(ZonemgrException, self.zonemgr._parse_cmd_params, params2, ZONE_NOTIFY_COMMAND)
+        params1 = {"zone_class" : "IN"}
+        self.assertRaises(ZonemgrException, self.zonemgr._parse_cmd_params, params2, ZONE_NOTIFY_COMMAND)
 
     def tearDown(self):
         pass

+ 31 - 29
src/bin/zonemgr/zonemgr.py.in

@@ -28,7 +28,6 @@ import os
 import time
 import signal
 import isc
-import struct
 import random
 import threading
 import select
@@ -43,28 +42,25 @@ from isc.config.ccsession import *
 if "B10_FROM_BUILD" in os.environ:
     SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/zonemgr"
     AUTH_SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/auth"
-    UNIX_SOCKET_FILE= os.environ["B10_FROM_BUILD"] + "/zonemgr_conn"
 else:
     PREFIX = "@prefix@"
     DATAROOTDIR = "@datarootdir@"
     SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
     AUTH_SPECFILE_PATH = SPECFILE_PATH
-    UNIX_SOCKET_FILE = "@@LOCALSTATEDIR@@/zonemgr_conn"
 
 SPECFILE_LOCATION = SPECFILE_PATH + "/zonemgr.spec"
 AUTH_SPECFILE_LOCATION = AUTH_SPECFILE_PATH + "/auth.spec"
 
 __version__ = "BIND10"
 
+# define module name
 XFRIN_MODULE_NAME = 'Xfrin'
 AUTH_MODULE_NAME = 'Auth'
+# define command name
 ZONE_XFRIN_FAILED_COMMAND = 'zone_xfrin_failed'
 ZONE_XFRIN_SUCCESS_COMMAND = 'zone_new_data_ready'
 ZONE_REFRESH_COMMAND = 'refresh'
 ZONE_NOTIFY_COMMAND = 'notify'
-#default master port
-DEFAULT_MASTER_PORT = "53"
-
 # define zone state
 ZONE_OK = 0
 ZONE_REFRESHING = 1
@@ -102,34 +98,42 @@ class ZoneMgrRefreshInfo:
         return time.time()
 
     def _set_zone_timer(self, zone_name_class, max, jitter):
+        """Set zone next refresh time."""
         self._set_zone_next_refresh_time(zone_name_class, self._get_current_time() + \
                                             self._random_jitter(max, jitter))
 
     def _set_zone_refresh_timer(self, zone_name_class):
-        """Set timer for zone refresh timeout after zone refresh success."""
+        """Set zone next refresh time after zone refresh success.
+           now + refresh*3/4 <= next_refresh_time <= now + refresh
+           """
         zone_refresh_time = float(self._get_zone_soa_rdata(zone_name_class).split(" ")[3])
         if (zone_refresh_time < LOWERBOUND_REFRESH):
             zone_refresh_time = LOWERBOUND_REFRESH
         self._set_zone_timer(zone_name_class, zone_refresh_time, (1 * zone_refresh_time) / 4)
 
     def _set_zone_retry_timer(self, zone_name_class):
-        """Set timer for zone retry timeout after zone refresh fail."""
+        """Set zone next refresh time after zone refresh fail.
+           now + retry*3/4 <= next_refresh_time <= now + retry
+           """
         zone_retry_time = float(self._get_zone_soa_rdata(zone_name_class).split(" ")[4])
         if (zone_retry_time < LOWERBOUND_RETRY):
             zone_retry_time = LOWERBOUND_RETRY
         self._set_zone_timer(zone_name_class, zone_retry_time, (1 * zone_retry_time) / 4)
 
     def _set_zone_notify_timer(self, zone_name_class):
-        """Set timer for a zone after receiving notify"""
+        """Set zone next refresh time after receiving notify
+           next_refresh_time = now 
+        """
         self._set_zone_timer(zone_name_class, 0, 0)
 
     def _zone_not_exist(self, zone_name_class):
+        """ Zone doesn't belong to zonemgr"""
         if zone_name_class in self._zonemgr_refresh_info.keys():
             return False
         return True
 
     def zone_refresh_success(self, zone_name_class):
-        """Update zone update info after zone refresh success"""
+        """Update zone info after zone refresh success"""
         if (self._zone_not_exist(zone_name_class)):
             raise ZonemgrException("[b10-zonemgr] Zone %s doesn't \
                                     belong to zonemgr" % zone_name_class[0])
@@ -140,7 +144,7 @@ class ZoneMgrRefreshInfo:
         self._set_zone_last_refresh_time(zone_name_class, self._get_current_time())
 
     def zone_refresh_fail(self, zone_name_class):
-        """Update zone update info after zone refresh fail"""
+        """Update zone info after zone refresh fail"""
         if (self._zone_not_exist(zone_name_class)):
             raise ZonemgrException("[b10-zonemgr] Zone %s doesn't \
                                     belong to zonemgr" % zone_name_class[0])
@@ -158,6 +162,7 @@ class ZoneMgrRefreshInfo:
         self._set_zone_notify_timer(zone_name_class)
 
     def _build_zonemgr_refresh_info(self):
+        """ Build zonemgr refresh info map."""
         for zone_name, zone_class in sqlite3_ds.get_zones_info(self._db_file):
             zone_info = {}
             zone_soa = sqlite3_ds.get_zone_soa(str(zone_name), self._db_file)
@@ -221,6 +226,7 @@ class ZoneMgrRefreshInfo:
         self._zonemgr_refresh_info[zone_name_class]["next_refresh_time"] = time
 
     def _send_command(self, module_name, command_name, params):
+        """Send command between modules."""
         msg = create_command(command_name, params)
         self._cc.group_sendmsg(msg, module_name)
 
@@ -241,7 +247,7 @@ class ZoneMgrRefreshInfo:
                 (not self._get_zone_notifier_master(zone_name_class))):
                 continue
 
-            # If hasn't received xfr response within xfr timeout, skip the zone
+            # If hasn't received refresh response within refresh timeout, skip the zone
             if (ZONE_REFRESHING == zone_state and
                 (self._get_zone_refresh_timeout(zone_name_class) > self._get_current_time())):
                 continue
@@ -249,10 +255,10 @@ class ZoneMgrRefreshInfo:
             # Get the zone with minimum next_refresh_time 
             if ((None == zone_need_refresh) or 
                 (self._get_zone_next_refresh_time(zone_name_class) < 
-                    self._get_zone_next_refresh_time(zone_need_refresh))):
+                 self._get_zone_next_refresh_time(zone_need_refresh))):
                 zone_need_refresh = zone_name_class
 
-            # Fine the zone need to refresh
+            # Find the zone need do refresh
             if (self._get_zone_next_refresh_time(zone_need_refresh) < self._get_current_time()):
                 break
 
@@ -260,15 +266,15 @@ class ZoneMgrRefreshInfo:
 
     
     def _do_refresh(self, zone_name_class):
-        """Do zone refresh"""
+        """Do zone refresh."""
         self._set_zone_state(zone_name_class, ZONE_REFRESHING)
         self._set_zone_refresh_timeout(zone_name_class, self._get_current_time() + MAX_TRANSFER_TIMEOUT) 
         notify_master = self._get_zone_notifier_master(zone_name_class)
-        # If has notify master, send notify command to xfrin module
+        # If the zone has notify master, send notify command to xfrin module
         if notify_master:
             param = {"zone_name" : zone_name_class[0],
                      "zone_class" : zone_name_class[1],
-                     "master" : notify_master[0]
+                     "master" : notify_master
                      }
             self._send_command(XFRIN_MODULE_NAME, ZONE_NOTIFY_COMMAND, param) 
             self._clear_zone_notifier_master(zone_name_class)
@@ -287,14 +293,15 @@ class ZoneMgrRefreshInfo:
         return False
 
     def run_timer(self):
-        """Keep track of zone timers"""
+        """Keep track of zone timers."""
         while True:
+            # Zonemgr has no zone.
             if self._zone_mgr_is_empty():
                 time.sleep(1) # A better time?
                 continue
 
             zone_need_refresh = self._find_need_do_refresh_zone()
-            # If don't get zone with minimum timeout, timer will wait LOWERBOUND_REFRESH
+            # If don't get zone with minimum next refresh time, set timer timeout = LOWERBOUND_REFRESH
             if not zone_need_refresh:
                 timeout = LOWERBOUND_REFRESH 
             else:
@@ -303,7 +310,8 @@ class ZoneMgrRefreshInfo:
                     self._do_refresh(zone_need_refresh)
                     continue
 
-            # Start timer, wait for timeout if not received notification
+            """ Wait for the socket notification for a maximum time of timeout 
+            in seconds (as float)."""
             try:
                 (rlist, wlist, xlist) = select.select([self._socket], [], [], timeout)
                 if rlist:
@@ -319,11 +327,6 @@ class ZoneMgrRefreshInfo:
                     break
 
 
-    def shutdown(self):
-        """Close socket"""
-        self._socket.close()
-
-
 def start_timer(zone_refresh_info):
     """Keep track of zone timers"""
     zone_refresh_info.run_timer()
@@ -334,8 +337,7 @@ class Zonemgr:
     def __init__(self, verbose = False):
         self._setup_session()
         self._db_file = self.get_db_file()
-        self._sock_file = UNIX_SOCKET_FILE 
-
+        # Create socket pair for communicating between main thread and zonemgr timer thread 
         self._master_socket, self._slave_scoket = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
         self._zone_refresh_info = ZoneMgrRefreshInfo(self._cc, self._db_file, self._slave_scoket)
         self._start_zone_refresh_timer()
@@ -370,7 +372,7 @@ class Zonemgr:
 
     def shutdown(self):
 
-        self._zone_refresh_info.shutdown()
+        self._slave_scoket.close()
         self._master_socket.close()
 
         self._shutdown_event.set()
@@ -417,7 +419,7 @@ class Zonemgr:
             self._lock.acquire()
             self._zone_refresh_info.zone_handle_notify(zone_name_class, master)
             self._lock.release()
-            # Send message to zonemgr timer, timer will stop waiting and check zone state
+            # Send notification to zonemgr timer thread
             self._master_socket.send(b" ")
 
         elif command == ZONE_XFRIN_SUCCESS_COMMAND: