Skip to content

Commit

Permalink
Fix Metric struct and connect all pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
jsousa02 committed May 27, 2024
1 parent b255564 commit b12baf0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
4 changes: 2 additions & 2 deletions proj/app/common/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Metrics struct {
MemStats struct {
Stats struct {
Cache float64 `json:"cache"`
}
} `json:"stats"`
Usage float64 `json:"usage"`
Limit float64 `json:"limit"`
} `json:"memory_stats"`
Expand All @@ -25,7 +25,7 @@ type Metrics struct {
TotalUsage float64 `json:"total_usage"`
} `json:"cpu_usage"`
SystemCPUUsage float64 `json:"system_cpu_usage"`
}
} `json:"precpu_stats"`
}

// Holds relevant metrics of a container
Expand Down
4 changes: 2 additions & 2 deletions proj/app/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
period: 10s
period: 3s

metrics:
cpu:
threshold: 20
memory:
threshold : 50
threshold : 1
1 change: 0 additions & 1 deletion proj/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func main() {
s.Wait()

for _, stat := range stats {
log.Println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
log.Println(stat)
// Convert stat to JSON
output, errParse := json.Marshal(stat)
Expand Down
51 changes: 38 additions & 13 deletions proj/app/scaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"context"
"errors"
"fmt"
"time"
"math"
"strconv"

"log"
"strings"
Expand All @@ -30,24 +31,48 @@ func Run(s *sync.WaitGroup, config *Config, stats []*Stats, ct *context.Context)
ctx, cancel := context.WithCancel(*ct)
defer cancel()

runningContainers, err := utils.GetContainersOnNetwork(utils.GRS_NETWORK, apiClient, &ctx)
if err != nil {
log.Fatalln("In scaler.Run: Failed to get containers on GRS network")
}

runningReplicas := len(*runningContainers) - 1 // remove load balancer

for _, stat := range stats {
fmt.Println("Printing stats received from Metric Collector")
utils.PrettyPrint(stat)
}

startErr := startContainer(utils.GRS_IMAGE, apiClient, &ctx)
if startErr != nil {
log.Fatalln(startErr.Error())
cancel()
}
memUsage, convErr := strconv.ParseFloat(strings.Split(stat.MemoryUsage, "%")[0], 32)

if convErr != nil {
fmt.Println("Error converting string to float, skipping...")
continue
}

time.Sleep(3 * time.Second)
memThreshold, convErr := strconv.ParseFloat(config.Metrics.Memory.Threshold, 32)
if convErr != nil {
fmt.Println("Error converting memory threshold to float, skipping...")
continue
}

stopErr := stopContainer(apiClient, &ctx)
if stopErr != nil {
fmt.Println(stopErr.Error())
}
cpuUsage, convErr := strconv.ParseFloat(strings.Split(stat.CPUUsage, "%")[0], 32)

cpuThreshold, convErr := strconv.ParseFloat(config.Metrics.CPU.Threshold, 32)

desiredReplicas := max(math.Ceil(float64(runningReplicas) * (memUsage / memThreshold)), math.Ceil(float64(runningReplicas) * (cpuUsage / cpuThreshold)))

fmt.Println(desiredReplicas, runningReplicas, memUsage, memThreshold)

if desiredReplicas > float64(runningReplicas) {
startContainer(utils.GRS_IMAGE, apiClient, &ctx)
break
}

if desiredReplicas < float64(runningReplicas) {
stopContainer(apiClient, &ctx)
break
}
}

s.Done()
}

Expand Down
7 changes: 7 additions & 0 deletions proj/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ services:
- "8080:80"
volumes:
- ./load_balancer/config.conf:/etc/nginx/nginx.conf
networks:
grs-net:
ipv4_address: 172.19.0.2

web-service:
image: grs
container_name: default_server
networks:
grs-net:
ipv4_address: 172.19.0.3
Expand Down
2 changes: 1 addition & 1 deletion proj/load_balancer/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ events {
}
http {
upstream load_balancer {
server modest_mcnulty:80;
server default_server:80;
}
server {
listen 80;
Expand Down

0 comments on commit b12baf0

Please sign in to comment.