Package titan.network
Class SchedulerServer
java.lang.Object
titan.network.SchedulerServer
The
SchedulerServer class acts as the central network endpoint for the Titan distributed scheduling system.
It listens for incoming client connections, processes various commands from workers and clients (e.g., registration,
job submission, asset management, statistics requests), and delegates core scheduling logic to an associated Scheduler instance.
This server operates in a multithreaded manner, using an ExecutorService to handle each client connection concurrently,
ensuring responsiveness and scalability.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate static final Stringprivate final int(package private) Schedulerprivate final ServerSocketprivate final ExecutorService -
Constructor Summary
ConstructorsConstructorDescriptionSchedulerServer(int port, Scheduler scheduler) Constructs a newSchedulerServerinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclientHandler(Socket socket) Handles a single client connection from end-to-end.private FilefindFileRecursive(String fileName) Searches for a specified file within thePERM_FILES_DIRand its subdirectories.private StringhandleDeployRequest(String fileName, String port, String requirement) Processes a request to deploy a file (e.g., a JAR or script) to a worker.private StringhandleRegistration(Socket socket, String request) Handles a worker registration request from a client.private StringhandleRunScript(String fileName, String requirement) Handles a request to execute a script or an executable file on a worker.private voidparseAndSubmitDAG(String request) Parses a string containing multiple job definitions for a Directed Acyclic Graph (DAG) and submits each job to theScheduler.private StringDispatches incomingTitanProtocol.TitanPackets to the appropriate handler method based on theiropCode.voidstart()Starts theSchedulerServer, making it listen for incoming client connections.voidstop()Shuts down theSchedulerServergracefully.
-
Field Details
-
port
private final int port -
isRunning
private boolean isRunning -
threadPool
-
scheduler
Scheduler scheduler -
serverSocket
-
PERM_FILES_DIR
- See Also:
-
-
Constructor Details
-
SchedulerServer
Constructs a newSchedulerServerinstance. Initializes the server to listen on the specified port and associates it with the providedScheduler. A cached thread pool is created to manage client connections.- Parameters:
port- The port number on which the server will listen for incoming client connections.scheduler- TheSchedulerinstance responsible for managing workers, jobs, and system state.- Throws:
IOException- If an I/O error occurs when attempting to open the server socket on the specified port.
-
-
Method Details
-
start
public void start()Starts theSchedulerServer, making it listen for incoming client connections. This method enters a continuous loop, accepting new client sockets and submitting them to the internal thread pool for asynchronous handling by theclientHandler(Socket)method. The server will continue to run until thestop()method is called or a critical error occurs. System messages are printed to indicate the server's listening status. -
handleRegistration
Handles a worker registration request from a client. This method parses the registration payload, extracts the worker's port, capabilities, and permanence status, and then registers the worker with the centralScheduler.- Parameters:
socket- TheSocketobject representing the client connection, used to determine the worker's host address.request- The payload string containing registration details, typically in the format "workerPort||capability||isPerm".- Returns:
- A string indicating the outcome of the registration, e.g., "REGISTERED" on success or "ERROR_INVALID_REGISTRATION" on failure.
-
clientHandler
Handles a single client connection from end-to-end. This method reads an incomingTitanProtocol.TitanPacket, dispatches it to the appropriate command processing method based on its operation code, and then sends a responseTitanProtocol.TitanPacketback to the client. It usesDataInputStreamandDataOutputStreamfor binary protocol communication. Errors during processing are caught, logged, and an error response is sent to the client.- Parameters:
socket- TheSocketobject representing the client connection to be handled.
-
handleDeployRequest
Processes a request to deploy a file (e.g., a JAR or script) to a worker. It reads the specified file from the server'sperm_filesdirectory, encodes its content in Base64, and constructs aDEPLOY_PAYLOADjob. This job is then submitted to theSchedulerfor execution on a suitable worker.- Parameters:
fileName- The name of the file to be deployed. This file must exist in theperm_filesdirectory.port- The port number to associate with the deployed worker, primarily used when deploying a 'Worker.jar'. Can benullor empty.requirement- The capability requirement (e.g., "GPU", "GENERAL") for the worker that should execute this deployment job.- Returns:
- A string indicating the status of the deployment job, such as "DEPLOY_QUEUED" or an error message if the file is not found or an I/O issue occurs.
-
handleRunScript
Handles a request to execute a script or an executable file on a worker. This method searches for the specified file recursively within theperm_filesdirectory, encodes its content in Base64, and creates aRUN_PAYLOADjob. The job is then submitted to theSchedulerfor execution on a worker matching the specified requirements.- Parameters:
fileName- The name of the script or executable file to be run.requirement- The capability requirement (e.g., "PYTHON", "GENERAL") for the worker that should execute this run job.- Returns:
- A string indicating the status of the job, such as "JOB_QUEUED" along with the job ID, or an error message if the file is not found or an I/O issue occurs.
-
parseAndSubmitDAG
Parses a string containing multiple job definitions for a Directed Acyclic Graph (DAG) and submits each job to theScheduler. Each job definition within the request string is expected to be separated by a semicolon (;). Jobs are parsed usingJob.fromDagString(String).- Parameters:
request- A string containing one or more job definitions, typically in a format like "job1_def;job2_def;..." where each definition can be parsed into aJobobject.
-
processCommand
Dispatches incomingTitanProtocol.TitanPackets to the appropriate handler method based on theiropCode. This method acts as the central command router for all operations received from clients, excluding initial worker registration. It supports a wide range of operations including deployment, script execution, system statistics, service management, job submission (single and DAG), worker control, logging, asset transfer, and key-value store interactions.- Parameters:
packet- TheTitanProtocol.TitanPacketreceived from the client, containing the operation code and its payload.- Returns:
- A response string to be sent back to the client, indicating the result of the command execution or an error message.
-
findFileRecursive
Searches for a specified file within thePERM_FILES_DIRand its subdirectories. This method first checks for a direct match in the root ofPERM_FILES_DIRand then performs a recursive walk.- Parameters:
fileName- The name of the file to search for.- Returns:
- A
Fileobject representing the found file, ornullif the file is not found or anIOExceptionoccurs during the directory traversal.
-
stop
public void stop()Shuts down theSchedulerServergracefully. This method sets a flag to terminate the main server listening loop, shuts down the internal thread pool, and attempts to close theServerSocketto release the bound port. AnyIOExceptionencountered during socket closure is caught and printed to the error stream.
-