Files
scylladb/test/boost/snitch_reset_test.cc
Pavel Emelyanov 38c77d0d85 snitch: Keep gossiper reference
The reference is put on the snitch_ptr because this is the sharded<>
thing and because gossiper reference is the same for different snitch
drivers. Also, getting gossiper from snitch_ptr by driver will look
simpler than getting it from any base class.

Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>
2022-05-03 10:57:40 +03:00

149 lines
5.5 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <boost/test/unit_test.hpp>
#include "locator/gossiping_property_file_snitch.hh"
#include "utils/fb_utilities.hh"
#include "gms/gossiper.hh"
#include <seastar/testing/test_case.hh>
#include <seastar/util/std-compat.hh>
#include <vector>
#include <string>
#include <tuple>
namespace fs = std::filesystem;
static fs::path test_files_subdir("test/resource/snitch_property_files");
future<> one_test(const std::string& property_fname1,
const std::string& property_fname2,
bool exp_result) {
using namespace locator;
using namespace std::filesystem;
utils::fb_utilities::set_broadcast_address(gms::inet_address("localhost"));
utils::fb_utilities::set_broadcast_rpc_address(gms::inet_address("localhost"));
printf("Testing %s and %s property files. Expected result is %s\n",
property_fname1.c_str(), property_fname2.c_str(),
(exp_result ? "success" : "failure"));
return seastar::async([&property_fname1, &property_fname2, exp_result] {
auto cpu0_dc = make_lw_shared<sstring>();
auto cpu0_rack = make_lw_shared<sstring>();
auto cpu0_dc_new = make_lw_shared<sstring>();
auto cpu0_rack_new = make_lw_shared<sstring>();
auto my_address = utils::fb_utilities::get_broadcast_address();
sharded<gms::gossiper> g;
sharded<snitch_ptr>& snitch = i_endpoint_snitch::snitch_instance();
try {
path fname1(test_files_subdir);
fname1 /= path(property_fname1);
path fname2(test_files_subdir);
fname2 /= path(property_fname2);
try {
snitch_config cfg;
cfg.name = "org.apache.cassandra.locator.GossipingPropertyFileSnitch";
cfg.properties_file_name = fname1.string();
snitch.start(cfg, std::ref(g)).get();
snitch.invoke_on_all(&snitch_ptr::start).get();
} catch (std::exception& e) {
printf("%s\n", e.what());
BOOST_ERROR("Failed to create an initial snitch");
return;
}
i_endpoint_snitch::snitch_instance().invoke_on(0,
[cpu0_dc, cpu0_rack, my_address] (snitch_ptr& inst) {
*cpu0_dc =inst->get_datacenter(my_address);
*cpu0_rack = inst->get_rack(my_address);
}).get();
snitch_config cfg;
cfg.name = "org.apache.cassandra.locator.GossipingPropertyFileSnitch";
cfg.properties_file_name = fname2.string();
i_endpoint_snitch::reset_snitch(cfg).get();
if (!exp_result) {
BOOST_ERROR("Failed to catch an error in a malformed "
"configuration file");
snitch.stop().get();
return;
}
auto res = make_lw_shared<bool>(true);
// Check that the returned DC and Rack values are different now
i_endpoint_snitch::snitch_instance().invoke_on(0,
[cpu0_dc_new, cpu0_rack_new, my_address] (snitch_ptr& inst) {
*cpu0_dc_new =inst->get_datacenter(my_address);
*cpu0_rack_new = inst->get_rack(my_address);
}).get();
if (*cpu0_dc == *cpu0_dc_new || *cpu0_rack == *cpu0_rack_new) {
BOOST_ERROR("Data center or Rack haven't been updated");
snitch.stop().get();
return;
}
// Check that the new DC and Rack values have been propagated to all CPUs
i_endpoint_snitch::snitch_instance().invoke_on_all(
[cpu0_dc_new, cpu0_rack_new, res, my_address] (snitch_ptr& inst) {
if (*cpu0_dc_new != inst->get_datacenter(my_address) ||
*cpu0_rack_new != inst->get_rack(my_address)) {
*res = false;
}
}).get();
if (!*res) {
BOOST_ERROR("Data center or Rack do not match on "
"different shards");
} else {
BOOST_CHECK(true);
}
snitch.stop().get();
} catch (std::exception& e) {
if (!exp_result) {
//
// Verify that the returned DC and Rack values remained the same
// despite the insuccessful reset_snitch() call.
//
i_endpoint_snitch::snitch_instance().invoke_on(0,
[cpu0_dc_new, cpu0_rack_new, my_address] (snitch_ptr& inst) {
*cpu0_dc_new =inst->get_datacenter(my_address);
*cpu0_rack_new = inst->get_rack(my_address);
}).get();
if (*cpu0_dc != *cpu0_dc_new || *cpu0_rack != *cpu0_rack_new) {
BOOST_ERROR("Data center or Rack have been updated while they shouldn't have");
snitch.stop().get();
return;
}
}
BOOST_CHECK(!exp_result);
snitch.stop().get();
}
});
}
#define RESET_SNITCH_TEST_CASE(tag1, tag2, exp_res) \
SEASTAR_TEST_CASE(tag2) { \
return one_test(#tag1".property", #tag2".property", exp_res); \
}
////////////////////////////////////////////////////////////////////////////////
RESET_SNITCH_TEST_CASE(good_2, bad_format_1, false);
RESET_SNITCH_TEST_CASE(good_1, good_2, true);