Skip to content

Commit

Permalink
Check result of cache related operations
Browse files Browse the repository at this point in the history
Skipped as not related to the offline map data:
StreamLayerClient, ApiCacheRepository, CatalogSettings

Relates-To: OLPEDGE-2852
Signed-off-by: Rustam Gamidov <[email protected]>
  • Loading branch information
rustam-gamidov-here committed Jan 24, 2024
1 parent 6f9d80e commit 9231298
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 46 deletions.
33 changes: 26 additions & 7 deletions olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2023 HERE Europe B.V.
* Copyright (C) 2019-2024 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,16 +72,28 @@ time_t GetRemainingExpiryTime(const std::string& key,
return expiry;
}

void PurgeDiskItem(const std::string& key, olp::cache::DiskCache& disk_cache,
bool PurgeDiskItem(const std::string& key, olp::cache::DiskCache& disk_cache,
uint64_t& removed_data_size) {
bool result = true;
auto expiry_key = CreateExpiryKey(key);
uint64_t data_size = 0u;

disk_cache.Remove(key, data_size);
if (!disk_cache.Remove(key, data_size)) {
OLP_SDK_LOG_ERROR_F(kLogTag, "PurgeDiskItem failed to remove key='%s'",

Check warning on line 82 in olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp#L82

Added line #L82 was not covered by tests
key.c_str());
result = false;

Check warning on line 84 in olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp#L84

Added line #L84 was not covered by tests
}
removed_data_size += data_size;

disk_cache.Remove(expiry_key, data_size);
if (!disk_cache.Remove(expiry_key, data_size)) {
OLP_SDK_LOG_ERROR_F(kLogTag,

Check warning on line 89 in olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp#L89

Added line #L89 was not covered by tests
"PurgeDiskItem failed to remove expiry_key='%s'",
expiry_key.c_str());
result = false;

Check warning on line 92 in olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp#L92

Added line #L92 was not covered by tests
}
removed_data_size += data_size;

return result;
}

size_t StoreExpiry(const std::string& key, leveldb::WriteBatch& batch,
Expand Down Expand Up @@ -392,8 +404,11 @@ bool DefaultCacheImpl::Remove(const std::string& key) {

if (mutable_cache_) {
uint64_t removed_data_size = 0;
PurgeDiskItem(key, *mutable_cache_, removed_data_size);

if (!PurgeDiskItem(key, *mutable_cache_, removed_data_size)) {
OLP_SDK_LOG_ERROR_F(kLogTag, "Remove() failed to purge item, key='%s'",

Check warning on line 408 in olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp#L408

Added line #L408 was not covered by tests
key.c_str());
return false;

Check warning on line 410 in olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp#L410

Added line #L410 was not covered by tests
}
mutable_cache_data_size_ -= removed_data_size;
}

Expand Down Expand Up @@ -974,7 +989,11 @@ bool DefaultCacheImpl::GetFromDiskCache(const std::string& key,

// Data expired in cache -> remove, but not protected keys
uint64_t removed_data_size = 0u;
PurgeDiskItem(key, *mutable_cache_, removed_data_size);
if (!PurgeDiskItem(key, *mutable_cache_, removed_data_size)) {
OLP_SDK_LOG_ERROR_F(

Check warning on line 993 in olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp#L993

Added line #L993 was not covered by tests
kLogTag, "GetFromDiskCache failed to purge an expired item, key='%s'",
key.c_str());
}
mutable_cache_data_size_ -= removed_data_size;
RemoveKeyLru(key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2021 HERE Europe B.V.
* Copyright (C) 2019-2024 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,14 +54,14 @@ CatalogCacheRepository::CatalogCacheRepository(
std::chrono::seconds default_expiry)
: hrn_(hrn), cache_(cache), default_expiry_(ConvertTime(default_expiry)) {}

void CatalogCacheRepository::Put(const model::Catalog& catalog) {
bool CatalogCacheRepository::Put(const model::Catalog& catalog) {
const std::string hrn(hrn_.ToCatalogHRNString());
const auto key = cache::KeyGenerator::CreateCatalogKey(hrn);
OLP_SDK_LOG_DEBUG_F(kLogTag, "Put -> '%s'", key.c_str());

cache_->Put(key, catalog,
[&]() { return olp::serializer::serialize(catalog); },
default_expiry_);
return cache_->Put(key, catalog,
[&]() { return olp::serializer::serialize(catalog); },
default_expiry_);
}

boost::optional<model::Catalog> CatalogCacheRepository::Get() {
Expand All @@ -80,14 +80,14 @@ boost::optional<model::Catalog> CatalogCacheRepository::Get() {
return boost::any_cast<model::Catalog>(cached_catalog);
}

void CatalogCacheRepository::PutVersion(const model::VersionResponse& version) {
bool CatalogCacheRepository::PutVersion(const model::VersionResponse& version) {
const std::string hrn(hrn_.ToCatalogHRNString());
const auto key = cache::KeyGenerator::CreateLatestVersionKey(hrn);
OLP_SDK_LOG_DEBUG_F(kLogTag, "PutVersion -> '%s'", key.c_str());

cache_->Put(key, version,
[&]() { return olp::serializer::serialize(version); },
default_expiry_);
return cache_->Put(key, version,
[&]() { return olp::serializer::serialize(version); },
default_expiry_);
}

boost::optional<model::VersionResponse> CatalogCacheRepository::GetVersion() {
Expand All @@ -105,12 +105,12 @@ boost::optional<model::VersionResponse> CatalogCacheRepository::GetVersion() {
return boost::any_cast<model::VersionResponse>(cached_version);
}

void CatalogCacheRepository::Clear() {
bool CatalogCacheRepository::Clear() {
const std::string hrn(hrn_.ToCatalogHRNString());
const auto key = cache::KeyGenerator::CreateCatalogKey(hrn);
OLP_SDK_LOG_INFO_F(kLogTag, "Clear -> '%s'", key.c_str());

cache_->RemoveKeysWithPrefix(hrn);
return cache_->RemoveKeysWithPrefix(hrn);
}

} // namespace repository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2021 HERE Europe B.V.
* Copyright (C) 2019-2024 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,15 +43,15 @@ class CatalogCacheRepository final {

~CatalogCacheRepository() = default;

void Put(const model::Catalog& catalog);
bool Put(const model::Catalog& catalog);

boost::optional<model::Catalog> Get();

void PutVersion(const model::VersionResponse& version);
bool PutVersion(const model::VersionResponse& version);

boost::optional<model::VersionResponse> GetVersion();

void Clear();
bool Clear();

private:
client::HRN hrn_;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2022 HERE Europe B.V.
* Copyright (C) 2019-2024 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -92,7 +92,12 @@ CatalogResponse CatalogRepository::GetCatalog(
config_client, catalog_str, request.GetBillingTag(), context);

if (catalog_response.IsSuccessful() && fetch_options != OnlineOnly) {
repository.Put(catalog_response.GetResult());
if (!repository.Put(catalog_response.GetResult())) {
OLP_SDK_LOG_WARNING_F(

Check warning on line 96 in olp-cpp-sdk-dataservice-read/src/repositories/CatalogRepository.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-dataservice-read/src/repositories/CatalogRepository.cpp#L96

Added line #L96 was not covered by tests
kLogTag,
"GetCatalog failed to cache received results, hrn='%s',key='%s'",
catalog_str.c_str(), request_key.c_str());
}
}
if (!catalog_response.IsSuccessful()) {
const auto& error = catalog_response.GetError();
Expand Down Expand Up @@ -163,7 +168,13 @@ CatalogVersionResponse CatalogRepository::GetLatestVersion(
// Write or update the version in cache, updating happens only when the new
// version is greater than cached.
if (!cached_version || (*cached_version).GetVersion() < new_version) {
repository.PutVersion(version_response.GetResult());
if (!repository.PutVersion(version_response.GetResult())) {
OLP_SDK_LOG_WARNING_F(kLogTag,
"GetLatestVersion failed to cache latest "
"version, hrn='%s', version=%" PRId64,
catalog_.ToCatalogHRNString().c_str(),
new_version);
}
if (fetch_option == CacheOnly) {
OLP_SDK_LOG_DEBUG_F(
kLogTag, "Latest user set version, hrn='%s', version=%" PRId64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ boost::optional<model::Partitions> PartitionsCacheRepository::Get(
return partitions;
}

void PartitionsCacheRepository::Put(
bool PartitionsCacheRepository::Put(
int64_t catalog_version, const model::LayerVersions& layer_versions) {
const auto key =
cache::KeyGenerator::CreateLayerVersionsKey(catalog_, catalog_version);
OLP_SDK_LOG_DEBUG_F(kLogTag, "Put -> '%s'", key.c_str());

cache_->Put(key, layer_versions,
[&]() { return serializer::serialize(layer_versions); },
default_expiry_);
return cache_->Put(key, layer_versions,
[&]() { return serializer::serialize(layer_versions); },
default_expiry_);
}

boost::optional<model::LayerVersions> PartitionsCacheRepository::Get(
Expand Down Expand Up @@ -237,25 +237,30 @@ bool PartitionsCacheRepository::Get(geo::TileKey tile_key, int32_t depth,
return false;
}

void PartitionsCacheRepository::Clear() {
bool PartitionsCacheRepository::Clear() {
auto key = catalog_ + "::" + layer_id_ + "::";
OLP_SDK_LOG_INFO_F(kLogTag, "Clear -> '%s'", key.c_str());
cache_->RemoveKeysWithPrefix(key);
return cache_->RemoveKeysWithPrefix(key);
}

void PartitionsCacheRepository::ClearPartitions(
bool PartitionsCacheRepository::ClearPartitions(
const std::vector<std::string>& partition_ids,
const boost::optional<int64_t>& version) {
OLP_SDK_LOG_INFO_F(kLogTag, "ClearPartitions -> '%s'", catalog_.c_str());
auto cached_partitions = Get(partition_ids, version);
bool suceeded = true;

// Partitions not processed here are not cached to begin with.
for (const auto& partition : cached_partitions.GetPartitions()) {
cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
"::" + partition.GetDataHandle());
cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
"::" + partition.GetPartition());
suceeded = suceeded &&
cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
"::" + partition.GetDataHandle());
suceeded = suceeded &&
cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
"::" + partition.GetPartition());

Check warning on line 260 in olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.cpp#L255-L260

Added lines #L255 - L260 were not covered by tests
}

return suceeded;
}

bool PartitionsCacheRepository::ClearQuadTree(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PartitionsCacheRepository final {
const PartitionsRequest& request,
const boost::optional<int64_t>& version);

void Put(int64_t catalog_version, const model::LayerVersions& layer_versions);
bool Put(int64_t catalog_version, const model::LayerVersions& layer_versions);

boost::optional<model::LayerVersions> Get(int64_t catalog_version);

Expand All @@ -70,9 +70,9 @@ class PartitionsCacheRepository final {
bool Get(geo::TileKey tile_key, int32_t depth,
const boost::optional<int64_t>& version, QuadTreeIndex& tree);

void Clear();
bool Clear();

void ClearPartitions(const std::vector<std::string>& partition_ids,
bool ClearPartitions(const std::vector<std::string>& partition_ids,
const boost::optional<int64_t>& version);

bool ClearQuadTree(geo::TileKey tile_key, int32_t depth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ PartitionsRepository::GetPartitionsExtendedResponse(
kLogTag,
"GetPartitions 403 received, remove from cache, hrn='%s', key='%s'",
catalog_str.c_str(), key.c_str());
cache_.Clear();
if (!cache_.Clear()) {
OLP_SDK_LOG_ERROR_F(

Check warning on line 324 in olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp#L324

Added line #L324 was not covered by tests
kLogTag, "Failed to clear data from cache, hrn='%s', key='%s'",
catalog_.ToCatalogHRNString().c_str(), key.c_str());
}
}
}

Expand Down Expand Up @@ -381,7 +385,16 @@ PartitionsResponse PartitionsRepository::GetPartitionById(
OLP_SDK_LOG_DEBUG_F(kLogTag,
"GetPartitionById put to cache, hrn='%s', key='%s'",
catalog_.ToCatalogHRNString().c_str(), key.c_str());
cache_.Put(query_response.GetResult(), version, boost::none);
const auto put_result =
cache_.Put(query_response.GetResult(), version, boost::none);
if (!put_result.IsSuccessful()) {
OLP_SDK_LOG_ERROR_F(kLogTag,
"GetPartitionById failed to write data to cache, "
"hrn='%s', key='%s', error=%s",
catalog_.ToCatalogHRNString().c_str(), key.c_str(),
put_result.GetError().GetMessage().c_str());
}

} else if (!query_response.IsSuccessful()) {
const auto& error = query_response.GetError();
if (error.GetHttpStatusCode() == http::HttpStatusCode::FORBIDDEN) {
Expand All @@ -390,7 +403,13 @@ PartitionsResponse PartitionsRepository::GetPartitionById(
"hrn='%s', key='%s'",
catalog_.ToCatalogHRNString().c_str(), key.c_str());
// Delete partitions only but not the layer
cache_.ClearPartitions(partitions, version);
if (!cache_.ClearPartitions(partitions, version)) {
OLP_SDK_LOG_ERROR_F(

Check warning on line 407 in olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp#L407

Added line #L407 was not covered by tests
kLogTag,
"GetPartitionById failed to clear partitions from cache, "
"hrn='%s', key='%s'",
catalog_.ToCatalogHRNString().c_str(), key.c_str());
}
}
}

Expand Down Expand Up @@ -507,7 +526,16 @@ QuadTreeIndexResponse PartitionsRepository::GetQuadTreeIndexForTile(
}

if (fetch_option != OnlineOnly) {
cache_.Put(root_tile_key, kAggregateQuadTreeDepth, tree, version);
const auto put_result =
cache_.Put(root_tile_key, kAggregateQuadTreeDepth, tree, version);
if (!put_result.IsSuccessful()) {
OLP_SDK_LOG_ERROR_F(kLogTag,

Check warning on line 532 in olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp#L532

Added line #L532 was not covered by tests
"GetQuadTreeIndexForTile failed to cache data, "
"hrn='%s', key='%s', error=%s",
catalog_.ToCatalogHRNString().c_str(),
root_tile_here.c_str(),
put_result.GetError().GetMessage().c_str());
}
}

return QuadTreeIndexResponse(std::move(tree),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2023 HERE Europe B.V.
* Copyright (C) 2019-2024 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -303,8 +303,15 @@ SubQuadsResponse PrefetchTilesRepository::GetVolatileSubQuads(
subtile.ToHereTile()));
}

// add to cache
cache_repository_.Put(partitions, boost::none, boost::none, false);
const auto put_result =
cache_repository_.Put(partitions, boost::none, boost::none, false);
if (!put_result.IsSuccessful()) {
OLP_SDK_LOG_ERROR_F(kLogTag,
"GetVolatileSubQuads failed to write data to cache, "
"hrn='%s', key='%s', error=%s",
catalog_str_.c_str(), tile_key.c_str(),
put_result.GetError().GetMessage().c_str());
}

return result;
}
Expand Down

0 comments on commit 9231298

Please sign in to comment.