Commit e467866b authored by Ramon Nou's avatar Ramon Nou
Browse files

Validate replica count during client startup

parent 38dfe2e0
Loading
Loading
Loading
Loading
+12 −0
Changes for include/client/rpc/replica.hpp: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -149,6 +149,18 @@ mutation_supported_with_replicas(const int replica_count) {
    return replica_count == 0;
}

inline void
validate_replica_count(const int replica_count, const std::size_t host_count) {
    if(replica_count < 0) {
        throw std::invalid_argument("LIBGKFS_NUM_REPL must not be negative");
    }
    if(host_count == 0 ||
       static_cast<std::size_t>(replica_count) >= host_count) {
        throw std::invalid_argument(
                "LIBGKFS_NUM_REPL must be less than the number of hosts");
    }
}

inline void
record_replica_read(const int copy) {
    if(copy == 0) {
+1 −0
Changes for src/client/preload.cpp: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -319,6 +319,7 @@ init_environment() {

    try {
        gkfs::utils::connect_to_hosts(hosts);
        gkfs::rpc::validate_replica_count(CTX->get_replicas(), hosts.size());
        if(CTX->use_proxy()) {
            LOG(INFO, "Connecting to proxy...");
            gkfs::utils::lookup_proxy_addr();
+6 −2
Changes for src/client/preload_context.cpp: 6 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -95,8 +95,12 @@ PreloadContext::PreloadContext()
            e.what());
        cwd_ = "/";
    }
    PreloadContext::set_replicas(
            std::stoi(gkfs::env::get_var(gkfs::env::NUM_REPL, "0")));
    const auto replicas =
            std::stoi(gkfs::env::get_var(gkfs::env::NUM_REPL, "0"));
    if(replicas < 0) {
        throw std::invalid_argument("LIBGKFS_NUM_REPL must not be negative");
    }
    PreloadContext::set_replicas(replicas);

    const std::string env_dirents_buff_size =
            gkfs::env::get_var(gkfs::env::DIRENTS_BUFF_SIZE);
+14 −0
Changes for tests/unit/test_replica_placement.cpp: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -57,6 +57,20 @@ TEST_CASE("locate_replicas rejects a negative replica count", "[replication]") {
            "replica count must not be negative");
}

TEST_CASE("replica count validation matches host capacity", "[replication]") {
    REQUIRE_NOTHROW(gkfs::rpc::validate_replica_count(0, 1));
    REQUIRE_NOTHROW(gkfs::rpc::validate_replica_count(2, 3));
    REQUIRE_THROWS_WITH(
            gkfs::rpc::validate_replica_count(-1, 4),
            "LIBGKFS_NUM_REPL must not be negative");
    REQUIRE_THROWS_WITH(
            gkfs::rpc::validate_replica_count(3, 3),
            "LIBGKFS_NUM_REPL must be less than the number of hosts");
    REQUIRE_THROWS_WITH(
            gkfs::rpc::validate_replica_count(0, 0),
            "LIBGKFS_NUM_REPL must be less than the number of hosts");
}

TEST_CASE("locate_metadata_replicas returns metadata targets in copy order",
          "[replication]") {
    const gkfs::rpc::SimpleHashDistributor distributor(0, 8);