-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcache.cc
More file actions
194 lines (159 loc) · 5.93 KB
/
cache.cc
File metadata and controls
194 lines (159 loc) · 5.93 KB
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
/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program 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, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <include/mysql/components/services/mysql_mutex.h>
#include <cassert>
#include <cstring>
#include "cache.h"
#include "channel.h"
#include "component.h"
namespace reference_caching {
cache_imp *cache_imp::create(channel_imp *channel,
SERVICE_TYPE(registry) * registry) {
assert(channel != nullptr);
mysql_rwlock_rdlock(&LOCK_channels);
cache_imp *retval = new cache_imp(channel, registry);
mysql_rwlock_unlock(&LOCK_channels);
return retval;
}
bool cache_imp::destroy(cache_imp *cache) {
delete cache;
return false;
}
bool cache_imp::get(unsigned service_name_index, const my_h_service **out_ref) {
const bool channel_is_valid = (m_cache_version == m_channel->version());
if (unlikely(service_name_index >= m_service_names.size())) {
*out_ref = nullptr;
return true;
}
*out_ref = nullptr;
if (m_populated && channel_is_valid) {
if (m_cache) {
// cache hit
*out_ref = m_cache[service_name_index];
}
return *out_ref ? false : true;
}
// cache miss
flush();
/*
Channel's ignore list may have been updated.
So we have to update the local copy.
Note that it is always safe to refer to m_channel
without taking LOCK_channels because reference count
for the channel was increased at the time of creating
the cache. A channel cannot be destroyed when its
reference count is > 1.
ignore_list_copy() will take read lock on the channel.
*/
m_channel->ignore_list_copy(m_ignore_list);
m_service_names = m_channel->get_service_names();
m_cache_version = m_channel->version();
bool no_op = true;
for (auto service_name : m_service_names) {
no_op &= (service_name.count_.load() == 0);
}
if (!no_op) {
m_cache = (my_h_service **)my_malloc(
KEY_mem_reference_cache,
m_service_names.size() * sizeof(my_h_service *), MY_ZEROFILL);
unsigned offset = 0;
for (auto service_name : m_service_names) {
if (service_name.count_.load() == 0) continue;
std::set<my_h_service> cache_set;
my_h_service_iterator iter;
if (!current_registry_query->create(service_name.name_.c_str(), &iter)) {
while (!current_registry_query->is_valid(iter)) {
const char *implementation_name;
my_h_service svc;
// can't get the name
if (current_registry_query->get(iter, &implementation_name)) break;
const char *dot = strchr(implementation_name, '.');
size_t service_name_length = (dot - implementation_name);
// not the same service
if ((service_name_length != service_name.name_.length()) ||
strncmp(implementation_name, service_name.name_.c_str(),
service_name.name_.length()))
break;
// not in the ignore list
if (dot != nullptr && ++dot != nullptr) {
if (m_ignore_list.find(dot) != m_ignore_list.end()) {
if (current_registry_query->next(iter)) break;
continue;
}
}
// add the reference to the list
if (!m_registry->acquire(implementation_name, &svc)) {
auto res = cache_set.insert(svc);
/*
release the unused reference if it's a duplicate of a reference
already added
*/
if (!res.second) m_registry->release(svc);
}
if (current_registry_query->next(iter)) break;
}
current_registry_query->release(iter);
} else {
// The service is not present in the registry.
continue;
}
my_h_service *cache_row = (my_h_service *)my_malloc(
KEY_mem_reference_cache,
(cache_set.size() + 1) * sizeof(my_h_service), MY_ZEROFILL);
my_h_service *cache_ptr = cache_row;
for (my_h_service ref : cache_set) *cache_ptr++ = ref;
if (offset == service_name_index) *out_ref = cache_row;
m_cache[offset++] = cache_row;
}
}
m_populated = true;
return *out_ref ? false : true;
}
bool cache_imp::flush() {
if (m_cache) {
unsigned offset = 0;
for (auto service_name : m_service_names) {
my_h_service *cache_row = m_cache[offset];
if (cache_row) {
for (my_h_service *iter = cache_row; *iter; iter++)
m_registry->release(*iter);
my_free(cache_row);
m_cache[offset] = nullptr;
}
offset++;
}
my_free(m_cache);
m_cache = nullptr;
}
m_populated = false;
return false;
}
cache_imp::cache_imp(channel_imp *channel, SERVICE_TYPE(registry) * registry)
: m_channel{channel->ref()},
m_cache{nullptr},
m_registry{registry},
m_cache_version{channel->version()},
m_populated{false} {
m_service_names = channel->get_service_names();
}
cache_imp::~cache_imp() {
flush();
m_channel->unref();
}
} // namespace reference_caching