> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Distribution Monitoring

## Tracing

The distribution layer leverages tracing to provides insights into the queries executed in the cluster.

Check [tracing](https://docs.activeviam.com/engine/java-sdk/6.2/monitoring/tracing.md) to configure it.

## Logging

Without tracing activated, the distribution layer provides some logging.

### Logger naming

To retrieve all the distribution logs, two loggers should be activated:

* `atoti.server.distribution` (new namespace)
* `com.activeviam.activepivot.dist` (legacy namespace)

The issue with the legacy logger is that its name hierarchy (e.g. `com.activeviam.activepivot.dist.impl.private_`) contains internal packages and classes,
thus making it hard to filter using a more granular logger, as the full logger name hierarchy is not public, and can be changed between two bugfix versions.

The new logger (`atoti.server.distribution`) will provide some more granular loggers, to focus on one particular aspect of
the distribution such as "atoti.server.distribution.messaging", ...

The exhaustive list will be made available in the future, but will be fully contained under `atoti.server.distribution`.

As loggers will be moved gradually from `com.activeviam.activepivot.dist` to `atoti.server.distribution`,
it is recommended to activate both loggers in your project, to avoid missing any logs when upgrading to a newer bugfix version.

## What is the distribution state of a query cube?

The distribution state is the view a query cube holds of its cluster. It has three levels: the query
cube, its remote data cubes, and the branches of those data cubes.

The state is built only from what the query cube has received, starting with the messages published
by its data cubes and the JGroups cluster membership notifications. Reading it tells you how far
each data cube and each branch has got: which ones the query cube has fully accepted, which are
still waiting for a message, and which have failed.

This section assumes familiarity with the [distributed architecture](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_intro.md) and with
the [communication flow](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/communication_flow.md) between data cubes and query cubes.

> [!WARNING]
>
> The distribution state reports exactly one thing: what the query cube has **received**.
>
> * It is not what the data cubes have **sent**. A message a data cube has already published, but
>   that has not reached the query cube yet, leaves no trace in the state.
> * It is not what the query cube has **applied** onto its own cube. A transaction message that has
>   arrived already advances the state of the branch it targets, while the transaction itself may not
>   have been committed onto the query cube yet, so a query does not return its data.
>
> A data cube reported as `LIVE` therefore means that the query cube received its discovery, not that
> every message it sent has arrived, and not that the work has been applied. The endpoints listed in
> [Where does an event stand in the processing pipeline](#where-does-an-event-stand-in-the-processing-pipeline)
> report where that input stands in the processing pipeline.

### How to request the distribution state

Every distribution observability endpoint is served under the same prefix:

```
/activeviam/distribution-observability/rest/v1
```

They are all `GET` endpoints. Each one takes the name of a query cube as a path variable and returns
JSON. Each one answers `404` when no query cube goes by that name, and when distribution is not
started on that cube. They are documented request by request in the
[REST service](https://docs.activeviam.com/engine/rest/6.2.0/distribution-observability/).

The state of a query cube is read from the `state` endpoint:

```http theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
GET /activeviam/distribution-observability/rest/v1/{queryCubeName}/state
```

The response nests the three levels:

1. The top object carries `queryCube`, the state of the query cube itself, and `dataCubes`, a map
   keyed by data cube address.
2. Each entry of `dataCubes` carries `dataCube`, the state of that data cube, and `branches`, a map
   keyed by branch name.
3. Each entry of `branches` carries the state of that branch.

The response below describes a converged cluster. It holds one data cube, known under the address
`data-cube-A`, with a single `master` branch:

```json theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
{
  "queryCube": {
    "state": "READY",
    "discoveredApplications": null,
    "undiscoveredApplications": null,
    "retainedActions": null
  },
  "dataCubes": {
    "data-cube-A": {
      "dataCube": {
        "state": "LIVE",
        "lastHelloViewId": 7,
        "lastHelloSequenceNumber": 0,
        "membershipRegistered": true,
        "pendingDiscoveryAgeSeconds": null,
        "reservationExpired": null,
        "failure": null
      },
      "branches": {
        "master": {
          "state": "LIVE",
          "lastProcessableSequenceNumber": 1,
          "lastProcessableEpoch": 2,
          "failure": null
        }
      }
    }
  }
}
```

### What are the query cube states?

The query cube reports one of two states.

| State | Meaning |
| - | - |
| `AWAITING_DISCOVERY` | The schema of at least one application has not been received, so the actions that contribute to the cube are held back. This state is only reachable when the `IDistributedActivePivotInstanceDescription.WAIT_FOR_ALL_APPLICATIONS` property is `true`. |
| `READY` | The cube applies the actions it receives as they are produced, holding nothing back. |

The `queryCube` object carries these fields.

| Field | Meaning |
| - | - |
| `state` | The state of the query cube. |
| `discoveredApplications` | The applications that have published their schema. |
| `undiscoveredApplications` | The applications whose schema are still awaited. |
| `retainedActions` | The actions held back until the last schema arrives. |

Every field but `state` is `null` except in the `AWAITING_DISCOVERY` state. See
[Wait for All Applications](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_properties.md#wait-for-all-applications) to configure the
property that makes that state reachable.

### What are the remote data cube states?

Each data cube the query cube knows reports one of six states.

| State | Meaning |
| - | - |
| `AWAITING_HELLO` | The data cube is in the cluster view, but its Hello has not been received yet. |
| `AWAITING_DISCOVERY` | The Hello has been received, the discovery has not. No transaction can be processed. |
| `LIVE` | The discovery has been submitted. Transactions can be processed. |
| `PENDING_REMOVAL` | The cluster membership has been removed from the query cube, and the contributions are kept. A data cube rejoining shortly is therefore not forced into a rebuild. |
| `REMOVED` | The cluster membership and the contributions have both been removed from the query cube. |
| `BROKEN` | An action execution for this data node failed. Messages for it are no longer applied. A removal is still honored, so a partially broken cluster can be cleaned. |

Each `dataCube` object carries these fields.

| Field | Meaning |
| - | - |
| `state` | The state of the data cube. |
| `lastHelloViewId` | The cluster view of the last Hello received. |
| `lastHelloSequenceNumber` | The sequence number of the last Hello received. |
| `membershipRegistered` | Whether the query cube still holds the cluster membership of the data cube. |
| `pendingDiscoveryAgeSeconds` | How long ago the pending discovery slot was reserved. It is `null` except in the `AWAITING_DISCOVERY` state. |
| `reservationExpired` | Whether that reservation stopped counting towards the limit. It is `null` except in the `AWAITING_DISCOVERY` state. |
| `failure` | The failure that broke the data cube, rendered as a single line of text. It is `null` except in the `BROKEN` state. |

A reservation expires after the delay set by
[Pending discovery timeout](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_properties.md#pending-discovery-timeout), and the number of
reservations held at once is capped by
[Pending initial discoveries](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_properties.md#pending-initial-discoveries).

### What are the remote branch states?

Each branch of a data cube reports one of four states.

| State | Meaning |
| - | - |
| `PENDING_CREATION` | The branch-creation commit has not been applied yet. Either it has not been received, or a message preceding it is still missing. |
| `LIVE` | The creation commit has been submitted. Transactions can be processed. |
| `DELETED` | The branch has been deleted. |
| `BROKEN` | An action execution for this branch failed. Messages for it are no longer applied. A removal is still honored, so a partially broken cluster can be cleaned. |

Each branch object carries these fields.

| Field | Meaning |
| - | - |
| `state` | The state of the branch. |
| `lastProcessableSequenceNumber` | The last sequence number declared processable. |
| `lastProcessableEpoch` | The epoch of that message. It is `null` in the `PENDING_CREATION` and `DELETED` states, where the branch holds nothing. |
| `failure` | The failure that broke the branch, rendered as a single line of text. It is `null` except in the `BROKEN` state. |

### Where does an event stand in the processing pipeline?

The `state` endpoint stops at what the query cube received. Four more endpoints report where that
input stands in the processing pipeline.

```mermaid theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
%%{init: {'themeVariables': {'fontSize': '18px'}, 'flowchart': {'subGraphTitleMargin': {'top': 8, 'bottom': 10}}}}%%
flowchart TD
    subgraph sources["Event sources"]
        direction TB
        msg("Messages from the data cubes")
        member("Cluster membership notifications")
        subgraph scheduledGroup["Scheduled events"]
            scheduled("/{queryCubeName}/scheduledEvents")
        end
    end
    subgraph step1["1. The event is received and waits its turn"]
        e1("/{queryCubeName}/queuedEvents")
    end
    subgraph step2["2. The event mutates the state"]
        e2("/{queryCubeName}/state")
    end
    subgraph step3["3. The action waits its turn"]
        e3("/{queryCubeName}/queuedActions")
    end
    subgraph step4["4. The action is executed onto the query cube"]
        e4("/{queryCubeName}/inFlightAction")
    end
    sources --> step1
    step1 --> step2
    step2 -- "the event is turned into an action" --> step3
    step3 --> step4

    linkStyle 2 font-size:22px
    classDef group fill:#eff6ff,stroke:#2563eb,stroke-width:2px,color:#123a6b,font-size:22px,rx:12,ry:12
    classDef sourcesGroup fill:#ecfdf5,stroke:#059669,stroke-width:2px,color:#064e3b,font-size:22px,rx:12,ry:12
    classDef plain fill:#dbeafe,stroke:#2563eb,stroke-width:2px,color:#123a6b,font-size:22px,rx:12,ry:12
    classDef endpoint fill:#fde68a,stroke:none,stroke-width:0px,color:#7c2d12,font-size:18px,rx:25,ry:25
    class step1,step2,step3,step4 group
    class sources sourcesGroup
    class msg,member,scheduledGroup plain
    class scheduled,e1,e2,e3,e4 endpoint
```

| Endpoint | Content |
| - | - |
| `/{queryCubeName}/queuedEvents` | The events received but not yet processed. |
| `/{queryCubeName}/queuedActions` | The actions produced and waiting to be applied. |
| `/{queryCubeName}/inFlightAction` | The action currently being applied. The response is empty while the executor is idle. |
| `/{queryCubeName}/scheduledEvents` | The delayed events waiting to fire, such as restart orders and scheduled removals. |

### Which messages is a query cube holding back?

A data cube publishes its messages in a sequence. The query cube buffers a message whose predecessor
in that sequence has not arrived. It keeps that message until the gap is filled. A growing buffer
means the cluster is waiting on a message that never came.

```http theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
GET /activeviam/distribution-observability/rest/v1/{queryCubeName}/queuedMessages
```

The response groups the buffered messages by data cube address, then by branch, in ascending
sequence order. A data cube buffering nothing is left out. An empty response therefore means every
data cube is up to date.

### How to request the same information over JMX

Every endpoint above has a JMX counterpart, each carried by its own monitored child of the query
cube MBean.

| Monitored child | Operation | REST equivalent |
| - | - | - |
| `DistributionState` | `printDistributionState` | `state` |
| `EventInbox` | `listQueuedEvents` | `queuedEvents` |
| `DistributedActionQueue` | `listQueuedActions` | `queuedActions` |
| `DistributedActionExecutor` | `getInFlightAction` | `inFlightAction` |
| `DistributedEventScheduler` | `listScheduledEvents` | `scheduledEvents` |
| `DistributionState` child, one per data cube address | `listQueuedMessages` | `queuedMessages` |

See [JMX monitoring](https://docs.activeviam.com/engine/java-sdk/6.2/monitoring/jmx_monitoring.md) to connect an MBean client to the server.

> [!NOTE]
>
> `listQueuedMessages` is reported per data cube, as one child of `DistributionState` per data cube
> address, while the `queuedMessages` endpoint returns every data cube in a single response.

### What to read next

* [Communication flow](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/communication_flow.md) describes the messages exchanged inside a cluster.
* [Distributed properties](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_properties.md) lists the properties that drive the behavior
  described above.
* [Distribution Observability REST service](https://docs.activeviam.com/engine/rest/6.2.0/distribution-observability/)
  documents every endpoint of this section, request by request.
* [REST APIs](https://docs.activeviam.com/engine/java-sdk/6.2/rest-api/rest-apis-overview.md) lists the other REST services of Atoti Server.
