def shard_config (self, *args, **kwargs): """configure the service to run a shard""" payload, start_response, body = self.get_response_context(args) if self.is_config: # hey, somebody call security... start_response('403 Forbidden', [('Content-Type', 'text/plain')]) body.put("Forbidden, shard is already in a configured state\r\n") body.put(StopIteration) logging.warning("denied configuring shard %s prefix %s", self.shard_id, self.prefix) else: self.is_config = True self.prefix = payload["prefix"] self.shard_id = payload["shard_id"] # dependency injection for UnitOfWork uow_name = payload["uow_name"] logging.info("initializing unit of work based on %s", uow_name) ff = instantiate_class(uow_name) self._uow = ff.instantiate_uow(uow_name, self.prefix) start_response('200 OK', [('Content-Type', 'text/plain')]) body.put("Bokay\r\n") body.put(StopIteration) logging.info("configuring shard %s prefix %s", self.shard_id, self.prefix)
def shard_config(self, *args, **kwargs): """configure the service to run a shard""" payload, start_response, body = self.get_response_context(args) if self.is_config: # hey, somebody call security... start_response('403 Forbidden', [('Content-Type', 'text/plain')]) body.put("Forbidden, shard is already in a configured state\r\n") body.put(StopIteration) logging.warning("denied configuring shard %s prefix %s", self.shard_id, self.prefix) else: self.is_config = True self.prefix = payload["prefix"] self.shard_id = payload["shard_id"] # dependency injection for UnitOfWork uow_name = payload["uow_name"] logging.info("initializing unit of work based on %s", uow_name) ff = instantiate_class(uow_name) self._uow = ff.instantiate_uow(uow_name, self.prefix) start_response('200 OK', [('Content-Type', 'text/plain')]) body.put("Bokay\r\n") body.put(StopIteration) logging.info("configuring shard %s prefix %s", self.shard_id, self.prefix)
def __init__(self, uow_name, prefix): self.uow_name = uow_name self.uow_factory = instantiate_class(uow_name) self.prefix = prefix self._shard_id = None self._shard_dict = None self._hash_ring = None
def __init__ (self, uow_name, prefix): self.uow_name = uow_name self.uow_factory = instantiate_class(uow_name) self.prefix = prefix self._shard_id = None self._shard_dict = None self._hash_ring = None
def __init__(self, uow_name, prefix="/tmp/exelixi"): """initialize the system parameters, which represent operational state""" self.uuid = uuid1().hex self.prefix = prefix + "/" + self.uuid logging.info("prefix: %s", self.prefix) # dependency injection for UnitOfWork self.uow_name = uow_name logging.info("initializing unit of work based on %s", uow_name) ff = instantiate_class(self.uow_name) self._uow = ff.instantiate_uow(self.uow_name, self.prefix) self._shard_assoc = None self._ring = None
def __init__ (self, uow_name, prefix="/tmp/exelixi"): """initialize the system parameters, which represent operational state""" self.uuid = uuid1().hex self.prefix = prefix + "/" + self.uuid logging.info("prefix: %s", self.prefix) # dependency injection for UnitOfWork self.uow_name = uow_name logging.info("initializing unit of work based on %s", uow_name) ff = instantiate_class(self.uow_name) self._uow = ff.instantiate_uow(self.uow_name, self.prefix) self._shard_assoc = None self._ring = None
# add the child Individual to the Population # failure semantics: ignore, the count will rebalance over the hash ring return pop.reify(child) if __name__=='__main__': ## test GA in standalone-mode, without distributed services # parse command line options if len(sys.argv) < 2: uow_name = "uow.UnitOfWorkFactory" else: uow_name = sys.argv[1] uow_factory = instantiate_class(uow_name) # initialize a Population of unique Individuals at generation 0 uow = uow_factory.instantiate_uow(uow_name, "/tmp/exelixi") uow.populate(uow.current_gen) fitness_cutoff = 0 # iterate N times or until a "good enough" solution is found while uow.current_gen < uow_factory.n_gen: hist = uow.get_part_hist() hist_items = map(lambda x: (float(x[0]), x[1],), sorted(hist.items(), reverse=True)) if uow.test_termination(uow.current_gen, hist_items): break fitness_cutoff = uow.get_fitness_cutoff(hist_items)
def test_termination (self, current_gen, hist_items, total_indiv): """evaluate the terminating condition for this generation and report progress""" ## NB: override this termination test # calculate a mean squared error (MSE) of fitness for a Population hist_keys = map(lambda x: x[0], hist_items) n_indiv = sum([ count for bin, count in hist_items ]) fit_mse = sum([ count * (1.0 - float(bin)) ** 2.0 for bin, count in hist_items ]) / float(n_indiv) # calculate summary stats fit_max = max(hist_keys) fit_avg = sum(hist_keys) / float(n_indiv) fit_med = self._calc_median_hist(hist_items, n_indiv) # report the progress for one generation gen_report = "gen\t%d\tsize\t%d\ttotal\t%d\tmse\t%.2e\tmax\t%.2e\tmed\t%.2e\tavg\t%.2e" % (current_gen, n_indiv, total_indiv, fit_mse, fit_max, fit_med, fit_avg) print gen_report logging.info(gen_report) logging.debug(filter(lambda x: x[1] > 0, hist_items)) # stop when a "good enough" solution is found return (fit_mse <= self.term_limit) or (total_indiv >= self.max_indiv) if __name__=='__main__': # a simple test uow_name = "uow.UnitOfWorkFactory" uow = instantiate_class(uow_name) print uow