def init_dataset_number(self): from ambry.identity import Identity, DatasetNumber, NumberServer try: ns = NumberServer(**self.group('numbers')) ds = ns.next() except Exception as e: from ..util import get_logger logger = get_logger(__name__) logger.error("Failed to get number from number sever; need to use self assigned number: {}" .format(e.message)) raise self.identity['id'] = str(ds) ident = Identity.from_dict(self.identity) ident._on = ds.rev(self.identity.revision) self.rewrite(**dict( identity=ident.ident_dict, names=ident.names_dict ))
def test_number_service(self): ## For this test, setup these access keys in the ## Redis Server: ## ## redis-cli set assignment_class:test-ac-authoritative authoritative ## redis-cli set assignment_class:test-ac-registered registered ## redis-cli set assignment_class:fe78d179-8e61-4cc5-ba7b-263d8d3602b9 unregistered from ambry.identity import NumberServer from ambry.run import get_runconfig from ambry.dbexceptions import ConfigurationError rc = get_runconfig() try: ng = rc.service('numbers') except ConfigurationError: return # You'll need to run a local service at this address host = "numbers" port = 7977 unregistered_key = 'fe78d179-8e61-4cc5-ba7b-263d8d3602b9' ns = NumberServer(host=host, port=port, key='test-ac-registered') n = ns.next() self.assertEqual(6,len(str(n))) # Next request is authoritative, so no need to sleep here. ns = NumberServer(host=host, port=port, key='test-ac-authoritative') n = ns.next() self.assertEqual(4,len(str(n))) ns.sleep() # Avoid being rate limited # Override to use a local numbers server: ns = NumberServer(host=host, port=port, key= unregistered_key) n = ns.next() self.assertEqual(8,len(str(n))) n1 = ns.find('foobar') self.assertEquals(str(n1), str(ns.find('foobar'))) self.assertEquals(str(n1), str(ns.find('foobar')))
def x_test_number_service(self): # For this test, setup these access keys in the # Redis Server: # # redis-cli set assignment_class:test-ac-authoritative authoritative # redis-cli set assignment_class:test-ac-registered registered # redis-cli set assignment_class:fe78d179-8e61-4cc5-ba7b-263d8d3602b9 unregistered from ambry.run import get_runconfig from ambry.dbexceptions import ConfigurationError rc = get_runconfig() try: # ng = rc.service('numbers') rc.service('numbers') except ConfigurationError: return # You'll need to run a local service at this address host = "numbers" port = 7977 unregistered_key = 'fe78d179-8e61-4cc5-ba7b-263d8d3602b9' ns = NumberServer(host=host, port=port, key='test-ac-registered') n = next(ns) self.assertEqual(6, len(str(n))) # Next request is authoritative, so no need to sleep here. ns = NumberServer(host=host, port=port, key='test-ac-authoritative') n = next(ns) self.assertEqual(4, len(str(n))) ns.sleep() # Avoid being rate limited # Override to use a local numbers server: ns = NumberServer(host=host, port=port, key=unregistered_key) n = next(ns) self.assertEqual(8, len(str(n))) n1 = ns.find('foobar') self.assertEqual(str(n1), str(ns.find('foobar'))) self.assertEqual(str(n1), str(ns.find('foobar')))
def number(self, assignment_class=None, namespace='d'): """ Return a new number. :param assignment_class: Determines the length of the number. Possible values are 'authority' (3 characters) , 'registered' (5) , 'unregistered' (7) and 'self' (9). Self assigned numbers are random and acquired locally, while the other assignment classes use the number server defined in the configuration. If None, then look in the number server configuration for one of the class keys, starting with the longest class and working to the shortest. :param namespace: The namespace character, the first character in the number. Can be one of 'd', 'x' or 'b' :return: """ if assignment_class == 'self': # When 'self' is explicit, don't look for number server config return str(DatasetNumber()) elif assignment_class is None: try: nsconfig = self.services['numbers'] except ConfigurationError: # A missing configuration is equivalent to 'self' self.logger.error( 'No number server configuration; returning self assigned number' ) return str(DatasetNumber()) for assignment_class in ('self', 'unregistered', 'registered', 'authority'): if assignment_class + '-key' in nsconfig: break # For the case where the number configuratoin references a self-assigned key if assignment_class == 'self': return str(DatasetNumber()) else: try: nsconfig = self.services['numbers'] except ConfigurationError: raise ConfigurationError('No number server configuration') if assignment_class + '-key' not in nsconfig: raise ConfigurationError( 'Assignment class {} not number server config'.format( assignment_class)) try: key = nsconfig[assignment_class + '-key'] config = { 'key': key, 'host': nsconfig['host'], 'port': nsconfig.get('port', 80) } ns = NumberServer(**config) n = str(next(ns)) self.logger.info('Got number from number server: {}'.format(n)) except HTTPError as e: self.logger.error( 'Failed to get number from number server for key: {}'.format( key, e.message)) self.logger.error( 'Using self-generated number. There is no problem with this, ' 'but they are longer than centrally generated numbers.') n = str(DatasetNumber()) return n