# Upgrading a high-availability pair

> Upgrade every node of an eaxScada high-availability cluster (primary, standby and witness) together, because builds from commit 6ac5700 onward keep the HA state where older builds cannot see it.

Product: eaxScada · Type: How-to guide · Editions: Enterprise
Source: https://eaxware.com/docs/eaxscada/how-to/upgrade-ha-pair/
Status: migrated page, not yet verified against the current code.

An eaxScada high-availability cluster has three servers: a primary, a
standby and a witness. This page explains how to upgrade them to a new
build.

:::caution[Upgrade the whole cluster together]
From commit `6ac5700` onward, eaxScada keeps its HA state in a broker
account of its own, `EAX_HA`. That state is the primary lease, the
replicated alarm and batch state, the alarm and audit sync subjects, the
shared nonce and e-signature stores and the integration registry. Older
builds keep the same state in the broker's global account.

The two accounts do not share anything. A node running an older build and
a node running a newer one do not see each other's lease, so **each can
believe it is the only primary**, and they do not replicate alarms or
single-use records to each other. Do not upgrade a cluster one node at a
time across this commit. Stop every node, upgrade every node, then start
them again.
:::

:::caution[Builds with the term counter: upgrade the whole cluster together too]
Newer builds number every primary lease with a term taken from a cluster
counter, the `ha_lease_term` bucket in `EAX_HA`. The counter never
expires. Each node stamps that term on the alarm state and batch record
entries it replicates, and the other nodes refuse state stamped with an older term
than the one they know.

Audit records are not fenced: every node sends the records it writes,
and each node keeps every node's records, each run of a server in its own
hash chain. A node that was stopped fetches the records it missed when it
comes back.

A node running an older build does not use the counter. It can start the
terms again at 1, and it does not stamp what it replicates. The other
nodes accept unstamped state, so they cannot refuse a late write from an
older-build primary that has since been replaced. The cluster still
elects one primary at a time while builds are mixed, but the term
guarantees only hold once every node runs the new build. Upgrade all
three nodes in the same maintenance window, as described below.
:::

:::caution[Builds that replicate audit records per node: upgrade both SCADA nodes together]
From commit `3aca899` onward, each audit record carries the run of the
server that wrote it (`origin`, `origin_seq`, `node`), and its hash covers
them. A node running an older build checks its whole ledger as one chain
in the old format. Once it holds records from a newer node, its audit
chain check fails. Upgrade both SCADA nodes of the pair in the same
maintenance window; do not run the pair with mixed builds.
:::

## Before you start

- Plan a maintenance window. While the cluster is stopped, no node accepts
  control writes.
- Get the same new build for all three servers.
- Back up each node's `data` directory.

## Upgrade the cluster

1. Stop the standby, then the witness, then the primary. Once the standby
   and the witness are gone, the primary loses quorum and demotes itself,
   so control writes stop from that point.
2. Check that no `scada-server` process of the cluster is still running on
   any of the three machines.
