Internal State in YTsaurus Flow (Go)

Internal State is the mechanism for working with internal state stored in the internal tables of Flow. Unlike External State, you don’t need to create the tables yourself — Flow manages them automatically.

For more on accessors and the general principles of working with state, see State Accessor (Go).

General information about stateful processing is available in the Stateful processing section.

Overview

The Go SDK provides three kinds of accessors for working with Internal State, differing in the serialization format. Separate accessors are used for external state — see External State (Go).

Accessor Format Opened by the function
YSONState YSON flow.OpenYSONState[T]
RawStateAccessor []byte flow.OpenRawState
ProtoState Protobuf flow.OpenProtoState[T]

RawStateAccessor reads and writes raw bytes explicitly through Get, Set, and Clear. YSONState and ProtoState provide a mutable value: the changes made to it are serialized automatically after the batch handlers complete successfully — see Changing the value in place.

Each of the flow.OpenXxxState functions takes three arguments:

  • rt — the flow.Runtime of the handler.
  • name — the state name. Internal state names don’t start with / and must be declared in parameters.internal_states of the computation spec, see Configuration in the static spec.
  • input — the input to whose key the state is bound. Any value implementing flow.Input will do: flow.ExtendedMessage, flow.Timer, flow.Visit.

Note

The accessor shows the state as it will be after the response to the worker: a state that wasn’t in the incoming request and a state cleared in the same call through Clear are read the same way — as missing.

Changing the value in place

The value returned by Value(), Get(), and Or() is live: it is decoded once per key and batch, every accessor for that key returns the same value, and the changes made to it are written to the state at the end of the batch without a Set() call. Nothing is written when the value did not change: a state that serializes to the bytes it arrived with does not go back. The default from Value() and Or() becomes the state value and is written as after Set(), so it can be changed right away:

state, err := flow.OpenYSONState[wordCountState](rt, "word-state", msg)
if err != nil {
    return err
}
state.Value().Count++

ProtoState.Set() still replaces the whole message, and Clear() removes the state. An empty Protobuf message serializes to zero bytes, and zero bytes are the absence of a state: an Or(&T{}) default is not written, while a message whose fields were all unset and a Set(&T{}) both delete the state. RawStateAccessor hands out no mutable value: its Get() returns a copy of the bytes, and they are only written through Set(). External State states are not tracked in place either.

A key has a single decoded value per batch, so opening the same state and key with another Go type returns an error. If a handler returned an error, the in-place changes don’t make it into the response to the worker. A state that Go serializes to bytes other than the ones it arrived with is rewritten canonically on the first read — once per key.

To detect the changes, a value that was read is re-encoded at the end of the batch. When the computation only reads a state, use ReadOnly(): that view returns the same value but does not track it, and ReadOnlyYSONState.Value() and ReadOnlyProtoState.Or() do not create the state. It has no write methods, so writing through it does not compile. The value is shared with the mutable state of the same key: if that state is also opened for writing somewhere, the changes made through the view still reach the worker.

count := state.ReadOnly().Value().Count

YSONState

Source code

YSONState[T] stores the state as a YSON-serialized value of type T. The type can be any structure with yson tags, as well as a map, a slice, or a scalar — anything yson.Marshal understands.

Getting the state

// For a message
state, err := flow.OpenYSONState[wordCountState](rt, "word-state", msg)

// For a timer
state, err := flow.OpenYSONState[wordCountState](rt, "word-state", timer)

Deserialization is performed on opening. Opening the same state and key again within the request returns the same mutable value.

Methods

Method Result type Description
Empty() bool Check whether the value is missing
Get() (*T, bool) Get the mutable value. The second result distinguishes a saved state from a missing one
Value() *T Get the mutable value; a zero value is created if the state is missing
Clear() — Delete the value
ReadOnly() ReadOnlyYSONState[T] A read-only view

The changes to the value are serialized automatically after all the batch handlers complete successfully. If a handler returned an error, the changes to the YSON state don’t make it into the response to the worker.

Example from WordCount

The type the pipeline stores for one word:


type wordCountState struct {
	Word  string `yson:"word"`
	Count int64  `yson:"count"`
}

The message handler:


type wordCountMapper struct{}

var _ flow.RowFunction = (*wordCountMapper)(nil)

func (*wordCountMapper) OnMessage(
	ctx context.Context,
	rt flow.Runtime,
	msg flow.ExtendedMessage,
	out flow.OutputCollector,
) error {
	var input wordMessage
	if err := msg.ConvertTo(&input); err != nil {
		return err
	}

	state, err := flow.OpenYSONState[wordCountState](rt, wordStateName, msg)
	if err != nil {
		return err
	}

	fresh := state.Empty()
	counter := state.Value()
	if fresh {
		counter.Word = input.Word
	}
	counter.Count++
	return nil
}

Full source code

Here the state is bound to the key of the message. For a new key, Empty() returns true, and Value() creates an empty wordCountState. Assignments to the fields are saved without a separate Set.

