def exposed_replicate(self, blk_id, dst): try: opath = path.join(self._data_dir, blk_id) conn = connect(dst) conn.root.write(blk_id, open(opath).read()) logging.info("Successfully replicated block %s to %s" % (blk_id, dst)) except: logging.error("Failed to replicate block %s to %s" % (blk_id, dst))
def cat(self, file): block_info = self.conn.root.fetch_metadata(file) for blk_id in block_info: if len(block_info[blk_id]) > 0: host = random.sample(block_info[blk_id], 1)[0] data_conn = connect(host) print data_conn.root.read(blk_id) else: raise Exception("No hosts available for block %s" % file)
def start_data_service(config, port): data_dir = path.join(path.abspath(config['dataserver.root.dir']), 'storage') id_file = path.join(path.abspath(config['dataserver.root.dir']), 'id') nameserver_address = config['nameserver.address'] dataserver_address = cat_host(gethostname(), port) mkdirp(data_dir) if os.path.exists(id_file): id = open(id_file, 'r').read() _refresh_blocks(data_dir) else: nameserver_conn = connect(nameserver_address) id = nameserver_conn.root.new_ds_id(dataserver_address) open(id_file, 'w').write(id) logging.info("ID is %s" % id) DataServer._id = id DataServer._data_dir = data_dir DataServer._config = config t = threading.Thread(target=start_rpyc_dataserver, args=[port]) t.daemon = True t.start() nameserver_conn = connect(nameserver_address) nameserver_conn.root.register(id, dataserver_address) nameserver_conn.close() dataserver_conn = connect(dataserver_address) try: while t.isAlive(): dataserver_conn.root.send_heartbeat() dataserver_conn.root.send_block_report() t.join(3) except: logging.info("Caught exception, unregistering") nameserver_conn = connect(nameserver_address) nameserver_conn.root.unregister(id, dataserver_address)
def put(self, src, dst): bytesWritten = 0 numBytes = os.path.getsize(src) f = open(src, 'r') while bytesWritten < numBytes: block_info = self.conn.root.new_block(dst) data_conn = connect(block_info["address"]) payload = f.read(self._block_size) if payload == "": break try: data_conn.root.write(block_info["id"], payload) bytesWritten += self._block_size except: self.conn.root.rm(dst) raise Exception("Failed to write %s to %s, removed" % (src, dst))
def _add_replicas(blk_id, ids, num_replicas): if len(ids) == 0: logging.error("Unable to replicate %s because there are currently 0 replicas" % blk_id) return replicas_to_add = min(num_replicas - len(ids), _num_alive_servers() - len(ids)) if replicas_to_add == 0: logging.error("Unable to replicate %s because there is no host is available for replication" % blk_id) return logging.debug("Adding %d replicas for %s" % (replicas_to_add, blk_id)) src = dataserver_metadata[random.sample(ids, 1)[0]]["address"] new_ids = list(ids) for i in range(0, replicas_to_add): (id, metadata) = _random_dataserver(exclude=new_ids) conn = connect(src) conn.root.replicate(blk_id, metadata["address"]) logging.info("Replicating %s from %s to %s" % (blk_id, src, metadata["address"])) new_ids.append(id)
def __init__(self, *args): super(DataServer, self).__init__(args) self._ns_conn = connect(self._config['nameserver.address'])
def __init__(self, config): self.config = config self.conn = connect(config["nameserver.address"])