-- Worker.mesa
-- Defines the volatile and persistent state of a worker.
-- Worker.Handle is the concrete form of AlpineInternal.TransactionHandle.
-- Last edited by
--   MBrown on January 30, 1984 11:50:47 am PST
--   Taft on  1-Feb-82 12:57:26

  DIRECTORY
    AlpineEnvironment USING [Outcome, TransID, nullTransID],
    AlpineImport USING [Handle, nullHandle],
    AlpineInternal USING [Difficulty, FileInstanceHandle, LockTransHeaderHandle,
      LogRecordID, nullLogRecordID, WorkerOutcome, WorkLevel],
    BasicTime USING [GMT],
    Process USING [Milliseconds],
    RPC USING [ConversationID, maxShortStringLength];

Worker: DEFINITIONS =
  BEGIN

  State: TYPE = { unknown, active, preparing, ready, completing,
    fpmComplete, fpmCompleteBeingForcedOut, complete };

  StateDuringRecovery: TYPE = { active, ready, committed, aborted };

  Outcome: TYPE = AlpineEnvironment.Outcome --{abort, commit, unknown}--;

  Object: TYPE = MONITORED RECORD [
    transID: AlpineEnvironment.TransID ← AlpineEnvironment.nullTransID, --immutable--
    beginRecord: AlpineInternal.LogRecordID ← AlpineInternal.nullLogRecordID, --immutable--
    coordinator: AlpineImport.Handle ← AlpineImport.nullHandle, --immutable--
    locks: AlpineInternal.LockTransHeaderHandle ← NIL, --immutable--
    coordinatorIsRemote: BOOL ← TRUE,
    state: State ← unknown, --increases monotonically to complete--
    outcome: Outcome ← unknown, --changes during state transition ready -> completing--
    allowableDifficulty: Difficulty ← hard,
      -- hard -> normal -> zero during preparing or completing state.
      -- Prevents new actions that dirty owner DB caches (CreateFile, ...) from starting
      --during Prepare.  ReadPages and WritePages are allowed during Prepare.
    nStarts: [0..maxStarts] ← 0,
      -- Number of actions in progress for this worker.  More precisely, the
      --number of StartWorking calls that returned TRUE, minus number of StopWorking calls.
    stateDuringRecovery: StateDuringRecovery ← active,
      -- State maintained during the analysis pass of recovery.
    timeOfLastStartWork: BasicTime.GMT,
      -- Time of last successful call to StartWork.
    estimatedUpdateCost: INT ← 0,
      -- 0 means that the transaction is readonly.  The estimatedUpdateCost increases during
      --the transaction.
    fileInstanceList: AlpineInternal.FileInstanceHandle ← NIL,
      -- Data structure giving access to all files opened by this transaction.
    enabledWheelList: LIST OF RPC.ConversationID ← NIL,
      -- List of all conversations that are enabled to perform "protected" Alpine operations
      --under this transaction.
    continueWorker: Handle ← nullHandle, --holds continuation transaction, if any--
    next: Handle ← nullHandle --WorkerMap data structure--
    ];

  Handle: TYPE = REF Object;
  nullHandle: Handle = NIL;

  shortWaitTime: Process.Milliseconds = 100;
    -- Timeout for a condition variable on which workers wait in order to let
    --some transient condition pass.

  
  Difficulty: TYPE = AlpineInternal.Difficulty; --{zero, normal, hard}
  WorkLevel: TYPE = AlpineInternal.WorkLevel; --Difficulty [normal .. hard]
    -- no interface procedure involving transactions has zero difficulty
    -- most have normal difficulty
    -- anything that can dirty a cache that must be flushed during Prepare is hard.
    
  maxStarts: CARDINAL = 7;
    -- Bound on number of concurrent actions allowed for a single worker.


  -- Persistent worker state.
  -- Note that workerReady and workerComplete log records contain no information
  --other than the transaction ID, so we don't define structures for them here.

  BeginLogRep: TYPE = MACHINE DEPENDENT RECORD [
    coordinator: StrBody];
  CompletingLogRep: TYPE = MACHINE DEPENDENT RECORD [
    outcome: AlpineInternal.WorkerOutcome];
  StrBody: TYPE = MACHINE DEPENDENT RECORD [
    length: CARDINAL ← 0, maxLength: CARDINAL ← RPC.maxShortStringLength,
    text: PACKED ARRAY[0..RPC.maxShortStringLength) OF CHAR];

  END.