The session framework provides a straightforward interface for scheduling multiple Activities on a single worker without requiring you to manually specify the task queue name. It also includes features like concurrent session limitation and worker failure detection.
File Processing: You may want to implement a Workflow that can download a file, process it, and then upload the modified version. If these three steps are implemented as three different Activities, all of them should be executed by the same worker.
Machine Learning Model Training: Training a machine learning model typically involves three stages: download the data set, optimize the model, and upload the trained parameter. Since the models may consume a large amount of resources (GPU memory for example), the number of models processed on a host needs to be limited.
Before using the session framework to write your Workflow code, you need to configure your worker to process sessions. To do that, set the
EnableSessionWorker field of
true when starting your worker.
The most important APIs provided by the session framework are
workflow.CompleteSession(). The basic idea is that all the Activities executed within a session will be processed by the same worker and these two APIs allow you to create new sessions and close them after all Activities finish executing.
Here's a more detailed description of these two APIs:
CreateSession() takes in
sessionOptions and returns a new context which contains metadata information of the created session (referred to as the session context below). When it's called, it will check the task queue name specified in the
ActivityOptions (or in the
StartWorkflowOptions if the task queue name is not specified in
ActivityOptions), and create the session on one of the workers which is polling that task queue.
The returned session context should be used to execute all Activities belonging to the session. The context will be cancelled if the worker executing this session dies or
CompleteSession() is called. When using the returned session context to execute Activities, a
workflow.ErrSessionFailed error may be returned if the session framework detects that the worker executing this session has died. The failure of your Activities won't affect the state of the session, so you still need to handle the errors returned from your Activities and call
CompleteSession() if necessary.
CreateSession() will return an error if the context passed in already contains an open session. If all the workers are currently busy and unable to handle new sessions, the framework will keep retrying until the
CreationTimeout you specified in
SessionOptions has passed before returning an error (check the Concurrent Session Limitation section for more details).
CompleteSession() releases the resources reserved on the worker, so it's important to call it as soon as you no longer need the session. It will cancel the session context and therefore all the Activities using that session context. Note that it's safe to call
CompleteSession() on a failed session, meaning that you can call it from a
defer function after the session is successfully created.
The session context also stores some session metadata, which can be retrieved by the
GetSessionInfo() API. If the context passed in doesn't contain any session metadata, this API will return a
To limit the number of concurrent sessions running on a worker, set the
MaxConcurrentSessionExecutionSize field of
worker.Options to the desired value. By default this field is set to a very large value, so there's no need to manually set it if no limitation is needed.
If a worker hits this limitation, it won't accept any new
CreateSession() requests until one of the existing sessions is completed.
CreateSession() will return an error if the session can't be created within
For long-running sessions, you may want to use the
ContinueAsNew feature to split the Workflow into multiple runs when all Activities need to be executed by the same worker. The
RecreateSession() API is designed for such a use case.
Its usage is the same as
CreateSession() except that it also takes in a
recreateToken, which is needed to create a new session on the same worker as the previous one. You can get the token by calling the
GetRecreateToken() method of the
If your Activity has already been scheduled, it will be cancelled.
If not, you will get a
workflow.ErrSessionFailed error when you call
It's per worker process, so make sure there's only one worker process running on the host if you plan to use that feature.
Right now a session is considered failed if the worker process dies. However, for some use cases, you may only care whether worker host is alive or not. For these uses cases, the session should be automatically re-established if the worker process is restarted.
The current implementation assumes that all sessions are consuming the same type of resource and there's only one global limitation. Our plan is to allow you to specify what type of resource your session will consume and enforce different limitations on different types of resources.