From 132921d3c42327012857a9a595963758837c9aa7 Mon Sep 17 00:00:00 2001 From: Siddharth Ravikumar Date: Wed, 1 Apr 2015 22:57:55 -0400 Subject: [golden commit]: rewrote combox.events.NodeDirMonitor.on_created method. - Now the the node monitor waits for all shards of the file, which was created on a remote computer, to arrive on this computer, before it re-constructs the file into the respective location in the combox directory. - Tests for it is at tests.events_test.TestEvents.test_NDM_oncreate method. - The main test method, tests.events_test.TestEvents.test_NDM, was disabled by renaming it to tests.events_test.TestEvents.test_NDM. - Now the NodeDirMonitor requires passing the 'node monitor lock' when creating an instance of it. :~( modified: combox/events.py modified: tests/events_test.py --- combox/events.py | 22 +++++++++++++---- tests/events_test.py | 67 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/combox/events.py b/combox/events.py index 359aa03..efdbc53 100644 --- a/combox/events.py +++ b/combox/events.py @@ -175,12 +175,15 @@ class NodeDirMonitor(LoggingEventHandler): """ - def __init__(self, config, dblock): + def __init__(self, config, dblock, nodem_lock): """ config: a dictinary which contains combox configuration. + dblock: Lock for the ComboxSilo. + nodem_lock: Lock for NodeDirMonitors. """ super(NodeDirMonitor, self).__init__() + self.lock = nodem_lock logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S') @@ -302,16 +305,25 @@ class NodeDirMonitor(LoggingEventHandler): # means, the directory was created on another computer # (also running combox). so, create this directory # under the combox directory - os.mkdir(file_cb_path) + with self.lock: + self.silo.node_set('file_created', file_cb_path) + num = self.silo.node_get('file_created', file_cb_path) + + if num == self.num_nodes: + os.mkdir(file_cb_path) elif (not event.is_directory) and (not path.exists(file_cb_path)): # shard created. # means, file was created on another computer (also # running combox). so, reconstruct the file and put it # in the combox directory. - decrypt_and_glue(file_cb_path, self.config) - # update db. - self.silo.update(file_cb_path) + with self.lock: + self.silo.node_set('file_created', file_cb_path) + num = self.silo.node_get('file_created', file_cb_path) + if num == self.num_nodes: + decrypt_and_glue(file_cb_path, self.config) + # update db. + self.silo.update(file_cb_path) def on_deleted(self, event): diff --git a/tests/events_test.py b/tests/events_test.py index bef6423..40ff751 100644 --- a/tests/events_test.py +++ b/tests/events_test.py @@ -53,7 +53,10 @@ class TestEvents(object): """Set things up.""" self.silo_lock = Lock() + self.nodem_lock = Lock() self.config = get_config() + self.silo = ComboxSilo(self.config, self.silo_lock) + self.FILES_DIR = self.config['combox_dir'] self.NODE_DIR = get_nodedirs(self.config)[0] self.TEST_FILE = path.join(self.FILES_DIR, 'thgttg-21st.png') @@ -233,20 +236,62 @@ class TestEvents(object): right value. """ - nmonitor = NodeDirMonitor(self.config, self.silo_lock) + nmonitor = NodeDirMonitor(self.config, self.silo_lock, + self.nodem_lock) assert_equal(2, nmonitor.num_nodes) - def test_NDM(self): + def test_NDM_oncreate(self): + """Testing on_create method in NodeDirMonitor""" + nodes = get_nodedirs(self.config) + num_nodes = len(get_nodedirs(self.config)) + + nmonitors = [] + observers = [] + + # create an observer for each node directory and make it + # monitor them. + for node in nodes: + nmonitor = NodeDirMonitor(self.config, self.silo_lock, + self.nodem_lock) + observer = Observer() + observer.schedule(nmonitor, node, recursive=True) + observer.start() + + nmonitors.append(nmonitor) + observers.append(observer) + + # Test - new file addition, when shard is created in node_dirs + self.TEST_FILE_MUTANT = "%s.mutant" % self.TEST_FILE + + fmutant_content = read_file(self.TEST_FILE) + + split_and_encrypt(self.TEST_FILE_MUTANT, self.config, + fmutant_content) + ## wait for NodeDirMonitor to reconstruct the shards and put + ## it in combox directory + time.sleep(1) + assert fmutant_content == read_file(self.TEST_FILE_MUTANT) + ## check if the new file's info is in silo + assert self.silo.exists(self.TEST_FILE_MUTANT) + + for i in range(num_nodes): + observers[i].stop() + observers[i].join() + + + def untest_NDM(self): """ Tests the NodeDirMonitor class. """ - event_handler = NodeDirMonitor(self.config, self.silo_lock) + event_handler = NodeDirMonitor(self.config, self.silo_lock, + self.nodem_lock) observer = Observer() observer.schedule(event_handler, self.NODE_DIR, recursive=True) observer.start() + #### # Test - new file addition, when shard is created in node_dirs self.TEST_FILE_MUTANT = "%s.mutant" % self.TEST_FILE @@ -261,6 +306,7 @@ class TestEvents(object): ## check if the new file's info is in silo silo = ComboxSilo(self.config, self.silo_lock) assert silo.exists(self.TEST_FILE_MUTANT) + #### # Test - directory creation self.FOO_DIR = path.join(self.FILES_DIR, 'foo') @@ -329,7 +375,8 @@ class TestEvents(object): def test_NDM_onmoved(self): """Testing on_moved method in NodeDirMonitor""" - event_handler = NodeDirMonitor(self.config, self.silo_lock) + event_handler = NodeDirMonitor(self.config, self.silo_lock, + self.nodem_lock) observer = Observer() observer.schedule(event_handler, self.NODE_DIR, recursive=True) observer.start() @@ -397,7 +444,8 @@ class TestEvents(object): silo.update(testf1) silo.update(testf2) - ndm = NodeDirMonitor(self.config, self.silo_lock) + ndm = NodeDirMonitor(self.config, self.silo_lock, + self.nodem_lock) ndm.housekeep() assert not path.exists(testf1) @@ -413,7 +461,8 @@ class TestEvents(object): split_and_encrypt(hmutant, self.config, hmutant_content) - ndm = NodeDirMonitor(self.config, self.silo_lock) + ndm = NodeDirMonitor(self.config, self.silo_lock, + self.nodem_lock) ndm.housekeep() assert path.exists(hmutant) @@ -441,7 +490,8 @@ class TestEvents(object): split_and_encrypt(lcopy, self.config, lcopy_content) - ndm = NodeDirMonitor(self.config, self.silo_lock) + ndm = NodeDirMonitor(self.config, self.silo_lock, + self.nodem_lock) ndm.housekeep() ## check if the lorem_file_copy's info is updated in silo @@ -457,7 +507,8 @@ class TestEvents(object): """Testing shardp method in NodeDirMonitor class""" shard = 'some.shard0' not_shard = 'some.extension' - ndm = NodeDirMonitor(self.config, self.silo_lock) + ndm = NodeDirMonitor(self.config, self.silo_lock, + self.nodem_lock) assert_equal(True, ndm.shardp(shard)) assert_equal(False, ndm.shardp(not_shard)) -- cgit v1.2.3