-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
12,172 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/************************************************************************ | ||
* | ||
* Copyright (c) 2016 Alibaba.com, Inc. All Rights Reserved | ||
* $Id: client_service.h,v 1.0 08/23/2016 10:02:33 AM yingqiang.zyq([email protected]) $ | ||
* | ||
************************************************************************/ | ||
|
||
/** | ||
* @file client_service.h | ||
* @author yingqiang.zyq([email protected]) | ||
* @date 08/23/2016 10:02:33 AM | ||
* @version 1.0 | ||
* @brief | ||
* | ||
**/ | ||
|
||
#ifndef client_service_INC | ||
#define client_service_INC | ||
|
||
#include <cstring> | ||
#include <string> | ||
#include <map> | ||
#include "easyNet.h" | ||
|
||
namespace alisql { | ||
|
||
class Paxos; | ||
|
||
/** | ||
* @class ClientService | ||
* | ||
* @brief | ||
* | ||
**/ | ||
class ClientService { | ||
public: | ||
ClientService () {}; | ||
virtual ~ClientService () {}; | ||
|
||
const std::string &get(const std::string &key) {return map_[key];} | ||
void set(const std::string &key, const std::string &val); | ||
const std::string set(const char *strKeyVal, uint64_t len); | ||
int serviceProcess(easy_request_t *r, void *args); | ||
|
||
protected: | ||
std::map<const std::string, const std::string> map_; | ||
|
||
|
||
private: | ||
ClientService ( const ClientService &other ); // copy constructor | ||
const ClientService& operator = ( const ClientService &other ); // assignment operator | ||
|
||
};/* end of class ClientService */ | ||
|
||
|
||
|
||
} //namespace alisql | ||
|
||
#endif //#ifndef client_service_INC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/************************************************************************ | ||
* | ||
* Copyright (c) 2016 Alibaba.com, Inc. All Rights Reserved | ||
* $Id: consensus.h,v 1.0 07/27/2016 11:42:30 AM yingqiang.zyq([email protected]) $ | ||
* | ||
************************************************************************/ | ||
|
||
/** | ||
* @file consensus.h | ||
* @author yingqiang.zyq([email protected]) | ||
* @date 07/27/2016 11:42:30 AM | ||
* @version 1.0 | ||
* @brief the interface class of Consensus | ||
* | ||
**/ | ||
|
||
#ifndef cluster_consensus_INC | ||
#define cluster_consensus_INC | ||
|
||
#include "paxos.pb.h" | ||
#include "service.h" | ||
|
||
namespace alisql { | ||
|
||
|
||
/** | ||
* @class Consensus | ||
* | ||
* @brief interface of consensus algorithm module | ||
* | ||
**/ | ||
class Consensus { | ||
public: | ||
enum MsgType { | ||
RequestVote= 0, | ||
RequestVoteResponce, | ||
AppendLog, | ||
AppendLogResponce, | ||
LeaderCommand, | ||
LeaderCommandResponce, | ||
ClusterIdNotMatch, | ||
OptimisticHeartbeat, | ||
PreCheckFailedResponce | ||
}; | ||
|
||
typedef enum CCOpType { | ||
CCNoOp= 0, | ||
CCMemberOp= 1, | ||
CCLearnerOp= 2, | ||
CCAddNode= 3, | ||
CCDelNode= 4, | ||
CCConfigureNode= 5, | ||
CCDowngradeNode= 6, | ||
CCSyncLearnerAll= 7, | ||
CCAddLearnerAutoChange= 8, | ||
CCLeaderTransfer= 9 | ||
} CCOpTypeT; | ||
|
||
Consensus () {}; | ||
virtual ~Consensus () {}; | ||
|
||
virtual int onRequestVote(PaxosMsg *msg, PaxosMsg *rsp) = 0; | ||
virtual int onAppendLog(PaxosMsg *msg, PaxosMsg *rsp) = 0; | ||
virtual int onRequestVoteResponce(PaxosMsg *msg) = 0; | ||
virtual int onAppendLogSendFail(PaxosMsg *msg, uint64_t *newId) = 0; | ||
virtual int onAppendLogResponce(PaxosMsg *msg) = 0; | ||
virtual int requestVote(bool force) = 0; | ||
virtual uint64_t replicateLog(LogEntry &entry) = 0; | ||
virtual int appendLog(const bool needLock) = 0; | ||
virtual int onLeaderCommand(PaxosMsg *msg, PaxosMsg *rsp) = 0; | ||
virtual int onLeaderCommandResponce(PaxosMsg *msg) = 0; | ||
virtual int onClusterIdNotMatch(PaxosMsg *msg) = 0; | ||
virtual int onMsgPreCheck(PaxosMsg *msg, PaxosMsg *rsp) = 0; | ||
virtual int onMsgPreCheckFailed(PaxosMsg *msg) = 0; | ||
virtual uint64_t getClusterId() = 0; | ||
virtual int setClusterId(uint64_t ci) = 0; | ||
virtual bool isShutdown() = 0; | ||
|
||
protected: | ||
|
||
private: | ||
Consensus ( const Consensus &other ); // copy constructor | ||
const Consensus& operator = ( const Consensus &other ); // assignment operator | ||
|
||
};/* end of class Consensus */ | ||
|
||
|
||
|
||
} //namespace alisql | ||
|
||
#endif //#ifndef cluster_consensus_INC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
namespace alisql { | ||
|
||
// Return the crc32c of concat(A, data[0,n-1]) where init_crc is the | ||
// crc32c of some string A. Extend() is often used to maintain the | ||
// crc32c of a stream of data. | ||
extern uint32_t Extend(uint32_t init_crc, const char* data, size_t n); | ||
|
||
// Return the crc32c of data[0,n-1] | ||
inline uint32_t calculateCRC32(const char* data, size_t n) { | ||
return Extend(0, data, n); | ||
} | ||
|
||
} // namespace alisql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/************************************************************************ | ||
* | ||
* Copyright (c) 2016 Alibaba.com, Inc. All Rights Reserved | ||
* $Id: easyNet.h,v 1.0 07/27/2016 03:51:26 PM yingqiang.zyq([email protected]) $ | ||
* | ||
************************************************************************/ | ||
|
||
/** | ||
* @file easyNet.h | ||
* @author yingqiang.zyq([email protected]) | ||
* @date 07/27/2016 03:51:26 PM | ||
* @version 1.0 | ||
* @brief | ||
* | ||
**/ | ||
|
||
#ifndef cluster_easynet_INC | ||
#define cluster_easynet_INC | ||
|
||
#include <map> | ||
#include <memory> | ||
#include <mutex> | ||
#include <easy_io.h> | ||
#include <easy_atomic.h> | ||
#include "net.h" | ||
|
||
namespace alisql { | ||
|
||
typedef struct NetPacket { | ||
uint type; | ||
uint64_t packetId; | ||
void *msg; | ||
int len; | ||
char *data; | ||
char buffer[0]; | ||
} NetPacket; | ||
|
||
const uint NetPacketTypeNet= 0; | ||
const uint NetPacketTypeAsync= 1; | ||
|
||
const uint64_t NetPacketHeaderSize= sizeof(uint64_t); | ||
|
||
// global variable to count easy pool memory usage | ||
extern easy_atomic_t easy_pool_alloc_byte; | ||
|
||
/** | ||
* @class EasyNet | ||
* | ||
* @brief | ||
* | ||
**/ | ||
class EasyNet : public Net{ | ||
public: | ||
EasyNet (uint64_t num= 2, const uint64_t sessionTimeout= 300, bool memory_usage_count= false); | ||
virtual ~EasyNet () {}; | ||
|
||
virtual int init(void *ptr= NULL); | ||
virtual int start(int port); | ||
virtual int shutdown(); | ||
virtual int stop(); | ||
|
||
/* TODO here we should use a general handler. */ | ||
virtual easy_addr_t createConnection(const std::string &addr, NetServerRef server, uint64_t timeout= 1000, uint64_t index= 0); | ||
virtual void disableConnection(easy_addr_t addr); | ||
virtual int sendPacket(easy_addr_t addr, const char *buf, uint64_t len, uint64_t id= 0); | ||
virtual int sendPacket(easy_addr_t addr, const std::string& buf, uint64_t id= 0); | ||
virtual int resendPacket(easy_addr_t addr, void *ptr, uint64_t id= 0); | ||
virtual int setRecvPacketCallback(void *handler); | ||
|
||
void setWorkPool(easy_thread_pool_t* tp) {std::lock_guard<std::mutex> lg(lock_); workPool_= tp;} | ||
void incRecived() {__sync_fetch_and_add(&reciveCnt_, 1);} | ||
uint64_t getReciveCnt() {return reciveCnt_;} | ||
// bool isShutDown() {return isShutdown_;} /* not used now. */ | ||
|
||
uint64_t getAddrKey(easy_addr_t addr); | ||
NetServerRef getConnData(easy_addr_t addr, bool locked = false); | ||
void setConnData(easy_addr_t addr, NetServerRef server); | ||
void delConnDataById(uint64_t id); | ||
NetServerRef getConnDataAndSetFail(easy_connection_t *c, bool isFail); | ||
uint64_t getConnCnt() {return connStatus_.size();} | ||
void setSessionTimeout(uint64_t t) { sessionTimeout_= t; } | ||
|
||
static void tryFreeMsg(NetPacket *np); | ||
|
||
/* Handler functions. */ | ||
static int reciveProcess(easy_request_t *r); | ||
static void *paxosDecode(easy_message_t *m); | ||
static int paxosEncode(easy_request_t *r, void *data); | ||
static int onConnected(easy_connection_t *c); | ||
static int onDisconnected(easy_connection_t *c); | ||
static int onClientCleanup(easy_request_t *r, void *apacket); | ||
static uint64_t getPacketId(easy_connection_t *c, void *data); | ||
|
||
protected: | ||
/* libeasy member. */ | ||
easy_io_t *eio_; | ||
easy_io_handler_pt clientHandler_; | ||
easy_io_handler_pt serverHandler_; | ||
easy_thread_pool_t *workPool_; | ||
|
||
/*TODO we shoud use shared_ptr here. */ | ||
std::map<uint64_t, NetServerRef> connStatus_; | ||
std::mutex lock_; | ||
|
||
uint64_t reciveCnt_; | ||
bool isShutdown_; | ||
uint64_t sessionTimeout_; | ||
|
||
private: | ||
EasyNet ( const EasyNet &other ); // copy constructor | ||
const EasyNet& operator = ( const EasyNet &other ); // assignment operator | ||
|
||
};/* end of class EasyNet */ | ||
|
||
|
||
|
||
} //namespace alisql | ||
#endif //#ifndef cluster_easynet_INC |
Oops, something went wrong.