Example #1
0
 async def upload_to_reflector(self, host: str,
                               port: int) -> typing.List[str]:
     sent = []
     protocol = StreamReflectorClient(self.blob_manager, self.descriptor)
     try:
         await self.loop.create_connection(lambda: protocol, host, port)
         await protocol.send_handshake()
         sent_sd, needed = await protocol.send_descriptor()
         if sent_sd:
             sent.append(self.sd_hash)
         if not sent_sd and not needed:
             if not self.fully_reflected.is_set():
                 self.fully_reflected.set()
                 await self.blob_manager.storage.update_reflected_stream(
                     self.sd_hash, f"{host}:{port}")
                 return []
         we_have = [
             blob_hash for blob_hash in needed
             if blob_hash in self.blob_manager.completed_blob_hashes
         ]
         for blob_hash in we_have:
             await protocol.send_blob(blob_hash)
             sent.append(blob_hash)
     except (asyncio.TimeoutError, ValueError):
         return sent
     except ConnectionRefusedError:
         return sent
     finally:
         if protocol.transport:
             protocol.transport.close()
     if not self.fully_reflected.is_set():
         self.fully_reflected.set()
         await self.blob_manager.storage.update_reflected_stream(
             self.sd_hash, f"{host}:{port}")
     return sent
Example #2
0
    async def upload_to_reflector(self, host: str,
                                  port: int) -> typing.List[str]:
        sent = []
        protocol = StreamReflectorClient(self.blob_manager, self.descriptor)
        try:
            self.uploading_to_reflector = True
            await self.loop.create_connection(lambda: protocol, host, port)
            await protocol.send_handshake()
            sent_sd, needed = await protocol.send_descriptor()
            if sent_sd:  # reflector needed the sd blob
                sent.append(self.sd_hash)
            if not sent_sd and not needed:  # reflector already has the stream
                if not self.fully_reflected.is_set():
                    self.fully_reflected.set()
                    await self.blob_manager.storage.update_reflected_stream(
                        self.sd_hash, f"{host}:{port}")
                    return []
            we_have = [
                blob_hash for blob_hash in needed
                if blob_hash in self.blob_manager.completed_blob_hashes
            ]
            log.info(
                "we have %i/%i needed blobs needed by reflector for lbry://%s#%s",
                len(we_have), len(needed), self.claim_name, self.claim_id)
            for i, blob_hash in enumerate(we_have):
                await protocol.send_blob(blob_hash)
                sent.append(blob_hash)
                self.reflector_progress = int((i + 1) / len(we_have) * 100)
        except (asyncio.TimeoutError, ValueError):
            return sent
        except ConnectionRefusedError:
            return sent
        except (OSError, Exception) as err:
            if isinstance(err, asyncio.CancelledError):
                log.warning("stopped uploading %s#%s to reflector",
                            self.claim_name, self.claim_id)
                raise err
            if isinstance(err, OSError):
                log.warning(
                    "stopped uploading %s#%s to reflector because blobs were deleted or moved",
                    self.claim_name, self.claim_id)
            else:
                log.exception("unexpected error reflecting %s#%s",
                              self.claim_name, self.claim_id)
            raise asyncio.CancelledError()
        finally:
            if protocol.transport:
                protocol.transport.close()
            self.uploading_to_reflector = False

        if not self.fully_reflected.is_set():
            self.fully_reflected.set()
            await self.blob_manager.storage.update_reflected_stream(
                self.sd_hash, f"{host}:{port}")
        return sent