-
Notifications
You must be signed in to change notification settings - Fork 138
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
Support marking nodes for maintenance #15
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2848e9a
Add cover target for makefile using HTML heatmap
tgross 2bace38
Change WriteHealthCheck to SendHeartbeat for API clarity
tgross a196c95
Signal handler for SIGUSR1 to mark for maintenance mode
tgross d3f800a
Merge in master; resolve conflicts with onStart handler branch
tgross ed1a7ad
Add README remarks for maintenance mode
tgross 23b9b86
Reap services from Consul when marking for maintenance
tgross 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,5 @@ coverage.out | |
.godeps/ | ||
build/ | ||
release/ | ||
containerbuddy | ||
examples/*/opt/containerbuddy/containerbuddy | ||
*.tar.gz |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package main | ||
|
||
type DiscoveryService interface { | ||
WriteHealthCheck(*ServiceConfig) | ||
SendHeartbeat(*ServiceConfig) | ||
CheckForUpstreamChanges(*BackendConfig) bool | ||
MarkForMaintenance(*ServiceConfig) | ||
} | ||
|
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
) | ||
|
||
// globals are eeeeevil | ||
var paused bool | ||
var pauseLock = sync.RWMutex{} | ||
|
||
// we wrap access to `paused` in a RLock so that if we're in the middle of | ||
// marking services for maintenance we don't get stale reads | ||
func inMaintenanceMode() bool { | ||
pauseLock.RLock() | ||
defer pauseLock.RUnlock() | ||
return paused | ||
} | ||
|
||
func toggleMaintenanceMode() { | ||
pauseLock.Lock() | ||
defer pauseLock.Unlock() | ||
paused = !paused | ||
} | ||
|
||
// Listen for and capture signals from the OS | ||
func handleSignals(config *Config) { | ||
sig := make(chan os.Signal, 1) | ||
signal.Notify(sig, syscall.SIGUSR1) | ||
go func() { | ||
for signal := range sig { | ||
switch signal { | ||
// there's only one handler today but this makes it obvious | ||
// where to add support for new signals | ||
case syscall.SIGUSR1: | ||
toggleMaintenanceMode() | ||
if inMaintenanceMode() { | ||
log.Println("we are paused!") | ||
for _, service := range config.Services { | ||
log.Printf("Marking for maintenance: %s\n", service.Name) | ||
service.MarkForMaintenance() | ||
} | ||
} | ||
} | ||
} | ||
}() | ||
} |
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,44 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"runtime" | ||
"syscall" | ||
"testing" | ||
) | ||
|
||
func TestMaintenanceSignal(t *testing.T) { | ||
|
||
if inMaintenanceMode() { | ||
t.Errorf("Should not be in maintenance mode before starting handler") | ||
} | ||
handleSignals(&Config{}) | ||
if inMaintenanceMode() { | ||
t.Errorf("Should not be in maintenance mode after starting handler") | ||
} | ||
|
||
sendAndWaitForSignal(t, syscall.SIGUSR1) | ||
if !inMaintenanceMode() { | ||
t.Errorf("Should be in maintenance mode after receiving SIGUSR1") | ||
} | ||
sendAndWaitForSignal(t, syscall.SIGUSR1) | ||
if inMaintenanceMode() { | ||
t.Errorf("Should not be in maintenance mode after receiving second SIGUSR1") | ||
} | ||
} | ||
|
||
// helper to ensure that we block for the signal to deliver any signal | ||
// we need, and then yield execution so that the handler gets a chance | ||
// at running. If we don't do this there's a race where we can check | ||
// resulting side-effects of a handler before it's been run | ||
func sendAndWaitForSignal(t *testing.T, s os.Signal) { | ||
sig := make(chan os.Signal, 1) | ||
signal.Notify(sig, syscall.SIGUSR1) | ||
me, _ := os.FindProcess(os.Getpid()) | ||
if err := me.Signal(s); err != nil { | ||
t.Errorf("Got error on SIGUSR1: %v", err) | ||
} | ||
<-sig | ||
runtime.Gosched() | ||
} |
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.
Thanks for testing how to unset maintenance mode.