StateAccessor in YTsaurus Flow (Go)
StateAccessor is the interface for reading, modifying, and deleting state values.
General information about stateful processing is available in the Stateful processing section.
How it works
State in Flow is stored in sorted dynamic tables.
For external state, you create this table yourself; for internal state, the tables are created and managed by Flow automatically.
For TTransformCompanionComputation, the key columns of the state table match the group_by_schema of the computation. For the internal state of TTransformOrderedSourceCompanionComputation, the key is the source partition key: group_by_schema isn’t supported in such a SourceComputation. In every case, messages with the same key share one state.
In Go, an accessor is a value that binds two things together: the holder of the state with a given name and the key of a specific input. The holders live in flow.Runtime and are available directly (rt.InternalState(name), rt.ExternalState(name), rt.JoinedExternalState(name)), but an ordinary computation doesn’t need them: the accessor is exactly the convenient view of the state of one key.
Reading and writing data
Working with the table directly (reading, writing, and deleting data) is done by the worker. When the next batch of messages arrives, the worker loads the state values for all the keys in the batch and sends them to the companion together with the messages and timers. For more details, see the interaction schema.
New values are written to the state table transactionally within an epoch.
What travels back to the worker isn’t the whole state but a delta: only the changed records. For Raw and External states, the write is performed via Set or Clear; YSON and Proto states are written by changing the value in place. A plain read sends nothing.
Clear doesn’t erase the record from the accessor, it marks the record as deleted, and the deletion reaches the worker in exactly that form. For the computation there is no difference: a state the request didn’t bring and a state cleared in this request are both read as missing — the computation sees the state as it will be after the response.
Warning
flow.Runtime and all the accessors opened from it belong to the goroutine serving the request and aren’t designed for concurrent use. If a handler parallelizes its work, read and write the state in the same goroutine the handler was called in. The rules for starting child goroutines are described in the Goroutines in a handler section.
Accessor types
The Go SDK provides five types of accessors:
| Accessor | Format | Opening | Description |
|---|---|---|---|
| RawStateAccessor | []byte |
flow.OpenRawState(rt, name, input) |
Raw bytes without serialization |
| YSONState | YSON | flow.OpenYSONState[T](rt, name, input) |
Serialization of a Go value into YSON |
| ProtoState | Protobuf | flow.OpenProtoState[T](rt, name, input) |
Serialization via Protobuf |
| ExternalStateAccessor | Go structure (table row) | flow.OpenExternalState(rt, "/name", input) |
Reading and writing a row of an external table |
| JoinedExternalStateAccessor | Go structure | flow.OpenJoinedExternalState(rt, "/name", input) |
Read-only access to another computation’s state table |
The first three work with internal state, whose tables are managed by Flow. RawStateAccessor serializes explicit writes; YSONState and ProtoState hold a mutable value and save it automatically after a successful batch.
ExternalStateAccessor and JoinedExternalStateAccessor work with external state — a dynamic table that you create yourself. They differ in rights: the first is available to the computation that owns the state, the second to the computation that only reads it.
Internal state API
The Raw accessor uses explicit read and write operations:
| Method | Result type | Description |
|---|---|---|
Get() |
([]byte, bool) |
A copy of the saved bytes |
Or(fallback []byte) |
[]byte |
The current value or fallback |
Set(data []byte) |
error |
Save the bytes; empty bytes delete the state |
Clear() |
error |
Delete the state |
YSON and Proto states are changed in place:
| Method | YSONState[T] |
ProtoState[T, PT] |
|---|---|---|
Empty() |
bool |
bool |
Get() |
(*T, bool) |
(PT, bool) |
Value() / Or(fallback) |
*T |
PT |
Set(value) |
no method | error |
Clear() |
no result | no result |
ReadOnly() |
ReadOnlyYSONState[T] |
ReadOnlyProtoState[T, PT] |
Deserialization is performed on opening. Changes to the value are saved automatically only after all the batch handlers have completed successfully — see Changing the value in place.
ExternalStateAccessor converts a table row into a Go structure:
| Method | Result type | Description |
|---|---|---|
ConvertTo(&value) |
(bool, error) |
Read the row into the structure; bool distinguishes a missing row |
ConvertFrom(&value) |
error |
Save the fields of the structure into the row |
Clear() |
error |
Delete the row |
JoinedExternalStateAccessor provides ConvertTo(&value), but neither writing nor clearing. For dynamic schemas, both accessors keep the low-level Get, Or, and Schema, and the owner also keeps Builder and Set.
Getting an accessor
An accessor is opened by a free function inside OnMessage, OnTimer, or OnVisit:
func (*myFunction) OnMessage(
ctx context.Context,
rt flow.Runtime,
msg flow.ExtendedMessage,
out flow.OutputCollector,
) error {
// YSON
ysonState, err := flow.OpenYSONState[myState](rt, "state-name", msg)
// Raw bytes
rawState, err := flow.OpenRawState(rt, "state-name", msg)
// Protobuf
protoState, err := flow.OpenProtoState[TMyState](rt, "state-name", msg)
// External state: the name must start with "/"
extState, err := flow.OpenExternalState(rt, "/state-name", msg)
// Read-only external state
joined, err := flow.OpenJoinedExternalState(rt, "/reference", msg)
}
func (*myFunction) OnTimer(
ctx context.Context,
rt flow.Runtime,
timer flow.Timer,
out flow.OutputCollector,
) error {
// The same thing, but a timer is passed instead of a message
state, err := flow.OpenYSONState[myState](rt, "state-name", timer)
}
Parameters:
rt—flow.Runtime, the second argument of the handler.name— the name of the state declared in the static spec. For internal states, it is an arbitrary string frominternal_states; for external ones, a key fromexternal_state_managersorexternal_state_joinersthat must start with/.input— the input to whose key the accessor is bound. Any value implementingflow.Inputwill do:flow.ExtendedMessage,flow.Timer, andflow.Visit.
The type parameter is specified only for the YSON and Proto accessors and sets the type of the state: flow.OpenProtoState[TMyState] produces an accessor over *TMyState. This is exactly why the opening functions are free functions rather than methods of Runtime: methods in Go have no type parameters of their own.
Opening returns an error if the state name isn’t suitable:
| Error | Reason |
|---|---|
flow.ErrUnknownState |
The name isn’t declared in the computation spec |
flow.ErrInvalidStateName |
The external state name isn’t an absolute path |
flow.ErrStateNotRead |
The request didn’t bring an external state with this name |
flow.ErrNoStateSchema |
The external state arrived without the schema of its rows |
Note
flow.ErrStateNotRead when opening a joined state is a normal situation, not a failure: the worker joins only those keys for which it found rows, so a batch that didn’t match any row of the reference dataset arrives without such a state. Distinguish this error with errors.Is and treat it as an absence of data.
When to use each type
| Situation | Recommended accessor |
|---|---|
| A structure with several fields whose schema changes together with the code | flow.OpenYSONState[T] (YSON) |
| Arbitrary binary data or your own serialization | flow.OpenRawState (Raw) |
| Structured data with a fixed schema shared with other languages | flow.OpenProtoState[T] (Protobuf) |
| Data that must be accessible from other systems | flow.OpenExternalState (External) |
| Data that requires a user table | flow.OpenExternalState (External) |
| A reference dataset that the computation only reads | flow.OpenJoinedExternalState (Joined) |
See also
- Internal State (Go)
- External State (Go)
- Working with states (Go) — a brief overview of all the types
- Computation (Go)
- Stateful processing