Skip to content

Commit

Permalink
optimize jvm parameters (#708)
Browse files Browse the repository at this point in the history
* optimize jvm parameters

* check style

* update
  • Loading branch information
nlu90 authored Dec 6, 2023
1 parent 3e78407 commit 759cbf6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 27 deletions.
32 changes: 8 additions & 24 deletions controllers/spec/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
autov2 "k8s.io/api/autoscaling/v2"
v1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -340,14 +339,14 @@ func makePodTemplate(container *corev1.Container, filebeatContainer *corev1.Cont
}
}

func MakeJavaFunctionCommand(downloadPath, packageFile, name, clusterName, generateLogConfigCommand, logLevel, details, extraDependenciesDir, uid string, memory *resource.Quantity,
func MakeJavaFunctionCommand(downloadPath, packageFile, name, clusterName, generateLogConfigCommand, logLevel, details, extraDependenciesDir, uid string,
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, memory, javaOpts, authProvided, tlsProvided, secretMaps, state, tlsConfig,
extraDependenciesDir, uid, javaOpts, authProvided, tlsProvided, secretMaps, state, tlsConfig,
authConfig, maxPendingAsyncRequests, logConfigFileName), " ")
if downloadPath != "" && !utils.EnableInitContainers {
// prepend download command if the downPath is provided
Expand Down Expand Up @@ -1091,7 +1090,7 @@ func setShardIDEnvironmentVariableCommand() string {
}

func getProcessJavaRuntimeArgs(name, packageName, clusterName, logLevel, details, extraDependenciesDir, uid string,
memory *resource.Quantity, javaOpts []string, authProvided, tlsProvided bool, secretMaps map[string]v1alpha1.SecretRef,
javaOpts []string, authProvided, tlsProvided bool, secretMaps map[string]v1alpha1.SecretRef,
state *v1alpha1.Stateful,
tlsConfig TLSConfig, authConfig *v1alpha1.AuthConfig,
maxPendingAsyncRequests *int32, logConfigFileName string) []string {
Expand All @@ -1113,7 +1112,6 @@ func getProcessJavaRuntimeArgs(name, packageName, clusterName, logLevel, details
},
" ")
}
xmsMemory := resource.NewScaledQuantity(memory.Value()/2, 0)
args := []string{
"exec",
"java",
Expand All @@ -1124,9 +1122,12 @@ func getProcessJavaRuntimeArgs(name, packageName, clusterName, logLevel, details
"-Dpulsar.function.log.dir=logs/functions",
"-Dpulsar.function.log.file=" + fmt.Sprintf("%s-${%s}", name, EnvShardID),
setLogLevel,
"-Xmx" + getDecimalSIMemory(memory),
"-Xms" + getDecimalSIMemory(xmsMemory),
"-XX:InitialRAMPercentage=20",
"-XX:MaxRAMPercentage=40",
"-XX:+UseG1GC",
"-XX:+HeapDumpOnOutOfMemoryError",
"-XX:HeapDumpPath=/pulsar/tmp/heapdump-%p.hprof",
"-Xlog:gc*:file=/pulsar/logs/gc.log:time,level,tags:filecount=5,filesize=10M",
strings.Join(javaOpts, " "),
"org.apache.pulsar.functions.instance.JavaInstanceMain",
"--jar",
Expand Down Expand Up @@ -1904,15 +1905,6 @@ func getPythonSecretProviderArgs(secretMaps map[string]v1alpha1.SecretRef) []str
return ret
}

func calcInstanceMemoryResources(resources corev1.ResourceRequirements) *resource.Quantity {
if resources.Requests.Memory() == resources.Limits.Memory() {
// if request and limit are the same, use the value * 0.9 as the instance (JVM) memory size, to prevent OOM
return resource.NewQuantity(int64(float64(resources.Requests.Memory().Value())*0.9), resource.DecimalSI)
}
// if request and limit are different, use the request value as the instance (JVM) memory size
return resources.Requests.Memory()
}

func getGenericSecretProviderArgs(secretMaps map[string]v1alpha1.SecretRef, language string) []string {
var ret []string
if len(secretMaps) > 0 {
Expand All @@ -1931,14 +1923,6 @@ func getGenericSecretProviderArgs(secretMaps map[string]v1alpha1.SecretRef, lang
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 getTLSTrustCertPath(tlsVolume TLSConfig, path string) string {
return fmt.Sprintf("%s/%s", tlsVolume.GetMountPath(), path)
}
Expand Down
1 change: 0 additions & 1 deletion controllers/spec/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func makeFunctionCommand(function *v1alpha1.Function) []string {
generateFunctionDetailsInJSON(function),
spec.Java.ExtraDependenciesDir,
string(function.UID),
calcInstanceMemoryResources(spec.Resources),
spec.Java.JavaOpts, hasPulsarctl, hasWget,
spec.Pulsar.AuthSecret != "", spec.Pulsar.TLSSecret != "",
spec.SecretsMap, spec.StateConfig, spec.Pulsar.TLSConfig,
Expand Down
1 change: 0 additions & 1 deletion controllers/spec/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ func MakeSinkCommand(sink *v1alpha1.Sink) []string {
parseJavaLogLevel(spec.Java),
generateSinkDetailsInJSON(sink),
spec.Java.ExtraDependenciesDir, string(sink.UID),
calcInstanceMemoryResources(spec.Resources),
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))
Expand Down
1 change: 0 additions & 1 deletion controllers/spec/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func makeSourceCommand(source *v1alpha1.Source) []string {
parseJavaLogLevel(spec.Java),
generateSourceDetailsInJSON(source),
spec.Java.ExtraDependenciesDir, string(source.UID),
calcInstanceMemoryResources(spec.Resources),
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))
Expand Down

0 comments on commit 759cbf6

Please sign in to comment.