-
Notifications
You must be signed in to change notification settings - Fork 143
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
Replace timer with ticker in download progress reporter #3548
Merged
ycombinator
merged 3 commits into
elastic:main
from
ycombinator:bugfix-upgrade-progress-reporter
Oct 10, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
changelog/fragments/1696530758-bugfix-upgrade-progress-reporter.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Kind can be one of: | ||
# - breaking-change: a change to previously-documented behavior | ||
# - deprecation: functionality that is being removed in a later release | ||
# - bug-fix: fixes a problem in a previous version | ||
# - enhancement: extends functionality but does not break or fix existing behavior | ||
# - feature: new functionality | ||
# - known-issue: problems that we are aware of in a given version | ||
# - security: impacts on the security of a product or a user’s deployment. | ||
# - upgrade: important information for someone upgrading from a prior version | ||
# - other: does not fit into any of the other categories | ||
kind: bug-fix | ||
|
||
# Change summary; a 80ish characters long description of the change. | ||
summary: Periodically report progress of Elastic Agent artifact download during upgrade | ||
|
||
# Long description; in case the summary is not enough to describe the change | ||
# this field accommodate a description without length limits. | ||
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. | ||
#description: | ||
|
||
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc. | ||
component: elastic-agent | ||
|
||
# PR URL; optional; the PR number that added the changeset. | ||
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. | ||
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. | ||
# Please provide it if you are adding a fragment for a different PR. | ||
pr: https://github.com/elastic/elastic-agent/pull/3548 | ||
|
||
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). | ||
# If not present is automatically filled by the tooling with the issue linked to the PR number. | ||
#issue: https://github.com/owner/repo/1234 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,11 +75,11 @@ func TestDownloadBodyError(t *testing.T) { | |
} | ||
|
||
func TestDownloadLogProgressWithLength(t *testing.T) { | ||
fileSize := 100 * units.MiB | ||
fileSize := 100 * units.MB | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just to make the math easier. |
||
chunks := 100 | ||
chunk := make([]byte, fileSize/chunks) | ||
delayBetweenChunks := 10 * time.Millisecond | ||
totalTime := time.Duration(chunks) * (delayBetweenChunks + 1*time.Millisecond) | ||
totalTime := time.Duration(chunks) * delayBetweenChunks | ||
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Length", strconv.Itoa(fileSize)) | ||
|
@@ -120,24 +120,32 @@ func TestDownloadLogProgressWithLength(t *testing.T) { | |
log.lock.RLock() | ||
defer log.lock.RUnlock() | ||
|
||
// 2 files are downloaded so 4 log messages are expected in the info level and only the complete is over the warn | ||
// window as 2 log messages for warn. | ||
require.Len(t, log.info, 4) | ||
assert.Equal(t, log.info[0].record, "download progress from %s is %s/%s (%.2f%% complete) @ %sps") | ||
assert.Equal(t, log.info[1].record, "download from %s completed in %s @ %sps") | ||
assert.Equal(t, log.info[2].record, "download progress from %s is %s/%s (%.2f%% complete) @ %sps") | ||
assert.Equal(t, log.info[3].record, "download from %s completed in %s @ %sps") | ||
require.Len(t, log.warn, 2) | ||
assert.Equal(t, log.warn[0].record, "download from %s completed in %s @ %sps") | ||
assert.Equal(t, log.warn[1].record, "download from %s completed in %s @ %sps") | ||
expectedProgressMsg := "download progress from %s is %s/%s (%.2f%% complete) @ %sps" | ||
|
||
// Two files are downloaded. Each file is being downloaded in 100 chunks with a delay of 10ms between chunks. The | ||
// expected time to download is, therefore, 100 * 10ms = 1000ms. In reality, the actual download time will be a bit | ||
// more than 1000ms because some time is spent downloading the chunk, in between inter-chunk delays. | ||
// Reporting happens every 0.05 * 1000ms = 50ms. We expect there to be as many log messages at that INFO level as | ||
// the actual total download time / 50ms, for each file. That works out to at least 1000ms / 50ms = 20 INFO log | ||
// messages, for each file, about its download progress. Additionally, we should expect 1 INFO log message, for | ||
// each file, about the download completing. | ||
assertLogs(t, log.info, 20, expectedProgressMsg) | ||
|
||
// By similar math as above, since the download of each file is expected to take 1000ms, and the progress logger | ||
// starts issuing WARN messages once the download has taken more than 75% of the expected time, | ||
// we should see warning messages for at least the last 250 seconds of the download. Given that | ||
// reporting happens every 50 seconds, we should see at least 250s / 50s = 5 WARN log messages, for each file, | ||
// about its download progress. Additionally, we should expect 1 WARN message, for each file, about the download | ||
// completing. | ||
assertLogs(t, log.warn, 5, expectedProgressMsg) | ||
} | ||
|
||
func TestDownloadLogProgressWithoutLength(t *testing.T) { | ||
fileSize := 100 * units.MiB | ||
chunks := 100 | ||
chunk := make([]byte, fileSize/chunks) | ||
delayBetweenChunks := 10 * time.Millisecond | ||
totalTime := time.Duration(chunks) * (delayBetweenChunks + 1*time.Millisecond) | ||
totalTime := time.Duration(chunks) * delayBetweenChunks | ||
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
|
@@ -177,16 +185,24 @@ func TestDownloadLogProgressWithoutLength(t *testing.T) { | |
log.lock.RLock() | ||
defer log.lock.RUnlock() | ||
|
||
// 2 files are downloaded so 4 log messages are expected in the info level and only the complete is over the warn | ||
// window as 2 log messages for warn. | ||
require.Len(t, log.info, 4) | ||
assert.Equal(t, log.info[0].record, "download progress from %s has fetched %s @ %sps") | ||
assert.Equal(t, log.info[1].record, "download from %s completed in %s @ %sps") | ||
assert.Equal(t, log.info[2].record, "download progress from %s has fetched %s @ %sps") | ||
assert.Equal(t, log.info[3].record, "download from %s completed in %s @ %sps") | ||
require.Len(t, log.warn, 2) | ||
assert.Equal(t, log.warn[0].record, "download from %s completed in %s @ %sps") | ||
assert.Equal(t, log.warn[1].record, "download from %s completed in %s @ %sps") | ||
expectedProgressMsg := "download progress from %s has fetched %s @ %sps" | ||
|
||
// Two files are downloaded. Each file is being downloaded in 100 chunks with a delay of 10ms between chunks. The | ||
// expected time to download is, therefore, 100 * 10ms = 1000ms. In reality, the actual download time will be a bit | ||
// more than 1000ms because some time is spent downloading the chunk, in between inter-chunk delays. | ||
// Reporting happens every 0.05 * 1000ms = 50ms. We expect there to be as many log messages at that INFO level as | ||
// the actual total download time / 50ms, for each file. That works out to at least 1000ms / 50ms = 20 INFO log | ||
// messages, for each file, about its download progress. Additionally, we should expect 1 INFO log message, for | ||
// each file, about the download completing. | ||
assertLogs(t, log.info, 20, expectedProgressMsg) | ||
|
||
// By similar math as above, since the download of each file is expected to take 1000ms, and the progress logger | ||
// starts issuing WARN messages once the download has taken more than 75% of the expected time, | ||
// we should see warning messages for at least the last 250 seconds of the download. Given that | ||
// reporting happens every 50 seconds, we should see at least 250s / 50s = 5 WARN log messages, for each file, | ||
// about its download progress. Additionally, we should expect 1 WARN message, for each file, about the download | ||
// completing. | ||
assertLogs(t, log.warn, 5, expectedProgressMsg) | ||
} | ||
|
||
type logMessage struct { | ||
|
@@ -227,3 +243,31 @@ func containsMessage(logs []logMessage, msg string) bool { | |
} | ||
return false | ||
} | ||
func assertLogs(t *testing.T, logs []logMessage, minExpectedProgressLogs int, expectedProgressMsg string) { | ||
t.Helper() | ||
|
||
// Verify that we've logged at least minExpectedProgressLogs (about download progress) + 1 log | ||
// message (about download completion), for each of the two files being downloaded. | ||
require.GreaterOrEqual(t, len(logs), (minExpectedProgressLogs+1)*2) | ||
|
||
// Verify that the first minExpectedProgressLogs messages are about the download progress (for the first file). | ||
i := 0 | ||
for ; i < minExpectedProgressLogs; i++ { | ||
assert.Equal(t, logs[i].record, expectedProgressMsg) | ||
} | ||
|
||
// Find the next message that's about the download being completed (for the first file). | ||
found := false | ||
for ; i < len(logs) && !found; i++ { | ||
found = logs[i].record == "download from %s completed in %s @ %sps" | ||
} | ||
assert.True(t, found) | ||
|
||
// Verify that the next minExpectedProgressLogs messages are about the download progress (for the second file). | ||
for j := 0; j < minExpectedProgressLogs; j++ { | ||
assert.Equal(t, logs[i+j].record, expectedProgressMsg) | ||
} | ||
|
||
// Verify that the last message is about the download being completed (for the second file). | ||
assert.Equal(t, logs[len(logs)-1].record, "download from %s completed in %s @ %sps") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is unrelated to this PR per se. But I noticed while working on this PR that this field was never being used, so I'm deleting it as part of this PR.