Implement ConsulOperator as a server broker to support HTTP Driver

This commit introduces the implementation of ConsulOperator.
It is designed to manipulate Consul, serving as a service broker
for the HTTP driver.

HTTP driver has been proposed as a new oslo.messaging driver for
RPC communication. It communicates RPC client and server directly
over HTTP without a messaging queue.

Partially-implements: blueprint oslo-http-driver

Change-Id: I81e52a3ab24828584efdfbf7b6a676a2a9c320a8
This commit is contained in:
Xiang Wang 2023-10-30 15:02:53 +09:00 committed by xiang-roger-wang
parent ebdc7db19e
commit 8dadf42024
6 changed files with 1360 additions and 0 deletions

View File

@ -0,0 +1,416 @@
# Copyright 2024 LY Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import consul
import datetime
import os
from oslo_config import cfg
from oslo_log import log as logging
from oslo_messaging._drivers.http_driver import service_broker
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
# Consul options.
consul_opts = [
cfg.IntOpt('port',
default=8500,
help='Port where the Consul service is running'),
cfg.StrOpt('host',
default="127.0.0.1",
help='Host where the Consul service is running'),
cfg.StrOpt('timeout',
default="50ms",
help='Timeout for access Consul API.'),
cfg.IntOpt('cache_max_age',
default=0,
help='Max age for the Consul local in memory cache '
'(in seconds). Never use cache if max age is 0.'),
cfg.StrOpt('check_interval',
default="60s",
help='Interval for performing a health check.'),
cfg.StrOpt('check_timeout',
default="10s",
help='Timeout for a health check to be critical state'),
cfg.StrOpt('check_deregister',
default="600s",
help='Specifies that checks associated with a service should '
'deregister after being the critical state for this '
'configured time.'),
cfg.StrOpt('token', default=os.environ.get('CONSUL_HTTP_TOKEN', None),
help='A Token for consul API call. Default is None'),
]
def register_consul_opts(conf):
consul_opt_group = cfg.OptGroup(name='consul',
title='Consul options')
conf.register_group(consul_opt_group)
conf.register_opts(consul_opts, group=consul_opt_group)
class ConsulOperator(service_broker.ServiceBroker):
"""A representation to operate on Consul.
It implements local in-memory cache to keep the consul service list
in case of Consul outages.
"""
def __init__(self, port=None, host=None):
register_consul_opts(CONF)
self.port = port if port else CONF.consul.port
self.host = host if host else CONF.consul.host
self.timeout = CONF.consul.timeout
self.consul_token = CONF.consul.token
self.consul = consul.Consul(port=self.port,
host=self.host,
token=self.consul_token)
self.check_interval = CONF.consul.check_interval
self.check_timeout = CONF.consul.check_timeout
self.check_deregister = CONF.consul.check_deregister
self.dc_access_counter = {} # {dc: counter}
# Cache related attributes.
# {service_name: {server: service_info}}
# E.g
# {
# "nova.scheduler":{
# "hostname1": service_info
# "hostname2": service_info
# },
# "nova.conductor":{
# "hostname1": service_info
# "hostname2": service_info
# }
self.cached_services = {}
self.cache_max_age = CONF.consul.cache_max_age # In seconds.
self.last_requested = None
def register_service(self, name, service_id, address, port, tags,
proxy_conf, enable_ssl=False):
reverse_proxy_info = {
'reverse_proxy_name': proxy_conf.reverse_proxy_name,
'reverse_proxy_endpoint': proxy_conf.reverse_proxy_endpoint
}
# Step 1: register the service.
# Register returns True/False/Exception
# https://github.com/cablehead/python-consul/blob/
# 53eb41c4760b983aec878ef73e72c11e0af501bb/consul/base.py#L171-L196
ret = self.consul.agent.service.register(
name=name,
service_id=service_id,
address=address,
port=port,
tags=tags,
token=self.consul_token,
meta=reverse_proxy_info)
if not ret:
return False
# Step 2: create a http check for this service.
header = {}
reverse_proxy = reverse_proxy_info.get('reverse_proxy_endpoint')
proxy_conf = proxy_conf.proxy_endpoint
if reverse_proxy and not proxy_conf:
# Send the healthcheck request to reverse proxy if applicable.
# If reverse proxy and proxy co-exist, it implies the LINE bank
# environment where health checks can be sent to the service
# directly without going through any reverse proxy.
if not reverse_proxy.endswith('/'):
reverse_proxy += '/'
url = reverse_proxy + 'healthcheck'
# Format of header: {"x-foo": ["bar", "baz"]}
header['x-rpchost'] = [address]
header['x-rpchost-port'] = [str(port)]
else:
# send it directly to the endpoint otherwise.
if enable_ssl:
url_format = 'https://{host:s}:{port:d}/healthcheck'
else:
url_format = 'http://{host:s}:{port:d}/healthcheck'
url = url_format.format(host=address, port=port)
http_check = consul.Check.http(url, self.check_interval,
self.check_timeout,
self.check_deregister, header,
tls_skip_verify=True)
LOG.debug("Check config done. url=%s, header=%s", url, header)
# Initial state of a health check to "passing".
http_check["status"] = "passing"
ret = self.consul.agent.check.register("check-%s" % service_id,
check=http_check,
token=self.consul_token,
service_id=service_id)
return ret
def deregister_service(self, service_id):
try:
ret = self.consul.agent.service.deregister(service_id)
except consul.ConsulException as e:
msg = ("The service(%s) has already been removed and ignores this "
"exception: %s." % (service_id, e))
LOG.warning(msg)
except Exception as e:
msg = ("Failed to deregister %s and ignores the following "
"exception: %s" % (service_id, e))
LOG.warning(msg)
else:
LOG.debug("Consul deregister returned %s", ret)
def _get_cached_service_list(self, consul_svc, server=None):
result = []
if consul_svc in self.cached_services:
if server:
single_service = self.cached_services[consul_svc].get(server)
if single_service:
# Since this return value is expected to be a list,
# make this single result as a list.
result = [single_service]
else:
# Return all cached services of consul_svc if no
# server(hostname) is provided.
result = list(self.cached_services[consul_svc].values())
return result
def _update_cache(self, consul_svc, server, new_service_list):
"""Update the cache for the given service `consul_svc` running on
the `server` with `new_service_list` returned from Consul. If `server`
is None, update the cache for all the service entries of
the `consul_svc`.
Sample format for `new_service_list`:
[
{'Address': '172.25.0.5',
'Datacenter': 'dc1',
'ID': 'hostname2:4000',
'Meta': {'reverse_proxy_endpoint': '', 'reverse_proxy_name': ''},
'Name': 'nova.scheduler',
'Node': 'consul-self.server1',
'Port': 4000,
'Tags': ['hostname2']},
{'Address': '172.25.0.4',
'Datacenter': 'dc1',
'ID': 'hostname1:3000',
'Meta': {'reverse_proxy_endpoint': '', 'reverse_proxy_name': ''},
'Name': 'nova.scheduler',
'Node': 'consul-self.server1',
'Port': 3000,
'Tags': ['hostname1']
]
:param consul_svc: Service name. e.g. nova.scheduler
:type consul_svc: str
:param server: Hostname of the server. e.g. hostname:3000
:type server: str
:param new_service_list: The services list returned from Consul.
:type new_service_list: dict
"""
if consul_svc not in self.cached_services:
self.cached_services[consul_svc] = {}
if server:
# If server is specified, there should be only one entry in
# new_service_list. Use index 0 to get the element.
self.cached_services[consul_svc][server] = new_service_list[0]
else:
for entry in new_service_list:
hostname = entry['Tags'][0]
self.cached_services[consul_svc][hostname] = entry
def _is_cache_expired(self, now):
if self.last_requested is None:
return True
else:
time_diff = now - self.last_requested
return time_diff.seconds > self.cache_max_age
def get_service_list(self, consul_svc, server,
force_revalidate=False,
cross_dc=False):
now = datetime.datetime.now()
# force_revalidate=True is equivalent to max_age=0, also meaning
# we always needs to fetch data from consul.
must_fetch_from_consul = self._is_cache_expired(now) or \
force_revalidate or \
self.cache_max_age == 0
result = []
if must_fetch_from_consul:
# Do not use cache.
try:
if cross_dc:
result = self._get_service_list_across_dc(
consul_svc, server)
else:
result = self._get_service_list(
consul_svc, server)
if len(result) == 0:
# If no result returned from Consul, we give a try with the
# local cache.
result = self._get_cached_service_list(consul_svc, server)
LOG.info("No result found on Consul for %s, try using "
"the cached service list", consul_svc)
if len(result) == 0:
# If still no result, then we return this empty
# result list as-is for the caller to handle.
LOG.error("No cached service list found for %s",
consul_svc)
else:
self._update_cache(consul_svc, server, result)
self.last_requested = now
except ConsulAccessException as e:
LOG.error(e)
# With ConsulAccessException, always try fetching
# the service list from the local cache.
result = self._get_cached_service_list(
consul_svc, server)
LOG.info("Unable to access to Consul, try using "
"the cached service list")
if len(result) == 0:
# If there is no cached service list, raise
# ConsulAccessException again for the caller to handle.
LOG.error("No cached service list found for %s",
consul_svc)
raise
else:
cached_services = self._get_cached_service_list(consul_svc,
server)
if len(cached_services) == 0:
# Cache miss, then try fetching from consul.
# Since we already know there is no cache for this consul_svc
# if ConsulAccessException happens, there is no need to try
# covering using cache again.
result = self._get_service_list(
consul_svc, server)
self._update_cache(consul_svc, server, result)
self.last_requested = now
else:
# Use cache.
result = cached_services
LOG.debug("Using cached service list. "
"Last consul request %s",
self.last_requested.strftime("%Y-%m-%d %H:%M:%S"))
return result
def _get_service_list(self, consul_svc, server):
# First, try getting the service list from the local DC.
try:
local_dc = self.consul.agent.self()['Config']['Datacenter']
except Exception as err:
err_msg = ("Unable to the local data center from consul: "
"%(host)s:%(port)s, Reason: %(reason)s") % \
{'host': self.host,
'port': self.port,
'reason': str(err)}
raise ConsulAccessException(err_msg)
service_list = self._get_service_list_from_dc(consul_svc, local_dc,
server)
if len(service_list) == 0:
# Since we failed to get the service list from the local DC,
# try getting service list from other DCs.
try:
other_dcs = self.consul.catalog.datacenters()
other_dcs.remove(local_dc)
except Exception as err:
err_msg = ("Unable to get data center list from consul: "
"%(host)s:%(port)s, Reason: %(reason)s") % \
{'host': self.host,
'port': self.port,
'reason': str(err)}
raise ConsulAccessException(err_msg)
other_dcs = [] if other_dcs is None else other_dcs
# Make sure all DCs have a count.
for dc in other_dcs:
if dc not in self.dc_access_counter:
self.dc_access_counter[dc] = 0
# Sort the DCs based on how many times each is accessed.
other_dcs = sorted(other_dcs, key=lambda x:
self.dc_access_counter[x])
for dc in other_dcs:
service_list = self._get_service_list_from_dc(consul_svc,
dc,
server)
if len(service_list) > 0:
break
return service_list
def _get_service_list_across_dc(self, consul_svc, server):
"""Get all the services across all datacenters.
"""
try:
dcs = self.consul.catalog.datacenters()
except Exception as err:
err_msg = ("Unable to get data center list from consul: "
"%(host)s:%(port)s, Reason: %(reason)s") % \
{'host': self.host,
'port': self.port,
'reason': str(err)}
raise ConsulAccessException(err_msg)
dcs = [] if dcs is None else dcs
services_across_dc = []
for dc in dcs:
services = self._get_service_list_from_dc(consul_svc,
dc,
server)
services_across_dc.extend(services)
return services_across_dc
def _get_service_list_from_dc(self, consul_svc, dc, server):
try:
# passing=True to filter results to only
# those nodes whose health checks are currently passing.
_, service_list = self.consul.health.service(
consul_svc, tag=server, wait=self.timeout, dc=dc,
token=self.consul_token, passing=True)
service_list = [service["Service"] for service in service_list]
LOG.debug("Get service list from consul at %(dc)s: "
"%(host)s:%(port)s, length: %(length)s",
{'dc': dc, 'host': self.host,
'port': self.port,
'length': len(service_list)})
self._increment_dc_access_count(dc)
return service_list
except Exception as err:
dc = ' at ' + dc if 'dc' in locals() else ''
err_msg = ("Unable to get services from consul%(dc)s: "
"%(host)s:%(port)s, Reason: %(reason)s") % {
'dc': dc, 'host': self.host,
'port': self.port, 'reason': str(err)}
raise ConsulAccessException(err_msg)
def _increment_dc_access_count(self, dc):
if dc in self.dc_access_counter:
self.dc_access_counter[dc] += 1
else:
self.dc_access_counter[dc] = 1
class ConsulRegistrationException(service_broker.ServiceBrokerException):
def __init__(self, message):
message = "Error during Consul Registration: %s" % message
super(ConsulRegistrationException, self).__init__(message)
class ConsulAccessException(service_broker.ServiceBrokerException):
def __init__(self, message):
message = "Error during accessing to Consul: %s" % message
super(ConsulAccessException, self).__init__(message)

