forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrow_cache.hh
200 lines (174 loc) · 6.53 KB
/
row_cache.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2015 Cloudius Systems
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/set.hpp>
#include "core/memory.hh"
#include <seastar/core/thread.hh>
#include "mutation_reader.hh"
#include "mutation_partition.hh"
#include "utils/logalloc.hh"
namespace scollectd {
struct registrations;
}
namespace bi = boost::intrusive;
// Intrusive set entry which holds partition data.
//
// TODO: Make memtables use this format too.
class cache_entry {
// We need auto_unlink<> option on the _cache_link because when entry is
// evicted from cache via LRU we don't have a reference to the container
// and don't want to store it with each entry. As for the _lru_link, we
// have a global LRU, so technically we could not use auto_unlink<> on
// _lru_link, but it's convenient to do so too. We may also want to have
// multiple eviction spaces in the future and thus multiple LRUs.
using lru_link_type = bi::list_member_hook<bi::link_mode<bi::auto_unlink>>;
using cache_link_type = bi::set_member_hook<bi::link_mode<bi::auto_unlink>>;
dht::decorated_key _key;
mutation_partition _p;
lru_link_type _lru_link;
cache_link_type _cache_link;
public:
friend class row_cache;
friend class cache_tracker;
cache_entry(const dht::decorated_key& key, const mutation_partition& p)
: _key(key)
, _p(p)
{ }
cache_entry(dht::decorated_key&& key, mutation_partition&& p) noexcept
: _key(std::move(key))
, _p(std::move(p))
{ }
cache_entry(cache_entry&&) noexcept;
const dht::decorated_key& key() const { return _key; }
const mutation_partition& partition() const { return _p; }
mutation_partition& partition() { return _p; }
struct compare {
dht::decorated_key::less_comparator _c;
compare(schema_ptr s)
: _c(std::move(s))
{}
bool operator()(const dht::decorated_key& k1, const cache_entry& k2) const {
return _c(k1, k2._key);
}
bool operator()(const cache_entry& k1, const cache_entry& k2) const {
return _c(k1._key, k2._key);
}
bool operator()(const cache_entry& k1, const dht::decorated_key& k2) const {
return _c(k1._key, k2);
}
};
};
// Tracks accesses and performs eviction of cache entries.
class cache_tracker final {
public:
using lru_type = bi::list<cache_entry,
bi::member_hook<cache_entry, cache_entry::lru_link_type, &cache_entry::_lru_link>,
bi::constant_time_size<false>>; // we need this to have bi::auto_unlink on hooks.
private:
uint64_t _hits = 0;
uint64_t _misses = 0;
uint64_t _insertions = 0;
uint64_t _merges = 0;
uint64_t _partitions = 0;
std::unique_ptr<scollectd::registrations> _collectd_registrations;
logalloc::region _region;
lru_type _lru;
private:
void setup_collectd();
public:
cache_tracker();
~cache_tracker();
void clear();
void touch(cache_entry&);
void insert(cache_entry&);
void on_erase();
void on_merge();
void on_hit();
void on_miss();
allocation_strategy& allocator();
logalloc::region& region();
const logalloc::region& region() const;
};
// Returns a reference to shard-wide cache_tracker.
cache_tracker& global_cache_tracker();
//
// A data source which wraps another data source such that data obtained from the underlying data source
// is cached in-memory in order to serve queries faster.
//
// To query the underlying data source through cache, use make_reader().
//
// Cache populates itself automatically during misses.
//
// Cache needs to be maintained externally so that it remains consistent with the underlying data source.
// Any incremental change to the underlying data source should result in update() being called on cache.
//
class row_cache final {
public:
using partitions_type = bi::set<cache_entry,
bi::member_hook<cache_entry, cache_entry::cache_link_type, &cache_entry::_cache_link>,
bi::constant_time_size<false>, // we need this to have bi::auto_unlink on hooks
bi::compare<cache_entry::compare>>;
friend class populating_reader;
public:
struct stats {
uint64_t hits;
uint64_t misses;
};
private:
cache_tracker& _tracker;
stats _stats{};
schema_ptr _schema;
partitions_type _partitions; // Cached partitions are complete.
mutation_source _underlying;
logalloc::allocating_section _update_section;
logalloc::allocating_section _populate_section;
logalloc::allocating_section _read_section;
mutation_reader make_scanning_reader(const query::partition_range&);
void on_hit();
void on_miss();
static thread_local seastar::thread_scheduling_group _update_thread_scheduling_group;
public:
~row_cache();
row_cache(schema_ptr, mutation_source underlying, cache_tracker&);
row_cache(row_cache&&) = default;
row_cache(const row_cache&) = delete;
row_cache& operator=(row_cache&&) = default;
public:
mutation_reader make_reader(const query::partition_range&);
const stats& stats() const { return _stats; }
public:
// Populate cache from given mutation. The mutation must contain all
// information there is for its partition in the underlying data sources.
void populate(const mutation& m);
// Synchronizes cache with the underlying data source from a memtable which
// has just been flushed to the underlying data source.
// The memtable can be queried during the process, but must not be written.
// After the update is complete, memtable is empty.
future<> update(memtable&, partition_presence_checker underlying_negative);
// Moves given partition to the front of LRU if present in cache.
void touch(const dht::decorated_key&);
auto num_entries() const {
return _partitions.size();
}
const cache_tracker& get_cache_tracker() const {
return _tracker;
}
};