use of herddb.model.GetResult in project herddb by diennea.
the class TableManager method executeGet.
private StatementExecutionResult executeGet(GetStatement get, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException, DataStorageManagerException {
Bytes key = new Bytes(get.getKey().computeNewValue(null, context, tableContext));
Predicate predicate = get.getPredicate();
boolean requireLock = get.isRequireLock();
long transactionId = transaction != null ? transaction.transactionId : 0;
LockHandle lock = (transaction != null || requireLock) ? lockForRead(key, transaction) : null;
try {
if (transaction != null) {
if (transaction.recordDeleted(table.name, key)) {
return GetResult.NOT_FOUND(transactionId);
}
Record loadedInTransaction = transaction.recordUpdated(table.name, key);
if (loadedInTransaction != null) {
if (predicate != null && !predicate.evaluate(loadedInTransaction, context)) {
return GetResult.NOT_FOUND(transactionId);
}
return new GetResult(transactionId, loadedInTransaction, table);
}
loadedInTransaction = transaction.recordInserted(table.name, key);
if (loadedInTransaction != null) {
if (predicate != null && !predicate.evaluate(loadedInTransaction, context)) {
return GetResult.NOT_FOUND(transactionId);
}
return new GetResult(transactionId, loadedInTransaction, table);
}
}
Long pageId = keyToPage.get(key);
if (pageId == null) {
return GetResult.NOT_FOUND(transactionId);
}
Record loaded = fetchRecord(key, pageId, null);
if (loaded == null || (predicate != null && !predicate.evaluate(loaded, context))) {
return GetResult.NOT_FOUND(transactionId);
}
return new GetResult(transactionId, loaded, table);
} finally {
if (transaction == null && lock != null) {
locksManager.releaseReadLockForKey(key, lock);
}
}
}
use of herddb.model.GetResult in project herddb by diennea.
the class TableManager method executeGetAsync.
private CompletableFuture<StatementExecutionResult> executeGetAsync(GetStatement get, Transaction transaction, StatementEvaluationContext context) {
Bytes key;
try {
key = Bytes.from_nullable_array(get.getKey().computeNewValue(null, context, tableContext));
} catch (StatementExecutionException validationError) {
return Futures.exception(validationError);
}
Predicate predicate = get.getPredicate();
boolean requireLock = get.isRequireLock();
boolean useWriteLock = requireLock && context.isForceAcquireWriteLock();
long transactionId = transaction != null ? transaction.transactionId : 0;
LockHandle lock = (transaction != null || requireLock) ? (useWriteLock ? lockForWrite(key, transaction) : lockForRead(key, transaction)) : null;
CompletableFuture<StatementExecutionResult> res = null;
try {
if (transaction != null) {
if (transaction.recordDeleted(table.name, key)) {
res = CompletableFuture.completedFuture(GetResult.NOT_FOUND(transactionId));
} else {
Record loadedInTransaction = transaction.recordUpdated(table.name, key);
if (loadedInTransaction != null) {
if (predicate != null && !predicate.evaluate(loadedInTransaction, context)) {
res = CompletableFuture.completedFuture(GetResult.NOT_FOUND(transactionId));
} else {
res = CompletableFuture.completedFuture(new GetResult(transactionId, loadedInTransaction, table));
}
} else {
loadedInTransaction = transaction.recordInserted(table.name, key);
if (loadedInTransaction != null) {
if (predicate != null && !predicate.evaluate(loadedInTransaction, context)) {
res = CompletableFuture.completedFuture(GetResult.NOT_FOUND(transactionId));
} else {
res = CompletableFuture.completedFuture(new GetResult(transactionId, loadedInTransaction, table));
}
}
}
}
}
if (res == null) {
Long pageId = keyToPage.get(key);
if (pageId == null) {
res = CompletableFuture.completedFuture(GetResult.NOT_FOUND(transactionId));
} else {
Record loaded = fetchRecord(key, pageId, null);
if (loaded == null || (predicate != null && !predicate.evaluate(loaded, context))) {
res = CompletableFuture.completedFuture(GetResult.NOT_FOUND(transactionId));
} else {
res = CompletableFuture.completedFuture(new GetResult(transactionId, loaded, table));
}
}
}
if (lock != null) {
if (transaction == null) {
res.whenComplete((r, e) -> {
locksManager.releaseReadLock(lock);
});
} else if (!context.isForceRetainReadLock() && !lock.write) {
transaction.releaseLockOnKey(table.name, key, locksManager);
}
}
return res;
} catch (HerdDBInternalException err) {
return Futures.exception(err);
}
}
use of herddb.model.GetResult in project herddb by diennea.
the class AutoTransactionTest method testAutoTransactionOnGet.
@Test
public void testAutoTransactionOnGet() throws Exception {
int i = 1;
Map<String, Object> data = new HashMap<>();
Bytes key = Bytes.from_string("key_" + i);
data.put("id", "key_" + i);
data.put("number", i);
Record record = RecordSerializer.toRecord(data, table);
InsertStatement st = new InsertStatement(tableSpace, tableName, record);
manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
long tx = get.transactionId;
assertTrue(get.found());
TestUtils.commitTransaction(manager, tableSpace, tx);
}
use of herddb.model.GetResult in project herddb by diennea.
the class AutoTransactionTest method testAutoTransactionOnFailedGet.
@Test
public void testAutoTransactionOnFailedGet() throws Exception {
int i = 1;
Map<String, Object> data = new HashMap<>();
Bytes key_bad = Bytes.from_string("key_bad_" + i);
data.put("id", "key_" + i);
data.put("number", i);
Record record = RecordSerializer.toRecord(data, table);
InsertStatement st = new InsertStatement(tableSpace, tableName, record);
manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
GetResult get = manager.get(new GetStatement(tableSpace, tableName, key_bad, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
long tx = get.transactionId;
assertTrue(!get.found());
TestUtils.commitTransaction(manager, tableSpace, tx);
}
use of herddb.model.GetResult in project herddb by diennea.
the class BootAsNewLeaderTest method testLeaderOnlineLogNoMoreAvailableDataAlreadyPresentBootAsNewLeader.
@Test
public void testLeaderOnlineLogNoMoreAvailableDataAlreadyPresentBootAsNewLeader() throws Exception {
final AtomicInteger countErase = new AtomicInteger();
SystemInstrumentation.addListener(new SystemInstrumentation.SingleInstrumentationPointListener("eraseTablespaceData") {
@Override
public void acceptSingle(Object... args) throws Exception {
countErase.incrementAndGet();
}
});
ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_RETENTION_PERIOD, 1);
serverconfig_1.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
// disabled
serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_MAX_IDLE_TIME, 0);
ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).column("s", ColumnTypes.INTEGER).primaryKey("c").build();
Index index = Index.builder().onTable(table).type(Index.TYPE_BRIN).column("s", ColumnTypes.STRING).build();
try (Server server_1 = new Server(serverconfig_1)) {
server_1.start();
server_1.waitForStandaloneBoot();
server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeStatement(new CreateIndexStatement(index), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1, "s", "1")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2, "s", "2")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3, "s", "3")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4, "s", "4")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
TranslatedQuery translated = server_1.getManager().getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM " + TableSpace.DEFAULT + ".t1 WHERE s=1", Collections.emptyList(), true, true, false, -1);
ScanStatement statement = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(statement.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
try (DataScanner scan = server_1.getManager().scan(statement, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(1, scan.consume().size());
}
}
String tableSpaceUUID;
try (Server server_1 = new Server(serverconfig_1)) {
server_1.start();
server_1.waitForStandaloneBoot();
{
ZookeeperMetadataStorageManager man = (ZookeeperMetadataStorageManager) server_1.getMetadataStorageManager();
tableSpaceUUID = man.describeTableSpace(TableSpace.DEFAULT).uuid;
LedgersInfo ledgersList = ZookeeperMetadataStorageManager.readActualLedgersListFromZookeeper(man.getZooKeeper(), testEnv.getPath() + "/ledgers", tableSpaceUUID);
assertEquals(2, ledgersList.getActiveLedgers().size());
}
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 5, "s", "5")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().checkpoint();
}
try (Server server_1 = new Server(serverconfig_1)) {
server_1.start();
server_1.waitForStandaloneBoot();
{
ZookeeperMetadataStorageManager man = (ZookeeperMetadataStorageManager) server_1.getMetadataStorageManager();
LedgersInfo ledgersList = ZookeeperMetadataStorageManager.readActualLedgersListFromZookeeper(man.getZooKeeper(), testEnv.getPath() + "/ledgers", tableSpaceUUID);
assertEquals(2, ledgersList.getActiveLedgers().size());
}
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 6, "s", "6")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
{
ZookeeperMetadataStorageManager man = (ZookeeperMetadataStorageManager) server_1.getMetadataStorageManager();
LedgersInfo ledgersList = ZookeeperMetadataStorageManager.readActualLedgersListFromZookeeper(man.getZooKeeper(), testEnv.getPath() + "/ledgers", tableSpaceUUID);
assertEquals(2, ledgersList.getActiveLedgers().size());
}
server_1.getManager().checkpoint();
}
assertEquals(0, countErase.get());
LogSequenceNumber server2checkpointPosition;
try (Server server_1 = new Server(serverconfig_1)) {
server_1.start();
server_1.waitForStandaloneBoot();
// start server_2, and flush data locally
try (Server server_2 = new Server(serverconfig_2)) {
server_2.start();
assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
// wait for data to arrive on server_2
for (int i = 0; i < 100; i++) {
GetResult found = server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
if (found.found()) {
break;
}
Thread.sleep(100);
}
// force a checkpoint, data is flushed to disk
server_2.getManager().checkpoint();
server2checkpointPosition = server_2.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog().getLastSequenceNumber();
System.out.println("server2 checkpoint time: " + server2checkpointPosition);
}
}
// start again server_1, in order to create a new ledger
try (Server server_1 = new Server(serverconfig_1)) {
server_1.start();
server_1.waitForStandaloneBoot();
BookkeeperCommitLog ll = (BookkeeperCommitLog) server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog();
// server_1 make much progress
ll.rollNewLedger();
ll.rollNewLedger();
ll.rollNewLedger();
ll.rollNewLedger();
ll.rollNewLedger();
ll.rollNewLedger();
ll.rollNewLedger();
ll.rollNewLedger();
// a checkpoint will delete old ledgers
server_1.getManager().checkpoint();
{
ZookeeperMetadataStorageManager man = (ZookeeperMetadataStorageManager) server_1.getMetadataStorageManager();
LedgersInfo ledgersList = ZookeeperMetadataStorageManager.readActualLedgersListFromZookeeper(man.getZooKeeper(), testEnv.getPath() + "/ledgers", tableSpaceUUID);
System.out.println("ledgerList: " + ledgersList);
assertEquals(1, ledgersList.getActiveLedgers().size());
assertTrue(!ledgersList.getActiveLedgers().contains(ledgersList.getFirstLedger()));
// we want to be sure that server_2 cannot recover from log
assertTrue(!ledgersList.getActiveLedgers().contains(server2checkpointPosition.ledgerId));
}
assertEquals(1, countErase.get());
// make server2 boot as leader, it will fence out server1
// but server1 is not writing so it won't know from BK
// it will see his new role from zookeeper (with a 'watch' notification)
server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server2", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// trying to become leader
try (Server server_2 = new Server(serverconfig_2)) {
server_2.start();
assertFalse(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 10000, true));
}
// make server_1 leader again
server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// now server_2 must be able to boot, downloading data from server_1 and easing local state
try (Server server_2 = new Server(serverconfig_2)) {
server_2.start();
assertFalse(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, true));
}
// make server_2 leader again
server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server2", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// stop server_1
server_1.close();
// it must have enough local data to boot
try (Server server_2 = new Server(serverconfig_2)) {
server_2.start();
assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, true));
}
}
}
Aggregations