use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testClientAbandonedTransaction.
@Test
public void testClientAbandonedTransaction() throws Exception {
Path baseDir = folder.newFolder().toPath();
ServerConfiguration config = newServerConfigurationWithAutoPort(baseDir);
config.set(ServerConfiguration.PROPERTY_ABANDONED_TRANSACTIONS_TIMEOUT, 5000);
try (Server server = new Server(config)) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
long tx = connection.beginTransaction(TableSpace.DEFAULT);
assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test1")).updateCount);
assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set s1=?", tx, false, true, Arrays.asList("test2")).updateCount);
// the client dies, it won't use the transaction any more
}
TableSpaceManager tableSpaceManager = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
assertFalse(tableSpaceManager.getTransactions().isEmpty());
// PROPERTY_ABANDONED_TRANSACTIONS_TIMEOUT+ 1000
Thread.sleep(6000);
server.getManager().checkpoint();
assertTrue(tableSpaceManager.getTransactions().isEmpty());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertEquals(0, connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set s1=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList("test3")).updateCount);
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testExecuteUpdatesWithDDL.
@Test
public void testExecuteUpdatesWithDDL() throws Exception {
Path baseDir = folder.newFolder().toPath();
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
{
List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(Collections.emptyList()));
assertEquals(1, executeUpdates.size());
assertEquals(1, executeUpdates.get(0).updateCount);
}
{
List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "DROP TABLE mytable", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(Collections.emptyList()));
assertEquals(1, executeUpdates.size());
assertEquals(1, executeUpdates.get(0).updateCount);
}
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testEnsureOpen.
@Test
public void testEnsureOpen() throws Exception {
Path baseDir = folder.newFolder().toPath();
AtomicReference<ClientSideConnectionPeer[]> connections = new AtomicReference<>();
ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.getNetworkServer().setEnableJVMNetwork(false);
server.getNetworkServer().setEnableRealNetwork(true);
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
try (HDBClient client = new HDBClient(clientConfiguration) {
@Override
public HDBConnection openConnection() {
HDBConnection con = new HDBConnection(this) {
@Override
protected ClientSideConnectionPeer chooseConnection(ClientSideConnectionPeer[] all) {
connections.set(all);
return all[0];
}
};
registerConnection(con);
return con;
}
};
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", 0, false, true, Arrays.asList(1, "test1")).updateCount);
// assert we are using real network
assertNotEquals(NettyChannel.ADDRESS_JVM_LOCAL, connections.get()[0].getChannel().getRemoteAddress());
io.netty.channel.Channel socket = ((NettyChannel) connections.get()[0].getChannel()).getSocket();
assertThat(socket, instanceOf(SocketChannel.class));
// invalidate socket connection (simulate network error)
socket.close().await();
// ensure reconnection is performed (using prepared statement)
connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable ", true, Collections.emptyList(), 0, 100, 1000, true).close();
// assert we are using real network
assertNotEquals(NettyChannel.ADDRESS_JVM_LOCAL, connections.get()[0].getChannel().getRemoteAddress());
io.netty.channel.Channel socket2 = ((NettyChannel) connections.get()[0].getChannel()).getSocket();
assertThat(socket2, instanceOf(SocketChannel.class));
assertNotSame(socket2, socket);
// invalidate socket connection (simulate network error)
socket2.close().await();
// ensure reconnection is performed (not using prepared statement)
connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable ", false, Collections.emptyList(), 0, 100, 1000, true).close();
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testHandleReadTimeout.
@Test
public void testHandleReadTimeout() throws Exception {
Path baseDir = folder.newFolder().toPath();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
final int timeout = 1000;
final AtomicInteger channelCreatedCount = new AtomicInteger();
clientConfiguration.set(ClientConfiguration.PROPERTY_NETWORK_TIMEOUT, timeout);
try (HDBClient client = new HDBClient(clientConfiguration) {
@Override
Channel createChannelTo(ServerHostData server, ChannelEventListener eventReceiver) throws IOException {
channelCreatedCount.incrementAndGet();
return super.createChannelTo(server, eventReceiver);
}
}) {
try (HDBConnection connection1 = client.openConnection()) {
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.start();
server.waitForStandaloneBoot();
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection1.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
channelCreatedCount.set(0);
long resultCreateTable = connection1.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, false, /*usePreparedStatement*/
Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
assertEquals(0, channelCreatedCount.get());
// wait for client side timeout
// the only connected socket will be disconnected
Thread.sleep(timeout + 1000);
connection1.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", false, /*usePreparedStatement*/
Collections.emptyList(), 0, 0, 10, true).close();
assertEquals(1, channelCreatedCount.get());
}
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testCachePreparedStatementsPrepareSqlAgain.
/**
* Testing that if the server discards the query the client will resend the PREPARE_STATEMENT COMMAND
*
* @throws Exception
*/
@Test
public void testCachePreparedStatementsPrepareSqlAgain() throws Exception {
Path baseDir = folder.newFolder().toPath();
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
{
long tx = connection.beginTransaction(TableSpace.DEFAULT);
long countInsert = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, true, Arrays.asList("test", 1, 2)).updateCount;
Assert.assertEquals(1, countInsert);
long countInsert2 = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, true, Arrays.asList("test2", 2, 3)).updateCount;
Assert.assertEquals(1, countInsert2);
connection.commitTransaction(TableSpace.DEFAULT, tx);
}
// SCAN
{
try (ScanResultSet res = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), TransactionContext.NOTRANSACTION_ID, 100, 100, true)) {
assertEquals(1, res.consume().size());
}
}
server.getManager().getPreparedStatementsCache().clear();
{
try (ScanResultSet res = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), TransactionContext.NOTRANSACTION_ID, 100, 100, true)) {
assertEquals(1, res.consume().size());
}
}
// GET
{
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", TransactionContext.NOTRANSACTION_ID, true, Collections.emptyList());
Map<RawString, Object> record = res.data;
Assert.assertNotNull(record);
assertEquals(RawString.of("test"), record.get(RawString.of("id")));
assertEquals(Long.valueOf(1), record.get(RawString.of("n1")));
assertEquals(Integer.valueOf(2), record.get(RawString.of("n2")));
}
server.getManager().getPreparedStatementsCache().clear();
{
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", TransactionContext.NOTRANSACTION_ID, true, Collections.emptyList());
Map<RawString, Object> record = res.data;
Assert.assertNotNull(record);
}
// EXECUTE UPDATES
{
List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(Arrays.asList(1, "test"), Arrays.asList(2, "test2"), Arrays.asList(3, "test_not_exists")));
assertEquals(3, executeUpdates.size());
assertEquals(1, executeUpdates.get(0).updateCount);
assertEquals(1, executeUpdates.get(1).updateCount);
assertEquals(0, executeUpdates.get(2).updateCount);
}
server.getManager().getPreparedStatementsCache().clear();
{
List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(Arrays.asList(1, "test"), Arrays.asList(2, "test2"), Arrays.asList(3, "test_not_exists")));
assertEquals(3, executeUpdates.size());
assertEquals(1, executeUpdates.get(0).updateCount);
assertEquals(1, executeUpdates.get(1).updateCount);
assertEquals(0, executeUpdates.get(2).updateCount);
}
// EXECUTE UPDATE
{
DMLResult executeUpdate = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test"));
assertEquals(1, executeUpdate.updateCount);
}
server.getManager().getPreparedStatementsCache().clear();
{
DMLResult executeUpdate = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test"));
assertEquals(1, executeUpdate.updateCount);
}
}
}
}
Aggregations