diff --git a/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp b/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp index bed764515..4a456eb58 100644 --- a/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp +++ b/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp @@ -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. @@ -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'", + key.c_str()); + result = false; + } 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, + "PurgeDiskItem failed to remove expiry_key='%s'", + expiry_key.c_str()); + result = false; + } removed_data_size += data_size; + + return result; } size_t StoreExpiry(const std::string& key, leveldb::WriteBatch& batch, @@ -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'", + key.c_str()); + return false; + } mutable_cache_data_size_ -= removed_data_size; } @@ -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( + kLogTag, "GetFromDiskCache failed to purge an expired item, key='%s'", + key.c_str()); + } mutable_cache_data_size_ -= removed_data_size; RemoveKeyLru(key); } diff --git a/olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.cpp b/olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.cpp index 51fcdda2f..d8b537e14 100644 --- a/olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.cpp +++ b/olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.cpp @@ -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. @@ -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 CatalogCacheRepository::Get() { @@ -80,14 +80,14 @@ boost::optional CatalogCacheRepository::Get() { return boost::any_cast(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 CatalogCacheRepository::GetVersion() { @@ -105,12 +105,12 @@ boost::optional CatalogCacheRepository::GetVersion() { return boost::any_cast(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 diff --git a/olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.h b/olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.h index e27b33d63..e5159d84d 100644 --- a/olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.h +++ b/olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.h @@ -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. @@ -43,15 +43,15 @@ class CatalogCacheRepository final { ~CatalogCacheRepository() = default; - void Put(const model::Catalog& catalog); + bool Put(const model::Catalog& catalog); boost::optional Get(); - void PutVersion(const model::VersionResponse& version); + bool PutVersion(const model::VersionResponse& version); boost::optional GetVersion(); - void Clear(); + bool Clear(); private: client::HRN hrn_; diff --git a/olp-cpp-sdk-dataservice-read/src/repositories/CatalogRepository.cpp b/olp-cpp-sdk-dataservice-read/src/repositories/CatalogRepository.cpp index 675e832a6..2180f75a3 100644 --- a/olp-cpp-sdk-dataservice-read/src/repositories/CatalogRepository.cpp +++ b/olp-cpp-sdk-dataservice-read/src/repositories/CatalogRepository.cpp @@ -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. @@ -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(); @@ -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, diff --git a/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.cpp b/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.cpp index b2cc6ca73..f0ca80d75 100644 --- a/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.cpp +++ b/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.cpp @@ -169,15 +169,15 @@ boost::optional 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 PartitionsCacheRepository::Get( @@ -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& partition_ids, const boost::optional& 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()); } + + return suceeded; } bool PartitionsCacheRepository::ClearQuadTree( diff --git a/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.h b/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.h index 0e8b0f4ab..297dae844 100644 --- a/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.h +++ b/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.h @@ -59,7 +59,7 @@ class PartitionsCacheRepository final { const PartitionsRequest& request, const boost::optional& 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 Get(int64_t catalog_version); @@ -70,9 +70,9 @@ class PartitionsCacheRepository final { bool Get(geo::TileKey tile_key, int32_t depth, const boost::optional& version, QuadTreeIndex& tree); - void Clear(); + bool Clear(); - void ClearPartitions(const std::vector& partition_ids, + bool ClearPartitions(const std::vector& partition_ids, const boost::optional& version); bool ClearQuadTree(geo::TileKey tile_key, int32_t depth, diff --git a/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp b/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp index 5385bd3e3..9c3a3daa5 100644 --- a/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp +++ b/olp-cpp-sdk-dataservice-read/src/repositories/PartitionsRepository.cpp @@ -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()); + } } } @@ -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) { @@ -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()); + } } } @@ -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), diff --git a/olp-cpp-sdk-dataservice-read/src/repositories/PrefetchTilesRepository.cpp b/olp-cpp-sdk-dataservice-read/src/repositories/PrefetchTilesRepository.cpp index 685dfe120..6ee9157f9 100644 --- a/olp-cpp-sdk-dataservice-read/src/repositories/PrefetchTilesRepository.cpp +++ b/olp-cpp-sdk-dataservice-read/src/repositories/PrefetchTilesRepository.cpp @@ -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. @@ -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; }