def test_get_all_host_states(self): context = 'fake_context' topic = CONF.volume_topic self.mox.StubOutWithMock(db, 'service_get_all_by_topic') self.mox.StubOutWithMock(host_manager.LOG, 'warn') self.mox.StubOutWithMock(host_manager.utils, 'service_is_up') ret_services = fakes.VOLUME_SERVICES db.service_get_all_by_topic(context, topic).AndReturn(ret_services) host_manager.utils.service_is_up(ret_services[0]).AndReturn(True) host_manager.utils.service_is_up(ret_services[1]).AndReturn(True) host_manager.utils.service_is_up(ret_services[2]).AndReturn(True) host_manager.utils.service_is_up(ret_services[3]).AndReturn(True) host_manager.utils.service_is_up(ret_services[4]).AndReturn(True) # Disabled service host_manager.LOG.warn("volume service is down or disabled. " "(host: host5)") db.service_get_all_by_topic(context, topic).AndReturn(ret_services) host_manager.utils.service_is_up(ret_services[0]).AndReturn(True) host_manager.utils.service_is_up(ret_services[1]).AndReturn(True) host_manager.utils.service_is_up(ret_services[2]).AndReturn(True) host_manager.utils.service_is_up(ret_services[3]).AndReturn(False) # Stopped service host_manager.LOG.warn("volume service is down or disabled. " "(host: host4)") host_manager.utils.service_is_up(ret_services[4]).AndReturn(True) # Disabled service host_manager.LOG.warn("volume service is down or disabled. " "(host: host5)") self.mox.ReplayAll() self.host_manager.get_all_host_states(context) host_state_map = self.host_manager.host_state_map self.assertEqual(len(host_state_map), 4) # Check that service is up for i in xrange(4): volume_node = fakes.VOLUME_SERVICES[i] host = volume_node['host'] self.assertEqual(host_state_map[host].service, volume_node) self.host_manager.get_all_host_states(context) host_state_map = self.host_manager.host_state_map self.assertEqual(len(host_state_map), 3) for i in xrange(3): volume_node = fakes.VOLUME_SERVICES[i] host = volume_node['host'] self.assertEqual(host_state_map[host].service, volume_node)
def test_hosts_up(self): service1 = {"host": "host1"} service2 = {"host": "host2"} services = [service1, service2] self.mox.StubOutWithMock(db, "service_get_all_by_topic") self.mox.StubOutWithMock(utils, "service_is_up") db.service_get_all_by_topic(self.context, self.topic).AndReturn(services) utils.service_is_up(service1).AndReturn(False) utils.service_is_up(service2).AndReturn(True) self.mox.ReplayAll() result = self.driver.hosts_up(self.context, self.topic) self.assertEqual(result, ["host2"])
def hosts_up(self, context, topic): """Return the list of hosts that have a running service for topic.""" services = db.service_get_all_by_topic(context, topic) return [service['host'] for service in services if utils.service_is_up(service)]
def _update_host_state_map(self, context): # Get resource usage across the available volume nodes: topic = CONF.volume_topic volume_services = db.service_get_all_by_topic(context, topic, disabled=False) active_hosts = set() for service in volume_services: host = service['host'] if not utils.service_is_up(service): LOG.warn(_LW("volume service is down. (host: %s)") % host) continue capabilities = self.service_states.get(host, None) host_state = self.host_state_map.get(host) if not host_state: host_state = self.host_state_cls(host, capabilities=capabilities, service=dict( service.iteritems())) self.host_state_map[host] = host_state # update capabilities and attributes in host_state host_state.update_from_volume_capability(capabilities, service=dict( service.iteritems())) active_hosts.add(host) # remove non-active hosts from host_state_map nonactive_hosts = set(self.host_state_map.keys()) - active_hosts for host in nonactive_hosts: LOG.info( _LI("Removing non-active host: %(host)s from " "scheduler cache.") % {'host': host}) del self.host_state_map[host]
def get_all_host_states(self, context): """Returns a dict of all the hosts the HostManager knows about. Also, each of the consumable resources in HostState are pre-populated and adjusted based on data in the db. For example: {'192.168.1.100': HostState(), ...} """ # Get resource usage across the available volume nodes: topic = FLAGS.volume_topic volume_services = db.service_get_all_by_topic(context, topic) for service in volume_services: if not utils.service_is_up(service) or service['disabled']: LOG.warn(_("service is down or disabled.")) continue host = service['host'] capabilities = self.service_states.get(host, None) host_state = self.host_state_map.get(host) if host_state: # copy capabilities to host_state.capabilities host_state.update_capabilities(capabilities, dict(service.iteritems())) else: host_state = self.host_state_cls(host, capabilities=capabilities, service=dict( service.iteritems())) self.host_state_map[host] = host_state # update host_state host_state.update_from_volume_capability(capabilities) return self.host_state_map.itervalues()
def get_all_host_states(self, context): """Returns a dict of all the hosts the HostManager knows about. Also, each of the consumable resources in HostState are pre-populated and adjusted based on data in the db. For example: {'192.168.1.100': HostState(), ...} """ # Get resource usage across the available volume nodes: topic = CONF.volume_topic volume_services = db.service_get_all_by_topic(context, topic) self.host_state_map.clear() for service in volume_services: host = service['host'] if not utils.service_is_up(service) or service['disabled']: LOG.warn(_("volume service is down or disabled. " "(host: %s)") % host) continue capabilities = self.service_states.get(host, None) host_state = self.host_state_map.get(host) if host_state: # copy capabilities to host_state.capabilities host_state.update_capabilities(capabilities, dict(service.iteritems())) else: host_state = self.host_state_cls(host, capabilities=capabilities, service= dict(service.iteritems())) self.host_state_map[host] = host_state # update host_state host_state.update_from_volume_capability(capabilities) return self.host_state_map.itervalues()
def test_hosts_up(self): service1 = {'host': 'host1'} service2 = {'host': 'host2'} services = [service1, service2] self.mox.StubOutWithMock(db, 'service_get_all_by_topic') self.mox.StubOutWithMock(utils, 'service_is_up') db.service_get_all_by_topic(self.context, self.topic).AndReturn(services) utils.service_is_up(service1).AndReturn(False) utils.service_is_up(service2).AndReturn(True) self.mox.ReplayAll() result = self.driver.hosts_up(self.context, self.topic) self.assertEqual(result, ['host2'])
def _update_host_state_map(self, context): # Get resource usage across the available volume nodes: topic = CONF.volume_topic volume_services = db.service_get_all_by_topic(context, topic, disabled=False) active_hosts = set() for service in volume_services: host = service['host'] if not utils.service_is_up(service): LOG.warn(_LW("volume service is down. (host: %s)") % host) continue capabilities = self.service_states.get(host, None) host_state = self.host_state_map.get(host) if not host_state: host_state = self.host_state_cls(host, capabilities=capabilities, service= dict(service.iteritems())) self.host_state_map[host] = host_state # update capabilities and attributes in host_state host_state.update_from_volume_capability(capabilities, service= dict(service.iteritems())) active_hosts.add(host) # remove non-active hosts from host_state_map nonactive_hosts = set(self.host_state_map.keys()) - active_hosts for host in nonactive_hosts: LOG.info(_LI("Removing non-active host: %(host)s from " "scheduler cache.") % {'host': host}) del self.host_state_map[host]
def mox_host_manager_db_calls(mock, context): mock.StubOutWithMock(db, 'service_get_all_by_topic') services = [ dict(id=1, host='host1', topic='volume', disabled=False, availability_zone='zone1', updated_at=timeutils.utcnow()), dict(id=2, host='host2', topic='volume', disabled=False, availability_zone='zone1', updated_at=timeutils.utcnow()), dict(id=3, host='host3', topic='volume', disabled=False, availability_zone='zone2', updated_at=timeutils.utcnow()), dict(id=4, host='host4', topic='volume', disabled=False, availability_zone='zone3', updated_at=timeutils.utcnow()), # service on host5 is disabled dict(id=5, host='host5', topic='volume', disabled=True, availability_zone='zone4', updated_at=timeutils.utcnow()), ] db.service_get_all_by_topic(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(services)
def get_all_host_states(self, context): """Returns a dict of all the hosts the HostManager knows about. Each of the consumable resources in HostState are populated with capabilities scheduler received from RPC. For example: {'192.168.1.100': HostState(), ...} """ # Get resource usage across the available volume nodes: topic = CONF.volume_topic volume_services = db.service_get_all_by_topic(context, topic, disabled=False) active_hosts = set() for service in volume_services: host = service['host'] if not utils.service_is_up(service): LOG.warn(_("volume service is down. (host: %s)") % host) continue capabilities = self.service_states.get(host, None) host_state = self.host_state_map.get(host) if not host_state: host_state = self.host_state_cls(host, capabilities=capabilities, service=dict( service.iteritems())) self.host_state_map[host] = host_state # update capabilities and attributes in host_state host_state.update_from_volume_capability(capabilities, service=dict( service.iteritems())) active_hosts.add(host) # remove non-active hosts from host_state_map nonactive_hosts = set(self.host_state_map.keys()) - active_hosts for host in nonactive_hosts: LOG.info( _("Removing non-active host: %(host)s from " "scheduler cache.") % {'host': host}) del self.host_state_map[host] # build a pool_state map and return that map instead of host_state_map all_pools = {} for host in active_hosts: state = self.host_state_map[host] for key in state.pools: pool = state.pools[key] # use host.pool_name to make sure key is unique pool_key = '.'.join([host, pool.pool_name]) all_pools[pool_key] = pool return all_pools.itervalues()
def test_service_get_all_by_topic(self): values = [ {"host": "host1", "topic": "t1"}, {"host": "host2", "topic": "t1"}, {"host": "host4", "disabled": True, "topic": "t1"}, {"host": "host3", "topic": "t2"}, ] services = [self._create_service(vals) for vals in values] expected = services[:3] real = db.service_get_all_by_topic(self.ctxt, "t1") self._assertEqualListsOfObjects(expected, real)
def test_service_get_all_by_topic(self): values = [ {'host': 'host1', 'topic': 't1'}, {'host': 'host2', 'topic': 't1'}, {'disabled': True, 'topic': 't1'}, {'host': 'host3', 'topic': 't2'} ] services = [self._create_service(vals) for vals in values] expected = services[:2] real = db.service_get_all_by_topic(self.ctxt, 't1') self._assertEqualListsOfObjects(expected, real)
def get_all_host_states(self, context): """Returns a dict of all the hosts the HostManager knows about. Each of the consumable resources in HostState are populated with capabilities scheduler received from RPC. For example: {'192.168.1.100': HostState(), ...} """ # Get resource usage across the available volume nodes: topic = CONF.volume_topic volume_services = db.service_get_all_by_topic(context, topic, disabled=False) active_hosts = set() for service in volume_services: host = service['host'] if not utils.service_is_up(service): LOG.warn(_("volume service is down. (host: %s)") % host) continue capabilities = self.service_states.get(host, None) host_state = self.host_state_map.get(host) if not host_state: host_state = self.host_state_cls(host, capabilities=capabilities, service= dict(service.iteritems())) self.host_state_map[host] = host_state # update capabilities and attributes in host_state host_state.update_from_volume_capability(capabilities, service= dict(service.iteritems())) active_hosts.add(host) # remove non-active hosts from host_state_map nonactive_hosts = set(self.host_state_map.keys()) - active_hosts for host in nonactive_hosts: LOG.info(_LI("Removing non-active host: %(host)s from " "scheduler cache.") % {'host': host}) del self.host_state_map[host] # build a pool_state map and return that map instead of host_state_map all_pools = {} for host in active_hosts: state = self.host_state_map[host] for key in state.pools: pool = state.pools[key] # use host.pool_name to make sure key is unique pool_key = '.'.join([host, pool.pool_name]) all_pools[pool_key] = pool return all_pools.itervalues()
def test_get_all_host_states(self): context = "fake_context" topic = FLAGS.volume_topic self.mox.StubOutWithMock(db, "service_get_all_by_topic") self.mox.StubOutWithMock(host_manager.LOG, "warn") ret_services = fakes.VOLUME_SERVICES db.service_get_all_by_topic(context, topic).AndReturn(ret_services) # Disabled service host_manager.LOG.warn("service is down or disabled.") self.mox.ReplayAll() self.host_manager.get_all_host_states(context) host_state_map = self.host_manager.host_state_map self.assertEqual(len(host_state_map), 4) # Check that service is up for i in xrange(4): volume_node = fakes.VOLUME_SERVICES[i] host = volume_node["host"] self.assertEqual(host_state_map[host].service, volume_node)
def test_get_all_host_states(self): context = 'fake_context' topic = FLAGS.volume_topic self.mox.StubOutWithMock(db, 'service_get_all_by_topic') self.mox.StubOutWithMock(host_manager.LOG, 'warn') ret_services = fakes.VOLUME_SERVICES db.service_get_all_by_topic(context, topic).AndReturn(ret_services) # Disabled service host_manager.LOG.warn("service is down or disabled.") self.mox.ReplayAll() self.host_manager.get_all_host_states(context) host_state_map = self.host_manager.host_state_map self.assertEqual(len(host_state_map), 4) # Check that service is up for i in xrange(4): volume_node = fakes.VOLUME_SERVICES[i] host = volume_node['host'] self.assertEqual(host_state_map[host].service, volume_node)
def get_all_host_states(self, context): """Returns a dict of all the hosts the HostManager knows about. Each of the consumable resources in HostState are populated with capabilities scheduler received from RPC. For example: {'192.168.1.100': HostState(), ...} """ # Get resource usage across the available volume nodes: topic = CONF.volume_topic volume_services = db.service_get_all_by_topic(context, topic) active_hosts = set() for service in volume_services: host = service['host'] if not utils.service_is_up(service) or service['disabled']: LOG.warn(_("volume service is down or disabled. " "(host: %s)") % host) continue capabilities = self.service_states.get(host, None) host_state = self.host_state_map.get(host) if host_state: # copy capabilities to host_state.capabilities host_state.update_capabilities(capabilities, dict(service.iteritems())) else: host_state = self.host_state_cls(host, capabilities=capabilities, service= dict(service.iteritems())) self.host_state_map[host] = host_state # update attributes in host_state that scheduler is interested in host_state.update_from_volume_capability(capabilities) active_hosts.add(host) if context.host and context.host == host: active_hosts = set() active_hosts.add(host) break # remove non-active hosts from host_state_map nonactive_hosts = set(self.host_state_map.keys()) - active_hosts for host in nonactive_hosts: LOG.info(_("Removing non-active host: %(host)s from " "scheduler cache.") % {'host': host}) del self.host_state_map[host] return self.host_state_map.itervalues()
def get_all_host_states(self, context): """Returns a dict of all the hosts the HostManager knows about. Each of the consumable resources in HostState are populated with capabilities scheduler received from RPC. For example: {'192.168.1.100': HostState(), ...} """ # Get resource usage across the available volume nodes: topic = CONF.volume_topic volume_services = db.service_get_all_by_topic(context, topic, disabled=False) active_hosts = set() for service in volume_services: host = service['host'] if not utils.service_is_up(service): LOG.warn(_("volume service is down. (host: %s)") % host) continue capabilities = self.service_states.get(host, None) host_state = self.host_state_map.get(host) if host_state: # copy capabilities to host_state.capabilities host_state.update_capabilities(capabilities, dict(service.iteritems())) else: host_state = self.host_state_cls(host, capabilities=capabilities, service=dict( service.iteritems())) self.host_state_map[host] = host_state # update attributes in host_state that scheduler is interested in host_state.update_from_volume_capability(capabilities) active_hosts.add(host) # remove non-active hosts from host_state_map nonactive_hosts = set(self.host_state_map.keys()) - active_hosts for host in nonactive_hosts: LOG.info( _("Removing non-active host: %(host)s from " "scheduler cache.") % {'host': host}) del self.host_state_map[host] return self.host_state_map.itervalues()
def mox_host_manager_db_calls(mock, context): mock.StubOutWithMock(db, 'service_get_all_by_topic') db.service_get_all_by_topic(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(VOLUME_SERVICES)
def get_all_by_topic(cls, context, topic, disabled=None): services = db.service_get_all_by_topic(context, topic, disabled=disabled) return base.obj_make_list(context, cls(context), objects.Service, services)
def test_get_all_host_states(self): context = 'fake_context' topic = CONF.volume_topic self.mox.StubOutWithMock(db, 'service_get_all_by_topic') self.mox.StubOutWithMock(host_manager.LOG, 'warn') self.mox.StubOutWithMock(host_manager.utils, 'service_is_up') services = [ dict(id=1, host='host1', topic='volume', disabled=False, availability_zone='zone1', updated_at=timeutils.utcnow()), dict(id=2, host='host2', topic='volume', disabled=False, availability_zone='zone1', updated_at=timeutils.utcnow()), dict(id=3, host='host3', topic='volume', disabled=False, availability_zone='zone2', updated_at=timeutils.utcnow()), dict(id=4, host='host4', topic='volume', disabled=False, availability_zone='zone3', updated_at=timeutils.utcnow()), # service on host5 is disabled dict(id=5, host='host5', topic='volume', disabled=True, availability_zone='zone4', updated_at=timeutils.utcnow()), ] db.service_get_all_by_topic(context, topic).AndReturn(services) host_manager.utils.service_is_up(services[0]).AndReturn(True) host_manager.utils.service_is_up(services[1]).AndReturn(True) host_manager.utils.service_is_up(services[2]).AndReturn(True) host_manager.utils.service_is_up(services[3]).AndReturn(True) host_manager.utils.service_is_up(services[4]).AndReturn(True) # Disabled service host_manager.LOG.warn("volume service is down or disabled. " "(host: host5)") db.service_get_all_by_topic(context, topic).AndReturn(services) host_manager.utils.service_is_up(services[0]).AndReturn(True) host_manager.utils.service_is_up(services[1]).AndReturn(True) host_manager.utils.service_is_up(services[2]).AndReturn(True) host_manager.utils.service_is_up(services[3]).AndReturn(False) # Stopped service host_manager.LOG.warn("volume service is down or disabled. " "(host: host4)") host_manager.utils.service_is_up(services[4]).AndReturn(True) # Disabled service host_manager.LOG.warn("volume service is down or disabled. " "(host: host5)") self.mox.ReplayAll() self.host_manager.get_all_host_states(context) host_state_map = self.host_manager.host_state_map self.assertEqual(len(host_state_map), 4) # Check that service is up for i in xrange(4): volume_node = services[i] host = volume_node['host'] self.assertEqual(host_state_map[host].service, volume_node) self.host_manager.get_all_host_states(context) host_state_map = self.host_manager.host_state_map self.assertEqual(len(host_state_map), 3) for i in xrange(3): volume_node = services[i] host = volume_node['host'] self.assertEqual(host_state_map[host].service, volume_node)