Skip to content

Commit 3e868e3

Browse files
author
Liubov Didkivska
authored
Add Protect/Release partition (#1199)
Add posibility to protect/release partition by its id, which includes metadata and data handle for partition. Resolves: OAM-1024 Signed-off-by: Liubov Didkivska <[email protected]>
1 parent 331e21d commit 3e868e3

File tree

9 files changed

+325
-31
lines changed

9 files changed

+325
-31
lines changed

olp-cpp-sdk-dataservice-read/include/olp/dataservice/read/VersionedLayerClient.h

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ class DATASERVICE_READ_API VersionedLayerClient final {
409409
*
410410
* @param partition_id The partition ID that should be removed.
411411
*
412-
* @note Before calling the API, specify a layer version. You can set it using
413-
* the constructor or after the first online request.
412+
* @note Before calling the API, specify a catalog version. You can set it
413+
* using the constructor or after the first online request.
414414
*
415415
* @return True if partition data is removed successfully; false otherwise.
416416
*/
@@ -421,8 +421,8 @@ class DATASERVICE_READ_API VersionedLayerClient final {
421421
*
422422
* @param tile The tile key that should be removed.
423423
*
424-
* @note Before calling the API, specify a layer version. You can set it using
425-
* the constructor or after the first online request.
424+
* @note Before calling the API, specify a catalog version. You can set it
425+
* using the constructor or after the first online request.
426426
*
427427
* @return True if tile data is removed successfully; false otherwise.
428428
*/
@@ -433,8 +433,8 @@ class DATASERVICE_READ_API VersionedLayerClient final {
433433
*
434434
* @param partition_id The partition ID.
435435
*
436-
* @note Before calling the API, specify a layer version. You can set it using
437-
* the constructor or after the first online request.
436+
* @note Before calling the API, specify a catalog version. You can set it
437+
* using the constructor or after the first online request.
438438
*
439439
* @return True if the partition data is cached; false otherwise.
440440
*/
@@ -447,8 +447,8 @@ class DATASERVICE_READ_API VersionedLayerClient final {
447447
* @param aggregated The aggregated flag, used to specify whether the tile is
448448
* aggregated or not.
449449
*
450-
* @note Before calling the API, specify a layer version. You can set it using
451-
* the constructor or after the first online request.
450+
* @note Before calling the API, specify a catalog version. You can set it
451+
* using the constructor or after the first online request.
452452
*
453453
* @return True if the tile data is cached; false otherwise.
454454
*/
@@ -463,22 +463,45 @@ class DATASERVICE_READ_API VersionedLayerClient final {
463463
* not expire. The quadtree stays protected if at least one tile key is
464464
* protected.
465465
*
466+
* @note Before calling the API, specify a catalog version. You can set it
467+
* using the constructor or after the first online request.
468+
*
466469
* @note You can only protect tiles which data handles are present in the
467470
* cache at the time of the call.
468471
*
469472
* @note Please do not call `Protect` while the `Release` call for the same
470473
* catalog and layer is in progress.
471474
*
472-
* @note Before calling the API, specify a layer version. You can set it using
473-
* the constructor or after the first online request.
474-
*
475475
* @param tiles The list of tile keys to be protected.
476476
*
477477
* @return True if some keys were successfully added to the protected list;
478478
* false otherwise.
479479
*/
480480
bool Protect(const TileKeys& tiles);
481481

482+
/**
483+
* @brief Protect partition from eviction.
484+
*
485+
* Protecting partition means that its data and metadata keys are added to the
486+
* protected list and stored in the cache. These keys are removed from the LRU
487+
* cache, so they could not be evicted. Also, they do not expire.
488+
*
489+
* @note Before calling the API, specify a catalog version. You can set it
490+
* using the constructor or after the first online request.
491+
*
492+
* @note You can only protect partitions which data handles are present in the
493+
* cache at the time of the call.
494+
*
495+
* @note Please do not call `Protect` while the `Release` call for the same
496+
* catalog and layer is in progress.
497+
*
498+
* @param partition_id Partition id to be protected.
499+
*
500+
* @return True if partition keys were successfully added to the protected
501+
* list; false otherwise.
502+
*/
503+
bool Protect(const std::string& partition_id);
504+
482505
/**
483506
* @brief Removes a list of tiles from protection.
484507
*
@@ -487,19 +510,40 @@ class DATASERVICE_READ_API VersionedLayerClient final {
487510
* Expiration value is restored, and keys can expire. The quadtree can be
488511
* removed from the protected list if all tile keys are no longer protected.
489512
*
513+
* @note Before calling the API, specify a catalog version. You can set it
514+
* using the constructor or after the first online request.
515+
*
490516
* @note Please make sure that `Protect` will not be called for the same
491517
* catalog and layer while the `Release` call is in progress.
492518
*
493-
* @note Before calling the API, specify a layer version. You can set it using
494-
* the constructor or after the first online request.
495-
*
496519
* @param tiles The list of tile keys to be removed from protection.
497520
*
498521
* @return True if some keys were successfully removed from the protected
499522
* list; false otherwise.
500523
*/
501524
bool Release(const TileKeys& tiles);
502525

526+
/**
527+
* @brief Removes partition from protection.
528+
*
529+
* Releasing partition id removes data handle and metadata keys from the
530+
* protected list. The keys are added to the LRU cache, so they could be
531+
* evicted. Expiration value is restored, and related to partition keys can
532+
* expire.
533+
*
534+
* @note Before calling the API, specify a catalog version. You can set it
535+
* using the constructor or after the first online request.
536+
*
537+
* @note Please make sure that `Protect` will not be called for the same
538+
* catalog and layer while the `Release` call is in progress.
539+
*
540+
* @param partition_id Partition id to be removed from protection.
541+
*
542+
* @return True if keys related to partition were successfully removed from
543+
* the protected list; false otherwise.
544+
*/
545+
bool Release(const std::string& partition_id);
546+
503547
private:
504548
std::unique_ptr<VersionedLayerClientImpl> impl_;
505549
};

olp-cpp-sdk-dataservice-read/src/VersionedLayerClient.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ bool VersionedLayerClient::Release(const TileKeys& tiles) {
141141
return impl_->Release(tiles);
142142
}
143143

144+
bool VersionedLayerClient::Protect(const std::string& partition_id) {
145+
return impl_->Protect(partition_id);
146+
}
147+
148+
bool VersionedLayerClient::Release(const std::string& partition_id) {
149+
return impl_->Release(partition_id);
150+
}
151+
144152
} // namespace read
145153
} // namespace dataservice
146154
} // namespace olp

olp-cpp-sdk-dataservice-read/src/VersionedLayerClientImpl.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,42 @@ bool VersionedLayerClientImpl::Release(const TileKeys& tiles) {
791791
return settings_.cache->Release(keys_to_release);
792792
}
793793

794+
bool VersionedLayerClientImpl::Protect(const std::string& partition_id) {
795+
if (!settings_.cache) {
796+
return {};
797+
}
798+
799+
auto version = catalog_version_.load();
800+
if (version == kInvalidVersion) {
801+
OLP_SDK_LOG_WARNING(kLogTag,
802+
"Method Protect failed, version is not initialized");
803+
return {};
804+
}
805+
806+
repository::PartitionsCacheRepository repository(catalog_, layer_id_,
807+
settings_.cache);
808+
809+
return repository.Protect(partition_id, version);
810+
}
811+
812+
bool VersionedLayerClientImpl::Release(const std::string& partition_id) {
813+
if (!settings_.cache) {
814+
return {};
815+
}
816+
817+
auto version = catalog_version_.load();
818+
if (version == kInvalidVersion) {
819+
OLP_SDK_LOG_WARNING(kLogTag,
820+
"Method Release failed, version is not initialized");
821+
return {};
822+
}
823+
824+
repository::PartitionsCacheRepository repository(catalog_, layer_id_,
825+
settings_.cache);
826+
827+
return repository.Release(partition_id, version);
828+
}
829+
794830
} // namespace read
795831
} // namespace dataservice
796832
} // namespace olp

