From 50d2e05ca763cc19752d53f7efdf29ecb91da597 Mon Sep 17 00:00:00 2001 From: Shihyu Ho Date: Thu, 18 Jan 2024 17:43:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E5=9F=B7=E8=A1=8C?= =?UTF-8?q?=E9=A0=86=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- calc/calculator.go | 75 +++++++++++++++++++++++++++++------------ calc/calculator_test.go | 40 +++++++++++----------- entrypoint.sh | 2 +- jib-demo/pom.xml | 3 ++ 5 files changed, 78 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 3a91ccd..b57ccf8 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ memory-calculator -h - `MEM_CALC_HOME`:用於設定執行檔的目錄,預設值為: `/usr/local/bin`。 - `MEM_CALC_ENABLED`:預設為 `true`,若將此值設為 `false`,則會跳過記憶體計算,直接啟動 Java 應用程式。 -- `DEBUG`:除錯模式,預設為 `false`,若將此值設為 `true`,則會列印出計算過程中除錯訊息,及啟動 Java 應用程式的完整指令。 +- `MEM_CALC_DEBUG`:除錯模式,預設為 `false`,若將此值設為 `true`,則會列印出計算過程中除錯訊息,及啟動 Java 應用程式的完整指令。 ## 開發前準備 diff --git a/calc/calculator.go b/calc/calculator.go index 5d647ed..8f6a0ba 100644 --- a/calc/calculator.go +++ b/calc/calculator.go @@ -12,7 +12,19 @@ import ( ) const ( - envBpiJvmCaCerts = "BPI_JVM_CACERTS" + envBpiJvmCaCerts = "BPI_JVM_CACERTS" + helperActiveProcessorCount = "active-processor-count" + helperJavaOpts = "java-opts" + helperJvmHeap = "jvm-heap" + helperLinkLocalDns = "link-local-dns" + helperMemoryCalculator = "memory-calculator" + helperOpensslCertificateLoader = "openssl-certificate-loader" + helperSecurityProvidersClasspath9 = "security-providers-classpath-9" + helperSecurityProvidersConfigurer = "security-providers-configurer" + helperDebug9 = "debug-9" + helperJmx = "jmx" + helperNmt = "nmt" + helperJfr = "jfr" ) type Calculator struct { @@ -60,14 +72,33 @@ func (c *Calculator) Execute() (*JavaToolOptions, error) { return nil, err } - cmds, err := c.buildCommands() + helpers, err := c.buildHelpers() if err != nil { return nil, err } - // 依序執行 helper - for _, cmd := range cmds { - values, err := cmd.Execute() + helpersInOrder := []string{ + helperActiveProcessorCount, + helperJavaOpts, + helperJvmHeap, + helperLinkLocalDns, + helperMemoryCalculator, + helperOpensslCertificateLoader, + helperSecurityProvidersClasspath9, + helperSecurityProvidersConfigurer, + helperDebug9, + helperJmx, + helperNmt, + helperJfr, + } + + // 按照指定順序執行命令 + for _, key := range helpersInOrder { + h, ok := helpers[key] + if !ok { + continue + } + values, err := h.Execute() if err != nil { return nil, err } @@ -85,7 +116,7 @@ func (c *Calculator) Execute() (*JavaToolOptions, error) { // 這邊基本上是從底層 libjvm 套件中複製過來, 我們只支援 Java 9+ 的計算 // https://github.com/paketo-buildpacks/libjvm/blob/main/cmd/helper/main.go // https://github.com/paketo-buildpacks/libjvm/blob/main/build.go#L274 -func (c *Calculator) buildCommands() (cmds map[string]sherpa.ExecD, err error) { +func (c *Calculator) buildHelpers() (helpers map[string]sherpa.ExecD, err error) { var ( l = bard.NewLogger(os.Stdout) cl = libjvm.NewCertificateLoader() @@ -115,30 +146,30 @@ func (c *Calculator) buildCommands() (cmds map[string]sherpa.ExecD, err error) { return nil, fmt.Errorf("unable to read DNS client configuration from %s\n%w", file, err) } - cmds = map[string]sherpa.ExecD{ - "active-processor-count": a, - "java-opts": j, - "jvm-heap": jh, - "link-local-dns": d, - "memory-calculator": m, - "openssl-certificate-loader": o, - "security-providers-classpath-9": s9, - "security-providers-configurer": spc, - "debug-9": d9, - "jmx": jm, - "nmt": n, - "jfr": jf, + helpers = map[string]sherpa.ExecD{ + helperActiveProcessorCount: a, + helperJavaOpts: j, + helperJvmHeap: jh, + helperLinkLocalDns: d, + helperMemoryCalculator: m, + helperOpensslCertificateLoader: o, + helperSecurityProvidersClasspath9: s9, + helperSecurityProvidersConfigurer: spc, + helperDebug9: d9, + helperJmx: jm, + helperNmt: n, + helperJfr: jf, } // 底層的實作中要求若開啟 jvm-cacert 則必須要設定相關的系統參數, 否則會報錯, 所以針對這個改成沒設定就不要跑了 if _, ok := os.LookupEnv(envBpiJvmCaCerts); !ok { - delete(cmds, "openssl-certificate-loader") + delete(helpers, helperOpensslCertificateLoader) } // 由於關閉 nmt 底層會印出一些關閉的 log, 我不想要看到那些, 所以針對這個改成沒開啟就不要跑了 if !*c.EnableNmt { - delete(cmds, "nmt") + delete(helpers, helperNmt) } - return cmds, nil + return helpers, nil } func contribute(cs ...Contributor) error { diff --git a/calc/calculator_test.go b/calc/calculator_test.go index acbd5ba..54b8d3f 100644 --- a/calc/calculator_test.go +++ b/calc/calculator_test.go @@ -27,62 +27,62 @@ func TestCalculator_Contribute(t *testing.T) { } } -func TestCalculator_buildCommands_HasCaCerts(t *testing.T) { +func TestCalculator_buildHelpers_HasCaCerts(t *testing.T) { calculator := NewCalculator() os.Setenv(envBpiJvmCaCerts, "some-value") defer os.Unsetenv(envBpiJvmCaCerts) - cmds, err := calculator.buildCommands() + helpers, err := calculator.buildHelpers() if err != nil { - t.Fatalf("buildCommands returned an error: %v", err) + t.Fatalf("buildHelpers returned an error: %v", err) } - if _, ok := cmds["openssl-certificate-loader"]; !ok { - t.Errorf("openssl-certificate-loader command not found in cmds") + if _, ok := helpers[helperOpensslCertificateLoader]; !ok { + t.Errorf(helperOpensslCertificateLoader + " helper not found in helpers") } } -func TestCalculator_buildCommands_NoCaCerts(t *testing.T) { +func TestCalculator_buildHelpers_NoCaCerts(t *testing.T) { calculator := NewCalculator() os.Unsetenv(envBpiJvmCaCerts) defer os.Unsetenv(envBpiJvmCaCerts) - cmds, err := calculator.buildCommands() + helpers, err := calculator.buildHelpers() if err != nil { - t.Fatalf("buildCommands returned an error: %v", err) + t.Fatalf("buildHelpers returned an error: %v", err) } - if _, ok := cmds["openssl-certificate-loader"]; ok { - t.Errorf("openssl-certificate-loader command should not be present") + if _, ok := helpers[helperOpensslCertificateLoader]; ok { + t.Errorf(helperOpensslCertificateLoader + " helper should not be present") } } -func TestCalculator_buildCommands_EnableNmt(t *testing.T) { +func TestCalculator_buildHelpers_EnableNmt(t *testing.T) { calculator := NewCalculator() *calculator.EnableNmt = true - cmds, err := calculator.buildCommands() + helpers, err := calculator.buildHelpers() if err != nil { - t.Fatalf("buildCommands returned an error: %v", err) + t.Fatalf("buildHelpers returned an error: %v", err) } - if _, ok := cmds["nmt"]; !ok { - t.Errorf("nmt command should be present") + if _, ok := helpers[helperNmt]; !ok { + t.Errorf(helperNmt + " helper should be present") } } -func TestCalculator_buildCommands_DisableNmt(t *testing.T) { +func TestCalculator_buildHelpers_DisableNmt(t *testing.T) { calculator := NewCalculator() *calculator.EnableNmt = false - cmds, err := calculator.buildCommands() + helpers, err := calculator.buildHelpers() if err != nil { - t.Fatalf("buildCommands returned an error: %v", err) + t.Fatalf("buildHelpers returned an error: %v", err) } - if _, ok := cmds["nmt"]; ok { - t.Errorf("nmt command should not be present") + if _, ok := helpers[helperNmt]; ok { + t.Errorf(helperNmt + " helper should not be present") } } diff --git a/entrypoint.sh b/entrypoint.sh index f10c9b5..7ef0cf0 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -37,7 +37,7 @@ execute_java_app() { [ "$DEBUG" = true ] && set +x } -DEBUG="${DEBUG:-false}" +DEBUG="${MEM_CALC_DEBUG:-false}" BIN="${MEM_CALC_HOME:-$DEFAULT_BIN_PATH}" JVM_FLAGS=$(read_jvm_flags) ARGS="$@" diff --git a/jib-demo/pom.xml b/jib-demo/pom.xml index 451ab37..267aaa5 100644 --- a/jib-demo/pom.xml +++ b/jib-demo/pom.xml @@ -74,6 +74,9 @@ some args + + -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5011 + 8080 5005