Unverified Commit b5a8c4bc authored by Joseph Noir's avatar Joseph Noir Committed by GitHub

Merge pull request #863

Make node_id opaque and add an URI-based alternative
parents c58b6a08 9d4ff082
...@@ -54,6 +54,7 @@ set(LIBCAF_CORE_SRCS ...@@ -54,6 +54,7 @@ set(LIBCAF_CORE_SRCS
src/event_based_actor.cpp src/event_based_actor.cpp
src/execution_unit.cpp src/execution_unit.cpp
src/exit_reason.cpp src/exit_reason.cpp
src/fnv_hash.cpp
src/forwarding_actor_proxy.cpp src/forwarding_actor_proxy.cpp
src/get_mac_addresses.cpp src/get_mac_addresses.cpp
src/get_process_id.cpp src/get_process_id.cpp
......
...@@ -20,16 +20,16 @@ ...@@ -20,16 +20,16 @@
#include <atomic> #include <atomic>
#include "caf/fwd.hpp" #include "caf/config.hpp"
#include "caf/error.hpp" #include "caf/error.hpp"
#include "caf/node_id.hpp" #include "caf/fwd.hpp"
#include "caf/intrusive_ptr.hpp" #include "caf/intrusive_ptr.hpp"
#include "caf/weak_intrusive_ptr.hpp"
#include "caf/meta/type_name.hpp"
#include "caf/meta/save_callback.hpp"
#include "caf/meta/load_callback.hpp" #include "caf/meta/load_callback.hpp"
#include "caf/meta/omittable_if_none.hpp" #include "caf/meta/omittable_if_none.hpp"
#include "caf/meta/save_callback.hpp"
#include "caf/meta/type_name.hpp"
#include "caf/node_id.hpp"
#include "caf/weak_intrusive_ptr.hpp"
namespace caf { namespace caf {
......
...@@ -40,8 +40,12 @@ std::string to_string(const atom_value& what); ...@@ -40,8 +40,12 @@ std::string to_string(const atom_value& what);
/// @relates atom_value /// @relates atom_value
atom_value to_lowercase(atom_value x); atom_value to_lowercase(atom_value x);
/// @relates atom_value
atom_value atom_from_string(string_view x); atom_value atom_from_string(string_view x);
/// @relates atom_value
int compare(atom_value x, atom_value y);
/// Creates an atom from given string literal. /// Creates an atom from given string literal.
template <size_t Size> template <size_t Size>
constexpr atom_value atom(char const (&str)[Size]) { constexpr atom_value atom(char const (&str)[Size]) {
...@@ -219,4 +223,3 @@ struct hash<caf::atom_value> { ...@@ -219,4 +223,3 @@ struct hash<caf::atom_value> {
}; };
} // namespace std } // namespace std
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <cstdint>
#include <type_traits>
#include "caf/detail/type_traits.hpp"
namespace caf {
namespace detail {
/// Non-cryptographic hash function named after Glenn Fowler, Landon Curt Noll,
/// and Kiem-Phong Vo.
///
/// See:
/// - https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// - http://www.isthe.com/chongo/tech/comp/fnv/index.html
size_t fnv_hash(const unsigned char* first, const unsigned char* last);
size_t fnv_hash_append(size_t intermediate, const unsigned char* first,
const unsigned char* last);
template <class T>
enable_if_t<has_data_member<T>::value, size_t> fnv_hash(const T& x) {
auto ptr = x.data();
auto first = reinterpret_cast<const uint8_t*>(ptr);
auto last = first + (sizeof(decay_t<decltype(*ptr)>) * x.size());
return fnv_hash(first, last);
}
template <class T>
enable_if_t<has_data_member<T>::value, size_t>
fnv_hash_append(size_t intermediate, const T& x) {
auto ptr = x.data();
auto first = reinterpret_cast<const uint8_t*>(ptr);
auto last = first + (sizeof(decay_t<decltype(*ptr)>) * x.size());
return fnv_hash_append(intermediate, first, last);
}
template <class T>
enable_if_t<std::is_integral<T>::value, size_t> fnv_hash(const T& x) {
auto first = reinterpret_cast<const uint8_t*>(&x);
return fnv_hash(first, first + sizeof(T));
}
template <class T>
enable_if_t<std::is_integral<T>::value, size_t> fnv_hash_append(size_t interim,
const T& x) {
auto first = reinterpret_cast<const uint8_t*>(&x);
return fnv_hash_append(interim, first, first + sizeof(T));
}
} // namespace detail
} // namespace caf
This diff is collapsed.
...@@ -105,6 +105,9 @@ public: ...@@ -105,6 +105,9 @@ public:
/// Returns the fragment component. /// Returns the fragment component.
string_view fragment() const noexcept; string_view fragment() const noexcept;
/// Returns a hash code over all components.
size_t hash_code() const noexcept;
// -- comparison ------------------------------------------------------------- // -- comparison -------------------------------------------------------------
int compare(const uri& other) const noexcept; int compare(const uri& other) const noexcept;
...@@ -135,3 +138,14 @@ std::string to_string(const uri& x); ...@@ -135,3 +138,14 @@ std::string to_string(const uri& x);
error parse(string_view str, uri& dest); error parse(string_view str, uri& dest);
} // namespace caf } // namespace caf
namespace std {
template <>
struct hash<caf::uri> {
size_t operator()(const caf::uri& x) const noexcept {
return x.hash_code();
}
};
} // namespace std
...@@ -71,4 +71,8 @@ std::string to_string(const atom_value& x) { ...@@ -71,4 +71,8 @@ std::string to_string(const atom_value& x) {
return std::string(str.begin(), str.begin() + len); return std::string(str.begin(), str.begin() + len);
} }
int compare(atom_value x, atom_value y) {
return memcmp(&x, &y, sizeof(atom_value));
}
} // namespace caf } // namespace caf
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/fnv_hash.hpp"
namespace caf {
namespace detail {
namespace {
template <size_t IntegerSize>
struct hash_conf_helper;
template <>
struct hash_conf_helper<4> {
static constexpr size_t basis = 2166136261u;
static constexpr size_t prime = 16777619u;
};
template <>
struct hash_conf_helper<8> {
static constexpr size_t basis = 14695981039346656037u;
static constexpr size_t prime = 1099511628211u;
};
struct hash_conf : hash_conf_helper<sizeof(size_t)> {};
} // namespace
size_t fnv_hash(const unsigned char* first, const unsigned char* last) {
return fnv_hash_append(hash_conf::basis, first, last);
}
size_t fnv_hash_append(size_t intermediate, const unsigned char* first,
const unsigned char* last) {
auto result = intermediate;
for (; first != last; ++first) {
result *= hash_conf::prime;
result ^= *first;
}
return result;
}
} // namespace detail
} // namespace caf
This diff is collapsed.
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "caf/uri.hpp" #include "caf/uri.hpp"
#include "caf/deserializer.hpp" #include "caf/deserializer.hpp"
#include "caf/detail/fnv_hash.hpp"
#include "caf/detail/parser/read_uri.hpp" #include "caf/detail/parser/read_uri.hpp"
#include "caf/detail/uri_impl.hpp" #include "caf/detail/uri_impl.hpp"
#include "caf/error.hpp" #include "caf/error.hpp"
...@@ -64,6 +65,10 @@ string_view uri::fragment() const noexcept { ...@@ -64,6 +65,10 @@ string_view uri::fragment() const noexcept {
return impl_->fragment; return impl_->fragment;
} }
size_t uri::hash_code() const noexcept {
return detail::fnv_hash(str());
}
// -- comparison --------------------------------------------------------------- // -- comparison ---------------------------------------------------------------
int uri::compare(const uri& other) const noexcept { int uri::compare(const uri& other) const noexcept {
......
...@@ -541,7 +541,7 @@ void basp_broker::set_context(connection_handle hdl) { ...@@ -541,7 +541,7 @@ void basp_broker::set_context(connection_handle hdl) {
invalid_actor_id}; invalid_actor_id};
i = ctx i = ctx
.emplace(hdl, basp::endpoint_context{basp::await_header, hdr, hdl, .emplace(hdl, basp::endpoint_context{basp::await_header, hdr, hdl,
none, 0, 0, none}) node_id{}, 0, 0, none})
.first; .first;
} }
this_context = &i->second; this_context = &i->second;
......
...@@ -18,10 +18,10 @@ ...@@ -18,10 +18,10 @@
#include "caf/io/network/ip_endpoint.hpp" #include "caf/io/network/ip_endpoint.hpp"
#include "caf/sec.hpp" #include "caf/detail/fnv_hash.hpp"
#include "caf/logger.hpp"
#include "caf/io/network/native_socket.hpp" #include "caf/io/network/native_socket.hpp"
#include "caf/logger.hpp"
#include "caf/sec.hpp"
#ifdef CAF_WINDOWS #ifdef CAF_WINDOWS
# include <winsock2.h> # include <winsock2.h>
...@@ -41,41 +41,8 @@ ...@@ -41,41 +41,8 @@
using sa_family_t = short; using sa_family_t = short;
#endif #endif
namespace { using caf::detail::fnv_hash;
using caf::detail::fnv_hash_append;
template <class SizeType = size_t>
struct hash_conf {
template <class T = SizeType>
static constexpr caf::detail::enable_if_t<(sizeof(T) == 4), size_t> basis() {
return 2166136261u;
}
template <class T = SizeType>
static constexpr caf::detail::enable_if_t<(sizeof(T) == 4), size_t> prime() {
return 16777619u;
}
template <class T = SizeType>
static constexpr caf::detail::enable_if_t<(sizeof(T) == 8), size_t> basis() {
return 14695981039346656037u;
}
template <class T = SizeType>
static constexpr caf::detail::enable_if_t<(sizeof(T) == 8), size_t> prime() {
return 1099511628211u;
}
};
constexpr uint8_t static_bytes[] = {
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF
};
constexpr size_t prehash(int i = 11) {
return (i > 0)
? (prehash(i - 1) * hash_conf<>::prime()) ^ static_bytes[i]
: (hash_conf<>::basis() * hash_conf<>::prime()) ^ static_bytes[i];
}
} // namespace <anonymous>
namespace caf { namespace caf {
namespace io { namespace io {
...@@ -145,32 +112,16 @@ size_t ep_hash::operator()(const sockaddr& sa) const noexcept { ...@@ -145,32 +112,16 @@ size_t ep_hash::operator()(const sockaddr& sa) const noexcept {
} }
size_t ep_hash::hash(const sockaddr_in* sa) const noexcept { size_t ep_hash::hash(const sockaddr_in* sa) const noexcept {
auto& addr = sa->sin_addr; auto result = fnv_hash(sa->sin_addr.s_addr);
size_t res = prehash(); result = fnv_hash_append(result, sa->sin_port);
// the first loop was replaces with `constexpr size_t prehash()` return result;
for (int i = 0; i < 4; ++i) {
res = res * hash_conf<>::prime();
res = res ^ ((addr.s_addr >> i) & 0xFF);
}
res = res * hash_conf<>::prime();
res = res ^ (sa->sin_port >> 1);
res = res * hash_conf<>::prime();
res = res ^ (sa->sin_port & 0xFF);
return res;
} }
size_t ep_hash::hash(const sockaddr_in6* sa) const noexcept { size_t ep_hash::hash(const sockaddr_in6* sa) const noexcept {
auto& addr = sa->sin6_addr; auto& addr = sa->sin6_addr.s6_addr;
size_t res = hash_conf<>::basis(); auto result = fnv_hash(addr, addr + 16);
for (int i = 0; i < 16; ++i) { result = fnv_hash_append(result, sa->sin6_port);
res = res * hash_conf<>::prime(); return result;
res = res ^ addr.s6_addr[i];
}
res = res * hash_conf<>::prime();
res = res ^ (sa->sin6_port >> 1);
res = res * hash_conf<>::prime();
res = res ^ (sa->sin6_port & 0xFF);
return res;
} }
bool operator==(const ip_endpoint& lhs, const ip_endpoint& rhs) { bool operator==(const ip_endpoint& lhs, const ip_endpoint& rhs) {
......
...@@ -384,8 +384,8 @@ void middleman::init(actor_system_config& cfg) { ...@@ -384,8 +384,8 @@ void middleman::init(actor_system_config& cfg) {
.add_message_type<connection_handle>("@connection_handle") .add_message_type<connection_handle>("@connection_handle")
.add_message_type<connection_passivated_msg>("@connection_passivated_msg") .add_message_type<connection_passivated_msg>("@connection_passivated_msg")
.add_message_type<acceptor_passivated_msg>("@acceptor_passivated_msg"); .add_message_type<acceptor_passivated_msg>("@acceptor_passivated_msg");
// compute and set ID for this network node // Compute and set ID for this network node.
node_id this_node{node_id::data::create_singleton()}; auto this_node = node_id::default_data::local(cfg);
system().node_.swap(this_node); system().node_.swap(this_node);
// give config access to slave mode implementation // give config access to slave mode implementation
cfg.slave_mode_fun = &middleman::exec_slave_mode; cfg.slave_mode_fun = &middleman::exec_slave_mode;
......
...@@ -62,7 +62,7 @@ node_id routing_table::lookup_direct(const connection_handle& hdl) const { ...@@ -62,7 +62,7 @@ node_id routing_table::lookup_direct(const connection_handle& hdl) const {
auto i = direct_by_hdl_.find(hdl); auto i = direct_by_hdl_.find(hdl);
if (i != direct_by_hdl_.end()) if (i != direct_by_hdl_.end())
return i->second; return i->second;
return none; return {};
} }
optional<connection_handle> optional<connection_handle>
...@@ -71,24 +71,24 @@ routing_table::lookup_direct(const node_id& nid) const { ...@@ -71,24 +71,24 @@ routing_table::lookup_direct(const node_id& nid) const {
auto i = direct_by_nid_.find(nid); auto i = direct_by_nid_.find(nid);
if (i != direct_by_nid_.end()) if (i != direct_by_nid_.end())
return i->second; return i->second;
return none; return {};
} }
node_id routing_table::lookup_indirect(const node_id& nid) const { node_id routing_table::lookup_indirect(const node_id& nid) const {
std::unique_lock<std::mutex> guard{mtx_}; std::unique_lock<std::mutex> guard{mtx_};
auto i = indirect_.find(nid); auto i = indirect_.find(nid);
if (i == indirect_.end()) if (i == indirect_.end())
return none; return {};
if (!i->second.empty()) if (!i->second.empty())
return *i->second.begin(); return *i->second.begin();
return none; return {};
} }
node_id routing_table::erase_direct(const connection_handle& hdl) { node_id routing_table::erase_direct(const connection_handle& hdl) {
std::unique_lock<std::mutex> guard{mtx_}; std::unique_lock<std::mutex> guard{mtx_};
auto i = direct_by_hdl_.find(hdl); auto i = direct_by_hdl_.find(hdl);
if (i == direct_by_hdl_.end()) if (i == direct_by_hdl_.end())
return none; return {};
direct_by_nid_.erase(i->second); direct_by_nid_.erase(i->second);
node_id result = std::move(i->second); node_id result = std::move(i->second);
direct_by_hdl_.erase(i->first); direct_by_hdl_.erase(i->first);
......
...@@ -129,12 +129,13 @@ public: ...@@ -129,12 +129,13 @@ public:
registry_ = &sys.registry(); registry_ = &sys.registry();
registry_->put((*self_)->id(), actor_cast<strong_actor_ptr>(*self_)); registry_->put((*self_)->id(), actor_cast<strong_actor_ptr>(*self_));
// first remote node is everything of this_node + 1, then +2, etc. // first remote node is everything of this_node + 1, then +2, etc.
auto pid = static_cast<node_id::default_data&>(*this_node_).process_id();
auto hid = static_cast<node_id::default_data&>(*this_node_).host_id();
for (uint32_t i = 0; i < num_remote_nodes; ++i) { for (uint32_t i = 0; i < num_remote_nodes; ++i) {
auto& n = nodes_[i]; auto& n = nodes_[i];
node_id::host_id_type tmp = this_node_.host_id(); for (auto& c : hid)
for (auto& c : tmp) ++c;
c = static_cast<uint8_t>(c + i + 1); n.id = make_node_id(++pid, hid);
n.id = node_id{this_node_.process_id() + i + 1, tmp};
n.connection = connection_handle::from_int(i + 1); n.connection = connection_handle::from_int(i + 1);
new (&n.dummy_actor) scoped_actor(sys); new (&n.dummy_actor) scoped_actor(sys);
// register all pseudo remote actors in the registry // register all pseudo remote actors in the registry
......
...@@ -82,10 +82,9 @@ struct fixture : test_coordinator_fixture<> { ...@@ -82,10 +82,9 @@ struct fixture : test_coordinator_fixture<> {
node_id last_hop; node_id last_hop;
actor testee; actor testee;
fixture() fixture() : proxies_backend(sys), proxies(sys, proxies_backend) {
: proxies_backend(sys), auto tmp = make_node_id(123, "0011223344556677889900112233445566778899");
proxies(sys, proxies_backend), last_hop = unbox(std::move(tmp));
last_hop(123, "0011223344556677889900112233445566778899") {
testee = sys.spawn<lazy_init>(testee_impl); testee = sys.spawn<lazy_init>(testee_impl);
sys.registry().put(testee.id(), testee); sys.registry().put(testee.id(), testee);
} }
......
...@@ -150,7 +150,10 @@ std::istream& operator>>(std::istream& in, node_id& x) { ...@@ -150,7 +150,10 @@ std::istream& operator>>(std::istream& in, node_id& x) {
string node_hex_id; string node_hex_id;
uint32_t pid; uint32_t pid;
if (in >> rd_line(node_hex_id, '#') >> pid) { if (in >> rd_line(node_hex_id, '#') >> pid) {
x = node_id{pid, node_hex_id}; if (auto nid = make_node_id(pid, node_hex_id))
x = std::move(*nid);
else
in.setstate(std::ios::failbit);
} }
return in; return in;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment