Beispiel #1
0
    def test_get_all(self):
        host1 = self._host + '_1'
        host2 = self._host + '_2'

        serv1 = self.useFixture(
            ServiceFixture(host1, self._binary, self._topic)).serv
        serv1.start()

        serv2 = self.useFixture(
            ServiceFixture(host2, self._binary, self._topic)).serv
        serv2.start()

        service_ref1 = db.service_get_by_args(self._ctx,
                                              host1,
                                              self._binary)
        service_ref2 = db.service_get_by_args(self._ctx,
                                              host2,
                                              self._binary)

        services = self.servicegroup_api.get_all(self._topic)

        self.assertIn(service_ref1['host'], services)
        self.assertIn(service_ref2['host'], services)

        service_id = self.servicegroup_api.get_one(self._topic)
        self.assertIn(service_id, services)
Beispiel #2
0
    def test_memcached_driver(self):
        serv = self.useFixture(
            ServiceFixture(self._host, self._binary, self._topic)).serv
        serv.start()
        service_ref = db.service_get_by_args(self._ctx,
                                             self._host,
                                             self._binary)
        hostkey = str("%s:%s" % (self._topic, self._host))
        self.servicegroup_api._driver.mc.set(hostkey,
                                             timeutils.utcnow(),
                                             time=self.down_time)

        self.assertTrue(self.servicegroup_api.service_is_up(service_ref))
        self.useFixture(test.TimeOverride())
        timeutils.advance_time_seconds(self.down_time + 1)
        self.servicegroup_api._driver._report_state(serv)
        service_ref = db.service_get_by_args(self._ctx,
                                             self._host,
                                             self._binary)

        self.assertTrue(self.servicegroup_api.service_is_up(service_ref))
        serv.stop()
        timeutils.advance_time_seconds(self.down_time + 1)
        service_ref = db.service_get_by_args(self._ctx,
                                             self._host,
                                             self._binary)
        self.assertFalse(self.servicegroup_api.service_is_up(service_ref))
Beispiel #3
0
    def test_service_check_create_race(self):
        self.manager_mock = self.mox.CreateMock(FakeManager)
        self.mox.StubOutWithMock(sys.modules[__name__], 'FakeManager',
                                 use_mock_anything=True)
        self.mox.StubOutWithMock(self.manager_mock, 'init_host')
        self.mox.StubOutWithMock(self.manager_mock, 'pre_start_hook')
        self.mox.StubOutWithMock(self.manager_mock, 'post_start_hook')

        FakeManager(host=self.host).AndReturn(self.manager_mock)

        # init_host is called before any service record is created
        self.manager_mock.init_host()

        db.service_get_by_args(mox.IgnoreArg(), self.host, self.binary
                               ).AndRaise(exception.NotFound)
        ex = exception.ServiceTopicExists(host='foo', topic='bar')
        db.service_create(mox.IgnoreArg(), mox.IgnoreArg()
                          ).AndRaise(ex)

        class TestException(Exception):
            pass

        db.service_get_by_args(mox.IgnoreArg(), self.host, self.binary
                               ).AndRaise(TestException)

        self.mox.ReplayAll()

        serv = service.Service(self.host,
                               self.binary,
                               self.topic,
                               'rack.tests.test_service.FakeManager')
        self.assertRaises(TestException, serv.start)
