use of herddb.log.LogSequenceNumber in project herddb by diennea.
the class MemoryDataStorageManager method evaluateLogSequenceNumber.
private LogSequenceNumber evaluateLogSequenceNumber(String string) {
final Matcher matcher = LOG_SEQUENCE_PATTERN.matcher(string);
if (!matcher.matches()) {
return null;
}
try {
long ledgerId = Long.parseLong(matcher.group("ledgerId"));
long offset = Long.parseLong(matcher.group("offset"));
return new LogSequenceNumber(ledgerId, offset);
} catch (NumberFormatException e) {
return null;
}
}
use of herddb.log.LogSequenceNumber in project herddb by diennea.
the class Transaction method deserialize.
public static Transaction deserialize(String tableSpace, ExtendedDataInputStream in) throws IOException {
// version
long version = in.readVLong();
// flags for future implementations
long flags = in.readVLong();
if (version != 1 || flags != 0) {
throw new IOException("corrupted transaction file");
}
long id = in.readZLong();
long ledgerId = in.readZLong();
long offset = in.readZLong();
LogSequenceNumber lastSequenceNumber = new LogSequenceNumber(ledgerId, offset);
Transaction t = new Transaction(id, tableSpace, new CommitLogResult(lastSequenceNumber, false, true));
int size = in.readVInt();
for (int i = 0; i < size; i++) {
String table = in.readUTF();
int numRecords = in.readVInt();
Map<Bytes, Record> records = new HashMap<>();
for (int k = 0; k < numRecords; k++) {
byte[] key = in.readArray();
byte[] value = in.readArray();
Bytes bKey = Bytes.from_array(key);
Record record = new Record(bKey, Bytes.from_array(value));
records.put(bKey, record);
}
t.changedRecords.put(table, records);
}
size = in.readVInt();
for (int i = 0; i < size; i++) {
String table = in.readUTF();
int numRecords = in.readVInt();
Map<Bytes, Record> records = new HashMap<>();
for (int k = 0; k < numRecords; k++) {
byte[] key = in.readArray();
byte[] value = in.readArray();
Bytes bKey = Bytes.from_array(key);
Record record = new Record(bKey, Bytes.from_array(value));
records.put(bKey, record);
}
t.newRecords.put(table, records);
}
size = in.readVInt();
for (int i = 0; i < size; i++) {
String table = in.readUTF();
int numRecords = in.readVInt();
Set<Bytes> records = new HashSet<>();
for (int k = 0; k < numRecords; k++) {
byte[] key = in.readArray();
records.add(Bytes.from_array(key));
}
t.deletedRecords.put(table, records);
}
size = in.readVInt();
if (size > 0) {
t.newTables = new HashMap<>();
for (int i = 0; i < size; i++) {
byte[] data = in.readArray();
Table table = Table.deserialize(data);
t.newTables.put(table.name, table);
}
}
size = in.readVInt();
if (size > 0) {
t.droppedTables = new HashSet<>();
for (int i = 0; i < size; i++) {
t.droppedTables.add(in.readUTF());
}
}
size = in.readVInt();
if (size > 0) {
t.newIndexes = new HashMap<>();
for (int i = 0; i < size; i++) {
byte[] data = in.readArray();
Index index = Index.deserialize(data);
t.newIndexes.put(index.name, index);
}
}
size = in.readVInt();
if (size > 0) {
t.droppedIndexes = new HashSet<>();
for (int i = 0; i < size; i++) {
t.droppedIndexes.add(in.readUTF());
}
}
return t;
}
use of herddb.log.LogSequenceNumber in project herddb by diennea.
the class Transaction method updateLastSequenceNumber.
private boolean updateLastSequenceNumber(CommitLogResult writeResult) throws LogNotAvailableException {
touch();
if (writeResult.deferred) {
deferredWrites.add(writeResult);
return false;
}
LogSequenceNumber sequenceNumber = writeResult.getLogSequenceNumber();
if (lastSequenceNumber != null && lastSequenceNumber.after(sequenceNumber)) {
return true;
}
this.lastSequenceNumber = sequenceNumber;
return false;
}
use of herddb.log.LogSequenceNumber in project herddb by diennea.
the class BootFollowerTest method testFollowAfterLedgerRollback.
@Test
public void testFollowAfterLedgerRollback() throws Exception {
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);
// send a dummy write after 1 seconds of inactivity
serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_MAX_IDLE_TIME, 1000);
ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
try (Server server_1 = new Server(serverconfig_1)) {
server_1.start();
server_1.waitForStandaloneBoot();
Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), 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);
try (Server server_2 = new Server(serverconfig_2)) {
server_2.start();
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
TableSpaceManager tableSpaceManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
BookkeeperCommitLog logServer1 = (BookkeeperCommitLog) tableSpaceManager.getLog();
logServer1.rollNewLedger();
logServer1.rollNewLedger();
logServer1.rollNewLedger();
DMLStatementExecutionResult executeUpdateTransaction = server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 5)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
server_1.getManager().executeStatement(new CommitTransactionStatement(TableSpace.DEFAULT, executeUpdateTransaction.transactionId), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// 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(5), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
if (found.found()) {
break;
}
Thread.sleep(100);
}
for (int i = 1; i <= 5; i++) {
System.out.println("checking key c=" + i);
assertTrue(server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(i), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).found());
}
BookkeeperCommitLog logServer2 = (BookkeeperCommitLog) tableSpaceManager.getLog();
LogSequenceNumber lastSequenceNumberServer1 = logServer1.getLastSequenceNumber();
LogSequenceNumber lastSequenceNumberServer2 = logServer2.getLastSequenceNumber();
System.out.println("POS AT SERVER 1: " + lastSequenceNumberServer1);
System.out.println("POS AT SERVER 2: " + lastSequenceNumberServer2);
assertTrue(lastSequenceNumberServer1.equals(lastSequenceNumberServer2) || // maybe after due to NOOPs
lastSequenceNumberServer1.after(lastSequenceNumberServer2));
}
}
}
use of herddb.log.LogSequenceNumber in project herddb by diennea.
the class EdgeCasesFollowerTest method testLeaderOnlineLogNoMoreAvailableDataAlreadyPresent.
@Test
public void testLeaderOnlineLogNoMoreAvailableDataAlreadyPresent() 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());
// but the server already has local data, tablespace directory must be erased
try (Server server_2 = new Server(serverconfig_2)) {
server_2.start();
assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
assertTrue(server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).found());
assertEquals(2, countErase.get());
TranslatedQuery translated = server_2.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_2.getManager().scan(statement, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(1, scan.consume().size());
}
}
}
}
Aggregations