Skip to content

Commit 3eb35bf

Browse files
committed
Merge branch 'jmap-fail'
2 parents 8ab4a2b + 019eabd commit 3eb35bf

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

cf_cli_java_plugin.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (u uuidGeneratorImpl) Generate() string {
5656

5757
const (
5858
// JavaDetectionCommand is the prologue command to detect on the Garden container if it contains a Java app. Visible for tests
59-
JavaDetectionCommand = "if ! pgrep -x \"java\" > /dev/null; then echo \"No 'java' process found running. Are you sure this is a Java app?\" >&2; exit 1; fi;"
59+
JavaDetectionCommand = "if ! pgrep -x \"java\" > /dev/null; then echo \"No 'java' process found running. Are you sure this is a Java app?\" >&2; exit 1; fi"
6060
heapDumpCommand = "heap-dump"
6161
threadDumpCommand = "thread-dump"
6262
)
@@ -167,17 +167,31 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
167167
cfSSHArguments = append(cfSSHArguments, "--app-instance-index", strconv.Itoa(applicationInstance))
168168
}
169169

170-
var remoteCommandTokens = []string{}
170+
var remoteCommandTokens = []string{JavaDetectionCommand}
171171

172172
switch command {
173173
case heapDumpCommand:
174174
heapdumpFileName := "/tmp/heapdump-" + uuidGenerator.Generate() + ".hprof"
175-
remoteCommandTokens = append(remoteCommandTokens, JavaDetectionCommand+"$(find -executable -name jmap | head -1) -dump:format=b,file="+heapdumpFileName+" $(pidof java) > /dev/null", "cat "+heapdumpFileName)
175+
remoteCommandTokens = append(remoteCommandTokens,
176+
// Check file does not already exist
177+
"if [ -f "+heapdumpFileName+" ]; then echo >&2 'Heap dump "+heapdumpFileName+" already exists'; exit 1; fi",
178+
/*
179+
* If there is not enough space on the filesystem to write the dump, jmap will create a file
180+
* with size 0, output something about not enough space left on device and exit with status code 0.
181+
* Because YOLO.
182+
*
183+
* Also: if the heap dump file already exists, jmap will output something about the file already
184+
* existing and exit with status code 0. At least it is consistent.
185+
*/
186+
"OUTPUT=$( $(find -executable -name jmap | head -1) -dump:format=b,file="+heapdumpFileName+" $(pidof java) ) || STATUS_CODE=$?",
187+
"if [ ! -s "+heapdumpFileName+" ]; then echo >&2 ${OUTPUT}; exit 1; fi",
188+
"if [ ${STATUS_CODE:-0} -gt 0 ]; then echo >&2 ${OUTPUT}; exit ${STATUS_CODE}; fi",
189+
"cat "+heapdumpFileName)
176190
if !keepAfterDownload {
177191
remoteCommandTokens = append(remoteCommandTokens, "rm -f "+heapdumpFileName)
178192
}
179193
case threadDumpCommand:
180-
remoteCommandTokens = append(remoteCommandTokens, JavaDetectionCommand+"$(find -executable -name jstack | head -1) $(pidof java)")
194+
remoteCommandTokens = append(remoteCommandTokens, "$(find -executable -name jstack | head -1) $(pidof java)")
181195
}
182196