Beispiel #4
0
    def start(self):
        verstr = version.version_string_with_package()
        LOG.audit(_('Starting %(topic)s node (version %(version)s)'),
                  {'topic': self.topic, 'version': verstr})
        self.basic_config_check()
        self.manager.init_host()
        self.model_disconnected = False
        ctxt = context.get_admin_context()
        try:
            self.service_ref = db.service_get_by_args(ctxt,
                                                      self.host,
                                                      self.binary)
            self.service_id = self.service_ref['id']
        except exception.NotFound:
            try:
                self.service_ref = self._create_service_ref(ctxt)
            except exception.ServiceTopicExists:
                self.service_ref = db.service_get_by_args(ctxt,
                                                          self.host,
                                                          self.binary)

        self.manager.pre_start_hook()

        LOG.debug(_("Join ServiceGroup membership for this service %s")
                  % self.topic)
        self.servicegroup_api.join(self.host, self.topic, self)

        if self.backdoor_port is not None:
            self.manager.backdoor_port = self.backdoor_port

        LOG.debug(_("Creating RPC server for service %s") % self.topic)

        target = messaging.Target(topic=self.topic, server=self.host)

        endpoints = [
            self.manager,
            baserpc.BaseRPCAPI(self.manager.service_name, self.backdoor_port)
        ]
        endpoints.extend(self.manager.additional_endpoints)

        serializer = rack_object.RackObjectSerializer()

        self.rpcserver = rpc.get_server(target, endpoints, serializer)
        self.rpcserver.start()

        self.manager.post_start_hook()

        if self.periodic_enable:
            if self.periodic_fuzzy_delay:
                initial_delay = random.randint(0, self.periodic_fuzzy_delay)
            else:
                initial_delay = None

            self.tg.add_dynamic_timer(
                self.periodic_tasks,
                initial_delay=initial_delay,
                periodic_interval_max=self.periodic_interval_max)
Beispiel #5
0
    def test_service_get_by_args(self):
        values = [
            {'host': 'host1', 'binary': 'a'},
            {'host': 'host2', 'binary': 'b'}
        ]
        services = [self._create_service(vals) for vals in values]

        service1 = db.service_get_by_args(self.ctxt, 'host1', 'a')
        self._assertEqualObjects(services[0], service1)

        service2 = db.service_get_by_args(self.ctxt, 'host2', 'b')
        self._assertEqualObjects(services[1], service2)
Beispiel #6
0
    def _service_start_mocks(self):
        service_create = {'host': self.host,
                          'binary': self.binary,
                          'topic': self.topic,
                          'report_count': 0}
        service_ref = {'host': self.host,
                       'binary': self.binary,
                       'topic': self.topic,
                       'report_count': 0,
                       'id': 1}

        db.service_get_by_args(mox.IgnoreArg(),
                               self.host, self.binary)\
            .AndRaise(exception.NotFound())
        db.service_create(mox.IgnoreArg(),
                          service_create).AndReturn(service_ref)
        return service_ref
Beispiel #7
0
    def test_service_is_up(self):
        serv = self.useFixture(
            ServiceFixture(self._host, self._binary, self._topic)).serv
        serv.start()
        service_ref = db.service_get_by_args(self._ctx,
                                             self._host,
                                             self._binary)
        fake_now = 1000
        down_time = 15
        self.flags(service_down_time=down_time)
        self.mox.StubOutWithMock(timeutils, 'utcnow_ts')
        self.servicegroup_api = servicegroup.API()
        hostkey = str("%s:%s" % (self._topic, self._host))

        # Up (equal)
        timeutils.utcnow_ts().AndReturn(fake_now)
        timeutils.utcnow_ts().AndReturn(fake_now + down_time - 1)
        self.mox.ReplayAll()
        self.servicegroup_api._driver.mc.set(hostkey,
                                             timeutils.utcnow(),
                                             time=down_time)
        result = self.servicegroup_api.service_is_up(service_ref)
        self.assertTrue(result)

        self.mox.ResetAll()
        # Up
        timeutils.utcnow_ts().AndReturn(fake_now)
        timeutils.utcnow_ts().AndReturn(fake_now + down_time - 2)
        self.mox.ReplayAll()
        self.servicegroup_api._driver.mc.set(hostkey,
                                             timeutils.utcnow(),
                                             time=down_time)
        result = self.servicegroup_api.service_is_up(service_ref)
        self.assertTrue(result)

        self.mox.ResetAll()
        # Down
        timeutils.utcnow_ts().AndReturn(fake_now)
        timeutils.utcnow_ts().AndReturn(fake_now + down_time)
        self.mox.ReplayAll()
        self.servicegroup_api._driver.mc.set(hostkey,
                                             timeutils.utcnow(),
                                             time=down_time)
        result = self.servicegroup_api.service_is_up(service_ref)
        self.assertFalse(result)

        self.mox.ResetAll()
        # Down
        timeutils.utcnow_ts().AndReturn(fake_now)
        timeutils.utcnow_ts().AndReturn(fake_now + down_time + 1)
        self.mox.ReplayAll()
        self.servicegroup_api._driver.mc.set(hostkey,
                                             timeutils.utcnow(),
                                             time=down_time)
        result = self.servicegroup_api.service_is_up(service_ref)
        self.assertFalse(result)

        self.mox.ResetAll()
