set svn:keywords property for proper keywords expansion
[plcapi.git] / PLC / Methods / RefreshPeer.py
index b72eccb..40fb1e2 100644 (file)
@@ -1,6 +1,7 @@
 #
 # Thierry Parmentelat - INRIA
 # 
+# $Id$
 
 import time
 
@@ -20,11 +21,14 @@ from PLC.Nodes import Node, Nodes
 from PLC.SliceInstantiations import SliceInstantiations
 from PLC.Slices import Slice, Slices
 
+verbose=False
+
 class RefreshPeer(Method):
     """
-    Fetches node and slice data from the specified peer and caches it
-    locally; also deletes stale entries. Returns 1 if successful,
-    faults otherwise.
+    Fetches site, node, slice, person and key data from the specified peer
+    and caches it locally; also deletes stale entries.
+    Upon successful completion, returns a dict reporting various timers.
+    Faults otherwise.
     """
 
     roles = ['admin']
@@ -52,9 +56,12 @@ class RefreshPeer(Method):
 
         # Get peer data
         start = time.time()
+       print >>log, 'Issuing GetPeerData'
         peer_tables = peer.GetPeerData()
         timers['transport'] = time.time() - start - peer_tables['db_time']
         timers['peer_db'] = peer_tables['db_time']
+        if verbose:
+            print >>log, 'GetPeerData returned -> db=%d transport=%d'%(timers['peer_db'],timers['transport'])
 
         def sync(objects, peer_objects, classobj):
             """
@@ -66,13 +73,16 @@ class RefreshPeer(Method):
             keyed on their foreign identifiers.
             """
 
+            if verbose:
+                print >>log, 'Entering sync on',classobj(self.api).__class__.__name__
+
             synced = {}
 
             # Delete stale objects
             for peer_object_id, object in objects.iteritems():
                 if peer_object_id not in peer_objects:
                     object.delete(commit = False)
-                    print classobj, "object %d deleted" % object[object.primary_key]
+                    print >> log, peer['peername'],classobj(self.api).__class__.__name__, object[object.primary_key],"deleted" 
 
             # Add/update new/existing objects
             for peer_object_id, peer_object in peer_objects.iteritems():
@@ -121,8 +131,10 @@ class RefreshPeer(Method):
                 synced[peer_object_id] = object
 
                 if dbg:
-                    print >> log, peer['peername'], classobj(self.api).__class__.__name__, \
-                       object[object.class_key], object[object.primary_key], dbg
+                    print >> log, peer['peername'], classobj(self.api).__class__.__name__, object[object.primary_key], dbg
+
+            if verbose:
+                print >>log, 'Exiting sync on',classobj(self.api).__class__.__name__
 
             return synced
 
@@ -132,6 +144,8 @@ class RefreshPeer(Method):
 
         start = time.time()
 
+       print >>log, 'Dealing with Sites'
+
         # Compare only the columns returned by the GetPeerData() call
         if peer_tables['Sites']:
             columns = peer_tables['Sites'][0].keys()
@@ -158,6 +172,8 @@ class RefreshPeer(Method):
         # XXX Synchronize foreign key types
         #
 
+       print >>log, 'Dealing with Keys'
+
         key_types = KeyTypes(self.api).dict()
 
         #
@@ -202,6 +218,8 @@ class RefreshPeer(Method):
 
         start = time.time()
 
+       print >>log, 'Dealing with Persons'
+
         # Compare only the columns returned by the GetPeerData() call
         if peer_tables['Persons']:
             columns = peer_tables['Persons'][0].keys()
@@ -210,6 +228,11 @@ class RefreshPeer(Method):
 
         # Keyed on foreign person_id
         old_peer_persons = Persons(self.api, {'peer_id': peer_id}, columns).dict('peer_person_id')
+
+       # artificially attach the persons returned by GetPeerData to the new peer 
+       # this is because validate_email needs peer_id to be correct when checking for duplicates 
+       for person in peer_tables['Persons']: 
+           person['peer_id']=peer_id
         persons_at_peer = dict([(peer_person['person_id'], peer_person) \
                                 for peer_person in peer_tables['Persons']])
 
@@ -219,7 +242,8 @@ class RefreshPeer(Method):
         peer_persons = sync(old_peer_persons, persons_at_peer, Person)
 
        # transcoder : retrieve a local key_id from a peer_key_id
-       key_transcoder = dict ( [ (key['key_id'],key['peer_key_id']) for key in peer_keys.values()])
+       key_transcoder = dict ( [ (key['key_id'],peer_key_id) \
+                                 for peer_key_id,key in peer_keys.iteritems()])
 
         for peer_person_id, person in peer_persons.iteritems():
             # Bind any newly cached users to peer