3. Replace the `scada-server` binary on all three machines. Keep the
   configuration files: the `ha:` section (`role`, `node_id`,
   `cluster_size`, `lease_ttl`, `heartbeat_interval`) is unchanged.
   `public_url` is new and optional; the `HA_PUBLIC_URL` environment
   variable sets it per node. If the cluster does not replicate the
   engineering configuration yet, read
   [Upgrade an existing cluster to replicated configuration](#upgrade-an-existing-cluster-to-replicated-configuration)
   before you start the nodes.
4. Start the witness and the node configured as primary, then the
   standby.

   On a cluster that has no HA state yet (a new cluster, or one upgraded
   across commit `6ac5700`), no node becomes primary until all three
   servers are connected. The lease bucket and the term counter are
   created on three servers from the start. They are never created on one
   server and moved to three later, because a server restarted after such
   a move can briefly serve its old copy. Once all three servers are
   connected, the node configured as primary takes the lease. The standby
   leaves it free for 8 seconds after it starts.

   **Start the configured primary first.** The configured role is a
   preference; one primary at a time is the rule. On a new cluster, a
   configured standby that starts more than 8 seconds before the configured
   primary (or while the configured primary is slow to start) takes the
   lease itself once all three servers are connected: it becomes primary in
   term 1, and the configured primary then runs as its standby. The term
   counter hands term 1 out once, so the two nodes are never primary at the
   same time. `/api/ha/status` shows which node is primary.

   On a cluster that already has a term counter, a node that cannot find
   the lease waits before it takes the lease, whatever its configured
   role. It waits at least the lease TTL plus a sixth of it, and at least
   8 seconds after the node starts. A missing lease on such a cluster
   means unknown, not free.

## Check the result

On each SCADA node, open `/api/ha/status` and check that:

- exactly one node reports `"is_primary": true`;
- every node reports `"has_quorum": true`;
- `"lease_replicas"` reaches `3` within a few seconds of all three servers
  being connected. It counts the servers that hold both the lease and the
  term counter.
- the `"term"` of the primary is higher than it was before the upgrade, if
  the cluster had a term counter before. Terms only increase. They can
  skip numbers when two nodes try for the lease at the same time.

If two nodes report `"is_primary": true`, a node is still running an older
build. Stop that node and upgrade it.

## What the new build does not carry over

The new build does not move the state the older build kept in the global
account. The lease is taken again when the cluster starts. Replicated
alarm and batch state and the shared single-use records start fresh in
`EAX_HA`.

A lease bucket created by an earlier build keeps an expired lease for only
three lease TTLs. The new build raises that to 24 hours the first time it
runs, and the change applies to the existing bucket. Takeover no longer
depends on this setting.

A lease bucket that an earlier build created on one server and later moved
to three can still leave a stale copy behind. A new-build node does not
take the lease while the lease bucket or the term counter answers as
anything other than three servers with an up-to-date follower. A node
that reaches only the stale copy waits instead of promoting itself.

## Replicated buckets created by an earlier build

The other HA buckets are the alarm snapshot (`ha_active_alarms`), the
integration command nonces
(`ha_esign_nonces`), the e-signature tickets (`ha_esign_tickets`) and the
integration registry (`ha_integrations`). Newer builds create each of them
on three servers from the start, once all three servers are connected, and
mark it as created that way. Earlier builds created them on one server and
moved them to three later, which leaves the same stale-copy risk as the
lease bucket. A stale copy of the ticket bucket could let a signature
ticket be redeemed twice, and a stale copy of the nonce bucket could let a
signed command be replayed.

A new-build node treats any of these buckets that does not have this mark
as an earlier build's bucket. It does not use that bucket, and the primary
replaces it automatically:

1. Until the primary has replaced a bucket, every node refuses what depends
   on it. Signature tickets are neither issued nor redeemed, signed
   integration commands are refused, and integration identities cannot be
   changed. Each refused ticket or command is written to the audit trail
   with the reason. The node log shows `HA bucket not used` for each
   bucket.
2. Once the primary has seen a bucket in that state for 10 seconds, and
   all three servers are connected, it deletes the bucket and creates it
   again on three servers. The node log shows
   `legacy HA bucket recreated with three replicas`.
   - The alarm snapshot and the integration registry are copied into the
     new bucket. For each alarm, the copy keeps the entry with the highest
     term. The primary holds its own alarm writes during the copy and
     writes them afterwards. Every node then
     reads the whole integration registry again.
   - The ticket and nonce buckets are created empty. Signature tickets
     issued before the upgrade are refused, so the person signs again. For
     65 seconds after the new nonce bucket is created (the nonce lifetime
     plus 5 seconds of tolerated clock difference), every signed
     integration command is refused, so a command accepted before the
     upgrade cannot be replayed.

You do not need to run anything for this. Plan for about a minute after
the cluster is up during which signed commands and e-signature tickets are
refused.

The primary deletes a bucket only after it has read all of it. If the
primary stops after the delete and before the copy is finished:

- a bucket that was deleted is created again empty;
- a nonce bucket that is created again while the ticket bucket exists
  refuses signed commands for the same 65 seconds;
- an integration registry whose copy did not finish is filled again by the
  next node that connects, from that node's own copy of the registry.

Do not delete these buckets by hand.

This replacement assumes every node runs the new build. A node running an
earlier build can create a bucket again the earlier way. The new build
then treats that bucket as an earlier build's bucket and replaces it again.

The batch state bucket (`ha_batch_state`) of earlier builds is no longer
used. Batch records now replicate as their chain entries on a stream (see
[Batch records on a redundant pair](#batch-records-on-a-redundant-pair)).
New builds do not create this bucket, read it, write it or replace it. On
a cluster upgraded from an earlier build, the bucket stays as it was and
is not listed in the HA status. Broker users are still denied it. You can
leave it in place.

## Batch records on a redundant pair

The primary is the only node that writes batch records. It sends each
step's chain entries to the other nodes on the stream `EAX_HA_EBR`, in
the `EAX_HA` account, and waits until the cluster has taken them before
it answers the step.

- A forward step or a signature that the cluster does not acknowledge is
  refused with `503 EBR_NOT_WRITTEN`: nothing changes and no setpoint is
  written. PAUSE, HOLD, STOP and ABORT still take effect; their entries
  are sent late, and until the cluster takes them the batch takes no
  forward step and no signature.
- A step is acknowledged only once another running SCADA node holds its
  audit record. Before the step's entries go on `EAX_HA_EBR`, the
  primary asks on `ha.sync.audit.have`; the standby takes what it lacks
  from the primary and confirms. Not confirmed within 4 s, a forward
  step is refused (`503 EBR_NOT_WRITTEN`, `REVERTED`, as for a stream
  refusal), and a step toward rest applies and is sent again until it is
  confirmed. If no other SCADA node runs, steps go on as before. Steps
  can take up to about 1.5 s longer when the standby lags.
- After a failover, every acknowledged entry is anchored on the new
  primary and reads `valid` once it has caught up. Before, it could stay
  `not_anchored` until the old primary came back, or forever if it never
  did.
- A running but unhealthy standby (for example one whose audit trail is
  down) blocks forward batch steps. This is deliberate: record integrity
  comes before availability. Repair the standby or stop it; see
  [When a forward step is refused because of the standby](/docs/eaxscada/how-to/record-lots-and-measured-amounts/#when-a-forward-step-is-refused-because-of-the-standby).
- A SCADA node that runs but does not answer the confirmation (a node of
  an earlier build, while the builds are mixed) is not waited for: the
  step goes on as on a lone primary. The primary logs a warning
  ("a SCADA node seen running does not answer") and raises
  `ALM-SYS-HA-AUDIT-UNCONFIRMED` until a step is confirmed or it is no
  longer the primary.
- The standby applies only entries that link to its own chain, and
  rebuilds the batch, its phase log and its signatures from that chain.
  It never saves a batch as it receives it. An entry that does not link
  stops the replication of that batch on that node.
- A node that is not the primary refuses every batch step with
  `409 EBR_NOT_PRIMARY`, stops included. Send batch commands to the
  primary.
- After a failover, the new primary puts its term marker on the stream
  and applies every entry up to it before it runs forward steps. Until
  then it takes steps toward rest only, and its runner does not step.
- Entries that one node recorded but that never reached the other before
  a failover are reported as missing in that batch's EBR
  (`behind_ledger`, "recorded in the audit trail on node &lt;node&gt; and
  missing here"), never invented.
- Phase logs and signatures now survive a failover. Earlier builds
  replicated only the batch's state, without them.
- Audit records the primary kept to write late (while its audit trail
  was down) go on `EAX_HA_EBR` too, subject `ebr.late.<batch key>`. The
  standby holds them, and its batch reads `not_checked`
  (`audit_records_pending`) until they are written. After a failover the
  new primary writes them once, after it has caught up, and the batch
  reads `valid` there instead of `not_anchored`. A node that has run in
  a cluster waits up to 60 s at start for its replication before it
  writes such records; records the cluster does not take are sent again
  after 10 s. See
  [Late audit records on a redundant pair](/docs/eaxscada/how-to/record-lots-and-measured-amounts/#late-audit-records-on-a-redundant-pair).

The state of this replication is in System Health, HA section, and in
`GET /api/ha/status` under `buckets`, as `EAX_HA_EBR`: `ok`, or what is
wrong, such as "batch &lt;id&gt; not replicated here: &lt;reason&gt;" or the
number of batches with entries the cluster has not taken yet.

Limits:

- A STOP on a primary that has not caught up can fork the batch's record
  on that node. That batch's replication then stops there, and the HA
  status reports it ("the record forked").
- A step taken while no other SCADA node was running is anchored on a
  later primary only once the old primary's records reach it.
- With more than two SCADA nodes, the first node to answer confirms. A
  newly elected node other than that one may lack the anchors until it
  fetches them from the node that holds them (its audit catch-up).
- `ALM-SYS-HA-AUDIT-UNCONFIRMED` takes a node as running for 65 s after
  it was last seen. A standby stopped on purpose less than 65 s before a
  batch step can raise it; it clears with the next step the standby
  confirms once it is back.
- Batches that ended before the upgrade are reported missing after a
  failover: their entries were never sent to the other node. Batches still
  running at the upgrade are sent by the primary.
- If the old primary writes a late audit record and fails before that
  record reaches any other node, the new primary writes it too. The audit
  trail then holds two identical records (same action and `version_ref`)
  from two nodes. This is accepted: the batch record still checks
  `valid`.

Upgrade both SCADA nodes in the same maintenance window. A node of an
earlier build neither sends nor applies these entries, so batch records
do not replicate while the builds are mixed.

Details: [The batch record's integrity on redundant pairs](/docs/eaxscada/how-to/record-lots-and-measured-amounts/#redundant-pairs-enterprise).

## What HA replicates, and what you must keep identical on every node

This section describes current behaviour.

An eaxScada cluster is active–passive. One SCADA node holds the primary
lease and does the work; the standby takes over when it can take the
lease. The nodes do not share the load. The diagram steps through the loss
of the primary.

**Diagram: An eaxScada pair loses its primary** (failover scenario)

Two eaxScada nodes and a witness share replicated state. One node holds the primary lease and does the work; the standby reads its devices and never writes. When the primary is lost, the standby takes the lease and becomes primary.

**Parts**

- **Operator stations** (person or role)
- **eaxScada node A** (product, primary): holds the primary lease
- **eaxScada node B** (product, standby): ready to take the lease
- **Witness** (service or process): third replica
- **PLCs and devices** (device or controller)

**Connections**

- **Operator stations → eaxScada node A** (data or telemetry): screens, writes
- **eaxScada node A ↔ eaxScada node B** (replication): replicated state
- **eaxScada node A ↔ Witness** (replication)
- **eaxScada node B ↔ Witness** (replication)
- **eaxScada node A → PLCs and devices** (command to equipment): writes
- **PLCs and devices → eaxScada node A** (data or telemetry): reads
- **PLCs and devices → eaxScada node B** (data or telemetry): reads
- **eaxScada node B → PLCs and devices** (command to equipment, not active): never writes

**Step 1 of 3: Normal**

Node A holds the primary lease and does the work: it serves the operator stations and writes to the devices. Node B, the standby, keeps reading its devices and never writes to them.

**Step 2 of 3: Primary lost**

Node A is lost. Nothing writes to the devices. Node B and the witness still hold two of the three replicas, so node B can take the lease.

- **eaxScada node A**: lost, down, not reachable
- **Operator stations → eaxScada node A** (data or telemetry, not active): screens, writes
- **eaxScada node A ↔ eaxScada node B** (replication, not active): replicated state
- **eaxScada node A ↔ Witness** (replication, not active)
- **eaxScada node A → PLCs and devices** (command to equipment, not active): stopped
- **PLCs and devices → eaxScada node A** (data or telemetry, not active): reads
- **eaxScada node B → PLCs and devices** (command to equipment, not active): not yet

**Step 3 of 3: Standby active**

Node B has taken the lease and is primary. It already held the configuration; it reads the whole store again before it accepts changes. For a few seconds the cluster can refuse a save: save again.

- **eaxScada node A**: lost, down, not reachable
- **eaxScada node B**: primary, holds the primary lease
- **Operator stations → eaxScada node B** (data or telemetry): screens, writes
- **eaxScada node A ↔ eaxScada node B** (replication, not active): replicated state
- **eaxScada node A ↔ Witness** (replication, not active)
- **eaxScada node A → PLCs and devices** (command to equipment, not active): stopped
- **PLCs and devices → eaxScada node A** (data or telemetry, not active): reads
- **eaxScada node B → PLCs and devices** (command to equipment): writes

Active-passive: one node works, the other waits. The witness holds the third replica, so one node and the witness can still agree when the other node is lost.

### How the primary keeps its lease

The primary renews its lease every `ha.heartbeat_interval` (0.5 s by
default); each renewal sets the lease to expire `ha.lease_ttl` later (3 s
by default). Its renewal deadline is the lease's expiry minus a sixth of
the TTL (0.5 s by default). A renewal that fails for a transient reason
(no answer in time, a broker election) is retried until that deadline.
Each attempt waits at most one twelfth of the TTL for its answer (at most
500 ms; 250 ms by default). An attempt that timed out is retried at once;
a fast error is retried after a short backoff (25 ms, doubling to 200 ms).
So during a lease stream leader election the primary tries again every
250 ms or less by default, and renews in the short spells between
elections when the stream takes writes. At the deadline the primary
demotes with the cause `lease_lost` and accepts no more control. The
deadline always falls before any other node can take the lease. A
conflict that is not the primary's own landed renewal (below) demotes it
at once. Before
20 November (eaxScada `b4faddea`), each attempt waited up to a sixth of the
TTL and a timeout was followed by a backoff, so a primary tried about every
700 ms, could miss those short spells, and demoted needlessly.

A renewal can land in the lease store although its reply is lost. The next
attempt then finds the lease at a newer revision than the one the primary
holds. The primary reads the lease back from the lease stream's leader,
never from a replica that may still be behind, and only reads it: it never
writes or extends another node's lease. It keeps the lease only if it is
its own (same node, same term), not expired, at a newer revision; the log
says "its earlier renewal landed although its reply was lost ... kept".
Another node's lease or term, a deleted lease, a lease that does not
decode or an expired lease demotes the primary at once (cause
`term_superseded`). A read that fails, or that still finds the old
revision, is retried until the renewal deadline; then the primary demotes
with `lease_lost`. Before 16 November (eaxScada `48d053dc`), the read could
be answered by a replica that was behind, and a busy primary demoted with
"lease revision conflict on renewal" although no other node had written:
needless failovers under CPU load.

### Replicated between the nodes

This state is kept in the `EAX_HA` broker account, with three replicas on
a cluster of three or more servers. The witness holds the third replica.

- the primary lease and its term;
- the states of active alarms, shelves included. A shelve made on the
  primary is also recorded on the standby, so it survives a restart of
  the standby;
- batch records, as their chain entries, synchronously (see
  [Batch records on a redundant pair](#batch-records-on-a-redundant-pair));
- audit records: every node keeps every node's records, primary or
  standby, and a node that was stopped fetches the ones it missed;
- e-signature tickets and integration command nonces;
- the integration registry;
- the view-only stations (`ha_stations`, since eaxScada `df57e95a`): a
  station paired, changed, disabled or deleted on one node is the same on
  every node, so paired screens keep working after a failover. See
  [View-only stations on an HA cluster](#view-only-stations-on-an-ha-cluster);
- sign-in lockouts (`ha_lockouts`, since eaxScada `528843ae`): the counts
  of wrong passwords and the automatic locks, so a lock set on one node
  holds on every node. See
  [Sign-in lockouts on an HA cluster](#sign-in-lockouts-on-an-ha-cluster);
- eaxTags definitions, scripts, written values and script state;
- the engineering configuration:
  - projects: pages, docks, shared settings and the plant emergency stop
    setting, and which project is active;
  - screens, the display tree and reusable components;
  - the deploy manifest of each explicit deploy;
  - driver configuration: channels, devices, tag mappings and register
    maps. Driver credentials stay sealed. See
    [Driver credentials](#driver-credentials-one-key-for-the-cluster);
  - alarm definitions and thresholds;
  - master recipes, with their status and signatures;
  - the 3D asset library and dashboards.

### How the engineering configuration is replicated

The cluster holds the engineering configuration in the bucket `ha_config`
and, for large files, the object store `ha_config_obj`, both in `EAX_HA`.
This copy is the reference. The `data` folder of each node is that node's
copy of it.

**Diagram: How a saved change reaches every node** (sequence)

An engineering change is saved on the primary of an HA cluster. The primary checks that it may make the change, writes it to the cluster store (the bucket ha_config) first and to its own disk after that, and every node applies it from the store. The diagram also shows the refusals: before anything is written, when the cluster does not take the write (the item changed there meanwhile, or the write was not held), and on a standby. After a failover the new primary reads the whole store again.

**Participants**

- **Dev Studio** (service or process): an engineer
- **Primary** (product): eaxScada node
- **Standby** (product): eaxScada node
- **Cluster store** (data store): ha_config

**Steps, in order**

1. **Dev Studio → Primary** (configuration or deploy): Saves a change. with the revision it loaded
2. **Primary**: Checks before it writes anything: it is the primary and has quorum; it has read the store; three servers: the bucket has its leader and a current follower; the item is still at the revision Dev Studio loaded
3. A check fails:
   1. **Primary → Dev Studio** (refusal): Refuses, nothing is written. 503: no quorum, or not replicated 409: changed by someone else, reload
4. **Primary → Cluster store** (replication): Writes the change to the cluster first. only over the revision this node last read
5. Cluster refuses:
   1. **Cluster store → Primary** (refusal): Refuses the write. another revision, or not held
   2. **Primary → Dev Studio** (refusal): Refuses, nothing is saved. 409: changed elsewhere, reload 503: the cluster did not hold it
6. Held by the cluster:
   1. **Primary**: Writes its own disk, applies the change
   2. **Primary → Dev Studio** (reply): Saved
   3. **Cluster store → Standby** (replication): Delivers the change. every node watches the store
   4. **Standby**: Applies it: only changed channels restart; writes nothing to devices
7. A save sent to a standby:
   1. **Dev Studio → Standby** (configuration or deploy): Saves a change
   2. **Standby → Dev Studio** (refusal): 409, names the primary. its ha.public_url, when it set one
8. After a failover:
   1. **Standby → Cluster store** (data or telemetry): New primary: reads it all again. a save made from an older reading is refused as a conflict
9. Note: /api/ha/status, under config, on every node: generation, drift, pending

The cluster store is the reference: a change that the cluster does not hold is not saved anywhere.

- **Changes are made on the primary.** On a standby, every engineering
  change is refused with HTTP 409 and a message that names the primary.
  If the primary sets `ha.public_url`, the message gives that address.
  A primary that has lost quorum refuses with HTTP 503. You can still open
  and browse Dev Studio on a standby.
- **A save reaches the cluster first.** The primary writes the change to
  the cluster, then to its own disk. A change is accepted only when the
  bucket's leader and at least one other replica hold it. If the cluster
  cannot keep the change, the save fails with a message, and nothing
  changes on the primary.
- **Concurrent changes conflict.** If an item changed in the cluster after
  the node read it, the later save is refused. Reload the item and make
  the change again.
- **Two engineers editing the same item.** Screens, projects and their
  page layouts, components, alarm definitions, recipes, driver channels,
  devices and tag mappings each carry a revision. Dev Studio sends the
  revision it loaded with every save. If someone else saved or deleted the
  item in the meantime, the save is refused with HTTP 409 and Dev Studio
  shows **Changed by someone else — reload**. Nothing is overwritten.
  Reload, then make the change again. This works the same way on a single
  server and on a cluster. A client that sends no revision (an older
  script, for example) is still accepted, and the server logs the save.
- **Every node applies each change.** Screens, projects, the display tree,
  alarm definitions and recipes take effect at once on every node. For
  the drivers, only the channels whose configuration changed are
  restarted. A standby keeps reading its devices as it did before, and it
  never writes to them.
- **The plant emergency stop setting moves the signal on every node.**
  While the signal is active, a node keeps the current tag and logs an
  error. It retries until the signal is clear. Until then the HA status
  lists the change as not applied.
- **Failover.** The standby already holds the configuration when it
  becomes primary. It reads the whole store again at once. Saves stay
  accepted during that reading: one made from an older reading is refused
  as a conflict, never written over a newer change. For a few seconds
  after a failover, the cluster can refuse saves while it chooses new
  bucket leaders. Save again.

Each node reports the state of the configuration in `/api/ha/status`,
under `config`:

- `generation`: a hash of every document this node has applied;
- `peers`: the generation of each other SCADA node;
- `drift` and `drift_reason`: the nodes do not hold the same
  configuration, or a change is not applied on this node;
- `pending`: the changes this node has not applied yet, with the reason;
- `warnings`, for example a driver secrets key that differs from the
  primary's;
- `adopted`: what this node replaced when it joined the cluster.

`accepts_engineering` is `true` on the node where changes can be saved.
In Dev Studio, **System Health** shows the same information in the
**High availability** card. Dev Studio also shows a banner when the
configuration drifts ("HA configuration drift: ..."), in the language
the person chose, with the server's `drift_reason` as it is. The
standby and quorum-lost banners and the control-status badge follow the
chosen language too. After an upgrade, check the Replication card of the
System Health HA panel: it lists every replicated store under its own
name, those not in use first. A store in use reads **replicated**; for
each store not in use, the note under the rows gives the reason. The System Health HA panel, with
the last takeover and why, is described in
[Metrics, logs and System Health](/docs/eaxscada/reference/metrics/#ha-panel-enterprise).

### View-only stations on an HA cluster

Since eaxScada `df57e95a` the view-only stations are kept in the bucket
`ha_stations` in `EAX_HA`. A single node keeps its own stations, as
before.

- A station paired, changed, disabled or deleted on one node is the same
  on every node. A disable or a delete stops the screen on every node
  within moments and ends its sessions there.
- Each node binds a station to its own viewer identity of the same
  username, of the viewer kind. Create the viewer identities on every
  node, like users. A node without one refuses the station
  (`VIEWONLY_REFUSED`, "viewer identity missing, disabled or not of the
  viewer kind"). Create a missing viewer identity in the Studio of that
  node: **Security > Stations**, then **Create viewer identity** in the
  toolbar (in the **More** menu when the window is narrow), with the
  same name as on the other nodes. The stations that name it and have
  no viewer there are bound to it at once, each recorded as
  `STATION_REPLICATED`. A new viewer identity has no read rule: give it
  the same read rights as on the other nodes.
- Or create it with `POST /api/security/stations/viewers` and
  `{"name": "<username>"}` (since eaxScada `ca8c23dc`): the answer lists
  the stations bound to it in `rebound`.
- On a node where the station has no usable viewer identity, a station
  change, a new pairing code or a pair-here is refused with
  `409 viewer_unusable`, nothing changed on any node. The message names
  the viewer identity to create on that node, and the answer carries it
  as `viewer_name`. The station dialog explains the refusal and offers
  **Create viewer identity** with that name filled in. A change that
  picks a viewer identity this node has is allowed.
- A station change is refused with `503 store_unavailable`, nothing
  changed, while the cluster has not confirmed it, or while the node is
  not yet synchronized or has lost its change feed.
- Fail closed: a node uses no station credential until it holds the
  cluster's stations, a few seconds after it starts or while its change
  feed is lost. During that time the wall screens show the sign-in page,
  and `VIEWONLY_REFUSED` says "stations not synchronized with the HA
  cluster".
- Each change a node applies from the cluster is recorded in its Access
  audit as `STATION_REPLICATED`.
- **On upgrade**, each node adds the stations it already had to the
  cluster's copy, once. After that the cluster's copy is the reference: a
  station deleted while a node was stopped does not come back from that
  node. Check **Security > Stations** after the upgrade, and delete any
  station you no longer want.

Pairing codes are redeemed only on the node that issued them. Sign-in
lockouts are shared by the nodes (see
[Sign-in lockouts on an HA cluster](#sign-in-lockouts-on-an-ha-cluster)). See
[Set up a view-only station](/docs/eaxscada/how-to/set-up-a-view-only-station/#7-on-an-ha-cluster).

### Sign-in lockouts on an HA cluster

Since eaxScada `528843ae` the sign-in lockout is shared by the nodes, in
the bucket `ha_lockouts` in `EAX_HA`. A single node is unchanged.

- Wrong passwords of sign-ins and of electronic signatures are counted
  across the cluster. 5 from one address lock the account from that
  address on every node; 20 from all addresses together lock the account
  on every node, for 15 minutes.
- A node that refuses a sign-in because of a lock set by the cluster
  copies the account lock into its own security store, so its
  **Security > Users** shows the account as locked.
- A correct password clears the count of its address on every node. An
  administrator's **Unlock** or password reset on one node clears the
  lock and every count of the account on all nodes.
- Accounts are matched by username: create the same accounts on every
  node. An administrative lock is still each node's own.
- If the bucket does not answer within 500 milliseconds, or cannot be
  used yet (for example while it is not replicated), that attempt uses
  the node's own counts and locks, as on a single node. The node records
  `LOCKOUT_SHARED_UNAVAILABLE` at most once a minute, and
  `GET /api/ha/status` shows under `buckets`, as `ha_lockouts`, that
  sign-in lockouts are per node until it answers. In System Health, the
  HA panel's Replication card shows the Sign-in lockouts row as
  **per node**, with the reason under the rows. When it answers again,
  the node merges its counts and locks into it and records
  `LOCKOUT_SHARED_RESTORED`. An unreachable bucket never unlocks anything,
  and never refuses every sign-in.
- Limits: while the bucket does not answer, a node does not know about
  locks that only other nodes set. An unlock made meanwhile applies on
  that node only, and is recorded; the other nodes keep the lock until it
  expires, and once the bucket answers the lock applies on that node
  again: unlock the account again then. Keep the node clocks in sync (NTP): an unlock is compared with
  the time the lock was set.
- Broker users are denied the bucket, as every HA bucket.

See [Lockout parameters](/docs/platform/explanation/security-and-part-11/#42-lockout-parameters).

### Driver credentials: one key for the cluster

Driver credentials, passwords inside broker and endpoint URLs included,
are sealed on disk with the driver secrets key, and they stay sealed in the cluster. The key itself is never replicated.
Every SCADA node must use the same key: point
`EAX_DRIVER_SECRETS_KEY_FILE` at the same key file on every node, or copy
`data/security/driver_secrets.key` from the primary to the other nodes
before you start them.

The key file holds exactly 32 random bytes, stored as raw bytes: not hex,
not Base64, and with no trailing newline. A file of any other size is
refused at start-up. A node that has no key file creates one of its own,
so create the shared file once, before the first start, and never create
it again: credentials sealed with one key cannot be opened with another.
Back it up.

- Linux: `head -c 32 /dev/urandom > driver_secrets.key`, then
  `chmod 400 driver_secrets.key`.
- Windows PowerShell:

  ```powershell
  $b = New-Object byte[] 32
  [System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($b)
  [System.IO.File]::WriteAllBytes("driver_secrets.key", $b)
  ```

The server reads the file but never changes an existing one. It must be
readable by the account that runs the server.

#### Docker Compose (`deploy/pro/docker-compose.ha.yml`)

The three nodes of `docker-compose.ha.yml` share one configuration file,
`nats-cluster.yaml`. The settings that differ per node are set in each
service's environment:

| Variable | Setting | Node 1 | Node 2 |
|---|---|---|---|
| `HA_NODE_ID` | `ha.node_id` | `scada-node-1` | `scada-node-2` |
| `HA_ROLE` | `ha.role` | `primary` | `standby` |
| `HA_PUBLIC_URL` | `ha.public_url` | `SCADA_NODE_1_PUBLIC_URL` | `SCADA_NODE_2_PUBLIC_URL` |
| `EAX_DRIVER_SECRETS_KEY_FILE` | driver secrets key | `/run/secrets/driver_secrets_key` | `/run/secrets/driver_secrets_key` |

`HA_PUBLIC_URL` overrides `ha.public_url` from the configuration file when
it is set and not empty. Set the two addresses in a `.env` file next to the
compose file, for example
`SCADA_NODE_1_PUBLIC_URL=https://scada-a.plant.local:8080`.

Both SCADA nodes mount the same host file, `secrets/driver_secrets.key`,
read-only as the Docker secret `driver_secrets_key`. The witness runs no
drivers and does not need it. Create the file once with
`./gen-secrets.sh` (or `gen-secrets.ps1`) in `deploy/pro`. The script
keeps an existing key. On Linux, run it as root once so that the container
user (uid 10001) can read the file. Then check the file with
`docker compose -f docker-compose.ha.yml config`.

The cluster's copy of the driver configuration records a fingerprint of
the key that sealed it. The fingerprint does not reveal the key. A node
whose key differs still applies the configuration, but it keeps every
channel that has credentials stopped. The channel shows the reason, and
`/api/ha/status` shows a warning.

This keeps the key out of the broker's storage. The witness and every
backup of the broker data hold only sealed credentials. The key stays
only where you install it.

### Not replicated: each node's own

Each node keeps the following on its own disk or volume:

- users, roles, passwords, sign-in providers and API tokens (a token works
  on the node that issued it only), and the viewer identities of view-only
  stations;
- an administrative lock of an account (the automatic sign-in lockout is
  shared since eaxScada `528843ae`: see
  [Sign-in lockouts on an HA cluster](#sign-in-lockouts-on-an-ha-cluster));
- view-only station pairing codes (a code is redeemed only on the node that
  issued it) and when and from which address each station was last seen;
- the historian, its configuration and the alarm journal (the history of
  alarm events);
- the licence and the AI assistant settings;
- key material: the driver secrets key, the PLC token signing key and the
  NATS NKey seeds (`-plc-nkey-seed`, `-edge-plc-seed-dir`). Copy them to
  every node by hand.

### Keep the nodes identical

1. Give every SCADA node the same driver secrets key, the same PLC token
   signing key and the same NKey seeds.
2. Create the same users and roles on every node, and the same viewer
   identities of view-only stations, with the same usernames.
3. Make engineering changes on the primary.
4. Before you rely on a failover, check `/api/ha/status` on every SCADA
   node: `config.drift` must be `false`, `config.pending` must be empty,
   and there must be no `config.warnings`.

## Upgrade an existing cluster to replicated configuration

Builds that replicate the engineering configuration start doing so as soon
as the cluster runs them. Upgrade the whole cluster in one maintenance
window, as described in [Upgrade the cluster](#upgrade-the-cluster). A
node running an older build does not read the cluster's configuration and
still accepts changes on its own disk.

1. Choose the node whose configuration is the reference: projects,
   screens, drivers, alarm definitions, recipes, 3D assets and dashboards.
   When the cluster starts for the first time on the new build, the node
   that becomes primary copies its own configuration into the cluster.
   Configure that node as `primary`.
2. Copy that node's driver secrets key to the other SCADA node, or point
   `EAX_DRIVER_SECRETS_KEY_FILE` at the same file on both nodes. With
   `docker-compose.ha.yml`, copy the reference node's key to
   `deploy/pro/secrets/driver_secrets.key` before the first start of the
   new build. The file is mounted on both nodes. If the reference node's
   credentials were sealed with a key you no longer have, create a new
   file and enter the credentials again after the upgrade.
3. Optionally, set `ha.public_url` on each SCADA node to the address users
   open, for example `https://scada-a.plant.local:8080`. A standby gives
   this address when it refuses a change. When the nodes share one
   configuration file, set `HA_PUBLIC_URL` in each node's environment
   instead.
4. Start the witness and the reference node, then the standby.

When the standby joins, every item it holds that differs from the cluster
is replaced by the cluster's version. Items the cluster does not have are
removed. Before it replaces or removes anything, the node copies its own
version to `data/ha-config-backup/<timestamp>/`, in one folder per kind:
`studio`, `drivers`, `alarms`, `recipes`, `assets3d` and `dashboards`.
Nothing is deleted without that copy. The node log shows
`this node adopted the cluster's configuration`, and `/api/ha/status`
reports it under `config.adopted`. A node that was stopped while changes
were made adopts them the same way when it starts again.

The witness keeps a replica of the configuration, 3D models included.
Give its data volume room for them.
