Skip to content

Commit

Permalink
add an example of cancel operation
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Apr 2, 2024
1 parent b340f41 commit 58e4118
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 2 deletions.
16 changes: 14 additions & 2 deletions modules/clients/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ tasks.register('runServiceClientCollector', JavaExec) {

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

systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', findProperty('loglevel') ?: 'info'
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'
Expand All @@ -80,7 +80,19 @@ tasks.register('runHandshakeExample', JavaExec) {

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

systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', findProperty('loglevel') ?: 'info'
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('runCancel', JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'com.tsurugidb.tsubakuro.examples.cancel.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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.tsurugidb.tsubakuro.examples.cancel;

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

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

public class Insert {
final String url;
SqlClient sqlClient;
Session session;

public Insert(String url) throws IOException, ServerException, InterruptedException, TimeoutException {
this.url = url;
session = SessionBuilder.connect(url)
.withCredential(new UsernamePasswordCredential("user", "pass"))
.create(10, TimeUnit.SECONDS);
sqlClient = SqlClient.attach(session);
}

public void createTable() throws IOException, ServerException, InterruptedException {
String createTable = "CREATE TABLE ORDERS (o_id BIGINT NOT NULL, o_d_id BIGINT NOT NULL, o_w_id BIGINT NOT NULL, o_c_id BIGINT NOT NULL, o_entry_d CHAR(25) NOT NULL, o_carrier_id BIGINT, o_ol_cnt BIGINT NOT NULL, o_all_local BIGINT NOT NULL, PRIMARY KEY(o_w_id, o_d_id, o_id))";
String createIndex = "CREATE INDEX ORDERS_SECONDARY ON ORDERS (o_w_id, o_d_id, o_c_id, o_id)";
try (Transaction transaction = sqlClient.createTransaction().await()) {
try {
transaction.executeStatement(createTable).get();
transaction.executeStatement(createIndex).get();
transaction.commit().get();
} catch (ServerException e) {
transaction.rollback().get();
}
}
}

public void prepareAndInsert() throws IOException, ServerException, InterruptedException {
String sql = "INSERT INTO ORDERS (o_id, o_d_id, o_w_id, o_c_id, o_entry_d, o_carrier_id, o_ol_cnt, o_all_local) VALUES (:o_id, :o_d_id, :o_w_id, :o_c_id, :o_entry_d, :o_carrier_id, :o_ol_cnt, :o_all_local)";
try (var preparedStatement = sqlClient.prepare(sql,
Placeholders.of("o_id", long.class),
Placeholders.of("o_d_id", long.class),
Placeholders.of("o_w_id", long.class),
Placeholders.of("o_c_id", long.class),
Placeholders.of("o_entry_d", String.class),
Placeholders.of("o_carrier_id", long.class),
Placeholders.of("o_ol_cnt", long.class),
Placeholders.of("o_all_local", long.class)).get();

Transaction transaction = sqlClient.createTransaction().await()) {

try {
var result = transaction.executeStatement(preparedStatement,
Parameters.of("o_id", (long) 99999999),
Parameters.of("o_d_id", (long) 3),
Parameters.of("o_w_id", (long) 1),
Parameters.of("o_c_id", (long) 1234),
Parameters.of("o_entry_d", "20210620"),
Parameters.of("o_carrier_id", (long) 3),
Parameters.of("o_ol_cnt", (long) 7),
Parameters.of("o_all_local", (long) 0));
result.close();
transaction.commit().get();
} catch (ServerException e) {
transaction.rollback().get();
}
}
}
public void insertByText() throws IOException, ServerException, InterruptedException {
// String sql = "INSERT INTO ORDERS (o_id, o_d_id, o_w_id, o_c_id, o_entry_d, o_carrier_id, o_ol_cnt, o_all_local) VALUES (99999999, 3, 1, 5678, '20221120', 2, 6, 0)";
String sql = "INSERT INTO ORDERS VALUES (99999999, 3, 1, 5678, '20221120', 2, 6, 0)";
try (Transaction transaction = sqlClient.createTransaction().await()) {
try {
var result = transaction.executeStatement(sql).get(100, TimeUnit.SECONDS);
transaction.commit().get();
// } catch (ServerException | TimeoutException e) {
} catch (ServerException | TimeoutException e) {
System.out.println(e);
transaction.rollback().get();
}
}
}

public void close() throws IOException, ServerException, InterruptedException {
session.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.tsurugidb.tsubakuro.examples.cancel;

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

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.exception.ServerException;
import com.tsurugidb.tsubakuro.sql.SqlClient;

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 selectOnly = false;
private static boolean textInsert = false;
private static int loopCount = 1;
private static int selectCount = 1;
private static int threadCount = 1;
private static int sleepSeconds = 0;
private static int sessionCount = 1;
private static boolean suppressDisplay = false;
private static long timeout = 5000;

public static void main(String[] args) {
// コマンドラインオプションの設定
Options options = new Options();

options.addOption(Option.builder("s").argName("select").desc("Select only mode.").build());
options.addOption(Option.builder("t").argName("text_insert").desc("Do Insert by text SQL.").build());
options.addOption(Option.builder("c").argName("concurrency").hasArg().desc("Specify the number of threads conducting the select operation.").build());
options.addOption(Option.builder("n").argName("number").hasArg().desc("Specify the execution count of the select operation.").build());
options.addOption(Option.builder("l").argName("loops").hasArg().desc("Specify the number of loop count of the thread invocation.").build());
options.addOption(Option.builder("p").argName("pause").hasArg().desc("Sleep specified time before session close.").build());
options.addOption(Option.builder("d").argName("display").desc("No result printout.").build());
options.addOption(Option.builder("m").argName("session number").hasArg().desc("The number of session.").build());
options.addOption(Option.builder("o").argName("timeout").hasArg().desc("timeout value.").build());

CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;

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

if (cmd.hasOption("s")) {
selectOnly = true;
System.err.println("select only");
}
if (cmd.hasOption("t")) {
textInsert = true;
System.err.println("text insert");
}
if (cmd.hasOption("n")) {
selectCount = Integer.parseInt(cmd.getOptionValue("n"));
System.err.println("select count = " + selectCount);
}
if (cmd.hasOption("c")) {
threadCount = Integer.parseInt(cmd.getOptionValue("c"));
System.err.println("thread count = " + threadCount);
}
if (cmd.hasOption("l")) {
loopCount = Integer.parseInt(cmd.getOptionValue("l"));
System.err.println("loop count = " + loopCount);
}
if (cmd.hasOption("p")) {
sleepSeconds = Integer.parseInt(cmd.getOptionValue("p"));
System.err.println("sleep before session close for " + sleepSeconds + " seconds");
}
if (cmd.hasOption("d")) {
suppressDisplay = true;
System.err.println("No result display");
}
if (cmd.hasOption("m")) {
sessionCount = Integer.parseInt(cmd.getOptionValue("m"));
System.err.println("Session count = " + sessionCount);
}
if (cmd.hasOption("o")) {
timeout = Long.parseLong(cmd.getOptionValue("o")) * 1000;
System.err.println("tmeout = " + timeout);
}
} catch (ParseException e) {
System.err.println("cmd parser failed." + e);
}

for (int i = 0; i < sessionCount; i++ ) {
if (sessionCount > 1) {
System.out.println("======== session no. " + (i + 1) + " ========");
}
try {
if (!selectOnly) {
var insert = new Insert(url);
insert.createTable();
if (!textInsert) {
insert.prepareAndInsert();
} else {
insert.insertByText();
}
insert.close();
}
var select = new Select(url, loopCount, selectCount, threadCount, suppressDisplay, sleepSeconds, timeout);
select.prepareAndSelect();
} catch (IOException | ServerException | InterruptedException | TimeoutException e) {
System.out.println(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.tsurugidb.tsubakuro.examples.cancel;

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

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

public class Select {
final String url;
final int loopCount;
final int selectCount;
final int threadCount;
final boolean suppressDisplay;
final int sleepSeconds;
final long timeout;

static class ThreadForSelect extends Thread {
final String url;
final int id;
final int selectCount;
final boolean suppressDisplay;
final int sleepSeconds;
final long timeout;

ThreadForSelect(String url, int id, int selectCount, boolean suppressDisplay, int sleepSeconds, long timeout) {
this.url = url;
this.id = id;
this.selectCount = selectCount;
this.suppressDisplay = suppressDisplay;
this.sleepSeconds = sleepSeconds;
this.timeout = timeout;
}
public void run() {
String sql = "SELECT * FROM ORDERS WHERE o_w_id = :o_w_id AND o_d_id = :o_d_id AND o_id = :o_id";
try (
Session session = SessionBuilder.connect(url)
.withCredential(new UsernamePasswordCredential("user", "pass"))
.create(10, TimeUnit.SECONDS);
SqlClient sqlClient = SqlClient.attach(session);
var preparedStatement = sqlClient.prepare(sql,
Placeholders.of("o_id", long.class),
Placeholders.of("o_d_id", long.class),
Placeholders.of("o_w_id", long.class)).get(timeout, TimeUnit.MILLISECONDS);

var transaction = sqlClient.createTransaction().get(timeout, TimeUnit.MILLISECONDS)) {

for (int i = 0; i < selectCount; i++) {
var future = transaction.executeQuery(preparedStatement,
Parameters.of("o_id", (long) 99999999),
Parameters.of("o_d_id", (long) 3),
Parameters.of("o_w_id", (long) 1));
future.close();
}
transaction.commit().await();

if (sleepSeconds > 0) {
Thread.sleep(sleepSeconds * 1000);
}

} catch (ServerException | InterruptedException | IOException | TimeoutException e) {
System.err.println(e);
e.printStackTrace();
return;
}
}
}

public Select(String url, int loopCount, int selectCount, int threadCount, boolean suppressDisplay, int sleepSeconds, long timeout) throws IOException, ServerException, InterruptedException {
this.url = url;
this.loopCount = loopCount;
this.selectCount = selectCount;
this.threadCount = threadCount;
this.suppressDisplay = suppressDisplay;
this.sleepSeconds = sleepSeconds;
this.timeout = timeout;
}

public void prepareAndSelect() throws IOException, ServerException, InterruptedException {
Thread[] threads = new Thread[threadCount];
for (int j = 0; j < loopCount; j++) {
for (int i = 0; i < threadCount; i++) {
threads[i] = new ThreadForSelect(url, i, selectCount, suppressDisplay, sleepSeconds, timeout);
threads[i].start();
}
for (int i = 0; i < threadCount; i++) {
threads[i].join();
}
}
}
}

0 comments on commit 58e4118

Please sign in to comment.