MySQL 9.7.2
Source Code Documentation
jit_executor_component.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2026, 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 <algorithm>
30#include <chrono>
31#include <cstdint>
32#include <memory>
33#include <mutex>
34#include <optional>
35#include <string>
36#include <thread>
37#include <tuple>
38#include <unordered_map>
39#include <utility>
40#include <vector>
41
43#include "mysqlrouter/jit_executor_plugin_export.h"
46
47namespace jit_executor {
48
49static const uint64_t k_default_memory_units = 10;
51 bool operator==(const GlobalConfig &o) const {
52 return std::tie(maximum_ram_size, maximum_idle_time) ==
54 }
55
56 std::optional<uint64_t> maximum_ram_size;
57 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> memory_units;
65 std::optional<uint64_t> max_heap_size;
66};
67
68/** Interface defining central location for the handlers associated to a
69 * database service
70 */
72 public:
73 virtual ~IServiceHandlers() = default;
74 virtual std::shared_ptr<IContextHandle> get_context(
75 const std::string &debug_port = "") = 0;
76
77 virtual void release_debug_context() = 0;
78
79 virtual bool init() = 0;
80 virtual std::string error() = 0;
81 virtual void teardown() = 0;
82
83 virtual std::chrono::seconds idle_time() const = 0;
84 virtual uint64_t memory_units() const = 0;
85
86 virtual void set_max_heap_size(uint64_t) = 0;
87};
88
89/**
90 * Registry of graal contexts to be used by each service.
91 *
92 * NOTE: The original idea, was to have a pool of contexts on which the service
93 * module files would be loaded once and then shared across the contexts in the
94 * pool. The script end points would be getting a context from the pool use it
95 * and the release it. However, the main pre-requisite for that is that the
96 * context could be reset to the original state, which is NOT possible in Graal.
97 *
98 * Suggestion from the Graal Team
99 *
100 * By default, each context would internally create an engine which would hold
101 * the resources used in the context. However, it is possible to use a common
102 * engine to enable the sharing of the resources across contexts (i.e.
103 * including parsed source code). Following this approach the context pool is
104 * not needed since we would simply create/release the context on demand and it
105 * would use the shared resources from the engine.
106 *
107 * Even this is the current implementation, expectation was that the module
108 * files would be loaded only ONCE but that's not the case, they get reloaded on
109 * every created context, even the shared engine is used.
110 *
111 * This class holds a registry of service ids vs ContextHandlers (who keep the
112 * shared engine) and allows creating a context using the shared engine.
113 */
114class JIT_EXECUTOR_PLUGIN_EXPORT JitExecutorComponent {
115 public:
116 static JitExecutorComponent &get_instance();
118
120 void operator=(JitExecutorComponent const &) = delete;
121
124
125 void stop_debug_context(const std::string &service_id);
126
127 std::shared_ptr<IContextHandle> get_context(
128 const std::string &service_id, const ServiceHandlerConfig &config,
129 const std::string &debug_port = "", bool reset_context = false);
130
131 void delete_context(const std::string &service_id);
132
133 void update_global_config(const std::string &options);
134
135 private:
137 void update_active_contexts(
138 const std::pair<std::string, std::shared_ptr<IServiceHandlers>>
139 &replacement = {});
141
143
144 std::unordered_map<std::string, std::shared_ptr<IServiceHandlers>>
146
147 std::unordered_map<std::string, std::string> m_handler_errors;
148
149 std::vector<std::shared_ptr<IServiceHandlers>> m_inactive_context_handlers;
150};
151
152} // namespace jit_executor
153
154#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:71
virtual void set_max_heap_size(uint64_t)=0
virtual std::string error()=0
virtual uint64_t memory_units() const =0
virtual ~IServiceHandlers()=default
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:114
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:142
std::unordered_map< std::string, std::string > m_handler_errors
Definition: jit_executor_component.h:147
std::unordered_map< std::string, std::shared_ptr< IServiceHandlers > > m_service_context_handlers
Definition: jit_executor_component.h:145
std::mutex m_context_creation
Definition: jit_executor_component.h:140
std::vector< std::shared_ptr< IServiceHandlers > > m_inactive_context_handlers
Definition: jit_executor_component.h:149
Definition: jit_executor_callbacks.h:36
static const uint64_t k_default_memory_units
Definition: jit_executor_component.h:49
std::chrono::seconds seconds
Definition: authorize_manager.cc:70
Definition: options.cc:57
Value::Map_type_ref Dictionary_t
Definition: jit_executor_value.h:431
Definition: jit_executor_component.h:50
std::optional< uint64_t > maximum_idle_time
Definition: jit_executor_component.h:57
std::optional< uint64_t > maximum_ram_size
Definition: jit_executor_component.h:56
bool operator==(const GlobalConfig &o) const
Definition: jit_executor_component.h:51
Definition: jit_executor_component.h:60
shcore::Dictionary_t globals
Definition: jit_executor_component.h:63
std::optional< uint64_t > memory_units
Definition: jit_executor_component.h:64
std::vector< std::string > module_files
Definition: jit_executor_component.h:62
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:65