| 1 |
package detector |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io/ioutil" |
| 8 |
"net" |
| 9 |
"net/http" |
| 10 |
"strconv" |
| 11 |
"sync" |
| 12 |
"time" |
| 13 |
|
| 14 |
log "github.com/golang/glog" |
| 15 |
mesos "github.com/mesos/mesos-go/api/v0/mesosproto" |
| 16 |
"github.com/mesos/mesos-go/api/v0/upid" |
| 17 |
"golang.org/x/net/context" |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
defaultMesosHttpClientTimeout = 10 * time.Second |
| 22 |
defaultMesosLeaderSyncInterval = 30 * time.Second |
| 23 |
defaultMesosMasterPort = 5050 |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
type fetcherFunc func(ctx context.Context, address string) (*upid.UPID, error) |
| 28 |
|
| 29 |
type Standalone struct { |
| 30 |
ch chan *mesos.MasterInfo |
| 31 |
client *http.Client |
| 32 |
tr *http.Transport |
| 33 |
pollOnce sync.Once |
| 34 |
initial *mesos.MasterInfo |
| 35 |
done chan struct{} |
| 36 |
cancelOnce sync.Once |
| 37 |
leaderSyncInterval time.Duration |
| 38 |
httpClientTimeout time.Duration |
| 39 |
assumedMasterPort int |
| 40 |
poller func(pf fetcherFunc) |
| 41 |
fetchPid fetcherFunc |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func NewStandalone(mi *mesos.MasterInfo) *Standalone { |
| 46 |
log.V(2).Infof("creating new standalone detector for %+v", mi) |
| 47 |
stand := &Standalone{ |
| 48 |
ch: make(chan *mesos.MasterInfo), |
| 49 |
tr: &http.Transport{}, |
| 50 |
initial: mi, |
| 51 |
done: make(chan struct{}), |
| 52 |
leaderSyncInterval: defaultMesosLeaderSyncInterval, |
| 53 |
httpClientTimeout: defaultMesosHttpClientTimeout, |
| 54 |
assumedMasterPort: defaultMesosMasterPort, |
| 55 |
} |
| 56 |
stand.poller = stand._poller |
| 57 |
stand.fetchPid = stand._fetchPid |
| 58 |
return stand |
| 59 |
} |
| 60 |
|
| 61 |
func (s *Standalone) String() string { |
| 62 |
return fmt.Sprintf("{initial: %+v}", s.initial) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func (s *Standalone) Detect(o MasterChanged) error { |
| 67 |
log.V(2).Info("Detect()") |
| 68 |
s.pollOnce.Do(func() { |
| 69 |
log.V(1).Info("spinning up asyc master detector poller") |
| 70 |
|
| 71 |
s.client = &http.Client{ |
| 72 |
Transport: s.tr, |
| 73 |
Timeout: s.httpClientTimeout, |
| 74 |
} |
| 75 |
go s.poller(s.fetchPid) |
| 76 |
}) |
| 77 |
if o != nil { |
| 78 |
log.V(1).Info("spawning asyc master detector listener") |
| 79 |
go func() { |
| 80 |
log.V(2).Infof("waiting for polled to send updates") |
| 81 |
pollWaiter: |
| 82 |
for { |
| 83 |
select { |
| 84 |
case mi, ok := <-s.ch: |
| 85 |
if !ok { |
| 86 |
break pollWaiter |
| 87 |
} |
| 88 |
log.V(1).Infof("detected master change: %+v", mi) |
| 89 |
o.OnMasterChanged(mi) |
| 90 |
case <-s.done: |
| 91 |
return |
| 92 |
} |
| 93 |
} |
| 94 |
o.OnMasterChanged(nil) |
| 95 |
}() |
| 96 |
} else { |
| 97 |
log.Warningf("detect called with a nil master change listener") |
| 98 |
} |
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
func (s *Standalone) Done() <-chan struct{} { |
| 103 |
return s.done |
| 104 |
} |
| 105 |
|
| 106 |
func (s *Standalone) Cancel() { |
| 107 |
s.cancelOnce.Do(func() { close(s.done) }) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
func (s *Standalone) _poller(pf fetcherFunc) { |
| 117 |
defer func() { |
| 118 |
defer s.Cancel() |
| 119 |
log.Warning("shutting down standalone master detection") |
| 120 |
}() |
| 121 |
if s.initial == nil { |
| 122 |
log.Errorf("aborting master poller since initial master info is nil") |
| 123 |
return |
| 124 |
} |
| 125 |
addr := s.initial.GetHostname() |
| 126 |
if len(addr) == 0 { |
| 127 |
if s.initial.GetIp() == 0 { |
| 128 |
log.Warningf("aborted mater poller since initial master info has no host") |
| 129 |
return |
| 130 |
} |
| 131 |
ip := make([]byte, 4) |
| 132 |
binary.BigEndian.PutUint32(ip, s.initial.GetIp()) |
| 133 |
addr = net.IP(ip).To4().String() |
| 134 |
} |
| 135 |
port := uint32(s.assumedMasterPort) |
| 136 |
if s.initial.Port != nil && *s.initial.Port != 0 { |
| 137 |
port = *s.initial.Port |
| 138 |
} |
| 139 |
addr = net.JoinHostPort(addr, strconv.Itoa(int(port))) |
| 140 |
log.V(1).Infof("polling for master leadership at '%v'", addr) |
| 141 |
var lastpid *upid.UPID |
| 142 |
for { |
| 143 |
startedAt := time.Now() |
| 144 |
ctx, cancel := context.WithTimeout(context.Background(), s.leaderSyncInterval) |
| 145 |
if pid, err := pf(ctx, addr); err == nil { |
| 146 |
if !pid.Equal(lastpid) { |
| 147 |
log.V(2).Infof("detected leadership change from '%v' to '%v'", lastpid, pid) |
| 148 |
lastpid = pid |
| 149 |
elapsed := time.Now().Sub(startedAt) |
| 150 |
mi := CreateMasterInfo(pid) |
| 151 |
select { |
| 152 |
case s.ch <- mi: |
| 153 |
case <-time.After(s.leaderSyncInterval - elapsed): |
| 154 |
|
| 155 |
goto continuePolling |
| 156 |
case <-s.done: |
| 157 |
cancel() |
| 158 |
return |
| 159 |
} |
| 160 |
} else { |
| 161 |
log.V(2).Infof("no change to master leadership: '%v'", lastpid) |
| 162 |
} |
| 163 |
} else if err == context.DeadlineExceeded { |
| 164 |
if lastpid != nil { |
| 165 |
lastpid = nil |
| 166 |
select { |
| 167 |
case s.ch <- nil: |
| 168 |
case <-s.done: |
| 169 |
return |
| 170 |
} |
| 171 |
} |
| 172 |
goto continuePolling |
| 173 |
} else { |
| 174 |
select { |
| 175 |
case <-s.done: |
| 176 |
cancel() |
| 177 |
return |
| 178 |
default: |
| 179 |
if err != context.Canceled { |
| 180 |
log.Error(err) |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
if remaining := s.leaderSyncInterval - time.Now().Sub(startedAt); remaining > 0 { |
| 185 |
log.V(3).Infof("master leader poller sleeping for %v", remaining) |
| 186 |
time.Sleep(remaining) |
| 187 |
} |
| 188 |
continuePolling: |
| 189 |
cancel() |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
func (s *Standalone) _fetchPid(ctx context.Context, address string) (*upid.UPID, error) { |
| 195 |
|
| 196 |
uri := fmt.Sprintf("http://%s/state", address) |
| 197 |
req, err := http.NewRequest("GET", uri, nil) |
| 198 |
if err != nil { |
| 199 |
return nil, err |
| 200 |
} |
| 201 |
var pid *upid.UPID |
| 202 |
err = s.httpDo(ctx, req, func(res *http.Response, err error) error { |
| 203 |
if err != nil { |
| 204 |
return err |
| 205 |
} |
| 206 |
defer res.Body.Close() |
| 207 |
if res.StatusCode != 200 { |
| 208 |
return fmt.Errorf("HTTP request failed with code %d: %v", res.StatusCode, res.Status) |
| 209 |
} |
| 210 |
blob, err1 := ioutil.ReadAll(res.Body) |
| 211 |
if err1 != nil { |
| 212 |
return err1 |
| 213 |
} |
| 214 |
log.V(3).Infof("Got mesos state, content length %v", len(blob)) |
| 215 |
type State struct { |
| 216 |
Leader string `json:"leader"` |
| 217 |
} |
| 218 |
state := &State{} |
| 219 |
err = json.Unmarshal(blob, state) |
| 220 |
if err != nil { |
| 221 |
return err |
| 222 |
} |
| 223 |
pid, err = upid.Parse(state.Leader) |
| 224 |
return err |
| 225 |
}) |
| 226 |
return pid, err |
| 227 |
} |
| 228 |
|
| 229 |
type responseHandler func(*http.Response, error) error |
| 230 |
|
| 231 |
|
| 232 |
func (s *Standalone) httpDo(ctx context.Context, req *http.Request, f responseHandler) error { |
| 233 |
|
| 234 |
ch := make(chan error, 1) |
| 235 |
go func() { ch <- f(s.client.Do(req)) }() |
| 236 |
select { |
| 237 |
case <-ctx.Done(): |
| 238 |
s.tr.CancelRequest(req) |
| 239 |
<-ch |
| 240 |
return ctx.Err() |
| 241 |
case err := <-ch: |
| 242 |
return err |
| 243 |
} |
| 244 |
} |