Package titan.tasks

Class ProcessRegistry

java.lang.Object
titan.tasks.ProcessRegistry

public class ProcessRegistry extends Object
A registry for managing and cleaning up processes, particularly those that might become "zombied" due to abrupt worker node shutdowns or crashes. This class provides mechanisms to register running processes, unregister them, and on startup, load previously registered processes from a persistent file to check for and forcibly terminate any lingering zombie processes.

The registry maintains an in-memory map of service IDs to process IDs (PIDs) and persists this information to a local file to ensure state can be recovered across restarts.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final Map<String,Long>
    An in-memory concurrent map that stores the mapping from a service identifier to its process ID (PID).
    private static final String
    The name of the file used for persisting the process registry data to disk.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Loads the process registry from the persistent file, checks for any processes that are still running (potential zombies), and forcibly terminates them.
    static void
    register(String serviceId, long pid)
    Registers a new process with the registry.
    private static void
    Persists the current state of the in-memory process registry to the designated file.
    static void
    unregister(String serviceId)
    Unregisters a process from the registry.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • persistentMap

      private static final Map<String,Long> persistentMap
      An in-memory concurrent map that stores the mapping from a service identifier to its process ID (PID). This map is used to keep track of active processes that should be persisted to disk.
    • PROCESS_REGISTRY_FILE

      private static final String PROCESS_REGISTRY_FILE
      The name of the file used for persisting the process registry data to disk. This file stores service ID to PID mappings, allowing the system to recover and clean up processes after a restart.
      See Also:
  • Constructor Details

    • ProcessRegistry

      public ProcessRegistry()
  • Method Details

    • loadAndCleanUpProcesses

      public static void loadAndCleanUpProcesses()
      Loads the process registry from the persistent file, checks for any processes that are still running (potential zombies), and forcibly terminates them. After checking and cleaning up, the persistent file is cleared to ensure a fresh start.

      This method is typically called during application startup to handle cases where processes might have been left running due to an unexpected shutdown.

      Any errors encountered during file reading or process termination are logged but do not prevent the application from starting.
    • register

      public static void register(String serviceId, long pid)
      Registers a new process with the registry. The service ID and its corresponding PID are stored in the in-memory map and immediately persisted to disk.
      Parameters:
      serviceId - The unique identifier for the service or task.
      pid - The process ID (PID) of the running process associated with the service.
    • unregister

      public static void unregister(String serviceId)
      Unregisters a process from the registry. The entry corresponding to the given service ID is removed from the in-memory map, and the updated state is immediately persisted to disk.
      Parameters:
      serviceId - The unique identifier of the service or task to unregister.
    • saveToDisk

      private static void saveToDisk()
      Persists the current state of the in-memory process registry to the designated file. Each entry (service ID and PID) is written as a comma-separated line. This method is synchronized to prevent concurrent writes to the file.