Skip to main content

Distributed FSM over etcd

Project description

etcd-fsm

🤖 absolute ai, 0% of code written by human 🤖


A typed, asynchronous distributed finite-state machine over etcd.

The current implementation contains the declarative type system, schema and processor compiler, signature-derived transition graph, an internal etcd contract, a deterministic MVCC fake, and the production aetcd adapter. The production runtime is under construction.

from enum import StrEnum, auto
from typing import Literal
from uuid import UUID

from etcd_fsm import BaseState, FSMProcessor, FSMSchema, Retry


class OrderStatus(StrEnum):
    CREATED = auto()
    PAYMENT_REQUIRED = auto()


class OrderState[S: OrderStatus](BaseState[S]):
    order_id: UUID

    def fsm_key(self) -> str:
        return str(self.order_id)


class Created(OrderState[Literal[OrderStatus.CREATED]]):
    state: Literal[OrderStatus.CREATED] = OrderStatus.CREATED


class PaymentRequired(OrderState[Literal[OrderStatus.PAYMENT_REQUIRED]]):
    state: Literal[OrderStatus.PAYMENT_REQUIRED] = OrderStatus.PAYMENT_REQUIRED
    attempt: int = 1


orders = FSMSchema(
    name="orders",
    namespace="/state/orders",
    state_enum=OrderStatus,
    states=(Created, PaymentRequired),
)
payments = FSMProcessor(schema=orders, name="payments")


@payments.reaction()
async def request_payment(state: Created) -> PaymentRequired | Retry:
    return PaymentRequired(order_id=state.order_id)


payments.finalize()

Function annotations are the transition definition. Concrete State Models use literal enum discriminators, models are immutable snapshots, and the schema rejects incomplete or inconsistent declarations before a runtime starts.

The public Python API and graph CLI are documented in Russian in docs/API.md. The PyPI release process is documented in docs/RELEASING.md.

Transition graph

Import modules containing schemas and processors and render their combined transition graph as Mermaid:

etcd-fsm graph my_service.orders my_service.payments

Python file paths are accepted as well:

etcd-fsm graph tests/order_domain.py

Every transition is labeled with its processor, function, and producer kind. Use explicit module:OBJECT or path/to/file.py:OBJECT references when automatic discovery is not appropriate, or render Graphviz DOT into a file:

etcd-fsm graph my_service.orders:ORDER_SCHEMA \
  my_service.payments:PAYMENTS --format dot -o orders.dot

The architectural vocabulary is documented in CONTEXT.md. Design premises and implementation order are recorded in docs/DESIGN-NOTES.md, with accepted decisions under docs/adr/.

Local etcd

Start an ephemeral etcd instance for integration tests:

docker compose up -d --wait

The client endpoint is http://127.0.0.1:12379. Stop it and discard its data with docker compose down.

Run the real-etcd integration suite explicitly:

uv run pytest --run-integration -m integration

The integration tests use a unique /etcd-fsm-tests/<uuid>/ prefix and clean it after each test. Override the endpoint with ETCD_FSM_TEST_HOST and ETCD_FSM_TEST_PORT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

etcd_fsm-0.1.2.tar.gz (76.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

etcd_fsm-0.1.2-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file etcd_fsm-0.1.2.tar.gz.

File metadata

  • Download URL: etcd_fsm-0.1.2.tar.gz
  • Upload date:
  • Size: 76.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for etcd_fsm-0.1.2.tar.gz
Algorithm Hash digest
SHA256 242f236a526f21ded5531b104a223bf55f3fc0f6e705a13d360103d5f628f7a7
MD5 ff34b6dd08dfa0ded3cf023b0ff81de6
BLAKE2b-256 653443e195aa91a9acc04ced01116f86a8276e50577b882beb5e11ca3544f7f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for etcd_fsm-0.1.2.tar.gz:

Publisher: release.yml on Rubikoid/etcd-fsm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file etcd_fsm-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: etcd_fsm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for etcd_fsm-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 677c3657a871916d20db53a350d162523f37bb8870af57795a0313d80c32a9f6
MD5 785868ef2e865b839628d3349c6b53be
BLAKE2b-256 1fef0ce8088576de4e9ee08b60b8f32e813fb47b552f98732eb05d15f4408103

See more details on using hashes here.

Provenance

The following attestation bundles were made for etcd_fsm-0.1.2-py3-none-any.whl:

Publisher: release.yml on Rubikoid/etcd-fsm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page