The same state is also opened in the timer handler. This is how the URL Downloader works: OnMessage accumulates a batch, and OnTimer reads it and clears it through Clear.

RawStateAccessor

Source code

RawStateAccessor works with raw bytes without serialization and deserialization. It is the accessor the other two are built on top of — take it when you define the state format yourself.

Getting the accessor

// For a message
state, err := flow.OpenRawState(rt, "raw-state", msg)

// For a timer
state, err := flow.OpenRawState(rt, "raw-state", timer)

Methods

Method Result type Description
Get() ([]byte, bool) Get the raw bytes. The second result distinguishes a saved state from a missing one
Or(fallback []byte) []byte Return the current value, or fallback if there is no state
Set(data []byte) error Save the raw bytes; empty bytes delete the state
Clear() error Delete the state for the current key

The Get and Or methods don’t return an error: there is nothing to deserialize here.

Usage example

state, err := flow.OpenRawState(rt, "raw-state", msg)
if err != nil {
    return err
}

// Reading the raw data
if data, ok := state.Get(); ok {
    // Processing the raw data...
    _ = data
}

// Writing the raw data
if err := state.Set([]byte{0x01, 0x02, 0x03}); err != nil {
    return err
}

// Clearing
return state.Clear()

ProtoState

Source code

ProtoState serializes the state through Protobuf. The type of the Protobuf message is given in its value form, and the state returns a pointer to it: flow.OpenProtoState[TJoinState] returns a state over *TJoinState.

Getting the state

// For a message
state, err := flow.OpenProtoState[TJoinState](rt, "join-state", msg)

// For a timer
state, err := flow.OpenProtoState[TJoinState](rt, "join-state", timer)

Deserialization is performed on opening. Opening the same state and key again within the request returns the same mutable message.

Methods

Method Result type Description
Empty() bool Check whether the value is missing
Get() (*T, bool) Get the mutable message. The second result distinguishes a saved state from a missing one
Or(fallback *T) *T Return the current message, or write and return fallback if there is no state
Set(value *T) error Replace the whole state value; an empty message deletes the state
Clear() — Delete the state for the current key
ReadOnly() ReadOnlyProtoState[T, PT] A read-only view

Note

Unlike in Python, where get_or_default() without arguments returns an empty instance of the Proto class, in Go the default value is set explicitly — pass &T{} if you want to start with an empty message. An empty message serializes to zero bytes, and zero bytes mean the state is absent: such a message is not written for a key that has no state, and deletes the state of a key that has one. Note that a message whose every field holds its zero value is empty in exactly this sense.

Usage example

state, err := flow.OpenProtoState[TJoinState](rt, "join-state", msg)
if err != nil {
    return err
}

window := state.Or(&TJoinState{})
window.ShowTime = showTime

return nil

Configuration in the static spec

Internal State doesn’t require creating external tables. The states are stored automatically in the internal tables of Flow.

The names of internal states must be declared in the internal_states section of the computation parameters in the static spec:

{
    "spec" = {
        "computations" = {
            "reader" = {
                "computation_class_name" = "NYT::NFlow::TSwiftPassthroughOrderedSourceComputation";
                "output_stream_ids" = ["words"];
                "source_streams" = {
                    "queue" = {
                        "source_class_name" = "NYT::NFlow::TQueueSource";
                        "parameters" = {
                            "queue_path" = "<cluster=cluster_name>//path/to/queue";
                            "consumer_path" = "<cluster=cluster_name>//path/to/consumer";
                            "finite" = false;
                        };
                    };
                };
                "parameters" = {};
            };
            "mapper" = {
                "computation_class_name" = "NYT::NFlow::NCompanion::TTransformCompanionComputation";
                "group_by_schema" = [
                    {"name" = "hash"; "expression" = "farm_hash(word)"; "type" = "uint64"; required = %true;};
                    {"name" = "word"; "type" = "string";};
                ];
                "input_stream_ids" = ["words"];
                "output_stream_ids" = [];
                "required_resource_ids" = {
                    "CompanionManager" = {
                        "worker" = true;
                        "controller" = false;
                    };
                };
                "parameters" = {
                    "internal_states" = ["word-state"];
                };
            };
        };
        "resources" = {
            "CompanionManager" = {
                "resource_class_name" = "NYT::NFlow::NCompanion::TCompanionManager";
                "parameters" = {
                };
                "dependencies" = {};
            };
        };
    };
    "dynamic_spec" = {
        "computations" = {
            "reader" = {
                "parameters" = {
                };
            };
            "mapper" = {
                "parameters" = {
                };
            };
        };
    };
}

The state name in the code (the second argument of flow.OpenYSONState, flow.OpenRawState, or flow.OpenProtoState) must match the name declared in internal_states.

Warning

If the state name isn’t declared in internal_states, the opening function returns an error wrapping flow.ErrUnknownState; the error text lists the declared names. An error returned from a handler stops the processing of the whole batch — the worker retries the entire request.

See also