Session settings stores¶
A settings store holds the FixSessionSettings of the sessions an engine runs. Configuring a
session covers what goes into those settings. This guide covers where they are kept, and how
they reach the engine.
Three stores ship with Staffix:
| store | module | settings come from | changes are written back |
|---|---|---|---|
| memory | staffix-sessions-settings-store-memory-impl |
Java, or Spring properties | no, kept in memory only |
| file | staffix-sessions-settings-store-file-impl |
YAML files, in a directory or at URIs | to the directory, never to URIs |
| JDBC | staffix-sessions-settings-store-jdbc-impl |
a database table, one row per session | to the table |
How the engine uses a store¶
A store is registered on the engine like every other component, under an instance id:
FixEngineBuilder.builder()
.fixSessionsSettingsStore(MemorySessionsSettingsStoreSettings.builder()
.instanceId("ACCEPTOR")
.fixSessionSetting(acceptorSession)
.build())
// …
An acceptor serves every session of the stores it targets. It names them with
targetFixSessionsSettingsStoreInstancesId; with none named, it uses the store whose id is default. Adding a
session to one of those stores makes the acceptor accept it, without a restart.
fixEngine.newAcceptor(FixAcceptorBuilder.builder()
.bindAddress(bindAddress)
.targetFixSessionsSettingsStoreInstancesId("ACCEPTOR")
.build()).start();
An initiator runs one session at a time: its main target, or a backup target it was
switched to. For its main target's session id the
engine looks for an INITIATOR session in every registered store and takes the first match; if none is found, building
the initiator fails and lists the ids it did find. A backup target runs on the main target's settings under its own
session id, so it must have no settings of its own: building or starting the initiator fails if a store holds some. A
change of the main target's settings applies to whichever target runs, and only that session is restarted or
disconnected by it.
A store is looked up by FixSessionId and FixSessionType, so one store may hold the acceptor and the initiator
side of the same id.
The memory store¶
The sessions are declared in Java and held in memory:
MemorySessionsSettingsStoreSettings.builder()
.instanceId("ACCEPTOR")
.fixSessionSetting(session1)
.fixSessionSetting(session2)
.defaultFixSessionSettings((declared, builder) -> builder.fixMessageStoreInstanceId("ACCEPTOR"))
.build();
defaultFixSessionSettings is applied to every session, the declared ones and any added at runtime. It receives the
session as declared and a builder already loaded with it, so it can check a field before setting it. On a map field
the last write wins, so a default written there replaces the session's own entry unless the function checks first.
The store has no backing source: add, update and remove change what it holds, and nothing survives a restart. A
reload returns what it holds, so it changes nothing. It is the store
for tests, for sessions built from your own configuration system, and for Spring Boot, where
staffix.sessions-settings-stores-memory.instances.<name>.sessions[n] declares its sessions (see Spring
Boot).
The file store¶
The sessions are YAML files, one session per file:
# yaml-language-server: $schema=fix-session-settings.v1.schema.json
schemaVersion: 1
fixSessionId:
name: "acceptor-initiator1"
fixVersion: "FIX.4.4"
senderCompID: "ACCEPTOR"
targetCompID: "INITIATOR_1"
fixSessionType: "ACCEPTOR"
fixMessageStoreInstanceId: "ACCEPTOR"
resetSeqNumOnLogon: true
The store finds the files in one of two ways. Set exactly one of them: both, or neither, fails at construction.
| builder setting | the store reads | changes made at runtime |
|---|---|---|
fixSessionSettingsDirectory |
every *.yaml file in a directory |
written back to the files |
fixSessionSettingsUri, once per file |
the files you list: classpath resources, URLs | kept in memory only |
The file-session-settings example uses both: its acceptor reads a directory,
its initiators read the classpath.
Loading every file of a directory¶
FileSessionsSettingsStoreSettings.builder()
.instanceId("ACCEPTOR")
.fixSessionSettingsDirectory(new File("/etc/myapp/sessions/acceptor"))
.build();
Every *.yaml file in the directory is a session, except default.yaml (see below).
A file added to the directory is picked up by the next reload.
The directory is also where changes go: add and update write the session's file, remove deletes it. On start,
the store creates the directory if needed and copies the JSON schema into it. A read-only
directory only costs the schema file, with a warning.
Loading files from the classpath or a URL¶
Use this when the files ship inside your jar, or are served from a configuration server. List each file:
FileSessionsSettingsStoreSettings.builder()
.instanceId("INITIATOR")
.fixSessionSettingsUri(MyApp.class.getResource("/sessions/default.yaml").toURI())
.fixSessionSettingsUri(MyApp.class.getResource("/sessions/initiator1.yaml").toURI())
.fixSessionSettingsUri(URI.create("https://config.example.com/sessions/initiator2.yaml"))
.build();
There is no classpath: prefix: resolve the resource yourself with getResource(...).toURI(), as above. Each URI is
opened with URI.toURL().openStream(), so a resource inside a jar works as well as one on disk.
A list cannot be scanned like a directory, so each URI is one file, and a new session needs a new entry. A URI
ending in default.yaml provides the defaults; listing two is an error, as is the same session id in two files.
These files are read-only. add, update and remove still change the sessions the engine runs, but nothing is
written back, and the change is lost on restart. Each one is logged rather than failing.
Sharing settings with default.yaml¶
Both ways support a default.yaml holding what the sessions have in common, so each session file only carries what
is its own. Each session file is merged over it: a field the session leaves out is taken from the defaults, and nested
groups such as heartBeatInterval are merged field by field. Lists are concatenated, and maps are merged key by key,
the session's own entry winning.
Reading is strict. An unknown field, an unknown enum value or trailing content fails the load and names the file, so a typo is an error rather than a silently missing setting. The merged result is then validated.
Every file declares schemaVersion: 1, default.yaml included: the version of the file's layout. A file without
it, or with a version newer than the running staffix knows, is refused. A file of an older version is upgraded as it is
read, and written back at the current version, so a future release can change the layout without breaking your files.
The same goes for a document sent to the HTTP admin API.
The JSON schema¶
The build generates a JSON schema from the session settings document, the model a session file is read into, so an
editor completes and validates a session file, including ${...} placeholders in fields that are not strings, and
shows what each setting does and its default. It is the
fix-session-settings.v1.schema.json
in the staffix-sessions-settings-document module's etc directory, and it can be reached in three other ways:
| where | how |
|---|---|
| in the jar | the resource org/lolaf/staffix/sessions/settings/document/fix-session-settings.v1.schema.json of staffix-sessions-settings-document |
| in the Maven repository | staffix-sessions-settings-document, classifier schema, type json |
| next to your files | a store reading a directory copies it there on start |
A file opts in with a comment read by editors built on the YAML language server. The path is relative to the file, or a URL:
# yaml-language-server: $schema=fix-session-settings.v1.schema.json
Values from system properties and environment variables¶
A value in a session file may be a ${...} placeholder resolved when the file is loaded, so one file can be deployed
unchanged across environments:
fixSessionId:
name: "${sysprop:session.id:defaultIfAbsent}"
fixVersion: "FIX.4.4"
logInOrOutResponseTimeout: "${env:LOGON_TIMEOUT:PT30S}"
Three forms are accepted: ${sysprop:key:default}, ${env:KEY:default}, and ${key:default}, the last matching
Spring's own syntax, asking every source in turn. The default is optional, may itself contain :, and is used only
after every resolver has declined; with no default, the load fails naming the placeholder and the field. A value may
be part placeholder, prefix-${env:X}-suffix, and may hold several. Nothing escapes an opening brace, so a value that
merely looks like a placeholder is treated as one.
Placeholders work in a field of any type, not only strings: resolution happens on the parsed file before it is
bound, so "${env:LOGON_TIMEOUT:PT30S}" above is a valid Duration. They work the same in a directory and at URIs.
Writing a session back keeps the placeholder. The store rebuilds the file from the runtime settings, so it
remembers the file as it was read and puts each placeholder back. A value backed by a placeholder is owned by its
source, so changing one through the store is refused rather than silently written as a literal. A placeholder in
default.yaml is resolved but not remembered: that file is only ever read, and a value it contributes is written into
a session file as the literal it resolved to.
To resolve from somewhere else, implement ConfigValueResolver and hand it to the store:
FileSessionsSettingsStoreSettings.builder()
.fixSessionSettingsDirectory(directory)
.configValueResolver(myResolver)
.build();
Resolvers are asked in order and the first non-empty answer wins. Supplying any replaces the built-in
system-property and environment resolvers rather than joining them, so a deployment can say exactly where
configuration comes from. refresh() is called once per load, for a resolver backed by something that changes. It is
not called once per file, so every file in a load sees the same snapshot.
Under Spring Boot¶
The file store is declared under staffix.sessions-settings-stores-file.instances.<name>, with either .directory or
.uris, never both. .config-value-resolver-beans names extra ConfigValueResolver beans; they are asked after the
system-property and environment resolvers and before Spring's own Environment.
The JDBC store¶
The sessions are rows of a fix_sessions_settings table, reached through a DataSource you supply:
JdbcSessionsSettingsStoreSettings.builder()
.instanceId("ACCEPTOR")
.dataSource(dataSource)
.tablePrefix("desk") // optional: the table is then desk_fix_sessions_settings
.build();
Each row holds one session's settings as a JSON document: the same document as a YAML session file, with the same
schemaVersion, written in JSON. A row is keyed by the engine's instance id, the store's instance id and the
session's group.name, so several engines, and several stores of one engine, can share one table, each seeing only
its own sessions. Give each engine and each store a fixed instance id: started under another id, they find none of
their sessions.
engineIdOverride replaces the engine's instance id in that key, so the two nodes of a
high availability pair read the same rows while keeping their own ids: give both the same value,
the pair's name for example, and never give it to unrelated engines, which would share their sessions. Without it, two
nodes on one table each have their own rows.
A value in a row may be a ${...} placeholder, as in a session file: resolved when the row is read, by the store's
configValueResolvers (system properties then environment variables by default; in Spring Boot, as for the file store,
with config-value-resolver-beans). The row keeps the expression and never the value it resolved to, and changing a
value backed by a placeholder through the store is refused. Nothing escapes an opening brace, so a value that merely
looks like a placeholder is treated as one.
Create the table with the DDL plugin, which writes the script for your database:
<plugin>
<groupId>org.lolaf.staffix</groupId>
<artifactId>staffix-jdbc-ddl-generator-maven-plugin</artifactId>
<version>${staffix.version}</version>
<configuration>
<schema>SESSIONS_SETTINGS</schema>
<dialect>org.hibernate.dialect.PostgreSQLDialect</dialect>
<tablePrefix>desk</tablePrefix>
</configuration>
</plugin>
mvn staffix-jdbc-ddl:generate-ddl then writes target/generated-ddl/schema.sql.
Two writers cannot overwrite each other. Every row carries a version that each write checks and increments. When
another engine or a person changed the row since this store read it, an update fails with an error saying so and the
store keeps what it had; reload it and apply the change again. A
remove always deletes the row.
A row edited directly in the database reaches a running engine on its next reload. A row the store cannot read, such as invalid JSON, a document written by a newer Staffix, or a row whose key does not match the session it holds, fails the start or the reload with an error naming that row.
Under Spring Boot¶
The JDBC store is declared under staffix.sessions-settings-stores-jdbc.instances.<name>, with .data-source-bean
naming the DataSource bean (required) and an optional .table-prefix.
Changing settings on a running engine¶
Every store accepts add, update and remove while the engine runs, and the engine's acceptors and initiators react
to them. reloadFixSessionsSettingsStore(instanceId) re-reads a store and applies the difference the same way, which
is how an edited YAML file or table row reaches a running engine. A live session whose settings changed keeps the old
ones until it next connects, unless the reload asks for a restart, and a session whose settings went is stopped:
Runtime administration covers the reload, and
Configuring a session how a session takes a
change.
Writing your own store¶
When sessions live somewhere no store reads, such as a configuration service, write a store. A
store is plugged in like every other Staffix component: a settings class, a factory found through ServiceLoader,
and an instance id.
1. A settings class implementing FixSessionsSettingsStoreSettings, carrying what your store needs and the
instanceId acceptors target it by:
@Getter
@Builder
public class DbSessionsSettingsStoreSettings implements FixSessionsSettingsStoreSettings {
@Builder.Default
private final String instanceId = DEFAULT_INSTANCE_ID;
private final DataSource dataSource;
}
2. The store, extending FixSessionsSettingsStore.AbstractFixSessionSettingsStore. The base class holds the
sessions and notifies the engine of every add, remove and update, so you only manage your storage:
public class DbSessionsSettingsStore extends FixSessionsSettingsStore.AbstractFixSessionSettingsStore {
@Override
protected void startMe() {
onLoad().forEach(this::hold);
}
@Override
protected Set<FixSessionSettings> onLoad() {
return readAllSessionsFromTheDatabase();
}
@Override
protected FixSessionSettings onAdd(FixSessionSettings added) {
insertIntoTheDatabase(added);
return added;
}
// onRemove, onUpdate, stopMe, getInstanceId, isPersistent
}
| method | contract |
|---|---|
startMe() |
hold the sessions found: acceptors and initiators read them as soon as the store has started |
getEngineId() |
the instance id of the engine the store serves, known from before startMe(): a backing source several engines share keeps their sessions apart by it |
onLoad() |
read the backing source and return what is there: on a reload, the engine compares the result with getSettings() and applies the difference through add, remove and update |
onAdd, onUpdate |
write to the backing source if the store does, and return the settings to manage; onUpdate replaces the entry with the same FixSessionId and type |
onRemove |
delete from the backing source if the store writes to it |
To have a store replicated between the two nodes of a high availability pair,
override getStorageIdentity to name its storage, equal on two nodes only when they read the same data. Without it the
store is only compared. A store resolving ${...} placeholders also overrides getConfigValueResolvers.
A store persisting the session document (JSON or YAML) can use FixSessionSettingsDocumentCodec, from
staffix-sessions-settings-document, around its storage. It reads a document into settings, resolving ${...}
placeholders, and writes settings back with the expressions, so what a placeholder resolved to never reaches the
storage.
3. A factory, usually nested in the store, and a ServiceLoader declaration for it in
src/main/resources/META-INF/services/org.lolaf.staffix.api.session.FixSessionsSettingsStoreSettings$FixSessionsStoreFactory:
public static class DbStoreFactory
implements FixSessionsSettingsStoreSettings.FixSessionsStoreFactory<DbSessionsSettingsStoreSettings> {
@Override
public Class<DbSessionsSettingsStoreSettings> getSettingsClass() {
return DbSessionsSettingsStoreSettings.class;
}
@Override
public FixSessionsSettingsStore newInstance(DbSessionsSettingsStoreSettings settings) {
return new DbSessionsSettingsStore(settings);
}
}
The engine picks the factory whose getSettingsClass() is exactly the class of the settings it was given, so a
subclass of your settings needs a factory of its own. Miss the services file and the engine fails at startup with
Unable to find any SPI instance for target settings class.
4. Register it with FixEngineBuilder.fixSessionsSettingsStore(...) and target it from an acceptor, exactly as
the two stores above. Under Spring Boot, a FixSessionsSettingsStoreSettingsContributor bean adds your settings to
the stores the starter builds, keyed by instance id; the two shipped stores use one each.
MemoryFixSessionsSettingsStore
is the smallest complete store to start from.