-
Notifications
You must be signed in to change notification settings - Fork 342
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
bugfix: add restart empty sd protection #1494
Merged
+129
−9
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9923baa
bugfix:
Wanghb1 a4831e1
Update first_launch.go
Wanghb1 9f08d34
Update first_launch.go
Wanghb1 f651b89
Update first_launch.go
Wanghb1 18b0dbb
Update first_launch.go
Wanghb1 c1830e3
Update first_launch.go
Wanghb1 98a0a0e
Update first_launch.go
Wanghb1 ceeb7e2
review
Wanghb1 2905de9
review
Wanghb1 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package protect | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/apache/servicecomb-service-center/server/config" | ||
|
||
"github.com/apache/servicecomb-service-center/pkg/log" | ||
) | ||
|
||
/** | ||
for restart service center, set a restartProtectInterval time window to return RestartProtectHttpCode on discovery apis, | ||
indicating that sdk not need to clear cache | ||
*/ | ||
|
||
var ( | ||
isWithinProtection bool | ||
startupTimestamp int64 | ||
enableInstanceNullProtect bool | ||
restartProtectInterval time.Duration | ||
RestartProtectHttpCode int | ||
validProtectCode = map[int]struct{}{http.StatusNotModified: {}, http.StatusUnprocessableEntity: {}, http.StatusInternalServerError: {}} | ||
) | ||
|
||
const ( | ||
maxInterval = 120 * time.Second | ||
minInterval = 0 * time.Second | ||
defaultRestartProtectInterval = 120 * time.Second | ||
) | ||
|
||
func Init() { | ||
enableInstanceNullProtect = config.GetBool("instance_null_protect.enable", false) | ||
if !enableInstanceNullProtect { | ||
return | ||
} | ||
restartProtectInterval = time.Duration(config.GetInt("instance_null_protect.restart_protect_interval", 120)) * time.Second | ||
if restartProtectInterval > maxInterval || restartProtectInterval < minInterval { | ||
log.Warn(fmt.Sprintf("invalid instance_null_protect.restart_protect_interval: %d,"+ | ||
" must between %d-%ds inclusively", restartProtectInterval, minInterval, maxInterval)) | ||
restartProtectInterval = defaultRestartProtectInterval | ||
} | ||
RestartProtectHttpCode = config.GetInt("instance_null_protect.http_status", http.StatusNotModified) | ||
if _, ok := validProtectCode[RestartProtectHttpCode]; !ok { | ||
log.Warn(fmt.Sprintf("invalid instance_null_protect.http_status: %d, must be %v", RestartProtectHttpCode, validProtectCode)) | ||
RestartProtectHttpCode = http.StatusNotModified | ||
} | ||
|
||
log.Info(fmt.Sprintf("instance_null_protect.enable: %t", enableInstanceNullProtect)) | ||
log.Info(fmt.Sprintf("instance_null_protect.restart_protect_interval: %d", restartProtectInterval)) | ||
log.Info(fmt.Sprintf("instance_null_protect.http_status: %d", RestartProtectHttpCode)) | ||
startupTimestamp = time.Now().UnixNano() | ||
isWithinProtection = true | ||
} | ||
|
||
func IsWithinRestartProtection() bool { | ||
if !enableInstanceNullProtect { | ||
return false | ||
} | ||
|
||
if !isWithinProtection { | ||
return false | ||
} | ||
|
||
if time.Now().Add(-restartProtectInterval).UnixNano() > startupTimestamp { | ||
log.Info("restart protection stop") | ||
isWithinProtection = false | ||
return false | ||
} | ||
log.Info("within restart protection") | ||
return true | ||
} |
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,30 @@ | ||
package protect | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsWithinRestartProtection(t *testing.T) { | ||
restartProtectInterval = 2 * time.Minute | ||
|
||
// protection switch off | ||
enableInstanceNullProtect = false | ||
assert.False(t, IsWithinRestartProtection()) | ||
// within protection | ||
enableInstanceNullProtect = true | ||
isWithinProtection = true | ||
startupTimestamp = time.Now().Add(-1 * time.Minute).UnixNano() | ||
assert.True(t, IsWithinRestartProtection()) | ||
|
||
// protection delay exceed | ||
enableInstanceNullProtect = true | ||
isWithinProtection = true | ||
startupTimestamp = time.Now().Add(-2 * time.Minute).Unix() | ||
assert.False(t, IsWithinRestartProtection()) | ||
|
||
// always false after exceed | ||
assert.False(t, IsWithinRestartProtection()) | ||
} |
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
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.
enable为false的时候,直接退出,不用做初始化的操作了