Spaces:
Build error
Build error
File size: 6,982 Bytes
d8435ba |
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
use actix_web::{post, web, Responder};
use actix_web_validator::{Json, Path, Query};
use api::rest::{QueryGroupsRequest, QueryRequest, QueryRequestBatch, QueryResponse};
use collection::operations::shard_selector_internal::ShardSelectorInternal;
use itertools::Itertools;
use storage::content_manager::collection_verification::{
check_strict_mode, check_strict_mode_batch,
};
use storage::content_manager::errors::StorageError;
use storage::dispatcher::Dispatcher;
use tokio::time::Instant;
use super::read_params::ReadParams;
use super::CollectionPath;
use crate::actix::auth::ActixAccess;
use crate::actix::helpers::{self, get_request_hardware_counter, process_response_error};
use crate::common::inference::query_requests_rest::{
convert_query_groups_request_from_rest, convert_query_request_from_rest,
};
use crate::common::points::do_query_point_groups;
use crate::settings::ServiceConfig;
#[post("/collections/{name}/points/query")]
async fn query_points(
dispatcher: web::Data<Dispatcher>,
collection: Path<CollectionPath>,
request: Json<QueryRequest>,
params: Query<ReadParams>,
service_config: web::Data<ServiceConfig>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
let QueryRequest {
internal: query_request,
shard_key,
} = request.into_inner();
let pass = match check_strict_mode(
&query_request,
params.timeout_as_secs(),
&collection.name,
&dispatcher,
&access,
)
.await
{
Ok(pass) => pass,
Err(err) => return process_response_error(err, Instant::now(), None),
};
let request_hw_counter = get_request_hardware_counter(
&dispatcher,
collection.name.clone(),
service_config.hardware_reporting(),
);
let timing = Instant::now();
let shard_selection = match shard_key {
None => ShardSelectorInternal::All,
Some(shard_keys) => shard_keys.into(),
};
let hw_measurement_acc = request_hw_counter.get_counter();
let result = async move {
let request = convert_query_request_from_rest(query_request).await?;
let points = dispatcher
.toc(&access, &pass)
.query_batch(
&collection.name,
vec![(request, shard_selection)],
params.consistency,
access,
params.timeout(),
hw_measurement_acc,
)
.await?
.pop()
.ok_or_else(|| {
StorageError::service_error("Expected at least one response for one query")
})?
.into_iter()
.map(api::rest::ScoredPoint::from)
.collect_vec();
Ok(QueryResponse { points })
}
.await;
helpers::process_response(result, timing, request_hw_counter.to_rest_api())
}
#[post("/collections/{name}/points/query/batch")]
async fn query_points_batch(
dispatcher: web::Data<Dispatcher>,
collection: Path<CollectionPath>,
request: Json<QueryRequestBatch>,
params: Query<ReadParams>,
service_config: web::Data<ServiceConfig>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
let QueryRequestBatch { searches } = request.into_inner();
let pass = match check_strict_mode_batch(
searches.iter().map(|i| &i.internal),
params.timeout_as_secs(),
&collection.name,
&dispatcher,
&access,
)
.await
{
Ok(pass) => pass,
Err(err) => return process_response_error(err, Instant::now(), None),
};
let request_hw_counter = get_request_hardware_counter(
&dispatcher,
collection.name.clone(),
service_config.hardware_reporting(),
);
let timing = Instant::now();
let hw_measurement_acc = request_hw_counter.get_counter();
let result = async move {
let mut batch = Vec::with_capacity(searches.len());
for request in searches {
let QueryRequest {
internal,
shard_key,
} = request;
let request = convert_query_request_from_rest(internal).await?;
let shard_selection = match shard_key {
None => ShardSelectorInternal::All,
Some(shard_keys) => shard_keys.into(),
};
batch.push((request, shard_selection));
}
let res = dispatcher
.toc(&access, &pass)
.query_batch(
&collection.name,
batch,
params.consistency,
access,
params.timeout(),
hw_measurement_acc,
)
.await?
.into_iter()
.map(|response| QueryResponse {
points: response
.into_iter()
.map(api::rest::ScoredPoint::from)
.collect_vec(),
})
.collect_vec();
Ok(res)
}
.await;
helpers::process_response(result, timing, request_hw_counter.to_rest_api())
}
#[post("/collections/{name}/points/query/groups")]
async fn query_points_groups(
dispatcher: web::Data<Dispatcher>,
collection: Path<CollectionPath>,
request: Json<QueryGroupsRequest>,
params: Query<ReadParams>,
service_config: web::Data<ServiceConfig>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
let QueryGroupsRequest {
search_group_request,
shard_key,
} = request.into_inner();
let pass = match check_strict_mode(
&search_group_request,
params.timeout_as_secs(),
&collection.name,
&dispatcher,
&access,
)
.await
{
Ok(pass) => pass,
Err(err) => return process_response_error(err, Instant::now(), None),
};
let request_hw_counter = get_request_hardware_counter(
&dispatcher,
collection.name.clone(),
service_config.hardware_reporting(),
);
let timing = Instant::now();
let hw_measurement_acc = request_hw_counter.get_counter();
let result = async move {
let shard_selection = match shard_key {
None => ShardSelectorInternal::All,
Some(shard_keys) => shard_keys.into(),
};
let query_group_request =
convert_query_groups_request_from_rest(search_group_request).await?;
do_query_point_groups(
dispatcher.toc(&access, &pass),
&collection.name,
query_group_request,
params.consistency,
shard_selection,
access,
params.timeout(),
hw_measurement_acc,
)
.await
}
.await;
helpers::process_response(result, timing, request_hw_counter.to_rest_api())
}
pub fn config_query_api(cfg: &mut web::ServiceConfig) {
cfg.service(query_points);
cfg.service(query_points_batch);
cfg.service(query_points_groups);
}
|