Skip to content

Commit

Permalink
Fix java client (vesoft-inc#819)
Browse files Browse the repository at this point in the history
* enhance java client

* fix docs/nGQL-tutorial.md
  • Loading branch information
yaphet authored and dangleptr committed Aug 29, 2019
1 parent cb20134 commit 39fc096
Show file tree
Hide file tree
Showing 20 changed files with 336 additions and 276 deletions.
2 changes: 1 addition & 1 deletion .linters/java/nebula_java_style_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
</module>
<module name="AbbreviationAsWordInName">
<property name="ignoreFinal" value="false"/>
<property name="allowedAbbreviationLength" value="1"/>
<property name="allowedAbbreviationLength" value="4"/>
</module>
<module name="OverloadMethodsDeclarationOrder"/>
<module name="VariableDeclarationUsageDistance"/>
Expand Down
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,6 @@ macro(nebula_link_libraries target)
endmacro(nebula_link_libraries)

if (NOT SKIP_JAVA_CLIENT)
if (NOT EXISTS "${NEBULA_THIRDPARTY_ROOT}/fbthrift/thrift-1.0-SNAPSHOT.jar")
message(FATAL_ERROR "Doesn't have thrift-1.0-SNAPSHOT.jar")
endif()
set(java-fbthrift-jar ${NEBULA_THIRDPARTY_ROOT}/fbthrift/thrift-1.0-SNAPSHOT.jar)
set(NEBULA_CLEAN_ALL_DEPS clean-interface clean-pch clean-hbase clean-java-client)
else()
set(NEBULA_CLEAN_ALL_DEPS clean-interface clean-pch clean-hbase)
Expand Down
9 changes: 8 additions & 1 deletion docs/how-to-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ bash> make
bash> sudo make install
```
If you want to build the java client, you should run the following command:
```
cd third-party/fbthrift/thrift/lib/java/thrift
mvn compile install
```
#### Step3: Build nebula
The default installation directory is **/usr/local/nebula**
Expand Down Expand Up @@ -363,4 +370,4 @@ bash> sudo make install
```
sudo update-alternatives --config java
```
and select the java-1.8.0-openjdk/java-8-openjdk
and select the java-1.8.0-openjdk/java-8-openjdk
2 changes: 1 addition & 1 deletion src/client/java/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
add_custom_target(
java-client ALL
COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/build.sh" ${java-fbthrift-jar} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ">" "./_build.log" "2>&1"
COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/build.sh" ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ">" "./_build.log" "2>&1"
DEPENDS graph_thrift_obj
)

Expand Down
10 changes: 10 additions & 0 deletions src/client/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

### Java Client

***

The Java Client is dependent on `com.facebook:thrift:1.0-SNAPSHOT`.

Please make sure the dependency have been install on your maven repo.

Please see [how-to-build.md](../../../docs/how-to-build.md)
49 changes: 21 additions & 28 deletions src/client/java/build.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/bin/bash


# Copyright (c) 2018 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License,
# attached with Common Clause Condition 1.0, found in the LICENSES directory.

#-----------------------------------------------
# variable value
#-----------------------------------------------
# $1: the path of thrift-1.0-SNAPSHOT.jar
# $2: the path of java src
# $3: the path of current build dir
java_fbthrift_jar=$1
src_root_path=$2
output_src_file=$3
# $1: the path of java src
# $2: the path of current build dir
src_root_path=$1
output_src_file=$2
pom_file_path=${src_root_path}/pom.xml
graph_gen_java_path=${output_src_file}/../../interface/gen-java/com/vesoft/nebula/graph/
java_client_source_file_path=${src_root_path}/src/main/java/com/vesoft/nebula/graph/
Expand All @@ -18,43 +21,33 @@ java_client_source_file_path=${src_root_path}/src/main/java/com/vesoft/nebula/gr
#-----------------------------------------------
check_file_exist()
{
if [ -f $1 ] || [ -d $1 ]
then
echo "File or path exist : $1"
else
echo "File or path not exist : $1"
exit 1;
fi
}

#-----------------------------------------------
# check maven tool exist
#-----------------------------------------------

maven_check()
{
command -v mvn >/dev/null 2>&1 || { echo >&2 "Require maven but it's not installed. Aborting."; exit 1; }
if [ -f $1 ] || [ -d $1 ]
then
echo "File or path exist : $1"
else
echo "File or path not exist : $1"
exit 1;
fi
}

#-----------------------------------------------
# setup dependent graph source files
#-----------------------------------------------
setup_graph_source()
{
check_file_exist $graph_gen_java_path
find $java_client_source_file_path -type l |xargs rm -rf {}
ln -s $graph_gen_java_path/* $java_client_source_file_path
check_file_exist $graph_gen_java_path
find $java_client_source_file_path -type l |xargs rm -rf {}
ln -s $graph_gen_java_path/* $java_client_source_file_path
}

#-----------------------------------------------
# compile java-client
#-----------------------------------------------
compile_java_client()
{
mvn clean package -DJAVA_FBTHRIFT_JAR=$java_fbthrift_jar -f ${pom_file_path}
mvn clean package -f ${pom_file_path}
}


setup_graph_source
compile_java_client

6 changes: 2 additions & 4 deletions src/client/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>nebula-graph</groupId>
<artifactId>graph-client</artifactId>
<version>0.1.0</version>
<version>1.0.0-beta</version>

<properties>
<project.root.dir>../../..</project.root.dir>
Expand Down Expand Up @@ -70,9 +70,7 @@
<dependency>
<groupId>com.facebook</groupId>
<artifactId>thrift</artifactId>
<version>1.0.0-SNAPSHORT</version>
<scope>system</scope>
<systemPath>${JAVA_FBTHRIFT_JAR}</systemPath>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright (c) 2019 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

package com.vesoft.nebula.graph.client;

public class ConnectionException extends Exception {

public ConnectionException() {
super();
}

public ConnectionException(String message) {
super(message);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,180 +6,21 @@

package com.vesoft.nebula.graph.client;

import static com.google.common.base.Preconditions.checkArgument;

import com.facebook.thrift.TException;
import com.facebook.thrift.protocol.TBinaryProtocol;
import com.facebook.thrift.protocol.TProtocol;
import com.facebook.thrift.transport.TSocket;
import com.facebook.thrift.transport.TTransport;
import com.facebook.thrift.transport.TTransportException;
import com.google.common.collect.Lists;
import com.google.common.net.InetAddresses;
import com.vesoft.nebula.graph.AuthResponse;
import com.vesoft.nebula.graph.ErrorCode;
import com.vesoft.nebula.graph.ExecutionResponse;
import com.vesoft.nebula.graph.GraphService;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GraphClient implements GraphClientIface {

private static final Logger LOGGER = LoggerFactory.getLogger(GraphClient.class.getName());

private int retry;
private final int timeout;
private long sessionId_;
private TTransport transport = null;
private GraphService.Client syncClient;
private List<InetSocketAddress> nodes = Lists.newLinkedList();

public GraphClient(Map<String, Integer> addresses, int timeout, int retry) {
checkArgument(addresses != null && addresses.size() != 0);
checkArgument(timeout > 0);
checkArgument(retry > 0);

for (Map.Entry<String, Integer> entry : addresses.entrySet()) {
String host = entry.getKey();
int port = entry.getValue();
if (!InetAddresses.isInetAddress(host) || (port <= 0 || port >= 65535)) {
throw new IllegalArgumentException(String.format("%s:%d is not a valid address", host, port));
} else {
nodes.add(new InetSocketAddress(host, port));
}
}
this.timeout = timeout;
this.retry = retry;
}

public GraphClient(Map<String, Integer> addresses) {
this(addresses, DEFAULT_TIMEOUT_MS, DEFAULT_CONNECTION_RETRY_SIZE);
}

@Override
public int connect(String username, String password) {
while (retry != 0) {
retry -= 1;

Random random = new Random(System.currentTimeMillis());
int index = random.nextInt(nodes.size());
InetSocketAddress activeNode = nodes.get(index);

String activeHost;
InetAddress address = activeNode.getAddress();
if (address instanceof Inet6Address) {
activeHost = InetAddresses.toUriString(address);
} else {
activeHost = InetAddresses.toAddrString(address);
}

transport = new TSocket(activeHost,
activeNode.getPort(), timeout);
TProtocol protocol = new TBinaryProtocol(transport);

try {
transport.open();
syncClient = new GraphService.Client(protocol);
AuthResponse result = syncClient.authenticate(username, password);
if (result.getError_code() == ErrorCode.E_BAD_USERNAME_PASSWORD) {
LOGGER.error("User name or password error");
return ErrorCode.E_BAD_USERNAME_PASSWORD;
}

if (result.getError_code() != ErrorCode.SUCCEEDED) {
LOGGER.error(String.format("Host : %s error : %s",
activeHost, result.getError_msg()));
} else {
sessionId_ = result.getSession_id();
return ErrorCode.SUCCEEDED;
}
} catch (TTransportException tte) {
LOGGER.error("Connect failed: " + tte.getMessage());
} catch (TException te) {
LOGGER.error("Connect failed: " + te.getMessage());
}
}
return ErrorCode.E_FAIL_TO_CONNECT;
}

@Override
public void disconnect() {
if (!checkTransportOpened(transport)) {
return;
}

try {
syncClient.signout(sessionId_);
} catch (TException e) {
LOGGER.error("Disconnect error: " + e.getMessage());
} finally {
transport.close();
}
}

@Override
public int execute(String stmt) {
if (!checkTransportOpened(transport)) {
return ErrorCode.E_DISCONNECTED;
}
public interface GraphClient {

try {
ExecutionResponse executionResponse = syncClient.execute(sessionId_, stmt);
if (executionResponse.getError_code() != ErrorCode.SUCCEEDED) {
LOGGER.error("execute error: " + executionResponse.getError_msg());
}
return executionResponse.getError_code();
} catch (TException e) {
LOGGER.error("Thrift rpc call failed: " + e.getMessage());
return ErrorCode.E_RPC_FAILURE;
}
}
public static final int DEFAULT_PORT = 3699;
public static final int DEFAULT_TIMEOUT_MS = 1000;
public static final int DEFAULT_CONNECTION_RETRY_SIZE = 3;

@Override
public int executeUpdate(String stmt) {
return execute(stmt);
}
public int connect(String username, String password);

@Override
public ResultSet executeQuery(String stmt) {
ExecutionResponse executionResponse = internalExecuteQuery(stmt);
if (Objects.isNull(executionResponse)) {
return new ResultSet();
} else {
return new ResultSet(executionResponse.getColumn_names(),
executionResponse.getRows());
}
}
public void disconnect();

private boolean checkTransportOpened(TTransport transport) {
return !Objects.isNull(transport) && transport.isOpen();
}
public int execute(String stmt);

private ExecutionResponse internalExecuteQuery(String stmt) {
if (!checkTransportOpened(transport)) {
LOGGER.error("Thrift rpc call failed");
return null;
}
public int executeUpdate(String stmt);

try {
ExecutionResponse executionResponse = syncClient.execute(sessionId_, stmt);
if (executionResponse.getError_code() == ErrorCode.SUCCEEDED) {
return executionResponse;
} else {
LOGGER.error("execute error: " + executionResponse.getError_msg());
}
} catch (TException e) {
LOGGER.error("Thrift rpc call failed: " + e.getMessage());
}
return null;
}
public ResultSet executeQuery(String stmt) throws ConnectionException, NGQLException, TException;
}

This file was deleted.

Loading

0 comments on commit 39fc096

Please sign in to comment.