Skip to content

Commit 0f8246d

Browse files
committed
clean up unused cache entries
1 parent cac49bc commit 0f8246d

File tree

14 files changed

+151
-46
lines changed

14 files changed

+151
-46
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "schema_version.h"
2+
3+
namespace NKikimr::NOlap {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include <ydb/library/accessor/accessor.h>
4+
5+
#include <util/digest/numeric.h>
6+
7+
namespace NKikimr::NOlap {
8+
9+
class TSchemaVersionId {
10+
private:
11+
YDB_READONLY_DEF(ui64, PresetId);
12+
YDB_READONLY_DEF(ui64, Version);
13+
14+
public:
15+
struct THash {
16+
ui64 operator()(const TSchemaVersionId& object) const {
17+
return CombineHashes(object.PresetId, object.Version);
18+
}
19+
};
20+
21+
bool operator==(const TSchemaVersionId& other) const {
22+
return std::tie(PresetId, Version) == std::tie(other.PresetId, other.Version);
23+
}
24+
25+
TSchemaVersionId(const ui64 presetId, const ui64 version)
26+
: PresetId(presetId)
27+
, Version(version) {
28+
}
29+
};
30+
31+
}
32+
33+
template <>
34+
struct THash<NKikimr::NOlap::TSchemaVersionId> {
35+
inline size_t operator()(const NKikimr::NOlap::TSchemaVersionId& key) const {
36+
return CombineHashes(key.GetPresetId(), key.GetVersion());
37+
}
38+
};

ydb/core/tx/columnshard/engines/scheme/abstract/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ LIBRARY()
33
SRCS(
44
index_info.cpp
55
column_ids.cpp
6+
schema_version.cpp
67
)
78

89
PEERDIR(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "cache.h"
2+
3+
namespace NKikimr::NOlap {}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#pragma once
2+
3+
#include <util/generic/hash.h>
4+
#include <util/system/guard.h>
5+
#include <util/system/mutex.h>
6+
7+
#include <memory>
8+
9+
namespace NKikimr::NOlap {
10+
11+
template <typename TKey, typename TObject>
12+
class TObjectCache {
13+
private:
14+
THashMap<TKey, std::weak_ptr<const TObject>> Objects;
15+
mutable TMutex Mutex;
16+
17+
public:
18+
class TEntryGuard {
19+
private:
20+
TKey Key;
21+
std::shared_ptr<const TObject> Object;
22+
TObjectCache& Cache;
23+
24+
public:
25+
TEntryGuard(TKey key, const std::shared_ptr<const TObject> object, TObjectCache& cache)
26+
: Key(key)
27+
, Object(object)
28+
, Cache(cache) {
29+
}
30+
31+
const TObject* operator->() const {
32+
return Object.get();
33+
}
34+
const TObject& operator*() const {
35+
return *Object;
36+
}
37+
38+
~TEntryGuard() {
39+
Object.reset();
40+
Cache.TryFree(Key);
41+
}
42+
};
43+
44+
public:
45+
TEntryGuard Upsert(TKey key, TObject&& object) {
46+
TGuard lock(Mutex);
47+
auto* findSchema = Objects.FindPtr(key);
48+
std::shared_ptr<const TObject> cachedObject;
49+
if (findSchema) {
50+
cachedObject = findSchema->lock();
51+
}
52+
if (!cachedObject) {
53+
cachedObject = std::make_shared<const TObject>(std::move(object));
54+
Objects[key] = cachedObject;
55+
}
56+
return TEntryGuard(std::move(key), cachedObject, *this);
57+
}
58+
59+
void TryFree(const TKey& key) {
60+
TGuard lock(Mutex);
61+
auto findObject = Objects.FindPtr(key);
62+
if (findObject) {
63+
if (findObject->expired()) {
64+
Objects.erase(key);
65+
}
66+
}
67+
}
68+
};
69+
70+
} // namespace NKikimr::NOlap
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
LIBRARY()
2+
3+
SRCS(
4+
cache.cpp
5+
)
6+
7+
PEERDIR(
8+
ydb/library/actors/core
9+
)
10+
11+
YQL_LAST_ABI_VERSION()
12+
13+
END()

ydb/core/tx/columnshard/engines/scheme/objects_cache.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@
44

55
namespace NKikimr::NOlap {
66

7-
std::shared_ptr<const TIndexInfo> TSchemaObjectsCache::UpsertIndexInfo(const ui64 presetId, TIndexInfo&& indexInfo) {
7+
TSchemaObjectsCache::TSchemasCache::TEntryGuard TSchemaObjectsCache::UpsertIndexInfo(const ui64 presetId, TIndexInfo&& indexInfo) {
88
const TSchemaVersionId versionId(presetId, indexInfo.GetVersion());
9-
TGuard lock(SchemasMutex);
10-
auto* findSchema = SchemasByVersion.FindPtr(versionId);
11-
std::shared_ptr<const TIndexInfo> cachedSchema;
12-
if (findSchema) {
13-
cachedSchema = findSchema->lock();
14-
}
15-
if (!cachedSchema) {
16-
cachedSchema = std::make_shared<TIndexInfo>(std::move(indexInfo));
17-
SchemasByVersion[versionId] = cachedSchema;
18-
}
19-
return cachedSchema;
9+
return SchemasByVersion.Upsert(versionId, std::move(indexInfo));
2010
}
2111

2212
} // namespace NKikimr::NOlap

ydb/core/tx/columnshard/engines/scheme/objects_cache.h

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
#pragma once
22
#include "column_features.h"
33

4+
#include <ydb/core/tx/columnshard/engines/scheme/abstract/schema_version.h>
5+
#include <ydb/core/tx/columnshard/engines/scheme/common/cache.h>
6+
47
#include <contrib/libs/apache/arrow/cpp/src/arrow/type.h>
58
#include <library/cpp/string_utils/quote/quote.h>
69
#include <util/generic/hash.h>
710

811
namespace NKikimr::NOlap {
912

1013
class TSchemaObjectsCache {
11-
public:
12-
class TSchemaVersionId {
13-
private:
14-
YDB_READONLY_DEF(ui64, PresetId);
15-
YDB_READONLY_DEF(ui64, Version);
16-
17-
public:
18-
struct THash {
19-
ui64 operator()(const TSchemaVersionId& object) const {
20-
return CombineHashes(object.PresetId, object.Version);
21-
}
22-
};
23-
24-
bool operator==(const TSchemaVersionId& other) const {
25-
return std::tie(PresetId, Version) == std::tie(other.PresetId, other.Version);
26-
}
27-
28-
TSchemaVersionId(const ui64 presetId, const ui64 version)
29-
: PresetId(presetId)
30-
, Version(version) {
31-
}
32-
};
33-
3414
private:
3515
THashMap<TString, std::shared_ptr<arrow::Field>> Fields;
3616
mutable ui64 AcceptionFieldsCount = 0;
@@ -40,8 +20,8 @@ class TSchemaObjectsCache {
4020
mutable ui64 AcceptionFeaturesCount = 0;
4121
mutable TMutex FeaturesMutex;
4222

43-
THashMap<TSchemaVersionId, std::weak_ptr<const TIndexInfo>, TSchemaVersionId::THash> SchemasByVersion;
44-
mutable TMutex SchemasMutex;
23+
using TSchemasCache = TObjectCache<TSchemaVersionId, TIndexInfo>;
24+
TSchemasCache SchemasByVersion;
4525

4626
THashSet<TString> StringsCache;
4727
mutable TMutex StringsMutex;
@@ -102,7 +82,7 @@ class TSchemaObjectsCache {
10282
return it->second;
10383
}
10484

105-
std::shared_ptr<const TIndexInfo> UpsertIndexInfo(const ui64 presetId, TIndexInfo&& indexInfo);
85+
TSchemasCache::TEntryGuard UpsertIndexInfo(const ui64 presetId, TIndexInfo&& indexInfo);
10686
};
10787

10888
class TSchemaCachesManager {

ydb/core/tx/columnshard/engines/scheme/versions/snapshot_scheme.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "snapshot_scheme.h"
2+
#include <ydb/core/tx/columnshard/engines/scheme/abstract/schema_version.h>
23

34
namespace NKikimr::NOlap {
45

5-
TSnapshotSchema::TSnapshotSchema(const std::shared_ptr<const TIndexInfo>& indexInfo, const TSnapshot& snapshot)
6+
TSnapshotSchema::TSnapshotSchema(TObjectCache<TSchemaVersionId, TIndexInfo>::TEntryGuard&& indexInfo, const TSnapshot& snapshot)
67
: IndexInfo(std::move(indexInfo))
78
, Schema(IndexInfo->ArrowSchemaWithSpecials())
8-
, Snapshot(snapshot)
9-
{
9+
, Snapshot(snapshot) {
1010
}
1111

1212
TColumnSaver TSnapshotSchema::GetColumnSaver(const ui32 columnId) const {

ydb/core/tx/columnshard/engines/scheme/versions/snapshot_scheme.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
#include <ydb/core/tx/columnshard/engines/scheme/index_info.h>
66

7+
#include <ydb/core/tx/columnshard/engines/scheme/abstract/schema_version.h>
8+
79
namespace NKikimr::NOlap {
810

911
class TSnapshotSchema: public ISnapshotSchema {
1012
private:
11-
std::shared_ptr<const TIndexInfo> IndexInfo;
13+
TObjectCache<TSchemaVersionId, TIndexInfo>::TEntryGuard IndexInfo;
1214
std::shared_ptr<NArrow::TSchemaLite> Schema;
1315
TSnapshot Snapshot;
1416
protected:
@@ -21,7 +23,7 @@ class TSnapshotSchema: public ISnapshotSchema {
2123
;
2224
}
2325
public:
24-
TSnapshotSchema(const std::shared_ptr<const TIndexInfo>& indexInfo, const TSnapshot& snapshot);
26+
TSnapshotSchema(TObjectCache<TSchemaVersionId, TIndexInfo>::TEntryGuard&& indexInfo, const TSnapshot& snapshot);
2527

2628
virtual TColumnIdsView GetColumnIds() const override {
2729
return IndexInfo->GetColumnIds();

0 commit comments

Comments
 (0)