use of herddb.client.GetResult in project herddb by diennea.
the class HistoryChangelogTest method test.
@Test
@Ignore
public void test() 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, 0);
serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
ConcurrentSkipListSet<Long> doneElements = new ConcurrentSkipListSet<>();
ConcurrentHashMap<Long, Element> elements = 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 long primary key, hid long, status integer)", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
long resultCreateTableHistory = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE history (id long, hid long, status integer, primary key (id,hid) )", 0, false, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTableHistory);
long tx = connection.beginTransaction(TableSpace.DEFAULT);
for (long i = 0; i < TABLESIZE; i++) {
int status = 0;
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,hid,status) values(?,?,?)", tx, false, Arrays.asList(i, 0, status));
elements.put(i, new Element(0, status));
}
connection.commitTransaction(TableSpace.DEFAULT, tx);
ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
try {
List<Future> futures = new ArrayList<>();
AtomicLong updates = new AtomicLong();
for (int i = 0; i < TABLESIZE * MULTIPLIER; i++) {
futures.add(threadPool.submit(new Runnable() {
@Override
public void run() {
try {
long id = ThreadLocalRandom.current().nextInt(TABLESIZE);
doneElements.add(id);
Element element = elements.remove(id);
if (element == null) {
return;
}
int new_status = element.status + 1;
long next_hid = element.hid + 1;
long transactionId;
updates.incrementAndGet();
DMLResult updateResult = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set hid=?,status=? WHERE id=?", TransactionContext.AUTOTRANSACTION_ID, false, Arrays.asList(next_hid, new_status, id));
transactionId = updateResult.transactionId;
if (updateResult.updateCount <= 0) {
throw new RuntimeException("not updated ?");
}
DMLResult insertResult = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO history (id,hid,status) values (?,?,?)", transactionId, false, Arrays.asList(id, next_hid, new_status));
if (insertResult.updateCount <= 0) {
throw new RuntimeException("not inserted ?");
}
connection.commitTransaction(TableSpace.DEFAULT, transactionId);
// make the element available again
elements.put(id, new Element(new_status, next_hid));
} catch (Exception err) {
err.printStackTrace();
throw new RuntimeException(err);
}
}
}));
}
for (Future f : futures) {
f.get();
}
System.out.println("stats::updates:" + updates);
assertTrue(updates.get() > 0);
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());
assertEquals(TABLESIZE, stats.getTablesize());
for (Map.Entry<Long, Element> entry : elements.entrySet()) {
{
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT status,hid FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, Arrays.asList(entry.getKey()));
assertNotNull(res.data);
assertEquals(entry.getValue().status, res.data.get("status"));
assertEquals(entry.getValue().hid, res.data.get("hid"));
}
if (doneElements.contains(entry.getKey())) {
ScanResultSet res = connection.executeScan(TableSpace.DEFAULT, "SELECT id, status, hid, (SELECT MAX(hid) as mm from history where history.id=mytable.id) as maxhid " + "FROM mytable where id=?", Arrays.asList(entry.getKey()), TransactionContext.NOTRANSACTION_ID, -1, 10000);
List<Map<String, Object>> consume = res.consume();
assertEquals(1, consume.size());
Map<String, Object> data = consume.get(0);
System.out.println("data:" + data);
assertEquals(entry.getValue().status, data.get("status"));
assertEquals(entry.getValue().hid, data.get("hid"));
assertEquals(entry.getValue().hid, data.get("maxhid"));
assertEquals(entry.getKey(), data.get("id"));
}
}
} 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<Long, Element> entry : elements.entrySet()) {
{
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT status,hid FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, Arrays.asList(entry.getKey()));
assertNotNull(res.data);
assertEquals(entry.getValue().status, res.data.get("status"));
assertEquals(entry.getValue().hid, res.data.get("hid"));
}
if (doneElements.contains(entry.getKey())) {
ScanResultSet res = connection.executeScan(TableSpace.DEFAULT, "SELECT id, status, hid, (SELECT MAX(hid) as mm from history where history.id=mytable.id) as maxhid " + "FROM mytable where id=?", Arrays.asList(entry.getKey()), TransactionContext.NOTRANSACTION_ID, -1, 10000);
List<Map<String, Object>> consume = res.consume();
assertEquals(1, consume.size());
Map<String, Object> data = consume.get(0);
System.out.println("data:" + data);
assertEquals(entry.getValue().status, data.get("status"));
assertEquals(entry.getValue().hid, data.get("hid"));
assertEquals(entry.getValue().hid, data.get("maxhid"));
assertEquals(entry.getKey(), data.get("id"));
}
}
}
}
}
use of herddb.client.GetResult in project herddb by diennea.
the class SimpleClientServerAutoTransactionTest method test.
@Test
public void test() throws Exception {
Path baseDir = folder.newFolder().toPath();
;
String _baseDir = baseDir.toString();
try (Server server = new Server(new ServerConfiguration(baseDir))) {
server.start();
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);
DMLResult executeUpdateResult = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", TransactionContext.AUTOTRANSACTION_ID, false, Arrays.asList("test", 1, 2));
long tx = executeUpdateResult.transactionId;
long countInsert = executeUpdateResult.updateCount;
Assert.assertEquals(1, countInsert);
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, Collections.emptyList());
;
Map<String, Object> record = res.data;
Assert.assertNotNull(record);
assertEquals("test", record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
connection.commitTransaction(TableSpace.DEFAULT, tx);
try (ScanResultSet scan = connection.executeScan(server.getManager().getVirtualTableSpaceId(), "SELECT * FROM sysconfig", Collections.emptyList(), 0, 0, 10)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
String name = (String) aa.get("name");
assertEquals("server.base.dir", name);
String value = (String) aa.get("value");
assertEquals(_baseDir, value);
}
}
try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", Collections.emptyList(), 0, 0, 10)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
assertEquals("jvm-local", aa.get("address"));
assertEquals("1", aa.get("id") + "");
assertEquals(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT, aa.get("username"));
assertNotNull(aa.get("connectionts"));
}
assertEquals(1, all.size());
}
}
}
}
use of herddb.client.GetResult 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(new ServerConfiguration(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, 100));
assertFalse(connection.waitForTableSpace("bad-tablespace", 100));
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);
long 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);
long countInsert2 = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test2", 2, 3)).updateCount;
Assert.assertEquals(1, countInsert2);
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, Collections.emptyList());
Map<String, Object> record = res.data;
Assert.assertNotNull(record);
assertEquals("test", record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", tx, false, 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", Collections.emptyList(), 0, 0, 10)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
String name = (String) aa.get("name");
assertEquals("server.base.dir", name);
String value = (String) aa.get("value");
assertEquals(_baseDir, value);
}
}
try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", Collections.emptyList(), 0, 0, 10)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
assertEquals("jvm-local", aa.get("address"));
assertNotNull(aa.get("id"));
assertEquals(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT, aa.get("username"));
assertNotNull(aa.get("connectionts"));
}
assertEquals(1, all.size());
}
}
}
}
use of herddb.client.GetResult 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"));
}
}
}
}
Aggregations