|
| 1 | +#!/usr/bin/python -u |
| 2 | +# Copyright (c) 2010-2012 OpenStack Foundation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | +# implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +from swiftclient import client |
| 20 | +from unittest import main |
| 21 | + |
| 22 | +from swift.common.exceptions import LockTimeout |
| 23 | +from swift.common.manager import Manager |
| 24 | +from swift.common.utils import hash_path, readconf, Timestamp |
| 25 | +from swift.container.backend import ContainerBroker |
| 26 | + |
| 27 | +from test.probe.common import ( |
| 28 | + kill_nonprimary_server, kill_server, start_server, ReplProbeTest) |
| 29 | + |
| 30 | +# Why is this not called test_container_orphan? Because the crash |
| 31 | +# happens in the account server, so both account and container |
| 32 | +# services are involved. |
| 33 | +# |
| 34 | +# The common way users do this is to use TripleO to deploy an overcloud |
| 35 | +# and add Gnocchi. Gnocchi is hammering Swift, its container has updates |
| 36 | +# all the time. Then, users crash the overcloud and re-deploy it, |
| 37 | +# using the new suffix in swift.conf. Thereafter, container service |
| 38 | +# inherits old container with outstanding updates, container updater |
| 39 | +# tries to send updates to the account server, while the account cannot |
| 40 | +# be found anymore. In this situation, in Swift 2.25.0, account server |
| 41 | +# tracebacks, and the cycle continues without end. |
| 42 | + |
| 43 | + |
| 44 | +class TestOrphanContainer(ReplProbeTest): |
| 45 | + |
| 46 | + def get_account_db_files(self, account): |
| 47 | + |
| 48 | + # This is "more correct" than (port_num%100)//10, but is it worth it? |
| 49 | + # We have the assumption about port_num vs node_id embedded all over. |
| 50 | + account_configs = {} |
| 51 | + for _, cname in self.configs['account-server'].items(): |
| 52 | + conf = readconf(cname) |
| 53 | + # config parser cannot know if it's a number or not, so int() |
| 54 | + port = int(conf['app:account-server']['bind_port']) |
| 55 | + account_configs[port] = conf |
| 56 | + |
| 57 | + part, nodes = self.account_ring.get_nodes(account) |
| 58 | + hash_str = hash_path(account) |
| 59 | + |
| 60 | + ret = [] |
| 61 | + for node in nodes: |
| 62 | + |
| 63 | + data_dir = 'accounts' |
| 64 | + device = node['device'] |
| 65 | + conf = account_configs[node['port']] |
| 66 | + devices = conf['app:account-server']['devices'] |
| 67 | + |
| 68 | + # os.path.join is for the weak |
| 69 | + db_file = '%s/%s/%s/%s/%s/%s/%s.db' % ( |
| 70 | + devices, device, data_dir, part, |
| 71 | + hash_str[-3:], hash_str, hash_str) |
| 72 | + ret.append(db_file) |
| 73 | + return ret |
| 74 | + |
| 75 | + def test_update_pending(self): |
| 76 | + |
| 77 | + # Create container |
| 78 | + container = 'contx' |
| 79 | + client.put_container(self.url, self.token, container) |
| 80 | + |
| 81 | + part, nodes = self.account_ring.get_nodes(self.account) |
| 82 | + anode = nodes[0] |
| 83 | + |
| 84 | + # Stop a quorum of account servers |
| 85 | + # This allows the put to continue later. |
| 86 | + kill_nonprimary_server(nodes, self.ipport2server) |
| 87 | + kill_server((anode['ip'], anode['port']), self.ipport2server) |
| 88 | + |
| 89 | + # Put object |
| 90 | + # This creates an outstanding update. |
| 91 | + client.put_object(self.url, self.token, container, 'object1', b'123') |
| 92 | + |
| 93 | + cont_db_files = self.get_container_db_files(container) |
| 94 | + self.assertEqual(len(cont_db_files), 3) |
| 95 | + |
| 96 | + # Collect the observable state from containers |
| 97 | + outstanding_files = [] |
| 98 | + for cfile in cont_db_files: |
| 99 | + broker = ContainerBroker(cfile) |
| 100 | + try: |
| 101 | + info = broker.get_info() |
| 102 | + except LockTimeout: |
| 103 | + self.fail('LockTimeout at %s' % (cfile,)) |
| 104 | + if Timestamp(info['put_timestamp']) <= 0: |
| 105 | + self.fail('No put_timestamp at %s' % (cfile,)) |
| 106 | + # Correct even if reported_put_timestamp is zero. |
| 107 | + if info['put_timestamp'] > info['reported_put_timestamp']: |
| 108 | + outstanding_files.append(cfile) |
| 109 | + self.assertGreater(len(outstanding_files), 0) |
| 110 | + |
| 111 | + # At this point the users shut everything down and screw up the |
| 112 | + # hash in swift.conf. But we destroy the account DB instead. |
| 113 | + files = self.get_account_db_files(self.account) |
| 114 | + for afile in files: |
| 115 | + os.unlink(afile) |
| 116 | + |
| 117 | + # Restart the stopped primary server |
| 118 | + start_server((anode['ip'], anode['port']), self.ipport2server) |
| 119 | + |
| 120 | + # Make sure updaters run |
| 121 | + Manager(['container-updater']).once() |
| 122 | + |
| 123 | + # Collect the observable state from containers again and examine it |
| 124 | + outstanding_files_new = [] |
| 125 | + for cfile in cont_db_files: |
| 126 | + |
| 127 | + # We aren't catching DatabaseConnectionError, because |
| 128 | + # we only want to approve of DBs that were quarantined, |
| 129 | + # and not otherwise damaged. So if the code below throws |
| 130 | + # an exception for other reason, we want the test to fail. |
| 131 | + if not os.path.exists(cfile): |
| 132 | + continue |
| 133 | + |
| 134 | + broker = ContainerBroker(cfile) |
| 135 | + try: |
| 136 | + info = broker.get_info() |
| 137 | + except LockTimeout: |
| 138 | + self.fail('LockTimeout at %s' % (cfile,)) |
| 139 | + if Timestamp(info['put_timestamp']) <= 0: |
| 140 | + self.fail('No put_timestamp at %s' % (cfile,)) |
| 141 | + # Correct even if reported_put_timestamp is zero. |
| 142 | + if info['put_timestamp'] > info['reported_put_timestamp']: |
| 143 | + outstanding_files_new.append(cfile) |
| 144 | + self.assertLengthEqual(outstanding_files_new, 0) |
| 145 | + |
| 146 | + self.get_to_final_state() |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == '__main__': |
| 150 | + main() |
0 commit comments