Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-factor the vgraph.thrift #28

Merged
merged 3 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 20 additions & 50 deletions client/cpp/GraphDbClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "client/cpp/GraphDbClient.h"
#include <thrift/lib/cpp/async/TAsyncSocket.h>
#include <thrift/lib/cpp2/async/HeaderClientChannel.h>
#include "dataman/RowSetReader.h"

DEFINE_int32(conn_timeout_ms, 1000,
"Connection timeout in milliseconds");
Expand All @@ -17,15 +16,6 @@ DEFINE_int32(conn_timeout_ms, 1000,
namespace vesoft {
namespace vgraph {

enum class ClientError {
SUCCEEDED = 0,

E_DISCONNECTED = -1001,
E_FAIL_TO_CONNECT = -1002,
E_RPC_FAILURE = -1003,
};


GraphDbClient::GraphDbClient(const std::string& addr, uint16_t port)
: addr_(addr)
, port_(port)
Expand All @@ -38,8 +28,8 @@ GraphDbClient::~GraphDbClient() {
}


int32_t GraphDbClient::connect(const std::string& username,
const std::string& password) {
cpp2::ErrorCode GraphDbClient::connect(const std::string& username,
const std::string& password) {
using namespace apache::thrift;

auto socket = async::TAsyncSocket::newSocket(
Expand All @@ -50,11 +40,12 @@ int32_t GraphDbClient::connect(const std::string& username,
if (!socket) {
// Bad connection
LOG(ERROR) << "Failed to connect to " << addr_ << ":" << port_;
errorStr_ = "Server is unavailable";
return static_cast<int32_t>(ClientError::E_FAIL_TO_CONNECT);
return cpp2::ErrorCode::E_FAIL_TO_CONNECT;
}

// Wait until the socket bcomes connected
// Wait until the socket becomes connected
// TODO Obviously this is not the most efficient way. We need to
// change it to async implementation later
for (int i = 0; i < 4; i++) {
usleep(1000 * FLAGS_conn_timeout_ms / 4);
if (socket->good()) {
Expand All @@ -64,8 +55,7 @@ int32_t GraphDbClient::connect(const std::string& username,
}
if (!socket->good()) {
LOG(ERROR) << "Timed out when connecting to " << addr_ << ":" << port_;
errorStr_ = "Server is unavailable";
return static_cast<int32_t>(ClientError::E_FAIL_TO_CONNECT);
return cpp2::ErrorCode::E_FAIL_TO_CONNECT;
}

client_ = std::make_unique<cpp2::GraphDbServiceAsyncClient>(
Expand All @@ -74,19 +64,18 @@ int32_t GraphDbClient::connect(const std::string& username,
cpp2::AuthResponse resp;
try {
client_->sync_authenticate(resp, username, password);
if (resp.get_result() != cpp2::ResultCode::SUCCEEDED) {
errorStr_ = std::move(*(resp.get_errorMsg()));
return static_cast<int32_t>(resp.get_result());
if (resp.get_error_code() != cpp2::ErrorCode::SUCCEEDED) {
LOG(ERROR) << "Failed to authenticate \"" << username << "\": "
<< resp.get_error_msg();
return resp.get_error_code();
}
sessionId_ = *(resp.get_sessionId());
} catch (const std::exception& ex) {
LOG(ERROR) << "Thrift rpc call failed: " << ex.what();
errorStr_ = folly::stringPrintf("Failed to make the RPC call: %s",
ex.what());
return static_cast<int32_t>(ClientError::E_RPC_FAILURE);
return cpp2::ErrorCode::E_RPC_FAILURE;
}

return static_cast<int32_t>(ClientError::SUCCEEDED);
sessionId_ = *(resp.get_session_id());
return cpp2::ErrorCode::SUCCEEDED;
}


Expand All @@ -102,40 +91,21 @@ void GraphDbClient::disconnect() {
}


int32_t GraphDbClient::execute(folly::StringPiece stmt,
std::unique_ptr<RowSetReader>& rowsetReader) {
cpp2::ErrorCode GraphDbClient::execute(folly::StringPiece stmt,
cpp2::ExecutionResponse& resp) {
if (!client_) {
errorStr_ = "Disconnected from the server";
return static_cast<int32_t>(ClientError::E_DISCONNECTED);
LOG(ERROR) << "Disconnected from the server";
return cpp2::ErrorCode::E_DISCONNECTED;
}

cpp2::ExecutionResponse resp;
try {
client_->sync_execute(resp, sessionId_, stmt.toString());
if (resp.get_result() != cpp2::ResultCode::SUCCEEDED) {
errorStr_ = std::move(*(resp.get_errorMsg()));
return static_cast<int32_t>(resp.get_result());
}
} catch (const std::exception& ex) {
LOG(ERROR) << "Thrift rpc call failed: " << ex.what();
errorStr_ = folly::stringPrintf("Failed to make the RPC call: %s",
ex.what());
return static_cast<int32_t>(ClientError::E_RPC_FAILURE);
return cpp2::ErrorCode::E_RPC_FAILURE;
}

latencyInMs_ = resp.get_latencyInMs();
rowsetReader.reset(new RowSetReader(resp));
return static_cast<int32_t>(ClientError::SUCCEEDED);
}


const char* GraphDbClient::getErrorStr() const {
return errorStr_.c_str();
}


int32_t GraphDbClient::getServerLatency() const {
return latencyInMs_;
return resp.get_error_code();
}

} // namespace vgraph
Expand Down
30 changes: 5 additions & 25 deletions client/cpp/GraphDbClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,24 @@
namespace vesoft {
namespace vgraph {

class RowSetReader;

class GraphDbClient {
public:
GraphDbClient(const std::string& addr, uint16_t port);
virtual ~GraphDbClient();

// When authentication succeeds, the method returns 0, otherwise
// an negative error code will be returned
//
// When the method returns error, getErrorStr() can be called to
// get the human readable error message
int32_t connect(const std::string& username,
const std::string& password);
// Authenticate the user
cpp2::ErrorCode connect(const std::string& username,
const std::string& password);
void disconnect();

// When execution succeeds, the method returns 0, otherwise
// a negative error code will be returned
//
// When the method returns error, getErrorStr() can be called to
// get the human readable error message
int32_t execute(folly::StringPiece stmt,
std::unique_ptr<RowSetReader>& rowsetReader);

// Get the server latency in milliseconds
int32_t getServerLatency() const;

// Return the last human readable error message
const char* getErrorStr() const;
cpp2::ErrorCode execute(folly::StringPiece stmt,
cpp2::ExecutionResponse& resp);

private:
std::unique_ptr<cpp2::GraphDbServiceAsyncClient> client_;
const std::string addr_;
const uint16_t port_;
int64_t sessionId_;

std::string errorStr_;
int32_t latencyInMs_;
};

} // namespace vgraph
Expand Down
1 change: 0 additions & 1 deletion console/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_executable(
GraphDbConsole.cpp
$<TARGET_OBJECTS:console_obj>
$<TARGET_OBJECTS:client_cpp_obj>
$<TARGET_OBJECTS:dataman_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:vgraph_thrift_obj>
$<TARGET_OBJECTS:time_obj>
Expand Down
6 changes: 3 additions & 3 deletions console/CliManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ bool CliManager::connect(const std::string& addr,
username_ = user;

auto client = std::make_unique<GraphDbClient>(addr_, port_);
int32_t err = client->connect(user, pass);
if (!err) {
cpp2::ErrorCode res = client->connect(user, pass);
if (res == cpp2::ErrorCode::SUCCEEDED) {
std::cerr << "\nWelcome to vGraph (Version 0.1)\n\n";
cmdProcessor_ = std::make_unique<CmdProcessor>(std::move(client));
return true;
} else {
// There is an error
std::cout << "Authentication failed: " << client->getErrorStr() << "\n";
std::cout << "Authentication failed\n";
return false;
}
}
Expand Down
Loading