From 6fe9c32e4ae096a33c8aa7fc03c091b954558ab4 Mon Sep 17 00:00:00 2001 From: Jenna Kleiman Date: Fri, 16 Jun 2017 14:32:02 -0500 Subject: [PATCH] Remove Health Checker This commit deletes the health_checker and the associated unit test. Change-Id: I0a1e4d2609c42607b021a1a5b1d3e7f29cbfea5f --- .../optimizer/ostro_server/health_checker.py | 163 ------------------ valet/tests/unit/engine/test_ping.py | 93 ---------- 2 files changed, 256 deletions(-) delete mode 100644 valet/engine/optimizer/ostro_server/health_checker.py delete mode 100644 valet/tests/unit/engine/test_ping.py diff --git a/valet/engine/optimizer/ostro_server/health_checker.py b/valet/engine/optimizer/ostro_server/health_checker.py deleted file mode 100644 index 6377d9f..0000000 --- a/valet/engine/optimizer/ostro_server/health_checker.py +++ /dev/null @@ -1,163 +0,0 @@ -# -# Copyright 2014-2017 AT&T Intellectual Property -# -# 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 json -import os -import sys -import time -import uuid - -from oslo_config import cfg - -from valet.common.conf import get_logger -from valet.common.music import REST -from valet.engine.conf import init_engine - -CONF = cfg.CONF -LOG = get_logger(__name__) - - -class HealthCheck(object): - - rest = None - - def __init__(self, hosts=[]): - # default health_timeout=10 - self.tries = CONF.engine.health_timeout * 2 - self.uuid = str(uuid.uuid4()) - - kwargs = { - 'hosts': hosts, - 'port': CONF.music.port, - 'path': '/MUSIC/rest', - 'timeout': CONF.music.interval, - } - self.rest = REST(**kwargs) - - def ping(self): - - engine_id = None - try: - if self._send(): - engine_id = self._read_response() - finally: - self._delete_result() - return engine_id - - def _send(self): - request = [ - { - "action": "ping", - "stack_id": self.uuid - } - ] - - data = { - "values": { - "stack_id": self.uuid, - "request": request - }, - "consistencyInfo": { - "type": "eventual" - } - } - - path = '/keyspaces/%(keyspace)s/tables/%(table)s/rows' % { - 'keyspace': CONF.music.keyspace, - 'table': CONF.music.request_table, - } - response = self.rest.request(method='post', path=path, data=data) - - return response.status_code == 204 if response else False - - def _read_response(self): - - engine_id = None - pre = '/keyspaces/%(keyspace)s/tables/%(table)s/rows?stack_id=%(uid)s' - path = pre % { - 'keyspace': CONF.music.keyspace, - 'table': CONF.music.response_table, - 'uid': self.uuid, - } - - # default 20 tries * 0.5 sec = 10 sec. timeout - for i in range(self.tries): - time.sleep(0.5) - try: - response = self.rest.request(method='get', path=path) - - if response.status_code == 200 and len(response.text) > 3: - - j = json.loads(response.text) - if j['row 0']['stack_id'] != self.uuid: - continue - - placement = json.loads(j['row 0']['placement']) - engine_id = placement['resources']['id'] - break - except Exception as e: - LOG.warning("HealthCheck exception in read response, ", str(e)) - - return engine_id - - def _delete_result(self): - # leave a clean table - delete from requests and responses - data = { - "consistencyInfo": {"type": "eventual"} - } - - base = '/keyspaces/%(keyspace)s/tables/%(table)s/rows?stack_id=%(uid)s' - try: - path = base % { - 'keyspace': CONF.music.keyspace, - 'table': CONF.music.request_table, - 'uid': self.uuid - } - self.rest.request(method='delete', path=path, data=data) - except Exception as e: - LOG.warning("HealthCheck exception in delete request, ", str(e)) - - try: - path = base % { - 'keyspace': CONF.music.keyspace, - 'table': CONF.music.response_table, - 'uid': self.uuid - } - self.rest.request(method='delete', path=path, data=data) - except Exception as e: - LOG.warning("HealthCheck exception in delete response, ", str(e)) - - -if __name__ == "__main__": - - respondent_id = None - code = 0 - init_engine(default_config_files=['/etc/valet/valet.conf']) - - if os.path.exists(CONF.engine.pid): - respondent_id = HealthCheck().ping() - - if respondent_id == CONF.engine.priority: - code = CONF.engine.priority - LOG.info("HealthCheck - Alive, " - "respondent instance id: {}".format(respondent_id)) - else: - LOG.warning("HealthCheck - pid file exists, " - "engine {} did not respond in a timely manner " - "(respondent id {})".format(CONF.engine.priority, - respondent_id)) - else: - LOG.info("HealthCheck - no pid file, engine is not running!") - sys.exit(code) diff --git a/valet/tests/unit/engine/test_ping.py b/valet/tests/unit/engine/test_ping.py deleted file mode 100644 index c02a739..0000000 --- a/valet/tests/unit/engine/test_ping.py +++ /dev/null @@ -1,93 +0,0 @@ -# -# Copyright 2014-2017 AT&T Intellectual Property -# -# 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 mock - -import valet.engine.optimizer.ostro_server.health_checker as ping -from valet.engine.optimizer.ostro_server.health_checker import HealthCheck -from valet.tests.base import Base - -json = (r'{"row 0":{"placement": "{\"status\": {\"message\": ' - r'\"ping\", \"type\": \"ok\"},\"resources\": ' - r'{\"ip\": \"localhost\", \"id\": %d}}","stack_id":"%s"}}') - - -class TestHealthCheck(Base): - - def setUp(self): - super(TestHealthCheck, self).setUp() - ping.CONF = mock.MagicMock() - ping.REST = mock.MagicMock() - self.pingger = HealthCheck() - - @mock.patch.object(HealthCheck, '_send') - @mock.patch.object(HealthCheck, '_read_response') - def test_ping(self, mock_read, mock_send): - mock_send.return_value = True - mock_read.return_value = True - - self.validate_test(self.pingger.ping() == 1) - - @mock.patch.object(HealthCheck, '_send') - @mock.patch.object(HealthCheck, '_read_response') - def test_ping_unhappy(self, mock_read, mock_send): - mock_send.return_value = False - mock_read.return_value = True - - self.validate_test(self.pingger.ping() is None) - - @mock.patch.object(HealthCheck, '_send') - @mock.patch.object(HealthCheck, '_read_response') - def test_ping_unhappy_2(self, mock_read, mock_send): - mock_send.return_value = True - mock_read.return_value = False - - self.validate_test(not self.pingger.ping()) - - def test_send(self): - self.pingger.rest.request.return_value.status_code = 204 - self.validate_test(self.pingger._send()) - - def test_send_unhappy(self): - self.pingger.rest.request.return_value.status_code = 200 - self.validate_test(not self.pingger._send()) - - def test_read_response(self): - mid = 1 - self.pingger.rest.request.return_value.status_code = 200 - self.pingger.rest.request.return_value.text = json % ( - mid, self.pingger.uuid) - self.validate_test(self.pingger._read_response()) - - def test_read_response_from_other_engine(self): - my_id = 1 - self.pingger.rest.request.return_value.status_code = 200 - self.pingger.rest.request.return_value.text = json % ( - my_id, self.pingger.uuid) - self.validate_test(not self.pingger._read_response() == 2) - - def test_read_response_unhappy_wrong_res_code(self): - self.pingger.rest.request.return_value.status_code = 204 - self.pingger.rest.request.return_value.text = self.pingger.uuid - self.validate_test(not self.pingger._read_response()) - - def test_read_response_unhappy_wrong_body(self): - self.pingger.rest.request.return_value.status_code = 200 - self.pingger.rest.request.return_value.text = "" - self.validate_test(not self.pingger._read_response()) - - def test_read_response_unhappy_wrong_both(self): - self.pingger.rest.request.return_value.status_code = 204 - self.pingger.rest.request.return_value.text = "" - self.validate_test(not self.pingger._read_response())