Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Amygdala crate

This is a binary crate so main.rs and the main() method are the main entry points.

Process Overview

The central type in amygdala is the Launcher and its implementation of the Launch trait (from the amygdala-api crate). This is wrapped in tonic infrastructure to make it available via gRPC.

The Launcher holds an instance of a ProcessManager that manages the actual GDA server while it handles incoming requests, delegating them to the process manager and converting the response as required. As multiple requests can be received concurrently, the process manager is held in an Arc<Mutex< >> so that the ProcessManager type doesn't have to be thread safe.

The Launcher also holds the configuration loaded from the configuration file. This configuration is a wrapper around a lazy loading container (an Arc around a OnceLock) allowing the configuration to be cloneable and to be created before the configuration file it is based on is available.

When requests are received from clients, the request is combined with the configuration (which is loaded if needed) to create the specific configuration required for that request. This new derived configuration is passed into the ProcessManager where, if it was starting a new server, it is held for the lifetime of that process allowing relevant information to be queried for the current instance of the server.

The internal structure of the Launcher changed for version 0.3.0. Prior to this, the configuration was held in the process manager and the request was combined to create the derived configuration within the process manager methods.
Client start stop status client_detail version manage Configuration LazyConfiguration service process ProcessManager start stop status client_details Option<Process> ProcessManager Launcher

Configuration

There are two kinds of configuration.

Provided Configuration

The first, the 'provided' configuration, is that read from the config file. It is constant for the lifetime of the amygdala process.

Derived Configuration

The second, the 'derived' configuration, is the specific configuration required to start the server. It includes options provided in the start request as well as timestamped log files and build specific runtime directories. This configuration is created when the GDA server is started and dropped when it is shutdown. This configuration is the source of client details as they depend of the specific server that is running.

Startup Files

If the GDA server fails to start, it writes the fatal error message to a startup file. This provides an easy mechanism to determine the error that killed it instead of reading through the logs and guessing. The file that is written is set via the OBJECT_SERVER_STARTUP_FILE environment variable. Amygdala creates this file beforehand as a named pipe/fifo so that it can be instantly notified when it is written to instead of polling the filesystem. This file is deleted when it is dropped so that the file is not left behind in the case where the server did not write to it.

Cancelling Requests

There are two kinds of request. The simpler request/response model used for version and manage where the client makes a request and the server sends a reponse (as in 'normal' http requests), and the more complex streaming request where the client makes a request and then the server sends a possibly variable number of response messages asynchronously. These streaming requests are used for start and stop requests where the process is long running and there are updates at various points through the process.

When a client connects to make a streaming request, eg start, the processManager mutex is locked and the start task is spawned in the background. To allow the client to be sent messages, a channel is created to allow the updates to be sent. If the client, receiver side of the channel is closed (for instance if the client disconnects), the spawned process aborts and cancels its current task.

This won't always prevent its effects. For instance, cancelling a stop command before it completes is likely to leave the server shutdown if the task was cancelled after the interrupt was sent but before the server process ended.

Interrupting the service

If a task is in process, the server is blocked from accepting other requests (as the process manager is locked in a mutex). While this is intentional and not an issue in normal use, if a request takes a long time (for instance the server hangs on startup), and the client is left waiting, no other commands would normally be able to access the server. If the original client is run by a user who is no longer able to cancel the request, there needs to be a way for other users to interrupt the blocked task.

For this reason, as well as the task/client channel described above, an interrupt channel is passed in. This is exposed via the manage endpoint and allows other clients to trigger an interrupt of a running task.

This same process is used when the amygdala process itself is interrupted to allow it to exit promptly.

GDA Server Status Port

The GDA server can take a while to start up and errors can occur at any time in this process. To indicate to external processes that the server has started and is in a stable state, it opens a status port when the initialisation process is complete. Amygdala uses this port to monitor the startup and block until it is complete. This lets clients assume that, as long as an error is not returned, when the requests completes, the server is available. This status port can also be queried for basic server status information such as version, although amygdala does not make use of this yet.

The status port is also used at shutdown as a check to ensure that the server has shutdown correctly, as the process amygdala holds is the eclipse launcher rather than the server itself.

Interactions with Externally Started Servers

The status port described above is used when starting the server to ensure that the previous server is not still running. This means that as amygdala cannot be aware of servers that it did not start, it is possible to get into a state where amygdala reports that it can't start a server as there is already one running and also that it can't stop the server as there is no server running.

For now, the fix for this situation is to shutdown the server in the same way that is was started which will allow amygdala to return to normal operation.

Restarting Amygdala

The manage endpoint has the option to restart the amygdala process. This is not a true restart and relies on external infrastructure to start a new process.

It shuts down any running servers, then exits with a sentinel exit code intended to be used with systemd RestartForceExitStatus to trigger a restart. To help prevent the service shutting down when not in a situation where it will be restarted, it checks for the presence of a AMYGDALA_SYSTEM environment variable. If this is not present the manage endpoint will return a precondition_failed error and not shutdown.

Abnormal Shutdown

Normally, when amygdala is shutdown it will shutdown any servers it has started first. If it is sent a SIGKILL signal it is not able to do any cleanup work and can leave a started server running in the background. In this situation, manual intervention is required to shutdown the server so for this reason, it is recommended that amygdala be shutdown via an interrupt or SIGSTOP rather than SIGKILL.