forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PikaProxy (OpenAtomFoundation#927) (OpenAtomFoundation#931)
ThreadModle done. TODO redis response parser, redis command builder.
- Loading branch information
Showing
10 changed files
with
541 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
#ifndef PIKA_PROXY_H_ | ||
#define PIKA_PROXY_H_ | ||
|
||
#include "include/pika_proxy_conn.h" | ||
#include "include/pika_proxy_cli.h" | ||
#include "include/pika_client_conn.h" | ||
|
||
class ProxyCliManager { | ||
public: | ||
ProxyCliManager(int conn_every_backend, int keepalive_time); | ||
~ProxyCliManager(); | ||
int Start(); | ||
int Stop(); | ||
// no need mutex lock here | ||
Status ChooseForwardToBackend(ProxyTask* task); | ||
|
||
private: | ||
Status ForwardNextAvailableConn(ProxyTask* task); | ||
std::vector<std::shared_ptr<ProxyCli>> clis_; | ||
std::atomic<uint64_t> rr_counter_; | ||
int conn_every_backend_; | ||
}; | ||
|
||
class PikaProxy { | ||
public: | ||
PikaProxy(); | ||
~PikaProxy(); | ||
int Start(); | ||
int Stop(); | ||
// write to client_thread and put it into task_queue | ||
static void ForwardToBackend(void* arg); | ||
// grep task_queue and | ||
static void WritebackToCliConn(void* arg); | ||
// bypass to g_pika_server | ||
void ScheduleForwardToBackend( | ||
const std::shared_ptr<PikaClientConn>& conn_ptr, | ||
const std::vector<pink::RedisCmdArgsType>& redis_cmds, | ||
const std::vector<Node>& dst); | ||
void MayScheduleWritebackToCliConn(std::shared_ptr<PikaProxyConn> conn_ptr, | ||
std::shared_ptr<ProxyCli> cli, const std::string res); | ||
std::shared_ptr<ProxyCliManager> cli_manager() { | ||
return cli_manager_ptr_; | ||
} | ||
|
||
private: | ||
std::shared_ptr<ProxyCliManager> cli_manager_ptr_; | ||
}; | ||
|
||
#endif // PIKA_PROXY_H_ |
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,92 @@ | ||
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
#ifndef PIKA_PROXY_CLI_H_ | ||
#define PIKA_PROXY_CLI_H_ | ||
|
||
#include "pink/include/pink_conn.h" | ||
#include "pink/include/client_thread.h" | ||
|
||
#include "include/pika_proxy_conn.h" | ||
|
||
using slash::Status; | ||
class ProxyCli; | ||
class ProxyFactory : public pink::ConnFactory { | ||
public: | ||
explicit ProxyFactory(std::shared_ptr<ProxyCli> proxy_cli); | ||
virtual std::shared_ptr<pink::PinkConn> NewPinkConn( | ||
int connfd, | ||
const std::string &ip_port, | ||
pink::Thread *thread, | ||
void* worker_specific_data, | ||
pink::PinkEpoll* pink_epoll) const override { | ||
return std::static_pointer_cast<pink::PinkConn> | ||
(std::make_shared<PikaProxyConn>( | ||
connfd, ip_port, thread, pink_epoll, proxy_cli_)); | ||
} | ||
private: | ||
std::shared_ptr<ProxyCli> proxy_cli_; | ||
}; | ||
|
||
class ProxyHandle : public pink::ClientHandle { | ||
public: | ||
explicit ProxyHandle(std::shared_ptr<ProxyCli> proxy_cli) : ClientHandle() { | ||
proxy_cli_ = proxy_cli; | ||
} | ||
~ProxyHandle() { | ||
} | ||
void CronHandle() const override { | ||
} | ||
void FdTimeoutHandle(int fd, const std::string& ip_port) const override { | ||
} | ||
void FdClosedHandle(int fd, const std::string& ip_port) const override; | ||
bool AccessHandle(std::string& ip) const override { | ||
return true; | ||
} | ||
int CreateWorkerSpecificData(void** data) const override { | ||
return 0; | ||
} | ||
int DeleteWorkerSpecificData(void* data) const override { | ||
return 0; | ||
} | ||
void DestConnectFailedHandle( | ||
std::string ip_port, std::string reason) const override { | ||
} | ||
|
||
private: | ||
std::shared_ptr<ProxyCli> proxy_cli_; | ||
}; | ||
|
||
class ProxyCli : public std::enable_shared_from_this<ProxyCli> { | ||
public: | ||
ProxyCli(int cron_interval, int keepalive_timeout); | ||
int Start(); | ||
int Stop(); | ||
Status ForwardToBackend(ProxyTask* task); | ||
Status WritebackUpdate(const std::string& ip_port, | ||
const std::string& res, bool* write_back, ProxyTask** res_task); | ||
|
||
struct ProxyCliTask { | ||
std::shared_ptr<PikaClientConn> conn_ptr; | ||
std::shared_ptr<std::string> resp_ptr; | ||
}; | ||
void LostConn(const std::string& ip_port); | ||
|
||
private: | ||
int cron_interval_; | ||
int keepalive_timeout_; | ||
ProxyFactory* proxy_factory_; | ||
ProxyHandle* proxy_handle_; | ||
|
||
slash::Mutex input_l_; | ||
std::shared_ptr<pink::ClientThread> client_ptr_; | ||
// pair(backend conn ip + port, std::deque<ProxyCliTask>) | ||
std::unordered_map<std::string, std::deque<ProxyCliTask>> backend_task_queue_; | ||
// pair(client ip + port, ProxyTask*) | ||
std::unordered_map<std::string, ProxyTask*> task_queue_; | ||
}; | ||
|
||
#endif // PIKA_PROXY_CLI_H_ | ||
# |
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,40 @@ | ||
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
#ifndef PIKA_PROXY_CONN_H_ | ||
#define PIKA_PROXY_CONN_H_ | ||
|
||
#include "pink/include/redis_conn.h" | ||
|
||
#include "include/pika_client_conn.h" | ||
|
||
class ProxyCli; | ||
class PikaProxyConn; | ||
|
||
struct ProxyTask { | ||
ProxyTask() { | ||
} | ||
std::shared_ptr<PikaClientConn> conn_ptr; | ||
std::vector<pink::RedisCmdArgsType> redis_cmds; | ||
std::vector<Node> redis_cmds_forward_dst; | ||
}; | ||
|
||
class PikaProxyConn: public pink::RedisConn { | ||
public: | ||
PikaProxyConn(int fd, std::string ip_port, | ||
pink::Thread *server_thread, | ||
pink::PinkEpoll* pink_epoll, | ||
std::shared_ptr<ProxyCli> proxy_cli); | ||
virtual ~PikaProxyConn() {} | ||
|
||
private: | ||
int DealMessage( | ||
const pink::RedisCmdArgsType& argv, | ||
std::string* response) override; | ||
|
||
std::shared_ptr<ProxyCli> proxy_cli_; | ||
}; | ||
|
||
#endif // PIKA_PROXY_CONN_H_ |
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
Oops, something went wrong.