-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(dump): add comprehensive tests for stack trace and coredump functionalities #5434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
748b8bf
a5adf37
9d84db2
260ac21
e403662
888be95
e65906e
d5c4189
838810a
0309050
5e55bb8
d4fe51c
da0e1e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,18 +21,19 @@ import ( | |||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||
| "os/signal" | ||||||||||||||||||||||||||||||
| "runtime" | ||||||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||||||
| "sync/atomic" | ||||||||||||||||||||||||||||||
| "syscall" | ||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| "github.com/go-logr/logr" | ||||||||||||||||||||||||||||||
| ctrl "sigs.k8s.io/controller-runtime" | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var log logr.Logger | ||||||||||||||||||||||||||||||
| var log = ctrl.Log.WithName("dump") | ||||||||||||||||||||||||||||||
| var initialized int32 // Changed to atomic int32 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var initialized bool | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var dumpfile string = "%s/%s-%s.txt" | ||||||||||||||||||||||||||||||
| var dumpfileMutex sync.RWMutex | ||||||||||||||||||||||||||||||
| var dumpfile string = "%s-%s.txt" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| func StackTrace(all bool) string { | ||||||||||||||||||||||||||||||
| buf := make([]byte, 10240) | ||||||||||||||||||||||||||||||
|
|
@@ -52,39 +53,36 @@ func StackTrace(all bool) string { | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| func InstallgoroutineDumpGenerator() { | ||||||||||||||||||||||||||||||
| if !initialized { | ||||||||||||||||||||||||||||||
| log = ctrl.Log.WithName("dump") | ||||||||||||||||||||||||||||||
| log.Info("Register goroutine dump generator") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| signals := make(chan os.Signal, 1) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| signal.Notify(signals, syscall.SIGQUIT) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||||
| for { | ||||||||||||||||||||||||||||||
| sig := <-signals | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| switch sig { | ||||||||||||||||||||||||||||||
| case syscall.SIGQUIT: | ||||||||||||||||||||||||||||||
| t := time.Now() | ||||||||||||||||||||||||||||||
| timestamp := fmt.Sprint(t.Format("20060102150405")) | ||||||||||||||||||||||||||||||
| log.Info("User uses kill -3 to generate goroutine dump") | ||||||||||||||||||||||||||||||
| // coredump("/tmp/go_" + timestamp + ".txt") | ||||||||||||||||||||||||||||||
| coredump(fmt.Sprintf(dumpfile, "/tmp", "go", timestamp)) | ||||||||||||||||||||||||||||||
| // case syscall.SIGTERM: | ||||||||||||||||||||||||||||||
| // fmt.Println("User told me to exit") | ||||||||||||||||||||||||||||||
| // os.Exit(0) | ||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| initialized = true | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| if !atomic.CompareAndSwapInt32(&initialized, 0, 1) { | ||||||||||||||||||||||||||||||
| log.Info("Do nothing for installing grouting dump.") | ||||||||||||||||||||||||||||||
|
adity1raut marked this conversation as resolved.
|
||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| log.Info("Register goroutine dump generator") | ||||||||||||||||||||||||||||||
|
Comment on lines
+56
to
+61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a potential panic here. The package-level To fix this, Additionally, there's a typo in the log message on line 59: "grouting" should be "goroutine".
Suggested change
Comment on lines
+56
to
+61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a race condition here. If two goroutines call To fix this, you should initialize the // At package level (e.g. line 33)
var log = ctrl.Log.WithName("dump")
// Then, remove the initialization from this function:
func InstallgoroutineDumpGenerator() {
if !atomic.CompareAndSwapInt32(&initialized, 0, 1) {
log.Info("Do nothing for installing grouting dump.")
return
}
log.Info("Register goroutine dump generator")
// ... rest of the function
} |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| signals := make(chan os.Signal, 1) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| signal.Notify(signals, syscall.SIGQUIT) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||||
| for { | ||||||||||||||||||||||||||||||
| sig := <-signals | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| switch sig { | ||||||||||||||||||||||||||||||
| case syscall.SIGQUIT: | ||||||||||||||||||||||||||||||
| t := time.Now() | ||||||||||||||||||||||||||||||
| timestamp := fmt.Sprint(t.Format("20060102150405")) | ||||||||||||||||||||||||||||||
| log.Info("User uses kill -3 to generate goroutine dump") | ||||||||||||||||||||||||||||||
| dumpfileMutex.RLock() | ||||||||||||||||||||||||||||||
| filename := fmt.Sprintf(dumpfile, "/tmp", "go", timestamp) | ||||||||||||||||||||||||||||||
|
adity1raut marked this conversation as resolved.
|
||||||||||||||||||||||||||||||
| dumpfileMutex.RUnlock() | ||||||||||||||||||||||||||||||
|
Comment on lines
+76
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
adity1raut marked this conversation as resolved.
|
||||||||||||||||||||||||||||||
| coredump(filename) | ||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| func coredump(fileName string) { | ||||||||||||||||||||||||||||||
|
|
@@ -98,3 +96,9 @@ func coredump(fileName string) { | |||||||||||||||||||||||||||||
| log.Info(stdout) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // ResetForTesting resets the package state for testing purposes | ||||||||||||||||||||||||||||||
| // This should only be used in tests | ||||||||||||||||||||||||||||||
| func ResetForTesting() { | ||||||||||||||||||||||||||||||
|
Comment on lines
+100
to
+102
|
||||||||||||||||||||||||||||||
| // ResetForTesting resets the package state for testing purposes | |
| // This should only be used in tests | |
| func ResetForTesting() { | |
| // resetForTesting resets the package state for testing purposes | |
| // This should only be used in tests | |
| func resetForTesting() { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| Copyright 2023 The Fluid Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package dump | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestDump(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Dump Suite") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in log message: "grouting" should be "goroutine".