Show status of global announce server (fixes #71)

This commit is contained in:
Jakob Borg 2014-04-16 17:36:09 +02:00
parent 31bfd8c039
commit b802cb1e36
4 changed files with 69 additions and 36 deletions

View File

@ -168,6 +168,9 @@ func restGetSystem(w http.ResponseWriter) {
res["goroutines"] = runtime.NumGoroutine() res["goroutines"] = runtime.NumGoroutine()
res["alloc"] = m.Alloc res["alloc"] = m.Alloc
res["sys"] = m.Sys res["sys"] = m.Sys
if discoverer != nil {
res["extAnnounceOK"] = discoverer.ExtAnnounceOK()
}
cpuUsageLock.RLock() cpuUsageLock.RLock()
var cpusum float64 var cpusum float64
for _, p := range cpuUsagePercent { for _, p := range cpuUsagePercent {

View File

@ -33,6 +33,7 @@ var (
confDir string confDir string
rateBucket *ratelimit.Bucket rateBucket *ratelimit.Bucket
stop = make(chan bool) stop = make(chan bool)
discoverer *discover.Discoverer
) )
const ( const (
@ -228,8 +229,8 @@ func main() {
m.SaveIndexes(confDir) m.SaveIndexes(confDir)
// Routine to connect out to configured nodes // Routine to connect out to configured nodes
disc := discovery() discoverer = discovery()
go listenConnect(myID, disc, m, tlsCfg) go listenConnect(myID, m, tlsCfg)
for _, repo := range cfg.Repositories { for _, repo := range cfg.Repositories {
// Routine to pull blocks from other nodes to synchronize the local // Routine to pull blocks from other nodes to synchronize the local
@ -337,7 +338,7 @@ func saveConfig() {
saveConfigCh <- struct{}{} saveConfigCh <- struct{}{}
} }
func listenConnect(myID string, disc *discover.Discoverer, m *Model, tlsCfg *tls.Config) { func listenConnect(myID string, m *Model, tlsCfg *tls.Config) {
var conns = make(chan *tls.Conn) var conns = make(chan *tls.Conn)
// Listen // Listen
@ -389,8 +390,8 @@ func listenConnect(myID string, disc *discover.Discoverer, m *Model, tlsCfg *tls
var addrs []string var addrs []string
for _, addr := range nodeCfg.Addresses { for _, addr := range nodeCfg.Addresses {
if addr == "dynamic" { if addr == "dynamic" {
if disc != nil { if discoverer != nil {
t := disc.Lookup(nodeCfg.NodeID) t := discoverer.Lookup(nodeCfg.NodeID)
if len(t) == 0 { if len(t) == 0 {
continue continue
} }

View File

@ -19,16 +19,18 @@ const (
) )
type Discoverer struct { type Discoverer struct {
MyID string myID string
ListenAddresses []string listenAddrs []string
BroadcastIntv time.Duration localBcastIntv time.Duration
ExtBroadcastIntv time.Duration globalBcastIntv time.Duration
beacon *mc.Beacon beacon *mc.Beacon
registry map[string][]string registry map[string][]string
registryLock sync.RWMutex registryLock sync.RWMutex
extServer string extServer string
localBroadcastTick <-chan time.Time localBcastTick <-chan time.Time
forcedBroadcastTick chan time.Time forcedBcastTick chan time.Time
extAnnounceOK bool
extAnnounceOKmut sync.Mutex
} }
var ( var (
@ -42,24 +44,22 @@ const maxErrors = 30
func NewDiscoverer(id string, addresses []string) (*Discoverer, error) { func NewDiscoverer(id string, addresses []string) (*Discoverer, error) {
disc := &Discoverer{ disc := &Discoverer{
MyID: id, myID: id,
ListenAddresses: addresses, listenAddrs: addresses,
BroadcastIntv: 30 * time.Second, localBcastIntv: 30 * time.Second,
ExtBroadcastIntv: 1800 * time.Second, globalBcastIntv: 1800 * time.Second,
beacon: mc.NewBeacon("239.21.0.25", 21025), beacon: mc.NewBeacon("239.21.0.25", 21025),
registry: make(map[string][]string), registry: make(map[string][]string),
} }
// Receive announcements sent to the local multicast group.
go disc.recvAnnouncements() go disc.recvAnnouncements()
return disc, nil return disc, nil
} }
func (d *Discoverer) StartLocal() { func (d *Discoverer) StartLocal() {
d.localBroadcastTick = time.Tick(d.BroadcastIntv) d.localBcastTick = time.Tick(d.localBcastIntv)
d.forcedBroadcastTick = make(chan time.Time) d.forcedBcastTick = make(chan time.Time)
go d.sendLocalAnnouncements() go d.sendLocalAnnouncements()
} }
@ -68,9 +68,15 @@ func (d *Discoverer) StartGlobal(server string) {
go d.sendExternalAnnouncements() go d.sendExternalAnnouncements()
} }
func (d *Discoverer) ExtAnnounceOK() bool {
d.extAnnounceOKmut.Lock()
defer d.extAnnounceOKmut.Unlock()
return d.extAnnounceOK
}
func (d *Discoverer) announcementPkt() []byte { func (d *Discoverer) announcementPkt() []byte {
var addrs []Address var addrs []Address
for _, astr := range d.ListenAddresses { for _, astr := range d.listenAddrs {
addr, err := net.ResolveTCPAddr("tcp", astr) addr, err := net.ResolveTCPAddr("tcp", astr)
if err != nil { if err != nil {
log.Printf("discover/announcement: %v: not announcing %s", err, astr) log.Printf("discover/announcement: %v: not announcing %s", err, astr)
@ -88,7 +94,7 @@ func (d *Discoverer) announcementPkt() []byte {
} }
var pkt = AnnounceV2{ var pkt = AnnounceV2{
Magic: AnnouncementMagicV2, Magic: AnnouncementMagicV2,
NodeID: d.MyID, NodeID: d.myID,
Addresses: addrs, Addresses: addrs,
} }
return pkt.MarshalXDR() return pkt.MarshalXDR()
@ -101,8 +107,8 @@ func (d *Discoverer) sendLocalAnnouncements() {
d.beacon.Send(buf) d.beacon.Send(buf)
select { select {
case <-d.localBroadcastTick: case <-d.localBcastTick:
case <-d.forcedBroadcastTick: case <-d.forcedBcastTick:
} }
} }
} }
@ -124,22 +130,40 @@ func (d *Discoverer) sendExternalAnnouncements() {
var errCounter = 0 var errCounter = 0
for errCounter < maxErrors { for errCounter < maxErrors {
var ok bool
if debug { if debug {
dlog.Printf("send announcement -> %v\n%s", remote, hex.Dump(buf)) dlog.Printf("send announcement -> %v\n%s", remote, hex.Dump(buf))
} }
_, err = conn.WriteTo(buf, remote) _, err = conn.WriteTo(buf, remote)
if err != nil { if err != nil {
log.Println("discover/write: warning:", err) log.Println("discover/write: warning:", err)
errCounter++ errCounter++
ok = false
} else { } else {
errCounter = 0 errCounter = 0
}
if debug { // Verify that the announce server responds positively for our node ID
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
res := d.externalLookup(d.MyID) res := d.externalLookup(d.myID)
dlog.Println("external lookup check:", res) if debug {
dlog.Println("external lookup check:", res)
}
ok = len(res) > 0
}
d.extAnnounceOKmut.Lock()
d.extAnnounceOK = ok
d.extAnnounceOKmut.Unlock()
if ok {
time.Sleep(d.globalBcastIntv)
} else {
time.Sleep(60 * time.Second)
} }
time.Sleep(d.ExtBroadcastIntv)
} }
log.Printf("discover/write: %v: stopping due to too many errors: %v", remote, err) log.Printf("discover/write: %v: stopping due to too many errors: %v", remote, err)
} }
@ -162,7 +186,7 @@ func (d *Discoverer) recvAnnouncements() {
dlog.Printf("parsed announcement: %#v", pkt) dlog.Printf("parsed announcement: %#v", pkt)
} }
if pkt.NodeID != d.MyID { if pkt.NodeID != d.myID {
var addrs []string var addrs []string
for _, a := range pkt.Addresses { for _, a := range pkt.Addresses {
var nodeAddr string var nodeAddr string
@ -182,7 +206,7 @@ func (d *Discoverer) recvAnnouncements() {
_, seen := d.registry[pkt.NodeID] _, seen := d.registry[pkt.NodeID]
if !seen { if !seen {
select { select {
case d.forcedBroadcastTick <- time.Now(): case d.forcedBcastTick <- time.Now():
} }
} }
d.registry[pkt.NodeID] = addrs d.registry[pkt.NodeID] = addrs

View File

@ -248,6 +248,11 @@
</div> </div>
</li> </li>
<li> <li>
<div ng-if="system.extAnnounceOK != undefined" class="li-column" title="Global announce server">
<span class="text-muted glyphicon glyphicon-bullhorn"></span>
<span class="data text-success" ng-if="system.extAnnounceOK">Online</span>
<span class="data text-danger" ng-if="!system.extAnnounceOK">Offline</span>
</div>
<div class="li-column"> <div class="li-column">
<span class="text-muted glyphicon glyphicon-cog"></span> <span class="text-muted glyphicon glyphicon-cog"></span>
<span class="data"><a href="" ng-click="editNode(nodeCfg)"><span class="glyphicon glyphicon-pencil"></span> Edit</a></span> <span class="data"><a href="" ng-click="editNode(nodeCfg)"><span class="glyphicon glyphicon-pencil"></span> Edit</a></span>