|
| 1 | +from pymilvus import utility, connections, Collection |
| 2 | +from pymilvus.client.constants import DEFAULT_RESOURCE_GROUP |
| 3 | +from pymilvus.client.types import ResourceGroupConfig |
| 4 | +from typing import List |
| 5 | +from example import create_connection, create_collection, insert, create_index |
| 6 | + |
| 7 | +_PENDING_NODES_RESOURCE_GROUP="pending_nodes" |
| 8 | +# Vector parameters |
| 9 | +_DIM = 128 |
| 10 | +_COLLECTION_NAME = 'rg_declarative_demo' |
| 11 | +_ID_FIELD_NAME = 'id_field' |
| 12 | +_VECTOR_FIELD_NAME = 'float_vector_field' |
| 13 | + |
| 14 | +def create_example_collection_and_load(replica_number: int, resource_groups: List[str]): |
| 15 | + print(f"\nCreate collection and load...") |
| 16 | + coll = create_collection(_COLLECTION_NAME, _ID_FIELD_NAME, _VECTOR_FIELD_NAME) |
| 17 | + insert(coll, 10000, _DIM) |
| 18 | + coll.flush() |
| 19 | + create_index(coll, _VECTOR_FIELD_NAME) |
| 20 | + coll.load(replica_number=replica_number, _resource_groups=resource_groups) |
| 21 | + |
| 22 | +def transfer_replica(src: str, dest: str, num_replica: int): |
| 23 | + utility.transfer_replica(source_group=src, target_group=dest, collection_name=_COLLECTION_NAME, num_replicas=num_replica) |
| 24 | + |
| 25 | +def list_replica(): |
| 26 | + coll = Collection(name=_COLLECTION_NAME) |
| 27 | + replicas = coll.get_replicas() |
| 28 | + print(replicas) |
| 29 | + |
| 30 | +def init_cluster(node_num: int): |
| 31 | + print(f"Init cluster with {node_num} nodes, all nodes will be put in default resource group") |
| 32 | + # create a pending resource group, which can used to hold the pending nodes that do not hold any data. |
| 33 | + utility.create_resource_group(name=_PENDING_NODES_RESOURCE_GROUP, config=ResourceGroupConfig( |
| 34 | + requests={"node_num": 0}, # this resource group can hold 0 nodes, no data will be load on it. |
| 35 | + limits={"node_num": 10000}, # this resource group can hold at most 10000 nodes |
| 36 | + )) |
| 37 | + |
| 38 | + # create a default resource group, which can used to hold the nodes that all initial node in it. |
| 39 | + utility.update_resource_groups({ |
| 40 | + DEFAULT_RESOURCE_GROUP: ResourceGroupConfig( |
| 41 | + requests={"node_num": node_num}, |
| 42 | + limits={"node_num": node_num}, |
| 43 | + transfer_from=[{"resource_group": _PENDING_NODES_RESOURCE_GROUP}], # recover missing node from pending resource group at high priority. |
| 44 | + transfer_to=[{"resource_group": _PENDING_NODES_RESOURCE_GROUP}], # recover redundant node to pending resource group at low priority. |
| 45 | + )}) |
| 46 | + |
| 47 | +def list_all_resource_groups(): |
| 48 | + rg_names = utility.list_resource_groups() |
| 49 | + |
| 50 | + for rg_name in rg_names: |
| 51 | + resource_group = utility.describe_resource_group(rg_name) |
| 52 | + print(resource_group) |
| 53 | + # print(f"Resource group {rg_name} has {resource_group.nodes} with config: {resource_group.config}") |
| 54 | + |
| 55 | +def scale_resource_group_to(name :str, node_num: int): |
| 56 | + """scale resource group to node_num nodes, new query node need to be added from outside orchestration system""" |
| 57 | + utility.update_resource_groups({ |
| 58 | + name: ResourceGroupConfig( |
| 59 | + requests={"node_num": node_num}, |
| 60 | + limits={"node_num": node_num}, |
| 61 | + transfer_from=[{"resource_group": _PENDING_NODES_RESOURCE_GROUP}], # recover missing node from pending resource group at high priority. |
| 62 | + transfer_to=[{"resource_group": _PENDING_NODES_RESOURCE_GROUP}], # recover redundant node to pending resource group at low priority. |
| 63 | + ) |
| 64 | + }) |
| 65 | + |
| 66 | +def create_resource_group(name: str, node_num: int): |
| 67 | + print(f"Create resource group {name} with {node_num} nodes") |
| 68 | + utility.create_resource_group(name, config=ResourceGroupConfig( |
| 69 | + requests={"node_num": node_num}, |
| 70 | + limits={"node_num": node_num}, |
| 71 | + transfer_from=[{"resource_group": _PENDING_NODES_RESOURCE_GROUP}], # recover missing node from pending resource group at high priority. |
| 72 | + transfer_to=[{"resource_group": _PENDING_NODES_RESOURCE_GROUP}], # recover redundant node to pending resource group at low priority. |
| 73 | + )) |
| 74 | + |
| 75 | +def resource_group_management(): |
| 76 | + # cluster is initialized with 1 node in default resource group, and 0 node in pending resource group. |
| 77 | + init_cluster(1) |
| 78 | + list_all_resource_groups() |
| 79 | + # DEFAULT_RESOURCE_GROUP: 1 |
| 80 | + # _PENDING_NODES_RESOURCE_GROUP: 0 |
| 81 | + |
| 82 | + # rg1 missing two query node. |
| 83 | + # create_resource_group("rg1", 2) |
| 84 | + list_all_resource_groups() |
| 85 | + # DEFAULT_RESOURCE_GROUP: 1 |
| 86 | + # _PENDING_NODES_RESOURCE_GROUP: 0 |
| 87 | + # rg1: 0(missing 2) |
| 88 | + |
| 89 | + # scale_out(2) |
| 90 | + # scale out two new query node into cluster by orchestration system, these node will be added to rg1 automatically. |
| 91 | + list_all_resource_groups() |
| 92 | + # DEFAULT_RESOURCE_GROUP: 1 |
| 93 | + # _PENDING_NODES_RESOURCE_GROUP: 0 |
| 94 | + # rg1: 2 |
| 95 | + |
| 96 | + |
| 97 | + # rg1 missing one query node. |
| 98 | + scale_resource_group_to("rg1", 3) |
| 99 | + list_all_resource_groups() |
| 100 | + # DEFAULT_RESOURCE_GROUP: 1 |
| 101 | + # _PENDING_NODES_RESOURCE_GROUP: 0 |
| 102 | + # rg1: 2(missing 1) |
| 103 | + |
| 104 | + # scale_out(2) |
| 105 | + # scale out two new query node into cluster by orchestration system, one node will be added to rg1 automatically |
| 106 | + # and one redundant node will be added to pending resource group. |
| 107 | + list_all_resource_groups() |
| 108 | + # DEFAULT_RESOURCE_GROUP: 1 |
| 109 | + # _PENDING_NODES_RESOURCE_GROUP: 1 |
| 110 | + # rg1: 3 |
| 111 | + |
| 112 | + scale_resource_group_to("rg1", 1) |
| 113 | + list_all_resource_groups() |
| 114 | + # DEFAULT_RESOURCE_GROUP: 1 |
| 115 | + # _PENDING_NODES_RESOURCE_GROUP: 3 |
| 116 | + # rg1: 1 |
| 117 | + |
| 118 | + # rg2 missing three query node, will be added from pending resource group. |
| 119 | + # create_resource_group("rg2", 3) |
| 120 | + list_all_resource_groups() |
| 121 | + # DEFAULT_RESOURCE_GROUP: 1 |
| 122 | + # _PENDING_NODES_RESOURCE_GROUP: 0 |
| 123 | + # rg1: 1 |
| 124 | + # rg2: 3 |
| 125 | + |
| 126 | + scale_resource_group_to(DEFAULT_RESOURCE_GROUP, 5) |
| 127 | + list_all_resource_groups() |
| 128 | + # DEFAULT_RESOURCE_GROUP: 1(missing 4) |
| 129 | + # _PENDING_NODES_RESOURCE_GROUP: 0 |
| 130 | + # rg1: 1 |
| 131 | + # rg2: 3 |
| 132 | + |
| 133 | + # scale_out(4) |
| 134 | + list_all_resource_groups() |
| 135 | + # DEFAULT_RESOURCE_GROUP: 5 |
| 136 | + # _PENDING_NODES_RESOURCE_GROUP: 1 |
| 137 | + # rg1: 1 |
| 138 | + # rg2: 3 |
| 139 | + |
| 140 | +def replica_management(): |
| 141 | + # load collection into default. |
| 142 | + # create_example_collection_and_load(5, [DEFAULT_RESOURCE_GROUP]) |
| 143 | + # one replica per node in default resource group. |
| 144 | + transfer_replica(DEFAULT_RESOURCE_GROUP, "rg1", 1) |
| 145 | + transfer_replica(DEFAULT_RESOURCE_GROUP, "rg2", 2) |
| 146 | + # DEFAULT_RESOURCE_GROUP: 2 replica on 5 nodes. |
| 147 | + # rg1: 1 replica on 1 node. |
| 148 | + # rg2: 2 replica on 3 nodes. |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + create_connection() |
| 152 | + resource_group_management() |
| 153 | + replica_management() |
0 commit comments