Class CancellationManager

java.lang.Object
com.complexible.common.cancellation.CancellationManager
All Implemented Interfaces:
Disposable

public final class CancellationManager extends Object implements Disposable
This is general abstraction to make 5 main functions: 1) Create cancellation point for specific database; Locks global mutex in readLock mode; Takes the database-related mutex briefly; 2) Execute exclusive database operation for specific database; Locks global mutex in readLock mode; Holds the database-related mutex for the whole operation; So it is impossible to create new cancellation point or to execute critical section while this lock is acquired; 3) Unregister cancellation point; Does not have locks with other operations; 4) Cancel cancellation points; Does not have locks with other operations; 5) Dispose cancellation manager (for all databases); Locks global mutex in write lock mode; It will be impossible to run all parallel methods during dispose;
  • Constructor Details

    • CancellationManager

      public CancellationManager()
    • CancellationManager

      public CancellationManager(long cancelTimeoutMs, long databaseLockTimeoutMs, long forceCloseWaitMs, long disposeTimeoutMs)
      Parameters:
      cancelTimeoutMs - how long to wait for cancelled cancellation points to finish
      databaseLockTimeoutMs - how long to wait for the per-db lock; an exclusive operation waits twice this long
      forceCloseWaitMs - how long an exclusive operation waits before it calls its force-close callback
      disposeTimeoutMs - how long shutdown waits for stuck cancellation points; 0 waits forever
  • Method Details

    • defaultCancelTimeoutMs

      public static long defaultCancelTimeoutMs()
      Default timeouts. Each one can be set with the JVM system property, or in the server with stardog.properties.
    • defaultDatabaseLockTimeoutMs

      public static long defaultDatabaseLockTimeoutMs()
      See Also:
    • defaultForceCloseWaitMs

      public static long defaultForceCloseWaitMs()
      See Also:
    • defaultShutdownTimeoutMs

      public static long defaultShutdownTimeoutMs()
      See Also:
    • registerCancellationPoint

      public CancellationPoint registerCancellationPoint(String theCancellationMessage, String theDatabaseName)
      Register cancellation point with default thrower;
      Parameters:
      theCancellationMessage - - the cancellation message;
      theDatabaseName - - the database name;
      Returns:
      created cancellation point;
    • registerCancellationPoint

      public CancellationPoint registerCancellationPoint(String theCancellationMessage, String theDatabaseName, Function<String,? extends RuntimeException> theCancellationThrower)
      This method is used to register new cancellation point;
      Parameters:
      theCancellationMessage - - the new cancellation message;
      theDatabaseName - - the database name;
      theCancellationThrower - - the thrower which will be used;
      Returns:
      created cancellation point;
    • registerDatabase

      public void registerDatabase(String theDatabaseName)
      Register database in the system;
      Parameters:
      theDatabaseName - - the database name;
    • unregisterDatabase

      public void unregisterDatabase(String theDatabaseName)
      De-register database;
      Parameters:
      theDatabaseName - - the database name;
    • executeExclusiveDatabaseOperation

      public <T> T executeExclusiveDatabaseOperation(String theDatabaseName, String theProcessDescription, Runnable theForceClose, Callable<T> theWorker)
      Executes database-critical operation; It acquires global lock in shared mode; It acquires local per-db lock in exclusive mode;
      Parameters:
      theDatabaseName - - the database name;
      theProcessDescription - - the description of process;
      theForceClose - - Callback if not all CPs close after a timeout
      theWorker - - the worked for execution;
      Returns:
      the result of the Worker's call;
    • executeExclusiveDatabaseOperation

      public <T> T executeExclusiveDatabaseOperation(String theDatabaseName, String theProcessDescription, Runnable theForceClose, CancellationPoint theIgnoreCp, long timeoutMs, Callable<T> theWorker)
      Variant that does NOT cancel a specific CancellationPoint already held by the caller. Use when the calling thread itself owns a registered CP (e.g. a long-running admin operation that took a CP via its process monitor) and would otherwise deadlock waiting for itself to finish.
      Parameters:
      theIgnoreCp - CancellationPoint to exclude from the cancel-and-wait step
      See Also:
    • executeExclusiveDatabaseOperation

      public <T> T executeExclusiveDatabaseOperation(String theDatabaseName, String theProcessDescription, Callable<T> theWorker)
      See Also:
    • executeExclusiveDatabaseOperation

      public <T> T executeExclusiveDatabaseOperation(String theDatabaseName, String theProcessDescription, Runnable theForceClose, CancellationManager.CheckedFunction<CancellationPoint,T> theWorker)
      Executes database-critical operation; Creates a cancellation point which may be used to cancel the operation. It is not possible to create any new Cancellation Points before this method returns. It acquires global lock in shared mode; It acquires local per-db lock in exclusive mode;
      Parameters:
      theForceClose - callback if there are remaining CPs not closed after a timeout.
      theWorker - the callable which will be executed with the CP
      See Also:
    • cancel

      public void cancel()
      Cancel all registered cancellation points;
    • predisposed

      public void predisposed()
    • cancel

      public void cancel(String theDatabaseName)
      Cancel all registered cancellation points for the specific database.
      Parameters:
      theDatabaseName - - the database's name;
    • dispose

      public void dispose()
      Dispose cancellation manager; It cancels all active cancellation points and cleanup all resources;
      Specified by:
      dispose in interface Disposable
    • wait

      public void wait(String dbname, String theProcessDescription, long waitTimeMs)
    • countCancellationPoint

      public int countCancellationPoint(String db)