def __getitem__(self, name): """ __getitem__(self,name) If name is a string, it is interpreted as a machine_id, and it returns the machine with that ID (or a CoordMachineNotFound exception). If name is a CoordAddress, it returns the node pointed by the address. If the CoordAddress points a machine, it returns the machine, if the CoordAddress points a instance, it returns the instance, if the CoordAddress points a server, it returns the server. It can raise any CoordNodeNotFound exception (CoordMachineNotFound, CoordInstanceNotFound, CoordServerNotFound) """ if type(name) is str: key = name elif isinstance(name, CoordAddress.CoordAddress): key = name.machine_id else: raise CoordErrors.CoordInvalidKey( "Invalid key: %s. Only strings and CoordAddress are allowed" % name) self._machines_read_lock.acquire() try: if self._machines.has_key(key): machine = self._machines[key] else: raise CoordErrors.CoordMachineNotFound('Machine %s not found' % name) finally: self._machines_read_lock.release() if type(name) is str or name.is_machine(): return machine else: #name is a CoordAddress aiming instance or server return machine[name]
def __getitem__(self, name): if type(name) is str: key = name elif isinstance(name, CoordAddress.CoordAddress) and name.is_server(): key = name.server_id else: raise CoordErrors.CoordInvalidKey( "Invalid key: %s. Only strings and server CoordAddress are allowed" % name) if self._servers.has_key(key): return self._servers[key] else: raise CoordErrors.CoordServerNotFound('server %s not found in %s' % (name, self.name))
def set_status(self, machine_name, instance_name, server_name, new_status): if not self._machines.has_key(machine_name): raise CoordErrors.CoordMachineNotFound('machine %s not found' % machine_name) self._machines[machine_name]._set_status(instance_name, server_name, new_status) self._increase_version()
def append_accesses(self, machine_name, instance_name, server_name, accesses): if not self._machines.has_key(machine_name): raise CoordErrors.CoordMachineNotFound('machine %s not found' % machine_name) self._machines[machine_name]._append_accesses(instance_name, server_name, accesses) self._increase_version()
def wrapper(self, *args, **kargs): self._map_read_lock.acquire() try: if self._map is None: raise CoordErrors.CoordMapNotInitialized( 'Map not initialized at %s' % self) finally: self._map_read_lock.release() return func(self, *args, **kargs)
def _add_new_server(self, instance_name, server_name, server_type, accesses, restrictions=()): if not self._instances.has_key(instance_name): raise CoordErrors.CoordInstanceNotFound( "Instance %s not found. Couldn't add server" % instance_name) self._instances[instance_name]._add_new_server(server_name, server_type, accesses, restrictions) self._increase_version()
def add_new_server(self, machine_name, instance_name, server_name, server_type, accesses, restrictions=()): if not self._machines.has_key(machine_name): #TODO: testme (and all exceptions in this file) raise CoordErrors.CoordMachineNotFound( "Machine %s not found. Couldn't add instance" % machine_name) self._machines[machine_name]._add_new_server(instance_name, server_name, server_type, accesses, restrictions) self._increase_version()
def __getitem__(self, name): if type(name) is str: key = name elif isinstance(name, CoordAddress.CoordAddress) and (name.is_instance() or name.is_server()): key = name.instance_id else: raise CoordErrors.CoordInvalidKey( "Invalid key: %s. Only strings and server or instance CoordAddress are allowed" % name) self._instances_read_lock.acquire() try: if self._instances.has_key(key): instance = self._instances[key] else: raise CoordErrors.CoordInstanceNotFound( 'Instance %s not found in %s' % (name, self.name)) finally: self._instances_read_lock.release() if type(name) is str or name.is_instance(): return instance else: #name is a CoordAddress aiming a server return instance[name]
def get_servers(self, original_server, server_type, restriction=None): """ get_servers(original_server,server_type) -> iterator original_server is an instance of CoordServer server_type is a member of an enumerator listing the types of server Provided a CoordServer which needs to talk with a server of type server_type, the CoordinationMap will provide an iterator which will look from the closest to the furthest server which matches server_type. The point is that if in the same instance of the original_server we have a server of server_type, that will be the best option, and nobody needs to look for anything better. But, if when trying to contact this server, it has too much load or whatever, the .next() will look for the next closest node, and so on. The result is an iterator of voodoo.gen.coordinator.Access.Network, which will provide the way to connect to that network. """ if original_server.address is None: raise CoordErrors.CoordInvalidServer( "Can't search original_server's address: %s" % original_server) instance_address = original_server.address.get_instance_address() instance = self[instance_address] # The first place to look for is in the same instance for i in instance.get_servers(): if i.server_type == server_type: if i.address != original_server.address: if restriction is None or restriction == ( ) or i.can_handle(restriction): networks = original_server.can_connect(i) if len(networks) > 0: yield i, networks # The next place to look for is in the same machine machine_address = original_server.address.get_machine_address() machine = self[machine_address] # TODO: This could be optimized so that when checking another # instance and a server is added to a walked instance, the # new server would be checked # Let's look for all the instances in the same machine # (except for the instance we have just checked) for i in machine.get_instances(): if i != instance: for j in i.get_servers(): if j.server_type == server_type: if restriction is None or restriction == ( ) or j.can_handle(restriction): networks = original_server.can_connect(j) if len(networks) > 0: yield j, networks # The next place to look for is in other machine for i in self.get_machines(): if i != machine: for j in i.get_instances(): for k in j.get_servers(): if k.server_type == server_type: if restriction is None or restriction == ( ) or k.can_handle(restriction): networks = original_server.can_connect(k) if len(networks) > 0: yield k, networks
def add_new_instance(self, machine_name, instance_name): if not self._machines.has_key(machine_name): raise CoordErrors.CoordMachineNotFound( "Machine %s not found. Couldn't add instance" % machine_name) self._machines[machine_name]._add_new_instance(instance_name) self._increase_version()
def _set_status(self, instance_name, server_name, new_status): if not self._instances.has_key(instance_name): raise CoordErrors.CoordInstanceNotFound('Instance %s not found' % instance_name) self._instances[instance_name]._set_status(server_name, new_status) self._increase_version()
def _append_accesses(self, instance_name, server_name, accesses): if not self._instances.has_key(instance_name): raise CoordErrors.CoordInstanceNotFound('Instance %s not found' % instance_name) self._instances[instance_name]._append_accesses(server_name, accesses) self._increase_version()
def _set_status(self, server_name, new_status): if not self._servers.has_key(server_name): raise CoordErrors.CoordServerNotFound('Server %s not found' % server_name) self._servers[server_name]._set_status(new_status) self._increase_version()
def _append_accesses(self, server_name, accesses): if not self._servers.has_key(server_name): raise CoordErrors.CoordServerNotFound('Server %s not found' % server_name) self._servers[server_name]._append_accesses(accesses) self._increase_version()