use of herddb.model.TransactionContext in project herddb by diennea.
the class SecondaryNonUniqueIndexAccessSuite method dropIndexInTransaction.
@Test
public void dropIndexInTransaction() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
Bytes key = Bytes.from_int(1234);
Bytes value = Bytes.from_long(8888);
Table transacted_table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
CreateTableStatement st_create = new CreateTableStatement(transacted_table);
manager.executeStatement(st_create, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values(?,?)", Arrays.asList("a", "n1"), TransactionContext.NO_TRANSACTION);
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values(?,?)", Arrays.asList("b", "n1"), TransactionContext.NO_TRANSACTION);
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values(?,?)", Arrays.asList("c", "n2"), TransactionContext.NO_TRANSACTION);
Index index = Index.builder().onTable(transacted_table).type(indexType).column("name", ColumnTypes.STRING).build();
CreateIndexStatement createIndex = new CreateIndexStatement(index);
manager.executeStatement(createIndex, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(2, scan1.consume().size());
}
}
long tx = TestUtils.beginTransaction(manager, "tblspace1");
DropIndexStatement dropIndex = new DropIndexStatement(index.tablespace, index.name, false);
manager.executeStatement(dropIndex, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
TestUtils.commitTransaction(manager, "tblspace1", tx);
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertFalse(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(2, scan1.consume().size());
}
}
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class DirectMultipleConcurrentUpdatesSuite method performTest.
protected void performTest(boolean useTransactions, long checkPointPeriod, boolean withIndexes, boolean uniqueIndexes) throws Exception {
Path baseDir = folder.newFolder().toPath();
ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_DATA_MEMORY, 1024 * 1024 / 4);
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();
DBManager manager = server.getManager();
execute(manager, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", Collections.emptyList());
if (withIndexes) {
if (uniqueIndexes) {
// use n1 + id in order to not have collisions and lock timeouts
execute(manager, "CREATE UNIQUE INDEX theindex ON mytable (n1, id)", Collections.emptyList());
} else {
execute(manager, "CREATE INDEX theindex ON mytable (n1)", Collections.emptyList());
}
}
long tx = TestUtils.beginTransaction(manager, TableSpace.DEFAULT);
for (int i = 0; i < TABLESIZE; i++) {
TestUtils.executeUpdate(manager, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", Arrays.asList("test_" + i, 1, 2), new TransactionContext(tx));
expectedValue.put("test_" + i, 1L);
}
TestUtils.commitTransaction(manager, 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();
DMLStatementExecutionResult updateResult = TestUtils.executeUpdate(manager, "UPDATE mytable set n1=? WHERE id=?", Arrays.asList(value, "test_" + k), new TransactionContext(useTransactions ? TransactionContext.AUTOTRANSACTION_ID : TransactionContext.NOTRANSACTION_ID));
long count = updateResult.getUpdateCount();
transactionId = updateResult.transactionId;
if (count <= 0) {
throw new RuntimeException("not updated ?");
}
} else {
gets.incrementAndGet();
DataScanner res = TestUtils.scanKeepReadLocks(manager, "SELECT * FROM mytable where id=?", Arrays.asList("test_" + k), new TransactionContext(useTransactions ? TransactionContext.AUTOTRANSACTION_ID : TransactionContext.NOTRANSACTION_ID));
if (!res.hasNext()) {
throw new RuntimeException("not found?");
}
res.close();
transactionId = res.getTransactionId();
// value did not change actually
value = actual;
}
if (useTransactions) {
if (transactionId <= 0) {
throw new RuntimeException("no transaction ?");
}
commitTransaction(manager, 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);
assertTrue(updates.get() > 0);
assertTrue(gets.get() > 0);
List<String> erroredKeys = new ArrayList<>();
for (Map.Entry<String, Long> entry : expectedValue.entrySet()) {
List<DataAccessor> records;
DataAccessor data;
try (DataScanner res = scan(manager, "SELECT n1 FROM mytable where id=?", Arrays.asList(entry.getKey()))) {
records = res.consume();
data = records.get(0);
}
assertEquals(1, records.size());
if (!entry.getValue().equals(data.get("n1"))) {
System.out.println("expected value " + 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.waitForTableSpaceBoot(TableSpace.DEFAULT, 300000, true);
DBManager manager = server.getManager();
List<String> erroredKeys = new ArrayList<>();
for (Map.Entry<String, Long> entry : expectedValue.entrySet()) {
List<DataAccessor> records;
DataAccessor data;
try (DataScanner res = scan(manager, "SELECT n1 FROM mytable where id=?", Arrays.asList(entry.getKey()))) {
records = res.consume();
data = records.get(0);
}
assertEquals(1, records.size());
if (!entry.getValue().equals(data.get("n1"))) {
System.out.println("expected value " + data.get("n1") + ", but got " + Long.valueOf(entry.getValue()) + " for key " + entry.getKey());
erroredKeys.add(entry.getKey());
}
}
assertTrue(erroredKeys.isEmpty());
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class TableSpaceManager method executeStatement.
public StatementExecutionResult executeStatement(Statement statement, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
boolean rollbackOnError = false;
/* Do not autostart transaction on alter table statements */
if (transactionContext.transactionId == TransactionContext.AUTOTRANSACTION_ID && statement.supportsTransactionAutoCreate()) {
StatementExecutionResult newTransaction = beginTransaction();
transactionContext = new TransactionContext(newTransaction.transactionId);
rollbackOnError = true;
}
Transaction transaction = transactions.get(transactionContext.transactionId);
if (transaction != null && !transaction.tableSpace.equals(tableSpaceName)) {
throw new StatementExecutionException("transaction " + transaction.transactionId + " is for tablespace " + transaction.tableSpace + ", not for " + tableSpaceName);
}
if (transactionContext.transactionId > 0 && transaction == null) {
throw new StatementExecutionException("transaction " + transactionContext.transactionId + " not found on tablespace " + tableSpaceName);
}
try {
if (statement instanceof TableAwareStatement) {
return executeTableAwareStatement(statement, transaction, context);
}
if (statement instanceof SQLPlannedOperationStatement) {
return executePlannedOperationStatement(statement, transactionContext, context);
}
if (statement instanceof BeginTransactionStatement) {
if (transaction != null) {
throw new IllegalArgumentException("transaction already started");
}
return beginTransaction();
}
if (statement instanceof RollbackTransactionStatement) {
return rollbackTransaction((RollbackTransactionStatement) statement);
}
if (statement instanceof CommitTransactionStatement) {
return commitTransaction((CommitTransactionStatement) statement);
}
if (statement instanceof CreateTableStatement) {
return createTable((CreateTableStatement) statement, transaction);
}
if (statement instanceof CreateIndexStatement) {
return createIndex((CreateIndexStatement) statement, transaction);
}
if (statement instanceof DropTableStatement) {
return dropTable((DropTableStatement) statement, transaction);
}
if (statement instanceof DropIndexStatement) {
return dropIndex((DropIndexStatement) statement, transaction);
}
if (statement instanceof AlterTableStatement) {
return alterTable((AlterTableStatement) statement, transactionContext);
}
throw new StatementExecutionException("unsupported statement " + statement);
} catch (StatementExecutionException error) {
if (rollbackOnError) {
rollbackTransaction(new RollbackTransactionStatement(tableSpaceName, transactionContext.transactionId));
}
throw error;
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class DBManager method executeJoinedScansPlan.
private StatementExecutionResult executeJoinedScansPlan(List<DataScanner> scanResults, StatementEvaluationContext context, TransactionContext transactionContext, ExecutionPlan plan) throws StatementExecutionException {
try {
List<Column> composedSchema = new ArrayList<>();
for (DataScanner ds : scanResults) {
composedSchema.addAll(Arrays.asList(ds.getSchema()));
}
Column[] finalSchema = new Column[composedSchema.size()];
composedSchema.toArray(finalSchema);
String[] finalSchemaFieldNames = Column.buildFieldNamesList(finalSchema);
MaterializedRecordSet finalResultSet = recordSetFactory.createRecordSet(finalSchemaFieldNames, finalSchema);
DataScannerJoinExecutor joinExecutor;
if (plan.joinProjection != null) {
if (plan.joinFilter != null) {
TuplePredicate joinFilter = plan.joinFilter;
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
if (joinFilter.matches(t, context)) {
finalResultSet.add(plan.joinProjection.map(t, context));
}
});
} else {
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
finalResultSet.add(plan.joinProjection.map(t, context));
});
}
} else {
if (plan.joinFilter != null) {
TuplePredicate joinFilter = plan.joinFilter;
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
if (joinFilter.matches(t, context)) {
finalResultSet.add(t);
}
});
} else {
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
finalResultSet.add(t);
});
}
}
joinExecutor.executeJoin();
finalResultSet.writeFinished();
finalResultSet.sort(plan.comparator);
finalResultSet.applyLimits(plan.limits, context);
return new ScanResult(transactionContext.transactionId, new SimpleDataScanner(transactionContext.transactionId, finalResultSet));
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class DeleteOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
ScanResult downstreamScanResult = (ScanResult) input;
final Table table = tableSpaceManager.getTableManager(tableName).getTable();
long transactionId = transactionContext.transactionId;
int updateCount = 0;
Bytes key = null;
try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
while (inputScanner.hasNext()) {
DataAccessor row = inputScanner.next();
long transactionIdFromScanner = inputScanner.getTransactionId();
if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
transactionId = transactionIdFromScanner;
transactionContext = new TransactionContext(transactionId);
}
key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
DMLStatement deleteStatement = new DeleteStatement(tableSpace, tableName, null, new RawKeyEquals(key));
DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(deleteStatement, context, transactionContext);
updateCount += _result.getUpdateCount();
if (_result.transactionId > 0 && _result.transactionId != transactionId) {
transactionId = _result.transactionId;
transactionContext = new TransactionContext(transactionId);
}
key = _result.getKey();
}
return new DMLStatementExecutionResult(transactionId, updateCount, key, null);
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
Aggregations