MySQL 9.3.0
Source Code Documentation
jit_executor_component.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef ROUTER_SRC_JIT_EXECUTOR_INCLUDE_MYSQLROUTER_JIT_EXECUTOR_COMPONENT_H_
27#define ROUTER_SRC_JIT_EXECUTOR_INCLUDE_MYSQLROUTER_JIT_EXECUTOR_COMPONENT_H_
28
29#include <chrono>
30#include <memory>
31#include <mutex>
32#include <optional>
33#include <string>
34#include <tuple>
35#include <unordered_map>
36#include <utility>
37#include <vector>
38
40#include "mysqlrouter/jit_executor_plugin_export.h"
43
44namespace jit_executor {
45
46static const uint64_t k_default_pool_size = 8;
47
49 bool operator==(const GlobalConfig &o) const {
53 }
54
55 std::optional<uint64_t> maximum_ram_size;
56 std::optional<uint64_t> maximum_idle_time;
58};
59
61 std::shared_ptr<shcore::polyglot::IFile_system> fs;
62 std::vector<std::string> module_files;
64 std::optional<uint64_t> pool_size;
66 std::optional<uint64_t> max_heap_size;
67};
68
69/** Interface defining central location for the handlers associated to a
70 * database service
71 */
73 public:
74 virtual ~IServiceHandlers() = default;
75 virtual std::shared_ptr<IContextHandle> get_context(
76 const std::string &debug_port = "") = 0;
77
78 virtual void release_debug_context() = 0;
79
80 virtual bool init() = 0;
81 virtual void teardown() = 0;
82
83 virtual std::chrono::seconds idle_time() const = 0;
84 virtual uint64_t pool_size() const = 0;
85
86 virtual void set_max_heap_size(uint64_t) = 0;
87 virtual void set_default_pool_size(uint64_t) = 0;
88};
89
90/**
91 * Registry of graal contexts to be used by each service.
92 *
93 * NOTE: The original idea, was to have a pool of contexts on which the service
94 * module files would be loaded once and then shared across the contexts in the
95 * pool. The script end points would be getting a context from the pool use it
96 * and the release it. However, the main pre-requisite for that is that the
97 * context could be reset to the original state, which is NOT possible in Graal.
98 *
99 * Suggestion from the Graal Team
100 *
101 * By default, each context would internally create an engine which would hold
102 * the resources used in the context. However, it is possible to use a common
103 * engine to enable the sharing of the resources across contexts (i.e.
104 * including parsed source code). Following this approach the context pool is
105 * not needed since we would simply create/release the context on demand and it
106 * would use the shared resources from the engine.
107 *
108 * Even this is the current implementation, expectation was that the module
109 * files would be loaded only ONCE but that's not the case, they get reloaded on
110 * every created context, even the shared engine is used.
111 *
112 * This class holds a registry of service ids vs ContextHandlers (who keep the
113 * shared engine) and allows creating a context using the shared engine.
114 */
115class JIT_EXECUTOR_PLUGIN_EXPORT JitExecutorComponent {
116 public:
117 static JitExecutorComponent &get_instance();
119
121 void operator=(JitExecutorComponent const &) = delete;
122
125
126 void stop_debug_context(const std::string &service_id);
127
128 std::shared_ptr<IContextHandle> get_context(
129 const std::string &service_id, const ServiceHandlerConfig &config,
130 const std::string &debug_port = "", bool reset_context = false);
131
132 void update_global_config(const std::string &options);
133
134 private:
136 void update_active_contexts(
137 const std::pair<std::string, std::shared_ptr<IServiceHandlers>>
138 &replacement = {});
140
142
143 std::unordered_map<std::string, std::shared_ptr<IServiceHandlers>>
145
146 std::vector<std::shared_ptr<IServiceHandlers>> m_inactive_context_handlers;
147};
148
149} // namespace jit_executor
150
151#endif // ROUTER_SRC_JIT_EXECUTOR_INCLUDE_MYSQLROUTER_JIT_EXECUTOR_COMPONENT_H_
Interface defining central location for the handlers associated to a database service.
Definition: jit_executor_component.h:72
virtual void set_max_heap_size(uint64_t)=0
virtual uint64_t pool_size() const =0
virtual ~IServiceHandlers()=default
virtual void set_default_pool_size(uint64_t)=0
virtual std::shared_ptr< IContextHandle > get_context(const std::string &debug_port="")=0
virtual std::chrono::seconds idle_time() const =0
virtual void release_debug_context()=0
Registry of graal contexts to be used by each service.
Definition: jit_executor_component.h:115
JitExecutorComponent(JitExecutorComponent &&)=delete
void operator=(JitExecutorComponent &&)=delete
void operator=(JitExecutorComponent const &)=delete
JitExecutorComponent(JitExecutorComponent const &)=delete
GlobalConfig m_global_config
Definition: jit_executor_component.h:141
std::unordered_map< std::string, std::shared_ptr< IServiceHandlers > > m_service_context_handlers
Definition: jit_executor_component.h:144
std::mutex m_context_creation
Definition: jit_executor_component.h:139
std::vector< std::shared_ptr< IServiceHandlers > > m_inactive_context_handlers
Definition: jit_executor_component.h:146
Definition: jit_executor_callbacks.h:36
static const uint64_t k_default_pool_size
Definition: jit_executor_component.h:46
std::chrono::seconds seconds
Definition: authorize_manager.cc:69
Definition: options.cc:57
Value::Map_type_ref Dictionary_t
Definition: jit_executor_value.h:430
Definition: jit_executor_component.h:48
uint64_t default_pool_size
Definition: jit_executor_component.h:57
std::optional< uint64_t > maximum_idle_time
Definition: jit_executor_component.h:56
std::optional< uint64_t > maximum_ram_size
Definition: jit_executor_component.h:55
bool operator==(const GlobalConfig &o) const
Definition: jit_executor_component.h:49
Definition: jit_executor_component.h:60
shcore::Dictionary_t globals
Definition: jit_executor_component.h:63
std::vector< std::string > module_files
Definition: jit_executor_component.h:62
std::optional< uint64_t > pool_size
Definition: jit_executor_component.h:64
uint64_t default_pool_size
Definition: jit_executor_component.h:65
std::shared_ptr< shcore::polyglot::IFile_system > fs
Definition: jit_executor_component.h:61
std::optional< uint64_t > max_heap_size
Definition: jit_executor_component.h:66