├── .gitmodules ├── .travis.yml ├── AUTHORS ├── INSTALL ├── LICENSE ├── Makefile.define ├── README ├── README.md ├── README.zh_CN.md ├── autoinstall.sh ├── build.sh ├── doc ├── images │ ├── delete_log_before_checkpoint.jpg │ ├── start_with_checkpoint.jpg │ └── transfer_checkpoint_automatic.jpg └── pdf │ └── code_reading_note.pdf ├── include ├── Makefile.define └── phxpaxos │ ├── breakpoint.h │ ├── def.h │ ├── log.h │ ├── network.h │ ├── node.h │ ├── options.h │ ├── sm.h │ └── storage.h ├── makefile.mk ├── plugin ├── Makefile.define ├── include │ ├── Makefile.define │ └── phxpaxos_plugin │ │ ├── logger_google.h │ │ └── monitor.h ├── logger_google │ ├── Makefile.define │ ├── logger_google.cpp │ ├── logger_google_impl.cpp │ └── logger_google_impl.h └── monitor │ ├── Makefile.define │ ├── monitor.cpp │ ├── monitor_bp.cpp │ └── monitor_bp.h ├── sample ├── phxecho │ ├── Makefile.define │ ├── README │ ├── echo_server.cpp │ ├── echo_server.h │ ├── echo_sm.cpp │ ├── echo_sm.h │ ├── main.cpp │ └── run_echo.sh ├── phxelection │ ├── Makefile.define │ ├── README │ ├── election.cpp │ ├── election.h │ ├── election_main.cpp │ └── run_election_sample.sh └── phxkv │ ├── Makefile.define │ ├── README │ ├── client_tools_sample.sh │ ├── def.h │ ├── kv.cpp │ ├── kv.h │ ├── kv_grpc_client.cpp │ ├── kv_grpc_client.h │ ├── kv_grpc_client_main.cpp │ ├── kv_grpc_server.cpp │ ├── kv_grpc_server.h │ ├── kv_grpc_server_main.cpp │ ├── kv_paxos.cpp │ ├── kv_paxos.h │ ├── kvsm.cpp │ ├── kvsm.h │ ├── log.cpp │ ├── log.h │ ├── phxkv.proto │ ├── prepare_dir.sh │ └── run_server_sample.sh ├── src ├── algorithm │ ├── Makefile.define │ ├── acceptor.cpp │ ├── acceptor.h │ ├── base.cpp │ ├── base.h │ ├── checkpoint_receiver.cpp │ ├── checkpoint_receiver.h │ ├── checkpoint_sender.cpp │ ├── checkpoint_sender.h │ ├── commitctx.cpp │ ├── commitctx.h │ ├── committer.cpp │ ├── committer.h │ ├── instance.cpp │ ├── instance.h │ ├── ioloop.cpp │ ├── ioloop.h │ ├── learner.cpp │ ├── learner.h │ ├── learner_sender.cpp │ ├── learner_sender.h │ ├── msg_counter.cpp │ ├── msg_counter.h │ ├── proposer.cpp │ └── proposer.h ├── benchmark │ ├── HOW_TO_BENCH │ ├── Makefile.define │ ├── bench_db.cpp │ ├── bench_main.cpp │ ├── bench_server.cpp │ ├── bench_server.h │ ├── bench_sm.cpp │ ├── bench_sm.h │ └── fsync_bench.cpp ├── checkpoint │ ├── Makefile.define │ ├── cleaner.cpp │ ├── cleaner.h │ ├── cp_mgr.cpp │ ├── cp_mgr.h │ ├── replayer.cpp │ └── replayer.h ├── comm │ ├── Makefile.define │ ├── breakpoint.cpp │ ├── comm_include.h │ ├── commdef.h │ ├── inside_options.cpp │ ├── inside_options.h │ ├── logger.cpp │ ├── logger.h │ ├── msg_transport.h │ ├── options.cpp │ └── paxos_msg.proto ├── communicate │ ├── Makefile.define │ ├── communicate.cpp │ ├── communicate.h │ ├── dfnetwork.cpp │ ├── dfnetwork.h │ ├── network.cpp │ ├── tcp │ │ ├── Makefile.define │ │ ├── event_base.cpp │ │ ├── event_base.h │ │ ├── event_loop.cpp │ │ ├── event_loop.h │ │ ├── message_event.cpp │ │ ├── message_event.h │ │ ├── notify.cpp │ │ ├── notify.h │ │ ├── tcp.cpp │ │ ├── tcp.h │ │ ├── tcp_acceptor.cpp │ │ ├── tcp_acceptor.h │ │ ├── tcp_client.cpp │ │ └── tcp_client.h │ ├── udp.cpp │ └── udp.h ├── config │ ├── Makefile.define │ ├── config.cpp │ ├── config.h │ ├── config_include.h │ ├── inside_sm.h │ ├── system_v_sm.cpp │ └── system_v_sm.h ├── logstorage │ ├── Makefile.define │ ├── db.cpp │ ├── db.h │ ├── log_store.cpp │ ├── log_store.h │ ├── paxos_log.cpp │ ├── paxos_log.h │ ├── system_variables_store.cpp │ └── system_variables_store.h ├── master │ ├── Makefile.define │ ├── master_mgr.cpp │ ├── master_mgr.h │ ├── master_sm.cpp │ ├── master_sm.h │ ├── master_sm.proto │ ├── master_variables_store.cpp │ └── master_variables_store.h ├── node │ ├── Makefile.define │ ├── group.cpp │ ├── group.h │ ├── node.cpp │ ├── pnode.cpp │ ├── pnode.h │ ├── propose_batch.cpp │ ├── propose_batch.h │ └── test_propose_batch.cpp ├── sm-base │ ├── Makefile.define │ ├── sm.cpp │ ├── sm_base.cpp │ └── sm_base.h ├── test │ ├── Makefile.define │ ├── test_main.cpp │ ├── test_server.cpp │ ├── test_server.h │ ├── test_sm.cpp │ └── test_sm.h ├── tools │ ├── Makefile.define │ ├── paxos_log_tools.cpp │ ├── system_variables_tools.cpp │ └── vfile_fetch.cpp ├── ut │ ├── Makefile.define │ ├── acceptor_ut.cpp │ ├── db_ut.cpp │ ├── make_class.cpp │ ├── make_class.h │ ├── mock_class.h │ ├── nodeid_ut.cpp │ ├── proposer_ut.cpp │ ├── timer_ut.cpp │ ├── ut_main.cpp │ └── wait_lock_ut.cpp └── utils │ ├── Makefile.define │ ├── bytes_buffer.cpp │ ├── bytes_buffer.h │ ├── concurrent.cpp │ ├── concurrent.h │ ├── crc32.cpp │ ├── crc32.h │ ├── notifier_pool.cpp │ ├── notifier_pool.h │ ├── serial_lock.cpp │ ├── serial_lock.h │ ├── socket.cpp │ ├── socket.h │ ├── timer.cpp │ ├── timer.h │ ├── util.cpp │ ├── util.h │ ├── utils_include.h │ ├── wait_lock.cpp │ └── wait_lock.h ├── src_list ├── third_party └── autoinstall.sh └── tools ├── build_comm.py ├── check_install.py └── create_makefile.py /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third_party/gtest"] 2 | path = third_party/gtest 3 | url = https://github.com/google/googletest 4 | branch = release-1.6.0 5 | [submodule "third_party/gflags"] 6 | path = third_party/gflags 7 | url = https://github.com/gflags/gflags 8 | [submodule "third_party/protobuf"] 9 | path = third_party/protobuf 10 | url = https://github.com/google/protobuf 11 | [submodule "third_party/glog"] 12 | path = third_party/glog 13 | url = https://github.com/google/glog 14 | [submodule "third_party/leveldb"] 15 | path = third_party/leveldb 16 | url = https://github.com/google/leveldb 17 | [submodule "third_party/gmock"] 18 | path = third_party/gmock 19 | url = https://github.com/google/googlemock 20 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Ubuntu 14.04 Trusty support 2 | sudo: required 3 | dist: trusty 4 | 5 | language: cpp 6 | compiler: g++ 7 | git: 8 | submodules: false 9 | script: 10 | - ./build.sh 11 | notifications: 12 | email: true 13 | branches: 14 | only: 15 | - /.*/ 16 | 17 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # Names should be added to this file like so: 2 | # Name or Organization 3 | 4 | Tencent Inc. 5 | 6 | Haochuan Cui 7 | Duokai Huang 8 | Junechao Chen 9 | Ming Chen 10 | -------------------------------------------------------------------------------- /Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=elibphxpaxos.a 21 | 22 | PHXPAXOS_OBJ= 23 | 24 | PHXPAXOS_LIB=src/node:node include:include 25 | 26 | PHXPAXOS_SYS_LIB= 27 | 28 | PHXPAXOS_INCS=$(SRC_BASE_PATH)/ 29 | 30 | PHXPAXOS_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This repository contains a C++ implementation of the Phxpaxos module. 2 | 3 | See INSTALL for (generic) installation instructions for C++: basically 4 | sh autoinstall.sh && make && make install 5 | -------------------------------------------------------------------------------- /autoinstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | base_dir=`pwd` 4 | make_file_tools=$base_dir/tools/create_makefile.py 5 | check_env_tools=$base_dir/tools/check_install.py 6 | src_dir=$base_dir/src_list 7 | 8 | function check_env(){ 9 | python $check_env_tools $base_dir 10 | if [ $? -gt 0 ]; 11 | then 12 | exit 1 13 | fi 14 | } 15 | 16 | function create_makefile(){ 17 | python $make_file_tools $base_dir $1 18 | } 19 | 20 | function scandir(){ 21 | if [ $1 ];then 22 | echo "[creating makefile] $1" 23 | create_makefile $1 24 | for file in `ls $1` 25 | do 26 | if ([ -d $1"/"$file ] && [ "$file" != "glog" ]) 27 | then 28 | scandir $1"/"$file 29 | fi 30 | done 31 | fi 32 | } 33 | 34 | function process(){ 35 | create_makefile $base_dir 36 | res=`cat $src_dir` 37 | echo $res 38 | for file in $res 39 | do 40 | if ([ -d $base_dir"/"$file ] && [ "$file" != "glog" ]) 41 | then 42 | scandir $base_dir"/"$file 43 | fi 44 | done 45 | } 46 | 47 | function check(){ 48 | make verify-install --file=makefile.mk 49 | if [ $? -eq 0 ]; then 50 | return 0 51 | else 52 | echo "install fail" 53 | return 1 54 | fi 55 | } 56 | 57 | function showusage(){ 58 | echo "Configuration:" 59 | echo " -h, --help display this help and exit" 60 | echo "Installation directories:" 61 | echo " --prefix=PREFIX install architecture-independent files in PREFIX" 62 | echo " [.]" 63 | exit 64 | } 65 | 66 | prefix= 67 | 68 | ARGS=`getopt -o h -l prefix:,help -- "$@"` 69 | eval set -- "${ARGS}" 70 | 71 | while true 72 | do 73 | case "$1" in 74 | --prefix) 75 | if [ $2 ]; then 76 | sed -i -r "s#PREFIX=.*#PREFIX=$2#" makefile.mk 77 | fi 78 | shift 79 | ;; 80 | -h|--help) 81 | showusage 82 | break 83 | ;; 84 | --) 85 | shift 86 | break 87 | ;; 88 | esac 89 | shift 90 | done 91 | 92 | check_env 93 | echo "check evn done" 94 | #check 95 | if [ $? -eq 0 ]; then 96 | process 97 | fi 98 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | 2 | set -e # exit immediately on error 3 | set -x # display all commands 4 | 5 | git submodule update --init --recursive 6 | 7 | (cd third_party && bash ./autoinstall.sh) 8 | 9 | ./autoinstall.sh 10 | 11 | make 12 | 13 | -------------------------------------------------------------------------------- /doc/images/delete_log_before_checkpoint.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tencent/phxpaxos/c6c8806c3497cabe6e74dc09aa4487b6fd2057f7/doc/images/delete_log_before_checkpoint.jpg -------------------------------------------------------------------------------- /doc/images/start_with_checkpoint.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tencent/phxpaxos/c6c8806c3497cabe6e74dc09aa4487b6fd2057f7/doc/images/start_with_checkpoint.jpg -------------------------------------------------------------------------------- /doc/images/transfer_checkpoint_automatic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tencent/phxpaxos/c6c8806c3497cabe6e74dc09aa4487b6fd2057f7/doc/images/transfer_checkpoint_automatic.jpg -------------------------------------------------------------------------------- /doc/pdf/code_reading_note.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tencent/phxpaxos/c6c8806c3497cabe6e74dc09aa4487b6fd2057f7/doc/pdf/code_reading_note.pdf -------------------------------------------------------------------------------- /include/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libinclude.a 21 | 22 | INCLUDE_OBJ= 23 | 24 | INCLUDE_LIB= 25 | 26 | INCLUDE_SYS_LIB= 27 | 28 | INCLUDE_INCS=$(SRC_BASE_PATH)/include 29 | 30 | INCLUDE_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /include/phxpaxos/def.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | namespace phxpaxos 25 | { 26 | 27 | #define SYSTEM_V_SMID 100000000 28 | #define MASTER_V_SMID 100000001 29 | #define BATCH_PROPOSE_SMID 100000002 30 | 31 | enum PaxosTryCommitRet 32 | { 33 | PaxosTryCommitRet_OK = 0, 34 | PaxosTryCommitRet_Reject = -2, 35 | PaxosTryCommitRet_Conflict = 14, 36 | PaxosTryCommitRet_ExecuteFail = 15, 37 | PaxosTryCommitRet_Follower_Cannot_Commit = 16, 38 | PaxosTryCommitRet_Im_Not_In_Membership = 17, 39 | PaxosTryCommitRet_Value_Size_TooLarge = 18, 40 | PaxosTryCommitRet_Timeout = 404, 41 | PaxosTryCommitRet_TooManyThreadWaiting_Reject = 405, 42 | }; 43 | 44 | enum PaxosNodeFunctionRet 45 | { 46 | Paxos_SystemError = -1, 47 | Paxos_GroupIdxWrong = -5, 48 | Paxos_MembershipOp_GidNotSame = -501, 49 | Paxos_MembershipOp_VersionConflit = -502, 50 | Paxos_MembershipOp_NoGid = 1001, 51 | Paxos_MembershipOp_Add_NodeExist = 1002, 52 | Paxos_MembershipOp_Remove_NodeNotExist = 1003, 53 | Paxos_MembershipOp_Change_NoChange = 1004, 54 | Paxos_GetInstanceValue_Value_NotExist = 1005, 55 | Paxos_GetInstanceValue_Value_Not_Chosen_Yet = 1006, 56 | }; 57 | 58 | } 59 | 60 | -------------------------------------------------------------------------------- /include/phxpaxos/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | 27 | namespace phxpaxos 28 | { 29 | 30 | enum class LogLevel 31 | { 32 | LogLevel_None = 0, 33 | LogLevel_Error = 1, 34 | LogLevel_Warning = 2, 35 | LogLevel_Info = 3, 36 | LogLevel_Verbose = 4, 37 | }; 38 | 39 | typedef std::function< void(const int, const char *, va_list) > LogFunc; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /include/phxpaxos/network.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | //You can use your own network to make paxos communicate. :) 32 | 33 | class Node; 34 | 35 | class NetWork 36 | { 37 | public: 38 | NetWork(); 39 | virtual ~NetWork() {} 40 | 41 | //Network must not send/recieve any message before paxoslib called this funtion. 42 | virtual void RunNetWork() = 0; 43 | 44 | //If paxoslib call this function, network need to stop receive any message. 45 | virtual void StopNetWork() = 0; 46 | 47 | virtual int SendMessageTCP(const int iGroupIdx, const std::string & sIp, const int iPort, const std::string & sMessage) = 0; 48 | 49 | virtual int SendMessageUDP(const int iGroupIdx, const std::string & sIp, const int iPort, const std::string & sMessage) = 0; 50 | 51 | //When receive a message, call this funtion. 52 | //This funtion is async, just enqueue an return. 53 | int OnReceiveMessage(const char * pcMessage, const int iMessageLen); 54 | 55 | private: 56 | friend class Node; 57 | Node * m_poNode; 58 | }; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /include/phxpaxos/storage.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | //Paxoslib need to storage many datas, if you want to storage datas yourself, 32 | //you must implememt all function in class LogStorage, and make sure that observe the writeoptions. 33 | 34 | class WriteOptions 35 | { 36 | public: 37 | WriteOptions() : bSync(true) { } 38 | bool bSync; 39 | }; 40 | 41 | class LogStorage 42 | { 43 | public: 44 | virtual ~LogStorage() {} 45 | 46 | virtual const std::string GetLogStorageDirPath(const int iGroupIdx) = 0; 47 | 48 | virtual int Get(const int iGroupIdx, const uint64_t llInstanceID, std::string & sValue) = 0; 49 | 50 | virtual int Put(const WriteOptions & oWriteOptions, const int iGroupIdx, const uint64_t llInstanceID, const std::string & sValue) = 0; 51 | 52 | virtual int Del(const WriteOptions & oWriteOptions, int iGroupIdx, const uint64_t llInstanceID) = 0; 53 | 54 | virtual int GetMaxInstanceID(const int iGroupIdx, uint64_t & llInstanceID) = 0; 55 | 56 | virtual int SetMinChosenInstanceID(const WriteOptions & oWriteOptions, const int iGroupIdx, const uint64_t llMinInstanceID) = 0; 57 | 58 | virtual int GetMinChosenInstanceID(const int iGroupIdx, uint64_t & llMinInstanceID) = 0; 59 | 60 | virtual int ClearAllLog(const int iGroupIdx) = 0; 61 | 62 | virtual int SetSystemVariables(const WriteOptions & oWriteOptions, const int iGroupIdx, const std::string & sBuffer) = 0; 63 | 64 | virtual int GetSystemVariables(const int iGroupIdx, std::string & sBuffer) = 0; 65 | 66 | virtual int SetMasterVariables(const WriteOptions & oWriteOptions, const int iGroupIdx, const std::string & sBuffer) = 0; 67 | 68 | virtual int GetMasterVariables(const int iGroupIdx, std::string & sBuffer) = 0; 69 | }; 70 | 71 | } 72 | -------------------------------------------------------------------------------- /plugin/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=elibphxpaxos_plugin.a 21 | 22 | PHXPAXOS_PLUGIN_OBJ= 23 | 24 | PHXPAXOS_PLUGIN_LIB=plugin/logger_google:logger_google plugin/monitor:monitor 25 | 26 | PHXPAXOS_PLUGIN_SYS_LIB= 27 | 28 | PHXPAXOS_PLUGIN_INCS=$(SRC_BASE_PATH)/plugin 29 | 30 | PHXPAXOS_PLUGIN_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /plugin/include/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libinclude.a 21 | 22 | INCLUDE_OBJ= 23 | 24 | INCLUDE_LIB= 25 | 26 | INCLUDE_SYS_LIB= 27 | 28 | INCLUDE_INCS=$(SRC_BASE_PATH)/plugin/include 29 | 30 | INCLUDE_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /plugin/include/phxpaxos_plugin/logger_google.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include "phxpaxos/log.h" 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | class LoggerGoogle 32 | { 33 | public: 34 | //LogLevel_None = 0, 35 | //LogLevel_Error = 1, 36 | //LogLevel_Warning = 2, 37 | //LogLevel_Info = 3, 38 | //LogLevel_Verbose = 4, 39 | static int GetLogger(const std::string & sModuleName, const std::string & sLogPath, const int iLogLevel, LogFunc & pLogFunc); 40 | 41 | static void Log(const int iLogLevel, const char * pcFormat, va_list args); 42 | 43 | static void LogError(const char * pcFormat, ...); 44 | 45 | static void LogWarning(const char * pcFormat, ...); 46 | 47 | static void LogInfo(const char * pcFormat, ...); 48 | 49 | static void LogVerbose(const char * pcFormat, ...); 50 | }; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /plugin/include/phxpaxos_plugin/monitor.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/breakpoint.h" 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | class MonitorConfig 30 | { 31 | public: 32 | MonitorConfig(); 33 | 34 | int iOssAttrID; 35 | int iUseTimeOssAttrID; 36 | }; 37 | 38 | typedef void (*IDKeyOssFunc)(const uint32_t iOssAttrID, const uint32_t iKey, const uint32_t iVal); 39 | 40 | class Monitor 41 | { 42 | public: 43 | static Breakpoint * GetBreakpoint(const MonitorConfig & oMonitorConfig, IDKeyOssFunc pIDKeyOssFunc); 44 | }; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /plugin/logger_google/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=liblogger_google.a 21 | 22 | LOGGER_GOOGLE_OBJ=logger_google.o 23 | 24 | LOGGER_GOOGLE_LIB=logger_google plugin/include:include 25 | 26 | LOGGER_GOOGLE_SYS_LIB=$(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(PROTOBUF_LIB_PATH)/libprotobuf.a $(GLOG_LIB_PATH)/libglog.a $(GFLAGS_LIB_PATH)/libgflags.a-lpthread 27 | 28 | LOGGER_GOOGLE_INCS=$(SRC_BASE_PATH)/plugin/logger_google $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 29 | 30 | LOGGER_GOOGLE_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /plugin/logger_google/logger_google_impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Tencent. See the AUTHORS file for names 3 | of contributors. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the 17 | Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 18 | Boston, MA 02110-1301, USA. 19 | 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/log.h" 25 | #include "phxpaxos_plugin/logger_google.h" 26 | #include 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | class LoggerGoogleImpl : public LoggerGoogle 32 | { 33 | public: 34 | LoggerGoogleImpl(const std::string & sModuleName, const std::string & sLogPath, const int iLogLevel); 35 | ~LoggerGoogleImpl(); 36 | 37 | void LogError(const char * pcFormat, ...); 38 | 39 | void LogWarning(const char * pcFormat, ...); 40 | 41 | void LogInfo(const char * pcFormat, ...); 42 | 43 | void LogVerbose(const char * pcFormat, ...); 44 | }; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /plugin/monitor/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libmonitor.a 21 | 22 | MONITOR_OBJ=monitor.o monitor_bp.o 23 | 24 | MONITOR_LIB=monitor plugin/include:include 25 | 26 | MONITOR_SYS_LIB=$(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(PROTOBUF_LIB_PATH)/libprotobuf.a -lpthread 27 | 28 | MONITOR_INCS=$(SRC_BASE_PATH)/plugin/monitor $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 29 | 30 | MONITOR_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /plugin/monitor/monitor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "phxpaxos_plugin/monitor.h" 23 | #include "monitor_bp.h" 24 | 25 | namespace phxpaxos 26 | { 27 | 28 | MonitorConfig :: MonitorConfig() 29 | : iOssAttrID(0), iUseTimeOssAttrID(0) 30 | { 31 | } 32 | 33 | phxpaxos::Breakpoint * Monitor :: GetBreakpoint(const MonitorConfig & oMonitorConfig, IDKeyOssFunc pIDKeyOssFunc) 34 | { 35 | if (pIDKeyOssFunc == nullptr) 36 | { 37 | return nullptr; 38 | } 39 | 40 | MonitorBP * poMonitorBP = new MonitorBP(oMonitorConfig, pIDKeyOssFunc); 41 | return poMonitorBP; 42 | } 43 | 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /sample/phxecho/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=phxecho 21 | 22 | PHXECHO_OBJ=echo_sm.o echo_server.o main.o 23 | 24 | PHXECHO_LIB= 25 | 26 | PHXECHO_SYS_LIB=$(PHXPAXOS_LIB_PATH)/libphxpaxos_plugin.a $(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(PROTOBUF_LIB_PATH)/libprotobuf.a $(GLOG_LIB_PATH)/libglog.a $(GFLAGS_LIB_PATH)/libgflags.a -lpthread 27 | 28 | PHXECHO_INCS=$(SRC_BASE_PATH)/sample/phxecho $(PHXPAXOS_PLUGIN_PATH) $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 29 | 30 | PHXECHO_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /sample/phxecho/README: -------------------------------------------------------------------------------- 1 | This is not a real server. just to tell you how to use phxpaxos. 2 | 3 | The echo command will replicate to other echo_server by phxpaxos. 4 | 5 | The suggest start shell write on echo.sh. you can run three echo_server to test phxpaxos. 6 | 7 | First, you need to create a new directory names "log" as the subdirectory of the directory where your shell running. 8 | 9 | Echo_server will create a workspace dir on ./logpath_ip_port, data generate by phxpaxos will write on this path. 10 | 11 | Have fun :). 12 | -------------------------------------------------------------------------------- /sample/phxecho/echo_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/node.h" 25 | #include "echo_sm.h" 26 | #include 27 | #include 28 | #include "phxpaxos_plugin/logger_google.h" 29 | 30 | namespace phxecho 31 | { 32 | 33 | class PhxEchoServer 34 | { 35 | public: 36 | PhxEchoServer(const phxpaxos::NodeInfo & oMyNode, const phxpaxos::NodeInfoList & vecNodeList); 37 | ~PhxEchoServer(); 38 | 39 | int RunPaxos(); 40 | 41 | int Echo(const std::string & sEchoReqValue, std::string & sEchoRespValue); 42 | 43 | private: 44 | int MakeLogStoragePath(std::string & sLogStoragePath); 45 | 46 | private: 47 | phxpaxos::NodeInfo m_oMyNode; 48 | phxpaxos::NodeInfoList m_vecNodeList; 49 | 50 | phxpaxos::Node * m_poPaxosNode; 51 | PhxEchoSM m_oEchoSM; 52 | }; 53 | 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /sample/phxecho/echo_sm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "echo_sm.h" 23 | 24 | using namespace phxpaxos; 25 | 26 | namespace phxecho 27 | { 28 | 29 | PhxEchoSM :: PhxEchoSM() 30 | { 31 | } 32 | 33 | bool PhxEchoSM :: Execute(const int iGroupIdx, const uint64_t llInstanceID, 34 | const std::string & sPaxosValue, SMCtx * poSMCtx) 35 | { 36 | printf("[SM Execute] ok, smid %d instanceid %lu value %s\n", 37 | SMID(), llInstanceID, sPaxosValue.c_str()); 38 | 39 | //only commiter node have SMCtx. 40 | if (poSMCtx != nullptr && poSMCtx->m_pCtx != nullptr) 41 | { 42 | PhxEchoSMCtx * poPhxEchoSMCtx = (PhxEchoSMCtx *)poSMCtx->m_pCtx; 43 | poPhxEchoSMCtx->iExecuteRet = 0; 44 | poPhxEchoSMCtx->sEchoRespValue = sPaxosValue; 45 | } 46 | 47 | return true; 48 | } 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /sample/phxecho/echo_sm.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/sm.h" 25 | #include "phxpaxos/options.h" 26 | #include 27 | #include 28 | 29 | namespace phxecho 30 | { 31 | 32 | class PhxEchoSMCtx 33 | { 34 | public: 35 | int iExecuteRet; 36 | std::string sEchoRespValue; 37 | 38 | PhxEchoSMCtx() 39 | { 40 | iExecuteRet = -1; 41 | } 42 | }; 43 | 44 | class PhxEchoSM : public phxpaxos::StateMachine 45 | { 46 | public: 47 | PhxEchoSM(); 48 | 49 | bool Execute(const int iGroupIdx, const uint64_t llInstanceID, 50 | const std::string & sPaxosValue, phxpaxos::SMCtx * poSMCtx); 51 | 52 | const int SMID() const { return 1; } 53 | }; 54 | 55 | } 56 | -------------------------------------------------------------------------------- /sample/phxecho/run_echo.sh: -------------------------------------------------------------------------------- 1 | #sample 2 | #./phxecho 127.0.0.1:11111 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 3 | #./phxecho 127.0.0.1:11112 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 4 | #./phxecho 127.0.0.1:11113 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 5 | -------------------------------------------------------------------------------- /sample/phxelection/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=phxelection 21 | 22 | PHXELECTION_OBJ=election.o election_main.o 23 | 24 | PHXELECTION_LIB= 25 | 26 | PHXELECTION_SYS_LIB=$(PHXPAXOS_LIB_PATH)/libphxpaxos_plugin.a $(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(PROTOBUF_LIB_PATH)/libprotobuf.a $(GLOG_LIB_PATH)/libglog.a $(GFLAGS_LIB_PATH)/libgflags.a -lpthread 27 | 28 | PHXELECTION_INCS=$(SRC_BASE_PATH)/sample/phxelection $(PHXPAXOS_PLUGIN_PATH) $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 29 | 30 | PHXELECTION_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /sample/phxelection/README: -------------------------------------------------------------------------------- 1 | This is very simple sample to use phxpaxos to election. 2 | Check the code in election.cpp, that is really simple. 3 | -------------------------------------------------------------------------------- /sample/phxelection/election.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/node.h" 25 | #include 26 | #include 27 | #include "phxpaxos_plugin/logger_google.h" 28 | #include "phxpaxos/options.h" 29 | 30 | namespace phxelection 31 | { 32 | 33 | class PhxElection 34 | { 35 | public: 36 | PhxElection(const phxpaxos::NodeInfo & oMyNode, const phxpaxos::NodeInfoList & vecNodeList); 37 | ~PhxElection(); 38 | 39 | int RunPaxos(); 40 | 41 | const phxpaxos::NodeInfo GetMaster(); 42 | 43 | const phxpaxos::NodeInfo GetMasterWithVersion(uint64_t & llVersion); 44 | 45 | const bool IsIMMaster(); 46 | 47 | static void OnMasterChange(const int iGroupIdx, const phxpaxos::NodeInfo & oNewMaster, const uint64_t llVersion); 48 | 49 | private: 50 | int MakeLogStoragePath(std::string & sLogStoragePath); 51 | 52 | private: 53 | phxpaxos::NodeInfo m_oMyNode; 54 | phxpaxos::NodeInfoList m_vecNodeList; 55 | 56 | phxpaxos::Node * m_poPaxosNode; 57 | }; 58 | 59 | } 60 | 61 | 62 | -------------------------------------------------------------------------------- /sample/phxelection/run_election_sample.sh: -------------------------------------------------------------------------------- 1 | #sample 2 | #./phxelection 127.0.0.1:11111 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 3 | #./phxelection 127.0.0.1:11112 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 4 | #./phxelection 127.0.0.1:11113 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 5 | -------------------------------------------------------------------------------- /sample/phxkv/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=elibphxkv_client.a phxkv_client_tools phxkv_grpcserver 21 | 22 | PHXKV_CLIENT_OBJ=phxkv.pb.o phxkv.grpc.pb.o kv_grpc_client.o kv_grpc_client_main.o 23 | 24 | PHXKV_CLIENT_LIB=phxkv_client 25 | 26 | PHXKV_CLIENT_SYS_LIB=$(GRPC_LIBE_PATH)/libgrpc++_unsecure.a $(GRPC_LIBE_PATH)/libgrpc.a -lssl -lcrypto -ldl -lz -lrt $(PROTOBUF_LIB_PATH)/libprotobuf.a $(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(PROTOBUF_LIB_PATH)/libprotobuf.a -lpthread 27 | 28 | PHXKV_CLIENT_INCS=$(SRC_BASE_PATH)/sample/phxkv $(GRPC_INCLUDE_PATH) $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 29 | 30 | PHXKV_CLIENT_EXTRA_CPPFLAGS=-Wall 31 | 32 | PHXKV_CLIENT_TOOLS_OBJ=phxkv.pb.o phxkv.grpc.pb.o kv_grpc_client.o kv_grpc_client_main.o 33 | 34 | PHXKV_CLIENT_TOOLS_LIB=sample/phxkv:phxkv_client 35 | 36 | PHXKV_CLIENT_TOOLS_SYS_LIB= 37 | 38 | PHXKV_CLIENT_TOOLS_INCS=$(SRC_BASE_PATH)/sample/phxkv 39 | 40 | PHXKV_CLIENT_TOOLS_EXTRA_CPPFLAGS=-Wall 41 | 42 | PHXKV_GRPCSERVER_OBJ=phxkv.pb.o phxkv.grpc.pb.o kv.o kvsm.o kv_paxos.o log.o kv_grpc_server.o kv_grpc_server_main.o 43 | 44 | PHXKV_GRPCSERVER_LIB= 45 | 46 | PHXKV_GRPCSERVER_SYS_LIB=$(PHXPAXOS_LIB_PATH)/libphxpaxos_plugin.a $(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(GRPC_LIBE_PATH)/libgrpc++_unsecure.a $(GRPC_LIBE_PATH)/libgrpc.a $(PROTOBUF_LIB_PATH)/libprotobuf.a $(GLOG_LIB_PATH)/libglog.a $(GFLAGS_LIB_PATH)/libgflags.a -lssl -lcrypto -ldl -lz -lrt -lpthread 47 | 48 | PHXKV_GRPCSERVER_INCS=$(SRC_BASE_PATH)/sample/phxkv $(PHXPAXOS_PLUGIN_PATH) $(GRPC_INCLUDE_PATH) $(GRPC_INCLUDE_PATH) $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 49 | 50 | PHXKV_GRPCSERVER_EXTRA_CPPFLAGS=-Wall 51 | 52 | -------------------------------------------------------------------------------- /sample/phxkv/README: -------------------------------------------------------------------------------- 1 | This is a sample that make leveldb base on paxos to build a strong consistent distributed kv. 2 | Also, this sample show you how to unite grpc(maybe other rpc) and phxpaxos. 3 | 4 | #run server 5 | First check prepare_dir.sh to prepare all dir for server. 6 | Then check run_server_sample.sh to get command to run server. 7 | 8 | #use client tools 9 | Check client_tools_sample.sh. 10 | -------------------------------------------------------------------------------- /sample/phxkv/client_tools_sample.sh: -------------------------------------------------------------------------------- 1 | #./phxkv_client_tools 127.0.0.1:21112 put key_hello value_paxos 0 2 | #./phxkv_client_tools 127.0.0.1:21112 getlocal key_hello 3 | #./phxkv_client_tools 127.0.0.1:21112 getglobal key_hello 4 | #./phxkv_client_tools 127.0.0.1:21112 delete key_hello 0 5 | 6 | #./phxkv_client_tools 127.0.0.1:21111 put key_hello value_paxos 0 7 | #./phxkv_client_tools 127.0.0.1:21111 getlocal key_hello 8 | #./phxkv_client_tools 127.0.0.1:21111 getglobal key_hello 9 | #./phxkv_client_tools 127.0.0.1:21111 delete key_hello 0 10 | 11 | #./phxkv_client_tools 127.0.0.1:21113 put key_hello value_paxos 0 12 | #./phxkv_client_tools 127.0.0.1:21113 getlocal key_hello 13 | #./phxkv_client_tools 127.0.0.1:21113 getglobal key_hello 14 | #./phxkv_client_tools 127.0.0.1:21113 delete key_hello 0 15 | -------------------------------------------------------------------------------- /sample/phxkv/def.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | namespace phxkv 25 | { 26 | 27 | const uint64_t NullVersion = std::numeric_limits::min(); 28 | 29 | enum class PhxKVStatus 30 | { 31 | SUCC = 0, 32 | FAIL = -1, 33 | KEY_NOTEXIST = 1, 34 | VERSION_CONFLICT = -11, 35 | VERSION_NOTEXIST = -12, 36 | MASTER_REDIRECT = 10, 37 | NO_MASTER = 101, 38 | }; 39 | 40 | 41 | enum KVOperatorType 42 | { 43 | KVOperatorType_READ = 1, 44 | KVOperatorType_WRITE = 2, 45 | KVOperatorType_DELETE = 3, 46 | }; 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /sample/phxkv/kv.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "leveldb/db.h" 26 | #include 27 | 28 | namespace phxkv 29 | { 30 | 31 | enum KVClientRet 32 | { 33 | KVCLIENT_OK = 0, 34 | KVCLIENT_SYS_FAIL = -1, 35 | KVCLIENT_KEY_NOTEXIST = 1, 36 | KVCLIENT_KEY_VERSION_CONFLICT = -11, 37 | }; 38 | 39 | #define KV_CHECKPOINT_KEY ((uint64_t)-1) 40 | 41 | class KVClient 42 | { 43 | public: 44 | KVClient(); 45 | ~KVClient(); 46 | 47 | bool Init(const std::string & sDBPath); 48 | 49 | static KVClient * Instance(); 50 | 51 | KVClientRet Get(const std::string & sKey, std::string & sValue, uint64_t & llVersion); 52 | 53 | KVClientRet Set(const std::string & sKey, const std::string & sValue, const uint64_t llVersion); 54 | 55 | KVClientRet Del(const std::string & sKey, const uint64_t llVersion); 56 | 57 | KVClientRet GetCheckpointInstanceID(uint64_t & llCheckpointInstanceID); 58 | 59 | KVClientRet SetCheckpointInstanceID(const uint64_t llCheckpointInstanceID); 60 | 61 | private: 62 | leveldb::DB * m_poLevelDB; 63 | bool m_bHasInit; 64 | std::mutex m_oMutex; 65 | }; 66 | 67 | } 68 | -------------------------------------------------------------------------------- /sample/phxkv/kv_grpc_client.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include "phxkv.grpc.pb.h" 31 | 32 | #include "def.h" 33 | 34 | namespace phxkv 35 | { 36 | 37 | class PhxKVClient 38 | { 39 | public: 40 | PhxKVClient(std::shared_ptr channel); 41 | 42 | void NewChannel(const uint64_t llNodeID); 43 | 44 | int Put( 45 | const std::string & sKey, 46 | const std::string & sValue, 47 | const uint64_t llVersion, 48 | const int iDeep = 0); 49 | 50 | int GetLocal( 51 | const std::string & sKey, 52 | std::string & sValue, 53 | uint64_t & llVersion); 54 | 55 | int GetLocal( 56 | const std::string & sKey, 57 | const uint64_t minVersion, 58 | std::string & sValue, 59 | uint64_t & llVersion); 60 | 61 | int Delete( 62 | const std::string & sKey, 63 | const uint64_t llVersion, 64 | const int iDeep = 0); 65 | 66 | int GetGlobal( 67 | const std::string & sKey, 68 | std::string & sValue, 69 | uint64_t & llVersion, 70 | const int iDeep = 0); 71 | 72 | private: 73 | std::unique_ptr stub_; 74 | }; 75 | 76 | } 77 | -------------------------------------------------------------------------------- /sample/phxkv/kv_grpc_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "phxkv.grpc.pb.h" 26 | #include "kv_paxos.h" 27 | 28 | namespace phxkv 29 | { 30 | 31 | class PhxKVServiceImpl final : public PhxKVServer::Service 32 | { 33 | public: 34 | PhxKVServiceImpl(const phxpaxos::NodeInfo & oMyNode, const phxpaxos::NodeInfoList & vecNodeList, 35 | const std::string & sKVDBPath, const std::string & sPaxosLogPath); 36 | 37 | int Init(); 38 | 39 | grpc::Status Put(grpc::ServerContext* context, const KVOperator * request, KVResponse * reply) override; 40 | 41 | grpc::Status GetLocal(grpc::ServerContext* context, const KVOperator * request, KVResponse * reply) override; 42 | 43 | grpc::Status GetGlobal(grpc::ServerContext* context, const KVOperator * request, KVResponse * reply) override; 44 | 45 | grpc::Status Delete(grpc::ServerContext* context, const KVOperator * request, KVResponse * reply) override; 46 | 47 | private: 48 | PhxKV m_oPhxKV; 49 | }; 50 | 51 | } 52 | -------------------------------------------------------------------------------- /sample/phxkv/kv_paxos.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/node.h" 25 | #include "kvsm.h" 26 | #include 27 | #include 28 | #include "phxpaxos_plugin/logger_google.h" 29 | #include "log.h" 30 | #include "def.h" 31 | 32 | namespace phxkv 33 | { 34 | 35 | 36 | class PhxKV 37 | { 38 | public: 39 | PhxKV(const phxpaxos::NodeInfo & oMyNode, const phxpaxos::NodeInfoList & vecNodeList, 40 | const std::string & sKVDBPath, const std::string & sPaxosLogPath); 41 | ~PhxKV(); 42 | 43 | int RunPaxos(); 44 | 45 | const phxpaxos::NodeInfo GetMaster(const std::string & sKey); 46 | 47 | const bool IsIMMaster(const std::string & sKey); 48 | 49 | PhxKVStatus Put( 50 | const std::string & sKey, 51 | const std::string & sValue, 52 | const uint64_t llVersion = NullVersion); 53 | 54 | PhxKVStatus GetLocal( 55 | const std::string & sKey, 56 | std::string & sValue, 57 | uint64_t & llVersion); 58 | 59 | PhxKVStatus Delete( 60 | const std::string & sKey, 61 | const uint64_t llVersion = NullVersion); 62 | 63 | private: 64 | int GetGroupIdx(const std::string & sKey); 65 | 66 | int KVPropose(const std::string & sKey, const std::string & sPaxosValue, PhxKVSMCtx & oPhxKVSMCtx); 67 | 68 | private: 69 | phxpaxos::NodeInfo m_oMyNode; 70 | phxpaxos::NodeInfoList m_vecNodeList; 71 | std::string m_sKVDBPath; 72 | std::string m_sPaxosLogPath; 73 | 74 | int m_iGroupCount; 75 | phxpaxos::Node * m_poPaxosNode; 76 | PhxKVSM m_oPhxKVSM; 77 | }; 78 | 79 | } 80 | 81 | 82 | -------------------------------------------------------------------------------- /sample/phxkv/log.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "log.h" 23 | 24 | using namespace phxpaxos; 25 | 26 | namespace phxkv 27 | { 28 | 29 | LoggerGuard :: LoggerGuard() 30 | : m_pLogFunc(nullptr) 31 | { 32 | } 33 | 34 | LoggerGuard :: ~LoggerGuard() 35 | { 36 | } 37 | 38 | LoggerGuard * LoggerGuard :: Instance() 39 | { 40 | static LoggerGuard oLoggerGuard; 41 | return &oLoggerGuard; 42 | } 43 | 44 | int LoggerGuard :: Init(const std::string & sModuleName, const std::string & sLogPath, const int iLogLevel) 45 | { 46 | int ret = LoggerGoogle :: GetLogger(sModuleName, sLogPath, iLogLevel, m_pLogFunc); 47 | if (ret != 0) 48 | { 49 | printf("get logger_google fail, ret %d\n", ret); 50 | return ret; 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | phxpaxos::LogFunc LoggerGuard :: GetLogFunc() 57 | { 58 | return m_pLogFunc; 59 | } 60 | 61 | 62 | } 63 | 64 | -------------------------------------------------------------------------------- /sample/phxkv/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos_plugin/logger_google.h" 25 | #include 26 | #include 27 | 28 | namespace phxkv 29 | { 30 | 31 | #define LOGGER (LoggerGuard::Instance()) 32 | #define LOG_ERROR(format, args...)\ 33 | if (LOGGER->GetLogFunc() != nullptr)phxpaxos::LoggerGoogle::LogError(format, ## args); 34 | #define LOG_WARNING(format, args...)\ 35 | if (LOGGER->GetLogFunc() != nullptr)phxpaxos::LoggerGoogle::LogWarning(format, ## args); 36 | #define LOG_INFO(format, args...)\ 37 | if (LOGGER->GetLogFunc() != nullptr)phxpaxos::LoggerGoogle::LogInfo(format, ## args); 38 | #define LOG_VERBOSE(format, args...)\ 39 | if (LOGGER->GetLogFunc() != nullptr)phxpaxos::LoggerGoogle::LogVerbose(format, ## args); 40 | 41 | #define NLDebug(format, args...) LOG_VERBOSE("DEBUG: %s " format, __func__, ## args); 42 | #define NLErr(format, args...) LOG_ERROR("ERR: %s " format, __func__, ## args); 43 | 44 | #define PLErr(format, args...) LOG_ERROR("ERR: %s::%s " format, typeid(this).name(), __func__, ## args); 45 | #define PLImp(format, args...) LOG_INFO("Showy: %s::%s " format, typeid(this).name(), __func__, ## args); 46 | #define PLHead(format, args...) LOG_WARNING("Imp: %s::%s " format, typeid(this).name(), __func__, ## args); 47 | #define PLDebug(format, args...) LOG_VERBOSE("DEBUG: %s::%s " format, typeid(this).name(), __func__, ## args); 48 | 49 | class LoggerGuard 50 | { 51 | public: 52 | LoggerGuard(); 53 | ~LoggerGuard(); 54 | 55 | int Init(const std::string & sModuleName, const std::string & sLogPath, const int iLogLevel); 56 | 57 | static LoggerGuard * Instance(); 58 | 59 | phxpaxos::LogFunc GetLogFunc(); 60 | 61 | private: 62 | phxpaxos::LogFunc m_pLogFunc; 63 | }; 64 | 65 | } 66 | -------------------------------------------------------------------------------- /sample/phxkv/phxkv.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package phxkv; 4 | 5 | service PhxKVServer { 6 | rpc Put(KVOperator) returns (KVResponse) { } 7 | rpc GetLocal(KVOperator) returns (KVResponse) { } 8 | rpc GetGlobal(KVOperator) returns (KVResponse) { } 9 | rpc Delete(KVOperator) returns (KVResponse) { } 10 | } 11 | 12 | message KVOperator 13 | { 14 | string key = 1; 15 | bytes value = 2; 16 | uint64 version = 3; 17 | uint32 operator = 4; 18 | uint32 sid = 5; 19 | }; 20 | 21 | message KVData 22 | { 23 | bytes value = 1; 24 | uint64 version = 2; 25 | bool isdeleted = 3; 26 | }; 27 | 28 | message KVResponse 29 | { 30 | KVData data = 1; 31 | int32 ret = 2; 32 | uint64 master_nodeid = 3; 33 | }; 34 | -------------------------------------------------------------------------------- /sample/phxkv/prepare_dir.sh: -------------------------------------------------------------------------------- 1 | #create kvdb path and paxoslog path 2 | mkdir storage 3 | cd storage 4 | mkdir kvdb_0 5 | mkdir paxoslog_0 6 | mkdir kvdb_1 7 | mkdir paxoslog_1 8 | mkdir kvdb_2 9 | mkdir paxoslog_2 10 | cd .. 11 | 12 | #create glog path 13 | mkdir log 14 | -------------------------------------------------------------------------------- /sample/phxkv/run_server_sample.sh: -------------------------------------------------------------------------------- 1 | #sample 2 | #./phxkv_grpcserver 127.0.0.1:21111 127.0.0.1:11111 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 ./storage/kvdb_0 ./storage/paxoslog_0 3 | #./phxkv_grpcserver 127.0.0.1:21112 127.0.0.1:11112 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 ./storage/kvdb_1 ./storage/paxoslog_1 4 | #./phxkv_grpcserver 127.0.0.1:21113 127.0.0.1:11113 127.0.0.1:11111,127.0.0.1:11112,127.0.0.1:11113 ./storage/kvdb_2 ./storage/paxoslog_2 5 | -------------------------------------------------------------------------------- /src/algorithm/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libalgorithm.a 21 | 22 | ALGORITHM_OBJ=base.o proposer.o acceptor.o learner.o learner_sender.o instance.o ioloop.o commitctx.o committer.o checkpoint_sender.o checkpoint_receiver.o msg_counter.o 23 | 24 | ALGORITHM_LIB=algorithm src/comm:comm src/logstorage:logstorage src/sm-base:smbase include:include src/checkpoint:checkpoint src/config:config 25 | 26 | ALGORITHM_SYS_LIB= 27 | 28 | ALGORITHM_INCS=$(SRC_BASE_PATH)/src/algorithm 29 | 30 | ALGORITHM_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/algorithm/acceptor.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "base.h" 25 | #include 26 | #include "comm_include.h" 27 | #include "paxos_log.h" 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | class AcceptorState 33 | { 34 | public: 35 | AcceptorState(const Config * poConfig, const LogStorage * poLogStorage); 36 | ~AcceptorState(); 37 | 38 | void Init(); 39 | 40 | const BallotNumber & GetPromiseBallot() const; 41 | void SetPromiseBallot(const BallotNumber & oPromiseBallot); 42 | 43 | const BallotNumber & GetAcceptedBallot() const; 44 | void SetAcceptedBallot(const BallotNumber & oAcceptedBallot); 45 | 46 | const std::string & GetAcceptedValue(); 47 | void SetAcceptedValue(const std::string & sAcceptedValue); 48 | 49 | const uint32_t GetChecksum() const; 50 | 51 | int Persist(const uint64_t llInstanceID, const uint32_t iLastChecksum); 52 | int Load(uint64_t & llInstanceID); 53 | 54 | //private: 55 | BallotNumber m_oPromiseBallot; 56 | BallotNumber m_oAcceptedBallot; 57 | std::string m_sAcceptedValue; 58 | uint32_t m_iChecksum; 59 | 60 | Config * m_poConfig; 61 | PaxosLog m_oPaxosLog; 62 | 63 | int m_iSyncTimes; 64 | }; 65 | 66 | //////////////////////////////////////////////////////////////// 67 | 68 | class Acceptor : public Base 69 | { 70 | public: 71 | Acceptor( 72 | const Config * poConfig, 73 | const MsgTransport * poMsgTransport, 74 | const Instance * poInstance, 75 | const LogStorage * poLogStorage); 76 | ~Acceptor(); 77 | 78 | virtual void InitForNewPaxosInstance(); 79 | 80 | int Init(); 81 | 82 | AcceptorState * GetAcceptorState(); 83 | 84 | int OnPrepare(const PaxosMsg & oPaxosMsg); 85 | 86 | void OnAccept(const PaxosMsg & oPaxosMsg); 87 | 88 | //private: 89 | AcceptorState m_oAcceptorState; 90 | }; 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/algorithm/checkpoint_receiver.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include "phxpaxos/options.h" 27 | #include "comm_include.h" 28 | #include "config_include.h" 29 | 30 | namespace phxpaxos 31 | { 32 | 33 | class Config; 34 | class LogStorage; 35 | 36 | class CheckpointReceiver 37 | { 38 | public: 39 | CheckpointReceiver(Config * poConfig, LogStorage * poLogStorage); 40 | ~CheckpointReceiver(); 41 | 42 | void Reset(); 43 | 44 | int NewReceiver(const nodeid_t iSenderNodeID, const uint64_t llUUID); 45 | 46 | const bool IsReceiverFinish(const nodeid_t iSenderNodeID, const uint64_t llUUID, const uint64_t llEndSequence); 47 | 48 | const std::string GetTmpDirPath(const int iSMID); 49 | 50 | int ReceiveCheckpoint(const CheckpointMsg & oCheckpointMsg); 51 | 52 | int InitFilePath(const std::string & sFilePath, std::string & sFormatFilePath); 53 | private: 54 | 55 | int ClearCheckpointTmp(); 56 | 57 | int CreateDir(const std::string & sDirPath); 58 | 59 | private: 60 | Config * m_poConfig; 61 | LogStorage * m_poLogStorage; 62 | 63 | private: 64 | nodeid_t m_iSenderNodeID; 65 | uint64_t m_llUUID; 66 | uint64_t m_llSequence; 67 | 68 | private: 69 | std::map m_mapHasInitDir; 70 | }; 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/algorithm/checkpoint_sender.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "utils_include.h" 25 | #include "phxpaxos/options.h" 26 | #include "phxpaxos/sm.h" 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | class Learner; 32 | class Config; 33 | class SMFac; 34 | class CheckpointMgr; 35 | 36 | #define Checkpoint_ACK_TIMEOUT 120000 37 | #define Checkpoint_ACK_LEAD 10 38 | 39 | class CheckpointSender : public Thread 40 | { 41 | public: 42 | CheckpointSender( 43 | const nodeid_t iSendNodeID, 44 | Config * poConfig, 45 | Learner * poLearner, 46 | SMFac * poSMFac, 47 | CheckpointMgr * poCheckpointMgr); 48 | 49 | ~CheckpointSender(); 50 | 51 | void Stop(); 52 | 53 | void run(); 54 | 55 | void End(); 56 | 57 | const bool IsEnd() const; 58 | 59 | void Ack(const nodeid_t iSendNodeID, const uint64_t llUUID, const uint64_t llSequence); 60 | 61 | private: 62 | void SendCheckpoint(); 63 | 64 | int LockCheckpoint(); 65 | 66 | void UnLockCheckpoint(); 67 | 68 | int SendCheckpointFofaSM(StateMachine * poSM); 69 | 70 | int SendFile(const StateMachine * poSM, const std::string & sDirPath, const std::string & sFilePath); 71 | 72 | int SendBuffer(const int iSMID, const uint64_t llCheckpointInstanceID, const std::string & sFilePath, 73 | const uint64_t llOffset, const std::string & sBuffer); 74 | 75 | const bool CheckAck(const uint64_t llSendSequence); 76 | 77 | private: 78 | nodeid_t m_iSendNodeID; 79 | 80 | Config * m_poConfig; 81 | Learner * m_poLearner; 82 | SMFac * m_poSMFac; 83 | CheckpointMgr * m_poCheckpointMgr; 84 | 85 | bool m_bIsEnd; 86 | bool m_bIsEnded; 87 | bool m_bIsStarted; 88 | 89 | private: 90 | uint64_t m_llUUID; 91 | uint64_t m_llSequence; 92 | 93 | private: 94 | uint64_t m_llAckSequence; 95 | uint64_t m_llAbsLastAckTime; 96 | 97 | private: 98 | char m_sTmpBuffer[1048576]; 99 | 100 | std::map m_mapAlreadySendedFile; 101 | }; 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/algorithm/commitctx.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "comm_include.h" 26 | #include "config_include.h" 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | class StateMachine; 32 | 33 | class CommitCtx 34 | { 35 | public: 36 | CommitCtx(Config * poConfig); 37 | ~CommitCtx(); 38 | 39 | void NewCommit(std::string * psValue, SMCtx * poSMCtx, const int iTimeoutMs); 40 | 41 | const bool IsNewCommit() const; 42 | 43 | std::string & GetCommitValue(); 44 | 45 | void StartCommit(const uint64_t llInstanceID); 46 | 47 | bool IsMyCommit(const uint64_t llInstanceID, const std::string & sLearnValue, SMCtx *& poSMCtx); 48 | 49 | public: 50 | void SetResult(const int iCommitRet, const uint64_t llInstanceID, const std::string & sLearnValue); 51 | 52 | void SetResultOnlyRet(const int iCommitRet); 53 | 54 | int GetResult(uint64_t & llSuccInstanceID); 55 | 56 | public: 57 | const int GetTimeoutMs() const; 58 | 59 | private: 60 | Config * m_poConfig; 61 | 62 | uint64_t m_llInstanceID; 63 | int m_iCommitRet; 64 | bool m_bIsCommitEnd; 65 | int m_iTimeoutMs; 66 | 67 | std::string * m_psValue; 68 | SMCtx * m_poSMCtx; 69 | SerialLock m_oSerialLock; 70 | }; 71 | } 72 | -------------------------------------------------------------------------------- /src/algorithm/committer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include "comm_include.h" 27 | #include "sm_base.h" 28 | #include "config_include.h" 29 | 30 | namespace phxpaxos 31 | { 32 | 33 | class CommitCtx; 34 | class IOLoop; 35 | 36 | class Committer 37 | { 38 | public: 39 | Committer(Config * poConfig, CommitCtx * poCommitCtx, IOLoop * poIOLoop, SMFac * poSMFac); 40 | ~Committer(); 41 | 42 | public: 43 | int NewValueGetID(const std::string & sValue, uint64_t & llInstanceID); 44 | 45 | int NewValueGetID(const std::string & sValue, uint64_t & llInstanceID, SMCtx * poSMCtx); 46 | 47 | int NewValueGetIDNoRetry(const std::string & sValue, uint64_t & llInstanceID, SMCtx * poSMCtx); 48 | 49 | int NewValue(const std::string & sValue); 50 | 51 | public: 52 | void SetTimeoutMs(const int iTimeoutMs); 53 | 54 | void SetMaxHoldThreads(const int iMaxHoldThreads); 55 | 56 | void SetProposeWaitTimeThresholdMS(const int iWaitTimeThresholdMS); 57 | 58 | private: 59 | void LogStatus(); 60 | 61 | private: 62 | Config * m_poConfig; 63 | CommitCtx * m_poCommitCtx; 64 | IOLoop * m_poIOLoop; 65 | SMFac * m_poSMFac; 66 | 67 | WaitLock m_oWaitLock; 68 | int m_iTimeoutMs; 69 | 70 | uint64_t m_llLastLogTime; 71 | }; 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/algorithm/ioloop.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "timer.h" 26 | #include "utils_include.h" 27 | #include 28 | #include "comm_include.h" 29 | #include 30 | #include "config_include.h" 31 | 32 | namespace phxpaxos 33 | { 34 | 35 | #define RETRY_QUEUE_MAX_LEN 300 36 | 37 | class Instance; 38 | 39 | class IOLoop : public Thread 40 | { 41 | public: 42 | IOLoop(Config * poConfig, Instance * poInstance); 43 | virtual ~IOLoop(); 44 | 45 | void run(); 46 | 47 | void Stop(); 48 | 49 | void OneLoop(const int iTimeoutMs); 50 | 51 | void DealWithRetry(); 52 | 53 | void ClearRetryQueue(); 54 | 55 | public: 56 | int AddMessage(const char * pcMessage, const int iMessageLen); 57 | 58 | int AddRetryPaxosMsg(const PaxosMsg & oPaxosMsg); 59 | 60 | void AddNotify(); 61 | 62 | public: 63 | virtual bool AddTimer(const int iTimeout, const int iType, uint32_t & iTimerID); 64 | 65 | virtual void RemoveTimer(uint32_t & iTimerID); 66 | 67 | void DealwithTimeout(int & iNextTimeout); 68 | 69 | void DealwithTimeoutOne(const uint32_t iTimerID, const int iType); 70 | 71 | private: 72 | bool m_bIsEnd; 73 | bool m_bIsStart; 74 | Timer m_oTimer; 75 | std::map m_mapTimerIDExist; 76 | 77 | Queue m_oMessageQueue; 78 | std::queue m_oRetryQueue; 79 | 80 | int m_iQueueMemSize; 81 | 82 | Config * m_poConfig; 83 | Instance * m_poInstance; 84 | }; 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/algorithm/learner_sender.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "utils_include.h" 25 | #include "comm_include.h" 26 | #include "config_include.h" 27 | #include "paxos_log.h" 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | class Learner; 33 | 34 | class LearnerSender : public Thread 35 | { 36 | public: 37 | LearnerSender(Config * poConfig, Learner * poLearner, PaxosLog * poPaxosLog); 38 | ~LearnerSender(); 39 | 40 | void run(); 41 | 42 | void Stop(); 43 | 44 | public: 45 | const bool Prepare(const uint64_t llBeginInstanceID, const nodeid_t iSendToNodeID); 46 | 47 | const bool Comfirm(const uint64_t llBeginInstanceID, const nodeid_t iSendToNodeID); 48 | 49 | void Ack(const uint64_t llAckInstanceID, const nodeid_t iFromNodeID); 50 | 51 | private: 52 | void WaitToSend(); 53 | 54 | void SendLearnedValue(const uint64_t llBeginInstanceID, const nodeid_t iSendToNodeID); 55 | 56 | int SendOne(const uint64_t llSendInstanceID, const nodeid_t iSendToNodeID, uint32_t & iLastChecksum); 57 | 58 | void SendDone(); 59 | 60 | const bool IsIMSending(); 61 | 62 | void ReleshSending(); 63 | 64 | const bool CheckAck(const uint64_t llSendInstanceID); 65 | 66 | void CutAckLead(); 67 | 68 | private: 69 | Config * m_poConfig; 70 | Learner * m_poLearner; 71 | PaxosLog * m_poPaxosLog; 72 | SerialLock m_oLock; 73 | 74 | bool m_bIsIMSending; 75 | uint64_t m_llAbsLastSendTime; 76 | 77 | uint64_t m_llBeginInstanceID; 78 | nodeid_t m_iSendToNodeID; 79 | 80 | bool m_bIsComfirmed; 81 | 82 | uint64_t m_llAckInstanceID; 83 | uint64_t m_llAbsLastAckTime; 84 | int m_iAckLead; 85 | 86 | bool m_bIsEnd; 87 | bool m_bIsStart; 88 | }; 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/algorithm/msg_counter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "msg_counter.h" 23 | #include 24 | #include "config_include.h" 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | MsgCounter :: MsgCounter(const Config * poConfig) 30 | { 31 | m_poConfig = (Config *)poConfig; 32 | StartNewRound(); 33 | } 34 | 35 | MsgCounter :: ~MsgCounter() 36 | { 37 | } 38 | 39 | void MsgCounter :: StartNewRound() 40 | { 41 | m_setReceiveMsgNodeID.clear(); 42 | m_setRejectMsgNodeID.clear(); 43 | m_setPromiseOrAcceptMsgNodeID.clear(); 44 | } 45 | 46 | void MsgCounter :: AddReceive(const nodeid_t iNodeID) 47 | { 48 | if (m_setReceiveMsgNodeID.find(iNodeID) == m_setReceiveMsgNodeID.end()) 49 | { 50 | m_setReceiveMsgNodeID.insert(iNodeID); 51 | } 52 | } 53 | 54 | void MsgCounter :: AddReject(const nodeid_t iNodeID) 55 | { 56 | if (m_setRejectMsgNodeID.find(iNodeID) == m_setRejectMsgNodeID.end()) 57 | { 58 | m_setRejectMsgNodeID.insert(iNodeID); 59 | } 60 | } 61 | 62 | void MsgCounter :: AddPromiseOrAccept(const nodeid_t iNodeID) 63 | { 64 | if (m_setPromiseOrAcceptMsgNodeID.find(iNodeID) == m_setPromiseOrAcceptMsgNodeID.end()) 65 | { 66 | m_setPromiseOrAcceptMsgNodeID.insert(iNodeID); 67 | } 68 | } 69 | 70 | bool MsgCounter :: IsPassedOnThisRound() 71 | { 72 | return (int)m_setPromiseOrAcceptMsgNodeID.size() >= m_poConfig->GetMajorityCount(); 73 | } 74 | 75 | bool MsgCounter :: IsRejectedOnThisRound() 76 | { 77 | return (int)m_setRejectMsgNodeID.size() >= m_poConfig->GetMajorityCount(); 78 | } 79 | 80 | bool MsgCounter :: IsAllReceiveOnThisRound() 81 | { 82 | return (int)m_setReceiveMsgNodeID.size() == m_poConfig->GetNodeCount(); 83 | } 84 | 85 | } 86 | 87 | 88 | -------------------------------------------------------------------------------- /src/algorithm/msg_counter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include "commdef.h" 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | class Config; 32 | class PaxosLog; 33 | 34 | class MsgCounter 35 | { 36 | public: 37 | MsgCounter(const Config * poConfig); 38 | ~MsgCounter(); 39 | 40 | void AddReceive(const nodeid_t iNodeID); 41 | void AddReject(const nodeid_t iNodeID); 42 | void AddPromiseOrAccept(const nodeid_t iNodeID); 43 | 44 | bool IsPassedOnThisRound(); 45 | bool IsRejectedOnThisRound(); 46 | bool IsAllReceiveOnThisRound(); 47 | 48 | void StartNewRound(); 49 | 50 | public: 51 | Config * m_poConfig; 52 | 53 | std::set m_setReceiveMsgNodeID; 54 | std::set m_setRejectMsgNodeID; 55 | std::set m_setPromiseOrAcceptMsgNodeID; 56 | }; 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/benchmark/HOW_TO_BENCH: -------------------------------------------------------------------------------- 1 | 1. make. 2 | 2. run phx_paxos_bench on different machine. 3 | 3. run phx_paxos_bench and appoint bench. 4 | 5 | #args explanation: 6 | phx_paxos_bench have 4 args like " " 7 | 8 | Myip:myport means running machine's ip/port. 9 | The second arg is all running machine's ip/port list. 10 | Third arg, is to appoint bench or not. Only one machine need to appoint bench(means fill this arg as 'y'). 11 | Last arg, set how many paxos group you want to running on one machine. different paxos group count will have different benchmark. 12 | 13 | #sample command. 14 | 15 | Now grant we have three machine. (10.10.10.10:11111), (10.10.10.11:11111), (10.10.10.12:11111). 16 | You must change this ip/port list to your real environment first. 17 | And now I want to get 20 paxos group count's benchmark. 18 | We do this: 19 | 20 | #first run below command on first machine. 21 | ./phx_paxos_bench 10.10.10.10:11111 10.10.10.10:11111,10.10.10.11:11111,10.10.10.12:11111 n 20 22 | 23 | #second run below command on second machine. 24 | ./phx_paxos_bench 10.10.10.10:11112 10.10.10.10:11111,10.10.10.11:11111,10.10.10.12:11111 n 20 25 | 26 | #last run below command on third machine. Notice that need to appoint bench on args. 27 | ./phx_paxos_bench 10.10.10.12:11112 10.10.10.10:11111,10.10.10.11:11111,10.10.10.12:11111 y 20 28 | 29 | Wait a minutes. then you will get the benchmark(qps) on standard output. 30 | 31 | ##Notice 32 | Every times you run phx_paxos_bench, it will generate a dir names logpath_myip_myport on current dir. 33 | This dir is use to save paxos data on disk. 34 | 35 | So if you want to reset all things before bench, just rm this dirs on every machine. 36 | -------------------------------------------------------------------------------- /src/benchmark/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=phx_paxos_bench bench_db 21 | 22 | PHX_PAXOS_BENCH_OBJ=bench_sm.o bench_server.o bench_main.o 23 | 24 | PHX_PAXOS_BENCH_LIB=src/utils:utils src/algorithm:algorithm 25 | 26 | PHX_PAXOS_BENCH_SYS_LIB=$(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(PROTOBUF_LIB_PATH)/libprotobuf.a -lpthread 27 | 28 | PHX_PAXOS_BENCH_INCS=$(SRC_BASE_PATH)/src/benchmark $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 29 | 30 | PHX_PAXOS_BENCH_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | BENCH_DB_OBJ=bench_db.o 33 | 34 | BENCH_DB_LIB= 35 | 36 | BENCH_DB_SYS_LIB=$(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(PROTOBUF_LIB_PATH)/libprotobuf.a -lpthread 37 | 38 | BENCH_DB_INCS=$(SRC_BASE_PATH)/src/benchmark $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 39 | 40 | BENCH_DB_EXTRA_CPPFLAGS=-Wall -Werror 41 | 42 | -------------------------------------------------------------------------------- /src/benchmark/bench_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/node.h" 25 | #include "bench_sm.h" 26 | #include 27 | #include 28 | 29 | namespace bench 30 | { 31 | 32 | class BenchServer 33 | { 34 | public: 35 | BenchServer(const int iGroupCount, const phxpaxos::NodeInfo & oMyNode, const phxpaxos::NodeInfoList & vecNodeList); 36 | ~BenchServer(); 37 | 38 | int RunPaxos(); 39 | 40 | int ReadyBench(); 41 | 42 | int Write(const std::string & sBenchValue); 43 | 44 | int Write(const int iGroupIdx, const std::string & sBenchValue); 45 | 46 | private: 47 | int MakeLogStoragePath(std::string & sLogStoragePath); 48 | 49 | private: 50 | phxpaxos::NodeInfo m_oMyNode; 51 | phxpaxos::NodeInfoList m_vecNodeList; 52 | 53 | int m_iGroupCount; 54 | std::vector m_vecSMList; 55 | 56 | phxpaxos::Node * m_poPaxosNode; 57 | }; 58 | 59 | } 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/benchmark/bench_sm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "bench_sm.h" 23 | 24 | using namespace phxpaxos; 25 | 26 | namespace bench 27 | { 28 | 29 | BenchSM :: BenchSM(const phxpaxos::nodeid_t llMyNodeID, const int iGroupIdx) 30 | : m_llMyNodeID(llMyNodeID), m_iGroupIdx(iGroupIdx) 31 | { 32 | } 33 | 34 | bool BenchSM :: Execute(const int iGroupIdx, const uint64_t llInstanceID, 35 | const std::string & sPaxosValue, SMCtx * poSMCtx) 36 | { 37 | //bench sm do nothing 38 | return true; 39 | } 40 | 41 | const int BenchSM :: GetGroupIdx() const 42 | { 43 | return m_iGroupIdx; 44 | } 45 | 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/benchmark/bench_sm.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/sm.h" 25 | #include "phxpaxos/options.h" 26 | #include 27 | #include 28 | 29 | namespace bench 30 | { 31 | 32 | class BenchSM : public phxpaxos::StateMachine 33 | { 34 | public: 35 | BenchSM(const phxpaxos::nodeid_t llMyNodeID, const int iGroupIdx); 36 | 37 | bool Execute(const int iGroupIdx, const uint64_t llInstanceID, 38 | const std::string & sPaxosValue, phxpaxos::SMCtx * poSMCtx); 39 | 40 | const int SMID() const { return 1; } 41 | 42 | //no checkpoint 43 | bool ExecuteForCheckpoint(const int iGroupIdx, const uint64_t llInstanceID, 44 | const std::string & sPaxosValue) { return true; } 45 | 46 | const uint64_t GetCheckpointInstanceID(const int iGroupIdx) const { return phxpaxos::NoCheckpoint; } 47 | 48 | const int GetGroupIdx() const; 49 | 50 | public: 51 | //no checkpoint 52 | int GetCheckpointState(const int iGroupIdx, std::string & sDirPath, 53 | std::vector & vecFileList) { return 0; } 54 | 55 | //no checkpoint 56 | int LoadCheckpointState(const int iGroupIdx, const std::string & sCheckpointTmpFileDirPath, 57 | const std::vector & vecFileList, const uint64_t llCheckpointInstanceID) { return 0; } 58 | 59 | //no checkpoint 60 | int LockCheckpointState() { return 0; } 61 | 62 | //no checkpoint 63 | void UnLockCheckpointState() { } 64 | 65 | private: 66 | phxpaxos::nodeid_t m_llMyNodeID; 67 | int m_iGroupIdx; 68 | }; 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/checkpoint/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libcheckpoint.a 21 | 22 | CHECKPOINT_OBJ=cp_mgr.o replayer.o cleaner.o 23 | 24 | CHECKPOINT_LIB=checkpoint src/comm:comm src/logstorage:logstorage src/sm-base:smbase include:include src/utils:utils src/config:config 25 | 26 | CHECKPOINT_SYS_LIB= 27 | 28 | CHECKPOINT_INCS=$(SRC_BASE_PATH)/src/checkpoint 29 | 30 | CHECKPOINT_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/checkpoint/cleaner.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "utils_include.h" 26 | 27 | namespace phxpaxos 28 | { 29 | 30 | #define CAN_DELETE_DELTA 1000000 31 | #define DELETE_SAVE_INTERVAL 100 32 | 33 | class Config; 34 | class SMFac; 35 | class LogStorage; 36 | class CheckpointMgr; 37 | 38 | class Cleaner : public Thread 39 | { 40 | public: 41 | Cleaner( 42 | Config * poConfig, 43 | SMFac * poSMFac, 44 | LogStorage * poLogStorage, 45 | CheckpointMgr * poCheckpointMgr); 46 | 47 | ~Cleaner(); 48 | 49 | void Stop(); 50 | 51 | void run(); 52 | 53 | void Pause(); 54 | 55 | void Continue(); 56 | 57 | const bool IsPaused() const; 58 | 59 | public: 60 | void SetHoldPaxosLogCount(const uint64_t llHoldCount); 61 | 62 | int FixMinChosenInstanceID(const uint64_t llOldMinChosenInstanceID); 63 | 64 | private: 65 | bool DeleteOne(const uint64_t llInstanceID); 66 | 67 | private: 68 | Config * m_poConfig; 69 | SMFac * m_poSMFac; 70 | LogStorage * m_poLogStorage; 71 | CheckpointMgr * m_poCheckpointMgr; 72 | 73 | uint64_t m_llLastSave; 74 | 75 | bool m_bCanrun; 76 | bool m_bIsPaused; 77 | 78 | bool m_bIsEnd; 79 | bool m_bIsStart; 80 | 81 | uint64_t m_llHoldCount; 82 | }; 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/checkpoint/cp_mgr.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "replayer.h" 25 | #include "cleaner.h" 26 | #include "phxpaxos/options.h" 27 | #include 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | class CheckpointMgr 33 | { 34 | public: 35 | CheckpointMgr( 36 | Config * poConfig, 37 | SMFac * poSMFac, 38 | LogStorage * poLogStorage, 39 | const bool bUseCheckpointReplayer); 40 | 41 | ~CheckpointMgr(); 42 | 43 | int Init(); 44 | 45 | void Start(); 46 | 47 | void Stop(); 48 | 49 | Replayer * GetReplayer(); 50 | 51 | Cleaner * GetCleaner(); 52 | 53 | public: 54 | int PrepareForAskforCheckpoint(const nodeid_t iSendNodeID); 55 | 56 | const bool InAskforcheckpointMode() const; 57 | 58 | void ExitCheckpointMode(); 59 | 60 | public: 61 | const uint64_t GetMinChosenInstanceID() const; 62 | 63 | int SetMinChosenInstanceID(const uint64_t llMinChosenInstanceID); 64 | 65 | void SetMinChosenInstanceIDCache(const uint64_t llMinChosenInstanceID); 66 | 67 | const uint64_t GetCheckpointInstanceID() const; 68 | 69 | const uint64_t GetMaxChosenInstanceID() const; 70 | 71 | void SetMaxChosenInstanceID(const uint64_t llMaxChosenInstanceID); 72 | 73 | private: 74 | Config * m_poConfig; 75 | LogStorage * m_poLogStorage; 76 | SMFac * m_poSMFac; 77 | 78 | Replayer m_oReplayer; 79 | Cleaner m_oCleaner; 80 | 81 | uint64_t m_llMinChosenInstanceID; 82 | uint64_t m_llMaxChosenInstanceID; 83 | 84 | private: 85 | bool m_bInAskforCheckpointMode; 86 | std::set m_setNeedAsk; 87 | uint64_t m_llLastAskforCheckpointTime; 88 | 89 | bool m_bUseCheckpointReplayer; 90 | }; 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/checkpoint/replayer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "utils_include.h" 25 | #include "paxos_log.h" 26 | 27 | namespace phxpaxos 28 | { 29 | 30 | class Config; 31 | class SMFac; 32 | class LogStorage; 33 | class CheckpointMgr; 34 | 35 | class Replayer : public Thread 36 | { 37 | public: 38 | Replayer( 39 | Config * poConfig, 40 | SMFac * poSMFac, 41 | LogStorage * poLogStorage, 42 | CheckpointMgr * poCheckpointMgr); 43 | 44 | ~Replayer(); 45 | 46 | void Stop(); 47 | 48 | void run(); 49 | 50 | void Pause(); 51 | 52 | void Continue(); 53 | 54 | const bool IsPaused() const; 55 | 56 | private: 57 | bool PlayOne(const uint64_t llInstanceID); 58 | 59 | private: 60 | Config * m_poConfig; 61 | SMFac * m_poSMFac; 62 | PaxosLog m_oPaxosLog; 63 | CheckpointMgr * m_poCheckpointMgr; 64 | 65 | bool m_bCanrun; 66 | bool m_bIsPaused; 67 | bool m_bIsEnd; 68 | }; 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/comm/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libcomm.a 21 | 22 | COMM_OBJ=paxos_msg.pb.o breakpoint.o options.o inside_options.o logger.o 23 | 24 | COMM_LIB=comm include:include src/utils:utils 25 | 26 | COMM_SYS_LIB=$(PROTOBUF_LIB_PATH)/libprotobuf.a 27 | 28 | COMM_INCS=$(SRC_BASE_PATH)/src/comm $(PROTOBUF_INCLUDE_PATH) 29 | 30 | COMM_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/comm/breakpoint.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "phxpaxos/breakpoint.h" 23 | 24 | namespace phxpaxos 25 | { 26 | 27 | Breakpoint * Breakpoint :: m_poBreakpoint = nullptr; 28 | 29 | Breakpoint :: Breakpoint() 30 | { 31 | } 32 | 33 | void Breakpoint :: SetInstance(Breakpoint * poBreakpoint) 34 | { 35 | m_poBreakpoint = poBreakpoint; 36 | } 37 | 38 | Breakpoint * Breakpoint :: Instance() 39 | { 40 | if (m_poBreakpoint != nullptr) 41 | { 42 | return m_poBreakpoint; 43 | } 44 | 45 | static Breakpoint oBreakpoint; 46 | return &oBreakpoint; 47 | } 48 | 49 | ProposerBP * Breakpoint :: GetProposerBP() 50 | { 51 | return &m_oProposerBP; 52 | } 53 | 54 | AcceptorBP * Breakpoint :: GetAcceptorBP() 55 | { 56 | return &m_oAcceptorBP; 57 | } 58 | 59 | LearnerBP * Breakpoint :: GetLearnerBP() 60 | { 61 | return &m_oLearnerBP; 62 | } 63 | 64 | InstanceBP * Breakpoint :: GetInstanceBP() 65 | { 66 | return &m_oInstanceBP; 67 | } 68 | 69 | CommiterBP * Breakpoint :: GetCommiterBP() 70 | { 71 | return &m_oCommiterBP; 72 | } 73 | 74 | IOLoopBP * Breakpoint :: GetIOLoopBP() 75 | { 76 | return &m_oIOLoopBP; 77 | } 78 | 79 | NetworkBP * Breakpoint :: GetNetworkBP() 80 | { 81 | return &m_oNetworkBP; 82 | } 83 | 84 | LogStorageBP * Breakpoint :: GetLogStorageBP() 85 | { 86 | return &m_oLogStorageBP; 87 | } 88 | 89 | AlgorithmBaseBP * Breakpoint :: GetAlgorithmBaseBP() 90 | { 91 | return &m_oAlgorithmBaseBP; 92 | } 93 | 94 | CheckpointBP * Breakpoint :: GetCheckpointBP() 95 | { 96 | return &m_oCheckpointBP; 97 | } 98 | 99 | MasterBP * Breakpoint :: GetMasterBP() 100 | { 101 | return &m_oMasterBP; 102 | } 103 | 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /src/comm/comm_include.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "./paxos_msg.pb.h" 25 | #include "phxpaxos/breakpoint.h" 26 | #include "utils_include.h" 27 | #include "commdef.h" 28 | -------------------------------------------------------------------------------- /src/comm/logger.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "phxpaxos/log.h" 26 | #include "utils_include.h" 27 | #include 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | #define LOGGER (Logger::Instance()) 33 | #define LOG_ERROR(format, args...)\ 34 | LOGGER->LogError(format, ## args); 35 | #define LOG_STATUS(format, args...)\ 36 | LOGGER->LogStatus(format, ## args); 37 | #define LOG_WARNING(format, args...)\ 38 | LOGGER->LogWarning(format, ## args); 39 | #define LOG_INFO(format, args...)\ 40 | LOGGER->LogInfo(format, ## args); 41 | #define LOG_VERBOSE(format, args...)\ 42 | LOGGER->LogVerbose(format, ## args); 43 | #define LOG_SHOWY(format, args...)\ 44 | LOGGER->LogShowy(format, ## args); 45 | 46 | class Logger 47 | { 48 | public: 49 | Logger(); 50 | ~Logger(); 51 | 52 | static Logger * Instance(); 53 | 54 | void InitLogger(const LogLevel eLogLevel); 55 | 56 | void SetLogFunc(LogFunc pLogFunc); 57 | 58 | void LogError(const char * pcFormat, ...); 59 | 60 | void LogStatus(const char * pcFormat, ...); 61 | 62 | void LogWarning(const char * pcFormat, ...); 63 | 64 | void LogInfo(const char * pcFormat, ...); 65 | 66 | void LogVerbose(const char * pcFormat, ...); 67 | 68 | void LogShowy(const char * pcFormat, ...); 69 | 70 | private: 71 | LogFunc m_pLogFunc; 72 | LogLevel m_eLogLevel; 73 | std::mutex m_oMutex; 74 | }; 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/comm/msg_transport.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/options.h" 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | enum Message_SendType 30 | { 31 | Message_SendType_UDP = 0, 32 | Message_SendType_TCP = 1, 33 | }; 34 | 35 | class MsgTransport 36 | { 37 | public: 38 | virtual ~MsgTransport() {} 39 | 40 | virtual int SendMessage(const int iGroupIdx, const nodeid_t iSendtoNodeID, 41 | const std::string & sBuffer, const int iSendType = Message_SendType_UDP) = 0; 42 | 43 | virtual int BroadcastMessage(const int iGroupIdx, const std::string & sBuffer, 44 | const int iSendType = Message_SendType_UDP) = 0; 45 | 46 | virtual int BroadcastMessageFollower(const int iGroupIdx, const std::string & sBuffer, 47 | const int iSendType = Message_SendType_UDP) = 0; 48 | 49 | virtual int BroadcastMessageTempNode(const int iGroupIdx, const std::string & sBuffer, 50 | const int iSendType = Message_SendType_UDP) = 0; 51 | }; 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/comm/paxos_msg.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | package phxpaxos; 3 | 4 | message Header 5 | { 6 | required uint64 gid = 1; 7 | required uint64 rid = 2; 8 | required int32 cmdid = 3; 9 | optional int32 version = 4; 10 | }; 11 | 12 | message PaxosMsg 13 | { 14 | required int32 MsgType = 1; 15 | optional uint64 InstanceID = 2; 16 | optional uint64 NodeID = 3; 17 | optional uint64 ProposalID = 4; 18 | optional uint64 ProposalNodeID = 5; 19 | optional bytes Value = 6; 20 | optional uint64 PreAcceptID = 7; 21 | optional uint64 PreAcceptNodeID = 8; 22 | optional uint64 RejectByPromiseID = 9; 23 | optional uint64 NowInstanceID = 10; 24 | optional uint64 MinChosenInstanceID = 11; 25 | optional uint32 LastChecksum = 12; 26 | optional uint32 Flag = 13; 27 | optional bytes SystemVariables = 14; 28 | optional bytes MasterVariables = 15; 29 | }; 30 | 31 | message CheckpointMsg 32 | { 33 | required int32 MsgType = 1; 34 | required uint64 NodeID = 2; 35 | optional int32 Flag = 3; 36 | required uint64 UUID = 4; 37 | required uint64 Sequence = 5; 38 | optional uint64 CheckpointInstanceID = 6; 39 | optional uint32 Checksum = 7; 40 | optional string FilePath = 8; 41 | optional int32 SMID = 9; 42 | optional uint64 Offset = 10; 43 | optional bytes Buffer = 11; 44 | } 45 | 46 | message AcceptorStateData 47 | { 48 | required uint64 InstanceID = 1; 49 | required uint64 PromiseID = 2; 50 | required uint64 PromiseNodeID = 3; 51 | required uint64 AcceptedID = 4; 52 | required uint64 AcceptedNodeID = 5; 53 | required bytes AcceptedValue = 6; 54 | required uint32 Checksum = 7; 55 | }; 56 | 57 | message PaxosNodeInfo 58 | { 59 | required uint64 Rid = 1; 60 | required uint64 Nodeid = 2; 61 | }; 62 | 63 | message SystemVariables 64 | { 65 | required uint64 Gid = 1; 66 | repeated PaxosNodeInfo MemberShip = 2; 67 | required uint64 Version = 3; 68 | }; 69 | 70 | message MasterVariables 71 | { 72 | required uint64 MasterNodeid = 1; 73 | required uint64 Version = 2; 74 | required uint32 LeaseTime = 3; 75 | }; 76 | 77 | message PaxosValue 78 | { 79 | required int32 SMID = 1; 80 | required bytes Value = 2; 81 | }; 82 | 83 | message BatchPaxosValues 84 | { 85 | repeated PaxosValue Values = 1; 86 | }; 87 | -------------------------------------------------------------------------------- /src/communicate/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libcommunicate.a 21 | 22 | COMMUNICATE_OBJ=dfnetwork.o udp.o network.o communicate.o 23 | 24 | COMMUNICATE_LIB=communicate src/utils:utils src/comm:comm src/config:config include:include src/communicate/tcp:communicate_tcp 25 | 26 | COMMUNICATE_SYS_LIB= 27 | 28 | COMMUNICATE_INCS=$(SRC_BASE_PATH)/src/communicate 29 | 30 | COMMUNICATE_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/communicate/communicate.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/network.h" 25 | #include "phxpaxos/options.h" 26 | #include 27 | #include "msg_transport.h" 28 | #include "config_include.h" 29 | 30 | namespace phxpaxos 31 | { 32 | 33 | class Communicate : public MsgTransport 34 | { 35 | public: 36 | Communicate( 37 | const Config * poConfig, 38 | const nodeid_t iMyNodeID, 39 | const int iUDPMaxSize, 40 | NetWork * poNetwork); 41 | ~Communicate(); 42 | 43 | int SendMessage(const int iGroupIdx, const nodeid_t iSendtoNodeID, const std::string & sMessage, 44 | const int iSendType = Message_SendType_UDP); 45 | 46 | int BroadcastMessage(const int iGroupIdx, const std::string & sMessage, 47 | const int iSendType = Message_SendType_UDP); 48 | 49 | int BroadcastMessageFollower(const int iGroupIdx, const std::string & sMessage, 50 | const int iSendType = Message_SendType_UDP); 51 | 52 | int BroadcastMessageTempNode(const int iGroupIdx, const std::string & sMessage, 53 | const int iSendType = Message_SendType_UDP); 54 | 55 | public: 56 | void SetUDPMaxSize(const size_t iUDPMaxSize); 57 | 58 | private: 59 | int Send(const int iGroupIdx, const nodeid_t iNodeID, 60 | const NodeInfo & tNodeInfo, const std::string & sMessage, const int iSendType); 61 | 62 | private: 63 | Config * m_poConfig; 64 | NetWork * m_poNetwork; 65 | 66 | nodeid_t m_iMyNodeID; 67 | size_t m_iUDPMaxSize; 68 | }; 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/communicate/dfnetwork.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "dfnetwork.h" 23 | #include "udp.h" 24 | 25 | namespace phxpaxos 26 | { 27 | 28 | DFNetWork :: DFNetWork() : m_oUDPRecv(this), m_oTcpIOThread(this) 29 | { 30 | } 31 | 32 | DFNetWork :: ~DFNetWork() 33 | { 34 | PLHead("NetWork Deleted!"); 35 | } 36 | 37 | void DFNetWork :: StopNetWork() 38 | { 39 | m_oUDPRecv.Stop(); 40 | m_oUDPSend.Stop(); 41 | m_oTcpIOThread.Stop(); 42 | } 43 | 44 | int DFNetWork :: Init(const std::string & sListenIp, const int iListenPort, const int iIOThreadCount) 45 | { 46 | int ret = m_oUDPSend.Init(); 47 | if (ret != 0) 48 | { 49 | return ret; 50 | } 51 | 52 | ret = m_oUDPRecv.Init(iListenPort); 53 | if (ret != 0) 54 | { 55 | return ret; 56 | } 57 | 58 | ret = m_oTcpIOThread.Init(sListenIp, iListenPort, iIOThreadCount); 59 | if (ret != 0) 60 | { 61 | PLErr("m_oTcpIOThread Init fail, ret %d", ret); 62 | return ret; 63 | } 64 | 65 | return 0; 66 | } 67 | 68 | void DFNetWork :: RunNetWork() 69 | { 70 | m_oUDPSend.start(); 71 | m_oUDPRecv.start(); 72 | m_oTcpIOThread.Start(); 73 | } 74 | 75 | int DFNetWork :: SendMessageTCP(const int iGroupIdx, const std::string & sIp, const int iPort, const std::string & sMessage) 76 | { 77 | return m_oTcpIOThread.AddMessage(iGroupIdx, sIp, iPort, sMessage); 78 | } 79 | 80 | int DFNetWork :: SendMessageUDP(const int iGroupIdx, const std::string & sIp, const int iPort, const std::string & sMessage) 81 | { 82 | return m_oUDPSend.AddMessage(sIp, iPort, sMessage); 83 | } 84 | 85 | } 86 | 87 | 88 | -------------------------------------------------------------------------------- /src/communicate/dfnetwork.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "udp.h" 26 | #include "tcp.h" 27 | #include "phxpaxos/network.h" 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | class DFNetWork : public NetWork 33 | { 34 | public: 35 | DFNetWork(); 36 | virtual ~DFNetWork(); 37 | 38 | int Init(const std::string & sListenIp, const int iListenPort, const int iIOThreadCount); 39 | 40 | void RunNetWork(); 41 | 42 | void StopNetWork(); 43 | 44 | int SendMessageTCP(const int iGroupIdx, const std::string & sIp, const int iPort, const std::string & sMessage); 45 | 46 | int SendMessageUDP(const int iGroupIdx, const std::string & sIp, const int iPort, const std::string & sMessage); 47 | 48 | private: 49 | UDPRecv m_oUDPRecv; 50 | UDPSend m_oUDPSend; 51 | TcpIOThread m_oTcpIOThread; 52 | }; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/communicate/network.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "phxpaxos/network.h" 23 | #include "phxpaxos/node.h" 24 | #include "commdef.h" 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | NetWork :: NetWork() : m_poNode(nullptr) 30 | { 31 | } 32 | 33 | int NetWork :: OnReceiveMessage(const char * pcMessage, const int iMessageLen) 34 | { 35 | if (m_poNode != nullptr) 36 | { 37 | m_poNode->OnReceiveMessage(pcMessage, iMessageLen); 38 | } 39 | else 40 | { 41 | PLHead("receive msglen %d", iMessageLen); 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/communicate/tcp/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libcommunicate_tcp.a 21 | 22 | COMMUNICATE_TCP_OBJ=event_base.o message_event.o event_loop.o tcp_client.o tcp_acceptor.o notify.o tcp.o 23 | 24 | COMMUNICATE_TCP_LIB=communicate_tcp src/utils:utils include:include src/comm:comm 25 | 26 | COMMUNICATE_TCP_SYS_LIB= 27 | 28 | COMMUNICATE_TCP_INCS=$(SRC_BASE_PATH)/src/communicate/tcp 29 | 30 | COMMUNICATE_TCP_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/communicate/tcp/event_base.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "event_base.h" 23 | #include "event_loop.h" 24 | 25 | namespace phxpaxos 26 | { 27 | 28 | Event :: Event(EventLoop * poEventLoop) : 29 | m_iEvents(0), m_poEventLoop(poEventLoop) 30 | { 31 | m_bIsDestroy = false; 32 | } 33 | 34 | Event :: ~Event() 35 | { 36 | } 37 | 38 | int Event :: OnRead() 39 | { 40 | PLErr("Need Impl"); 41 | return -1; 42 | } 43 | 44 | int Event :: OnWrite() 45 | { 46 | PLErr("Need Impl"); 47 | return -1; 48 | } 49 | 50 | void Event :: OnTimeout(const uint32_t iTimerID, const int iType) 51 | { 52 | PLErr("Need Impl"); 53 | } 54 | 55 | void Event :: JumpoutEpollWait() 56 | { 57 | m_poEventLoop->JumpoutEpollWait(); 58 | } 59 | 60 | void Event :: AddEvent(const int iEvents) 61 | { 62 | int iBeforeEvent = m_iEvents; 63 | m_iEvents |= iEvents; 64 | if (m_iEvents == iBeforeEvent) 65 | { 66 | return; 67 | } 68 | 69 | m_poEventLoop->ModEvent(this, m_iEvents); 70 | } 71 | 72 | void Event :: RemoveEvent(const int iEvents) 73 | { 74 | int iBeforeEvent = m_iEvents; 75 | m_iEvents &= (~iEvents); 76 | if (m_iEvents == iBeforeEvent) 77 | { 78 | return; 79 | } 80 | 81 | if (m_iEvents == 0) 82 | { 83 | m_poEventLoop->RemoveEvent(this); 84 | } 85 | else 86 | { 87 | m_poEventLoop->ModEvent(this, m_iEvents); 88 | } 89 | } 90 | 91 | void Event :: AddTimer(const int iTimeoutMs, const int iType, uint32_t & iTimerID) 92 | { 93 | m_poEventLoop->AddTimer(this, iTimeoutMs, iType, iTimerID); 94 | } 95 | 96 | void Event :: RemoveTimer(const uint32_t iTimerID) 97 | { 98 | m_poEventLoop->RemoveTimer(iTimerID); 99 | } 100 | 101 | void Event :: Destroy() 102 | { 103 | m_bIsDestroy = true; 104 | } 105 | 106 | const bool Event :: IsDestroy() const 107 | { 108 | return m_bIsDestroy; 109 | } 110 | 111 | } 112 | 113 | 114 | -------------------------------------------------------------------------------- /src/communicate/tcp/event_base.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "commdef.h" 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | class EventLoop; 30 | 31 | class Event 32 | { 33 | public: 34 | Event(EventLoop * poEventLoop); 35 | virtual ~Event(); 36 | 37 | virtual int GetSocketFd() const = 0; 38 | 39 | virtual const std::string & GetSocketHost() = 0; 40 | 41 | virtual int OnRead(); 42 | 43 | virtual int OnWrite(); 44 | 45 | virtual void OnError(bool & bNeedDelete) = 0; 46 | 47 | virtual void OnTimeout(const uint32_t iTimerID, const int iType); 48 | 49 | public: 50 | void AddEvent(const int iEvents); 51 | 52 | void RemoveEvent(const int iEvents); 53 | 54 | void JumpoutEpollWait(); 55 | 56 | const bool IsDestroy() const; 57 | 58 | void Destroy(); 59 | 60 | public: 61 | void AddTimer(const int iTimeoutMs, const int iType, uint32_t & iTimerID); 62 | 63 | void RemoveTimer(const uint32_t iTimerID); 64 | 65 | protected: 66 | int m_iEvents; 67 | EventLoop * m_poEventLoop; 68 | 69 | bool m_bIsDestroy; 70 | }; 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/communicate/tcp/event_loop.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include "timer.h" 27 | #include "notify.h" 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | #define MAX_EVENTS 1024 33 | 34 | class Event; 35 | class TcpAcceptor; 36 | class TcpClient; 37 | class MessageEvent; 38 | class NetWork; 39 | 40 | class EventLoop 41 | { 42 | public: 43 | EventLoop(NetWork * poNetWork); 44 | virtual ~EventLoop(); 45 | 46 | int Init(const int iEpollLength); 47 | 48 | void ModEvent(const Event * poEvent, const int iEvents); 49 | 50 | void RemoveEvent(const Event * poEvent); 51 | 52 | void StartLoop(); 53 | 54 | void Stop(); 55 | 56 | void OnError(const int iEvents, Event * poEvent); 57 | 58 | virtual void OneLoop(const int iTimeoutMs); 59 | 60 | public: 61 | void SetTcpClient(TcpClient * poTcpClient); 62 | 63 | void JumpoutEpollWait(); 64 | 65 | public: 66 | bool AddTimer(const Event * poEvent, const int iTimeout, const int iType, uint32_t & iTimerID); 67 | 68 | void RemoveTimer(const uint32_t iTimerID); 69 | 70 | void DealwithTimeout(int & iNextTimeout); 71 | 72 | void DealwithTimeoutOne(const uint32_t iTimerID, const int iType); 73 | 74 | public: 75 | void AddEvent(int iFD, SocketAddress oAddr); 76 | 77 | void CreateEvent(); 78 | 79 | void ClearEvent(); 80 | 81 | int GetActiveEventCount(); 82 | 83 | public: 84 | typedef struct EventCtx 85 | { 86 | Event * m_poEvent; 87 | int m_iEvents; 88 | } EventCtx_t; 89 | 90 | private: 91 | bool m_bIsEnd; 92 | 93 | protected: 94 | int m_iEpollFd; 95 | epoll_event m_EpollEvents[MAX_EVENTS]; 96 | std::map m_mapEvent; 97 | NetWork * m_poNetWork; 98 | TcpClient * m_poTcpClient; 99 | Notify * m_poNotify; 100 | 101 | protected: 102 | Timer m_oTimer; 103 | std::map m_mapTimerID2FD; 104 | 105 | std::queue > m_oFDQueue; 106 | std::mutex m_oMutex; 107 | std::vector m_vecCreatedEvent; 108 | }; 109 | 110 | } 111 | -------------------------------------------------------------------------------- /src/communicate/tcp/message_event.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "event_base.h" 26 | #include "utils_include.h" 27 | #include "commdef.h" 28 | #include "comm_include.h" 29 | 30 | namespace phxpaxos 31 | { 32 | 33 | class EventLoop; 34 | class NetWork; 35 | 36 | enum MessageEventType 37 | { 38 | MessageEventType_RECV = 1, 39 | MessageEventType_SEND = 2, 40 | }; 41 | 42 | enum MessageEventTimerType 43 | { 44 | MessageEventTimerType_Reconnect = 1, 45 | }; 46 | 47 | class MessageEvent : public Event 48 | { 49 | public: 50 | MessageEvent( 51 | const int Type, 52 | const int fd, 53 | const SocketAddress & oAddr, 54 | EventLoop * poEventLoop, 55 | NetWork * poNetWork); 56 | ~MessageEvent(); 57 | 58 | int AddMessage(const std::string & sMessage); 59 | 60 | int GetSocketFd() const; 61 | 62 | const std::string & GetSocketHost(); 63 | 64 | int OnRead(); 65 | 66 | int OnWrite(); 67 | 68 | void OnTimeout(const uint32_t iTimerID, const int iType); 69 | 70 | void OnError(bool & bNeedDelete); 71 | 72 | void OpenWrite(); 73 | 74 | const bool IsActive(); 75 | 76 | private: 77 | int ReadLeft(); 78 | 79 | void ReadDone(BytesBuffer & oBytesBuffer, const int iLen); 80 | 81 | int WriteLeft(); 82 | 83 | void WriteDone(); 84 | 85 | int DoOnWrite(); 86 | 87 | void ReConnect(); 88 | 89 | private: 90 | Socket m_oSocket; 91 | SocketAddress m_oAddr; 92 | std::string m_sHost; 93 | NetWork * m_poNetWork; 94 | int m_iType; 95 | 96 | private: 97 | char m_sReadHeadBuffer[sizeof(int)]; 98 | int m_iLastReadHeadPos; 99 | BytesBuffer m_oReadCacheBuffer; 100 | int m_iLastReadPos; 101 | int m_iLeftReadLen; 102 | 103 | BytesBuffer m_oWriteCacheBuffer; 104 | int m_iLastWritePos; 105 | int m_iLeftWriteLen; 106 | 107 | struct QueueData 108 | { 109 | uint64_t llEnqueueAbsTime; 110 | std::string * psValue; 111 | }; 112 | 113 | std::queue m_oInQueue; 114 | int m_iQueueMemSize; 115 | std::mutex m_oMutex; 116 | 117 | uint64_t m_llLastActiveTime; 118 | 119 | private: 120 | uint32_t m_iReconnectTimeoutID; 121 | }; 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/communicate/tcp/notify.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "notify.h" 23 | #include "commdef.h" 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | Notify :: Notify(EventLoop * poEventLoop) 33 | : Event(poEventLoop) 34 | { 35 | m_iPipeFD[0] = -1; 36 | m_iPipeFD[1] = -1; 37 | m_sHost = "Notify"; 38 | } 39 | 40 | Notify :: ~Notify() 41 | { 42 | for (int i = 0; i < 2; i++) 43 | { 44 | if (m_iPipeFD[i] != -1) 45 | { 46 | close(m_iPipeFD[i]); 47 | } 48 | } 49 | } 50 | 51 | int Notify :: Init() 52 | { 53 | int ret = pipe(m_iPipeFD); 54 | if (ret != 0) 55 | { 56 | PLErr("create pipe fail, ret %d", ret); 57 | return ret; 58 | } 59 | 60 | fcntl(m_iPipeFD[0], F_SETFL, O_NONBLOCK); 61 | fcntl(m_iPipeFD[1], F_SETFL, O_NONBLOCK); 62 | 63 | AddEvent(EPOLLIN); 64 | return 0; 65 | } 66 | 67 | int Notify :: GetSocketFd() const 68 | { 69 | return m_iPipeFD[0]; 70 | } 71 | 72 | const std::string & Notify :: GetSocketHost() 73 | { 74 | return m_sHost; 75 | } 76 | 77 | void Notify :: SendNotify() 78 | { 79 | ssize_t iWriteLen = write(m_iPipeFD[1], (void *)"a", 1); 80 | if (iWriteLen != 1) 81 | { 82 | //PLErr("notify error, writelen %d", iWriteLen); 83 | } 84 | } 85 | 86 | int Notify :: OnRead() 87 | { 88 | char sTmp[2] = {0}; 89 | int iReadLen = read(m_iPipeFD[0], sTmp, 1); 90 | if (iReadLen < 0) 91 | { 92 | return -1; 93 | } 94 | 95 | return 0; 96 | } 97 | 98 | void Notify :: OnError(bool & bNeedDelete) 99 | { 100 | bNeedDelete = false; 101 | } 102 | 103 | } 104 | 105 | 106 | -------------------------------------------------------------------------------- /src/communicate/tcp/notify.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "event_base.h" 25 | #include 26 | 27 | namespace phxpaxos 28 | { 29 | 30 | class EventLoop; 31 | 32 | class Notify : public Event 33 | { 34 | public: 35 | Notify(EventLoop * poEventLoop); 36 | ~Notify(); 37 | 38 | int Init(); 39 | 40 | int GetSocketFd() const; 41 | 42 | const std::string & GetSocketHost(); 43 | 44 | void SendNotify(); 45 | 46 | int OnRead(); 47 | 48 | void OnError(bool & bNeedDelete); 49 | 50 | private: 51 | int m_iPipeFD[2]; 52 | std::string m_sHost; 53 | }; 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/communicate/tcp/tcp.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "event_loop.h" 25 | #include "tcp_acceptor.h" 26 | #include "tcp_client.h" 27 | #include "utils_include.h" 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | class TcpRead : public Thread 33 | { 34 | public: 35 | TcpRead(NetWork * poNetWork); 36 | ~TcpRead(); 37 | 38 | int Init(); 39 | 40 | void run(); 41 | 42 | void Stop(); 43 | 44 | EventLoop * GetEventLoop(); 45 | 46 | private: 47 | EventLoop m_oEventLoop; 48 | }; 49 | 50 | ///////////////////////////////////////////// 51 | 52 | class TcpWrite : public Thread 53 | { 54 | public: 55 | TcpWrite(NetWork * poNetWork); 56 | ~TcpWrite(); 57 | 58 | int Init(); 59 | 60 | void run(); 61 | 62 | void Stop(); 63 | 64 | int AddMessage(const std::string & sIP, const int iPort, const std::string & sMessage); 65 | 66 | private: 67 | TcpClient m_oTcpClient; 68 | EventLoop m_oEventLoop; 69 | }; 70 | 71 | class TcpIOThread 72 | { 73 | public: 74 | TcpIOThread(NetWork * poNetWork); 75 | ~TcpIOThread(); 76 | 77 | int Init(const std::string & sListenIp, const int iListenPort, const int iIOThreadCount); 78 | 79 | void Start(); 80 | 81 | void Stop(); 82 | 83 | int AddMessage(const int iGroupIdx, const std::string & sIP, const int iPort, const std::string & sMessage); 84 | 85 | private: 86 | NetWork * m_poNetWork; 87 | TcpAcceptor m_oTcpAcceptor; 88 | std::vector m_vecTcpRead; 89 | std::vector m_vecTcpWrite; 90 | bool m_bIsStarted; 91 | }; 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/communicate/tcp/tcp_acceptor.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include "utils_include.h" 27 | #include "message_event.h" 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | class EventLoop; 33 | class NetWork; 34 | 35 | class TcpAcceptor : public Thread 36 | { 37 | public: 38 | TcpAcceptor(); 39 | ~TcpAcceptor(); 40 | 41 | void Listen(const std::string & sListenIP, const int iListenPort); 42 | 43 | void run(); 44 | 45 | void Stop(); 46 | 47 | void AddEventLoop(EventLoop * poEventLoop); 48 | 49 | void AddEvent(int iFD, SocketAddress oAddr); 50 | 51 | private: 52 | ServerSocket m_oSocket; 53 | std::vector m_vecEventLoop; 54 | 55 | private: 56 | bool m_bIsEnd; 57 | bool m_bIsStarted; 58 | }; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/communicate/tcp/tcp_client.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | #include "message_event.h" 28 | #include "utils_include.h" 29 | 30 | namespace phxpaxos 31 | { 32 | 33 | class EventLoop; 34 | class NetWork; 35 | 36 | class TcpClient 37 | { 38 | public: 39 | TcpClient( 40 | EventLoop * poEventLoop, 41 | NetWork * poNetWork); 42 | ~TcpClient(); 43 | 44 | int AddMessage(const std::string & sIP, const int iPort, const std::string & sMessage); 45 | 46 | void DealWithWrite(); 47 | 48 | private: 49 | MessageEvent * GetEvent(const std::string & sIP, const int iPort); 50 | 51 | MessageEvent * CreateEvent(const uint64_t llNodeID, const std::string & sIP, const int iPort); 52 | 53 | private: 54 | EventLoop * m_poEventLoop; 55 | NetWork * m_poNetWork; 56 | 57 | private: 58 | std::map m_mapEvent; 59 | std::vector m_vecEvent; 60 | std::mutex m_oMutex; 61 | 62 | }; 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/communicate/udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "utils_include.h" 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | class DFNetWork; 30 | 31 | class UDPRecv : public Thread 32 | { 33 | public: 34 | UDPRecv(DFNetWork * poDFNetWork); 35 | ~UDPRecv(); 36 | 37 | int Init(const int iPort); 38 | 39 | void run(); 40 | 41 | void Stop(); 42 | 43 | private: 44 | DFNetWork * m_poDFNetWork; 45 | int m_iSockFD; 46 | bool m_bIsEnd; 47 | bool m_bIsStarted; 48 | }; 49 | 50 | class UDPSend : public Thread 51 | { 52 | public: 53 | UDPSend(); 54 | ~UDPSend(); 55 | 56 | int Init(); 57 | 58 | void run(); 59 | 60 | void Stop(); 61 | 62 | int AddMessage(const std::string & sIP, const int iPort, const std::string & sMessage); 63 | 64 | struct QueueData 65 | { 66 | std::string m_sIP; 67 | int m_iPort; 68 | std::string m_sMessage; 69 | }; 70 | 71 | private: 72 | void SendMessage(const std::string & sIP, const int iPort, const std::string & sMessage); 73 | 74 | private: 75 | Queue m_oSendQueue; 76 | int m_iSockFD; 77 | bool m_bIsEnd; 78 | bool m_bIsStarted; 79 | }; 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/config/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libconfig.a 21 | 22 | CONFIG_OBJ=config.o system_v_sm.o 23 | 24 | CONFIG_LIB=config src/comm:comm src/logstorage:logstorage src/sm-base:smbase include:include 25 | 26 | CONFIG_SYS_LIB= 27 | 28 | CONFIG_INCS=$(SRC_BASE_PATH)/src/config 29 | 30 | CONFIG_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/config/config_include.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "./config.h" 25 | #include "./system_v_sm.h" 26 | #include "./inside_sm.h" 27 | -------------------------------------------------------------------------------- /src/config/inside_sm.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/sm.h" 25 | #include 26 | 27 | namespace phxpaxos 28 | { 29 | 30 | class InsideSM : public StateMachine 31 | { 32 | public: 33 | virtual ~InsideSM() {} 34 | 35 | virtual int GetCheckpointBuffer(std::string & sCPBuffer) = 0; 36 | 37 | virtual int UpdateByCheckpoint(const std::string & sCPBuffer, bool & bChange) = 0; 38 | }; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/logstorage/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=liblogstorage.a 21 | 22 | LOGSTORAGE_OBJ=db.o paxos_log.o log_store.o system_variables_store.o 23 | 24 | LOGSTORAGE_LIB=logstorage src/comm:comm include:include 25 | 26 | LOGSTORAGE_SYS_LIB=$(LEVELDB_LIB_PATH)/libleveldb.a 27 | 28 | LOGSTORAGE_INCS=$(SRC_BASE_PATH)/src/logstorage $(LEVELDB_INCLUDE_PATH) 29 | 30 | LOGSTORAGE_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/logstorage/paxos_log.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | #include "phxpaxos/storage.h" 28 | #include "paxos_msg.pb.h" 29 | 30 | namespace phxpaxos 31 | { 32 | 33 | class PaxosLog 34 | { 35 | public: 36 | PaxosLog(const LogStorage * poLogStorage); 37 | ~PaxosLog(); 38 | 39 | int WriteLog(const WriteOptions & oWriteOptions, const int iGroupIdx, const uint64_t llInstanceID, const std::string & sValue); 40 | 41 | int ReadLog(const int iGroupIdx, const uint64_t llInstanceID, std::string & sValue); 42 | 43 | int GetMaxInstanceIDFromLog(const int iGroupIdx, uint64_t & llInstanceID); 44 | 45 | int WriteState(const WriteOptions & oWriteOptions, const int iGroupIdx, const uint64_t llInstanceID, const AcceptorStateData & oState); 46 | 47 | int ReadState(const int iGroupIdx, const uint64_t llInstanceID, AcceptorStateData & oState); 48 | 49 | private: 50 | LogStorage * m_poLogStorage; 51 | }; 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/logstorage/system_variables_store.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "system_variables_store.h" 23 | #include "db.h" 24 | 25 | namespace phxpaxos 26 | { 27 | 28 | SystemVariablesStore :: SystemVariablesStore(const LogStorage * poLogStorage) : m_poLogStorage((LogStorage *)poLogStorage) 29 | { 30 | } 31 | 32 | SystemVariablesStore :: ~SystemVariablesStore() 33 | { 34 | } 35 | 36 | int SystemVariablesStore :: Write(const WriteOptions & oWriteOptions, const int iGroupIdx, const SystemVariables & oVariables) 37 | { 38 | const int m_iMyGroupIdx = iGroupIdx; 39 | 40 | string sBuffer; 41 | bool sSucc = oVariables.SerializeToString(&sBuffer); 42 | if (!sSucc) 43 | { 44 | PLG1Err("Variables.Serialize fail"); 45 | return -1; 46 | } 47 | 48 | int ret = m_poLogStorage->SetSystemVariables(oWriteOptions, iGroupIdx, sBuffer); 49 | if (ret != 0) 50 | { 51 | PLG1Err("DB.Put fail, groupidx %d bufferlen %zu ret %d", 52 | iGroupIdx, sBuffer.size(), ret); 53 | return ret; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | int SystemVariablesStore :: Read(const int iGroupIdx, SystemVariables & oVariables) 60 | { 61 | const int m_iMyGroupIdx = iGroupIdx; 62 | 63 | string sBuffer; 64 | int ret = m_poLogStorage->GetSystemVariables(iGroupIdx, sBuffer); 65 | if (ret != 0 && ret != 1) 66 | { 67 | PLG1Err("DB.Get fail, groupidx %d ret %d", iGroupIdx, ret); 68 | return ret; 69 | } 70 | else if (ret == 1) 71 | { 72 | PLG1Imp("DB.Get not found, groupidx %d", iGroupIdx); 73 | return 1; 74 | } 75 | 76 | bool bSucc = oVariables.ParseFromArray(sBuffer.data(), sBuffer.size()); 77 | if (!bSucc) 78 | { 79 | PLG1Err("Variables.ParseFromArray fail, bufferlen %zu", sBuffer.size()); 80 | return -1; 81 | } 82 | 83 | return 0; 84 | } 85 | 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /src/logstorage/system_variables_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | #include "phxpaxos/storage.h" 28 | #include "paxos_msg.pb.h" 29 | 30 | namespace phxpaxos 31 | { 32 | 33 | class SystemVariablesStore 34 | { 35 | public: 36 | SystemVariablesStore(const LogStorage * poLogStorage); 37 | ~SystemVariablesStore(); 38 | 39 | int Write(const WriteOptions & oWriteOptions, const int iGroupIdx, const SystemVariables & oVariables); 40 | 41 | int Read(const int iGroupIdx, SystemVariables & oVariables); 42 | 43 | private: 44 | LogStorage * m_poLogStorage; 45 | }; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/master/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libmaster.a 21 | 22 | MASTER_OBJ=master_sm.pb.o master_sm.o master_mgr.o master_variables_store.o 23 | 24 | MASTER_LIB=master src/utils:utils src/comm:comm src/config:config src/logstorage:logstorage 25 | 26 | MASTER_SYS_LIB= 27 | 28 | MASTER_INCS=$(SRC_BASE_PATH)/src/master 29 | 30 | MASTER_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/master/master_mgr.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "commdef.h" 25 | #include "utils_include.h" 26 | #include "phxpaxos/node.h" 27 | #include "master_sm.h" 28 | 29 | namespace phxpaxos 30 | { 31 | 32 | class MasterMgr : public Thread 33 | { 34 | public: 35 | MasterMgr(const Node * poPaxosNode, 36 | const int iGroupIdx, 37 | const LogStorage * poLogStorage, 38 | MasterChangeCallback pMasterChangeCallback); 39 | ~MasterMgr(); 40 | 41 | void RunMaster(); 42 | 43 | void StopMaster(); 44 | 45 | int Init(); 46 | 47 | void run(); 48 | 49 | void SetLeaseTime(const int iLeaseTimeMs); 50 | 51 | void TryBeMaster(const int iLeaseTime); 52 | 53 | void DropMaster(); 54 | 55 | public: 56 | MasterStateMachine * GetMasterSM(); 57 | 58 | private: 59 | Node * m_poPaxosNode; 60 | 61 | MasterStateMachine m_oDefaultMasterSM; 62 | 63 | private: 64 | int m_iLeaseTime; 65 | 66 | bool m_bIsEnd; 67 | bool m_bIsStarted; 68 | 69 | int m_iMyGroupIdx; 70 | 71 | bool m_bNeedDropMaster; 72 | }; 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/master/master_sm.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | package phxpaxos; 3 | 4 | message MasterOperator 5 | { 6 | required uint64 nodeid = 1; 7 | required uint64 version = 2; 8 | required int32 timeout = 3; 9 | required uint32 operator = 4; 10 | required uint32 sid = 5; 11 | optional uint64 lastversion = 6; 12 | }; 13 | -------------------------------------------------------------------------------- /src/master/master_variables_store.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "master_variables_store.h" 23 | #include "db.h" 24 | 25 | namespace phxpaxos 26 | { 27 | 28 | MasterVariablesStore :: MasterVariablesStore(const LogStorage * poLogStorage) : m_poLogStorage((LogStorage *)poLogStorage) 29 | { 30 | } 31 | 32 | MasterVariablesStore :: ~MasterVariablesStore() 33 | { 34 | } 35 | 36 | int MasterVariablesStore :: Write(const WriteOptions & oWriteOptions, const int iGroupIdx, const MasterVariables & oVariables) 37 | { 38 | const int m_iMyGroupIdx = iGroupIdx; 39 | 40 | string sBuffer; 41 | bool sSucc = oVariables.SerializeToString(&sBuffer); 42 | if (!sSucc) 43 | { 44 | PLG1Err("Variables.Serialize fail"); 45 | return -1; 46 | } 47 | 48 | int ret = m_poLogStorage->SetMasterVariables(oWriteOptions, iGroupIdx, sBuffer); 49 | if (ret != 0) 50 | { 51 | PLG1Err("DB.Put fail, groupidx %d bufferlen %zu ret %d", 52 | iGroupIdx, sBuffer.size(), ret); 53 | return ret; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | int MasterVariablesStore :: Read(const int iGroupIdx, MasterVariables & oVariables) 60 | { 61 | const int m_iMyGroupIdx = iGroupIdx; 62 | 63 | string sBuffer; 64 | int ret = m_poLogStorage->GetMasterVariables(iGroupIdx, sBuffer); 65 | if (ret != 0 && ret != 1) 66 | { 67 | PLG1Err("DB.Get fail, groupidx %d ret %d", iGroupIdx, ret); 68 | return ret; 69 | } 70 | else if (ret == 1) 71 | { 72 | PLG1Imp("DB.Get not found, groupidx %d", iGroupIdx); 73 | return 1; 74 | } 75 | 76 | bool bSucc = oVariables.ParseFromArray(sBuffer.data(), sBuffer.size()); 77 | if (!bSucc) 78 | { 79 | PLG1Err("Variables.ParseFromArray fail, bufferlen %zu", sBuffer.size()); 80 | return -1; 81 | } 82 | 83 | return 0; 84 | } 85 | 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /src/master/master_variables_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | #include "phxpaxos/storage.h" 28 | #include "paxos_msg.pb.h" 29 | 30 | namespace phxpaxos 31 | { 32 | 33 | class MasterVariablesStore 34 | { 35 | public: 36 | MasterVariablesStore(const LogStorage * poLogStorage); 37 | ~MasterVariablesStore(); 38 | 39 | int Write(const WriteOptions & oWriteOptions, const int iGroupIdx, const MasterVariables & oVariables); 40 | 41 | int Read(const int iGroupIdx, MasterVariables & oVariables); 42 | 43 | private: 44 | LogStorage * m_poLogStorage; 45 | }; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/node/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libnode.a test_propose_batch 21 | 22 | NODE_OBJ=group.o pnode.o node.o propose_batch.o 23 | 24 | NODE_LIB=node src/comm:comm src/logstorage:logstorage src/communicate:communicate include:include src/algorithm:algorithm src/config:config src/master:master 25 | 26 | NODE_SYS_LIB= 27 | 28 | NODE_INCS=$(SRC_BASE_PATH)/src/node 29 | 30 | NODE_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | TEST_PROPOSE_BATCH_OBJ=test_propose_batch.o 33 | 34 | TEST_PROPOSE_BATCH_LIB=src/node:node 35 | 36 | TEST_PROPOSE_BATCH_SYS_LIB= 37 | 38 | TEST_PROPOSE_BATCH_INCS=$(SRC_BASE_PATH)/src/node 39 | 40 | TEST_PROPOSE_BATCH_EXTRA_CPPFLAGS=-Wall -Werror 41 | 42 | -------------------------------------------------------------------------------- /src/node/group.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "group.h" 23 | 24 | namespace phxpaxos 25 | { 26 | 27 | 28 | Group :: Group(LogStorage * poLogStorage, 29 | NetWork * poNetWork, 30 | InsideSM * poMasterSM, 31 | const int iGroupIdx, 32 | const Options & oOptions) : 33 | m_oCommunicate(&m_oConfig, oOptions.oMyNode.GetNodeID(), oOptions.iUDPMaxSize, poNetWork), 34 | m_oConfig(poLogStorage, oOptions.bSync, oOptions.iSyncInterval, oOptions.bUseMembership, 35 | oOptions.oMyNode, oOptions.vecNodeInfoList, oOptions.vecFollowerNodeInfoList, 36 | iGroupIdx, oOptions.iGroupCount, oOptions.pMembershipChangeCallback), 37 | m_oInstance(&m_oConfig, poLogStorage, &m_oCommunicate, oOptions), 38 | m_iInitRet(-1), m_poThread(nullptr) 39 | { 40 | m_oConfig.SetMasterSM(poMasterSM); 41 | } 42 | 43 | Group :: ~Group() 44 | { 45 | } 46 | 47 | void Group :: StartInit() 48 | { 49 | m_poThread = new std::thread(&Group::Init, this); 50 | assert(m_poThread != nullptr); 51 | } 52 | 53 | void Group :: Init() 54 | { 55 | m_iInitRet = m_oConfig.Init(); 56 | if (m_iInitRet != 0) 57 | { 58 | return; 59 | } 60 | 61 | //inside sm 62 | AddStateMachine(m_oConfig.GetSystemVSM()); 63 | AddStateMachine(m_oConfig.GetMasterSM()); 64 | 65 | m_iInitRet = m_oInstance.Init(); 66 | } 67 | 68 | int Group :: GetInitRet() 69 | { 70 | m_poThread->join(); 71 | delete m_poThread; 72 | 73 | return m_iInitRet; 74 | } 75 | 76 | void Group :: Start() 77 | { 78 | m_oInstance.Start(); 79 | } 80 | 81 | void Group :: Stop() 82 | { 83 | m_oInstance.Stop(); 84 | } 85 | 86 | Config * Group :: GetConfig() 87 | { 88 | return &m_oConfig; 89 | } 90 | 91 | Instance * Group :: GetInstance() 92 | { 93 | return &m_oInstance; 94 | } 95 | 96 | Committer * Group :: GetCommitter() 97 | { 98 | return m_oInstance.GetCommitter(); 99 | } 100 | 101 | Cleaner * Group :: GetCheckpointCleaner() 102 | { 103 | return m_oInstance.GetCheckpointCleaner(); 104 | } 105 | 106 | Replayer * Group :: GetCheckpointReplayer() 107 | { 108 | return m_oInstance.GetCheckpointReplayer(); 109 | } 110 | 111 | void Group :: AddStateMachine(StateMachine * poSM) 112 | { 113 | m_oInstance.AddStateMachine(poSM); 114 | } 115 | 116 | } 117 | 118 | 119 | -------------------------------------------------------------------------------- /src/node/group.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "comm_include.h" 26 | #include "config_include.h" 27 | #include "instance.h" 28 | #include "cleaner.h" 29 | #include "communicate.h" 30 | #include "phxpaxos/options.h" 31 | #include "phxpaxos/network.h" 32 | 33 | namespace phxpaxos 34 | { 35 | 36 | class Group 37 | { 38 | public: 39 | Group(LogStorage * poLogStorage, 40 | NetWork * poNetWork, 41 | InsideSM * poMasterSM, 42 | const int iGroupIdx, 43 | const Options & oOptions); 44 | 45 | ~Group(); 46 | 47 | void StartInit(); 48 | 49 | void Init(); 50 | 51 | int GetInitRet(); 52 | 53 | void Start(); 54 | 55 | void Stop(); 56 | 57 | Config * GetConfig(); 58 | 59 | Instance * GetInstance(); 60 | 61 | Committer * GetCommitter(); 62 | 63 | Cleaner * GetCheckpointCleaner(); 64 | 65 | Replayer * GetCheckpointReplayer(); 66 | 67 | void AddStateMachine(StateMachine * poSM); 68 | 69 | private: 70 | Communicate m_oCommunicate; 71 | Config m_oConfig; 72 | Instance m_oInstance; 73 | 74 | int m_iInitRet; 75 | std::thread * m_poThread; 76 | }; 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/node/node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "phxpaxos/node.h" 23 | #include "pnode.h" 24 | 25 | namespace phxpaxos 26 | { 27 | 28 | int Node :: RunNode(const Options & oOptions, Node *& poNode) 29 | { 30 | if (oOptions.bIsLargeValueMode) 31 | { 32 | InsideOptions::Instance()->SetAsLargeBufferMode(); 33 | } 34 | 35 | InsideOptions::Instance()->SetGroupCount(oOptions.iGroupCount); 36 | 37 | poNode = nullptr; 38 | NetWork * poNetWork = nullptr; 39 | 40 | Breakpoint::m_poBreakpoint = nullptr; 41 | BP->SetInstance(oOptions.poBreakpoint); 42 | 43 | PNode * poRealNode = new PNode(); 44 | int ret = poRealNode->Init(oOptions, poNetWork); 45 | if (ret != 0) 46 | { 47 | delete poRealNode; 48 | return ret; 49 | } 50 | 51 | //step1 set node to network 52 | //very important, let network on recieve callback can work. 53 | poNetWork->m_poNode = poRealNode; 54 | 55 | //step2 run network. 56 | //start recieve message from network, so all must init before this step. 57 | //must be the last step. 58 | poNetWork->RunNetWork(); 59 | 60 | 61 | poNode = poRealNode; 62 | 63 | return 0; 64 | } 65 | 66 | } 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/node/propose_batch.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "commdef.h" 25 | #include "utils_include.h" 26 | #include "phxpaxos/node.h" 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | namespace phxpaxos 33 | { 34 | 35 | class PendingProposal 36 | { 37 | public: 38 | PendingProposal(); 39 | const std::string * psValue; 40 | SMCtx * poSMCtx; 41 | 42 | //return parameter 43 | uint64_t * pllInstanceID; 44 | uint32_t * piBatchIndex; 45 | 46 | //notify 47 | Notifier * poNotifier; 48 | 49 | uint64_t llAbsEnqueueTime; 50 | }; 51 | 52 | /////////////////////////////////// 53 | 54 | class ProposeBatch 55 | { 56 | public: 57 | ProposeBatch(const int iGroupIdx, Node * poPaxosNode, NotifierPool * poNotifierPool); 58 | virtual ~ProposeBatch(); 59 | 60 | void Start(); 61 | 62 | void Run(); 63 | 64 | void Stop(); 65 | 66 | int Propose(const std::string & sValue, uint64_t & llInstanceID, uint32_t & iBatchIndex, SMCtx * poSMCtx); 67 | 68 | public: 69 | void SetBatchCount(const int iBatchCount); 70 | void SetBatchDelayTimeMs(const int iBatchDelayTimeMs); 71 | 72 | protected: 73 | virtual void DoPropose(std::vector & vecRequest); 74 | 75 | private: 76 | void AddProposal(const std::string & sValue, uint64_t & llInstanceID, uint32_t & iBatchIndex, 77 | SMCtx * poSMCtx, Notifier * poNotifier); 78 | void PluckProposal(std::vector & vecRequest); 79 | void OnlyOnePropose(PendingProposal & oPendingProposal); 80 | const bool NeedBatch(); 81 | 82 | private: 83 | const int m_iMyGroupIdx; 84 | Node * m_poPaxosNode; 85 | NotifierPool * m_poNotifierPool; 86 | 87 | std::mutex m_oMutex; 88 | std::condition_variable m_oCond; 89 | std::queue m_oQueue; 90 | bool m_bIsEnd; 91 | bool m_bIsStarted; 92 | int m_iNowQueueValueSize; 93 | 94 | private: 95 | int m_iBatchCount; 96 | int m_iBatchDelayTimeMs; 97 | int m_iBatchMaxSize; 98 | 99 | std::thread * m_poThread; 100 | }; 101 | 102 | } 103 | -------------------------------------------------------------------------------- /src/sm-base/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libsmbase.a 21 | 22 | SMBASE_OBJ=sm_base.o sm.o 23 | 24 | SMBASE_LIB=smbase src/comm:comm include:include 25 | 26 | SMBASE_SYS_LIB= 27 | 28 | SMBASE_INCS=$(SRC_BASE_PATH)/src/sm-base 29 | 30 | SMBASE_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/sm-base/sm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "phxpaxos/sm.h" 23 | #include "comm_include.h" 24 | 25 | namespace phxpaxos 26 | { 27 | 28 | SMCtx :: SMCtx(const int iSMID, void * pCtx) : m_iSMID(iSMID), m_pCtx(pCtx) 29 | { 30 | } 31 | 32 | SMCtx :: SMCtx() : m_iSMID(0), m_pCtx(nullptr) 33 | { 34 | } 35 | 36 | bool StateMachine :: ExecuteForCheckpoint(const int iGroupIdx, const uint64_t llInstanceID, 37 | const std::string & sPaxosValue) 38 | { 39 | return true; 40 | } 41 | 42 | const uint64_t StateMachine :: GetCheckpointInstanceID(const int iGroupIdx) const 43 | { 44 | return phxpaxos::NoCheckpoint; 45 | } 46 | 47 | //default no checkpoint 48 | int StateMachine :: GetCheckpointState(const int iGroupIdx, std::string & sDirPath, 49 | std::vector & vecFileList) 50 | { 51 | PLErr("func not impl, return -1"); 52 | return -1; 53 | } 54 | 55 | int StateMachine :: LoadCheckpointState(const int iGroupIdx, const std::string & sCheckpointTmpFileDirPath, 56 | const std::vector & vecFileList, const uint64_t llCheckpointInstanceID) 57 | { 58 | PLErr("func not impl, return -1"); 59 | return -1; 60 | } 61 | 62 | int StateMachine :: LockCheckpointState() 63 | { 64 | PLErr("func not impl, return -1"); 65 | return -1; 66 | } 67 | 68 | void StateMachine :: UnLockCheckpointState() 69 | { 70 | } 71 | 72 | void StateMachine :: BeforePropose(const int iGroupIdx, std::string & sValue) 73 | { 74 | } 75 | 76 | const bool StateMachine :: NeedCallBeforePropose() 77 | { 78 | return false; 79 | } 80 | 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /src/sm-base/sm_base.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "commdef.h" 25 | #include 26 | #include "phxpaxos/sm.h" 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | class BatchSMCtx 32 | { 33 | public: 34 | std::vector m_vecSMCtxList; 35 | }; 36 | 37 | class SMFac 38 | { 39 | public: 40 | SMFac(const int iMyGroupIdx); 41 | ~SMFac(); 42 | 43 | bool Execute(const int iGroupIdx, const uint64_t llInstanceID, 44 | const std::string & sPaxosValue, SMCtx * poSMCtx); 45 | 46 | bool ExecuteForCheckpoint(const int iGroupIdx, const uint64_t llInstanceID, const std::string & sPaxosValue); 47 | 48 | void PackPaxosValue(std::string & sPaxosValue, const int iSMID = 0); 49 | 50 | void AddSM(StateMachine * poSM); 51 | 52 | public: 53 | void BeforePropose(const int iGroupIdx, std::string & sValue); 54 | 55 | void BeforeBatchPropose(const int iGroupIdx, std::string & sValue); 56 | 57 | void BeforeProposeCall(const int iGroupIdx, const int iSMID, std::string & sValue, bool & change); 58 | 59 | public: 60 | const uint64_t GetCheckpointInstanceID(const int iGroupIdx) const; 61 | 62 | std::vector GetSMList(); 63 | 64 | private: 65 | bool BatchExecute(const int iGroupIdx, const uint64_t llInstanceID, 66 | const std::string & sBodyValue, BatchSMCtx * poBatchSMCtx); 67 | 68 | bool DoExecute(const int iGroupIdx, const uint64_t llInstanceID, 69 | const std::string & sBodyValue, const int iSMID, SMCtx * poSMCtx); 70 | 71 | bool BatchExecuteForCheckpoint(const int iGroupIdx, const uint64_t llInstanceID, 72 | const std::string & sBodyValue); 73 | 74 | bool DoExecuteForCheckpoint(const int iGroupIdx, const uint64_t llInstanceID, 75 | const std::string & sBodyValue, const int iSMID); 76 | 77 | private: 78 | std::vector m_vecSMList; 79 | int m_iMyGroupIdx; 80 | }; 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/test/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=phx_paxos_test 21 | 22 | PHX_PAXOS_TEST_OBJ=test_sm.o test_server.o test_main.o 23 | 24 | PHX_PAXOS_TEST_LIB=src/utils:utils src/comm:comm 25 | 26 | PHX_PAXOS_TEST_SYS_LIB=$(PHXPAXOS_LIB_PATH)/libphxpaxos.a $(LEVELDB_LIB_PATH)/libleveldb.a $(PROTOBUF_LIB_PATH)/libprotobuf.a -lpthread 27 | 28 | PHX_PAXOS_TEST_INCS=$(SRC_BASE_PATH)/src/test $(PHXPAXOS_INCLUDE_PATH) $(LEVELDB_INCLUDE_PATH) $(PROTOBUF_INCLUDE_PATH) 29 | 30 | PHX_PAXOS_TEST_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/test/test_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/node.h" 25 | #include "test_sm.h" 26 | #include 27 | #include 28 | 29 | namespace phxpaxos_test 30 | { 31 | 32 | class TestServer 33 | { 34 | public: 35 | TestServer(const phxpaxos::NodeInfo & oMyNode, const phxpaxos::NodeInfoList & vecNodeList); 36 | ~TestServer(); 37 | 38 | int RunPaxos(); 39 | 40 | int Write(const std::string & sTestValue, uint64_t & llInstanceID); 41 | 42 | int BatchWrite(const std::string & sTestValue, uint64_t & llInstanceID, uint32_t & iBatchIndex); 43 | 44 | int Ready(); 45 | 46 | TestSM * GetSM(); 47 | 48 | private: 49 | int MakeLogStoragePath(std::string & sLogStoragePath); 50 | 51 | private: 52 | phxpaxos::NodeInfo m_oMyNode; 53 | phxpaxos::NodeInfoList m_vecNodeList; 54 | 55 | TestSM m_oTestSM; 56 | 57 | phxpaxos::Node * m_poPaxosNode; 58 | }; 59 | 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /src/test/test_sm.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "phxpaxos/sm.h" 25 | #include "phxpaxos/options.h" 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | namespace phxpaxos_test 32 | { 33 | 34 | class TestSM : public phxpaxos::StateMachine 35 | { 36 | public: 37 | TestSM(); 38 | 39 | bool Execute(const int iGroupIdx, const uint64_t llInstanceID, 40 | const std::string & sPaxosValue, phxpaxos::SMCtx * poSMCtx); 41 | 42 | const int SMID() const { return 1; } 43 | 44 | bool CheckExecuteValueCorrect(const std::vector > & vecOtherExecuted); 45 | 46 | void BeforePropose(const int iGroupIdx, std::string & sValue); 47 | 48 | const bool NeedCallBeforePropose(); 49 | 50 | public: 51 | static std::string PackTestValue(const std::string & sValue); 52 | 53 | static void PackTestValueWithChecksum(std::string & sValue, const uint32_t iLastChecksum); 54 | 55 | static void UnPackTestValue(const std::string & sValue, std::string & sBodyValue, uint32_t & iLastChecksum); 56 | 57 | public: 58 | std::vector > m_vecExecuted; 59 | 60 | private: 61 | uint32_t m_iLastValueChecksum; 62 | }; 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/tools/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=system_variables_tools paxos_log_tools vfile_fetch 21 | 22 | SYSTEM_VARIABLES_TOOLS_OBJ=system_variables_tools.o 23 | 24 | SYSTEM_VARIABLES_TOOLS_LIB=src/comm:comm src/config:config src/logstorage:logstorage 25 | 26 | SYSTEM_VARIABLES_TOOLS_SYS_LIB= 27 | 28 | SYSTEM_VARIABLES_TOOLS_INCS=$(SRC_BASE_PATH)/src/tools 29 | 30 | SYSTEM_VARIABLES_TOOLS_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | PAXOS_LOG_TOOLS_OBJ=paxos_log_tools.o 33 | 34 | PAXOS_LOG_TOOLS_LIB=src/comm:comm src/logstorage:logstorage 35 | 36 | PAXOS_LOG_TOOLS_SYS_LIB= 37 | 38 | PAXOS_LOG_TOOLS_INCS=$(SRC_BASE_PATH)/src/tools 39 | 40 | PAXOS_LOG_TOOLS_EXTRA_CPPFLAGS=-Wall -Werror 41 | 42 | VFILE_FETCH_OBJ=vfile_fetch.o 43 | 44 | VFILE_FETCH_LIB=src/comm:comm src/logstorage:logstorage 45 | 46 | VFILE_FETCH_SYS_LIB= 47 | 48 | VFILE_FETCH_INCS=$(SRC_BASE_PATH)/src/tools 49 | 50 | VFILE_FETCH_EXTRA_CPPFLAGS=-Wall -Werror 51 | 52 | -------------------------------------------------------------------------------- /src/ut/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=phxpaxos_ut 21 | 22 | PHXPAXOS_UT_OBJ=ut_main.o db_ut.o nodeid_ut.o timer_ut.o wait_lock_ut.o make_class.o acceptor_ut.o proposer_ut.o 23 | 24 | PHXPAXOS_UT_LIB=src/logstorage:logstorage src/config:config src/algorithm:algorithm src/communicate:communicate 25 | 26 | PHXPAXOS_UT_SYS_LIB=$(SRC_BASE_PATH)/third_party/gmock/lib/libgmock.a $(SRC_BASE_PATH)/third_party/gmock/lib/libgmock_main.a $(SRC_BASE_PATH)/third_party/gtest/lib/libgtest.a 27 | 28 | PHXPAXOS_UT_INCS=$(SRC_BASE_PATH)/src/ut $(SRC_BASE_PATH)/third_party/gmock/include $(SRC_BASE_PATH)/third_party/gtest/include 29 | 30 | PHXPAXOS_UT_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | -------------------------------------------------------------------------------- /src/ut/make_class.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "config_include.h" 25 | #include "instance.h" 26 | #include "communicate.h" 27 | #include "acceptor.h" 28 | #include "proposer.h" 29 | #include "mock_class.h" 30 | 31 | namespace phxpaxos 32 | { 33 | 34 | NodeInfo GetMyNode(); 35 | 36 | void MakeConfig(MockLogStorage * poMockLogStorage, Config *& oConfig); 37 | 38 | void MakeCommunicate(MockNetWork * poMockNetWork, Config * poConfig, Communicate *& poCommunicate); 39 | 40 | void MakeInstance(MockLogStorage * poMockLogStorage, Config * poConfig, Communicate * poCommunicate, Instance *& poInstance); 41 | 42 | void MakeAcceptor(MockLogStorage * poMockLogStorage, Config * poConfig, Communicate * poCommunicate, Instance * poInstance, Acceptor *& poAcceptor); 43 | 44 | void MakeProposer(Config * poConfig, Communicate * poCommunicate, Instance * poInstance, Learner * poLearner, IOLoop * poIOLoop, Proposer *& poProposer); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/ut/nodeid_ut.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include 23 | #include "phxpaxos/options.h" 24 | #include "gmock/gmock.h" 25 | 26 | using namespace phxpaxos; 27 | using namespace std; 28 | using ::testing::_; 29 | using ::testing::Return; 30 | 31 | TEST(NodeID, IPPort2NodeID) 32 | { 33 | string sIP = "10.123.102.77"; 34 | int iPort = 8001; 35 | 36 | uint64_t llNodeID = 5577280471424835393lu; 37 | 38 | NodeInfo oNode(sIP, iPort); 39 | 40 | EXPECT_TRUE(llNodeID == oNode.GetNodeID()); 41 | } 42 | 43 | TEST(NodeID, NodeID2IPPort) 44 | { 45 | string sIP = "10.123.102.77"; 46 | int iPort = 8001; 47 | 48 | uint64_t llNodeID = 5577280471424835393lu; 49 | 50 | NodeInfo oNode(llNodeID); 51 | 52 | EXPECT_TRUE(oNode.GetIP() == sIP); 53 | EXPECT_TRUE(oNode.GetPort() == iPort); 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /src/ut/timer_ut.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include 23 | #include "comm_include.h" 24 | #include 25 | #include 26 | #include "gmock/gmock.h" 27 | 28 | using namespace phxpaxos; 29 | using namespace std; 30 | using ::testing::_; 31 | using ::testing::Return; 32 | 33 | TEST(Timer, AddTimer) 34 | { 35 | Timer oTimer; 36 | 37 | uint64_t llAbsTime = Time::GetSteadyClockMS() + 30; 38 | uint32_t iTimerID = 0; 39 | int iType = 321; 40 | oTimer.AddTimerWithType(llAbsTime, iType, iTimerID); 41 | 42 | //wait 30ms, and popout; 43 | Time::MsSleep(35); 44 | 45 | uint32_t iTakeTimerID = 0; 46 | int iTakeType = 0; 47 | bool bHasTimeout = oTimer.PopTimeout(iTakeTimerID, iTakeType); 48 | 49 | EXPECT_TRUE(bHasTimeout == true); 50 | EXPECT_TRUE(iTakeTimerID == iTimerID); 51 | EXPECT_TRUE(iTakeType == iType); 52 | } 53 | 54 | int PopTimeout(Timer & oTimer, std::map & mapTimerIDtoAbsTime, bool & bPass) 55 | { 56 | bool bHasTimeout = true; 57 | int iNextTimeout = 0; 58 | 59 | while(bHasTimeout) 60 | { 61 | uint32_t iTimerID = 0; 62 | int iType = 0; 63 | bHasTimeout = oTimer.PopTimeout(iTimerID, iType); 64 | 65 | if (bHasTimeout) 66 | { 67 | uint64_t llCut = mapTimerIDtoAbsTime[iTimerID] < Time::GetSteadyClockMS() ? 68 | Time::GetSteadyClockMS() - mapTimerIDtoAbsTime[iTimerID] 69 | : mapTimerIDtoAbsTime[iTimerID] - Time::GetSteadyClockMS(); 70 | 71 | if (llCut > 10) 72 | { 73 | bPass = false; 74 | } 75 | 76 | iNextTimeout = oTimer.GetNextTimeout(); 77 | 78 | if (iNextTimeout != 0) 79 | { 80 | break; 81 | } 82 | } 83 | 84 | } 85 | 86 | return iNextTimeout; 87 | } 88 | 89 | TEST(Timer, PopTimer) 90 | { 91 | Timer oTimer; 92 | 93 | std::map mapTimerIDtoAbsTime; 94 | 95 | srand((unsigned int)time(NULL)); 96 | 97 | int iTimerObjCount = 10; 98 | for (int i = 0; i < iTimerObjCount; i++) 99 | { 100 | uint64_t llAbsTime = Time::GetSteadyClockMS() + (OtherUtils::FastRand() % 500); 101 | uint32_t iTimerID = 0; 102 | 103 | oTimer.AddTimer(llAbsTime, iTimerID); 104 | mapTimerIDtoAbsTime[iTimerID] = llAbsTime; 105 | } 106 | 107 | bool bPass = true; 108 | int iNextTimeout = 0; 109 | while(iNextTimeout != -1) 110 | { 111 | iNextTimeout = PopTimeout(oTimer, mapTimerIDtoAbsTime, bPass); 112 | Time::MsSleep(iNextTimeout); 113 | } 114 | 115 | EXPECT_TRUE(bPass == true); 116 | } 117 | 118 | 119 | -------------------------------------------------------------------------------- /src/ut/ut_main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "gmock/gmock.h" 23 | 24 | int main(int argc, char **argv) 25 | { 26 | testing::InitGoogleMock(&argc, argv); 27 | return RUN_ALL_TESTS(); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/utils/Makefile.define: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making 2 | # PhxPaxos available. 3 | # Copyright (C) 2016 THL A29 Limited, a Tencent company. 4 | # All rights reserved. 5 | # 6 | # Licensed under the BSD 3-Clause License (the "License"); you may 7 | # not use this file except in compliance with the License. You may 8 | # obtain a copy of the License at 9 | # 10 | # https://opensource.org/licenses/BSD-3-Clause 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" basis, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. See the License for the specific language governing 16 | # permissions and limitations under the License. 17 | # 18 | # See the AUTHORS file for names of contributors. 19 | 20 | allobject=libutils.a test_notifier_pool 21 | 22 | UTILS_OBJ=concurrent.o socket.o util.o crc32.o timer.o bytes_buffer.o serial_lock.o wait_lock.o notifier_pool.o 23 | 24 | UTILS_LIB=utils 25 | 26 | UTILS_SYS_LIB=-lpthread 27 | 28 | UTILS_INCS=$(SRC_BASE_PATH)/src/utils 29 | 30 | UTILS_EXTRA_CPPFLAGS=-Wall -Werror 31 | 32 | TEST_NOTIFIER_POOL_OBJ=test_notifier_pool.o 33 | 34 | TEST_NOTIFIER_POOL_LIB=src/utils:utils 35 | 36 | TEST_NOTIFIER_POOL_SYS_LIB= 37 | 38 | TEST_NOTIFIER_POOL_INCS=$(SRC_BASE_PATH)/src/utils 39 | 40 | TEST_NOTIFIER_POOL_EXTRA_CPPFLAGS=-Wall -Werror 41 | 42 | -------------------------------------------------------------------------------- /src/utils/bytes_buffer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "bytes_buffer.h" 23 | #include 24 | #include 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | #define DEFAULT_BUFFER_LEN 1048576 30 | 31 | BytesBuffer :: BytesBuffer() 32 | : m_pcBuffer(nullptr), m_iLen(DEFAULT_BUFFER_LEN) 33 | { 34 | m_pcBuffer = new char[m_iLen]; 35 | assert(m_pcBuffer != nullptr); 36 | } 37 | 38 | BytesBuffer :: ~BytesBuffer() 39 | { 40 | delete []m_pcBuffer; 41 | } 42 | 43 | char * BytesBuffer :: GetPtr() 44 | { 45 | return m_pcBuffer; 46 | } 47 | 48 | int BytesBuffer :: GetLen() 49 | { 50 | return m_iLen; 51 | } 52 | 53 | void BytesBuffer :: Ready(const int iBufferLen) 54 | { 55 | if (m_iLen < iBufferLen) 56 | { 57 | delete []m_pcBuffer; 58 | m_pcBuffer = nullptr; 59 | 60 | while (m_iLen < iBufferLen) 61 | { 62 | m_iLen *= 2; 63 | } 64 | 65 | m_pcBuffer = new char[m_iLen]; 66 | assert(m_pcBuffer != nullptr); 67 | } 68 | } 69 | 70 | } 71 | 72 | 73 | -------------------------------------------------------------------------------- /src/utils/bytes_buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | namespace phxpaxos 25 | { 26 | 27 | class BytesBuffer 28 | { 29 | public: 30 | BytesBuffer(); 31 | ~BytesBuffer(); 32 | 33 | char * GetPtr(); 34 | 35 | int GetLen(); 36 | 37 | void Ready(const int iBufferLen); 38 | 39 | private: 40 | char * m_pcBuffer; 41 | int m_iLen; 42 | }; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/utils/concurrent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "concurrent.h" 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | static void* mmThreadRun(void* p) { 30 | phxpaxos::Thread* thread = (phxpaxos::Thread*)p; 31 | thread->run(); 32 | return 0; 33 | } 34 | 35 | namespace phxpaxos { 36 | 37 | ///////////////////////////////////////////////////////////Thread 38 | 39 | Thread::Thread() {} 40 | 41 | Thread::~Thread() {} 42 | 43 | void Thread::start() { 44 | _thread = std::thread(std::bind(&mmThreadRun, this)); 45 | } 46 | 47 | void Thread::join() { 48 | _thread.join(); 49 | } 50 | 51 | void Thread::detach() { 52 | _thread.detach(); 53 | } 54 | 55 | std::thread::id Thread::getId() const { 56 | return _thread.get_id(); 57 | } 58 | 59 | void Thread::sleep(int ms) { 60 | std::this_thread::sleep_for(std::chrono::milliseconds(ms)); 61 | } 62 | 63 | } 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/utils/crc32.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #ifndef __CRC32_H__ 23 | #define __CRC32_H__ 24 | 25 | #include 26 | 27 | uint32_t crc32(uint32_t crc, const uint8_t *buf, int len, int skiplen = 1); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /src/utils/notifier_pool.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "notifier_pool.h" 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | namespace phxpaxos 29 | { 30 | 31 | Notifier :: Notifier() 32 | { 33 | m_iPipeFD[0] = -1; 34 | m_iPipeFD[1] = -1; 35 | } 36 | 37 | Notifier :: ~Notifier() 38 | { 39 | for (int i = 0; i < 2; i++) 40 | { 41 | if (m_iPipeFD[i] != -1) 42 | { 43 | close(m_iPipeFD[i]); 44 | } 45 | } 46 | } 47 | 48 | int Notifier :: Init() 49 | { 50 | int ret = pipe(m_iPipeFD); 51 | if (ret != 0) 52 | { 53 | return ret; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | void Notifier :: SendNotify(const int ret) 60 | { 61 | int iWriteLen = write(m_iPipeFD[1], (char *)&ret, sizeof(int)); 62 | assert(iWriteLen == sizeof(int)); 63 | } 64 | 65 | void Notifier :: WaitNotify(int & ret) 66 | { 67 | ret = -1; 68 | int iReadLen = read(m_iPipeFD[0], (char *)&ret, sizeof(int)); 69 | assert(iReadLen == sizeof(int)); 70 | } 71 | 72 | /////////////////////////////////// 73 | 74 | NotifierPool :: NotifierPool() 75 | { 76 | } 77 | 78 | NotifierPool :: ~NotifierPool() 79 | { 80 | for (auto & it : m_mapPool) 81 | { 82 | delete it.second; 83 | } 84 | } 85 | 86 | int NotifierPool :: GetNotifier(const uint64_t iID, Notifier *& poNotifier) 87 | { 88 | poNotifier = nullptr; 89 | std::lock_guard oLock(m_oMutex); 90 | 91 | auto it = m_mapPool.find(iID); 92 | if (it != end(m_mapPool)) 93 | { 94 | poNotifier = it->second; 95 | return 0; 96 | } 97 | 98 | poNotifier = new Notifier(); 99 | assert(poNotifier != nullptr); 100 | int ret = poNotifier->Init(); 101 | if (ret != 0) 102 | { 103 | return ret; 104 | } 105 | 106 | m_mapPool[iID] = poNotifier; 107 | return 0; 108 | } 109 | 110 | } 111 | 112 | -------------------------------------------------------------------------------- /src/utils/notifier_pool.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | 27 | namespace phxpaxos 28 | { 29 | 30 | class Notifier 31 | { 32 | public: 33 | Notifier(); 34 | ~Notifier(); 35 | 36 | int Init(); 37 | 38 | void SendNotify(const int ret); 39 | 40 | void WaitNotify(int & ret); 41 | 42 | private: 43 | int m_iPipeFD[2]; 44 | }; 45 | 46 | ///////////////////////////////// 47 | 48 | class NotifierPool 49 | { 50 | public: 51 | NotifierPool(); 52 | ~NotifierPool(); 53 | 54 | int GetNotifier(const uint64_t iID, Notifier *& poNotifier); 55 | 56 | private: 57 | std::map m_mapPool; 58 | std::mutex m_oMutex; 59 | }; 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/utils/serial_lock.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "serial_lock.h" 23 | #include "util.h" 24 | 25 | namespace phxpaxos 26 | { 27 | 28 | SerialLock :: SerialLock() 29 | { 30 | } 31 | 32 | SerialLock :: ~SerialLock() 33 | { 34 | } 35 | 36 | void SerialLock :: Lock() 37 | { 38 | m_oMutex.lock(); 39 | } 40 | 41 | void SerialLock :: UnLock() 42 | { 43 | m_oMutex.unlock(); 44 | } 45 | 46 | void SerialLock :: Wait() 47 | { 48 | m_oCond.wait(m_oMutex); 49 | } 50 | 51 | void SerialLock :: Interupt() 52 | { 53 | m_oCond.notify_one(); 54 | } 55 | 56 | bool SerialLock :: WaitTime(const int iTimeMs) 57 | { 58 | return m_oCond.wait_for(m_oMutex, std::chrono::milliseconds(iTimeMs)) != std::cv_status::timeout; 59 | } 60 | 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/utils/serial_lock.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | 27 | namespace phxpaxos 28 | { 29 | 30 | class SerialLock 31 | { 32 | public: 33 | SerialLock(); 34 | ~SerialLock(); 35 | 36 | void Lock(); 37 | void UnLock(); 38 | 39 | void Wait(); 40 | void Interupt(); 41 | 42 | bool WaitTime(const int iTimeMs); 43 | 44 | private: 45 | std::mutex m_oMutex; 46 | std::condition_variable_any m_oCond; 47 | }; 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/utils/timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #include "timer.h" 23 | #include "util.h" 24 | #include 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | Timer :: Timer() : m_iNowTimerID(1) 30 | { 31 | } 32 | 33 | Timer :: ~Timer() 34 | { 35 | } 36 | 37 | void Timer :: AddTimer(const uint64_t llAbsTime, uint32_t & iTimerID) 38 | { 39 | return AddTimerWithType(llAbsTime, 0, iTimerID); 40 | } 41 | 42 | void Timer :: AddTimerWithType(const uint64_t llAbsTime, const int iType, uint32_t & iTimerID) 43 | { 44 | iTimerID = m_iNowTimerID++; 45 | 46 | TimerObj tObj(iTimerID, llAbsTime, iType); 47 | m_vecTimerHeap.push_back(tObj); 48 | push_heap(begin(m_vecTimerHeap), end(m_vecTimerHeap)); 49 | } 50 | 51 | const int Timer :: GetNextTimeout() const 52 | { 53 | if (m_vecTimerHeap.empty()) 54 | { 55 | return -1; 56 | } 57 | 58 | int iNextTimeout = 0; 59 | 60 | TimerObj tObj = m_vecTimerHeap.front(); 61 | uint64_t llNowTime = Time::GetSteadyClockMS(); 62 | if (tObj.m_llAbsTime > llNowTime) 63 | { 64 | iNextTimeout = (int)(tObj.m_llAbsTime - llNowTime); 65 | } 66 | 67 | return iNextTimeout; 68 | } 69 | 70 | bool Timer :: PopTimeout(uint32_t & iTimerID, int & iType) 71 | { 72 | if (m_vecTimerHeap.empty()) 73 | { 74 | return false; 75 | } 76 | 77 | TimerObj tObj = m_vecTimerHeap.front(); 78 | uint64_t llNowTime = Time::GetSteadyClockMS(); 79 | if (tObj.m_llAbsTime > llNowTime) 80 | { 81 | return false; 82 | } 83 | 84 | pop_heap(begin(m_vecTimerHeap), end(m_vecTimerHeap)); 85 | m_vecTimerHeap.pop_back(); 86 | 87 | iTimerID = tObj.m_iTimerID; 88 | iType = tObj.m_iType; 89 | 90 | return true; 91 | } 92 | 93 | } 94 | 95 | 96 | -------------------------------------------------------------------------------- /src/utils/timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | 27 | namespace phxpaxos 28 | { 29 | 30 | class Timer 31 | { 32 | public: 33 | Timer(); 34 | ~Timer(); 35 | 36 | void AddTimer(const uint64_t llAbsTime, uint32_t & iTimerID); 37 | 38 | void AddTimerWithType(const uint64_t llAbsTime, const int iType, uint32_t & iTimerID); 39 | 40 | bool PopTimeout(uint32_t & iTimerID, int & iType); 41 | 42 | const int GetNextTimeout() const; 43 | 44 | private: 45 | struct TimerObj 46 | { 47 | TimerObj(uint32_t iTimerID, uint64_t llAbsTime, int iType) 48 | : m_iTimerID(iTimerID), m_llAbsTime(llAbsTime), m_iType(iType) {} 49 | 50 | uint32_t m_iTimerID; 51 | uint64_t m_llAbsTime; 52 | int m_iType; 53 | 54 | bool operator < (const TimerObj & obj) const 55 | { 56 | if (obj.m_llAbsTime == m_llAbsTime) 57 | { 58 | return obj.m_iTimerID < m_iTimerID; 59 | } 60 | else 61 | { 62 | return obj.m_llAbsTime < m_llAbsTime; 63 | } 64 | } 65 | }; 66 | 67 | private: 68 | uint32_t m_iNowTimerID; 69 | std::vector m_vecTimerHeap; 70 | }; 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/utils/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | namespace phxpaxos { 41 | 42 | using std::string; 43 | using std::exception; 44 | using std::ostringstream; 45 | 46 | template 47 | string str(const T& t) { 48 | ostringstream os; 49 | os << t; 50 | return os.str(); 51 | } 52 | 53 | class SysCallException : public exception { 54 | public: 55 | SysCallException(int errCode, const string& errMsg, bool detail = true) : _errCode(errCode), _errMsg(errMsg) { 56 | if (detail) { 57 | _errMsg.append(", ").append(::strerror(errCode)); 58 | } 59 | } 60 | 61 | virtual ~SysCallException() throw () {} 62 | 63 | int getErrorCode() const throw () { 64 | return _errCode; 65 | } 66 | 67 | const char* what() const throw () { 68 | return _errMsg.c_str(); 69 | } 70 | 71 | protected: 72 | int _errCode; 73 | string _errMsg; 74 | }; 75 | 76 | class Noncopyable { 77 | protected: 78 | Noncopyable() {} 79 | ~Noncopyable() {} 80 | private: 81 | Noncopyable(const Noncopyable&); 82 | const Noncopyable& operator=(const Noncopyable&); 83 | }; 84 | 85 | /////////////////////////////////////////////////////////////// 86 | 87 | class Time 88 | { 89 | public: 90 | static const uint64_t GetTimestampMS(); 91 | 92 | static const uint64_t GetSteadyClockMS(); 93 | 94 | static void MsSleep(const int iTimeMs); 95 | }; 96 | 97 | class FileUtils 98 | { 99 | public: 100 | static int IsDir(const std::string & sPath, bool & bIsDir); 101 | 102 | static int DeleteDir(const std::string & sDirPath); 103 | 104 | static int IterDir(const std::string & sDirPath, std::vector & vecFilePathList); 105 | }; 106 | 107 | class TimeStat 108 | { 109 | public: 110 | TimeStat(); 111 | 112 | int Point(); 113 | private: 114 | uint64_t m_llTime; 115 | }; 116 | 117 | class OtherUtils 118 | { 119 | public: 120 | static uint64_t GenGid(const uint64_t llNodeID); 121 | 122 | static const uint32_t FastRand(); 123 | }; 124 | 125 | 126 | } 127 | -------------------------------------------------------------------------------- /src/utils/utils_include.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "./concurrent.h" 25 | #include "./socket.h" 26 | #include "./util.h" 27 | #include "./crc32.h" 28 | #include "./timer.h" 29 | #include "./serial_lock.h" 30 | #include "./wait_lock.h" 31 | #include "./bytes_buffer.h" 32 | #include "./notifier_pool.h" 33 | -------------------------------------------------------------------------------- /src/utils/wait_lock.h: -------------------------------------------------------------------------------- 1 | /* 2 | Tencent is pleased to support the open source community by making 3 | PhxPaxos available. 4 | Copyright (C) 2016 THL A29 Limited, a Tencent company. 5 | All rights reserved. 6 | 7 | Licensed under the BSD 3-Clause License (the "License"); you may 8 | not use this file except in compliance with the License. You may 9 | obtain a copy of the License at 10 | 11 | https://opensource.org/licenses/BSD-3-Clause 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" basis, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 16 | implied. See the License for the specific language governing 17 | permissions and limitations under the License. 18 | 19 | See the AUTHORS file for names of contributors. 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "serial_lock.h" 25 | 26 | namespace phxpaxos 27 | { 28 | 29 | #define WAIT_LOCK_USERTIME_AVG_INTERVAL 250 30 | 31 | class WaitLock 32 | { 33 | public: 34 | WaitLock(); 35 | ~WaitLock(); 36 | 37 | bool Lock(const int iTimeoutMs, int & iUseTimeMs); 38 | 39 | void UnLock(); 40 | 41 | void SetMaxWaitLockCount(const int iMaxWaitLockCount); 42 | 43 | void SetLockWaitTimeThreshold(const int iLockWaitTimeThresholdMS); 44 | 45 | public: 46 | //stat 47 | int GetNowHoldThreadCount(); 48 | 49 | int GetNowAvgThreadWaitTime(); 50 | 51 | int GetNowRejectRate(); 52 | 53 | private: 54 | void RefleshRejectRate(const int iUseTimeMs); 55 | 56 | bool CanLock(); 57 | 58 | private: 59 | SerialLock m_oSerialLock; 60 | bool m_bIsLockUsing; 61 | 62 | int m_iWaitLockCount; 63 | int m_iMaxWaitLockCount; 64 | 65 | int m_iLockUseTimeSum; 66 | int m_iAvgLockUseTime; 67 | int m_iLockUseTimeCount; 68 | 69 | int m_iRejectRate; 70 | int m_iLockWaitTimeThresholdMS; 71 | }; 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src_list: -------------------------------------------------------------------------------- 1 | src plugin include sample 2 | -------------------------------------------------------------------------------- /tools/build_comm.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | 4 | include_makefile_name="Makefile.define" 5 | user_home = os.path.expanduser('~') 6 | 7 | def GetLableName( name, suffix ): 8 | return name.upper()+"_"+suffix 9 | 10 | def GetLibName( name ): 11 | return "lib"+name+".a" 12 | 13 | def GetELibName( name ): 14 | return "elib"+name+".a" 15 | 16 | -------------------------------------------------------------------------------- /tools/check_install.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | from build_comm import * 4 | 5 | base_dir="" 6 | lib_dir="" 7 | bin_dir="" 8 | 9 | third_party_list=["PROTOBUF", "LEVELDB"] 10 | 11 | def GetPath(key): 12 | makefile_def=open("makefile.mk") 13 | lines = makefile_def.readlines() 14 | makefile_def.close() 15 | 16 | for line in lines: 17 | key_value = line.split('=') 18 | if( str.strip(key_value[0]) == key ): 19 | res=str.strip(key_value[1]) 20 | res=res.replace("$(SRC_BASE_PATH)",base_dir); 21 | res=res.replace("$(PHX_LIB_PATH)",lib_dir); 22 | return res 23 | return "" 24 | 25 | def GetPathPrefix(key): 26 | makefile_def=open("makefile.mk") 27 | lines = makefile_def.readlines() 28 | makefile_def.close() 29 | 30 | res_list=[] 31 | for line in lines: 32 | key_value = line.split('=') 33 | if( str.strip(key_value[0])[0:len(key)] == key and str.strip(key_value[0])[-4:]=="PATH"): 34 | res=str.strip(key_value[1]) 35 | res=res.replace("$(SRC_BASE_PATH)",base_dir); 36 | res=res.replace("$(PHX_LIB_PATH)",lib_dir); 37 | res_list.append( (str.strip(key_value[0]),res) ) 38 | return res_list 39 | 40 | def CheckBasePath(): 41 | global lib_dir, sbin_dir 42 | lib_dir=GetPath("PHX_LIB_PATH") 43 | 44 | if( not os.path.exists( lib_dir ) ): 45 | os.mkdir( lib_dir ) 46 | 47 | sbin_dir=GetPath("PHX_SBIN_PATH") 48 | 49 | if( not os.path.exists( sbin_dir ) ): 50 | os.mkdir( sbin_dir ) 51 | 52 | extlib_dir=GetPath("PHX_EXTLIB_PATH") 53 | if( not os.path.exists( extlib_dir ) ): 54 | os.mkdir( extlib_dir ) 55 | 56 | def Check3rdPath(): 57 | 58 | for lib in third_party_list: 59 | path_list=GetPathPrefix(lib) 60 | for path in path_list: 61 | if( not os.path.exists( path[1] ) ): 62 | print("%s not found" % path[1]) 63 | print("please make sure %s has been placed on third party directory" % lib) 64 | exit(1) 65 | 66 | if(__name__ == '__main__'): 67 | base_dir=sys.argv[1] 68 | CheckBasePath() 69 | Check3rdPath() 70 | exit(0) 71 | --------------------------------------------------------------------------------