-
Notifications
You must be signed in to change notification settings - Fork 436
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
chenzongjia
committed
Jun 8, 2017
1 parent
b33e170
commit e03a492
Showing
16 changed files
with
939 additions
and
3 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,38 @@ | ||
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "proto/timeoracle_client.h" | ||
|
||
namespace tera { | ||
namespace timeoracle { | ||
|
||
ThreadPool* TimeoracleClient::thread_pool_ = NULL; | ||
|
||
void TimeoracleClient::SetThreadPool(ThreadPool* thread_pool) { | ||
thread_pool_ = thread_pool; | ||
} | ||
|
||
void TimeoracleClient::SetRpcOption(int32_t max_inflow, int32_t max_outflow, | ||
int32_t pending_buffer_size, int32_t thread_num) { | ||
RpcClientBase::SetOption(max_inflow, max_outflow, | ||
pending_buffer_size, thread_num); | ||
} | ||
|
||
TimeoracleClient::TimeoracleClient(const std::string& server_addr, | ||
int32_t rpc_timeout) | ||
: RpcClient<TimeoracleServer::Stub>(server_addr), | ||
rpc_timeout_(rpc_timeout) {} | ||
|
||
TimeoracleClient::~TimeoracleClient() {} | ||
|
||
bool TimeoracleClient::GetTimestamp(const GetTimestampRequest* request, | ||
GetTimestampResponse* response, | ||
std::function<void (GetTimestampRequest*, GetTimestampResponse*, bool, int)> done) { | ||
return SendMessageWithRetry(&TimeoracleServer::Stub::GetTimestamp, | ||
request, response, done, "GetTimestamp", | ||
rpc_timeout_, thread_pool_); | ||
} | ||
|
||
} // namespace timeoracle | ||
} // namespace tera |
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,44 @@ | ||
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef TERA_TIMEORACLE_TIMEORACLE_CLIENT_H | ||
#define TERA_TIMEORACLE_TIMEORACLE_CLIENT_H | ||
|
||
#include <gflags/gflags.h> | ||
#include <sofa/pbrpc/pbrpc.h> | ||
|
||
#include "proto/timeoracle_rpc.pb.h" | ||
#include "proto/rpc_client.h" | ||
|
||
DECLARE_int32(tera_rpc_timeout_period); | ||
|
||
class ThreadPool; | ||
|
||
namespace tera { | ||
namespace timeoracle { | ||
|
||
class TimeoracleClient : public RpcClient<TimeoracleServer::Stub> { | ||
public: | ||
static void SetThreadPool(ThreadPool* thread_pool); | ||
|
||
static void SetRpcOption(int32_t max_inflow = -1, int32_t max_outflow = -1, | ||
int32_t pending_buffer_size = -1, | ||
int32_t thread_num = -1); | ||
|
||
TimeoracleClient(const std::string& addr = "", | ||
int32_t rpc_timeout = FLAGS_tera_rpc_timeout_period); | ||
|
||
~TimeoracleClient(); | ||
|
||
bool GetTimestamp(const GetTimestampRequest* request, GetTimestampResponse* response, | ||
std::function<void (GetTimestampRequest*, GetTimestampResponse*, bool, int)> done); | ||
private: | ||
int32_t rpc_timeout_; | ||
static ThreadPool* thread_pool_; | ||
}; | ||
|
||
} // namespace timeoracle | ||
} // namespace tera | ||
|
||
#endif // TERA_TIMEORACLE_TIMEORACLE_CLIENT_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,20 @@ | ||
import "sofa/pbrpc/rpc_option.proto"; | ||
import "status_code.proto"; | ||
|
||
package tera; | ||
|
||
message GetTimestampRequest { | ||
optional uint64 number = 1; | ||
} | ||
|
||
message GetTimestampResponse { | ||
optional StatusCode status = 1; | ||
optional uint64 start_timestamp = 2; | ||
optional uint64 number = 3; | ||
} | ||
|
||
service TimeoracleServer { | ||
rpc GetTimestamp(GetTimestampRequest) returns(GetTimestampResponse); | ||
} | ||
|
||
option cc_generic_services = true; |
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,46 @@ | ||
#include <iostream> | ||
#include <gflags/gflags.h> | ||
#include <glog/logging.h> | ||
#include "common/mutex.h" | ||
#include "common/thread_pool.h" | ||
#include "common/this_thread.h" | ||
|
||
#include "proto/timeoracle_client.h" | ||
|
||
using namespace tera; | ||
using namespace tera::timeoracle; | ||
|
||
int main(int argc, char** argv) { | ||
::google::ParseCommandLineFlags(&argc, &argv, true); | ||
|
||
std::shared_ptr<common::ThreadPool> thread_pool(new common::ThreadPool(10)); | ||
timeoracle::TimeoracleClient::SetThreadPool(thread_pool.get()); | ||
|
||
GetTimestampRequest request; | ||
GetTimestampResponse response; | ||
|
||
request.set_number(10); | ||
timeoracle::TimeoracleClient timeoracle_client("127.0.0.1:8881"); | ||
|
||
uint64_t x = 10000000000000ULL; | ||
uint64_t i = 0; | ||
while (++i) { | ||
if ((i % 10) == 0) { | ||
request.set_number(x); | ||
} else { | ||
request.set_number(10); | ||
} | ||
|
||
if (timeoracle_client.GetTimestamp(&request, &response, nullptr)) { | ||
if (!response.has_status()) { | ||
std::cout << "success ,timestamp=" << response.start_timestamp() | ||
<< ",number=" << response.number() << std::endl; | ||
continue; | ||
} | ||
} | ||
std::cout << "failed,,code=" << (int)response.status() << std::endl; | ||
ThisThread::Sleep(200); | ||
} | ||
|
||
return 0; | ||
} |
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,73 @@ | ||
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef TERA_TIMEORACLE_REMOTE_TIMEORACLE_H | ||
#define TERA_TIMEORACLE_REMOTE_TIMEORACLE_H | ||
|
||
#include <sofa/pbrpc/pbrpc.h> | ||
#include "common/base/scoped_ptr.h" | ||
#include "common/thread_pool.h" | ||
|
||
#include "proto/timeoracle_rpc.pb.h" | ||
#include "timeoracle/timeoracle.h" | ||
|
||
namespace tera { | ||
namespace timeoracle { | ||
|
||
class ClosureGuard { | ||
public: | ||
ClosureGuard(::google::protobuf::Closure* done) : done_(done) { | ||
} | ||
|
||
~ClosureGuard() { | ||
if (done_) { | ||
done_->Run(); | ||
} | ||
} | ||
|
||
::google::protobuf::Closure* release() { | ||
auto done = done_; | ||
done_ = nullptr; | ||
return done; | ||
} | ||
|
||
private: | ||
ClosureGuard(const ClosureGuard&) = delete; | ||
private: | ||
::google::protobuf::Closure* done_; | ||
}; | ||
|
||
class RemoteTimeoracle : public TimeoracleServer { | ||
public: | ||
RemoteTimeoracle(uint64_t start_timestamp) : timeoracle_(start_timestamp) { | ||
} | ||
|
||
virtual void GetTimestamp(::google::protobuf::RpcController* controller, | ||
const ::tera::GetTimestampRequest* request, | ||
::tera::GetTimestampResponse* response, | ||
::google::protobuf::Closure* done) { | ||
ClosureGuard closure_guard(done); | ||
|
||
uint64_t number = request->number(); | ||
uint64_t start_timestamp = timeoracle_.GetTimestamp(number); | ||
if (start_timestamp) { | ||
response->set_start_timestamp(start_timestamp); | ||
response->set_number(number); | ||
} else { | ||
response->set_status(kTimeoracleBusy); | ||
} | ||
} | ||
|
||
Timeoracle* GetTimeoracle() { | ||
return &timeoracle_; | ||
} | ||
|
||
private: | ||
Timeoracle timeoracle_; | ||
}; | ||
|
||
} // namespace timeoracle | ||
} // namespace tera | ||
|
||
#endif // TERA_TIMEORACLE_REMOTE_TIMEORACLE_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,13 @@ | ||
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "timeoracle/timeoracle.h" | ||
|
||
namespace tera { | ||
namespace timeoracle { | ||
|
||
std::atomic<uint64_t> Timeoracle::s_last_timestamp_ns; | ||
|
||
} // namespace timeoracle | ||
} // namespace tera |
Oops, something went wrong.