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

feat: add light java sdk #3642

Merged
merged 7 commits into from
Dec 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class SdkOption {
private int glogLevel = 0;
private String glogDir = "";
private int maxSqlCacheSize = 50;
private boolean isLight = false;

private void buildBaseOptions(BasicRouterOptions opt) {
opt.setEnable_debug(getEnableDebug());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@
public SqlException(String message) {
super(message);
}

public SqlException(String message, Throwable cause) {
super(message, cause);
}

Check warning on line 26 in java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/SqlException.java

View check run for this annotation

Codecov / codecov/patch

java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/SqlException.java#L25-L26

Added lines #L25 - L26 were not covered by tests

public SqlException(Throwable cause) {
super(cause);
}

Check warning on line 30 in java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/SqlException.java

View check run for this annotation

Codecov / codecov/patch

java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/SqlException.java#L29-L30

Added lines #L29 - L30 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,28 @@
private static final AtomicBoolean initialized = new AtomicBoolean(false);
private SQLRouter sqlRouter;
private DeploymentManager deploymentManager;
private ZKClient zkClient;
private InsertPreparedStatementCache insertCache;

public SqlClusterExecutor(SdkOption option, String libraryPath) throws SqlException {
initJavaSdkLibrary(libraryPath);

ZKClient zkClient = null;
if (option.isClusterMode()) {
SQLRouterOptions sqlOpt = option.buildSQLRouterOptions();
this.sqlRouter = sql_router_sdk.NewClusterSQLRouter(sqlOpt);
sqlOpt.delete();
zkClient = new ZKClient(ZKConfig.builder()
.cluster(option.getZkCluster())
.namespace(option.getZkPath())
.sessionTimeout((int)option.getSessionTimeout())
.build());
try {
if (!zkClient.connect()) {
throw new SqlException("zk client connect failed.");
if (!option.isLight()) {
zkClient = new ZKClient(ZKConfig.builder()
.cluster(option.getZkCluster())
.namespace(option.getZkPath())
.sessionTimeout((int)option.getSessionTimeout())
.build());
try {
if (!zkClient.connect()) {
throw new SqlException("zk client connect failed.");

Check warning on line 82 in java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/impl/SqlClusterExecutor.java

View check run for this annotation

Codecov / codecov/patch

java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/impl/SqlClusterExecutor.java#L82

Added line #L82 was not covered by tests
}
} catch (Exception e) {
throw new SqlException("init zk client failed", e);

Check warning on line 85 in java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/impl/SqlClusterExecutor.java

View check run for this annotation

Codecov / codecov/patch

java/openmldb-jdbc/src/main/java/com/_4paradigm/openmldb/sdk/impl/SqlClusterExecutor.java#L84-L85

Added lines #L84 - L85 were not covered by tests
}
} catch (Exception e) {
throw new SqlException("init zk client failed. " + e.getMessage());
}
} else {
StandaloneOptions sqlOpt = option.buildStandaloneOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

public class SQLRouterSmokeTest {
public static SqlExecutor clusterExecutor;
public static SqlExecutor lightClusterExecutor;
public static SqlExecutor standaloneExecutor;

static {
Expand All @@ -54,9 +55,10 @@ public class SQLRouterSmokeTest {
option.setZkCluster(TestConfig.ZK_CLUSTER);
option.setSessionTimeout(200000);
clusterExecutor = new SqlClusterExecutor(option);
java.sql.Statement state = clusterExecutor.getStatement();
state.execute("SET @@execute_mode='online';");
state.close();
setOnlineMode(clusterExecutor);
option.setLight(true);
lightClusterExecutor = new SqlClusterExecutor(option);
setOnlineMode(lightClusterExecutor);
// create standalone router
SdkOption standaloneOption = new SdkOption();
standaloneOption.setHost(TestConfig.HOST);
Expand All @@ -69,6 +71,16 @@ public class SQLRouterSmokeTest {
}
}

static void setOnlineMode(SqlExecutor executor) {
java.sql.Statement state = executor.getStatement();
try {
state.execute("SET @@execute_mode='online';");
state.close();
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
void testMoreOptions() throws Exception {
SdkOption option = new SdkOption();
Expand All @@ -82,7 +94,7 @@ void testMoreOptions() throws Exception {

@DataProvider(name = "executor")
public Object[] executor() {
return new Object[] { clusterExecutor, standaloneExecutor };
return new Object[] { clusterExecutor, lightClusterExecutor, standaloneExecutor };
}

@Test(dataProvider = "executor")
Expand Down Expand Up @@ -128,7 +140,7 @@ public void testSmoke(SqlExecutor router) {

// select
String select1 = "select * from tsql1010;";
SQLResultSet rs1 = (SQLResultSet) router .executeSQL(dbname, select1);
SQLResultSet rs1 = (SQLResultSet) router.executeSQL(dbname, select1);

Assert.assertEquals(2, rs1.GetInternalSchema().getColumnList().size());
Assert.assertEquals(Types.BIGINT, rs1.GetInternalSchema().getColumnType(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
option = new SdkOption();
option.setZkCluster(zkCluster);
option.setZkPath(zkPath);
option.setLight(true);

Check warning on line 56 in java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/OpenmldbSource.java

View check run for this annotation

Codecov / codecov/patch

java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/OpenmldbSource.java#L56

Added line #L56 was not covered by tests
String timeout = options.get("sessionTimeout");
if (timeout != null) {
option.setSessionTimeout(Integer.parseInt(timeout));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
SdkOption option = new SdkOption();
option.setZkCluster(config.zkCluster);
option.setZkPath(config.zkPath);
option.setLight(true);

Check warning on line 47 in java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/write/OpenmldbDataSingleWriter.java

View check run for this annotation

Codecov / codecov/patch

java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/write/OpenmldbDataSingleWriter.java#L47

Added line #L47 was not covered by tests
SqlClusterExecutor executor = new SqlClusterExecutor(option);
String dbName = config.dbName;
String tableName = config.tableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
SdkOption option = new SdkOption();
option.setZkCluster(config.zkCluster);
option.setZkPath(config.zkPath);
option.setLight(true);

Check warning on line 47 in java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/write/OpenmldbDataWriter.java

View check run for this annotation

Codecov / codecov/patch

java/openmldb-spark-connector/src/main/java/com/_4paradigm/openmldb/spark/write/OpenmldbDataWriter.java#L47

Added line #L47 was not covered by tests
SqlClusterExecutor executor = new SqlClusterExecutor(option);
String dbName = config.dbName;
String tableName = config.tableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class OpenmldbPartitionReader(config: OpenmldbReadConfig) extends PartitionReade
val option = new SdkOption
option.setZkCluster(config.zkCluster)
option.setZkPath(config.zkPath)
option.setLight(true)
val executor = new SqlClusterExecutor(option)
val dbName: String = config.dbName
val tableName: String = config.tableName
Expand Down
Loading