一致性级别
作为分布式向量数据库,Milvus 提供了多种一致性级别,以确保每个节点或副本在读写操作期间都能访问相同的数据。目前,支持的一致性级别包括“强一致性”、“有界一致性”、“最终一致性”和“会话一致性”,其中“有界一致性”是默认的一致性级别。
概述
Milvus 是一个将存储与计算分离的系统。流式节点将所有写入操作追加到写入前日志(WAL)中,并保存不断增长的数据——即仍处于 WAL 中且尚未持久化的数据。 恢复过程将增长数据转换为历史数据(封装在 MinIO/S3 等对象存储中的已封装分段),这些数据由查询节点提供服务。 每个搜索或查询请求都会由托管该 Collection WAL 的流式处理节点上的查询委托器接收:该委托器会生成查询计划,在本地执行“增长数据”部分,将“历史数据”部分转发至相关的查询节点,并在返回结果前合并结果。详情请参阅流式服务。
为了提供一致性保证,Milvus 为每条消息分配一个时间戳预言机(TSO)时间戳,该时间戳决定了其在 WAL 中的顺序,并会持续将同步时间戳与数据消息一同插入 WAL 中。 每当接收到同步时间戳(syncTs)时,系统会将其记录为 ServiceTime,这意味着该 ServiceTime 之前的所有数据均可被查询访问。基于 ServiceTime,Milvus 提供保证时间戳(GuaranteeTs),以满足用户对一致性和可用性的不同需求。 用户可通过在搜索请求中指定 GuaranteeTs,告知委托方需要在搜索范围内包含特定时间点之前的数据。
服务时间与保证时间
如上图所示,如果 GuaranteeTs 小于 ServiceTime,则表示指定时间点之前的所有数据均已可见,允许搜索立即执行。当 GuaranteeTs 大于 ServiceTime 时,搜索必须等待直至 ServiceTime 超过 GuaranteeTs 之后才能执行。
用户需要在查询准确性和查询延迟之间进行权衡。如果用户对一致性要求较高且对查询延迟不敏感,则可将 GuaranteeTs 设置为尽可能大的值;如果用户希望快速获得搜索结果且对查询准确性的容忍度较高,则可将 GuaranteeTs 设置为较小的值。
一致性级别示例
Milvus 提供了四种具有不同 GuaranteeTs 值的一致性级别。
强一致性
使用最新的时间戳作为 GuaranteeTs,搜索必须等到 ServiceTime 满足 GuaranteeTs 后才能执行。
最终
将 GuaranteeTs 设置为极小的值(例如 1),以避免一致性检查,从而使搜索能够立即基于当前可见的数据执行。
有限过时
将 GuaranteeTs 设置为早于最新时间戳的时间点,这样搜索在执行时会容忍某些最近的数据尚未可见的情况。
会话
将客户端插入数据的最新时间点用作 GuaranteeTs,以便搜索能够覆盖客户端插入的所有数据。
Milvus 将“有限过时”作为默认的一致性级别。如果未指定 GuaranteeTs,则使用最新的 ServiceTime 作为 GuaranteeTs。
设置一致性级别
在创建Collection以及执行搜索和查询时,您可以设置不同的一致性级别。
在创建Collection时设置一致性级别
在创建Collection时,您可以为Collection内的搜索和查询设置一致性级别。以下代码示例将一致性级别设置为Bounded。
client.create_collection(
collection_name="my_collection",
schema=schema,
consistency_level="Bounded",
)
CreateCollectionReq createCollectionReq = CreateCollectionReq.builder()
.collectionName("my_collection")
.collectionSchema(schema)
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build();
client.createCollection(createCollectionReq);
err = client.CreateCollection(ctx,
milvusclient.NewCreateCollectionOption("my_collection", schema).
WithConsistencyLevel(entity.ClBounded))
if err != nil {
fmt.Println(err.Error())
// handle error
}
export schema='{
"autoId": true,
"enabledDynamicField": false,
"fields": [
{
"fieldName": "id",
"dataType": "Int64",
"isPrimary": true
},
{
"fieldName": "vector",
"dataType": "FloatVector",
"elementTypeParams": {
"dim": "5"
}
},
{
"fieldName": "my_varchar",
"dataType": "VarChar",
"isClusteringKey": true,
"elementTypeParams": {
"max_length": 512
}
}
]
}'
export params='{
"consistencyLevel": "Bounded"
}'
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d "{
\"collectionName\": \"my_collection\",
\"schema\": $schema,
\"params\": $params
}"
consistency_level 参数的可能取值为Strong 、Bounded 、Eventually 和Session 。
在搜索中设置一致性级别
您可以随时更改特定搜索的一致性级别。以下代码示例将一致性级别重置为“Bounded”。此更改仅适用于当前的搜索请求。
res = client.search(
collection_name="my_collection",
data=[query_vector],
limit=3,
search_params={"metric_type": "IP"},
consistency_level="Bounded",
)
SearchReq searchReq = SearchReq.builder()
.collectionName("my_collection")
.data(Collections.singletonList(queryVector))
.topK(3)
.searchParams(params)
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build();
SearchResp searchResp = client.search(searchReq);
resultSets, err := client.Search(ctx, milvusclient.NewSearchOption(
"my_collection", // collectionName
3, // limit
[]entity.Vector{entity.FloatVector(queryVector)},
).WithConsistencyLevel(entity.ClBounded).
WithANNSField("vector"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/search" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"data": [
[0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592]
],
"limit": 3,
"consistencyLevel": "Bounded"
}'
此参数在混合搜索和搜索迭代器中同样可用。consistency_level 参数的可能取值为Strong 、Bounded 、Eventually 和Session 。
在查询中设置一致性级别
您可以随时更改特定搜索的一致性级别。以下代码示例将一致性级别设置为“Eventually”。该设置仅适用于当前的查询请求。
res = client.query(
collection_name="my_collection",
filter="color like \"red%\"",
output_fields=["vector", "color"],
limit=3,
consistency_level="Eventually",
)
QueryReq queryReq = QueryReq.builder()
.collectionName("my_collection")
.filter("color like \"red%\"")
.outputFields(Arrays.asList("vector", "color"))
.limit(3)
.consistencyLevel(ConsistencyLevel.EVENTUALLY)
.build();
QueryResp getResp = client.query(queryReq);
resultSet, err := client.Query(ctx, milvusclient.NewQueryOption("my_collection").
WithFilter("color like \"red%\"").
WithOutputFields("vector", "color").
WithLimit(3).
WithConsistencyLevel(entity.ClEventually))
if err != nil {
fmt.Println(err.Error())
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"filter": "color like \"red_%\"",
"consistencyLevel": "Bounded",
"limit": 3
}'
该参数在查询迭代器中同样可用。consistency_level 参数的可能取值为Strong 、Bounded 、Eventually 和Session 。