public StateStorage create(final int processorId, final String processorName) { final String name = String.format("%d_%s", processorId, processorName); final File processorDirectory = new File(rootDirectory, name); final File runtimeDirectory = new File(processorDirectory, DEFAULT_RUNTIME_PATH); final File snapshotsDirectory = new File(processorDirectory, DEFAULT_SNAPSHOTS_PATH); if (!processorDirectory.exists()) { processorDirectory.mkdir(); } if (!snapshotsDirectory.exists()) { snapshotsDirectory.mkdir(); } return new StateStorage(runtimeDirectory, snapshotsDirectory); }
@Override public void purgeAll(Predicate<StateSnapshotMetadata> matcher) throws Exception { final List<StateSnapshotMetadata> others = storage.list(matcher); for (final StateSnapshotMetadata other : others) { FileUtil.deleteFolder(storage.getSnapshotDirectoryFor(other).getAbsolutePath()); LOG.trace("Purged snapshot {}", other); } }
@Override public StateSnapshotMetadata recover( long commitPosition, int term, Predicate<StateSnapshotMetadata> filter) throws Exception { final File runtimeDirectory = storage.getRuntimeDirectory(); final List<StateSnapshotMetadata> snapshots = storage.listRecoverable(commitPosition); StateSnapshotMetadata recoveredMetadata = null; if (!snapshots.isEmpty()) { recoveredMetadata = snapshots.stream() .sorted(Comparator.reverseOrder()) .filter(filter) .findFirst() .orElse(null); } if (runtimeDirectory.exists()) { FileUtil.deleteFolder(runtimeDirectory.getAbsolutePath()); } if (recoveredMetadata != null) { final File snapshotPath = storage.getSnapshotDirectoryFor(recoveredMetadata); copySnapshot(runtimeDirectory, snapshotPath); } else { recoveredMetadata = StateSnapshotMetadata.createInitial(term); } return recoveredMetadata; }
private boolean exists(final StateSnapshotMetadata metadata) { return storage.getSnapshotDirectoryFor(metadata).exists(); }
public List<StateSnapshotMetadata> list() { return list(s -> true); }
@Override public ZeebeDb openDb() { db = zeebeDbFactory.createDb(storage.getRuntimeDirectory()); return db; }
public List<StateSnapshotMetadata> list(Predicate<StateSnapshotMetadata> filter) { final File[] snapshotFolders = snapshotsDirectory.listFiles(); final List<StateSnapshotMetadata> snapshots = new ArrayList<>(); if (snapshotFolders == null || snapshotFolders.length == 0) { return snapshots; } for (final File folder : snapshotFolders) { if (folder.isDirectory()) { try { final StateSnapshotMetadata metadata = getSnapshotMetadata(folder); if (filter.test(metadata)) { snapshots.add(metadata); } } catch (final Exception ex) { LOG.warn("error listing snapshot {}", folder.getAbsolutePath()); } } } return snapshots; } }
@Override public StateSnapshotMetadata recover( long commitPosition, int term, Predicate<StateSnapshotMetadata> filter) throws Exception { final File runtimeDirectory = storage.getRuntimeDirectory(); final List<StateSnapshotMetadata> snapshots = storage.listRecoverable(commitPosition); StateSnapshotMetadata recoveredMetadata = null; if (!snapshots.isEmpty()) { recoveredMetadata = snapshots.stream() .sorted(Comparator.reverseOrder()) .filter(filter) .findFirst() .orElse(null); } if (runtimeDirectory.exists()) { FileUtil.deleteFolder(runtimeDirectory.getAbsolutePath()); } if (recoveredMetadata != null) { final File snapshotPath = storage.getSnapshotDirectoryFor(recoveredMetadata); copySnapshot(runtimeDirectory, snapshotPath); } else { recoveredMetadata = StateSnapshotMetadata.createInitial(term); } return recoveredMetadata; }
private boolean exists(final StateSnapshotMetadata metadata) { return storage.getSnapshotDirectoryFor(metadata).exists(); }
public List<StateSnapshotMetadata> list() { return list(s -> true); }
@Override public ZeebeDb openDb() { db = zeebeDbFactory.createDb(storage.getRuntimeDirectory()); return db; }
public List<StateSnapshotMetadata> list(Predicate<StateSnapshotMetadata> filter) { final File[] snapshotFolders = snapshotsDirectory.listFiles(); final List<StateSnapshotMetadata> snapshots = new ArrayList<>(); if (snapshotFolders == null || snapshotFolders.length == 0) { return snapshots; } for (final File folder : snapshotFolders) { if (folder.isDirectory()) { try { final StateSnapshotMetadata metadata = getSnapshotMetadata(folder); if (filter.test(metadata)) { snapshots.add(metadata); } } catch (final Exception ex) { LOG.warn("error listing snapshot {}", folder.getAbsolutePath()); } } } return snapshots; } }
@Override public void purgeAll(Predicate<StateSnapshotMetadata> matcher) throws Exception { final List<StateSnapshotMetadata> others = storage.list(matcher); for (final StateSnapshotMetadata other : others) { FileUtil.deleteFolder(storage.getSnapshotDirectoryFor(other).getAbsolutePath()); LOG.trace("Purged snapshot {}", other); } }
@Override public void takeSnapshot(final StateSnapshotMetadata metadata) { if (db == null) { throw new IllegalStateException("Cannot create snapshot of not open database."); } if (exists(metadata)) { return; } final File snapshotDir = storage.getSnapshotDirectoryFor(metadata); db.createSnapshot(snapshotDir); }
public List<StateSnapshotMetadata> listRecoverable(long lastSuccessfulProcessedEventPosition) { return list(s -> s.getLastWrittenEventPosition() <= lastSuccessfulProcessedEventPosition); }
public StateStorage create(final int processorId, final String processorName) { final String name = String.format("%d_%s", processorId, processorName); final File processorDirectory = new File(rootDirectory, name); final File runtimeDirectory = new File(processorDirectory, DEFAULT_RUNTIME_PATH); final File snapshotsDirectory = new File(processorDirectory, DEFAULT_SNAPSHOTS_PATH); if (!processorDirectory.exists()) { processorDirectory.mkdir(); } if (!snapshotsDirectory.exists()) { snapshotsDirectory.mkdir(); } return new StateStorage(runtimeDirectory, snapshotsDirectory); }
@Override public void takeSnapshot(final StateSnapshotMetadata metadata) { if (db == null) { throw new IllegalStateException("Cannot create snapshot of not open database."); } if (exists(metadata)) { return; } final File snapshotDir = storage.getSnapshotDirectoryFor(metadata); db.createSnapshot(snapshotDir); }
public List<StateSnapshotMetadata> listRecoverable(long lastSuccessfulProcessedEventPosition) { return list(s -> s.getLastWrittenEventPosition() <= lastSuccessfulProcessedEventPosition); }