Package com.illumon.iris.db.v2.remote
Class ConstructSnapshot
java.lang.Object
com.illumon.iris.db.v2.remote.ConstructSnapshot
A Set of static utilities for computing values from a table while avoiding the use of the LTM lock. This class supports
snapshots in both position space and key space.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static interface
static interface
static interface
Interface used to control snapshot behavior, including previous value usage and consistency testing.static interface
static class
Exception thrown for "fail-fast" purposes when it's been detected that a snapshot will fail.static final class
An implementation ofConstructSnapshot.SnapshotControl
for sources that cannot update.static interface
Interface forConstructSnapshot.UsePreviousValues.usePreviousValues(long)
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic com.fishlib.base.log.LogOutput
appendConcurrentAttemptClockInfo
(com.fishlib.base.log.LogOutput logOutput) Append clock info that pertains to the concurrent attempt state tologOutput
.static long
callDataSnapshotFunction
(com.fishlib.base.log.LogOutputAppendable logPrefix, ConstructSnapshot.SnapshotControl control, ConstructSnapshot.SnapshotFunction function) Invokes the snapshot function in a loop until it succeeds with provably consistent results, or untilMAX_CONCURRENT_ATTEMPTS
orMAX_CONCURRENT_ATTEMPT_DURATION_MILLIS
are exceeded.static long
callDataSnapshotFunction
(String logPrefix, ConstructSnapshot.SnapshotControl control, ConstructSnapshot.SnapshotFunction function) Invokes the snapshot function in a loop until it succeeds with provably consistent results, or untilMAX_CONCURRENT_ATTEMPTS
orMAX_CONCURRENT_ATTEMPT_DURATION_MILLIS
are exceeded.static boolean
Test that determines whether the currently-active concurrent snapshot attempt has become inconsistent.static InitialSnapshot
constructInitialSnapshot
(Object logIdentityObject, BaseTable table) Create asnapshot
of the entire table specified.static InitialSnapshot
constructInitialSnapshot
(Object logIdentityObject, BaseTable table, BitSet columnsToSerialize, Index keysToSnapshot) Create asnapshot
of the specified table using a set of requested columns and keys.static InitialSnapshot
constructInitialSnapshotInPositionSpace
(Object logIdentityObject, BaseTable table, BitSet columnsToSerialize, Index positionsToSnapshot) Create asnapshot
of the specified table using a set of requested columns and positions.static List<InitialSnapshot>
constructInitialSnapshots
(Object logIdentityObject, BaseTable... tables) ConstructsInitialSnapshot
s for the entirety of the tables.static long
estimateSnapshotSize
(Table table) Estimate the size of a complete table snapshot in bytes.static long
estimateSnapshotSize
(TableDefinition tableDefinition, BitSet columns, long rowCount) Make a rough guess at the size of a snapshot, using the column types and common column names.static void
Check that fails if the currently-active concurrent snapshot attempt has become inconsistent.static long
Return the currently-active concurrent snapshot attempt's "before" clock value, or zero if there is no concurrent attempt active.makeSnapshotControl
(boolean notificationAware, NotificationStepSource source) Make a defaultConstructSnapshot.SnapshotControl
for a single source.makeSnapshotControl
(boolean notificationAware, NotificationStepSource... sources) Make a defaultConstructSnapshot.SnapshotControl
for one or more sources.makeSnapshotControl
(ConstructSnapshot.UsePreviousValues usePreviousValues, ConstructSnapshot.SnapshotConsistent snapshotConsistent, ConstructSnapshot.SnapshotCompletedConsistently snapshotCompletedConsistently) static void
maybeWaitForSatisfaction
(NotificationQueue.Dependency dependency) Wait for a dependency to become satisfied on the current cycle if we're trying to use current values for the currently-active concurrent snapshot attempt.static boolean
serializeAllTable
(boolean usePrev, InitialSnapshot snapshot, BaseTable table, Object logIdentityObject, BitSet columnsToSerialize, Index keysToSnapshot) Populate anInitialSnapshot
with the specified keys and columns to snapshot.
-
Constructor Details
-
ConstructSnapshot
public ConstructSnapshot()
-
-
Method Details
-
concurrentAttemptInconsistent
public static boolean concurrentAttemptInconsistent()Test that determines whether the currently-active concurrent snapshot attempt has become inconsistent. Always returnsfalse
if there is no snapshot attempt active, or if there is a locked attempt active (necessarily at lower depth than the lowest concurrent attempt).- Returns:
- Whether the clock or sources have changed in such a way as to make the currently-active concurrent snapshot attempt inconsistent
-
failIfConcurrentAttemptInconsistent
public static void failIfConcurrentAttemptInconsistent()Check that fails if the currently-active concurrent snapshot attempt has become inconsistent. source. This is a no-op if there is no snapshot attempt active, or if there is a locked attempt active (necessarily at lower depth than the lowest concurrent attempt).- Throws:
ConstructSnapshot.SnapshotInconsistentException
- If the currently-active concurrent snapshot attempt has become inconsistent
-
maybeWaitForSatisfaction
Wait for a dependency to become satisfied on the current cycle if we're trying to use current values for the currently-active concurrent snapshot attempt. This is a no-op if there is no snapshot attempt active, or if there is a locked attempt active (necessarily at lower depth than the lowest concurrent attempt).- Parameters:
dependency
- The dependency, which may be null in order to avoid redundant checks in calling code- Throws:
ConstructSnapshot.SnapshotInconsistentException
- If we cannot wait for this dependency on the current step because the step changed
-
getConcurrentAttemptClockValue
public static long getConcurrentAttemptClockValue()Return the currently-active concurrent snapshot attempt's "before" clock value, or zero if there is no concurrent attempt active.- Returns:
- The concurrent snapshot attempt's "before" clock value, or zero
-
appendConcurrentAttemptClockInfo
public static com.fishlib.base.log.LogOutput appendConcurrentAttemptClockInfo(@NotNull com.fishlib.base.log.LogOutput logOutput) Append clock info that pertains to the concurrent attempt state tologOutput
.- Parameters:
logOutput
- TheLogOutput
- Returns:
logOutput
-
constructInitialSnapshot
public static InitialSnapshot constructInitialSnapshot(Object logIdentityObject, @NotNull BaseTable table) Create asnapshot
of the entire table specified. Note that this method is notification-oblivious, i.e. it makes no attempt to ensure that notifications are not missed.- Parameters:
logIdentityObject
- An object used to prepend to log rows.table
- the table to snapshot.- Returns:
- a snapshot of the entire base table.
-
constructInitialSnapshot
public static InitialSnapshot constructInitialSnapshot(Object logIdentityObject, @NotNull BaseTable table, BitSet columnsToSerialize, Index keysToSnapshot) Create asnapshot
of the specified table using a set of requested columns and keys. Note that this method uses an index that is in Key space, and that it is notification-oblivious, i.e. it makes no attempt to ensure that notifications are not missed.- Parameters:
logIdentityObject
- An object used to prepend to log rows.table
- the table to snapshot.columnsToSerialize
- a bitset of columns to include in the snapshotkeysToSnapshot
- an Index of keys to include in the snapshot- Returns:
- a snapshot of the entire base table.
-
constructInitialSnapshotInPositionSpace
public static InitialSnapshot constructInitialSnapshotInPositionSpace(Object logIdentityObject, @NotNull BaseTable table, BitSet columnsToSerialize, Index positionsToSnapshot) Create asnapshot
of the specified table using a set of requested columns and positions. Note that this method uses an index that is in Position space, and that it is notification-oblivious, i.e. it makes no attempt to ensure that notifications are not missed.- Parameters:
logIdentityObject
- An object used to prepend to log rows.table
- the table to snapshot.columnsToSerialize
- a bitset of columns to include in the snapshotpositionsToSnapshot
- an Index of keys to include in the snapshot- Returns:
- a snapshot of the entire base table.
-
constructInitialSnapshots
public static List<InitialSnapshot> constructInitialSnapshots(Object logIdentityObject, BaseTable... tables) ConstructsInitialSnapshot
s for the entirety of the tables. Note that this method is notification-oblivious, i.e. it makes no attempt to ensure that notifications are not missed.- Parameters:
logIdentityObject
- identifier prefixing the log messagetables
- tables to snapshot- Returns:
- list of the resulting
InitialSnapshot
s
-
makeSnapshotControl
public static ConstructSnapshot.SnapshotControl makeSnapshotControl(@NotNull ConstructSnapshot.UsePreviousValues usePreviousValues, @NotNull ConstructSnapshot.SnapshotConsistent snapshotConsistent, @Nullable ConstructSnapshot.SnapshotCompletedConsistently snapshotCompletedConsistently) -
makeSnapshotControl
public static ConstructSnapshot.SnapshotControl makeSnapshotControl(boolean notificationAware, @NotNull NotificationStepSource source) Make a defaultConstructSnapshot.SnapshotControl
for a single source.- Parameters:
notificationAware
- Whether the result should be concerned with not missing notificationssource
- The source- Returns:
- An appropriate
ConstructSnapshot.SnapshotControl
-
makeSnapshotControl
public static ConstructSnapshot.SnapshotControl makeSnapshotControl(boolean notificationAware, @NotNull NotificationStepSource... sources) Make a defaultConstructSnapshot.SnapshotControl
for one or more sources.- Parameters:
notificationAware
- Whether the result should be concerned with not missing notificationssources
- The sources- Returns:
- An appropriate
ConstructSnapshot.SnapshotControl
-
callDataSnapshotFunction
public static long callDataSnapshotFunction(@NotNull String logPrefix, @NotNull ConstructSnapshot.SnapshotControl control, @NotNull ConstructSnapshot.SnapshotFunction function) Invokes the snapshot function in a loop until it succeeds with provably consistent results, or untilMAX_CONCURRENT_ATTEMPTS
orMAX_CONCURRENT_ATTEMPT_DURATION_MILLIS
are exceeded. Falls back to acquiring a shared LTM lock for a final attempt.- Parameters:
logPrefix
- A prefix for our log messagescontrol
- AConstructSnapshot.SnapshotControl
to define the parameters and consistency for this snapshotfunction
- The function to execute- Returns:
- The logical clock step that applied to this snapshot
-
callDataSnapshotFunction
public static long callDataSnapshotFunction(@NotNull com.fishlib.base.log.LogOutputAppendable logPrefix, @NotNull ConstructSnapshot.SnapshotControl control, @NotNull ConstructSnapshot.SnapshotFunction function) Invokes the snapshot function in a loop until it succeeds with provably consistent results, or untilMAX_CONCURRENT_ATTEMPTS
orMAX_CONCURRENT_ATTEMPT_DURATION_MILLIS
are exceeded. Falls back to acquiring a shared LTM lock for a final attempt.- Parameters:
logPrefix
- A prefix for our log messagescontrol
- AConstructSnapshot.SnapshotControl
to define the parameters and consistency for this snapshotfunction
- The function to execute- Returns:
- The logical clock step that applied to this snapshot
-
serializeAllTable
public static boolean serializeAllTable(boolean usePrev, InitialSnapshot snapshot, BaseTable table, Object logIdentityObject, BitSet columnsToSerialize, Index keysToSnapshot) Populate an
InitialSnapshot
with the specified keys and columns to snapshot.Note that care must be taken while using this method to ensure the underlying table is locked or does not change, otherwise the resulting snapshot may be inconsistent. In general users should instead use
constructInitialSnapshot(java.lang.Object, com.illumon.iris.db.v2.BaseTable)
for simple use cases orcallDataSnapshotFunction(int, com.illumon.iris.db.v2.remote.ConstructSnapshot.SnapshotControl, com.illumon.iris.db.v2.remote.ConstructSnapshot.SnapshotFunction)
for more advanced uses.- Parameters:
usePrev
- Whether to use previous valuessnapshot
- The snapshot to populatelogIdentityObject
- An object for use with log() messagescolumnsToSerialize
- ABitSet
of columns to include, null for allkeysToSnapshot
- An Index of keys within the table to include, null for all- Returns:
- Whether the snapshot succeeded
-
estimateSnapshotSize
Estimate the size of a complete table snapshot in bytes.- Parameters:
table
- the table to estimate- Returns:
- the estimated snapshot size in bytes.
-
estimateSnapshotSize
public static long estimateSnapshotSize(TableDefinition tableDefinition, BitSet columns, long rowCount) Make a rough guess at the size of a snapshot, using the column types and common column names. The use case is when a user requests something from the GUI; we'd like to know if it is ridiculous before actually doing it.- Parameters:
tableDefinition
- the table definitionmcolumns
- a bitset indicating which columns are includedrowCount
- how many rows of this data we'll be snapshotting- Returns:
- the estimated size of the snapshot
-