| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package healthchecker |
| 20 |
|
| 21 |
import ( |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
"net/http" |
| 25 |
"sync" |
| 26 |
"sync/atomic" |
| 27 |
"time" |
| 28 |
|
| 29 |
log "github.com/golang/glog" |
| 30 |
"github.com/mesos/mesos-go/api/v0/upid" |
| 31 |
) |
| 32 |
|
| 33 |
const ( |
| 34 |
defaultTimeout = time.Second |
| 35 |
defaultCheckDuration = time.Second |
| 36 |
defaultThreshold = 5 |
| 37 |
) |
| 38 |
|
| 39 |
var errCheckerStopped = errors.New("aborted HTTP request because checker was asked to stop") |
| 40 |
|
| 41 |
|
| 42 |
type SlaveHealthChecker struct { |
| 43 |
sync.RWMutex |
| 44 |
slaveUPID *upid.UPID |
| 45 |
tr *http.Transport |
| 46 |
client *http.Client |
| 47 |
threshold int32 |
| 48 |
checkDuration time.Duration |
| 49 |
continuousUnhealthyCount int32 |
| 50 |
stop chan struct{} |
| 51 |
ch chan time.Time |
| 52 |
paused bool |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func NewSlaveHealthChecker(slaveUPID *upid.UPID, threshold int, checkDuration time.Duration, timeout time.Duration) *SlaveHealthChecker { |
| 58 |
tr := &http.Transport{} |
| 59 |
checker := &SlaveHealthChecker{ |
| 60 |
slaveUPID: slaveUPID, |
| 61 |
client: &http.Client{Timeout: timeout, Transport: tr}, |
| 62 |
threshold: int32(threshold), |
| 63 |
checkDuration: checkDuration, |
| 64 |
stop: make(chan struct{}), |
| 65 |
ch: make(chan time.Time, 1), |
| 66 |
tr: tr, |
| 67 |
} |
| 68 |
if timeout == 0 { |
| 69 |
checker.client.Timeout = defaultTimeout |
| 70 |
} |
| 71 |
if checkDuration == 0 { |
| 72 |
checker.checkDuration = defaultCheckDuration |
| 73 |
} |
| 74 |
if threshold <= 0 { |
| 75 |
checker.threshold = defaultThreshold |
| 76 |
} |
| 77 |
return checker |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func (s *SlaveHealthChecker) Start() <-chan time.Time { |
| 82 |
go func() { |
| 83 |
t := time.NewTicker(s.checkDuration) |
| 84 |
defer t.Stop() |
| 85 |
for { |
| 86 |
select { |
| 87 |
case <-t.C: |
| 88 |
select { |
| 89 |
case <-s.stop: |
| 90 |
return |
| 91 |
default: |
| 92 |
|
| 93 |
} |
| 94 |
if paused, slavepid := func() (x bool, y upid.UPID) { |
| 95 |
s.RLock() |
| 96 |
defer s.RUnlock() |
| 97 |
x = s.paused |
| 98 |
if s.slaveUPID != nil { |
| 99 |
y = *s.slaveUPID |
| 100 |
} |
| 101 |
return |
| 102 |
}(); !paused { |
| 103 |
s.doCheck(slavepid) |
| 104 |
} |
| 105 |
case <-s.stop: |
| 106 |
return |
| 107 |
} |
| 108 |
} |
| 109 |
}() |
| 110 |
return s.ch |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
func (s *SlaveHealthChecker) Pause() { |
| 115 |
s.Lock() |
| 116 |
defer s.Unlock() |
| 117 |
s.paused = true |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func (s *SlaveHealthChecker) Continue(slaveUPID *upid.UPID) { |
| 122 |
s.Lock() |
| 123 |
defer s.Unlock() |
| 124 |
s.paused = false |
| 125 |
s.slaveUPID = slaveUPID |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
func (s *SlaveHealthChecker) Stop() { |
| 131 |
close(s.stop) |
| 132 |
} |
| 133 |
|
| 134 |
type errHttp struct{ StatusCode int } |
| 135 |
|
| 136 |
func (e *errHttp) Error() string { return fmt.Sprintf("http error code: %d", e.StatusCode) } |
| 137 |
|
| 138 |
func (s *SlaveHealthChecker) doCheck(pid upid.UPID) { |
| 139 |
unhealthy := false |
| 140 |
path := fmt.Sprintf("http://%s:%s/%s/health", pid.Host, pid.Port, pid.ID) |
| 141 |
req, err := http.NewRequest("HEAD", path, nil) |
| 142 |
req.Close = true |
| 143 |
err = s.httpDo(req, func(resp *http.Response, err error) error { |
| 144 |
if err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
defer resp.Body.Close() |
| 148 |
if resp.StatusCode != http.StatusOK { |
| 149 |
return &errHttp{resp.StatusCode} |
| 150 |
} |
| 151 |
return nil |
| 152 |
}) |
| 153 |
select { |
| 154 |
case <-s.stop: |
| 155 |
return |
| 156 |
default: |
| 157 |
} |
| 158 |
if err != nil { |
| 159 |
log.Errorf("Failed to request the health path: %v", err) |
| 160 |
unhealthy = true |
| 161 |
} |
| 162 |
if unhealthy { |
| 163 |
x := atomic.AddInt32(&s.continuousUnhealthyCount, 1) |
| 164 |
if x >= s.threshold { |
| 165 |
select { |
| 166 |
case s.ch <- time.Now(): |
| 167 |
default: |
| 168 |
} |
| 169 |
atomic.StoreInt32(&s.continuousUnhealthyCount, 0) |
| 170 |
} |
| 171 |
return |
| 172 |
} |
| 173 |
atomic.StoreInt32(&s.continuousUnhealthyCount, 0) |
| 174 |
} |
| 175 |
|
| 176 |
func (s *SlaveHealthChecker) httpDo(req *http.Request, f func(*http.Response, error) error) error { |
| 177 |
|
| 178 |
c := make(chan error, 1) |
| 179 |
go func() { c <- f(s.client.Do(req)) }() |
| 180 |
select { |
| 181 |
case <-s.stop: |
| 182 |
s.tr.CancelRequest(req) |
| 183 |
<-c |
| 184 |
return errCheckerStopped |
| 185 |
case err := <-c: |
| 186 |
return err |
| 187 |
} |
| 188 |
} |