本文整理汇总了Java中io.atomix.copycat.server.Snapshottable类的典型用法代码示例。如果您正苦于以下问题:Java Snapshottable类的具体用法?Java Snapshottable怎么用?Java Snapshottable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Snapshottable类属于io.atomix.copycat.server包,在下文中一共展示了Snapshottable类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: takeSnapshot
import io.atomix.copycat.server.Snapshottable; //导入依赖的package包/类
/**
* Takes a snapshot of the state machine state if necessary.
* <p>
* Snapshots of the state machine are taken only once the log becomes compactable. This means snapshots
* are largely dependent on the storage configuration and ensures that snapshots are not taken more
* frequently than will benefit log compaction.
*/
private void takeSnapshot() {
state.checkThread();
// If no snapshot has been taken, take a snapshot and hold it in memory until the complete
// index has met the snapshot index. Note that this will be executed in the state machine thread.
// Snapshots are only taken of the state machine when the log becomes compactable. If the log compactor's
// compactIndex is greater than the last snapshot index and the lastApplied index is greater than the
// last snapshot index, take the snapshot.
Snapshot currentSnapshot = state.getSnapshotStore().currentSnapshot();
if (pendingSnapshot == null && stateMachine instanceof Snapshottable
&& (currentSnapshot == null || (log.compactor().compactIndex() > currentSnapshot.index() && lastApplied > currentSnapshot.index()))) {
pendingSnapshot = state.getSnapshotStore().createSnapshot(lastApplied);
// Write the snapshot data. Note that we don't complete the snapshot here since the completion
// of a snapshot is predicated on session events being received by clients up to the snapshot index.
LOGGER.info("{} - Taking snapshot {}", state.getCluster().member().address(), pendingSnapshot.index());
executor.executor().execute(() -> {
synchronized (pendingSnapshot) {
try (SnapshotWriter writer = pendingSnapshot.writer()) {
((Snapshottable) stateMachine).snapshot(writer);
}
}
});
}
}
示例2: installSnapshot
import io.atomix.copycat.server.Snapshottable; //导入依赖的package包/类
/**
* Installs a snapshot of the state machine state if necessary.
* <p>
* Snapshots are installed only if there's a local snapshot stored with a version equal to the
* last applied index.
*/
private void installSnapshot() {
state.checkThread();
// If the last stored snapshot has not yet been installed and its index matches the last applied state
// machine index, install the snapshot. This requires that the state machine see all indexes sequentially
// even for entries that have been compacted from the log.
Snapshot currentSnapshot = state.getSnapshotStore().currentSnapshot();
if (currentSnapshot != null && currentSnapshot.index() > log.compactor().snapshotIndex() && currentSnapshot.index() == lastApplied && stateMachine instanceof Snapshottable) {
// Install the snapshot in the state machine thread. Multiple threads can access snapshots, so we
// synchronize on the snapshot object. In practice, this probably isn't even necessary and could prove
// to be an expensive operation. Snapshots can be read concurrently with separate SnapshotReaders since
// memory snapshots are copied to the reader and file snapshots open a separate FileBuffer for each reader.
LOGGER.info("{} - Installing snapshot {}", state.getCluster().member().address(), currentSnapshot.index());
executor.executor().execute(() -> {
synchronized (currentSnapshot) {
try (SnapshotReader reader = currentSnapshot.reader()) {
((Snapshottable) stateMachine).install(reader);
}
}
});
// Once a snapshot has been applied, snapshot dependent entries can be cleaned from the log.
log.compactor().snapshotIndex(currentSnapshot.index());
}
}
示例3: reset
import io.atomix.copycat.server.Snapshottable; //导入依赖的package包/类
/**
* Resets the state log.
*
* @return The server context.
*/
ServerContext reset() {
// Delete the existing log.
if (log != null) {
log.close();
storage.deleteLog(name);
}
// Delete the existing snapshot store.
if (snapshot != null) {
snapshot.close();
storage.deleteSnapshotStore(name);
}
// Open the log.
log = storage.openLog(name);
// Open the snapshot store.
snapshot = storage.openSnapshotStore(name);
// Create a new user state machine.
StateMachine stateMachine = stateMachineFactory.get();
// Configure the log compaction mode. If the state machine supports snapshotting, the default
// compaction mode is SNAPSHOT, otherwise the default is SEQUENTIAL.
if (stateMachine instanceof Snapshottable) {
log.compactor().withDefaultCompactionMode(Compaction.Mode.SNAPSHOT);
} else {
log.compactor().withDefaultCompactionMode(Compaction.Mode.SEQUENTIAL);
}
// Create a new internal server state machine.
this.stateMachine = new ServerStateMachine(stateMachine, this, stateContext);
return this;
}