Skip to content

Commit 15c6c63

Browse files
author
V S Murthy Sidagam
committed
wl#12653 A reference caching service
To call a service implementation one needs to: 1. query the registry to get a reference to the service needed 2. call the service via the reference 3. call the registry to release the reference While #2 is very fast (just a function pointer call) #1 and #3 can be expensive since they'd need to interact with the registry's global structure in a read/write fashion. Hence if the above sequence is to be repeated in a quick succession it'd be beneficial to do steps #1 and #3 just once and aggregate as many #2 steps in a single sequence. This will usually mean to cache the service reference received in #1 and delay 3 for as much as possible. But since there's an active reference held to the service implementation until 3 is taken special handling is needed to make sure that: The references are released at regular intervals so changes in the registry can become effective. There is a way to mark a service implementation as "inactive" ("dying") so that until all of the active references to it are released no new ones are possible. All of the above is part of the current audit API machinery, but needs to be isolated into a separate service suite and made generally available to all services. This is what this worklog aims to implement. RB#24806
1 parent 29e7290 commit 15c6c63

29 files changed

+1856
-6
lines changed

components/libminchassis/dynamic_loader.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ bool mysql_dynamic_loader_imp::unload_do_lock_provided_services(
11361136
*/
11371137
minimal_chassis::rwlock_scoped_lock lock =
11381138
mysql_registry_imp::lock_registry_for_write();
1139-
return mysql_dynamic_loader_imp ::
1139+
return mysql_dynamic_loader_imp::
11401140
unload_do_check_provided_services_reference_count(
11411141
components_to_unload, dependency_graph, scheme_services);
11421142
}
@@ -1157,7 +1157,7 @@ bool mysql_dynamic_loader_imp::unload_do_lock_provided_services(
11571157
@retval false success
11581158
@retval true failure
11591159
*/
1160-
bool mysql_dynamic_loader_imp ::
1160+
bool mysql_dynamic_loader_imp::
11611161
unload_do_check_provided_services_reference_count(
11621162
const std::vector<mysql_component *> &components_to_unload,
11631163
const std::map<const void *, std::vector<mysql_component *>>

components/library_mysys/my_memory.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
1+
/* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License, version 2.0,
@@ -40,7 +40,6 @@
4040
} while (0)
4141
#endif /* HAVE_VALGRIND */
4242

43-
#define MY_ZEROFILL 32 /* fill array with zero */
4443
#define HEADER_SIZE 32
4544
#define MAGIC 1234
4645
#define USER_TO_HEADER(P) ((my_memory_header *)(((char *)P) - HEADER_SIZE))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
#
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License, version 2.0,
5+
# as published by the Free Software Foundation.
6+
#
7+
# This program is also distributed with certain software (including
8+
# but not limited to OpenSSL) that is licensed under separate terms,
9+
# as designated in a particular file or component or in included license
10+
# documentation. The authors of MySQL hereby grant you an additional
11+
# permission to link the program and your derivative works with the
12+
# separately licensed software that they have included with MySQL.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License, version 2.0, for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program; if not, write to the Free Software
21+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
23+
MYSQL_ADD_COMPONENT(reference_cache
24+
component.cc
25+
channel.cc
26+
cache.cc
27+
MODULE_ONLY
28+
LINK_LIBRARIES library_mysys)

components/reference_cache/cache.cc

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License, version 2.0,
5+
as published by the Free Software Foundation.
6+
7+
This program is also distributed with certain software (including
8+
but not limited to OpenSSL) that is licensed under separate terms,
9+
as designated in a particular file or component or in included license
10+
documentation. The authors of MySQL hereby grant you an additional
11+
permission to link the program and your derivative works with the
12+
separately licensed software that they have included with MySQL.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License, version 2.0, for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22+
#include "cache.h"
23+
#include <include/mysql/components/services/mysql_mutex.h>
24+
#include <cassert>
25+
#include <cstring>
26+
#include "channel.h"
27+
28+
namespace reference_caching {
29+
cache_imp *cache_imp::create(channel_imp *channel,
30+
SERVICE_TYPE(registry) * registry) {
31+
assert(channel != nullptr);
32+
return new cache_imp(channel, registry);
33+
}
34+
35+
bool cache_imp::destroy(cache_imp *cache) {
36+
delete cache;
37+
return false;
38+
}
39+
40+
bool cache_imp::get(unsigned service_name_index, const my_h_service **out_ref) {
41+
bool channel_is_valid = m_channel->is_valid();
42+
43+
if (m_cache && channel_is_valid) {
44+
// cache hit
45+
*out_ref = m_cache[service_name_index];
46+
return false;
47+
}
48+
49+
// cache miss
50+
flush();
51+
52+
m_cache = (my_h_service **)my_malloc(
53+
KEY_mem_reference_cache, m_service_names.size() * sizeof(my_h_service *),
54+
MY_ZEROFILL);
55+
56+
my_service<SERVICE_TYPE(registry_query)> query("registry_query", m_registry);
57+
58+
unsigned offset = 0;
59+
for (std::string service_name : m_service_names) {
60+
std::set<my_h_service> cache_set;
61+
62+
my_h_service_iterator iter;
63+
if (!query->create(service_name.c_str(), &iter)) {
64+
while (!query->is_valid(iter)) {
65+
const char *implementation_name;
66+
my_h_service svc;
67+
68+
// can't get the name
69+
if (query->get(iter, &implementation_name)) break;
70+
71+
// not the same service
72+
if (strncmp(implementation_name, service_name.c_str(),
73+
service_name.length()))
74+
break;
75+
76+
// not in the ignore list
77+
if (m_ignore_list.find(implementation_name) != m_ignore_list.end())
78+
continue;
79+
80+
// add the reference to the list
81+
if (!m_registry->acquire(implementation_name, &svc)) {
82+
auto res = cache_set.insert(svc);
83+
84+
/*
85+
release the unused reference if it's a duplicate of a reference
86+
already added
87+
*/
88+
if (!res.second) m_registry->release(svc);
89+
}
90+
91+
if (query->next(iter)) break;
92+
}
93+
query->release(iter);
94+
} else {
95+
// The service is not present in the registry.
96+
continue;
97+
}
98+
99+
my_h_service *cache_row = (my_h_service *)my_malloc(
100+
KEY_mem_reference_cache, (cache_set.size() + 1) * sizeof(my_h_service),
101+
MY_ZEROFILL);
102+
103+
my_h_service *cache_ptr = cache_row;
104+
for (my_h_service ref : cache_set) *cache_ptr++ = ref;
105+
106+
if (offset == service_name_index) *out_ref = cache_row;
107+
108+
m_cache[offset++] = cache_row;
109+
}
110+
return false;
111+
}
112+
113+
bool cache_imp::flush() {
114+
if (m_cache) {
115+
unsigned offset = 0;
116+
for (auto service_name : m_service_names) {
117+
my_h_service *cache_row = m_cache[offset];
118+
if (cache_row) {
119+
for (my_h_service *iter = cache_row; *iter; iter++)
120+
m_registry->release(*iter);
121+
my_free(cache_row);
122+
m_cache[offset] = nullptr;
123+
}
124+
offset++;
125+
}
126+
my_free(m_cache);
127+
m_cache = nullptr;
128+
}
129+
return false;
130+
}
131+
132+
cache_imp::cache_imp(channel_imp *channel, SERVICE_TYPE(registry) * registry)
133+
: m_channel{channel->ref()}, m_cache{nullptr}, m_registry{registry} {
134+
m_service_names = channel->get_service_names();
135+
}
136+
137+
cache_imp::~cache_imp() {
138+
flush();
139+
m_channel->unref();
140+
}
141+
} // namespace reference_caching

components/reference_cache/cache.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License, version 2.0,
5+
as published by the Free Software Foundation.
6+
7+
This program is also distributed with certain software (including
8+
but not limited to OpenSSL) that is licensed under separate terms,
9+
as designated in a particular file or component or in included license
10+
documentation. The authors of MySQL hereby grant you an additional
11+
permission to link the program and your derivative works with the
12+
separately licensed software that they have included with MySQL.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License, version 2.0, for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22+
23+
#include <mysql/components/my_service.h>
24+
#include <mysql/components/services/registry.h>
25+
#include <set>
26+
#include <string>
27+
#include <unordered_map>
28+
#include "cache_allocator.h"
29+
#include "reference_cache_common.h"
30+
31+
namespace reference_caching {
32+
33+
class channel_imp;
34+
35+
class cache_imp : public Cache_malloced {
36+
public: /* top level APIs */
37+
static cache_imp *create(channel_imp *channel,
38+
SERVICE_TYPE(registry) * registry);
39+
static bool destroy(cache_imp *cache);
40+
bool get(unsigned service_name_index, const my_h_service **ref);
41+
bool flush();
42+
43+
public: /* utility */
44+
cache_imp(channel_imp *channel, SERVICE_TYPE(registry) * registry);
45+
~cache_imp();
46+
47+
private:
48+
// disable copy constructors
49+
cache_imp(const cache_imp &);
50+
cache_imp &operator=(const cache_imp &);
51+
52+
channel_imp *m_channel;
53+
/*
54+
This is a opaque pointer handle used to store the acquired service
55+
implementaions handles.
56+
*/
57+
my_h_service **m_cache;
58+
SERVICE_TYPE(registry) * m_registry;
59+
service_names_set<> m_service_names;
60+
service_names_set<> m_ignore_list;
61+
};
62+
63+
} // namespace reference_caching
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License, version 2.0,
5+
as published by the Free Software Foundation.
6+
7+
This program is also distributed with certain software (including
8+
but not limited to OpenSSL) that is licensed under separate terms,
9+
as designated in a particular file or component or in included license
10+
documentation. The authors of MySQL hereby grant you an additional
11+
permission to link the program and your derivative works with the
12+
separately licensed software that they have included with MySQL.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License, version 2.0, for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22+
23+
#include <mysql/components/library_mysys/component_malloc_allocator.h>
24+
25+
#ifndef REFERENCE_CACHE_ALLOCATOR_H
26+
#define REFERENCE_CACHE_ALLOCATOR_H
27+
28+
namespace reference_caching {
29+
30+
class Cache_malloced {
31+
public:
32+
static void *operator new(std::size_t sz);
33+
static void operator delete(void *ptr, std::size_t sz);
34+
};
35+
36+
} // namespace reference_caching
37+
38+
#endif /* REFERENCE_CACHE_ALLOCATOR_H */

0 commit comments

Comments
 (0)