Skip to content

Commit 6ed2d94

Browse files
Tarun-Aroracopybara-github
authored andcommitted
Add tpu_raiden_storage_metadata_check_total counter for POSIX secondary storage lookups
Register and instrument `storage_metadata_check_total` (exported as `tpu_raiden_storage_metadata_check_total`) to count secondary storage metadata existence checks in `PosixKVCacheStoreBackend::Lookup`: - **Metric Metadata (`metrics_backend.h`)**: Register `kStorageMetadataCheckTotal` (`MetricType::kCounter`) with labels `{dp_rank, result}` where `result` is one of `cache_hit` (`kResultCacheHit`), `stat_hit` (`kResultStatHit`), or `stat_miss` (`kResultStatMiss`). Host/pod IP is not duplicated in metric labels because Prometheus/GMP target scraping attaches `instance`/`pod` automatically, and `dp_rank` is populated from `RaidenId::data_replica_idx`. - **POSIX Lookup Instrumentation (`posix_backend.{h,cc}`, `tds_backend.cc`, `kv_cache_store.cc`)**: - Propagate `effective_raiden_id` to secondary backend configs in `KVCacheStore::Create` when `effective_config.raiden_id.empty()`, and pass `absl::StrCat(config.raiden_id.data_replica_idx)` to `PosixKVCacheStoreBackend` in the `"posix"` and `"tds"` backend factory registrations (defaulting to `"0"`). - Batch shard-level metadata check counts per `PosixKVCacheStoreBackend::Lookup()` call across Phase 1 (`CachedFresh` positive-only TTL cache hits -> `cache_hit`) and Phase 2 (`ProbeExists` filesystem `stat()` hits/misses -> `stat_hit` / `stat_miss`), emitting at most three `IncrementCounter` calls per `Lookup()` when `metric_store.HasBackends()` is true and `count > 0` outside `cache_mu_`. - **Unit Tests**: Update lockstep registration tests in `metrics_api_test.cc` and `telemetry_binding_test.py`, and add unit tests in `posix_backend_test.cc` and `kv_cache_store_test.cc` covering disabled-cache `stat_hit`/`stat_miss`, enabled-cache `cache_hit` + TTL expiry + `Delete()` invalidation, and custom `dp_rank` propagation. PiperOrigin-RevId: 1013600513
1 parent 938de4f commit 6ed2d94

7 files changed

Lines changed: 274 additions & 15 deletions

File tree

