Skip to content

Commit

Permalink
Handle missing Opensearch indices
Browse files Browse the repository at this point in the history
PBENCH-1308

An Opensearch-based query like `/api/v1/datasets/{dataset}/values/iterations`
requires that the `{dataset}` have at least one `result-data-sample` indexed
document. However it doesn't actually check that this is true. This leads to
an Opensearch query with a bad URL fragment causing an Opensearch error we
report as an internal server error.

This change detects early that the dataset has no indices for the required
index root name, and fails with a meaningful message.
  • Loading branch information
dbutenhof committed Dec 8, 2023
1 parent b5bee97 commit 2f1c10a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def get_index(self, dataset: Dataset, root_index_name: str) -> str:
Raises:
APIAbort(CONFLICT) if indexing was disabled on the target dataset.
APIAbort(NOT_FOUND) if the dataset has no matching index data
Returns:
A string that joins all selected indices with ",", suitable for use
Expand All @@ -133,7 +134,13 @@ def get_index(self, dataset: Dataset, root_index_name: str) -> str:
if Metadata.getvalue(dataset, Metadata.SERVER_ARCHIVE):
raise APIAbort(HTTPStatus.CONFLICT, "Dataset indexing was disabled")

index_keys = IndexMap.indices(dataset, root_index_name)
index_keys = list(IndexMap.indices(dataset, root_index_name))

if not index_keys:
raise APIAbort(
HTTPStatus.NOT_FOUND, f"Dataset has no {root_index_name!r} data"
)

indices = ",".join(index_keys)
return indices

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from pbench.server.api.resources import ApiMethod
from pbench.server.api.resources import APIAbort, ApiMethod
from pbench.server.api.resources.query_apis.datasets.namespace_and_rows import (
SampleNamespace,
SampleValues,
Expand Down Expand Up @@ -774,7 +774,8 @@ def test_get_index(self, attach_dataset, provide_metadata):

# The "test" dataset has no index map initially; verify that we get
# no indices.
assert self.cls_obj.get_index(test, "result-data-sample") == ""
with pytest.raises(APIAbort, match="Dataset has no 'result-data-sample' data"):
self.cls_obj.get_index(test, "result-data-sample")

# Add an index that doesn't have the desired root name, and verify that
# we still don't find a match.
Expand All @@ -786,7 +787,8 @@ def test_get_index(self, attach_dataset, provide_metadata):
}
},
)
assert self.cls_obj.get_index(test, "result-data-sample") == ""
with pytest.raises(APIAbort, match="Dataset has no 'result-data-sample' data"):
self.cls_obj.get_index(test, "result-data-sample")

# Add an index with the expected root, and verify that we get the one
# matching index name.
Expand Down

0 comments on commit 2f1c10a

Please sign in to comment.