Skip to content

Commit 53d1685

Browse files
Check result of cache related operations (#1466)
Skipped as not related to the offline map data: StreamLayerClient, ApiCacheRepository, CatalogSettings Relates-To: OLPEDGE-2852 Signed-off-by: Rustam Gamidov <ext-rustam.gamidov@here.com>
1 parent f5e6cb5 commit 53d1685

12 files changed

+554
-65
lines changed

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,16 +72,28 @@ time_t GetRemainingExpiryTime(const std::string& key,
7272
return expiry;
7373
}
7474

75-
void PurgeDiskItem(const std::string& key, olp::cache::DiskCache& disk_cache,
75+
bool PurgeDiskItem(const std::string& key, olp::cache::DiskCache& disk_cache,
7676
uint64_t& removed_data_size) {
77+
bool result = true;
7778
auto expiry_key = CreateExpiryKey(key);
7879
uint64_t data_size = 0u;
7980

80-
disk_cache.Remove(key, data_size);
81+
if (!disk_cache.Remove(key, data_size)) {
82+
OLP_SDK_LOG_ERROR_F(kLogTag, "PurgeDiskItem failed to remove key='%s'",
83+
key.c_str());
84+
result = false;
85+
}
8186
removed_data_size += data_size;
8287

83-
disk_cache.Remove(expiry_key, data_size);
88+
if (!disk_cache.Remove(expiry_key, data_size)) {
89+
OLP_SDK_LOG_ERROR_F(kLogTag,
90+
"PurgeDiskItem failed to remove expiry_key='%s'",
91+
expiry_key.c_str());
92+
result = false;
93+
}
8494
removed_data_size += data_size;
95+
96+
return result;
8597
}
8698

8799
size_t StoreExpiry(const std::string& key, leveldb::WriteBatch& batch,
@@ -392,9 +404,14 @@ bool DefaultCacheImpl::Remove(const std::string& key) {
392404

393405
if (mutable_cache_) {
394406
uint64_t removed_data_size = 0;
395-
PurgeDiskItem(key, *mutable_cache_, removed_data_size);
396-
407+
bool purge_passed = PurgeDiskItem(key, *mutable_cache_, removed_data_size);
397408
mutable_cache_data_size_ -= removed_data_size;
409+
410+
if (!purge_passed) {
411+
OLP_SDK_LOG_ERROR_F(kLogTag, "Remove() failed to purge item, key='%s'",
412+
key.c_str());
413+
return false;
414+
}
398415
}
399416

400417
return true;
@@ -974,7 +991,11 @@ bool DefaultCacheImpl::GetFromDiskCache(const std::string& key,
974991

975992
// Data expired in cache -> remove, but not protected keys
976993
uint64_t removed_data_size = 0u;
977-
PurgeDiskItem(key, *mutable_cache_, removed_data_size);
994+
if (!PurgeDiskItem(key, *mutable_cache_, removed_data_size)) {
995+
OLP_SDK_LOG_ERROR_F(
996+
kLogTag, "GetFromDiskCache failed to purge an expired item, key='%s'",
997+
key.c_str());
998+
}
978999
mutable_cache_data_size_ -= removed_data_size;
9791000
RemoveKeyLru(key);
9801001
}

olp-cpp-sdk-core/src/cache/DiskCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@ bool DiskCache::Contains(const std::string& key) {
364364
}
365365

366366
bool DiskCache::Remove(const std::string& key, uint64_t& removed_data_size) {
367+
removed_data_size = 0u;
367368
if (!database_) {
368369
OLP_SDK_LOG_ERROR(kLogTag, "Remove: Database is not initialized");
369-
removed_data_size = 0;
370370
return false;
371371
}
372372

olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,14 +54,14 @@ CatalogCacheRepository::CatalogCacheRepository(
5454
std::chrono::seconds default_expiry)
5555
: hrn_(hrn), cache_(cache), default_expiry_(ConvertTime(default_expiry)) {}
5656

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

62-
cache_->Put(key, catalog,
63-
[&]() { return olp::serializer::serialize(catalog); },
64-
default_expiry_);
62+
return cache_->Put(key, catalog,
63+
[&]() { return olp::serializer::serialize(catalog); },
64+
default_expiry_);
6565
}
6666

6767
boost::optional<model::Catalog> CatalogCacheRepository::Get() {
@@ -80,14 +80,14 @@ boost::optional<model::Catalog> CatalogCacheRepository::Get() {
8080
return boost::any_cast<model::Catalog>(cached_catalog);
8181
}
8282

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

88-
cache_->Put(key, version,
89-
[&]() { return olp::serializer::serialize(version); },
90-
default_expiry_);
88+
return cache_->Put(key, version,
89+
[&]() { return olp::serializer::serialize(version); },
90+
default_expiry_);
9191
}
9292

9393
boost::optional<model::VersionResponse> CatalogCacheRepository::GetVersion() {
@@ -105,12 +105,12 @@ boost::optional<model::VersionResponse> CatalogCacheRepository::GetVersion() {
105105
return boost::any_cast<model::VersionResponse>(cached_version);
106106
}
107107

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

113-
cache_->RemoveKeysWithPrefix(hrn);
113+
return cache_->RemoveKeysWithPrefix(hrn);
114114
}
115115

116116
} // namespace repository

olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,15 +43,15 @@ class CatalogCacheRepository final {
4343

4444
~CatalogCacheRepository() = default;
4545

46-
void Put(const model::Catalog& catalog);
46+
bool Put(const model::Catalog& catalog);
4747

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

50-
void PutVersion(const model::VersionResponse& version);
50+
bool PutVersion(const model::VersionResponse& version);
5151

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

54-
void Clear();
54+
bool Clear();
5555

5656
private:
5757
client::HRN hrn_;

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2022 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,7 +92,12 @@ CatalogResponse CatalogRepository::GetCatalog(
9292
config_client, catalog_str, request.GetBillingTag(), context);
9393

9494
if (catalog_response.IsSuccessful() && fetch_options != OnlineOnly) {
95-
repository.Put(catalog_response.GetResult());
95+
if (!repository.Put(catalog_response.GetResult())) {
96+
OLP_SDK_LOG_WARNING_F(
97+
kLogTag,
98+
"GetCatalog failed to cache received results, hrn='%s',key='%s'",
99+
catalog_str.c_str(), request_key.c_str());
100+
}
96101
}
97102
if (!catalog_response.IsSuccessful()) {
98103
const auto& error = catalog_response.GetError();
@@ -163,7 +168,13 @@ CatalogVersionResponse CatalogRepository::GetLatestVersion(
163168
// Write or update the version in cache, updating happens only when the new
164169
// version is greater than cached.
165170
if (!cached_version || (*cached_version).GetVersion() < new_version) {
166-
repository.PutVersion(version_response.GetResult());
171+
if (!repository.PutVersion(version_response.GetResult())) {
172+
OLP_SDK_LOG_WARNING_F(kLogTag,
173+
"GetLatestVersion failed to cache latest "
174+
"version, hrn='%s', version=%" PRId64,
175+
catalog_.ToCatalogHRNString().c_str(),
176+
new_version);
177+
}
167178
if (fetch_option == CacheOnly) {
168179
OLP_SDK_LOG_DEBUG_F(
169180
kLogTag, "Latest user set version, hrn='%s', version=%" PRId64,

olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ boost::optional<model::Partitions> PartitionsCacheRepository::Get(
169169
return partitions;
170170
}
171171

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

178-
cache_->Put(key, layer_versions,
179-
[&]() { return serializer::serialize(layer_versions); },
180-
default_expiry_);
178+
return cache_->Put(key, layer_versions,
179+
[&]() { return serializer::serialize(layer_versions); },
180+
default_expiry_);
181181
}
182182

183183
boost::optional<model::LayerVersions> PartitionsCacheRepository::Get(
@@ -237,25 +237,30 @@ bool PartitionsCacheRepository::Get(geo::TileKey tile_key, int32_t depth,
237237
return false;
238238
}
239239

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

246-
void PartitionsCacheRepository::ClearPartitions(
246+
bool PartitionsCacheRepository::ClearPartitions(
247247
const std::vector<std::string>& partition_ids,
248248
const boost::optional<int64_t>& version) {
249249
OLP_SDK_LOG_INFO_F(kLogTag, "ClearPartitions -> '%s'", catalog_.c_str());
250250
auto cached_partitions = Get(partition_ids, version);
251+
bool passed = true;
251252

252253
// Partitions not processed here are not cached to begin with.
253254
for (const auto& partition : cached_partitions.GetPartitions()) {
254-
cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
255-
"::" + partition.GetDataHandle());
256-
cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
257-
"::" + partition.GetPartition());
255+
passed = cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
256+
"::" + partition.GetDataHandle()) &&
257+
passed;
258+
passed = cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
259+
"::" + partition.GetPartition()) &&
260+
passed;
258261
}
262+
263+
return passed;
259264
}
260265

261266
bool PartitionsCacheRepository::ClearQuadTree(

olp-cpp-sdk-dataservice-read/src/repositories/PartitionsCacheRepository.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PartitionsCacheRepository final {
5959
const PartitionsRequest& request,
6060
const boost::optional<int64_t>& version);
6161

62-
void Put(int64_t catalog_version, const model::LayerVersions& layer_versions);
62+
bool Put(int64_t catalog_version, const model::LayerVersions& layer_versions);
6363

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

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

73-
void Clear();
73+
bool Clear();
7474

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

7878
bool ClearQuadTree(geo::TileKey tile_key, int32_t depth,

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ PartitionsRepository::GetPartitionsExtendedResponse(
320320
kLogTag,
321321
"GetPartitions 403 received, remove from cache, hrn='%s', key='%s'",
322322
catalog_str.c_str(), key.c_str());
323-
cache_.Clear();
323+
if (!cache_.Clear()) {
324+
OLP_SDK_LOG_ERROR_F(
325+
kLogTag, "Failed to clear data from cache, hrn='%s', key='%s'",
326+
catalog_.ToCatalogHRNString().c_str(), key.c_str());
327+
}
324328
}
325329
}
326330

@@ -381,7 +385,16 @@ PartitionsResponse PartitionsRepository::GetPartitionById(
381385
OLP_SDK_LOG_DEBUG_F(kLogTag,
382386
"GetPartitionById put to cache, hrn='%s', key='%s'",
383387
catalog_.ToCatalogHRNString().c_str(), key.c_str());
384-
cache_.Put(query_response.GetResult(), version, boost::none);
388+
const auto put_result =
389+
cache_.Put(query_response.GetResult(), version, boost::none);
390+
if (!put_result.IsSuccessful()) {
391+
OLP_SDK_LOG_ERROR_F(kLogTag,
392+
"GetPartitionById failed to write data to cache, "
393+
"hrn='%s', key='%s', error=%s",
394+
catalog_.ToCatalogHRNString().c_str(), key.c_str(),
395+
put_result.GetError().GetMessage().c_str());
396+
}
397+
385398
} else if (!query_response.IsSuccessful()) {
386399
const auto& error = query_response.GetError();
387400
if (error.GetHttpStatusCode() == http::HttpStatusCode::FORBIDDEN) {
@@ -390,7 +403,13 @@ PartitionsResponse PartitionsRepository::GetPartitionById(
390403
"hrn='%s', key='%s'",
391404
catalog_.ToCatalogHRNString().c_str(), key.c_str());
392405
// Delete partitions only but not the layer
393-
cache_.ClearPartitions(partitions, version);
406+
if (!cache_.ClearPartitions(partitions, version)) {
407+
OLP_SDK_LOG_ERROR_F(
408+
kLogTag,
409+
"GetPartitionById failed to clear partitions from cache, "
410+
"hrn='%s', key='%s'",
411+
catalog_.ToCatalogHRNString().c_str(), key.c_str());
412+
}
394413
}
395414
}
396415

@@ -507,7 +526,16 @@ QuadTreeIndexResponse PartitionsRepository::GetQuadTreeIndexForTile(
507526
}
508527

509528
if (fetch_option != OnlineOnly) {
510-
cache_.Put(root_tile_key, kAggregateQuadTreeDepth, tree, version);
529+
const auto put_result =
530+
cache_.Put(root_tile_key, kAggregateQuadTreeDepth, tree, version);
531+
if (!put_result.IsSuccessful()) {
532+
OLP_SDK_LOG_ERROR_F(kLogTag,
533+
"GetQuadTreeIndexForTile failed to cache data, "
534+
"hrn='%s', key='%s', error=%s",
535+
catalog_.ToCatalogHRNString().c_str(),
536+
root_tile_here.c_str(),
537+
put_result.GetError().GetMessage().c_str());
538+
}
511539
}
512540

513541
return QuadTreeIndexResponse(std::move(tree),

olp-cpp-sdk-dataservice-read/src/repositories/PrefetchTilesRepository.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -303,8 +303,15 @@ SubQuadsResponse PrefetchTilesRepository::GetVolatileSubQuads(
303303
subtile.ToHereTile()));
304304
}
305305

306-
// add to cache
307-
cache_repository_.Put(partitions, boost::none, boost::none, false);
306+
const auto put_result =
307+
cache_repository_.Put(partitions, boost::none, boost::none, false);
308+
if (!put_result.IsSuccessful()) {
309+
OLP_SDK_LOG_ERROR_F(kLogTag,
310+
"GetVolatileSubQuads failed to write data to cache, "
311+
"hrn='%s', key='%s', error=%s",
312+
catalog_str_.c_str(), tile_key.c_str(),
313+
put_result.GetError().GetMessage().c_str());
314+
}
308315

309316
return result;
310317
}

0 commit comments

Comments
 (0)