Spaces:
Build error
Build error
File size: 6,299 Bytes
3932407 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
import pathlib
from .utils import *
N_PEERS = 3
N_SHARDS = 1
N_REPLICAS = 1
COLLECTION_NAME = "test_collection"
def create_collection_with_custom_sharding(
peer_url,
collection=COLLECTION_NAME,
shard_number=1,
replication_factor=1,
write_consistency_factor=1,
timeout=10
):
# Create collection in peer_url
r_batch = requests.put(
f"{peer_url}/collections/{collection}?timeout={timeout}", json={
"vectors": {
"size": 4,
"distance": "Dot"
},
"shard_number": shard_number,
"replication_factor": replication_factor,
"write_consistency_factor": write_consistency_factor,
"sharding_method": "custom",
})
assert_http_ok(r_batch)
def create_shard(
peer_url,
collection,
shard_key,
shard_number=1,
replication_factor=1,
placement=None,
timeout=10
):
r_batch = requests.put(
f"{peer_url}/collections/{collection}/shards?timeout={timeout}", json={
"shard_key": shard_key,
"shards_number": shard_number,
"replication_factor": replication_factor,
"placement": placement,
})
assert_http_ok(r_batch)
def delete_shard(
peer_url,
collection,
shard_key,
timeout=10
):
r_batch = requests.post(
f"{peer_url}/collections/{collection}/shards/delete?timeout={timeout}",
json={
"shard_key": shard_key,
}
)
assert_http_ok(r_batch)
def test_shard_consistency(tmp_path: pathlib.Path):
assert_project_root()
peer_api_uris, peer_dirs, bootstrap_uri = start_cluster(tmp_path, N_PEERS)
create_collection_with_custom_sharding(peer_api_uris[0], shard_number=N_SHARDS, replication_factor=N_REPLICAS)
wait_collection_exists_and_active_on_all_peers(collection_name=COLLECTION_NAME, peer_api_uris=peer_api_uris)
# Create shards
create_shard(
peer_api_uris[0],
COLLECTION_NAME,
shard_key="cats",
shard_number=1,
replication_factor=1
)
create_shard(
peer_api_uris[0],
COLLECTION_NAME,
shard_key="dogs",
shard_number=1,
replication_factor=1
)
# Insert data
# Create points in first peer's collection
r = requests.put(
f"{peer_api_uris[0]}/collections/{COLLECTION_NAME}/points?wait=true", json={
"shard_key": "cats",
"points": [
{"id": 1, "vector": [0.29, 0.81, 0.75, 0.11], "payload": {"name": "Barsik"}},
{"id": 2, "vector": [0.19, 0.11, 0.15, 0.21], "payload": {"name": "Murzik"}},
{"id": 3, "vector": [0.99, 0.81, 0.75, 0.31], "payload": {"name": "Vaska"}},
{"id": 4, "vector": [0.29, 0.01, 0.05, 0.91], "payload": {"name": "Chubais"}},
]
})
assert_http_ok(r)
r = requests.put(
f"{peer_api_uris[0]}/collections/{COLLECTION_NAME}/points?wait=true", json={
"shard_key": "dogs",
"points": [
{"id": 5, "vector": [0.29, 0.81, 0.75, 0.11], "payload": {"name": "Sharik"}},
{"id": 6, "vector": [0.19, 0.11, 0.15, 0.21], "payload": {"name": "Tuzik"}},
{"id": 7, "vector": [0.99, 0.81, 0.75, 0.31], "payload": {"name": "Bobik"}},
{"id": 8, "vector": [0.29, 0.01, 0.05, 0.91], "payload": {"name": "Muhtar"}},
]
})
assert_http_ok(r)
# Check total number of points
r = requests.post(
f"{peer_api_uris[0]}/collections/{COLLECTION_NAME}/points/count",
json={
"exact": True,
}
)
assert_http_ok(r)
assert r.json()["result"]["count"] == 8
# Search points within the shard
r = requests.post(
f"{peer_api_uris[0]}/collections/{COLLECTION_NAME}/points/search",
json={
"vector": [0.29, 0.81, 0.75, 0.11],
"shard_key": "cats",
"limit": 10,
"with_payload": True,
}
)
assert_http_ok(r)
result = r.json()["result"]
assert len(result) == 4
for point in result:
assert point["payload"]["name"] in ["Barsik", "Murzik", "Vaska", "Chubais"]
assert point["shard_key"] == "cats"
# Search points within 2 shards
r = requests.post(
f"{peer_api_uris[0]}/collections/{COLLECTION_NAME}/points/search",
json={
"vector": [0.29, 0.81, 0.75, 0.11],
"shard_key": ["cats", "dogs"],
"limit": 10,
"with_payload": True,
}
)
assert_http_ok(r)
result = r.json()["result"]
assert len(result) == 8
for point in result:
assert point["shard_key"] in ["cats", "dogs"]
# Search across all the shards
r = requests.post(
f"{peer_api_uris[0]}/collections/{COLLECTION_NAME}/points/search",
json={
"vector": [0.29, 0.81, 0.75, 0.11],
"limit": 10,
}
)
assert_http_ok(r)
result = r.json()["result"]
assert len(result) == 8
for point in result:
assert point["shard_key"] in ["cats", "dogs"]
delete_shard(
peer_api_uris[0],
COLLECTION_NAME,
shard_key="cats",
)
create_shard(
peer_api_uris[0],
COLLECTION_NAME,
shard_key="birds",
shard_number=1,
replication_factor=1
)
r = requests.put(
f"{peer_api_uris[0]}/collections/{COLLECTION_NAME}/points?wait=true", json={
"shard_key": "birds",
"points": [
{"id": 9, "vector": [0.29, 0.81, 0.75, 0.11], "payload": {"name": "Kesha"}},
{"id": 10, "vector": [0.19, 0.11, 0.15, 0.21], "payload": {"name": "Gosha"}},
]
})
assert_http_ok(r)
# Search across all the shards
r = requests.post(
f"{peer_api_uris[0]}/collections/{COLLECTION_NAME}/points/search",
json={
"vector": [0.29, 0.81, 0.75, 0.11],
"limit": 10,
}
)
assert_http_ok(r)
result = r.json()["result"]
assert len(result) == 6
for point in result:
assert point["shard_key"] in ["dogs", "birds"]
|