PostgresCheckpointer

class PostgresCheckpointer<State>(dataSource: DataSource, serializer: StateSerializer<State>, table: String = DEFAULT_TABLE, maxHistory: Int = Int.MAX_VALUE, createTable: Boolean = true) : Checkpointer<State>

A persistent Checkpointer that saves the checkpoints of each thread in a table of a PostgreSQL database, through the DataSource of the app. Servers with several instances share it, so a run that paused on one instance is resumed on another:

val dataSource = HikariDataSource(HikariConfig().apply { jdbcUrl = "jdbc:postgresql://db/app" })
val config = GraphConfig(
threadId = "order-42",
checkpointer = PostgresCheckpointer(dataSource, KotlinxStateSerializer<Order>()),
interruptBefore = setOf("pay"),
)

The app creates the data source, with the JDBC driver of PostgreSQL (org.postgresql:postgresql) and the pool it likes, and closes it. Each call takes a connection for one transaction and gives it back.

The table. The checkpointer creates its table when it first needs it, with CREATE TABLE IF NOT EXISTS, so the database may be the one the app keeps its own data in. An app that creates its tables with migrations, such as Flyway or Liquibase, passes createTable = false and adds this table itself. It has one row for each step of a thread:

CREATE TABLE telar_checkpoints (
thread_id TEXT NOT NULL,
step INTEGER NOT NULL,
checkpoint TEXT NOT NULL, -- the checkpoint in the format of CheckpointCodec
PRIMARY KEY (thread_id, step)
)

A save is one transaction: it replaces the checkpoints of the thread with the same step or a later one, adds the new one, and removes the oldest beyond maxHistory. A crash in the middle leaves the checkpoints as they were.

Two instances that save the same thread at the same time take turns: a save first takes a lock of its thread (pg_advisory_xact_lock), held until its transaction ends, so the rows of a thread are always those of one save after another, and the last save is the one that is loaded. Which of two runs of the same thread saves last is not decided by the checkpointer, so an app still runs a thread in one place at a time, as with every checkpointer. One instance of the class is safe to share between coroutines.

JDBC blocks, so every call runs on Dispatchers.IO. A failure of the database, such as a java.sql.SQLException, is thrown as it is.

Parameters

dataSource

the database. It has to be PostgreSQL, version 12 or later.

serializer

converts the graph state to and from a string.

table

the name of the table, letters, digits and _, not starting with a digit, with a schema in front or not, such as agents.telar_checkpoints.

maxHistory

how many checkpoints a thread keeps, counted from the latest, for CompiledGraph.history and fork. The default keeps all of them; 1 keeps only the latest.

createTable

whether the checkpointer creates table when it does not exist. false for an app whose migrations create it.

Throws

if table is not a plain name or maxHistory is not positive.

Constructors

Link copied to clipboard
constructor(dataSource: DataSource, serializer: StateSerializer<State>, table: String = DEFAULT_TABLE, maxHistory: Int = Int.MAX_VALUE, createTable: Boolean = true)

Functions

Link copied to clipboard
open suspend override fun delete(threadId: String)
Link copied to clipboard
open suspend override fun history(threadId: String): List<Checkpoint<State>>
Link copied to clipboard
open suspend override fun load(threadId: String): Checkpoint<State>?
Link copied to clipboard
open suspend override fun save(threadId: String, checkpoint: Checkpoint<State>)