To create your own cluster in the Cellframe network, you should firstly know more about cluster architecture in our blockchain.
If you already know what cluster are, click on the link [[Create your own Cluster with Python#python example|python example]].
![[Architecture Overview#Network clusterization]]
## Python example
Here is the example of how to create your own **AUTONOMIC** cluster in the Backbone network using python script for 3 nodes with **ROOT** role.
```python
from pycfhelpers.node.net import CFNodeAddress
from pycfhelpers.node import CFNet
from pycfhelpers.node.crypto import CFGUUID
# imported necessary python libraries
BACKBONE_NET = CFNet("Backbone")
MY_CLUSTER_ID = 0xAA
CLUSTER_NODES = [CFNodeAddress("AAAA::AAAA::AAAA::AAAA"),
CFNodeAddress("BBBB::BBBB::BBBB::BBBB"),
CFNodeAddress("CCCC::CCCC::CCCC::CCCC")]
# created three nodes and specified their addresses
def setup_cluster():
net = BACKBONE_NET
cluster = CFGDBCluster("myclustername", #mnemomic
CFGUUID.compose(net.id.long, MY_CLUSTER_ID), #cluster id
"mycluster.gdb.group.mask.*", #gdb groups mask
3, #TTL of objects in cluster DB (hours)
True, # Root access for cluster owner
CFGDBCluster.MemberRole.NOBODY, # ignores for AUTONOMIC
CFGDBCluster.ClusterRole.AUTONOMIC) #Autonomic cluster
# created constructor for new cluster with specified parameters (full description in more detailed chapter)
cluster.add_net_associate(net) #important, cluster will go online if net does
# associated cluster with the network
#add members
for member in CLUSTER_NODES:
cluster.member_add(member, CFGDBCluster.MemberRole.ROOT)
# added members and gave them root role
```
### More detailed description of the snippet
**Imports:**
```python
from pycfhelpers.node.net import CFNodeAddress
from pycfhelpers.node import CFNet
from pycfhelpers.node.crypto import CFGUUID
```
These lines import necessary modules from the `pycfhelpers` library:
> [!NOTE] Important
> For more information about **pycfhelpers** library, see article [[Sphinx AutoDocumentation]]
- `CFNodeAddress`: This class represents an address for a node in the network.
- `CFNet`: This class represents a specific network within the system.
- `CFGUUID`: This class deals with generating unique identifiers.
**Constants:**
```python
BACKBONE_NET = CFNet("Backbone")
MY_CLUSTER_ID = 0xAA
CLUSTER_NODES = [CFNodeAddress("AAAA::AAAA::AAAA::AAAA"),
CFNodeAddress("BBBB::BBBB::BBBB::BBBB"),
CFNodeAddress("CCCC::CCCC::CCCC::CCCC")]
```
- `BACKBONE_NET`: Defines a network named "Backbone" using the `CFNet` class.
- `MY_CLUSTER_ID`: Sets a constant cluster ID with the value `0xAA`.
- `CLUSTER_NODES`: Defines a list containing three `CFNodeAddress` objects with addresses specified.
**Function Definition:**
```python
def setup_cluster():
```
This line defines a function named `setup_cluster` for setting up a cluster.
**Function Body:**
```python
net = BACKBONE_NET
```
This line assigns the previously defined `BACKBONE_NET` constant to a local variable `net`.
**Cluster Creation:**
```python
cluster = CFGDBCluster("myclustername", # mnemomic
CFGUUID.compose(net.id.long, MY_CLUSTER_ID), # cluster ID
"mycluster.gdb.group.mask.*", # gdb groups mask
3, # TTL of objects in cluster DB (in hours)
True, # Root access for owner
CFGDBCluster.MemberRole.NOBODY, # ignores for AUTONOMIC (role of the used node)
CFGDBCluster.ClusterRole.AUTONOMIC) # Autonomic cluster (must be common)
```
This line creates a cluster object using a class named `CFGDBCluster`. The arguments passed to the constructor are:
- `"myclustername"`: (string) A name for the cluster
- `CFGUUID.compose(net.id.long, MY_CLUSTER_ID)`: (method which returns CFGUUID) Creates a unique cluster ID using the `CFGUUID` class. It combines the network ID with the `MY_CLUSTER_ID` constant.
- `"mycluster.gdb.group.mask.*"`: (string) Defines a mask for GDB groups, which is used in internal communication between nodes.
- `3`: (integer) Sets the Time-To-Live (TTL) for objects in the cluster database to 3 (hours).
- `True`: (boolean) Defines whether cluster owner get ROOT access or not (yes if true).
- `CFGDBCluster.MemberRole.NOBODY`: (constant) Sets **NOBODY** as member role.
- `CFGDBCluster.ClusterRole.AUTONOMIC`: (constant) Sets **AUTONOMIC** as cluster role.
**Associating Network:**
```python
cluster.add_net_associate(net) # important, cluster will go online if net does
```
This line associates the previously defined `net` (Backbone network) with the cluster. It is an important step for the cluster to become operational.
**Adding Members:**
```python
# add members
for member in CLUSTER_NODES:
cluster.member_add(member, CFGDBCluster.MemberRole.ROOT)
```
This loop iterates through the `CLUSTER_NODES` list (containing node addresses) and adds each node as a member to the cluster. The `member_add` function takes two arguments:
- `member`: The address of the node to be added (from `CLUSTER_NODES`).
- `CFGDBCluster.MemberRole.ROOT`: This sets the role of the added member to `ROOT`.