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

Wip/i 75 #25

Merged
merged 4 commits into from
May 27, 2024
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
24 changes: 24 additions & 0 deletions modules/clients/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,27 @@ tasks.register('runSession', JavaExec) {
systemProperty 'org.slf4j.simpleLogger.showShortLogName', 'true'
systemProperty 'org.slf4j.simpleLogger.levelInBrackets', 'true'
}

tasks.register('runShutdown', JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'com.tsurugidb.tsubakuro.examples.sessionShutdown.Main'

systemProperty 'tsurugi.dbname', findProperty('tsurugi.dbname') ?: 'ipc:tsurugi'

systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', findProperty('loglevel') ?: 'info'
systemProperty 'org.slf4j.simpleLogger.showThreadName', 'false'
systemProperty 'org.slf4j.simpleLogger.showShortLogName', 'true'
systemProperty 'org.slf4j.simpleLogger.levelInBrackets', 'true'
}

tasks.register('runLongQuery', JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'com.tsurugidb.tsubakuro.examples.longQuery.Main'

systemProperty 'tsurugi.dbname', findProperty('tsurugi.dbname') ?: 'ipc:tsurugi'

systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', findProperty('loglevel') ?: 'info'
systemProperty 'org.slf4j.simpleLogger.showThreadName', 'false'
systemProperty 'org.slf4j.simpleLogger.showShortLogName', 'true'
systemProperty 'org.slf4j.simpleLogger.levelInBrackets', 'true'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.tsurugidb.tsubakuro.examples.longQuery;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.text.MessageFormat;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.tsurugidb.tsubakuro.common.Session;
import com.tsurugidb.tsubakuro.common.SessionBuilder;
import com.tsurugidb.tsubakuro.common.ShutdownType;
import com.tsurugidb.tsubakuro.channel.common.connection.UsernamePasswordCredential;
import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.sql.SqlClient;

// Prior to executing this program, the following create table and insert to table must be executed.
// DROP TABLE IF EXISTS TBL01;
// CREATE TABLE TBL01(pk INT PRIMARY KEY);
// INSERT INTO TBL01 VALUES(1);
// INSERT INTO TBL01 VALUES(2);
// ......
// INSERT INTO TBL01 VALUES(n);
// DROP TABLE IF EXISTS TBL02;
// CREATE TABLE TBL02(pk INT PRIMARY KEY);
// INSERT INTO TBL02 VALUES(1);
// INSERT INTO TBL02 VALUES(2);
// ......
// INSERT INTO TBL02 VALUES(n);
// where n is a value of about 20 to 25
public final class Main {
private static long timeout = 5000;
private Main(String[] args) {
}
// set dbname as follows
// -Ptsurugi.dbname=ipc:tsurugi or
// -Ptsurugi.dbname=tcp://localhost:12345
// when run this example by `./gradlew run` command
private static String url = System.getProperty("tsurugi.dbname");

private static boolean forceful = false;
private static boolean requestShutdown = true;

public static void main(String[] args) {
try (var session = SessionBuilder.connect(url)
.withCredential(new UsernamePasswordCredential("user", "pass"))
.create(10, TimeUnit.SECONDS);
SqlClient sqlClient = SqlClient.attach(session);
var transaction = sqlClient.createTransaction().get(timeout, TimeUnit.MILLISECONDS)) {

System.out.println("---- program begin with using " + url + " to connect tsurugidb ----");
try {
System.out.println("---- ( start query 1 ) ----");
long start = System.currentTimeMillis();
Select.query(transaction, "SELECT * FROM TBL02,TBL02,TBL02,TBL02,TBL02,TBL01 WHERE TBL01.pk=-1");
System.out.println("---- ( end query 1 in " + (System.currentTimeMillis() - start) + " milli sec. ) ----");
} finally {
System.out.println("---- ( start query 2 ) ----");
long start = System.currentTimeMillis();
Select.query(transaction, "SELECT * FROM TBL02,TBL02,TBL02,TBL02,TBL01 WHERE TBL01.pk=-1");
System.out.println("---- ( end query 2 in " + (System.currentTimeMillis() - start) + " milli sec. ) ----");
}
System.out.println("---- ( commit ) ----");
transaction.commit().await();
} catch (IOException | ServerException | InterruptedException | TimeoutException e) {
System.err.println(e);
} finally {
System.out.println("---- program end ----");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.tsurugidb.tsubakuro.examples.longQuery;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.sql.Transaction;
import com.tsurugidb.tsubakuro.sql.ResultSet;

public class Select {
private static long timeout = 5000;

static void query(Transaction transaction, String query, boolean suppressDisplay) {
try (var resultSet = transaction.executeQuery(query).get(timeout, TimeUnit.MILLISECONDS)) {
if (!suppressDisplay) {
printResultset(resultSet);
}
} catch (ServerException | InterruptedException | IOException | TimeoutException e) {
// Intentionally grasps exceptions to allow the caller to continue processing
System.err.println(e);
}
}
static void query(Transaction transaction, String query) {
query(transaction, query, false);
}

static void printResultset(ResultSet resultSet) throws InterruptedException, IOException, ServerException {
int count = 1;

while (resultSet.nextRow()) {
System.out.println("---- ( count: " + count + " ) ----");
count++;
int columnIndex = 0;
var metadata = resultSet.getMetadata().getColumns();
while (resultSet.nextColumn()) {
if (!resultSet.isNull()) {
switch (metadata.get(columnIndex).getAtomType()) {
case INT4:
System.out.println(resultSet.fetchInt4Value());
break;
case INT8:
System.out.println(resultSet.fetchInt8Value());
break;
case FLOAT4:
System.out.println(resultSet.fetchFloat4Value());
break;
case FLOAT8:
System.out.println(resultSet.fetchFloat8Value());
break;
case CHARACTER:
System.out.println(resultSet.fetchCharacterValue());
break;
default:
throw new IOException("the column type is invalid");
}
} else {
System.out.println("the column is NULL");
}
columnIndex++;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.tsurugidb.tsubakuro.examples.sessionShutdown;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.text.MessageFormat;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.tsurugidb.tsubakuro.common.Session;
import com.tsurugidb.tsubakuro.common.SessionBuilder;
import com.tsurugidb.tsubakuro.common.ShutdownType;
import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.channel.common.connection.UsernamePasswordCredential;

public final class Main {
private Main(String[] args) {
}

// set dbname as follows
// -Ptsurugi.dbname=ipc:tsurugi or
// -Ptsurugi.dbname=tcp://localhost:12345
// when run this example by `./gradlew run` command
private static String url = System.getProperty("tsurugi.dbname");

private static boolean forceful = false;
private static boolean requestShutdown = true;

public static void main(String[] args) {
// コマンドラインオプションの設定
Options options = new Options();
options.addOption(Option.builder("f").argName("forceful").desc("forceful shutdown").build());
options.addOption(Option.builder("n").argName("noShutdown").desc("do not request shutdown").build());
CommandLineParser parser = new DefaultParser();

try {
CommandLine cmd = parser.parse(options, args);

if (cmd.hasOption("f")) {
forceful = true;
System.err.println("forceful shutdown");
}
if (cmd.hasOption("n")) {
requestShutdown = false;
System.err.println("no shutdown request");
}
} catch (ParseException e) {
System.err.println("cmd parser failed." + e);
System.exit(0);
}

try (var session = SessionBuilder.connect(url)
.withCredential(new UsernamePasswordCredential("user", "pass"))
.create(10, TimeUnit.SECONDS);
) {

if (requestShutdown) {
Thread.sleep(2000);
session.shutdown(forceful ? ShutdownType.FORCEFUL : ShutdownType.GRACEFUL).get();
}

while (session.isAlive()) {
Thread.sleep(2000);
System.out.println("session is still alive");
}

} catch (IOException | ServerException | InterruptedException | TimeoutException e) {
System.out.println(e);
}
System.out.println("session shutdown has been completed");
}
}