Skip to content

Commit 855596d

Browse files
committed
Bugfix/issue 3807 setting not apply in v8 heap limit (#3816)
* switch to function arg passed context, instead thread context * switch the default v8 heap soft limit from 100MB to 200 MB * move log to ctor. output like ``` 2024.03.01 23:52:39.428662 [ 200546 ] {0b772a75-c715-4ffc-8a9f-c980c835842c} <Information> JavaScriptAggregateFunction: udf name=test_sec_large, javascript_max_memory_bytes=23 ```
1 parent 2b89ef1 commit 855596d

File tree

10 files changed

+35
-23
lines changed

10 files changed

+35
-23
lines changed

programs/server/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ settings:
980980
part_commit_pool_size: 8 # Total shared thread pool size for building and committing parts for Stream
981981
max_idempotent_ids: 1000 # Maximum idempotent IDs to keep in memory and on disk for idempotent data ingestion
982982
_tp_internal_system_open_sesame: true # Control the access to system.* streams
983-
javascript_max_memory_bytes: 104857600 #Maximum heap size of javascript UDA/UDF in bytes, default is 100*1024*1024 bytes
983+
javascript_max_memory_bytes: 204857600 #Maximum heap size of javascript UDA/UDF in bytes, default is 100*1024*1024 bytes
984984
recovery_policy: "strict" # Recovery policy for materialized view. strict or best_effort
985985
recovery_retry_for_sn_failure: 3 # retry times for sn failure. this value only apply if the `recovery_policy` is `best_effort`
986986
max_block_size: 65409 # 65536 - (PADDING_FOR_SIMD - 1)

programs/server/embedded.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
<part_commit_pool_size>8</part_commit_pool_size>
142142
<max_idempotent_ids>1000</max_idempotent_ids>
143143
<_tp_internal_system_open_sesame>true</_tp_internal_system_open_sesame>
144-
<javascript_max_memory_bytes>104857600</javascript_max_memory_bytes>
144+
<javascript_max_memory_bytes>204857600</javascript_max_memory_bytes>
145145
</global>
146146
<stream>
147147
<default_shards>1</default_shards>

src/AggregateFunctions/AggregateFunctionFactory.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ AggregateFunctionPtr AggregateFunctionFactory::get(
7676
const DataTypes & argument_types,
7777
const Array & parameters,
7878
AggregateFunctionProperties & out_properties,
79+
ContextPtr context,
7980
bool is_changelog_input) const
8081
/// proton: ends
8182
{
@@ -98,7 +99,7 @@ AggregateFunctionPtr AggregateFunctionFactory::get(
9899
[](const auto & type) { return type->onlyNull(); });
99100

100101
AggregateFunctionPtr nested_function = getImpl(
101-
name, nested_types, nested_parameters, out_properties, has_null_arguments, is_changelog_input);
102+
name, nested_types, nested_parameters, out_properties, has_null_arguments, context, is_changelog_input);
102103

103104
// Pure window functions are not real aggregate functions. Applying
104105
// combinators doesn't make sense for them, they must handle the
@@ -109,7 +110,7 @@ AggregateFunctionPtr AggregateFunctionFactory::get(
109110
return combinator->transformAggregateFunction(nested_function, out_properties, types_without_low_cardinality, parameters);
110111
}
111112

112-
auto with_original_arguments = getImpl(name, types_without_low_cardinality, parameters, out_properties, false, is_changelog_input);
113+
auto with_original_arguments = getImpl(name, types_without_low_cardinality, parameters, out_properties, false, context, is_changelog_input);
113114

114115
if (!with_original_arguments)
115116
throw Exception("Logical error: AggregateFunctionFactory returned nullptr", ErrorCodes::LOGICAL_ERROR);
@@ -123,6 +124,7 @@ AggregateFunctionPtr AggregateFunctionFactory::getImpl(
123124
const Array & parameters,
124125
AggregateFunctionProperties & out_properties,
125126
bool has_null_arguments,
127+
ContextPtr context,
126128
bool is_changelog_input) const
127129
/// proton: ends
128130
{
@@ -202,7 +204,7 @@ AggregateFunctionPtr AggregateFunctionFactory::getImpl(
202204
}
203205

204206
/// proton: starts. Check user defined aggr function
205-
auto aggr = UserDefinedFunctionFactory::getAggregateFunction(name, argument_types, parameters, out_properties, is_changelog_input);
207+
auto aggr = UserDefinedFunctionFactory::getAggregateFunction(name, argument_types, parameters, out_properties, context, is_changelog_input);
206208
if (aggr)
207209
return aggr;
208210
/// proton: ends
@@ -225,12 +227,13 @@ AggregateFunctionPtr AggregateFunctionFactory::tryGet(
225227
const DataTypes & argument_types,
226228
const Array & parameters,
227229
AggregateFunctionProperties & out_properties,
230+
ContextPtr context,
228231
bool is_changelog_input) const
229232
/// proton: ends
230233
{
231234
return isAggregateFunctionName(name)
232235
/// proton: starts
233-
? get(name, argument_types, parameters, out_properties, is_changelog_input)
236+
? get(name, argument_types, parameters, out_properties, context, is_changelog_input)
234237
/// proton: ends
235238
: nullptr;
236239
}

src/AggregateFunctions/AggregateFunctionFactory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class AggregateFunctionFactory final : private boost::noncopyable, public IFacto
6969
const DataTypes & argument_types,
7070
const Array & parameters,
7171
AggregateFunctionProperties & out_properties,
72+
ContextPtr context = nullptr,
7273
bool is_changelog_input = false) const;
7374

7475
/// Returns nullptr if not found.
@@ -77,6 +78,7 @@ class AggregateFunctionFactory final : private boost::noncopyable, public IFacto
7778
const DataTypes & argument_types,
7879
const Array & parameters,
7980
AggregateFunctionProperties & out_properties,
81+
ContextPtr context = nullptr,
8082
bool is_changelog_input = false) const;
8183
/// proton: ends
8284

