Skip to content

Commit

Permalink
wl#12653 A reference caching service
Browse files Browse the repository at this point in the history
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
  • Loading branch information
V S Murthy Sidagam committed Sep 21, 2020
1 parent 29e7290 commit 15c6c63
Show file tree
Hide file tree
Showing 29 changed files with 1,856 additions and 6 deletions.
4 changes: 2 additions & 2 deletions components/libminchassis/dynamic_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ bool mysql_dynamic_loader_imp::unload_do_lock_provided_services(
*/
minimal_chassis::rwlock_scoped_lock lock =
mysql_registry_imp::lock_registry_for_write();
return mysql_dynamic_loader_imp ::
return mysql_dynamic_loader_imp::
unload_do_check_provided_services_reference_count(
components_to_unload, dependency_graph, scheme_services);
}
Expand All @@ -1157,7 +1157,7 @@ bool mysql_dynamic_loader_imp::unload_do_lock_provided_services(
@retval false success
@retval true failure
*/
bool mysql_dynamic_loader_imp ::
bool mysql_dynamic_loader_imp::
unload_do_check_provided_services_reference_count(
const std::vector<mysql_component *> &components_to_unload,
const std::map<const void *, std::vector<mysql_component *>>
Expand Down
3 changes: 1 addition & 2 deletions components/library_mysys/my_memory.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
/* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
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,
Expand Down Expand Up @@ -40,7 +40,6 @@
} while (0)
#endif /* HAVE_VALGRIND */

#define MY_ZEROFILL 32 /* fill array with zero */
#define HEADER_SIZE 32
#define MAGIC 1234
#define USER_TO_HEADER(P) ((my_memory_header *)(((char *)P) - HEADER_SIZE))
Expand Down
28 changes: 28 additions & 0 deletions components/reference_cache/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
#
# 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 also distributed 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 included with MySQL.
#
# 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

MYSQL_ADD_COMPONENT(reference_cache
component.cc
channel.cc
cache.cc
MODULE_ONLY
LINK_LIBRARIES library_mysys)
141 changes: 141 additions & 0 deletions components/reference_cache/cache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
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 "cache.h"
#include <include/mysql/components/services/mysql_mutex.h>
#include <cassert>
#include <cstring>
#include "channel.h"

namespace reference_caching {
cache_imp *cache_imp::create(channel_imp *channel,
SERVICE_TYPE(registry) * registry) {
assert(channel != nullptr);
return new cache_imp(channel, registry);
}

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) {
bool channel_is_valid = m_channel->is_valid();

if (m_cache && channel_is_valid) {
// cache hit
*out_ref = m_cache[service_name_index];
return false;
}

// cache miss
flush();

m_cache = (my_h_service **)my_malloc(
KEY_mem_reference_cache, m_service_names.size() * sizeof(my_h_service *),
MY_ZEROFILL);

my_service<SERVICE_TYPE(registry_query)> query("registry_query", m_registry);

unsigned offset = 0;
for (std::string service_name : m_service_names) {
std::set<my_h_service> cache_set;

my_h_service_iterator iter;
if (!query->create(service_name.c_str(), &iter)) {
while (!query->is_valid(iter)) {
const char *implementation_name;
my_h_service svc;

// can't get the name
if (query->get(iter, &implementation_name)) break;

// not the same service
if (strncmp(implementation_name, service_name.c_str(),
service_name.length()))
break;

// not in the ignore list
if (m_ignore_list.find(implementation_name) != m_ignore_list.end())
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 (query->next(iter)) break;
}
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;
}
return false;
}

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;
}
return false;
}

cache_imp::cache_imp(channel_imp *channel, SERVICE_TYPE(registry) * registry)
: m_channel{channel->ref()}, m_cache{nullptr}, m_registry{registry} {
m_service_names = channel->get_service_names();
}

cache_imp::~cache_imp() {
flush();
m_channel->unref();
}
} // namespace reference_caching
63 changes: 63 additions & 0 deletions components/reference_cache/cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
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 <mysql/components/my_service.h>
#include <mysql/components/services/registry.h>
#include <set>
#include <string>
#include <unordered_map>
#include "cache_allocator.h"
#include "reference_cache_common.h"

namespace reference_caching {

class channel_imp;

class cache_imp : public Cache_malloced {
public: /* top level APIs */
static cache_imp *create(channel_imp *channel,
SERVICE_TYPE(registry) * registry);
static bool destroy(cache_imp *cache);
bool get(unsigned service_name_index, const my_h_service **ref);
bool flush();

public: /* utility */
cache_imp(channel_imp *channel, SERVICE_TYPE(registry) * registry);
~cache_imp();

private:
// disable copy constructors
cache_imp(const cache_imp &);
cache_imp &operator=(const cache_imp &);

channel_imp *m_channel;
/*
This is a opaque pointer handle used to store the acquired service
implementaions handles.
*/
my_h_service **m_cache;
SERVICE_TYPE(registry) * m_registry;
service_names_set<> m_service_names;
service_names_set<> m_ignore_list;
};

} // namespace reference_caching
38 changes: 38 additions & 0 deletions components/reference_cache/cache_allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
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 <mysql/components/library_mysys/component_malloc_allocator.h>

#ifndef REFERENCE_CACHE_ALLOCATOR_H
#define REFERENCE_CACHE_ALLOCATOR_H

namespace reference_caching {

class Cache_malloced {
public:
static void *operator new(std::size_t sz);
static void operator delete(void *ptr, std::size_t sz);
};

} // namespace reference_caching

#endif /* REFERENCE_CACHE_ALLOCATOR_H */
Loading

0 comments on commit 15c6c63

Please sign in to comment.