Ejemplo n.º 1
0
 async def copy_to(self, to_node: int, interval: Interval):
     """
     Copy the keys of an interval from our persistant storage to the `to_node`.
     """
     trace_log("KeyTransferer ", self.chord_addr,
               " : Starting copy of keys from myself to ", to_node)
     all_keys = await self.persistant_storage.get_keys()
     keys_to_transfer = [
         key for key in all_keys if interval.in_interval(hash_key(key))
     ]
     dict_to_transfer = {}
     for key in keys_to_transfer:
         dict_to_transfer[key] = await self.persistant_storage.get(key)
     await self.copy_key_values_to(to_node, interval, dict_to_transfer)
Ejemplo n.º 2
0
    async def handle_transfer_keys_to_me(self, msg: Message):
        """
        Handle a request to transfer keys to me.
        """
        if self.router == None:
            return
        interval, data_dict, copy_node, receiving_node = msg.get_args(
            ['interval', 'data_dict', 'copy_node', 'receiving_node'])
        interval = parse_interval_str(interval)
        receiving_node = int(receiving_node)
        copy_node = int(copy_node)
        if debug_log_key_transfers:
            debug_log("KeyTransferer", self.chord_addr,
                      ": Handling transfer keys from",
                      copy_node, "to", self.chord_addr, "of interval",
                      interval.to_string(), "current-pred",
                      self.router.predecessor, " succ ", self.router.successor)

        data_to_store = None
        # Check that we should recieve these and that this is in fact intended for us.
        if (self.chord_addr == self.router.successor
                or self.router.predecessor == None):
            # These are intended for us, because we control everything
            # Store the data on disk
            data_to_store = data_dict
        else:
            # assert(interval.top == self.chord_addr and interval.top_closed)
            print("interval is", interval, "predecessor",
                  self.router.predecessor)
            if (interval.in_interval(self.router.predecessor)):
                # This means the whole interval isn't for us.
                # Take out what's ours and foward what ever else there is
                our_key_range = Interval(self.chord_addr, True,
                                         self.router.successor, False)
                our_keys = set(key for key in data_dict.keys()
                               if our_key_range.in_interval(hash_key(key)))
                dict_that_is_ours = dict(
                    {key: data_dict[key]
                     for key in our_keys})

                other_keys = set(data_dict.keys()).difference(our_keys)
                dict_that_isnt_ours = {
                    key: data_dict[key]
                    for key in other_keys
                }
                # Assume we are the end of the interval, then we need to pass it back wards.
                other_range = Interval(interval.bottom, interval.bottom_closed,
                                       self.router.predecessor, True)
                # Pass along the rest of the interval
                await self.copy_key_values_to(self.router.predecessor,
                                              other_range, dict_that_isnt_ours)
                data_to_store = dict_that_is_ours
            else:
                # This means we are replicating
                data_to_store = data_dict

        # Store the data on disk
        debug_log("KeyTransferer", self.chord_addr,
                  ": Handling transfer keys from",
                  copy_node, "to", self.chord_addr, "of interval",
                  interval.to_string(), "current-pred",
                  self.router.predecessor, " succ ", self.router.successor,
                  "data", data_to_store)
        await self.persistant_storage.add_dict(data_to_store)

        # Confirm what we stored.
        self.send_confirm_transfer(copy_node, interval, data_dict)