@@ -97,6 +99,7 @@ class AggregateFunctionFactory final : private boost::noncopyable, public IFacto
9799
const Array & parameters,
98100
AggregateFunctionProperties & out_properties,
99101
bool has_null_arguments,
102+
ContextPtr context,
100103
bool is_changelog_input = false) const;
101104
/// proton: ends
102105

src/AggregateFunctions/AggregateFunctionJavaScriptAdapter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ AggregateFunctionJavaScriptAdapter::AggregateFunctionJavaScriptAdapter(
283283
, is_changelog_input(is_changelog_input_)
284284
, max_v8_heap_size_in_bytes(max_v8_heap_size_in_bytes_)
285285
, blueprint(config->name, config->source)
286+
, logger(&Poco::Logger::get("JavaScriptAggregateFunction"))
286287
{
288+
LOG_INFO(logger, "udf name={}, javascript_max_memory_bytes={}", config->name, max_v8_heap_size_in_bytes);
287289
}
288290

289291
String AggregateFunctionJavaScriptAdapter::getName() const

src/AggregateFunctions/AggregateFunctionJavaScriptAdapter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class AggregateFunctionJavaScriptAdapter final : public IAggregateFunctionHelper
9191
bool is_changelog_input = false;
9292
size_t max_v8_heap_size_in_bytes;
9393
JavaScriptBlueprint blueprint;
94-
94+
Poco::Logger * logger;
9595
public:
9696
AggregateFunctionJavaScriptAdapter(
9797
JavaScriptUserDefinedFunctionConfigurationPtr config_,

src/Core/Settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ static constexpr UInt64 operator""_GiB(unsigned long long value)
845845
M(UInt64, aysnc_ingest_max_outstanding_blocks, 10000, "Max outstanding blocks to be committed during async ingestion", 0) \
846846
M(Bool, _tp_internal_system_open_sesame, true, "Control the access to system.* streams", 0) \
847847
M(Bool, is_internal, false, "Control the statistics of select query", 0) \
848-
M(UInt64, javascript_max_memory_bytes, 100 * 1024 * 1024, "Maximum heap size of javascript UDA/UDF in bytes", 0) \
848+
M(UInt64, javascript_max_memory_bytes, 200 * 1024 * 1024, "Maximum heap size of javascript UDA/UDF in bytes", 0) \
849849
M(Bool, enable_dependency_check, true, "Enable the dependency check of view/materialized view", 0) \
850850
M(RecoveryPolicy, recovery_policy, RecoveryPolicy::Strict, "Default recovery policy for materialized view when inner query failed. 'strict': always recover from checkpointed; 'best_effort': attempts to recover from checkpointed and allow skipping of some data with permanent errors;", 0) \
851851
M(UInt64, recovery_retry_for_sn_failure, 3, "Default retry times for sn failure. only apply for 'best_effort': attempts to recover from checkpointed and allow skipping of some data with permanent errors;", 0) \

src/Functions/UserDefined/UserDefinedFunctionFactory.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ extern const int UNKNOWN_FUNCTION;
3232
/// proton: ends
3333
}
3434

35+
UserDefinedFunctionFactory::UserDefinedFunctionFactory() : logger(&Poco::Logger::get("UserDefinedFunctionFactory"))
36+
{
37+
}
38+
3539
UserDefinedFunctionFactory & UserDefinedFunctionFactory::instance()
3640
{
3741
static UserDefinedFunctionFactory result;
@@ -68,6 +72,7 @@ AggregateFunctionPtr UserDefinedFunctionFactory::getAggregateFunction(
6872
const DataTypes & types,
6973
const Array & parameters,
7074
AggregateFunctionProperties & /*properties*/,
75+
ContextPtr context,
7176
bool is_changelog_input)
7277
{
7378
const auto & loader = ExternalUserDefinedFunctionsLoader::instance(nullptr);
@@ -107,18 +112,14 @@ AggregateFunctionPtr UserDefinedFunctionFactory::getAggregateFunction(
107112
size_t num_of_args = config->arguments.size();
108113
validate_arguments(types.back()->getName() == "int8" ? num_of_args : num_of_args - 1);
109114

110-
ContextPtr query_context;
111-
if (CurrentThread::isInitialized())
112-
query_context = CurrentThread::get().getQueryContext();
113-
114-
if (!query_context || !query_context->getSettingsRef().javascript_max_memory_bytes)
115+
if (!context || !context->getSettingsRef().javascript_max_memory_bytes)
115116
{
116-
LOG_ERROR(&Poco::Logger::get("UserDefinedFunctionFactory"), "query_context is invalid");
117+
LOG_ERROR(instance().getLogger(), "query_context is invalid");
117118
return nullptr;
118119
}
119120

120121
return std::make_shared<AggregateFunctionJavaScriptAdapter>(
121-
config, types, parameters, is_changelog_input, query_context->getSettingsRef().javascript_max_memory_bytes);
122+
config, types, parameters, is_changelog_input, context->getSettingsRef().javascript_max_memory_bytes);
122123
}
123124

124125
return nullptr;
@@ -160,7 +161,7 @@ FunctionOverloadResolverPtr UserDefinedFunctionFactory::tryGet(const String & fu
160161
/// proton: starts
161162
try
162163
{
163-
return get(function_name,std::move(context));
164+
return get(function_name, std::move(context));
164165
}
165166
catch (Exception &)
166167
{
@@ -196,11 +197,7 @@ std::vector<String> UserDefinedFunctionFactory::getRegisteredNames(ContextPtr co
196197

197198
/// proton: starts
198199
bool UserDefinedFunctionFactory::registerFunction(
199-
ContextPtr context,
200-
const String & function_name,
201-
Poco::JSON::Object::Ptr json_func,
202-
bool throw_if_exists,
203-
bool replace_if_exists)
200+
ContextPtr context, const String & function_name, Poco::JSON::Object::Ptr json_func, bool throw_if_exists, bool replace_if_exists)
204201
{
205202
Streaming::validateUDFName(function_name);
206203

src/Functions/UserDefined/UserDefinedFunctionFactory.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class UserDefinedFunctionFactory
3131
const DataTypes & types,
3232
const Array & parameters,
3333
AggregateFunctionProperties & properties,
34+
ContextPtr context,
3435
/// whether input of aggregation function is changelog, aggregate function does not pass _tp_delta column to UDA if it is false
3536
bool is_changelog_input = false);
3637

@@ -53,6 +54,12 @@ class UserDefinedFunctionFactory
5354
static bool has(const String & function_name, ContextPtr context);
5455

5556
static std::vector<String> getRegisteredNames(ContextPtr context);
57+
58+
Poco::Logger * getLogger() const { return logger; }
59+
60+
private:
61+
UserDefinedFunctionFactory();
62+
Poco::Logger * logger;
5663
};
5764

5865
}

src/Interpreters/ExpressionAnalyzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ AggregateFunctionPtr getAggregateFunction(
281281
/// Examples: Translate `quantile(x, 0.5)` to `quantile(0.5)(x)`
282282
tryTranslateToParametricAggregateFunction(node, types, parameters, argument_names, context);
283283
if (throw_if_empty)
284-
return AggregateFunctionFactory::instance().get(node->name, types, parameters, properties, is_changelog_input);
284+
return AggregateFunctionFactory::instance().get(node->name, types, parameters, properties, context, is_changelog_input);
285285
else
286-
return AggregateFunctionFactory::instance().tryGet(node->name, types, parameters, properties, is_changelog_input);
286+
return AggregateFunctionFactory::instance().tryGet(node->name, types, parameters, properties, context, is_changelog_input);
287287
}
288288
/// proton: ends.
289289
}

0 commit comments

Comments
 (0)