On this page

Share one instance across a cluster๏ƒ

Prerequisites: dynamic linking and permissions. A named cluster groups dynamic conduits for unique_per_conduit_cluster resolution.

The saved cluster lesson remains Intermediate 25, its original location. Its topic belongs here as well, so both guides point to the same lesson.

Establish the sharing structure๏ƒ

The example binds a cluster bus with create permissions, conjures two named conduits, and links them. It creates the cluster through the cloud, adds the owner, elects the owner as leader, and then adds the second member.

Leader election matters: the shared creations store belongs to the cluster, and it is served only while a leader is elected. Membership alone does not serve it. A leader change keeps the shared object, and deleting the cluster disposes it. Links matter too: the sharing contracts use the relationship between the conduits.

The final assertion checks that both members resolve the same bus object. Use that identity check when adapting the pattern, not merely the membership list.

The workflow in code๏ƒ

These are the core steps from the saved example. Use its complete linked script for all class definitions and setup.

 1def main() -> None:
 2    """Build a linked cluster and assert that its members resolve the same bus."""
 3    owner_book = dynamic_spellbook()
 4    # permissions="create" matters: cluster auto-sharing contracts the
 5    # spell to each joining member WITH the spell's own permissions.
 6    owner_book.bind(spell=ClusterBus,
 7                    existence="unique_per_conduit_cluster",
 8                    permissions="create")
 9    owner = owner_book.conjure(dynamic=True, name="cluster-owner")
10    member = dynamic_spellbook().conjure(dynamic=True, name="cluster-member")
11
12    # Contracts ride LINKS: the cluster auto-share adds spells into the
13    # members' existing contracts, and the contract bucket is born at
14    # link(). Unlinked members share NOTHING.
15    owner.link(member)
16
17    cloud = owner.get_conduit_cloud()
18    cloud.create_cluster("workers")
19    cloud.add_conduit_to_cluster(owner, "workers")
20
21    # ELECT THE LEADER before more members join: the cluster's shared
22    # store belongs to the cluster and is served while a leader is elected,
23    # and joins auto-share against the armed cluster - then the rest falls
24    # into place.
25    cloud.get_cluster("workers").elect_leader(owner.id)
26
27    cloud.add_conduit_to_cluster(member, "workers")
28
29    bus_a = owner.meld("ClusterBus")
30    bus_b = member.meld("ClusterBus")
31    assert bus_a is bus_b, "cluster members must share the cluster's bus"
32    print("one bus per cluster, shared by members:", bus_a is bus_b)

Runnable examples๏ƒ

All advanced examples ยท Level contents ยท Full contents

Canonical page source