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