From c79c657e383875c3cbbc4a9846708e1fbd77c1fb Mon Sep 17 00:00:00 2001 From: jiangpengcheng Date: Fri, 20 Dec 2024 10:18:05 +0800 Subject: [PATCH] Set MaxDirectMemorySize to 20% of limit memory --- controllers/spec/common.go | 18 +++++++++++++++--- controllers/spec/function.go | 1 + controllers/spec/sink.go | 1 + controllers/spec/source.go | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/controllers/spec/common.go b/controllers/spec/common.go index b953431f..9cc9c01d 100644 --- a/controllers/spec/common.go +++ b/controllers/spec/common.go @@ -39,6 +39,7 @@ import ( v1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" @@ -436,13 +437,13 @@ func makePodTemplate(container *corev1.Container, filebeatContainer *corev1.Cont } func MakeJavaFunctionCommand(downloadPath, packageFile, name, clusterName, generateLogConfigCommand, logLevel, details, extraDependenciesDir, uid string, - javaOpts []string, hasPulsarctl, hasWget, authProvided, tlsProvided bool, secretMaps map[string]v1alpha1.SecretRef, + memory *resource.Quantity, javaOpts []string, hasPulsarctl, hasWget, authProvided, tlsProvided bool, secretMaps map[string]v1alpha1.SecretRef, state *v1alpha1.Stateful, tlsConfig TLSConfig, authConfig *v1alpha1.AuthConfig, maxPendingAsyncRequests *int32, logConfigFileName string) []string { processCommand := setShardIDEnvironmentVariableCommand() + " && " + generateLogConfigCommand + strings.Join(getProcessJavaRuntimeArgs(name, packageFile, clusterName, logLevel, details, - extraDependenciesDir, uid, javaOpts, authProvided, tlsProvided, secretMaps, state, tlsConfig, + extraDependenciesDir, uid, memory, javaOpts, authProvided, tlsProvided, secretMaps, state, tlsConfig, authConfig, maxPendingAsyncRequests, logConfigFileName), " ") if downloadPath != "" && !utils.EnableInitContainers { // prepend download command if the downPath is provided @@ -1184,7 +1185,7 @@ func setShardIDEnvironmentVariableCommand() string { } func getProcessJavaRuntimeArgs(name, packageName, clusterName, logLevel, details, extraDependenciesDir, uid string, - javaOpts []string, authProvided, tlsProvided bool, secretMaps map[string]v1alpha1.SecretRef, + memory *resource.Quantity, javaOpts []string, authProvided, tlsProvided bool, secretMaps map[string]v1alpha1.SecretRef, state *v1alpha1.Stateful, tlsConfig TLSConfig, authConfig *v1alpha1.AuthConfig, maxPendingAsyncRequests *int32, logConfigFileName string) []string { @@ -1206,6 +1207,8 @@ func getProcessJavaRuntimeArgs(name, packageName, clusterName, logLevel, details }, " ") } + // maxDirectMemory takes 20% of the total memory, while MaxRamPercentage is 70%, the rest 10% is for misc usage + maxDirectMemory := resource.NewScaledQuantity(memory.Value()/5, 0) args := []string{ "exec", "java", @@ -1218,6 +1221,7 @@ func getProcessJavaRuntimeArgs(name, packageName, clusterName, logLevel, details "-Dpulsar.allocator.exit_on_oom=true", setLogLevel, "-XX:MaxRAMPercentage=70", + "-XX:MaxDirectMemorySize=" + getDecimalSIMemory(maxDirectMemory), "-XX:+UseG1GC", "-XX:+HeapDumpOnOutOfMemoryError", "-XX:HeapDumpPath=/pulsar/tmp/heapdump-%p.hprof", @@ -2027,6 +2031,14 @@ func getPythonSecretProviderArgs(secretMaps map[string]v1alpha1.SecretRef) []str return ret } +// Java command requires memory values in resource.DecimalSI format +func getDecimalSIMemory(quantity *resource.Quantity) string { + if quantity.Format == resource.DecimalSI { + return quantity.String() + } + return resource.NewQuantity(quantity.Value(), resource.DecimalSI).String() +} + func getGenericSecretProviderArgs(secretMaps map[string]v1alpha1.SecretRef, language string) []string { var ret []string if len(secretMaps) > 0 { diff --git a/controllers/spec/function.go b/controllers/spec/function.go index 738dd302..08cb32f0 100644 --- a/controllers/spec/function.go +++ b/controllers/spec/function.go @@ -237,6 +237,7 @@ func makeFunctionCommand(function *v1alpha1.Function) []string { generateFunctionDetailsInJSON(function), spec.Java.ExtraDependenciesDir, string(function.UID), + spec.Resources.Limits.Memory(), spec.Java.JavaOpts, hasPulsarctl, hasWget, spec.Pulsar.AuthSecret != "", spec.Pulsar.TLSSecret != "", spec.SecretsMap, spec.StateConfig, spec.Pulsar.TLSConfig, diff --git a/controllers/spec/sink.go b/controllers/spec/sink.go index 81e37d5e..2a1028f6 100644 --- a/controllers/spec/sink.go +++ b/controllers/spec/sink.go @@ -231,6 +231,7 @@ func MakeSinkCommand(sink *v1alpha1.Sink) []string { parseJavaLogLevel(spec.Java), generateSinkDetailsInJSON(sink), spec.Java.ExtraDependenciesDir, string(sink.UID), + spec.Resources.Limits.Memory(), spec.Java.JavaOpts, hasPulsarctl, hasWget, spec.Pulsar.AuthSecret != "", spec.Pulsar.TLSSecret != "", spec.SecretsMap, spec.StateConfig, spec.Pulsar.TLSConfig, spec.Pulsar.AuthConfig, nil, GenerateJavaLogConfigFileName(spec.Java)) diff --git a/controllers/spec/source.go b/controllers/spec/source.go index b10e140a..80d10285 100644 --- a/controllers/spec/source.go +++ b/controllers/spec/source.go @@ -178,6 +178,7 @@ func makeSourceCommand(source *v1alpha1.Source) []string { parseJavaLogLevel(spec.Java), generateSourceDetailsInJSON(source), spec.Java.ExtraDependenciesDir, string(source.UID), + spec.Resources.Limits.Memory(), spec.Java.JavaOpts, hasPulsarctl, hasWget, spec.Pulsar.AuthSecret != "", spec.Pulsar.TLSSecret != "", spec.SecretsMap, spec.StateConfig, spec.Pulsar.TLSConfig, spec.Pulsar.AuthConfig, nil, GenerateJavaLogConfigFileName(spec.Java))