-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_planner_config.ex
More file actions
275 lines (232 loc) · 8.23 KB
/
Copy pathquery_planner_config.ex
File metadata and controls
275 lines (232 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# SPDX-License-Identifier: MPL-2.0
# hypatia: allow code_safety/elixir_send_unsanitised
defmodule VeriSim.QueryPlannerConfig do
@moduledoc """
Configuration for query planner tuning.
Supports three optimization modes:
- :conservative - Worst-case estimates, prioritize correctness
- :balanced - Historical averages, good for most workloads
- :aggressive - Optimistic estimates, prioritize speed
Can be set globally or per-modality.
"""
use GenServer
@type optimization_mode :: :conservative | :balanced | :aggressive
@type config :: %{
global_mode: optimization_mode(),
modality_overrides: %{String.t() => optimization_mode()},
statistics_weight: float(), # How much to trust historical data (0.0-1.0)
enable_adaptive: boolean(), # Auto-tune based on query patterns
}
# Default configuration
@default_config %{
global_mode: :balanced,
modality_overrides: %{
# Vector searches are predictable → aggressive
"VECTOR" => :aggressive,
# Graph traversal is unpredictable → conservative
"GRAPH" => :conservative,
# Semantic ZKP verification is expensive → conservative
"SEMANTIC" => :conservative,
},
statistics_weight: 0.7, # 70% historical, 30% estimates
enable_adaptive: true,
}
# === API ===
@doc """
Get optimization mode for a specific modality.
Falls back to global mode if no override.
"""
def get_mode_for_modality(modality) do
config = get_config()
Map.get(config.modality_overrides, modality, config.global_mode)
end
@doc """
Set global optimization mode.
"""
def set_global_mode(mode) when mode in [:conservative, :balanced, :aggressive] do
GenServer.call(__MODULE__, {:set_global_mode, mode})
end
@doc """
Set optimization mode for specific modality.
"""
def set_modality_mode(modality, mode) when mode in [:conservative, :balanced, :aggressive] do
GenServer.call(__MODULE__, {:set_modality_mode, modality, mode})
end
@doc """
Enable/disable adaptive tuning.
When enabled, system auto-adjusts based on query performance.
"""
def set_adaptive(enabled) when is_boolean(enabled) do
GenServer.call(__MODULE__, {:set_adaptive, enabled})
end
# === Selectivity Multipliers ===
@doc """
Get selectivity multiplier based on optimization mode.
Conservative: Multiply by 2.0 (assume more results)
Balanced: Multiply by 1.0 (use estimate as-is)
Aggressive: Multiply by 0.5 (assume fewer results)
"""
def selectivity_multiplier(mode) do
case mode do
:conservative -> 2.0
:balanced -> 1.0
:aggressive -> 0.5
end
end
@doc """
Get cost multiplier based on optimization mode.
Conservative: Add safety buffer (1.5x)
Balanced: Use estimate as-is (1.0x)
Aggressive: Assume best case (0.8x)
"""
def cost_multiplier(mode) do
case mode do
:conservative -> 1.5
:balanced -> 1.0
:aggressive -> 0.8
end
end
# === Adaptive Tuning ===
@doc """
Record actual query performance for adaptive tuning.
If estimates were consistently wrong, adjust mode automatically.
"""
def record_performance(modality, estimated_cost, actual_cost, estimated_selectivity, actual_selectivity) do
GenServer.cast(__MODULE__, {:record_performance, %{
modality: modality,
estimated_cost: estimated_cost,
actual_cost: actual_cost,
estimated_selectivity: estimated_selectivity,
actual_selectivity: actual_selectivity,
timestamp: DateTime.utc_now(),
}})
end
# === GenServer Implementation ===
def start_link(_opts) do
GenServer.start_link(__MODULE__, @default_config, name: __MODULE__)
end
@impl true
def init(config) do
# Load from persistent storage if available
config = load_config_from_storage() || config
{:ok, %{config: config, performance_history: []}}
end
@impl true
def handle_call({:set_global_mode, mode}, _from, state) do
new_config = %{state.config | global_mode: mode}
persist_config(new_config)
{:reply, :ok, %{state | config: new_config}}
end
@impl true
def handle_call({:set_modality_mode, modality, mode}, _from, state) do
new_overrides = Map.put(state.config.modality_overrides, modality, mode)
new_config = %{state.config | modality_overrides: new_overrides}
persist_config(new_config)
{:reply, :ok, %{state | config: new_config}}
end
@impl true
def handle_call({:set_adaptive, enabled}, _from, state) do
new_config = %{state.config | enable_adaptive: enabled}
persist_config(new_config)
{:reply, :ok, %{state | config: new_config}}
end
@impl true
def handle_call(:get_config, _from, state) do
{:reply, state.config, state}
end
@impl true
def handle_cast({:record_performance, perf}, state) do
new_history = [perf | state.performance_history] |> Enum.take(1000) # Keep last 1000
# Adaptive tuning: adjust modes if estimates are consistently wrong
new_state = if state.config.enable_adaptive do
maybe_auto_tune(%{state | performance_history: new_history})
else
%{state | performance_history: new_history}
end
{:noreply, new_state}
end
# === Private Helpers ===
defp get_config do
GenServer.call(__MODULE__, :get_config)
end
defp maybe_auto_tune(state) do
# Analyze last 50 queries for each modality
recent = Enum.take(state.performance_history, 50)
state.config.modality_overrides
|> Enum.reduce(state, fn {modality, current_mode}, acc ->
modality_perfs = Enum.filter(recent, &(&1.modality == modality))
if length(modality_perfs) >= 10 do
# Calculate average error
avg_cost_error = calculate_average_error(modality_perfs, :cost)
avg_selectivity_error = calculate_average_error(modality_perfs, :selectivity)
# Adjust mode based on error patterns
new_mode = case {avg_cost_error, avg_selectivity_error} do
# Consistently underestimating → more conservative
{error, _} when error < -0.3 -> shift_mode(current_mode, :more_conservative)
# Consistently overestimating → more aggressive
{error, _} when error > 0.3 -> shift_mode(current_mode, :more_aggressive)
# Good estimates → keep current
_ -> current_mode
end
if new_mode != current_mode do
Logger.info("Adaptive tuning: #{modality} mode changed from #{current_mode} to #{new_mode}")
update_modality_mode(acc, modality, new_mode)
else
acc
end
else
acc
end
end)
end
defp calculate_average_error(performances, :cost) do
performances
|> Enum.map(fn p -> (p.estimated_cost - p.actual_cost) / p.actual_cost end)
|> Enum.sum()
|> Kernel./(length(performances))
end
defp calculate_average_error(performances, :selectivity) do
performances
|> Enum.map(fn p -> (p.estimated_selectivity - p.actual_selectivity) / p.actual_selectivity end)
|> Enum.sum()
|> Kernel./(length(performances))
end
defp shift_mode(:conservative, :more_conservative), do: :conservative
defp shift_mode(:conservative, :more_aggressive), do: :balanced
defp shift_mode(:balanced, :more_conservative), do: :conservative
defp shift_mode(:balanced, :more_aggressive), do: :aggressive
defp shift_mode(:aggressive, :more_conservative), do: :balanced
defp shift_mode(:aggressive, :more_aggressive), do: :aggressive
defp update_modality_mode(state, modality, new_mode) do
new_overrides = Map.put(state.config.modality_overrides, modality, new_mode)
new_config = %{state.config | modality_overrides: new_overrides}
persist_config(new_config)
%{state | config: new_config}
end
defp load_config_from_storage do
path = config_storage_path()
case File.read(path) do
{:ok, content} ->
try do
:erlang.binary_to_term(content, [:safe])
rescue
_ -> nil
end
{:error, _} -> nil
end
end
defp persist_config(config) do
path = config_storage_path()
File.mkdir_p!(Path.dirname(path))
File.write!(path, :erlang.term_to_binary(config))
:ok
rescue
e ->
require Logger
Logger.warning("Failed to persist config: #{inspect(e)}")
:ok
end
defp config_storage_path do
Path.join([System.tmp_dir!(), "verisimdb", "query_planner_config.bin"])
end
end