|
| 1 | +# Copyright 2013-2017 DataStax, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from tests.integration import CCM_KWARGS, use_cluster, remove_cluster, MockLoggingHandler |
| 17 | +from tests.integration import setup_keyspace |
| 18 | + |
| 19 | +from cassandra.cluster import Cluster |
| 20 | +from cassandra import cluster |
| 21 | + |
| 22 | +from collections import namedtuple |
| 23 | +from functools import wraps |
| 24 | +from threading import Thread, Event |
| 25 | +from ccmlib.node import TimeoutError |
| 26 | +import time |
| 27 | +import logging |
| 28 | + |
| 29 | +try: |
| 30 | + import unittest2 as unittest |
| 31 | +except ImportError: |
| 32 | + import unittest # noqa |
| 33 | + |
| 34 | + |
| 35 | +def setup_module(): |
| 36 | + remove_cluster() |
| 37 | + |
| 38 | + |
| 39 | +UPGRADE_CLUSTER = "upgrade_cluster" |
| 40 | +UpgradePath = namedtuple('UpgradePath', ('name', 'starting_version', 'upgrade_version', 'configuration_options')) |
| 41 | + |
| 42 | + |
| 43 | +class upgrade_paths(object): |
| 44 | + """ |
| 45 | + Decorator used to specify the upgrade paths for a particular method |
| 46 | + """ |
| 47 | + def __init__(self, paths): |
| 48 | + self.paths = paths |
| 49 | + |
| 50 | + def __call__(self, method): |
| 51 | + @wraps(method) |
| 52 | + def wrapper(*args, **kwargs): |
| 53 | + for path in self.paths: |
| 54 | + self_from_decorated = args[0] |
| 55 | + self_from_decorated.UPGRADE_PATH = path |
| 56 | + self_from_decorated._setUp() |
| 57 | + method(*args, **kwargs) |
| 58 | + self_from_decorated._tearDown() |
| 59 | + return wrapper |
| 60 | + |
| 61 | + |
| 62 | +class UpgradeBase(unittest.TestCase): |
| 63 | + """ |
| 64 | + Base class for the upgrade tests. The _setup method |
| 65 | + will clean the environment and start the appropriate C* version according |
| 66 | + to the upgrade path. The upgrade can be done in a different thread using the |
| 67 | + start_upgrade upgrade_method (this would be the most realistic scenario) |
| 68 | + or node by node, waiting for the upgrade to happen, using _upgrade_one_node method |
| 69 | + """ |
| 70 | + UPGRADE_PATH = None |
| 71 | + start_cluster = True |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def setUpClass(cls): |
| 75 | + cls.logger_handler = MockLoggingHandler() |
| 76 | + logger = logging.getLogger(cluster.__name__) |
| 77 | + logger.addHandler(cls.logger_handler) |
| 78 | + |
| 79 | + def _setUp(self): |
| 80 | + """ |
| 81 | + This is not the regular _setUp method because it will be called from |
| 82 | + the decorator instead of letting nose handle it. |
| 83 | + This setup method will start a cluster with the right version according |
| 84 | + to the variable UPGRADE_PATH. |
| 85 | + """ |
| 86 | + remove_cluster() |
| 87 | + self.cluster = use_cluster(UPGRADE_CLUSTER + self.UPGRADE_PATH.name, [3], |
| 88 | + ccm_options=self.UPGRADE_PATH.starting_version, |
| 89 | + configuration_options=self.UPGRADE_PATH.configuration_options) |
| 90 | + self.nodes = self.cluster.nodelist() |
| 91 | + self.last_node_upgraded = None |
| 92 | + self.upgrade_done = Event() |
| 93 | + self.upgrade_thread = None |
| 94 | + |
| 95 | + if self.start_cluster: |
| 96 | + setup_keyspace() |
| 97 | + |
| 98 | + self.cluster_driver = Cluster() |
| 99 | + self.session = self.cluster_driver.connect() |
| 100 | + self.logger_handler.reset() |
| 101 | + |
| 102 | + def _tearDown(self): |
| 103 | + """ |
| 104 | + special tearDown method called by the decorator after the method has ended |
| 105 | + """ |
| 106 | + if self.upgrade_thread: |
| 107 | + self.upgrade_thread.join(timeout=5) |
| 108 | + self.upgrade_thread = None |
| 109 | + |
| 110 | + if self.start_cluster: |
| 111 | + self.cluster_driver.shutdown() |
| 112 | + |
| 113 | + def start_upgrade(self, time_node_upgrade): |
| 114 | + """ |
| 115 | + Starts the upgrade in a different thread |
| 116 | + """ |
| 117 | + self.upgrade_thread = Thread(target=self._upgrade, args=(time_node_upgrade,)) |
| 118 | + self.upgrade_thread.start() |
| 119 | + |
| 120 | + def _upgrade(self, time_node_upgrade): |
| 121 | + """ |
| 122 | + Starts the upgrade in the same thread |
| 123 | + """ |
| 124 | + start_time = time.time() |
| 125 | + while self._upgrade_one_node(): |
| 126 | + end_time = time.time() |
| 127 | + time_to_upgrade = end_time - start_time |
| 128 | + if time_node_upgrade > time_to_upgrade: |
| 129 | + time.sleep(time_node_upgrade - time_to_upgrade) |
| 130 | + self.upgrade_done.set() |
| 131 | + |
| 132 | + def is_upgraded(self): |
| 133 | + """ |
| 134 | + Returns True if the upgrade has finished and False otherwise |
| 135 | + """ |
| 136 | + return self.upgrade_done.is_set() |
| 137 | + |
| 138 | + def wait_for_upgrade(self, timeout=None): |
| 139 | + """ |
| 140 | + Waits until the upgrade has completed |
| 141 | + """ |
| 142 | + self.upgrade_done.wait(timeout=timeout) |
| 143 | + |
| 144 | + def _upgrade_one_node(self): |
| 145 | + """ |
| 146 | + Upgrades only one node. Return True if the upgrade |
| 147 | + has finished and False otherwise |
| 148 | + """ |
| 149 | + if self.last_node_upgraded is None: |
| 150 | + node_to_upgrade = self.nodes[0] |
| 151 | + self.last_node_upgraded = 0 |
| 152 | + else: |
| 153 | + if len(self.nodes) - 1 == self.last_node_upgraded: |
| 154 | + return False |
| 155 | + self.last_node_upgraded += 1 |
| 156 | + node_to_upgrade = self.nodes[self.last_node_upgraded] |
| 157 | + |
| 158 | + node_to_upgrade.drain() |
| 159 | + node_to_upgrade.stop(gently=True) |
| 160 | + |
| 161 | + node_to_upgrade.set_install_dir(**self.UPGRADE_PATH.upgrade_version) |
| 162 | + |
| 163 | + # There must be a cleaner way of doing this, but it's necessary here |
| 164 | + # to call the private method from cluster __update_topology_files |
| 165 | + self.cluster._Cluster__update_topology_files() |
| 166 | + try: |
| 167 | + node_to_upgrade.start(wait_for_binary_proto=True, wait_other_notice=True) |
| 168 | + except TimeoutError: |
| 169 | + self.fail("Error starting C* node while upgrading") |
| 170 | + |
| 171 | + return True |
| 172 | + |
| 173 | + |
| 174 | +class UpgradeBaseAuth(UpgradeBase): |
| 175 | + """ |
| 176 | + Base class of authentication test, the authentication parameters for |
| 177 | + C* still have to be specified within the upgrade path variable |
| 178 | + """ |
| 179 | + start_cluster = False |
0 commit comments