‎tpu_sync/kv_cache/BUILD‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,7 @@ cc_test(
11231123
"//tpu_sync/kv_cache/global_registry:global_registry_server_lib",
11241124
"//tpu_sync/kv_cache/global_registry:test_util",
11251125
"//tpu_sync/proto:kv_cache_store_service_cc_grpc",
1126+
"//tpu_sync/telemetry:metrics_api",
11261127
"//tpu_sync/telemetry:metrics_backend",
11271128
"//tpu_sync/telemetry:mock_metrics_backend",
11281129
"@com_github_grpc_grpc//:grpc++",

‎tpu_sync/kv_cache/backends/storage/BUILD‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ cc_library(
3737
"//tpu_sync/kv_cache:kv_cache_store_backend_factory",
3838
"//tpu_sync/kv_cache:lru_cache",
3939
"//tpu_sync/kv_cache/backends:backend",
40+
"//tpu_sync/telemetry:metrics_api",
41+
"//tpu_sync/telemetry:metrics_backend",
4042
"@com_google_absl//absl/algorithm:container",
4143
"@com_google_absl//absl/base:core_headers",
4244
"@com_google_absl//absl/base:nullability",
@@ -76,6 +78,9 @@ cc_test(
7678
"//tpu_sync/kv_cache:kv_cache_store_backend_factory",
7779
"//tpu_sync/kv_cache/backends:backend",
7880
"//tpu_sync/proto:worker_service_cc_proto",
81+
"//tpu_sync/telemetry:metrics_api",
82+
"//tpu_sync/telemetry:metrics_backend",
83+
"//tpu_sync/telemetry:mock_metrics_backend",
7984
"@com_google_absl//absl/base:core_headers",
8085
"@com_google_absl//absl/container:flat_hash_map",
8186
"@com_google_absl//absl/status",

‎tpu_sync/kv_cache/backends/storage/posix_backend.cc‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
#include "tpu_sync/kv_cache/backends/backend.h"
6161
#include "tpu_sync/kv_cache/kv_cache_store_backend.h"
6262
#include "tpu_sync/kv_cache/kv_cache_store_backend_factory.h"
63+
#include "tpu_sync/telemetry/metrics_api.h"
64+
#include "tpu_sync/telemetry/metrics_backend.h"
6365

6466
namespace tpu_raiden {
6567
namespace kv_cache {
@@ -780,13 +782,14 @@ MetadataCacheOptions MetadataCacheOptions::FromPosixOptions(
780782
PosixKVCacheStoreBackend::PosixKVCacheStoreBackend(
781783
std::shared_ptr<KVBackend> storage_backend, std::string name,
782784
size_t capacity_bytes, size_t lookup_batch_size,
783-
MetadataCacheOptions cache_options)
785+
MetadataCacheOptions cache_options, std::string dp_rank)
784786
: storage_backend_(std::move(storage_backend)),
785787
name_(std::move(name)),
786788
capacity_bytes_(capacity_bytes),
787789
lookup_batch_size_(lookup_batch_size > 0 ? lookup_batch_size
788790
: kDefaultLookupBatchSize),
789791
cache_options_(cache_options),
792+
dp_rank_(dp_rank.empty() ? "0" : std::move(dp_rank)),
790793
cache_(cache_options.max_entries) {
791794
if (cache_options_.enabled()) {
792795
LOG(INFO) << "PosixKVCacheStoreBackend '" << name_
@@ -968,12 +971,19 @@ absl::StatusOr<BlockSliceList> PosixKVCacheStoreBackend::Lookup(
968971
size_t block;
969972
size_t shard;
970973
};
974+
uint64_t cache_hits = 0;
971975
std::vector<ShardRef> misses;
972976
for (size_t i = 0; i < shard_keys.size(); ++i) {
973977
for (size_t j = 0; j < shard_keys[i].size(); ++j) {
974-
if (!present[i][j]) misses.push_back({i, j});
978+
if (present[i][j]) {
979+
++cache_hits;
980+
} else {
981+
misses.push_back({i, j});
982+
}
975983
}
976984
}
985+
uint64_t stat_hits = 0;
986+
uint64_t stat_misses = 0;
977987
std::vector<const BlockKey*> confirmed;
978988
for (size_t offset = 0; offset < misses.size();
979989
offset += lookup_batch_size_) {
@@ -992,8 +1002,10 @@ absl::StatusOr<BlockSliceList> PosixKVCacheStoreBackend::Lookup(
9921002
if (exists[k]) {
9931003
present[miss.block][miss.shard] = true;
9941004
confirmed.push_back(&shard_keys[miss.block][miss.shard]);
1005+
++stat_hits;
9951006
} else {
9961007
chunk_has_absent = true;
1008+
++stat_misses;
9971009
}
9981010
}
9991011
if (options.match_mode == LookupMatchMode::kPrefix && chunk_has_absent) {
@@ -1002,6 +1014,23 @@ absl::StatusOr<BlockSliceList> PosixKVCacheStoreBackend::Lookup(
10021014
}
10031015
CacheInsert(confirmed);
10041016

1017+
telemetry::RaidenMetricStore& metric_store =
1018+
telemetry::RaidenMetricStore::GetGlobalMetricStore();
1019+
if (metric_store.HasBackends()) {
1020+
auto record = [&](absl::string_view result, uint64_t count) {
1021+
if (count == 0) return;
1022+
const telemetry::MetricLabel labels[] = {
1023+
{telemetry::metric_labels::kDpRank, dp_rank_},
1024+
{telemetry::metric_labels::kResult, result},
1025+
};
1026+
metric_store.IncrementCounter(
1027+
telemetry::metric_names::kStorageMetadataCheckTotal, labels, count);
1028+
};
1029+
record(telemetry::metric_labels::kResultCacheHit, cache_hits);
1030+
record(telemetry::metric_labels::kResultStatHit, stat_hits);
1031+
record(telemetry::metric_labels::kResultStatMiss, stat_misses);
1032+
}
1033+
10051034
// Phase 3 (merge).
10061035
auto all_present = [](const std::vector<bool>& shards) {
10071036
return !shards.empty() && absl::c_all_of(shards, [](bool shard_present) {
@@ -1127,5 +1156,6 @@ REGISTER_KV_CACHE_STORE_BACKEND(
11271156
std::move(backend), backend_name, options.capacity_bytes,
11281157
options.lookup_batch_size,
11291158
::tpu_raiden::kv_cache::backends::storage::MetadataCacheOptions::
1130-
FromPosixOptions(options));
1159+
FromPosixOptions(options),
1160+
absl::StrCat(config.raiden_id.data_replica_idx));
11311161
});

‎tpu_sync/kv_cache/backends/storage/posix_backend.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,13 @@ class PosixKVCacheStoreBackend : public KVCacheStoreBackend {
272272
std::string name = std::string(kPosixBackendName),
273273
size_t capacity_bytes = 0,
274274
size_t lookup_batch_size = kDefaultLookupBatchSize,
275-
MetadataCacheOptions cache_options = {});
275+
MetadataCacheOptions cache_options = {},
276+
std::string dp_rank = "0");
276277

277278
std::string name() const override { return name_; }
278279

280+
const std::string& dp_rank() const { return dp_rank_; }
281+
279282
size_t lookup_batch_size() const { return lookup_batch_size_; }
280283

281284
const MetadataCacheOptions& metadata_cache_options() const {
@@ -354,6 +357,7 @@ class PosixKVCacheStoreBackend : public KVCacheStoreBackend {
354357
size_t capacity_bytes_ = 0;
355358
size_t lookup_batch_size_ = 32;
356359
const MetadataCacheOptions cache_options_;
360+
const std::string dp_rank_ = "0";
357361

358362
// Never held across storage I/O or key mapping.
359363
mutable absl::Mutex cache_mu_;

‎tpu_sync/kv_cache/backends/storage/posix_backend_test.cc‎

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#include "tpu_sync/kv_cache/kv_cache_store_backend.h"
6060
#include "tpu_sync/kv_cache/kv_cache_store_backend_factory.h"
6161
#include "tpu_sync/proto/worker_service.pb.h"
62+
#include "tpu_sync/telemetry/metrics_api.h"
63+
#include "tpu_sync/telemetry/metrics_backend.h"
64+
#include "tpu_sync/telemetry/mock_metrics_backend.h"
6265

6366
namespace tpu_raiden {
6467
namespace kv_cache {
@@ -2709,6 +2712,185 @@ TEST_F(MetadataCacheTest, UnmappableHashTruncatesPrefix) {
27092712
1);
27102713
}
27112714

2715+
auto MetadataCheckLabels(absl::string_view dp_rank, absl::string_view result) {
2716+
return testing::ElementsAre(
2717+
testing::FieldsAre(telemetry::metric_labels::kDpRank,
2718+
std::string(dp_rank)),
2719+
testing::FieldsAre(telemetry::metric_labels::kResult,
2720+
std::string(result)));
2721+
}
2722+
2723+
TEST_F(MetadataCacheTest, EmitsStatHitAndStatMissWhenCacheDisabled) {
2724+
Env env = CreateEnv(MetadataCacheOptions{}, /*batch_size=*/8, /*tp_size=*/2);
2725+
env.CreateAllShards("a");
2726+
env.CreateBlock("b", /*tp_rank=*/0); // Rank 1 shard is missing.
2727+
2728+
auto mock_backend = std::make_unique<telemetry::MockMetricsBackend>();
2729+
auto* raw_backend = mock_backend.get();
2730+
EXPECT_CALL(
2731+
*raw_backend,
2732+
IncrementCounter(
2733+
telemetry::metric_names::kStorageMetadataCheckTotal,
2734+
MetadataCheckLabels("0", telemetry::metric_labels::kResultStatHit),
2735+
3));
2736+
EXPECT_CALL(
2737+
*raw_backend,
2738+
IncrementCounter(
2739+
telemetry::metric_names::kStorageMetadataCheckTotal,
2740+
MetadataCheckLabels("0", telemetry::metric_labels::kResultStatMiss),
2741+
3));
2742+
2743+
telemetry::ScopedMetricsBackendReset scoped_reset(std::move(mock_backend));
2744+
// 6 shards in one chunk: a_r0, a_r1, b_r0 exist (3 stat_hits); b_r1, c_r0,
2745+
// c_r1 are absent (3 stat_misses).
2746+
EXPECT_EQ(env.LookupCount({"a", "b", "c"}), 1);
2747+
}
2748+
2749+
TEST_F(MetadataCacheTest,
2750+
EmitsCacheHitStatHitAndStatMissWithTtlExpiryAndDelete) {
2751+
Env env =
2752+
CreateEnv(Enabled(/*max_entries=*/1000, kShortTtl), /*batch_size=*/4);
2753+
env.CreateBlock("a");
2754+
env.CreateBlock("b");
2755+
2756+
// 1. Cold lookup of {"a", "b"}: 2 stat_hit, 0 cache_hit, 0 stat_miss.
2757+
{
2758+
auto mock_backend = std::make_unique<telemetry::MockMetricsBackend>();
2759+
EXPECT_CALL(
2760+
*mock_backend,
2761+
IncrementCounter(
2762+
telemetry::metric_names::kStorageMetadataCheckTotal,
2763+
MetadataCheckLabels("0", telemetry::metric_labels::kResultStatHit),
2764+
2));
2765+
telemetry::ScopedMetricsBackendReset scoped_reset(std::move(mock_backend));
2766+
EXPECT_EQ(env.LookupCount({"a", "b"}), 2);
2767+
}
2768+
2769+
// 2. Warm lookup of {"a", "b", "c"} where "c" is missing: 2 cache_hit,
2770+
// 1 stat_miss.
2771+
{
2772+
auto mock_backend = std::make_unique<telemetry::MockMetricsBackend>();
2773+
EXPECT_CALL(
2774+
*mock_backend,
2775+
IncrementCounter(
2776+
telemetry::metric_names::kStorageMetadataCheckTotal,
2777+
MetadataCheckLabels("0", telemetry::metric_labels::kResultCacheHit),
2778+
2));
2779+
EXPECT_CALL(
2780+
*mock_backend,
2781+
IncrementCounter(
2782+
telemetry::metric_names::kStorageMetadataCheckTotal,
2783+
MetadataCheckLabels("0", telemetry::metric_labels::kResultStatMiss),
2784+
1));
2785+
telemetry::ScopedMetricsBackendReset scoped_reset(std::move(mock_backend));
2786+
EXPECT_EQ(env.LookupCount({"a", "b", "c"}), 2);
2787+
}
2788+
2789+
// 3. Create "c" on disk and lookup {"a", "b", "c"}: 2 cache_hit, 1 stat_hit.
2790+
env.CreateBlock("c");
2791+
{
2792+
auto mock_backend = std::make_unique<telemetry::MockMetricsBackend>();
2793+
EXPECT_CALL(
2794+
*mock_backend,
2795+
IncrementCounter(
2796+
telemetry::metric_names::kStorageMetadataCheckTotal,
2797+
MetadataCheckLabels("0", telemetry::metric_labels::kResultCacheHit),
2798+
2));
2799+
EXPECT_CALL(
2800+
*mock_backend,
2801+
IncrementCounter(
2802+
telemetry::metric_names::kStorageMetadataCheckTotal,
2803+
MetadataCheckLabels("0", telemetry::metric_labels::kResultStatHit),
2804+
1));
2805+
telemetry::ScopedMetricsBackendReset scoped_reset(std::move(mock_backend));
2806+
EXPECT_EQ(env.LookupCount({"a", "b", "c"}), 3);
2807+
}
2808+
2809+
// 4. Delete "b" from cache and disk, then lookup {"a", "b", "c"}: "a" and "c"
2810+
// hit in cache (2 cache_hit), "b" is re-probed on disk (1 stat_miss).
2811+
EXPECT_EQ(env.store->Delete({"b"}), 1);
2812+
env.RemoveBlock("b");
2813+
{
2814+
auto mock_backend = std::make_unique<telemetry::MockMetricsBackend>();
2815+
EXPECT_CALL(
2816+
*mock_backend,
2817+
IncrementCounter(
2818+
telemetry::metric_names::kStorageMetadataCheckTotal,
2819+
MetadataCheckLabels("0", telemetry::metric_labels::kResultCacheHit),
2820+
2));
2821+
EXPECT_CALL(
2822+
*mock_backend,
2823+
IncrementCounter(
2824+
telemetry::metric_names::kStorageMetadataCheckTotal,
2825+
MetadataCheckLabels("0", telemetry::metric_labels::kResultStatMiss),
2826+
1));
2827+
telemetry::ScopedMetricsBackendReset scoped_reset(std::move(mock_backend));
2828+
EXPECT_EQ(env.LookupCount({"a", "b", "c"}), 1);
2829+
}
2830+
2831+
// 5. After TTL expiry, "a" is re-probed on disk (1 stat_hit, 0 cache_hit).
2832+
absl::SleepFor(kShortTtl + absl::Milliseconds(50));
2833+
{
2834+
auto mock_backend = std::make_unique<telemetry::MockMetricsBackend>();
2835+
EXPECT_CALL(
2836+
*mock_backend,
2837+
IncrementCounter(
2838+
telemetry::metric_names::kStorageMetadataCheckTotal,
2839+
MetadataCheckLabels("0", telemetry::metric_labels::kResultStatHit),
2840+
1));
2841+
telemetry::ScopedMetricsBackendReset scoped_reset(std::move(mock_backend));
2842+
EXPECT_EQ(env.LookupCount({"a"}), 1);
2843+
}
2844+
}
2845+
2846+
TEST_F(MetadataCacheTest, PropagatesCustomDpRankViaConstructorAndFactory) {
2847+
auto backend = std::make_shared<CountingPosixKVBackend>(
2848+
"posix", absl::flat_hash_map<std::string, std::string>{});
2849+
auto mapper = std::make_shared<PosixPathMapper>(
2850+
scratch_dir_, "model_test",
2851+
ParallelismConfig{.tp_size = 1, .tp_rank = 0});
2852+
backend->set_mapper(mapper);
2853+
2854+
// Empty dp_rank falls back to "0".
2855+
PosixKVCacheStoreBackend default_store(backend, "posix", /*capacity=*/0,
2856+
/*lookup_batch_size=*/4,
2857+
MetadataCacheOptions{},
2858+
/*dp_rank=*/"");
2859+
EXPECT_EQ(default_store.dp_rank(), "0");
2860+
2861+
// Factory propagates config.raiden_id.data_replica_idx to dp_rank.
2862+
BackendConfig config;
2863+
config.type = "posix";
2864+
config.SetProperty("root_dir", scratch_dir_);
2865+
config.SetProperty("model_name", "model_test");
2866+
config.parallelism = {.tp_size = 1, .tp_rank = 0};
2867+
config.raiden_id = {"test_job", "0", "kv_cache", /*data_replica_idx=*/3};
2868+
2869+
TF_ASSERT_OK_AND_ASSIGN(std::shared_ptr<KVCacheStoreBackend> created,
2870+
KVCacheStoreBackendFactory::Instance().CreateBackend(
2871+
config, /*controller=*/nullptr));
2872+
auto factory_store =
2873+
std::dynamic_pointer_cast<PosixKVCacheStoreBackend>(created);
2874+
ASSERT_NE(factory_store, nullptr);
2875+
EXPECT_EQ(factory_store->dp_rank(), "3");
2876+
2877+
auto mock_backend = std::make_unique<telemetry::MockMetricsBackend>();
2878+
auto* raw_backend = mock_backend.get();
2879+
EXPECT_CALL(
2880+
*raw_backend,
2881+
IncrementCounter(
2882+
telemetry::metric_names::kStorageMetadataCheckTotal,
2883+
MetadataCheckLabels("3", telemetry::metric_labels::kResultStatMiss),
2884+
1));
2885+
EXPECT_CALL(*raw_backend,
2886+
SetGauge(telemetry::metric_names::kStorageFilesystemBytes,
2887+
testing::_, testing::_))
2888+
.Times(testing::AnyNumber());
2889+
telemetry::ScopedMetricsBackendReset scoped_reset(std::move(mock_backend));
2890+
TF_ASSERT_OK_AND_ASSIGN(BlockSliceList result,
2891+
factory_store->Lookup({"missing_block"}));
2892+
EXPECT_TRUE(result.empty());
2893+
}
27122894
} // namespace
27132895
} // namespace storage
27142896
} // namespace backends

‎tpu_sync/kv_cache/kv_cache_store.cc‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,16 @@ absl::StatusOr<std::unique_ptr<KVCacheStore>> KVCacheStore::Create(
379379
const auto& config = effective_configs[i];
380380
BackendConfig effective_config = (i == 0) ? effective_config0 : config;
381381

382-
// Apply global registry address and raiden_id to tier 0
383-
// (HostOffloadBackend).
382+
// Apply global registry address to tier 0 (HostOffloadBackend), and
383+
// propagate the store's raiden_id to every tier that did not set its own.
384384
// TODO: Revisit secondary tier registration with global registry.
385-
if (i == 0) {
386-
if (effective_config.global_registry_address.empty() &&
387-
!global_registry_address.empty()) {
388-
effective_config.global_registry_address =
389-
std::string(global_registry_address);
390-
}
391-
if (effective_config.raiden_id.empty() && !raiden_id.empty()) {
392-
effective_config.raiden_id = raiden_id;
393-
}
385+
if (i == 0 && effective_config.global_registry_address.empty() &&
386+
!global_registry_address.empty()) {
387+
effective_config.global_registry_address =
388+
std::string(global_registry_address);
389+
}
390+
if (effective_config.raiden_id.empty()) {
391+
effective_config.raiden_id = effective_raiden_id;
394392
}
395393

396394
ABSL_ASSIGN_OR_RETURN(auto backend,

0 commit comments

Comments
 (0)