View File

@ -0,0 +1,39 @@
# Copyright 2024 LY Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import abc
class ServiceBroker(metaclass=abc.ABCMeta):
"""An abstract class that provides the interface to operate on the service
broker.
"""
@abc.abstractmethod
def register_service(self, name, service_id, address, port, tags,
proxy_conf, enable_ssl=False):
"""Register a service on the service broker."""
@abc.abstractmethod
def deregister_service(self, service_id):
"""Deregister a service from the service broker."""
@abc.abstractmethod
def get_service_list(self, service, server):
"""Get the list of services from the service broker."""
class ServiceBrokerException(Exception):
def __init__(self, message):
super(ServiceBrokerException, self).__init__(message)

View File

@ -0,0 +1,902 @@
# Copyright 2024 LY Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime
from unittest import mock
import consul
import testscenarios
from oslo_config import cfg
from oslo_messaging._drivers.http_driver import consul_operator
from oslo_messaging.tests import utils as test_utils
load_tests = testscenarios.load_tests_apply_scenarios
# Sample raw data returned from Consul health API
# i.e. /health/service/:service
raw_services = [
{"Service": {
"Node": "consul-self.server1",
"Address": "172.25.0.4",
"Datacenter": "dc1",
"ID": "hostname1:3000",
"Name": "nova.scheduler",
"Tags": [
"hostname1"
],
"Port": 3000,
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
}
},
{"Service": {
"Node": "consul-self.server1",
"Address": "172.25.0.5",
"Datacenter": "dc1",
"ID": "hostname2:4000",
"Name": "nova.scheduler",
"Tags": [
"hostname2"
],
"Port": 4000,
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
}}
]
# The raw data is filtered out by ConsulOperator when getting service list.
formatted_services = [service['Service'] for service in raw_services]
@mock.patch('consul.Consul')
class TestConsulOperator(test_utils.BaseTestCase):
def setUp(self):
self.conf = cfg.CONF
super(TestConsulOperator, self).setUp(conf=self.conf)
self.consul_svc = 'nova.scheduler'
self.server = 'hostname1'
consul_operator.register_consul_opts(self.conf)
def _init_consul_operator(self, cache_max_age=0, host=None, port=None):
consul = consul_operator.ConsulOperator(host=host, port=port)
consul.cache_max_age = cache_max_age
consul.check_interval = "60s"
consul.check_timeout = "5s"
consul.check_deregister = "600s"
return consul
def test_init_with_specified_token(self, m_consul):
self.conf.set_override('token', 'default_token', group='consul')
self._init_consul_operator()
m_consul.assert_called_once_with(
host='127.0.0.1', port=8500, token='default_token')
def test_init_specified_token_is_none(self, m_consul):
self.conf.set_override('token', None, group='consul')
self._init_consul_operator()
m_consul.assert_called_once_with(
host='127.0.0.1', port=8500, token=None)
def test_init_no_token_specified(self, m_consul):
self._init_consul_operator()
m_consul.assert_called_once_with(
host='127.0.0.1', port=8500, token=None)
def test__get_service_list_from_dc(self, m_consul):
operator = self._init_consul_operator()
dc = 'fake-dc'
m_consul.return_value.health.service.return_value = (
mock.MagicMock, raw_services)
operator._get_service_list_from_dc(self.consul_svc, dc, None)
# Check if the health.service is called correctly.
m_consul.return_value.health.service.assert_called_once_with(
'nova.scheduler', dc='fake-dc', tag=None, wait='50ms',
passing=True, token=None)
self.assertEqual(operator.dc_access_counter[dc], 1)
# Call the second time to check the counter.
operator._get_service_list_from_dc(self.consul_svc, dc, None)
self.assertEqual(operator.dc_access_counter[dc], 2)
other_dc = "other-dc"
operator._get_service_list_from_dc(self.consul_svc, other_dc, None)
self.assertEqual(operator.dc_access_counter[other_dc], 1)
def test__get_service_list_from_dc_with_error(self, m_consul):
operator = self._init_consul_operator()
dc = 'fake-dc'
m_consul.return_value.health.service.side_effect = [
Exception('fake consul error')]
# Check if ConsulAccessException is raised.
self.assertRaises(consul_operator.ConsulAccessException,
operator._get_service_list_from_dc,
self.consul_svc, dc, None)
# Check if the health.service is called correctly.
m_consul.return_value.health.service.assert_called_once_with(
'nova.scheduler', dc='fake-dc', tag=None, wait='50ms',
passing=True, token=None)
self.assertNotIn(dc, operator.dc_access_counter)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list_from_dc')
def test__get_service_list_with_local_dc(self, m__get_service_list_from_dc,
m_consul):
operator = self._init_consul_operator()
m_consul.return_value.agent.self.return_value = {
'Config': {'Datacenter': 'local'}}
m__get_service_list_from_dc.return_value = formatted_services
operator._get_service_list(self.consul_svc, self.server)
# Check if the local DC is used.
m__get_service_list_from_dc.assert_called_once_with(
self.consul_svc, 'local', self.server
)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list_from_dc')
def test__get_service_list_with_other_dc_simple(self,
m__service_list_from_dc,
m_consul):
operator = self._init_consul_operator()
m_consul.return_value.agent.self.return_value = {
'Config': {'Datacenter': 'local'}}
# Set the service list of the local DC to empty, so that we can
# access other DCs.
m__service_list_from_dc.return_value = []
m_consul.return_value.catalog.datacenters.return_value = ['local',
'tokyo-2',
'tokyo-3']
operator.dc_access_counter['tokyo-2'] = 1
operator._get_service_list(self.consul_svc, self.server)
# Local should always be accessed first.
calls = [mock.call(self.consul_svc, 'local', self.server),
mock.call(self.consul_svc, 'tokyo-3', self.server),
mock.call(self.consul_svc, 'tokyo-2', self.server), ]
m__service_list_from_dc.assert_has_calls(calls)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list_from_dc')
def test__get_service_list_with_other_dc_complex(self,
m__service_list_from_dc,
m_consul):
operator = self._init_consul_operator()
m_consul.return_value.agent.self.return_value = {
'Config': {'Datacenter': 'local'}}
# Set the service list of the local DC to empty, so that we can
# access other DCs.
m__service_list_from_dc.return_value = []
m_consul.return_value.catalog.datacenters.return_value = ['local',
'tokyo-2',
'tokyo-3']
operator.dc_access_counter = {'tokyo-2': 2, 'tokyo-3': 5, 'local': 8}
operator._get_service_list(self.consul_svc, self.server)
# Local should always be accessed first.
# Ensure we access DCs in a correct order.
calls = [mock.call(self.consul_svc, 'local', self.server),
mock.call(self.consul_svc, 'tokyo-2', self.server),
mock.call(self.consul_svc, 'tokyo-3', self.server), ]
m__service_list_from_dc.assert_has_calls(calls)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list_from_dc')
def test__get_service_list_across_dc_with_no_dc(self,
m__service_list_from_dc,
m_consul):
operator = self._init_consul_operator()
# Set the service list of any DC to be non-empty.
m__service_list_from_dc.return_value = ['service1']
m_consul.return_value.catalog.datacenters.return_value = []
result = operator._get_service_list_across_dc(self.consul_svc,
self.server)
m__service_list_from_dc.assert_not_called()
# The result should be empty as there is no DC.
self.assertEqual(result, [])
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list_from_dc')
def test__get_service_list_across_dc_with_one_dc(self,
m__service_list_from_dc,
m_consul):
operator = self._init_consul_operator()
# Set the service list of any DC to be non-empty.
m__service_list_from_dc.return_value = ['service1']
m_consul.return_value.catalog.datacenters.return_value = ['tokyo-1']
result = operator._get_service_list_across_dc(self.consul_svc,
self.server)
calls = [mock.call(self.consul_svc, 'tokyo-1', self.server), ]
m__service_list_from_dc.assert_has_calls(calls, any_order=True)
self.assertEqual(result, ['service1'])
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list_from_dc')
def test__get_service_list_across_dc_no_result(self,
m__service_list_from_dc,
m_consul):
operator = self._init_consul_operator()
# Set the service list of any DC to be empty.
m__service_list_from_dc.return_value = []
m_consul.return_value.catalog.datacenters.return_value = ['local',
'tokyo-2',
'tokyo-3']
result = operator._get_service_list_across_dc(self.consul_svc,
self.server)
calls = [mock.call(self.consul_svc, 'local', self.server),
mock.call(self.consul_svc, 'tokyo-2', self.server),
mock.call(self.consul_svc, 'tokyo-3', self.server), ]
# The order doesn't matter here.
m__service_list_from_dc.assert_has_calls(calls, any_order=True)
self.assertEqual(result, [])
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list_from_dc')
def test__get_service_list_across_dc(self,
m__service_list_from_dc,
m_consul):
operator = self._init_consul_operator()
m__service_list_from_dc.return_value = ['service1']
m_consul.return_value.catalog.datacenters.return_value = ['tokyo-2',
'tokyo-1']
result = operator._get_service_list_across_dc(self.consul_svc,
self.server)
calls = [mock.call(self.consul_svc, 'tokyo-1', self.server),
mock.call(self.consul_svc, 'tokyo-2', self.server)]
# The order doesn't matter here.
m__service_list_from_dc.assert_has_calls(calls, any_order=True)
# This result should contain services from tokyo-1 and tokyo-2.
self.assertEqual(result, ['service1', 'service1'])
def test__update_cached_with_single_server(self, m_consul):
operator = self._init_consul_operator()
self.assertEqual(operator.cached_services, {})
expected_cache = {
"nova.scheduler": {
"hostname1": {
"ID": "hostname1:3000",
"Name": "nova.scheduler",
"Port": 3000,
"Node": "consul-self.server1",
"Address": "172.25.0.4",
"Datacenter": "dc1",
"Tags": [
"hostname1"
],
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
},
}
}
# First update with hostname1.
operator._update_cache(self.consul_svc, "hostname1",
[formatted_services[0]])
self.assertEqual(operator.cached_services, expected_cache)
# Second update with hostname2.
operator._update_cache(self.consul_svc, "hostname2",
[formatted_services[1]])
# The cache should contain information for both hostname1 and
# hostname2.
expected_cache = {
"nova.scheduler": {
"hostname1": {
"ID": "hostname1:3000",
"Name": "nova.scheduler",
"Port": 3000,
"Node": "consul-self.server1",
"Address": "172.25.0.4",
"Datacenter": "dc1",
"Tags": [
"hostname1"
],
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
},
"hostname2": {
"ID": "hostname2:4000",
"Name": "nova.scheduler",
"Port": 4000,
"Node": "consul-self.server1",
"Address": "172.25.0.5",
"Datacenter": "dc1",
"Tags": [
"hostname2"
],
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
}
}
}
self.assertEqual(operator.cached_services, expected_cache)
def test__update_cached_with_multiple_servers(self, m_consul):
operator = self._init_consul_operator()
self.assertEqual(operator.cached_services, {})
expected_cache = {
"nova.scheduler": {
"hostname1": {
"ID": "hostname1:3000",
"Name": "nova.scheduler",
"Port": 3000,
"Node": "consul-self.server1",
"Address": "172.25.0.4",
"Datacenter": "dc1",
"Tags": [
"hostname1"
],
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
},
"hostname2": {
"ID": "hostname2:4000",
"Name": "nova.scheduler",
"Port": 4000,
"Node": "consul-self.server1",
"Address": "172.25.0.5",
"Datacenter": "dc1",
"Tags": [
"hostname2"
],
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
}
}
}
operator._update_cache(self.consul_svc, None, formatted_services)
self.assertEqual(operator.cached_services, expected_cache)
def test__update_cached_mix(self, m_consul):
operator = self._init_consul_operator()
# Update cache for nova.scheduler with all servers
# including hostname1 and hostname2.
operator._update_cache(self.consul_svc, None, formatted_services)
new_service_info = [
{
"Node": "consul-self.server1",
"Address": "6.6.6.6",
"Datacenter": "dc1",
"ID": "hostname2:5000",
"Name": "nova.scheduler",
"Tags": [
"hostname2"
],
"Port": 5000,
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
}]
# Update cache for nova.scheduler with hostname2.
operator._update_cache(self.consul_svc, 'hostname2', new_service_info)
expected_cache = {
"nova.scheduler": {
"hostname1": {
"ID": "hostname1:3000",
"Name": "nova.scheduler",
"Port": 3000,
"Node": "consul-self.server1",
"Address": "172.25.0.4",
"Datacenter": "dc1",
"Tags": [
"hostname1"
],
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
},
"hostname2": {
"ID": "hostname2:5000",
"Name": "nova.scheduler",
"Port": 5000,
"Node": "consul-self.server1",
"Address": "6.6.6.6",
"Datacenter": "dc1",
"Tags": [
"hostname2"
],
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
}
}
}
self.assertEqual(operator.cached_services, expected_cache)
def test__get_cached_service_list_without_server(self, m_consul):
operator = self._init_consul_operator()
operator._update_cache(self.consul_svc, None, formatted_services)
result = operator._get_cached_service_list(self.consul_svc)
self.assertEqual(result, formatted_services)
# Check if an empty list is returned when a service name doesn't exist
# in cache.
result = operator._get_cached_service_list("other_service_name")
self.assertEqual(result, [])
def test__get_cached_service_list_with_server(self, m_consul):
operator = self._init_consul_operator()
operator._update_cache(self.consul_svc, None, formatted_services)
result = operator._get_cached_service_list(self.consul_svc,
'hostname1')
expected_result = [
{
"ID": "hostname1:3000",
"Node": "consul-self.server1",
"Address": "172.25.0.4",
"Datacenter": "dc1",
"Name": "nova.scheduler",
"Tags": [
"hostname1"
],
"Port": 3000,
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
}, ]
self.assertEqual(result, expected_result)
result = operator._get_cached_service_list(self.consul_svc,
'hostname2')
expected_result = [
{
"ID": "hostname2:4000",
"Node": "consul-self.server1",
"Address": "172.25.0.5",
"Datacenter": "dc1",
"Name": "nova.scheduler",
"Tags": [
"hostname2"
],
"Port": 4000,
"Meta": {
"reverse_proxy_name": "",
"reverse_proxy_endpoint": ""
}
}, ]
self.assertEqual(result, expected_result)
result = operator._get_cached_service_list(self.consul_svc,
'non-exist')
self.assertEqual(result, [])
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_force_revalidate(self,
m__get_service_list,
m_consul):
operator = self._init_consul_operator()
operator.get_service_list(self.consul_svc, self.server, True)
m__get_service_list.assert_called_once_with(self.consul_svc,
self.server)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_zero_max_age(self,
m__get_service_list,
m_consul):
# Check if cache_max_age=0 is equivalent to force_revalidate=True
operator = self._init_consul_operator(cache_max_age=0)
operator.get_service_list(self.consul_svc, self.server, False)
m__get_service_list.assert_called_once_with(self.consul_svc,
self.server)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_first_access(self,
m__get_service_list,
m_consul):
operator = self._init_consul_operator(cache_max_age=100)
# Make sure the service list returned from Consul is not empty.
m__get_service_list.return_value = formatted_services
# Since we haven't access to Consul, last_requested should be None.
self.assertIsNone(operator.last_requested)
operator.get_service_list(self.consul_svc, self.server, False)
m__get_service_list.assert_called_once_with(self.consul_svc,
self.server)
self.assertIsNotNone(operator.last_requested)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_first_access_with_no_result(self,
m__get_service_list,
m_consul):
operator = self._init_consul_operator(cache_max_age=100)
# Test when accessing Consul for the first time and the service
# list returned from Consul is empty.
m__get_service_list.return_value = []
# Since we haven't access to Consul, last_requested should be None.
self.assertIsNone(operator.last_requested)
svc_list = operator.get_service_list(self.consul_svc, self.server,
False)
m__get_service_list.assert_called_once_with(self.consul_svc,
self.server)
# The returned svc_list should be also empty since no cache is
# available for the first time accessing Consul.
self.assertEqual(svc_list, [])
# Since we get an empty service list from Consul, last_requested should
# still be None.
self.assertIsNone(operator.last_requested)
@mock.patch('datetime.datetime')
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_cache_timeout(self,
m__get_service_list,
m_datetime,
m_consul):
operator = self._init_consul_operator(cache_max_age=2)
m_datetime.now.return_value = datetime(2022, 6, 29, 2, 22, 13)
operator.last_requested = datetime(2022, 6, 29, 2, 22, 10)
operator.get_service_list(self.consul_svc, self.server, False)
# Do not use cache since cache expired.
m__get_service_list.assert_called_once_with(self.consul_svc,
self.server)
@mock.patch('datetime.datetime')
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_cache_miss(self,
m__get_service_list,
m_datetime,
m_consul):
operator = self._init_consul_operator(cache_max_age=10)
m_datetime.now.return_value = datetime(2022, 6, 29, 2, 22, 13)
operator.last_requested = datetime(2022, 6, 29, 2, 22, 10)
operator.get_service_list(self.consul_svc, self.server, False)
# Test if we fetch data from Consul due the cache miss.
m__get_service_list.assert_called_once_with(self.consul_svc,
self.server)
@mock.patch('datetime.datetime')
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_cached(self,
m__get_service_list,
m_datetime,
m_consul):
operator = self._init_consul_operator(cache_max_age=2)
operator._update_cache(self.consul_svc, None, formatted_services)
m_datetime.now.return_value = datetime(2022, 6, 29, 2, 22, 12)
operator.last_requested = datetime(2022, 6, 29, 2, 22, 10)
result = operator.get_service_list(self.consul_svc, None, False)
# _get_service_list should not be called since we're using cache.
self.assertEqual(0, m__get_service_list.call_count)
# The order of entries in the list might be different.
# Sort the lists for comparison purposes.
self.assertEqual(result, formatted_services)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_exception(self,
m__get_service_list,
m_consul):
operator = self._init_consul_operator()
operator._update_cache(self.consul_svc, None, formatted_services)
m__get_service_list.side_effect = \
consul_operator.ConsulAccessException("some error")
result = operator.get_service_list(self.consul_svc, None, True)
# Check if cache is used ConsulAccessException happens.
self.assertEqual(result, formatted_services)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_exception_with_no_cache(self,
m__get_service_list,
m_consul):
operator = self._init_consul_operator()
operator.cached_services[self.consul_svc] = {}
m__get_service_list.side_effect = \
consul_operator.ConsulAccessException("some error")
# Check if ConsulAccessException is raised again when cache is empty.
self.assertRaises(consul_operator.ConsulAccessException,
operator.get_service_list,
self.consul_svc, None, True)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_empty(self,
m__get_service_list,
m_consul):
operator = self._init_consul_operator()
operator._update_cache(self.consul_svc, None, formatted_services)
m__get_service_list.return_value = []
result = operator.get_service_list(self.consul_svc, None, True)
# Check if cache is used when we get an empty service list (no result)
# from Consul.
self.assertEqual(result, formatted_services)
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list')
def test_get_service_list_empty_with_no_cache(self,
m__get_service_list,
m_consul):
operator = self._init_consul_operator()
operator.cached_services[self.consul_svc] = {}
m__get_service_list.return_value = []
result = operator.get_service_list(self.consul_svc, None, True)
# Check if an empty result is returned when neither Consul nor cache
# has the service list we are looking for.
self.assertEqual(result, [])
@mock.patch.object(consul_operator.ConsulOperator,
'_get_service_list_across_dc')
def test_get_service_list_with_cross_dc_true(self,
m__get_service_list_across_dc,
m_consul):
operator = self._init_consul_operator()
operator.get_service_list(self.consul_svc, self.server, cross_dc=True)
m__get_service_list_across_dc.assert_called_once_with(self.consul_svc,
self.server)
@mock.patch('consul.Check')
def test_register_service(self, m_check, m_consul):
operator = self._init_consul_operator()
target = mock.Mock()
target.server = 'server'
target.exchange = "nova"
target.topic = "scheduler"
# Mock the case where both service and check registrations succeed.
m_consul.return_value.agent.service.register.return_value = True
m_consul.return_value.agent.check.register.return_value = True
name = '{}.{}'.format(target.exchange, target.topic)
tags = [target.server]
proxy_conf = mock.Mock()
proxy_conf.reverse_proxy_name = 'test_reverse_proxy'
proxy_conf.reverse_proxy_endpoint = 'http://10.123.216.5:30900/'
proxy_conf.proxy_name = ''
proxy_conf.proxy_endpoint = ''
is_registered = operator.register_service(name, 'None:10000',
'127.0.0.1', 10000, tags,
proxy_conf)
# Check if we register the service on Consul.
m_consul.return_value.agent.service.register.assert_called_once_with(
name='nova.scheduler', service_id='None:10000',
address='127.0.0.1', port=10000,
tags=['server'], token=None,
meta={
'reverse_proxy_name': 'test_reverse_proxy',
'reverse_proxy_endpoint': 'http://10.123.216.5:30900/'
})
# Check if we attach a http check to this service.
expected_url = 'http://10.123.216.5:30900/healthcheck'
expected_header = {
'x-rpchost': ['127.0.0.1'],
'x-rpchost-port': ['10000']
}
m_check.http.assert_called_once_with(expected_url, '60s', '5s', '600s',
expected_header,
tls_skip_verify=True)
m_consul.return_value.agent.check.register.assert_called_once()
# Check the final result.
self.assertIs(is_registered, True)
@mock.patch('consul.Check')
def test_register_service_without_reserve_proxy(self, m_check, m_consul):
operator = self._init_consul_operator()
target = mock.Mock()
target.server = 'server'
target.exchange = "nova"
target.topic = "scheduler"
# Mock the case where both service and check registrations succeed.
m_consul.return_value.agent.service.register.return_value = True
m_consul.return_value.agent.check.register.return_value = True
name = '{}.{}'.format(target.exchange, target.topic)
tags = [target.server]
proxy_conf = mock.Mock()
proxy_conf.reverse_proxy_name = ''
proxy_conf.reverse_proxy_endpoint = ''
proxy_conf.proxy_name = ''
proxy_conf.proxy_endpoint = ''
is_registered = operator.register_service(name, 'None:10000',
'127.0.0.1', 10000, tags,
proxy_conf)
# Check if we register the service on Consul.
m_consul.return_value.agent.service.register.assert_called_once_with(
name='nova.scheduler', service_id='None:10000',
address='127.0.0.1', port=10000,
tags=['server'],
token=None,
meta={
'reverse_proxy_name': '',
'reverse_proxy_endpoint': ''
})
# Check if we attach a http check to this service.
expected_url = 'http://127.0.0.1:10000/healthcheck'
expected_header = {}
m_check.http.assert_called_once_with(expected_url, '60s', '5s', '600s',
expected_header,
tls_skip_verify=True)
m_consul.return_value.agent.check.register.assert_called_once()
# Check the final result.
self.assertIs(is_registered, True)
def test_register_service_failed_with_service(self, m_consul):
operator = self._init_consul_operator()
# Mock the case where service registrations fails.
m_consul.return_value.agent.service.register.return_value = False
proxy_conf = mock.Mock()
is_registered = operator.register_service('nova.compute', 'None:10000',
'127.0.0.1', 10000, [],
proxy_conf)
# Check if we register the service on Consul.
m_consul.return_value.agent.service.register.assert_called_once()
# Since the service registration failed (step 1), check registration
# shouldn't be called.
m_consul.return_value.agent.check.register.assert_not_called()
# Check the final result.
self.assertIs(is_registered, False)
@mock.patch('consul.Check')
def test_register_service_with_reserve_proxy_and_proxy(self, m_check,
m_consul):
operator = self._init_consul_operator()
target = mock.Mock()
target.server = 'server'
target.exchange = "nova"
target.topic = "scheduler"
# Mock the case where both service and check registrations succeed.
m_consul.return_value.agent.service.register.return_value = True
m_consul.return_value.agent.check.register.return_value = True
name = '{}.{}'.format(target.exchange, target.topic)
tags = [target.server]
proxy_conf = mock.Mock()
proxy_conf.reverse_proxy_name = 'test_reverse_proxy'
proxy_conf.reverse_proxy_endpoint = 'http://10.123.216.5:30900/'
proxy_conf.proxy_name = 'test_proxy'
proxy_conf.proxy_endpoint = 'http://10.123.216.8:30288/'
is_registered = operator.register_service(name, 'None:10000',
'127.0.0.1', 10000, tags,
proxy_conf)
# Check if we register the service on Consul.
m_consul.return_value.agent.service.register.assert_called_once_with(
name='nova.scheduler', service_id='None:10000',
address='127.0.0.1', port=10000,
tags=['server'],
token=None,
meta={
'reverse_proxy_name': 'test_reverse_proxy',
'reverse_proxy_endpoint': 'http://10.123.216.5:30900/'
})
# Check if we attach a http check to this service.
expected_url = 'http://127.0.0.1:10000/healthcheck'
# When reverse proxy and proxy co-exist, health check doesn't need to
# go though reverse-proxy.
expected_header = {}
m_check.http.assert_called_once_with(expected_url, '60s', '5s', '600s',
expected_header,
tls_skip_verify=True)
m_consul.return_value.agent.check.register.assert_called_once()
# Check the final result.
self.assertIs(is_registered, True)
@mock.patch('consul.Check')
def test_register_service_with_proxy_only(self, m_check, m_consul):
operator = self._init_consul_operator()
target = mock.Mock()
target.server = 'server'
target.exchange = "nova"
target.topic = "scheduler"
# Mock the case where both service and check registrations succeed.
m_consul.return_value.agent.service.register.return_value = True
m_consul.return_value.agent.check.register.return_value = True
name = '{}.{}'.format(target.exchange, target.topic)
tags = [target.server]
proxy_conf = mock.Mock()
proxy_conf.reverse_proxy_name = ''
proxy_conf.reverse_proxy_endpoint = ''
proxy_conf.proxy_name = 'test_proxy'
proxy_conf.proxy_endpoint = 'http://10.123.216.8:30288/'
is_registered = operator.register_service(name, 'None:10000',
'127.0.0.1', 10000, tags,
proxy_conf)
# Check if we register the service on Consul.
m_consul.return_value.agent.service.register.assert_called_once_with(
name='nova.scheduler', service_id='None:10000',
address='127.0.0.1', port=10000,
tags=['server'],
token=None,
meta={
'reverse_proxy_name': '',
'reverse_proxy_endpoint': ''
})
# Check if we attach a http check to this service.
expected_url = 'http://127.0.0.1:10000/healthcheck'
expected_header = {}
m_check.http.assert_called_once_with(expected_url, '60s', '5s', '600s',
expected_header,
tls_skip_verify=True)
m_consul.return_value.agent.check.register.assert_called_once()
# Check the final result.
self.assertIs(is_registered, True)
def test_register_service_failed_with_check(self, m_consul):
operator = self._init_consul_operator()
# Mock the case where only check registrations fails.
m_consul.return_value.agent.service.register.return_value = True
m_consul.return_value.agent.check.register.return_value = False
proxy_conf = mock.Mock()
is_registered = operator.register_service('nova.compute', 'None:10000',
'127.0.0.1', 10000, [],
proxy_conf)
# Check if we register the service on Consul.
m_consul.return_value.agent.service.register.assert_called_once()
# Since the service registration succeed (step 1), check registration
# should proceed.
m_consul.return_value.agent.check.register.assert_called_once()
# The final result should be False because check registration failed.
self.assertIs(is_registered, False)
def test_deregister_service_normal(self, m_consul):
operator = self._init_consul_operator()
operator.deregister_service('fake-service-id')
m_consul.return_value.agent.service.deregister.assert_called_once_with(
'fake-service-id')
def test_deregister_service_raised_consul_exception(self, m_consul):
operator = self._init_consul_operator()
m_consul.return_value.agent.service.deregister.side_effect = [
consul.ConsulException('500 Unknown service "faile-service-id')]
operator.deregister_service('fake-service-id')
m_consul.return_value.agent.service.deregister.assert_called_once_with(
'fake-service-id')
def test_deregister_service_raised_exception(self, m_consul):
operator = self._init_consul_operator()
m_consul.return_value.agent.service.deregister.side_effect = [
Exception('Unknown exception')]
operator.deregister_service('fake-service-id')
m_consul.return_value.agent.service.deregister.assert_called_once_with(
'fake-service-id')

View File

@ -28,3 +28,6 @@ oslo.middleware>=3.31.0 # Apache-2.0
# metrics
oslo.metrics>=0.2.1 # Apache-2.0
# http driver
python-consul2<=0.1.5 # Apache-2.0