Skip to content

Commit 98939cc

Browse files
committed
[minor] Add a query statement to clear cache access record
1 parent bdfe80b commit 98939cc

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/external_file_cache_stats_recorder.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ void ExternalFileCacheStatsRecorder::UpdateExternalFileCacheWithLock() {
114114
});
115115
}
116116

117+
void ExternalFileCacheStatsRecorder::ClearCacheAccessRecord() {
118+
std::lock_guard<std::mutex> lck(mu);
119+
cache_access_record.hit_count = 0;
120+
cache_access_record.miss_count = 0;
121+
cache_access_record.partial_hit_count = 0;
122+
}
123+
117124
void ExternalFileCacheStatsRecorder::ResetExternalFileCache(ExternalFileCache &cache) {
118125
std::lock_guard<std::mutex> lck(mu);
119126
external_file_cache = &cache;

src/include/external_file_cache_stats_recorder.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class ExternalFileCacheStatsRecorder {
4141
// Get cache access records.
4242
CacheAccessRecord GetCacheAccessRecord() const;
4343

44+
// Clear cache access record.
45+
void ClearCacheAccessRecord();
46+
4447
// Set external file cache, used to invoke at extension reload where database instance has changed.
4548
// TODO(hjiang): Current approach only assumes single external file cache and only supports one dataabase instance.
4649
void ResetExternalFileCache(ExternalFileCache &cache);

src/observefs_extension.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ namespace duckdb {
2222

2323
namespace {
2424

25+
// Indicates successful query.
26+
constexpr bool SUCCESS = true;
27+
2528
// Get database instance from expression state.
2629
// Returned instance ownership lies in the given [`state`].
2730
DatabaseInstance &GetDatabaseInstance(ExpressionState &state) {
@@ -76,7 +79,6 @@ void WrapFileSystem(const DataChunk &args, ExpressionState &state, Vector &resul
7679
ObservabilityFsRefRegistry::Get().Register(observe_filesystem.get());
7780
vfs.RegisterSubSystem(std::move(observe_filesystem));
7881

79-
constexpr bool SUCCESS = true;
8082
result.Reference(Value(SUCCESS));
8183
}
8284

@@ -108,6 +110,11 @@ void ListRegisteredFileSystems(DataChunk &args, ExpressionState &state, Vector &
108110
FlatVector::SetValidity(result, ValidityMask(filesystems.size()));
109111
}
110112

113+
void ClearExternalFileCacheStatsRecord(DataChunk &args, ExpressionState &state, Vector &result) {
114+
GetExternalFileCacheStatsRecorder().ClearCacheAccessRecord();
115+
result.Reference(Value(SUCCESS));
116+
}
117+
111118
// Extract or get httpfs filesystem.
112119
unique_ptr<FileSystem> ExtractOrCreateHttpfs(FileSystem &vfs) {
113120
auto filesystems = vfs.ListSubSystems();
@@ -228,6 +235,13 @@ void LoadInternal(ExtensionLoader &loader) {
228235
/*return_type=*/LogicalTypeId::BOOLEAN, WrapFileSystem);
229236
loader.RegisterFunction(wrap_cache_filesystem_function);
230237

238+
// Register a function to clear external file cache stats record.
239+
ScalarFunction clear_external_file_cache_access_record_function("observefs_clear_external_file_cache_access_record",
240+
/*arguments=*/ {},
241+
/*return_type=*/LogicalTypeId::BOOLEAN,
242+
ClearExternalFileCacheStatsRecord);
243+
loader.RegisterFunction(clear_external_file_cache_access_record_function);
244+
231245
// Register external file cache access query function.
232246
loader.RegisterFunction(ExternalFileCacheAccessQueryFunc());
233247
}

test/sql/external_file_cache_access_record.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
require observefs
66

7-
# No results initially.
7+
# Clear and query initial cache access record.
8+
statement ok
9+
SELECT observefs_clear_external_file_cache_access_record();
10+
811
query III
912
SELECT * FROM observefs_external_file_cache_access_record();
1013
----

0 commit comments

Comments
 (0)