use of herddb.client.ClientConfiguration in project herddb by diennea.
the class JmxTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
{
final ObjectName statusBeanName = new ObjectName("herddb.server:type=Table,Name=" + TableSpace.DEFAULT + ".mytable");
Object attribute = ManagementFactory.getPlatformMBeanServer().getAttribute(statusBeanName, "Tablesize");
assertEquals(0L, attribute);
}
{
final ObjectName statusBeanName = new ObjectName("herddb.server:type=TableSpace,Name=" + TableSpace.DEFAULT);
Object attribute = ManagementFactory.getPlatformMBeanServer().getAttribute(statusBeanName, "Tablesize");
assertEquals(0L, attribute);
}
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class MultipleConcurrentUpdatesTest method performTest.
private void performTest(boolean useTransactions, long checkPointPeriod, boolean withIndexes) throws Exception {
Path baseDir = folder.newFolder().toPath();
ServerConfiguration serverConfiguration = new ServerConfiguration(baseDir);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_DATA_MEMORY, 1024 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_PK_MEMORY, 1024 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, checkPointPeriod);
serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
ConcurrentHashMap<String, Long> expectedValue = new ConcurrentHashMap<>();
try (Server server = new Server(serverConfiguration)) {
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));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
if (withIndexes) {
long resultCreateIndex = connection.executeUpdate(TableSpace.DEFAULT, "CREATE INDEX theindex ON mytable (n1 long)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateIndex);
}
long tx = connection.beginTransaction(TableSpace.DEFAULT);
for (int i = 0; i < TABLESIZE; i++) {
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test_" + i, 1, 2));
expectedValue.put("test_" + i, 1L);
}
connection.commitTransaction(TableSpace.DEFAULT, tx);
ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
try {
List<Future> futures = new ArrayList<>();
AtomicLong updates = new AtomicLong();
AtomicLong skipped = new AtomicLong();
AtomicLong gets = new AtomicLong();
for (int i = 0; i < TABLESIZE * MULTIPLIER; i++) {
futures.add(threadPool.submit(new Runnable() {
@Override
public void run() {
try {
boolean update = ThreadLocalRandom.current().nextBoolean();
int k = ThreadLocalRandom.current().nextInt(TABLESIZE);
long value = ThreadLocalRandom.current().nextInt(TABLESIZE);
long transactionId;
String key = "test_" + k;
Long actual = expectedValue.remove(key);
if (actual == null) {
// another thread working on this entry, skip
skipped.incrementAndGet();
return;
}
if (update) {
updates.incrementAndGet();
DMLResult updateResult = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n1=? WHERE id=?", useTransactions ? TransactionContext.AUTOTRANSACTION_ID : TransactionContext.NOTRANSACTION_ID, false, Arrays.asList(value, "test_" + k));
long count = updateResult.updateCount;
transactionId = updateResult.transactionId;
if (count <= 0) {
throw new RuntimeException("not updated ?");
}
} else {
gets.incrementAndGet();
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable where id=?", useTransactions ? TransactionContext.AUTOTRANSACTION_ID : TransactionContext.NOTRANSACTION_ID, Arrays.asList("test_" + k));
if (res.data == null) {
throw new RuntimeException("not found?");
}
if (!res.data.get("n1").equals(actual)) {
throw new RuntimeException("unspected value " + res.data + ", expected: " + actual);
}
transactionId = res.transactionId;
// value did not change actually
value = actual;
}
if (useTransactions) {
if (transactionId <= 0) {
throw new RuntimeException("no transaction ?");
}
connection.commitTransaction(TableSpace.DEFAULT, transactionId);
}
expectedValue.put(key, value);
} catch (Exception err) {
throw new RuntimeException(err);
}
}
}));
}
for (Future f : futures) {
f.get();
}
System.out.println("stats::updates:" + updates);
System.out.println("stats::get:" + gets);
System.out.println("stats::skipped:" + skipped);
assertTrue(updates.get() > 0);
assertTrue(gets.get() > 0);
List<String> erroredKeys = new ArrayList<>();
for (Map.Entry<String, Long> entry : expectedValue.entrySet()) {
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT n1 FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, Arrays.asList(entry.getKey()));
assertNotNull(res.data);
if (!entry.getValue().equals(res.data.get("n1"))) {
if (!entry.getValue().equals(res.data.get("n1"))) {
System.out.println("expected value " + res.data.get("n1") + ", but got " + Long.valueOf(entry.getValue()) + " for key " + entry.getKey());
erroredKeys.add(entry.getKey());
}
}
}
assertTrue(erroredKeys.isEmpty());
TableManagerStats stats = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTableManager("mytable").getStats();
System.out.println("stats::tablesize:" + stats.getTablesize());
System.out.println("stats::dirty records:" + stats.getDirtyrecords());
System.out.println("stats::unload count:" + stats.getUnloadedPagesCount());
System.out.println("stats::load count:" + stats.getLoadedPagesCount());
System.out.println("stats::buffers used mem:" + stats.getBuffersUsedMemory());
assertTrue(stats.getUnloadedPagesCount() > 0);
assertEquals(TABLESIZE, stats.getTablesize());
} finally {
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.MINUTES);
}
}
}
// restart and recovery
try (Server server = new Server(serverConfiguration)) {
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));
for (Map.Entry<String, Long> entry : expectedValue.entrySet()) {
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT n1 FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, Arrays.asList(entry.getKey()));
assertNotNull(res.data);
assertEquals(entry.getValue(), res.data.get("n1"));
}
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class RollbackOnCloseConnectionTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
server.getNetworkServer().setEnableJVMNetwork(false);
server.start();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
long tx;
try (HDBConnection connection = client.openConnection()) {
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
tx = connection.beginTransaction(TableSpace.DEFAULT);
long countInsert = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test", 1, 2)).updateCount;
Assert.assertEquals(1, countInsert);
assertTrue(server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getOpenTransactions().contains(tx));
}
for (int i = 0; i < 100; i++) {
if (!server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getOpenTransactions().contains(tx)) {
break;
}
Thread.sleep(100);
}
assertFalse(server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getOpenTransactions().contains(tx));
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class MixedCaseIdentifiersTest method testUpdate.
@Test
public void testUpdate() throws Exception {
try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
Connection con = dataSource.getConnection();
Statement create = con.createStatement();
PreparedStatement statement_insert = con.prepareStatement("INSERT INTO q1_MESSAGE(msg_id,STATUS,recipient) values(?,?,?)");
PreparedStatement statement_update = con.prepareStatement("UPDATE q1_MESSAGE SET STATUS = 2,lastbouncecategory=null WHERE MSG_ID = ? and (status=1 or status=5)")) {
create.execute(CREATE_TABLE);
long msg_id = 213;
statement_insert.setLong(1, msg_id);
statement_insert.setInt(2, 1);
statement_insert.setString(3, "test@localhost");
assertEquals(1, statement_insert.executeUpdate());
statement_update.setLong(1, msg_id);
assertEquals(1, statement_update.executeUpdate());
try (ResultSet rs = create.executeQuery("SELECT M.MSG_ID FROM q1_MESSAGE M WHERE 1=1 AND (M.RECIPIENT LIKE '%@localhost%')")) {
long _msg_id = -1;
int record_count = 0;
while (rs.next()) {
_msg_id = rs.getLong(1);
record_count++;
}
assertEquals(1, record_count);
assertTrue(_msg_id > 0);
}
try (PreparedStatement ps = con.prepareStatement("UPDATE q1_MESSAGE SET STATUS= 6,SID=?,LAST_MODIFY_TIME=?,NEXT_SEND_TIME = null, LASTBOUNCECATEGORY=? WHERE MSG_ID = ?")) {
ps.setInt(1, 1);
ps.setTimestamp(2, new java.sql.Timestamp(System.currentTimeMillis()));
ps.setInt(3, 2);
ps.setLong(4, msg_id);
assertEquals(1, ps.executeUpdate());
}
try (ResultSet rs = create.executeQuery("SELECT M.MSG_ID FROM q1_MESSAGE M WHERE 1=1 AND status=6 and (M.RECIPIENT LIKE '%@localhost%')")) {
long _msg_id = -1;
int record_count = 0;
while (rs.next()) {
_msg_id = rs.getLong(1);
record_count++;
}
assertEquals(1, record_count);
assertTrue(_msg_id > 0);
}
}
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class MysqlCompatilityTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
// assumeTrue(server.getManager().getPlanner() instanceof SQLPlanner);
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
Connection con = dataSource.getConnection();
Connection con2 = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("DROP TABLE IF EXISTS `sm_machine`;");
statement.execute("CREATE TABLE `sm_machine` (\n" + " `ip` varchar(20) NOT NULL DEFAULT '',\n" + " `firefox_version` int(50) DEFAULT NULL,\n" + " `chrome_version` int(50) DEFAULT NULL,\n" + " `ie_version` int(50) DEFAULT NULL,\n" + " `log` varchar(2000) DEFAULT NULL,\n" + " `offset` int(50) DEFAULT NULL,\n" + " PRIMARY KEY (`ip`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
int multiInsertCount = statement.executeUpdate("INSERT INTO `sm_machine` VALUES" + "('10.168.10.106',26,36,9,NULL,1)," + "('10.168.10.107',26,31,10,NULL,1)," + "('10.168.10.108',26,36,11,NULL,2)," + "('10.168.10.109',33,38,10,NULL,3)," + "('10.168.10.110',33,38,10,NULL,4)");
assertEquals(5, multiInsertCount);
try (PreparedStatement ps = statement.getConnection().prepareStatement("INSERT INTO `sm_machine` VALUES" + "(?,?,?,?,?,?)," + "(?,?,?,?,?,?)," + "(?,?,?,?,?,?)," + "(?,?,?,?,?,?)," + "(?,?,?,?,?,?)")) {
int i = 1;
ps.setString(i++, "11.168.10.106");
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, null);
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, "11.168.10.107");
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, null);
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, "11.168.10.108");
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, null);
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, "11.168.10.109");
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, null);
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, "11.168.10.110");
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.setString(i++, null);
ps.setInt(i++, 1);
ps.setInt(i++, 1);
ps.execute();
int multiInsertCount2 = ps.getUpdateCount();
assertEquals(5, multiInsertCount2);
}
statement.executeQuery("SELECT ip, `offset` FROM sm_machine WHERE `offset` = 1").close();
statement.execute("DROP TABLE sm_machine;");
try {
statement.executeQuery("SELECT COUNT(*) FROM sm_machine").close();
fail();
} catch (SQLException err) {
assertTrue(err.getMessage().contains(herddb.model.TableDoesNotExistException.class.getName()) || err.getMessage().contains("SM_MACHINE"));
}
statement.execute("CREATE TABLE test_odd_names (\n" + " value varchar(20) NOT NULL DEFAULT '',\n" + " PRIMARY KEY (`value`)\n" + ")");
statement.executeUpdate("INSERT INTO test_odd_names VALUES" + "('10.168.10.106')");
try (ResultSet rs = statement.executeQuery("SELECT `value` FROM test_odd_names WHERE `value` = '10.168.10.106'")) {
assertTrue(rs.next());
assertEquals("10.168.10.106", rs.getString(1));
assertEquals("10.168.10.106", rs.getString("value"));
}
}
}
}
}
Aggregations