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 26, 2024
1 parent f5e6cb5 commit 5c6af10
Show file tree
Hide file tree
Showing 12 changed files with 554 additions and 65 deletions.
35 changes: 28 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,9 +404,14 @@ bool DefaultCacheImpl::Remove(const std::string& key) {

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

bool purge_passed = PurgeDiskItem(key, *mutable_cache_, removed_data_size);
mutable_cache_data_size_ -= removed_data_size;

if (!purge_passed) {
OLP_SDK_LOG_ERROR_F(kLogTag, "Remove() failed to purge item, key='%s'",

Check warning on line 411 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#L411

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

Check warning on line 413 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#L413

Added line #L413 was not covered by tests
}
}

return true;
Expand Down Expand Up @@ -974,7 +991,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 995 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#L995

Added line #L995 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
2 changes: 1 addition & 1 deletion olp-cpp-sdk-core/src/cache/DiskCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ bool DiskCache::Contains(const std::string& key) {
}

bool DiskCache::Remove(const std::string& key, uint64_t& removed_data_size) {
removed_data_size = 0u;
if (!database_) {
OLP_SDK_LOG_ERROR(kLogTag, "Remove: Database is not initialized");
removed_data_size = 0;
return false;
}

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(
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 passed = 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());
passed = cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
"::" + partition.GetDataHandle()) &&
passed;
passed = cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
"::" + partition.GetPartition()) &&
passed;
}

return passed;
}

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(
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(
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,
"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
Loading

0 comments on commit 5c6af10

Please sign in to comment.