Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement timeout for whole metrics send function #1400

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ jobs:
tar -czvf "../releases/livepeer-catalyst-api-${GOOS}-${GOARCH}.tar.gz" .

- name: Upload artifacts for cutting release
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: release-artifacts
name: release-artifacts-${{ matrix.platform.name }}-${{ matrix.arch }}
path: releases/

upload:
Expand All @@ -107,10 +107,11 @@ jobs:
- build
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-artifacts
pattern: release-artifacts-*
path: releases/
merge-multiple: true

- name: Generate sha256 checksum and gpg signatures for release artifacts
uses: livepeer/action-gh-checksum-and-gpg-sign@latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
make generate integration-test

- name: Upload logs
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: failure()
with:
name: cucumber-logs
Expand Down
104 changes: 61 additions & 43 deletions balancer/catabalancer/catalyst_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,58 +421,76 @@ func StartMetricSending(nodeName string, latitude float64, longitude float64, mi
ticker := time.NewTicker(updateNodeStatsEvery)
go func() {
for range ticker.C {
start := time.Now()
sysusage, err := GetSystemUsage()
if err != nil {
log.LogNoRequestID("catabalancer failed to get sys usage", "err", err)
done := make(chan bool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the more "golang way" of doing the same would be passing context with timeout into sendMetrics(). So you could have something like this:

ctx, cancel := context.WithTimeout(context.Background(), timeout)
sendMetrics(ctx, nodeName, latitude, longitude, mist, nodeStatsDB)

And then inside sendMetrics() to have

GetSystemUsage(ctx)

And so on. The good thing about this approach would be that in case of the timeout you cancel all system and network calls.


// use a separate goroutine to implement a timeout for the sendMetrics func
go func() {
sendMetrics(nodeName, latitude, longitude, mist, nodeStatsDB)
done <- true
}()

select {
case <-done:
continue
case <-time.After(updateNodeStatsEvery):
log.LogNoRequestID("catabalancer send metrics timed out")
continue
}
}
}()
}

event := NodeUpdateEvent{
Resource: "nodeUpdate",
NodeID: nodeName,
NodeMetrics: NodeMetrics{
CPUUsagePercentage: sysusage.CPUUsagePercentage,
RAMUsagePercentage: sysusage.RAMUsagePercentage,
BandwidthUsagePercentage: sysusage.BWUsagePercentage,
LoadAvg: sysusage.LoadAvg.Load5Min,
GeoLatitude: latitude,
GeoLongitude: longitude,
Timestamp: time.Now(),
},
}
func sendMetrics(nodeName string, latitude float64, longitude float64, mist clients.MistAPIClient, nodeStatsDB *sql.DB) {
start := time.Now()
sysusage, err := GetSystemUsage()
if err != nil {
log.LogNoRequestID("catabalancer failed to get sys usage", "err", err)
return
}

if mist != nil {
mistState, err := mist.GetState()
if err != nil {
log.LogNoRequestID("catabalancer failed to get mist state", "err", err)
continue
}

var nonIngestStreams, ingestStreams []string
for streamID := range mistState.ActiveStreams {
if mistState.IsIngestStream(streamID) {
ingestStreams = append(ingestStreams, streamID)
} else {
nonIngestStreams = append(nonIngestStreams, streamID)
}
}
event.SetStreams(nonIngestStreams, ingestStreams)
}
event := NodeUpdateEvent{
Resource: "nodeUpdate",
NodeID: nodeName,
NodeMetrics: NodeMetrics{
CPUUsagePercentage: sysusage.CPUUsagePercentage,
RAMUsagePercentage: sysusage.RAMUsagePercentage,
BandwidthUsagePercentage: sysusage.BWUsagePercentage,
LoadAvg: sysusage.LoadAvg.Load5Min,
GeoLatitude: latitude,
GeoLongitude: longitude,
Timestamp: time.Now(),
},
}

payload, err := json.Marshal(event)
if err != nil {
log.LogNoRequestID("catabalancer failed to marhsal node update", "err", err)
continue
}
sendMetrics(nodeStatsDB, nodeName, payload)
if mist != nil {
mistState, err := mist.GetState()
if err != nil {
log.LogNoRequestID("catabalancer failed to get mist state", "err", err)
return
}

metrics.Metrics.CatabalancerSendMetricDurationSec.Observe(time.Since(start).Seconds())
var nonIngestStreams, ingestStreams []string
for streamID := range mistState.ActiveStreams {
if mistState.IsIngestStream(streamID) {
ingestStreams = append(ingestStreams, streamID)
} else {
nonIngestStreams = append(nonIngestStreams, streamID)
}
}
}()
event.SetStreams(nonIngestStreams, ingestStreams)
}

payload, err := json.Marshal(event)
if err != nil {
log.LogNoRequestID("catabalancer failed to marhsal node update", "err", err)
return
}
sendMetricsToDB(nodeStatsDB, nodeName, payload)

metrics.Metrics.CatabalancerSendMetricDurationSec.Observe(time.Since(start).Seconds())
}

func sendMetrics(nodeStatsDB *sql.DB, nodeName string, payload []byte) {
func sendMetricsToDB(nodeStatsDB *sql.DB, nodeName string, payload []byte) {
start := time.Now()
queryContext, cancel := context.WithTimeout(context.Background(), updateNodeStatsEvery)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion balancer/catabalancer/sysstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func GetBandwidthUsage() (float64, error) {

// Parse json output
var data BandwidthData
err = json.Unmarshal([]byte(output), &data)
err = json.Unmarshal(output, &data)
if err != nil {
return -1, err
}
Expand Down
Loading