olp-cpp-sdk-dataservice-read/src/VersionedLayerClientImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ class VersionedLayerClientImpl {
109109

110110
virtual bool Release(const TileKeys& tiles);
111111

112+
virtual bool Protect(const std::string& partition_id);
113+
114+
virtual bool Release(const std::string& partition_id);
115+
112116
private:
113117
CatalogVersionResponse GetVersion(boost::optional<std::string> billing_tag,
114118
const FetchOptions& fetch_options,

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ std::string CreateKey(const std::string& hrn, const std::string& layer_id,
5050
return hrn + "::" + layer_id + "::" + partitionId +
5151
"::" + (version ? std::to_string(*version) + "::" : "") + "partition";
5252
}
53+
5354
std::string CreateKey(const std::string& hrn, const std::string& layer_id,
5455
const boost::optional<int64_t>& version) {
5556
return hrn + "::" + layer_id +
@@ -59,6 +60,11 @@ std::string CreateKey(const std::string& hrn, const int64_t catalogVersion) {
5960
return hrn + "::" + std::to_string(catalogVersion) + "::layerVersions";
6061
}
6162

63+
std::string CreateKey(const std::string& hrn, const std::string& layer_id,
64+
const std::string& datahandle) {
65+
return hrn + "::" + layer_id + "::" + datahandle + "::Data";
66+
}
67+
6268
time_t ConvertTime(std::chrono::seconds time) {
6369
return time == kChronoSecondsMax ? kTimetMax : time.count();
6470
}
@@ -327,6 +333,40 @@ bool PartitionsCacheRepository::ContainsTree(
327333
return cache_->Contains(CreateQuadKey(key, depth, version));
328334
}
329335

336+
cache::KeyValueCache::KeyListType
337+
PartitionsCacheRepository::CreatePartitionKeys(
338+
const std::string& partition_id, const boost::optional<int64_t>& version) {
339+
std::string handle;
340+
341+
if (GetPartitionHandle(partition_id, version, handle)) {
342+
return cache::KeyValueCache::KeyListType{
343+
CreateKey(catalog_, layer_id_, partition_id, version),
344+
CreateKey(catalog_, layer_id_, handle)};
345+
}
346+
347+
return {};
348+
}
349+
350+
bool PartitionsCacheRepository::Protect(
351+
const std::string& partition_id, const boost::optional<int64_t>& version) {
352+
const auto keys = CreatePartitionKeys(partition_id, version);
353+
if (keys.empty()) {
354+
return false;
355+
}
356+
357+
return cache_->Protect(keys);
358+
}
359+
360+
bool PartitionsCacheRepository::Release(
361+
const std::string& partition_id, const boost::optional<int64_t>& version) {
362+
const auto keys = CreatePartitionKeys(partition_id, version);
363+
if (keys.empty()) {
364+
return false;
365+
}
366+
367+
return cache_->Release(keys);
368+
}
369+
330370
} // namespace repository
331371
} // namespace read
332372
} // namespace dataservice

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ class PartitionsCacheRepository final {
9292
bool ContainsTree(geo::TileKey key, int32_t depth,
9393
const boost::optional<int64_t>& version) const;
9494

95+
bool Protect(const std::string& partition_id,
96+
const boost::optional<int64_t>& version);
97+
98+
bool Release(const std::string& partition_id,
99+
const boost::optional<int64_t>& version);
100+
95101
private:
102+
cache::KeyValueCache::KeyListType CreatePartitionKeys(
103+
const std::string& partition_id, const boost::optional<int64_t>& version);
104+
96105
const std::string catalog_;
97106
const std::string layer_id_;
98107
std::shared_ptr<cache::KeyValueCache> cache_;

0 commit comments

Comments
 (0)