Skip to content

Commit b5e656c

Browse files
Registry: Add CloudLatency
1 parent 4a93b53 commit b5e656c

5 files changed

Lines changed: 72 additions & 0 deletions

File tree

cmd/serverledge/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func main() {
9393
if err != nil {
9494
log.Fatal(err)
9595
}
96+
registration.InitCloudMonitoring()
9697
}
9798

9899
api.StartAPIServer(e)

internal/config/keys.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const REG_NEARBY_INTERVAL = "registry.nearby.interval"
5050
// long period for general monitoring inside the area
5151
const REG_MONITORING_INTERVAL = "registry.monitoring.interval"
5252

53+
// time interval for monitoring cloud latency
54+
const REG_CLOUD_MONITORING_INTERVAL = "registry.monitoring.cloud.interval"
55+
5356
// registration TTL in seconds
5457
const REGISTRATION_TTL = "registry.ttl"
5558

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package registration
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net"
7+
"time"
8+
9+
"github.com/serverledge-faas/serverledge/internal/config"
10+
)
11+
12+
func InitCloudMonitoring() {
13+
Reg.CloudLatency = make(map[string]float64)
14+
//complete monitoring phase at startup
15+
monitorCloudLatency()
16+
go runCloudMonitor()
17+
}
18+
19+
func runCloudMonitor() {
20+
cloudMonitorTicker := time.NewTicker(time.Duration(config.GetInt(config.REG_CLOUD_MONITORING_INTERVAL, 60)) * time.Second)
21+
for {
22+
select {
23+
case <-cloudMonitorTicker.C:
24+
monitorCloudLatency()
25+
}
26+
}
27+
}
28+
29+
func tcpLatency(hostAndPort string) (float64, error) {
30+
start := time.Now()
31+
conn, err := net.DialTimeout("tcp", hostAndPort, 3*time.Second)
32+
if err != nil {
33+
return 0, err
34+
}
35+
_ = conn.Close()
36+
return float64(time.Since(start).Milliseconds()), nil
37+
}
38+
39+
func monitorCloudLatency() {
40+
region := config.GetString(config.REGISTRY_AREA, "ROME")
41+
cloudNodes, err := GetCloudNodes(region)
42+
if err != nil {
43+
log.Println(fmt.Printf("Error while retrieving cloud nodes: %v", err))
44+
return
45+
}
46+
47+
if len(cloudNodes) == 0 {
48+
cloudAddress := config.GetString(config.CLOUD_URL, "")
49+
if cloudAddress != "" {
50+
cloudNodes["from-config"] = cloudAddress
51+
}
52+
}
53+
54+
for _, url := range cloudNodes {
55+
// TODO: retrieve from offloadingLatency?
56+
latency, latencyError := tcpLatency(url)
57+
if latencyError != nil {
58+
log.Printf("Unable to compute latency towards %v: %v\n", url, latencyError)
59+
delete(Reg.CloudLatency, url)
60+
continue
61+
}
62+
Reg.CloudLatency[url] = latency
63+
}
64+
65+
log.Printf("Map of cloud node latency at the end of monitoring: %v\n", Reg.CloudLatency)
66+
}

internal/registration/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Registry struct {
1818
RwMtx trylock.Mutex
1919
NearbyServersMap map[string]*StatusInformation
2020
serversMap map[string]*StatusInformation
21+
CloudLatency map[string]float64
2122
etcdCh chan bool
2223
}
2324

internal/test/main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func testStartServerledge(isInCloud bool, outboundIp string) (*registration.Regi
7878
if err != nil {
7979
log.Fatal(err)
8080
}
81+
registration.InitCloudMonitoring()
8182
}
8283
// needed: if you call a function composition, internally will invoke each function
8384
go api.StartAPIServer(e)

0 commit comments

Comments
 (0)