-
Notifications
You must be signed in to change notification settings - Fork 205
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
Added processDebugger component #4288
Merged
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
de90e23
- added processDebugger + integration
iulianpascalau d1409aa
Merge branch 'rc/2022-july' into process-debug-handler
iulianpascalau a2cddf0
- minor fixes
iulianpascalau f6a66f3
Merge branch 'rc/2022-july' into process-debug-handler
iulianpascalau 0c9b9ba
- fixed linter errors
iulianpascalau 2e8ff70
- increased polling interval
iulianpascalau 88856ff
- adjusted new log level
iulianpascalau 58583fd
Merge branch 'rc/2022-july' into process-debug-handler
iulianpascalau 86f5550
Merge branch 'rc/2022-july' into process-debug-handler
iulianpascalau 2341aeb
- fix after review
iulianpascalau c796b73
- fixed test
iulianpascalau c0c7fe8
Merge branch 'rc/2022-july' into process-debug-handler
iulianpascalau f0b261a
Merge branch 'rc/2022-july' into process-debug-handler
iulianpascalau 8f73599
Merge branch 'rc/v1.4.0' into process-debug-handler
iulianpascalau b1a9140
Merge branch 'rc/v1.4.0' into process-debug-handler
iulianpascalau 66e6f53
Merge branch 'rc/v1.4.0' into process-debug-handler
iulianpascalau dc680b2
Merge branch 'rc/v1.4.0' into process-debug-handler
iulianpascalau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package factory | ||
|
||
import ( | ||
"github.com/ElrondNetwork/elrond-go/config" | ||
"github.com/ElrondNetwork/elrond-go/debug/process" | ||
) | ||
|
||
// CreateProcessDebugger creates a new instance of type ProcessDebugger | ||
func CreateProcessDebugger(configs config.ProcessDebugConfig) (ProcessDebugger, error) { | ||
if !configs.Enabled { | ||
return process.NewDisabledDebugger(), nil | ||
} | ||
|
||
return process.NewProcessDebugger(configs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package factory | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ElrondNetwork/elrond-go-core/core/check" | ||
"github.com/ElrondNetwork/elrond-go/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateProcessDebugger(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("create disabled process debugger", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
configs := config.ProcessDebugConfig{ | ||
Enabled: false, | ||
} | ||
debugger, err := CreateProcessDebugger(configs) | ||
assert.Nil(t, err) | ||
assert.False(t, check.IfNil(debugger)) | ||
assert.Equal(t, "*process.disabledDebugger", fmt.Sprintf("%T", debugger)) | ||
}) | ||
t.Run("create real process debugger", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
configs := config.ProcessDebugConfig{ | ||
Enabled: true, | ||
LogLevelChanger: "", | ||
GoRoutinesDump: false, | ||
PollingTimeInSeconds: 1, | ||
} | ||
debugger, err := CreateProcessDebugger(configs) | ||
assert.Nil(t, err) | ||
assert.False(t, check.IfNil(debugger)) | ||
assert.Equal(t, "*process.processDebugger", fmt.Sprintf("%T", debugger)) | ||
}) | ||
t.Run("create real process debugger errors", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
configs := config.ProcessDebugConfig{ | ||
Enabled: true, | ||
} | ||
debugger, err := CreateProcessDebugger(configs) | ||
assert.NotNil(t, err) | ||
assert.True(t, check.IfNil(debugger)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package process | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"runtime" | ||
"sync" | ||
"time" | ||
|
||
logger "github.com/ElrondNetwork/elrond-go-logger" | ||
"github.com/ElrondNetwork/elrond-go/config" | ||
) | ||
|
||
const ( | ||
minAcceptedValue = 1 | ||
buffSize = 100 * 1024 * 1024 // 100MB | ||
) | ||
|
||
var log = logger.GetOrCreate("debug/process") | ||
|
||
type processDebugger struct { | ||
timer *time.Timer | ||
mut sync.RWMutex | ||
lastCheckedBlockRound int64 | ||
lastCommittedBlockRound int64 | ||
cancel func() | ||
goRoutinesDumpHandler func() | ||
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 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. no, can not properly test the component otherwise |
||
logChangeHandler func() | ||
pollingTime time.Duration | ||
debuggingLogLevel string | ||
dumpGoRoutines bool | ||
} | ||
|
||
// NewProcessDebugger creates a new debugger instance used to monitor the block process flow | ||
func NewProcessDebugger(config config.ProcessDebugConfig) (*processDebugger, error) { | ||
err := checkConfigs(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pollingTime := time.Duration(config.PollingTimeInSeconds) * time.Second | ||
d := &processDebugger{ | ||
timer: time.NewTimer(pollingTime), | ||
|
||
pollingTime: pollingTime, | ||
debuggingLogLevel: config.DebuggingLogLevel, | ||
dumpGoRoutines: config.GoRoutinesDump, | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
d.cancel = cancel | ||
d.goRoutinesDumpHandler = dumpGoRoutines | ||
d.logChangeHandler = d.changeLog | ||
|
||
go d.processLoop(ctx) | ||
|
||
return d, nil | ||
} | ||
|
||
func checkConfigs(config config.ProcessDebugConfig) error { | ||
if config.PollingTimeInSeconds < minAcceptedValue { | ||
return fmt.Errorf("%w for PollingTimeInSeconds, minimum %d, got %d", | ||
errInvalidValue, minAcceptedValue, config.PollingTimeInSeconds) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (debugger *processDebugger) processLoop(ctx context.Context) { | ||
log.Debug("processor debugger processLoop is starting...") | ||
|
||
defer debugger.timer.Stop() | ||
|
||
for { | ||
debugger.timer.Reset(debugger.pollingTime) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
log.Debug("processor debugger processLoop is closing...") | ||
return | ||
case <-debugger.timer.C: | ||
debugger.checkRounds() | ||
} | ||
} | ||
} | ||
|
||
func (debugger *processDebugger) checkRounds() { | ||
if debugger.shouldTriggerUpdatingLastCheckedRound() { | ||
debugger.trigger() | ||
} | ||
} | ||
|
||
func (debugger *processDebugger) shouldTriggerUpdatingLastCheckedRound() bool { | ||
debugger.mut.Lock() | ||
defer debugger.mut.Unlock() | ||
|
||
isNodeStarting := debugger.lastCheckedBlockRound == 0 && debugger.lastCommittedBlockRound <= 0 | ||
if isNodeStarting { | ||
log.Debug("processor debugger: node is starting") | ||
return false | ||
} | ||
|
||
defer func() { | ||
// update the last checked round | ||
debugger.lastCheckedBlockRound = debugger.lastCommittedBlockRound | ||
}() | ||
|
||
isFirstCommit := debugger.lastCheckedBlockRound == 0 && debugger.lastCommittedBlockRound > 0 | ||
if isFirstCommit { | ||
log.Debug("processor debugger: first committed block", "round", debugger.lastCommittedBlockRound) | ||
return false | ||
} | ||
|
||
isNodeRunning := debugger.lastCheckedBlockRound < debugger.lastCommittedBlockRound | ||
if isNodeRunning { | ||
log.Debug("processor debugger: node is running, nothing to do", "round", debugger.lastCommittedBlockRound) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (debugger *processDebugger) trigger() { | ||
debugger.mut.RLock() | ||
lastCommittedBlockRound := debugger.lastCommittedBlockRound | ||
debugger.mut.RUnlock() | ||
|
||
log.Warn("processor debugger: node is stuck", | ||
"last committed round", lastCommittedBlockRound) | ||
|
||
debugger.logChangeHandler() | ||
|
||
if debugger.dumpGoRoutines { | ||
debugger.goRoutinesDumpHandler() | ||
} | ||
} | ||
|
||
// SetLastCommittedBlockRound sets the last committed block's round | ||
func (debugger *processDebugger) SetLastCommittedBlockRound(round uint64) { | ||
debugger.mut.Lock() | ||
defer debugger.mut.Unlock() | ||
|
||
log.Debug("processor debugger: updated last committed block round", "round", round) | ||
debugger.lastCommittedBlockRound = int64(round) | ||
} | ||
|
||
// Close stops any started go routines | ||
func (debugger *processDebugger) Close() error { | ||
debugger.cancel() | ||
|
||
return nil | ||
} | ||
|
||
func dumpGoRoutines() { | ||
buff := make([]byte, buffSize) | ||
numBytes := runtime.Stack(buff, true) | ||
log.Debug(string(buff[:numBytes])) | ||
} | ||
|
||
func (debugger *processDebugger) changeLog() { | ||
errSetLogLevel := logger.SetLogLevel(debugger.debuggingLogLevel) | ||
if errSetLogLevel != nil { | ||
log.Error("debugger.changeLog: cannot change log level", "error", errSetLogLevel) | ||
} | ||
} | ||
|
||
// IsInterfaceNil returns true if there is no value under the interface | ||
func (debugger *processDebugger) IsInterfaceNil() bool { | ||
return debugger == nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
variables with the same
type
should stay together (optime memory usage)GoRoutinesDump
can be moved nearEnabled
fieldThere 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.
done