def test_service_is_up(self): fts_func = datetime.datetime.fromtimestamp fake_now = 1000 down_time = 5 self.flags(service_down_time=down_time) with mock.patch.object(timeutils, 'utcnow', mock.Mock(return_value=fts_func(fake_now))): # Up (equal) service = {'updated_at': fts_func(fake_now - down_time), 'created_at': fts_func(fake_now - down_time)} result = utils.service_is_up(service) self.assertTrue(result) timeutils.utcnow.assert_called_once_with() with mock.patch.object(timeutils, 'utcnow', mock.Mock(return_value=fts_func(fake_now))): # Up service = {'updated_at': fts_func(fake_now - down_time + 1), 'created_at': fts_func(fake_now - down_time + 1)} result = utils.service_is_up(service) self.assertTrue(result) timeutils.utcnow.assert_called_once_with() with mock.patch.object(timeutils, 'utcnow', mock.Mock(return_value=fts_func(fake_now))): # Down service = {'updated_at': fts_func(fake_now - down_time - 1), 'created_at': fts_func(fake_now - down_time - 1)} result = utils.service_is_up(service) self.assertFalse(result) timeutils.utcnow.assert_called_once_with()
def test_create_share_availability_zone(self): share_id = 'fake' fake_share = {'id': share_id, 'availability_zone': 'fake:fake', 'size': 1} fake_service_1 = {'disabled': False, 'host': 'fake_host1', 'availability_zone': 'fake'} fake_service_2 = {'disabled': False, 'host': 'fake_host2', 'availability_zone': 'super_fake'} fake_result = [(fake_service_1, 0), (fake_service_2, 1)] fake_request_spec = {'share_id': share_id, 'share_properties': fake_share} self.mox.StubOutWithMock(utils, 'service_is_up') self.mox.StubOutWithMock(driver, 'share_update_db') self.mox.StubOutWithMock(db, 'service_get_all_share_sorted') db.service_get_all_share_sorted(IsA(context.RequestContext))\ .AndReturn(fake_result) utils.service_is_up(fake_service_1).AndReturn(True) driver.share_update_db(IsA(context.RequestContext), share_id, fake_service_1['host']).AndReturn(fake_share) self.mox.ReplayAll() self.driver.schedule_create_share(self.context, fake_request_spec, {})
def test_service_is_up(self): fts_func = datetime.datetime.fromtimestamp fake_now = 1000 down_time = 5 self.flags(service_down_time=down_time) self.mox.StubOutWithMock(timeutils, 'utcnow') # Up (equal) timeutils.utcnow().AndReturn(fts_func(fake_now)) service = {'updated_at': fts_func(fake_now - down_time), 'created_at': fts_func(fake_now - down_time)} self.mox.ReplayAll() result = utils.service_is_up(service) self.assertTrue(result) self.mox.ResetAll() # Up timeutils.utcnow().AndReturn(fts_func(fake_now)) service = {'updated_at': fts_func(fake_now - down_time + 1), 'created_at': fts_func(fake_now - down_time + 1)} self.mox.ReplayAll() result = utils.service_is_up(service) self.assertTrue(result) self.mox.ResetAll() # Down timeutils.utcnow().AndReturn(fts_func(fake_now)) service = {'updated_at': fts_func(fake_now - down_time - 1), 'created_at': fts_func(fake_now - down_time - 1)} self.mox.ReplayAll() result = utils.service_is_up(service) self.assertFalse(result)
def test_create_share_if_two_services_up(self): share_id = 'fake' fake_share = {'id': share_id, 'size': 1} fake_service_1 = {'disabled': False, 'host': 'fake_host1'} fake_service_2 = {'disabled': False, 'host': 'fake_host2'} fake_result = [(fake_service_1, 2), (fake_service_2, 1)] self.mox.StubOutWithMock(db, 'service_get_all_share_sorted') self.mox.StubOutWithMock(utils, 'service_is_up') self.mox.StubOutWithMock(driver, 'share_update_db') fake_request_spec = {'share_id': share_id, 'share_properties': fake_share} db.service_get_all_share_sorted(IsA(context.RequestContext))\ .AndReturn(fake_result) utils.service_is_up(IsA(dict)).AndReturn(True) driver.share_update_db(IsA(context.RequestContext), share_id, 'fake_host1').AndReturn(fake_share) self.mox.ReplayAll() self.driver.schedule_create_share(self.context, fake_request_spec, {})
def schedule_create_share(self, context, request_spec, filter_properties): """Picks a host that is up and has the fewest shares.""" # TODO(rushiagr) - pick only hosts that run shares elevated = context.elevated() share_id = request_spec.get('share_id') snapshot_id = request_spec.get('snapshot_id') share_properties = request_spec.get('share_properties') share_size = share_properties.get('size') availability_zone = share_properties.get('availability_zone') zone, host = None, None if availability_zone: zone, _x, host = availability_zone.partition(':') if host and context.is_admin: service = db.service_get_by_args(elevated, host, CONF.share_topic) if not utils.service_is_up(service): raise exception.WillNotSchedule(host=host) updated_share = driver.share_update_db(context, share_id, host) self.share_rpcapi.create_share_instance( context, updated_share.instance, host, request_spec, None, snapshot_id=snapshot_id ) return None results = db.service_get_all_share_sorted(elevated) if zone: results = [(service_g, gigs) for (service_g, gigs) in results if service_g['availability_zone'] == zone] for result in results: (service, share_gigabytes) = result if share_gigabytes + share_size > CONF.max_gigabytes: msg = _("Not enough allocatable share gigabytes remaining") raise exception.NoValidHost(reason=msg) if utils.service_is_up(service) and not service['disabled']: updated_share = driver.share_update_db(context, share_id, service['host']) self.share_rpcapi.create_share_instance( context, updated_share.instance, service['host'], request_spec, None, snapshot_id=snapshot_id) return None msg = _("Is the appropriate service running?") raise exception.NoValidHost(reason=msg)
def schedule_create_share(self, context, request_spec, filter_properties): """Picks a host that is up and has the fewest shares.""" # TODO(rushiagr) - pick only hosts that run shares elevated = context.elevated() share_id = request_spec.get('share_id') snapshot_id = request_spec.get('snapshot_id') share_properties = request_spec.get('share_properties') share_size = share_properties.get('size') availability_zone = share_properties.get('availability_zone') zone, host = None, None if availability_zone: zone, _x, host = availability_zone.partition(':') if host and context.is_admin: service = db.service_get_by_args(elevated, host, CONF.share_topic) if not utils.service_is_up(service): raise exception.WillNotSchedule(host=host) updated_share = driver.share_update_db(context, share_id, host) self.share_rpcapi.create_share(context, updated_share, host, request_spec, None, snapshot_id=snapshot_id) return None results = db.service_get_all_share_sorted(elevated) if zone: results = [(service, gigs) for (service, gigs) in results if service['availability_zone'] == zone] for result in results: (service, share_gigabytes) = result if share_gigabytes + share_size > CONF.max_gigabytes: msg = _("Not enough allocatable share gigabytes remaining") raise exception.NoValidHost(reason=msg) if utils.service_is_up(service) and not service['disabled']: updated_share = driver.share_update_db(context, share_id, service['host']) self.share_rpcapi.create_share(context, updated_share, service['host'], request_spec, None, snapshot_id=snapshot_id) return None msg = _("Is the appropriate service running?") raise exception.NoValidHost(reason=msg)
def list(self): """Show a list of all manila services.""" ctxt = context.get_admin_context() services = db.service_get_all(ctxt) print_format = "%-16s %-36s %-16s %-10s %-5s %-10s" print(print_format % ( _('Binary'), _('Host'), _('Zone'), _('Status'), _('State'), _('Updated At')) ) for svc in services: alive = utils.service_is_up(svc) art = ":-)" if alive else "XXX" status = 'enabled' if svc['disabled']: status = 'disabled' print(print_format % ( svc['binary'], svc['host'].partition('.')[0], svc['availability_zone']['name'], status, art, svc['updated_at'], ))
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 index(self, req): """Return a list of all running services.""" context = req.environ['manila.context'] authorize(context) all_services = db.service_get_all(context) services = [] for service in all_services: service = { 'id': service['id'], 'binary': service['binary'], 'host': service['host'], 'zone': service['availability_zone'], 'status': 'disabled' if service['disabled'] else 'enabled', 'state': 'up' if utils.service_is_up(service) else 'down', 'updated_at': service['updated_at'], } services.append(service) search_opts = [ 'host', 'binary', 'zone', 'state', 'status', ] for search_opt in search_opts: value = '' if search_opt in req.GET: value = req.GET[search_opt] services = [s for s in services if s[search_opt] == value] if len(services) == 0: break return {'services': services}
def get_all_host_states_share(self, context): """Get all hosts and their states. 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 share nodes: topic = CONF.share_topic share_services = db.service_get_all_by_topic(context, topic) for service in share_services: if not utils.service_is_up(service) or service['disabled']: LOG.warn(_LW("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(six.iteritems(service))) else: host_state = self.host_state_cls(host, capabilities=capabilities, service=dict( six.iteritems(service))) self.host_state_map[host] = host_state # update host_state host_state.update_from_share_capability(capabilities) return self.host_state_map.itervalues()
def _update_host_state_map(self, context): # Get resource usage across the available share nodes: topic = CONF.share_topic share_services = db.service_get_all_by_topic(context, topic) for service in share_services: host = service['host'] # Warn about down services and remove them from host_state_map if not utils.service_is_up(service) or service['disabled']: LOG.warn(_LW("Share service is down. (host: %s)") % host) if self.host_state_map.pop(host, None): LOG.info(_LI("Removing non-active host: %s from " "scheduler cache.") % host) continue # Create and register host_state if not in host_state_map 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(six.iteritems(service))) self.host_state_map[host] = host_state # Update capabilities and attributes in host_state host_state.update_from_share_capability( capabilities, service=dict(six.iteritems(service)))
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 _index(self, req): """Return a list of all running services.""" context = req.environ['manila.context'] all_services = db.service_get_all(context) services = [] for service in all_services: service = { 'id': service['id'], 'binary': service['binary'], 'host': service['host'], 'zone': service['availability_zone']['name'], 'status': 'disabled' if service['disabled'] else 'enabled', 'state': 'up' if utils.service_is_up(service) else 'down', 'updated_at': service['updated_at'], } services.append(service) search_opts = [ 'host', 'binary', 'zone', 'state', 'status', ] for search_opt in search_opts: value = '' if search_opt in req.GET: value = req.GET[search_opt] services = [s for s in services if s[search_opt] == value] if len(services) == 0: break return self._view_builder.detail_list(services)
def _update_host_state_map(self, context): # Get resource usage across the available share nodes: topic = CONF.share_topic share_services = db.service_get_all_by_topic(context, topic) for service in share_services: host = service['host'] # Warn about down services and remove them from host_state_map if not utils.service_is_up(service) or service['disabled']: LOG.warning(_LW("Share service is down. (host: %s).") % host) if self.host_state_map.pop(host, None): LOG.info(_LI("Removing non-active host: %s from " "scheduler cache.") % host) continue # Create and register host_state if not in host_state_map 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(six.iteritems(service))) self.host_state_map[host] = host_state # Update capabilities and attributes in host_state host_state.update_from_share_capability( capabilities, service=dict(six.iteritems(service)))
def get_all_host_states_share(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 share nodes: topic = CONF.share_topic share_services = db.service_get_all_by_topic(context, topic) for service in share_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(six.iteritems(service))) else: host_state = self.host_state_cls( host, capabilities=capabilities, service=dict(six.iteritems(service))) self.host_state_map[host] = host_state # update host_state host_state.update_from_share_capability(capabilities) return self.host_state_map.itervalues()
def cleanup(self): """Remove manila services reporting as 'down'.""" ctxt = context.get_admin_context() services = db.service_get_all(ctxt) for svc in services: if utils.service_is_up(svc): continue db.service_destroy(ctxt, svc['id']) print("Cleaned up service %s" % svc['host'])
def test_create_share_availability_zone_if_service_down(self): share_id = 'fake' fake_share = {'id': share_id, 'availability_zone': 'fake:fake', 'size': 1} fake_request_spec = {'share_id': share_id, 'share_properties': fake_share} self.mox.StubOutWithMock(utils, 'service_is_up') self.mox.StubOutWithMock(db, 'service_get_by_args') db.service_get_by_args(IsA(context.RequestContext), 'fake', 'manila-share').AndReturn('fake_service') utils.service_is_up('fake_service').AndReturn(False) self.mox.ReplayAll() self.assertRaises(exception.WillNotSchedule, self.driver.schedule_create_share, self.admin_context, fake_request_spec, {})
def test_create_share_availability_zone_on_host(self): share_id = 'fake' fake_share = {'id': share_id, 'availability_zone': 'fake:fake', 'size': 1} fake_request_spec = {'share_id': share_id, 'share_properties': fake_share} self.mox.StubOutWithMock(utils, 'service_is_up') self.mox.StubOutWithMock(db, 'service_get_by_args') self.mox.StubOutWithMock(driver, 'share_update_db') db.service_get_by_args(IsA(context.RequestContext), 'fake', 'manila-share').AndReturn('fake_service') utils.service_is_up('fake_service').AndReturn(True) driver.share_update_db(IsA(context.RequestContext), share_id, 'fake').AndReturn(fake_share) self.mox.ReplayAll() self.driver.schedule_create_share(self.admin_context, fake_request_spec, {})
def list(self): """Show a list of all manila services.""" ctxt = context.get_admin_context() services = db.service_get_all(ctxt) print_format = "%-16s %-36s %-16s %-10s %-5s %-10s" print(print_format % (_("Binary"), _("Host"), _("Zone"), _("Status"), _("State"), _("Updated At"))) for svc in services: alive = utils.service_is_up(svc) art = ":-)" if alive else "XXX" status = "enabled" if svc["disabled"]: status = "disabled" print( print_format % ( svc["binary"], svc["host"].partition(".")[0], svc["availability_zone"]["name"], status, art, svc["updated_at"], ) )
def schedule_create_share(self, context, request_spec, filter_properties): """Picks a host that is up and has the fewest shares.""" # TODO(rushiagr) - pick only hosts that run shares elevated = context.elevated() share_id = request_spec.get('share_id') snapshot_id = request_spec.get('snapshot_id') share_properties = request_spec.get('share_properties') share_size = share_properties.get('size') instance_properties = request_spec.get('share_instance_properties', {}) availability_zone_id = instance_properties.get('availability_zone_id') results = db.service_get_all_share_sorted(elevated) if availability_zone_id: results = [ (service_g, gigs) for (service_g, gigs) in results if (service_g['availability_zone_id'] == availability_zone_id) ] for result in results: (service, share_gigabytes) = result if share_gigabytes + share_size > CONF.max_gigabytes: msg = _("Not enough allocatable share gigabytes remaining") raise exception.NoValidHost(reason=msg) if utils.service_is_up(service) and not service['disabled']: updated_share = base.share_update_db(context, share_id, service['host']) self.share_rpcapi.create_share_instance( context, updated_share.instance, service['host'], request_spec, None, snapshot_id=snapshot_id) return None msg = _("Is the appropriate service running?") raise exception.NoValidHost(reason=msg)
def schedule_create_share(self, context, request_spec, filter_properties): """Picks a host that is up and has the fewest shares.""" # TODO(rushiagr) - pick only hosts that run shares elevated = context.elevated() share_id = request_spec.get('share_id') snapshot_id = request_spec.get('snapshot_id') share_properties = request_spec.get('share_properties') share_size = share_properties.get('size') instance_properties = request_spec.get('share_instance_properties', {}) availability_zone_id = instance_properties.get('availability_zone_id') results = db.service_get_all_share_sorted(elevated) if availability_zone_id: results = [(service_g, gigs) for (service_g, gigs) in results if (service_g['availability_zone_id'] == availability_zone_id)] for result in results: (service, share_gigabytes) = result if share_gigabytes + share_size > CONF.max_gigabytes: msg = _("Not enough allocatable share gigabytes remaining") raise exception.NoValidHost(reason=msg) if utils.service_is_up(service) and not service['disabled']: updated_share = base.share_update_db(context, share_id, service['host']) self.share_rpcapi.create_share_instance( context, updated_share.instance, service['host'], request_spec, None, snapshot_id=snapshot_id) return None msg = _("Is the appropriate service running?") raise exception.NoValidHost(reason=msg)