//////////////////////////////////////////////////////////////////////////////// /// DISCLAIMER /// /// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany /// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. /// /// Copyright holder is ArangoDB GmbH, Cologne, Germany /// /// @author Kaveh Vahedipour //////////////////////////////////////////////////////////////////////////////// #ifndef ARANGOD_CONSENSUS_AGENCY_COMMON_H #define ARANGOD_CONSENSUS_AGENCY_COMMON_H 1 #include "Basics/VelocyPackHelper.h" #include "Logger/Logger.h" #include #include #include #include namespace arangodb { namespace consensus { /// @brief Term type typedef uint64_t term_t; /// @brief Agent roles enum role_t {FOLLOWER, CANDIDATE, LEADER}; /// @brief Duration type typedef std::chrono::duration> duration_t; /// @brief Log query type using query_t = std::shared_ptr; /// @brief Log entry index type typedef uint64_t index_t; /// @brief Read request return type struct read_ret_t { bool accepted; ///< @brief Query accepted (i.e. we are leader) std::string redirect; ///< @brief If not accepted redirect id std::vector success; ///< @brief Query's precond OK query_t result; ///< @brief Query result read_ret_t(bool a, std::string const& id, std::vector const& suc = std::vector(), query_t res = nullptr) : accepted(a), redirect(id), success(suc), result(res) {} }; /// @brief Write request return type struct write_ret_t { bool accepted; ///< @brief Query accepted (i.e. we are leader) std::string redirect; ///< @brief If not accepted redirect id std::vector applied; std::vector indices; // Indices of log entries (if any) to wait for write_ret_t() : accepted(false), redirect("") {} write_ret_t(bool a, std::string const& id) : accepted(a), redirect(id) {} write_ret_t(bool a, std::string const& id, std::vector const& app, std::vector const& idx) : accepted(a), redirect(id), applied(app), indices(idx) {} }; /// @brief Buffer type using buffer_t = std::shared_ptr>; /// @brief State entry struct log_t { index_t index; ///< @brief Log index term_t term; ///< @brief Log term buffer_t entry; ///< @brief To log std::chrono::milliseconds timestamp; ///< @brief Timestamp log_t(index_t idx, term_t t, buffer_t const& e) : index(idx), term(t), entry(e), timestamp(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch())) {} friend std::ostream& operator<<(std::ostream& o, log_t const& l) { o << l.index << " " << l.term << " " << VPackSlice(l.entry->data()).toJson() << " " << l.timestamp.count(); return o; } }; /// @brief Private RPC return type struct priv_rpc_ret_t { bool success; term_t term; priv_rpc_ret_t(bool s, term_t t) : success(s), term(t) {} }; } } #endif