@@ -233,12 +257,13 @@ class RefreshPeer(Method):
             peer_person = persons_at_peer[peer_person_id]
             
             # Foreign keys currently belonging to the user
-           old_person_key_ids = [key_transcoder[key_id] for key_id in person['key_ids']]
+           old_person_key_ids = [key_transcoder[key_id] for key_id in person['key_ids'] \
+                                 if key_transcoder[key_id] in peer_keys]
 
             # Foreign keys that should belong to the user
            # this is basically peer_person['key_ids'], we just check it makes sense 
            # (e.g. we might have failed importing it)
-           person_key_ids = [ x for x in peer_person['key_ids'] if x in peer_keys]
+           person_key_ids = [ key_id for key_id in peer_person['key_ids'] if key_id in peer_keys]
 
             # Remove stale keys from user
            for key_id in (set(old_person_key_ids) - set(person_key_ids)):
@@ -264,6 +289,8 @@ class RefreshPeer(Method):
 
         start = time.time()
 
+       print >>log, 'Dealing with Nodes'
+
         # Compare only the columns returned by the GetPeerData() call
         if peer_tables['Nodes']:
             columns = peer_tables['Nodes'][0].keys()
@@ -336,6 +363,8 @@ class RefreshPeer(Method):
 
         start = time.time()
 
+       print >>log, 'Dealing with Slices'
+
         # Compare only the columns returned by the GetPeerData() call
         if peer_tables['Slices']:
             columns = peer_tables['Slices'][0].keys()
@@ -371,8 +400,10 @@ class RefreshPeer(Method):
         peer_slices = sync(old_peer_slices, slices_at_peer, Slice)
 
        # transcoder : retrieve a local node_id from a peer_node_id
-       node_transcoder = dict ( [ (node['node_id'],node['peer_node_id']) for node in peer_nodes.values()])
-       person_transcoder = dict ( [ (person['person_id'],person['peer_person_id']) for person in peer_persons.values()])
+       node_transcoder = dict ( [ (node['node_id'],peer_node_id) \
+                                  for peer_node_id,node in peer_nodes.iteritems()])
+       person_transcoder = dict ( [ (person['person_id'],peer_person_id) \
+                                    for peer_person_id,person in peer_persons.iteritems()])
 
         for peer_slice_id, slice in peer_slices.iteritems():
             # Bind any newly cached foreign slices to peer
@@ -387,10 +418,11 @@ class RefreshPeer(Method):
             peer_slice = slices_at_peer[peer_slice_id]
 
             # Nodes that are currently part of the slice
-           old_slice_node_ids = [ node_transcoder[node_id] for node_id in slice['node_ids']]
+           old_slice_node_ids = [ node_transcoder[node_id] for node_id in slice['node_ids'] \
+                                  if node_transcoder[node_id] in peer_nodes]
 
             # Nodes that should be part of the slice
-           slice_node_ids = [ x for x in peer_slice['node_ids'] if x in peer_nodes]
+           slice_node_ids = [ node_id for node_id in peer_slice['node_ids'] if node_id in peer_nodes]
 
             # Remove stale nodes from slice
             for node_id in (set(old_slice_node_ids) - set(slice_node_ids)):
@@ -406,10 +438,22 @@ class RefreshPeer(Method):
             # by hand, are removed. In other words, don't do this.
 
             # Foreign users that are currently part of the slice
-           old_slice_person_ids = [ person_transcoder[person_id] for person_id in slice['person_ids']]
+           #old_slice_person_ids = [ person_transcoder[person_id] for person_id in slice['person_ids'] \
+           #                if person_transcoder[person_id] in peer_persons]
+           # An issue occurred with a user who registered on both sites (same email)
+           # So the remote person could not get cached locally
+           # The one-line map/filter style is nicer but ineffective here
+           old_slice_person_ids = []
+           for person_id in slice['person_ids']:
+               if not person_transcoder.has_key(person_id):
+                   print >> log, 'WARNING : person_id %d in %s not transcodable (1) - skipped'%(person_id,slice['name'])
+               elif person_transcoder[person_id] not in peer_persons:
+                   print >> log, 'WARNING : person_id %d in %s not transcodable (2) - skipped'%(person_id,slice['name'])
+               else:
+                   old_slice_person_ids += [person_transcoder[person_id]]
 
             # Foreign users that should be part of the slice
-           slice_person_ids = [ x for x in peer_slice['person_ids'] if x in peer_persons ]
+           slice_person_ids = [ person_id for person_id in peer_slice['person_ids'] if person_id in peer_persons ]
 
             # Remove stale users from slice
             for person_id in (set(old_slice_person_ids) - set(slice_person_ids)):