use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testTimeoutDuringAuth.
@Test
public void testTimeoutDuringAuth() throws Exception {
Path baseDir = folder.newFolder().toPath();
ServerConfiguration config = newServerConfigurationWithAutoPort(baseDir);
final AtomicBoolean suspendProcessing = new AtomicBoolean(false);
try (Server server = new Server(config) {
@Override
protected ServerSideConnectionPeer buildPeer(Channel channel) {
return new ServerSideConnectionPeer(channel, this) {
@Override
public void requestReceived(Pdu message, Channel channel) {
if (suspendProcessing.get()) {
LOG.log(Level.INFO, "dropping message type " + message.type + " id " + message.messageId);
message.close();
return;
}
super.requestReceived(message, channel);
}
};
}
}) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
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);
suspendProcessing.set(true);
try (HDBConnection connection2 = client.openConnection()) {
// auth will timeout
try {
connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test1"));
fail("insertion should fail");
} catch (Exception e) {
TestUtils.assertExceptionPresentInChain(e, HDBOperationTimeoutException.class);
}
suspendProcessing.set(false);
// new connection
assertEquals(1, connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test1")).updateCount);
}
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testClientCloseOnConnectionAndResumeTransaction.
@Test
public void testClientCloseOnConnectionAndResumeTransaction() throws Exception {
Path baseDir = folder.newFolder().toPath();
AtomicInteger connectionToUse = new AtomicInteger();
AtomicReference<ClientSideConnectionPeer[]> connections = new AtomicReference<>();
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 2);
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);
LOG.log(Level.INFO, "chooseConnection among " + all.length + " connections, getting " + connectionToUse);
return all[connectionToUse.get()];
}
};
registerConnection(con);
return con;
}
};
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
// force using the first connection of two
connectionToUse.set(0);
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);
// transaction is bound to the first connection (in HerdDB 11.0.0)
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);
// close the connection that initiated the transaction
connections.get()[0].close();
// give time to the server to close the connection
Thread.sleep(100);
// use the second connection, with the same transaction
connectionToUse.set(1);
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(2, "test1"));
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testKeepReadLocks.
@Test
public void testKeepReadLocks() 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);
// new transaction
tx = connection.beginTransaction(TableSpace.DEFAULT);
// do not keep locks
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), tx, 0, 10, false)) {
Map<String, Object> record = scan.consume().get(0);
assertEquals(RawString.of("test"), record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
assertNull(transaction.lookupLock("mytable", Bytes.from_string("test")));
}
// keep locks
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), tx, 0, 10, true)) {
Map<String, Object> record = scan.consume().get(0);
assertEquals(RawString.of("test"), record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
assertFalse(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
}
// upgrade lock to write
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test' FOR UPDATE", true, Collections.emptyList(), tx, 0, 10, false)) {
Map<String, Object> record = scan.consume().get(0);
assertEquals(RawString.of("test"), record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
assertTrue(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
}
connection.rollbackTransaction(TableSpace.DEFAULT, tx);
// new transaction
tx = connection.beginTransaction(TableSpace.DEFAULT);
// SELECT FOR UPDATE must hold WRITE LOCK even with keepLocks = false
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test' FOR UPDATE", true, Collections.emptyList(), tx, 0, 10, false)) {
Map<String, Object> record = scan.consume().get(0);
assertEquals(RawString.of("test"), record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
assertTrue(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
}
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method test.
@Test
public void test() throws Exception {
Path baseDir = folder.newFolder().toPath();
String _baseDir = baseDir.toString();
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);
{
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, 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'", tx, true, Collections.emptyList());
Map<RawString, Object> record = res.data;
Assert.assertNotNull(record);
}
List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", tx, 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);
assertEquals(tx, executeUpdates.get(0).transactionId);
assertEquals(tx, executeUpdates.get(1).transactionId);
assertEquals(tx, executeUpdates.get(2).transactionId);
connection.commitTransaction(TableSpace.DEFAULT, tx);
try (ScanResultSet scan = connection.executeScan(server.getManager().getVirtualTableSpaceId(), "SELECT * FROM sysconfig", true, Collections.emptyList(), 0, 0, 10, true)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
RawString name = (RawString) aa.get("name");
if (RawString.of("server.base.dir").equals(name)) {
assertEquals(RawString.of("server.base.dir"), name);
RawString value = (RawString) aa.get("value");
assertEquals(RawString.of(_baseDir), value);
} else if (RawString.of("server.port").equals(name)) {
RawString value = (RawString) aa.get("value");
assertEquals(RawString.of("0"), value);
} else {
assertEquals(RawString.of("server.node.id"), name);
}
}
}
try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", true, Collections.emptyList(), 0, 0, 10, true)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
assertEquals(RawString.of("jvm-local"), aa.get("address"));
assertNotNull(aa.get("id"));
assertEquals(RawString.of(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT), aa.get("username"));
assertNotNull(aa.get("connectionts"));
}
assertTrue(all.size() >= 1);
}
List<DMLResult> executeUpdatesWithoutParams = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=1 WHERE id='test'", -1, false, true, Arrays.asList(// empty list of parameters
Arrays.asList()));
assertEquals(1, executeUpdatesWithoutParams.size());
assertEquals(1, executeUpdatesWithoutParams.get(0).updateCount);
// missing JDBC parameter
try {
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList("test"));
fail("cannot issue a query without setting each parameter");
} catch (HDBException ok) {
assertTrue(ok.getMessage().contains(MissingJDBCParameterException.class.getName()));
}
// simple rollback
long txToRollback = connection.beginTransaction(TableSpace.DEFAULT);
long countInsertToRollback = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", txToRollback, false, true, Arrays.asList("test123", 7, 8)).updateCount;
Assert.assertEquals(1, countInsertToRollback);
connection.rollbackTransaction(TableSpace.DEFAULT, txToRollback);
}
}
}
use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.
the class SimpleClientServerTest method testSQLIntegrityViolation.
@Test
public void testSQLIntegrityViolation() 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 int primary key, s1 string)", 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,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test1")).updateCount;
Assert.assertEquals(1, countInsert);
try {
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test2"));
} catch (Exception ex) {
Assert.assertTrue(ex.getMessage().contains("SQLIntegrityConstraintViolationException"));
}
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(2, "test2"));
connection.commitTransaction(TableSpace.DEFAULT, tx);
try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM herd.mytable", true, Collections.emptyList(), 0, 0, 10, true)) {
List<Map<String, Object>> rows = scan.consume();
int i = 0;
for (Map<String, Object> row : rows) {
if (i == 0) {
i++;
Assert.assertEquals(row.get("id"), 1);
Assert.assertEquals(row.get("s1"), "test1");
} else {
Assert.assertEquals(row.get("id"), 2);
Assert.assertEquals(row.get("s1"), "test2");
}
}
}
}
}
}
Aggregations