Search in sources :

Example 46 with StorageCommand

use of org.neo4j.storageengine.api.StorageCommand in project neo4j by neo4j.

the class RecordStorageEngine method createUpgradeCommands.

@Override
public List<StorageCommand> createUpgradeCommands(KernelVersion versionToUpgradeTo, InjectedNLIUpgradeCallback injectedNLIUpgradeCallback) {
    MetaDataStore metaDataStore = neoStores.getMetaDataStore();
    KernelVersion currentVersion = metaDataStore.kernelVersion();
    Preconditions.checkState(currentVersion.isAtLeast(KernelVersion.V4_2), "Upgrade transaction was introduced in %s and must be done from at least %s. Tried upgrading from %s to %s", KernelVersion.V4_3_D4, KernelVersion.V4_2, currentVersion, versionToUpgradeTo);
    Preconditions.checkState(versionToUpgradeTo.isGreaterThan(currentVersion), "Can not downgrade from %s to %s", currentVersion, versionToUpgradeTo);
    int id = MetaDataStore.Position.KERNEL_VERSION.id();
    MetaDataRecord before = metaDataStore.newRecord();
    before.setId(id);
    before.initialize(true, currentVersion.version());
    MetaDataRecord after = metaDataStore.newRecord();
    after.setId(id);
    after.initialize(true, versionToUpgradeTo.version());
    // This command will be the first one in the "new" version, indicating the switch and writing it to the MetaDataStore
    LogCommandSerialization serialization = RecordStorageCommandReaderFactory.INSTANCE.get(versionToUpgradeTo);
    var commands = new ArrayList<StorageCommand>();
    commands.add(new Command.MetaDataCommand(serialization, before, after));
    // it in the schemaStore.
    if (currentVersion.isLessThan(KernelVersion.VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED)) {
        commands.add(createSchemaUpgradeCommand(serialization, injectedNLIUpgradeCallback));
    }
    return commands;
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion) StorageCommand(org.neo4j.storageengine.api.StorageCommand) MetaDataStore(org.neo4j.kernel.impl.store.MetaDataStore) ArrayList(java.util.ArrayList) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord)

Example 47 with StorageCommand

use of org.neo4j.storageengine.api.StorageCommand in project neo4j by neo4j.

the class CheckTxLogs method scan.

private <C extends Command, R extends AbstractBaseRecord> boolean scan(PhysicalLogFiles logFiles, InconsistenciesHandler handler, CheckType<C, R> check, boolean checkTxIds) throws IOException {
    out.println("Checking logs for " + check.name() + " inconsistencies");
    CommittedRecords<R> state = new CommittedRecords<>(check);
    List<CommandAndLogVersion> txCommands = new ArrayList<>();
    boolean validLogs = true;
    long commandsRead = 0;
    long lastSeenTxId = BASE_TX_ID;
    try (LogEntryCursor logEntryCursor = LogTestUtils.openLogs(fs, logFiles)) {
        while (logEntryCursor.next()) {
            LogEntry entry = logEntryCursor.get();
            if (entry instanceof LogEntryCommand) {
                StorageCommand command = ((LogEntryCommand) entry).getXaCommand();
                if (check.commandClass().isInstance(command)) {
                    long logVersion = logEntryCursor.getCurrentLogVersion();
                    txCommands.add(new CommandAndLogVersion(command, logVersion));
                }
            } else if (entry instanceof LogEntryCommit) {
                long txId = ((LogEntryCommit) entry).getTxId();
                if (checkTxIds) {
                    validLogs &= checkNoDuplicatedTxsInTheLog(lastSeenTxId, txId, handler);
                    lastSeenTxId = txId;
                }
                for (CommandAndLogVersion txCommand : txCommands) {
                    validLogs &= checkAndHandleInconsistencies(txCommand, check, state, txId, handler);
                }
                txCommands.clear();
            }
            commandsRead++;
        }
    }
    out.println("Processed " + commandsRead + " commands");
    out.println(state);
    if (!txCommands.isEmpty()) {
        out.println("Found " + txCommands.size() + " uncommitted commands at the end.");
        for (CommandAndLogVersion txCommand : txCommands) {
            validLogs &= checkAndHandleInconsistencies(txCommand, check, state, -1, handler);
        }
        txCommands.clear();
    }
    return validLogs;
}
Also used : LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ArrayList(java.util.ArrayList) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry)

Example 48 with StorageCommand

use of org.neo4j.storageengine.api.StorageCommand in project neo4j by neo4j.

the class LegacyLogEntryWriter method writeAllLogEntries.

