Skip to content

Commit

Permalink
fix: 修正執行順序
Browse files Browse the repository at this point in the history
  • Loading branch information
shihyuho committed Jan 18, 2024
1 parent e5a4e08 commit 50d2e05
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 應用程式的完整指令。

## 開發前準備

Expand Down
75 changes: 53 additions & 22 deletions calc/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 20 additions & 20 deletions calc/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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="$@"
Expand Down
3 changes: 3 additions & 0 deletions jib-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
<arg>some</arg>
<arg>args</arg>
</args>
<environment>
<JAVA_OPTS>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5011</JAVA_OPTS>
</environment>
<ports>
<port>8080</port>
<port>5005</port>
Expand Down

0 comments on commit 50d2e05

Please sign in to comment.