Beispiel #8
0
    def test_report_state(self):
        serv = self.useFixture(
            ServiceFixture(self._host, self._binary, self._topic)).serv
        serv.start()
        db.service_get_by_args(self._ctx, self._host, self._binary)
        self.servicegroup_api = servicegroup.API()

        # updating model_disconnected
        serv.model_disconnected = True
        self.servicegroup_api._driver._report_state(serv)
        self.assertFalse(serv.model_disconnected)

        # handling exception
        serv.model_disconnected = True
        self.servicegroup_api._driver.mc = None
        self.servicegroup_api._driver._report_state(serv)
        self.assertTrue(serv.model_disconnected)

        delattr(serv, 'model_disconnected')
        self.servicegroup_api._driver.mc = None
        self.servicegroup_api._driver._report_state(serv)
        self.assertTrue(serv.model_disconnected)
Beispiel #9
0
    def test_DB_driver(self):
        serv = self.useFixture(
            ServiceFixture(self._host, self._binary, self._topic)).serv
        serv.start()
        service_ref = db.service_get_by_args(self._ctx,
                                             self._host,
                                             self._binary)

        self.assertTrue(self.servicegroup_api.service_is_up(service_ref))
        self.useFixture(test.TimeOverride())
        timeutils.advance_time_seconds(self.down_time + 1)
        self.servicegroup_api._driver._report_state(serv)
        service_ref = db.service_get_by_args(self._ctx,
                                             self._host,
                                             self._binary)

        self.assertTrue(self.servicegroup_api.service_is_up(service_ref))
        serv.stop()
        timeutils.advance_time_seconds(self.down_time + 1)
        service_ref = db.service_get_by_args(self._ctx,
                                             self._host,
                                             self._binary)
        self.assertFalse(self.servicegroup_api.service_is_up(service_ref))
Beispiel #10
0
    def test_get_all(self):
        host1 = self._host + '_1'
        host2 = self._host + '_2'
        host3 = self._host + '_3'

        serv1 = self.useFixture(
            ServiceFixture(host1, self._binary, self._topic)).serv
        serv1.start()

        serv2 = self.useFixture(
            ServiceFixture(host2, self._binary, self._topic)).serv
        serv2.start()

        serv3 = self.useFixture(
            ServiceFixture(host3, self._binary, self._topic)).serv
        serv3.start()

        db.service_get_by_args(self._ctx, host1, self._binary)
        db.service_get_by_args(self._ctx, host2, self._binary)
        db.service_get_by_args(self._ctx, host3, self._binary)

        host1key = str("%s:%s" % (self._topic, host1))
        host2key = str("%s:%s" % (self._topic, host2))
        host3key = str("%s:%s" % (self._topic, host3))
        self.servicegroup_api._driver.mc.set(host1key,
                                             timeutils.utcnow(),
                                             time=self.down_time)
        self.servicegroup_api._driver.mc.set(host2key,
                                             timeutils.utcnow(),
                                             time=self.down_time)
        self.servicegroup_api._driver.mc.set(host3key,
                                             timeutils.utcnow(),
                                             time=-1)

        services = self.servicegroup_api.get_all(self._topic)

        self.assertIn(host1, services)
        self.assertIn(host2, services)
        self.assertNotIn(host3, services)

        service_id = self.servicegroup_api.get_one(self._topic)
        self.assertIn(service_id, services)