public void writeAllLogEntries(LogVersionedStoreChannel channel, IOCursor<LogEntry> cursor) throws IOException {
    try (PositionAwarePhysicalFlushableChannel writable = new PositionAwarePhysicalFlushableChannel(channel)) {
        final LogEntryWriter writer = factory.apply(writable);
        List<StorageCommand> commands = new ArrayList<>();
        while (cursor.next()) {
            LogEntry entry = cursor.get();
            if (entry instanceof LogEntryStart) {
                final LogEntryStart startEntry = entry.as();
                writer.writeStartEntry(startEntry.getMasterId(), startEntry.getLocalId(), startEntry.getTimeWritten(), startEntry.getLastCommittedTxWhenTransactionStarted(), startEntry.getAdditionalHeader());
            } else if (entry instanceof LogEntryCommit) {
                if (!commands.isEmpty()) {
                    writer.serialize(new PhysicalTransactionRepresentation(commands));
                    commands = new ArrayList<>();
                }
                final LogEntryCommit commitEntry = (LogEntryCommit) entry;
                writer.writeCommitEntry(commitEntry.getTxId(), commitEntry.getTimeWritten());
            } else if (entry instanceof LogEntryCommand) {
                commands.add(((LogEntryCommand) entry).getXaCommand());
            } else {
                throw new IllegalStateException("Unknown entry: " + entry);
            }
        }
    }
}
Also used : LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) PositionAwarePhysicalFlushableChannel(org.neo4j.kernel.impl.transaction.log.PositionAwarePhysicalFlushableChannel) StorageCommand(org.neo4j.storageengine.api.StorageCommand) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) ArrayList(java.util.ArrayList) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)

Example 49 with StorageCommand

use of org.neo4j.storageengine.api.StorageCommand in project neo4j by neo4j.

the class PhysicalTransactionRepresentation method toString.

@Override
public String toString() {
    StringBuilder builder = new StringBuilder(getClass().getSimpleName() + "[");
    builder.append("masterId:" + masterId + ",");
    builder.append("authorId:" + authorId + ",");
    builder.append("timeStarted:" + timeStarted + ",");
    builder.append("latestCommittedTxWhenStarted:" + latestCommittedTxWhenStarted + ",");
    builder.append("timeCommitted:" + timeCommitted + ",");
    builder.append("lockSession:" + lockSessionIdentifier + ",");
    builder.append("additionalHeader:" + Arrays.toString(additionalHeader));
    for (StorageCommand command : commands) {
        builder.append("\n" + command);
    }
    return builder.toString();
}
Also used : StorageCommand(org.neo4j.storageengine.api.StorageCommand)

Example 50 with StorageCommand

use of org.neo4j.storageengine.api.StorageCommand in project neo4j by neo4j.

the class ReplicatedTokenHolderTest method mockedStorageEngine.

@SuppressWarnings("unchecked")
private StorageEngine mockedStorageEngine() throws Exception {
    StorageEngine storageEngine = mock(StorageEngine.class);
    doAnswer(invocation -> {
        Collection<StorageCommand> target = invocation.getArgumentAt(0, Collection.class);
        ReadableTransactionState txState = invocation.getArgumentAt(1, ReadableTransactionState.class);
        txState.accept(new TxStateVisitor.Adapter() {

            @Override
            public void visitCreatedLabelToken(String name, int id) {
                LabelTokenRecord before = new LabelTokenRecord(id);
                LabelTokenRecord after = before.clone();
                after.setInUse(true);
                target.add(new Command.LabelTokenCommand(before, after));
            }
        });
        return null;
    }).when(storageEngine).createCommands(anyCollection(), any(ReadableTransactionState.class), any(StorageStatement.class), any(ResourceLocker.class), anyLong());
    StoreReadLayer readLayer = mock(StoreReadLayer.class);
    when(readLayer.newStatement()).thenReturn(mock(StorageStatement.class));
    when(storageEngine.storeReadLayer()).thenReturn(readLayer);
    return storageEngine;
}
Also used : TxStateVisitor(org.neo4j.storageengine.api.txstate.TxStateVisitor) StorageStatement(org.neo4j.storageengine.api.StorageStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ResourceLocker(org.neo4j.storageengine.api.lock.ResourceLocker) ReadableTransactionState(org.neo4j.storageengine.api.txstate.ReadableTransactionState) StorageEngine(org.neo4j.storageengine.api.StorageEngine) LabelTokenRecord(org.neo4j.kernel.impl.store.record.LabelTokenRecord)

Aggregations

StorageCommand (org.neo4j.storageengine.api.StorageCommand)77 ArrayList (java.util.ArrayList)39 Test (org.junit.jupiter.api.Test)34 CommandReader (org.neo4j.storageengine.api.CommandReader)23 InMemoryClosableChannel (org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel)22 RepeatedTest (org.junit.jupiter.api.RepeatedTest)19 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)14 LongArrayList (org.eclipse.collections.impl.list.mutable.primitive.LongArrayList)10 Test (org.junit.Test)10 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)9 RelationshipGroupRecord (org.neo4j.kernel.impl.store.record.RelationshipGroupRecord)9 PropertyRecord (org.neo4j.kernel.impl.store.record.PropertyRecord)8 Command (org.neo4j.kernel.impl.transaction.command.Command)7 NodeCommand (org.neo4j.kernel.impl.transaction.command.Command.NodeCommand)7 LogEntryCommand (org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand)7 SchemaRuleCommand (org.neo4j.internal.recordstorage.Command.SchemaRuleCommand)6 NeoStores (org.neo4j.kernel.impl.store.NeoStores)6 IOException (java.io.IOException)5 PropertyCommand (org.neo4j.internal.recordstorage.Command.PropertyCommand)5 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)5