183197
cfSSHArguments = append(cfSSHArguments, "--command")
@@ -215,7 +229,7 @@ func (c *JavaPlugin) GetMetadata() plugin.PluginMetadata {
215229
Name: "JavaPlugin",
216230
Version: plugin.VersionType{
217231
Major: 1,
218-
Minor: 0,
232+
Minor: 1,
219233
Build: 0,
220234
},
221235
MinCliVersion: plugin.VersionType{

cf_cli_java_plugin_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ var _ = Describe("CfJavaPlugin", func() {
180180
Expect(cliOutput).To(Equal(""))
181181

182182
Expect(commandExecutor.ExecuteCallCount()).To(Equal(1))
183-
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--command", JavaDetectionCommand + "$(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) > /dev/null; cat /tmp/heapdump-abcd-123456.hprof; rm -f /tmp/heapdump-abcd-123456.hprof"}))
183+
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--command", JavaDetectionCommand + "; if [ -f /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 'Heap dump /tmp/heapdump-abcd-123456.hprof already exists'; exit 1; fi; OUTPUT=$( $(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) ) || STATUS_CODE=$?; if [ ! -s /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 ${OUTPUT}; exit 1; fi; if [ ${STATUS_CODE:-0} -gt 0 ]; then echo >&2 ${OUTPUT}; exit ${STATUS_CODE}; fi; cat /tmp/heapdump-abcd-123456.hprof; rm -f /tmp/heapdump-abcd-123456.hprof"}))
184184
})
185185

186186
})
@@ -200,7 +200,7 @@ var _ = Describe("CfJavaPlugin", func() {
200200
Expect(cliOutput).To(Equal(""))
201201

202202
Expect(commandExecutor.ExecuteCallCount()).To(Equal(1))
203-
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--app-instance-index", "4", "--command", JavaDetectionCommand + "$(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) > /dev/null; cat /tmp/heapdump-abcd-123456.hprof; rm -f /tmp/heapdump-abcd-123456.hprof"}))
203+
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--app-instance-index", "4", "--command", JavaDetectionCommand + "; if [ -f /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 'Heap dump /tmp/heapdump-abcd-123456.hprof already exists'; exit 1; fi; OUTPUT=$( $(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) ) || STATUS_CODE=$?; if [ ! -s /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 ${OUTPUT}; exit 1; fi; if [ ${STATUS_CODE:-0} -gt 0 ]; then echo >&2 ${OUTPUT}; exit ${STATUS_CODE}; fi; cat /tmp/heapdump-abcd-123456.hprof; rm -f /tmp/heapdump-abcd-123456.hprof"}))
204204
})
205205

206206
})
@@ -220,7 +220,7 @@ var _ = Describe("CfJavaPlugin", func() {
220220
Expect(cliOutput).To(Equal(""))
221221

222222
Expect(commandExecutor.ExecuteCallCount()).To(Equal(1))
223-
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--app-instance-index", "4", "--command", JavaDetectionCommand + "$(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) > /dev/null; cat /tmp/heapdump-abcd-123456.hprof"}))
223+
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--app-instance-index", "4", "--command", JavaDetectionCommand + "; if [ -f /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 'Heap dump /tmp/heapdump-abcd-123456.hprof already exists'; exit 1; fi; OUTPUT=$( $(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) ) || STATUS_CODE=$?; if [ ! -s /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 ${OUTPUT}; exit 1; fi; if [ ${STATUS_CODE:-0} -gt 0 ]; then echo >&2 ${OUTPUT}; exit ${STATUS_CODE}; fi; cat /tmp/heapdump-abcd-123456.hprof"}))
224224
})
225225

226226
})
@@ -235,9 +235,9 @@ var _ = Describe("CfJavaPlugin", func() {
235235
return output, err
236236
})
237237

238-
Expect(output).To(Equal("cf ssh my_app --app-instance-index 4 --command '" + JavaDetectionCommand + "$(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) > /dev/null; cat /tmp/heapdump-abcd-123456.hprof'"))
238+
Expect(output).To(Equal("cf ssh my_app --app-instance-index 4 --command '" + JavaDetectionCommand + "; if [ -f /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 'Heap dump /tmp/heapdump-abcd-123456.hprof already exists'; exit 1; fi; OUTPUT=$( $(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) ) || STATUS_CODE=$?; if [ ! -s /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 ${OUTPUT}; exit 1; fi; if [ ${STATUS_CODE:-0} -gt 0 ]; then echo >&2 ${OUTPUT}; exit ${STATUS_CODE}; fi; cat /tmp/heapdump-abcd-123456.hprof'"))
239239
Expect(err).To(BeNil())
240-
Expect(cliOutput).To(ContainSubstring("cf ssh my_app --app-instance-index 4 --command '" + JavaDetectionCommand + "$(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) > /dev/null; cat /tmp/heapdump-abcd-123456.hprof'"))
240+
Expect(cliOutput).To(ContainSubstring("cf ssh my_app --app-instance-index 4 --command '" + JavaDetectionCommand + "; if [ -f /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 'Heap dump /tmp/heapdump-abcd-123456.hprof already exists'; exit 1; fi; OUTPUT=$( $(find -executable -name jmap | head -1) -dump:format=b,file=/tmp/heapdump-abcd-123456.hprof $(pidof java) ) || STATUS_CODE=$?; if [ ! -s /tmp/heapdump-abcd-123456.hprof ]; then echo >&2 ${OUTPUT}; exit 1; fi; if [ ${STATUS_CODE:-0} -gt 0 ]; then echo >&2 ${OUTPUT}; exit ${STATUS_CODE}; fi; cat /tmp/heapdump-abcd-123456.hprof'"))
241241

242242
Expect(commandExecutor.ExecuteCallCount()).To(Equal(0))
243243
})
@@ -303,7 +303,7 @@ var _ = Describe("CfJavaPlugin", func() {
303303
Expect(cliOutput).To(Equal(""))
304304

305305
Expect(commandExecutor.ExecuteCallCount()).To(Equal(1))
306-
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--command", JavaDetectionCommand + "$(find -executable -name jstack | head -1) $(pidof java)"}))
306+
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--command", JavaDetectionCommand + "; $(find -executable -name jstack | head -1) $(pidof java)"}))
307307
})
308308

309309
})
@@ -323,7 +323,7 @@ var _ = Describe("CfJavaPlugin", func() {
323323
Expect(cliOutput).To(Equal(""))
324324

325325
Expect(commandExecutor.ExecuteCallCount()).To(Equal(1))
326-
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--app-instance-index", "4", "--command", JavaDetectionCommand + "$(find -executable -name jstack | head -1) $(pidof java)"}))
326+
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"ssh", "my_app", "--app-instance-index", "4", "--command", JavaDetectionCommand + "; $(find -executable -name jstack | head -1) $(pidof java)"}))
327327
})
328328

329329
})
@@ -358,9 +358,9 @@ var _ = Describe("CfJavaPlugin", func() {
358358
return output, err
359359
})
360360

361-
Expect(output).To(Equal("cf ssh my_app --app-instance-index 4 --command '" + JavaDetectionCommand + "$(find -executable -name jstack | head -1) $(pidof java)'"))
361+
Expect(output).To(Equal("cf ssh my_app --app-instance-index 4 --command '" + JavaDetectionCommand + "; $(find -executable -name jstack | head -1) $(pidof java)'"))
362362
Expect(err).To(BeNil())
363-
Expect(cliOutput).To(ContainSubstring("cf ssh my_app --app-instance-index 4 --command '" + JavaDetectionCommand + "$(find -executable -name jstack | head -1) $(pidof java)'"))
363+
Expect(cliOutput).To(ContainSubstring("cf ssh my_app --app-instance-index 4 --command '" + JavaDetectionCommand + "; $(find -executable -name jstack | head -1) $(pidof java)'"))
364364

365365
Expect(commandExecutor.ExecuteCallCount()).To(Equal(0))
366366
})

0 commit comments

Comments
 (0)