Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Apr 2, 2024
1 parent f559940 commit 2855675
Showing 1 changed file with 94 additions and 44 deletions.
138 changes: 94 additions & 44 deletions tests/server/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,56 +927,106 @@ func TestPreparingProgress(t *testing.T) {
for i := 0; i < 100; i++ {
tests.MustPutRegion(re, cluster, uint64(i+1), uint64(i)%3+1, []byte(fmt.Sprintf("%20d", i)), []byte(fmt.Sprintf("%20d", i+1)), core.SetApproximateSize(10))
}
// no store preparing
output := sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?action=preparing", http.MethodGet, http.StatusNotFound)
re.Contains((string(output)), "no progress found for the action")
output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?id=4", http.MethodGet, http.StatusNotFound)
re.Contains((string(output)), "no progress found for the given store ID")

// is not prepared
time.Sleep(2 * time.Second)
output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?action=preparing", http.MethodGet, http.StatusNotFound)
re.Contains((string(output)), "no progress found for the action")
output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?id=4", http.MethodGet, http.StatusNotFound)
re.Contains((string(output)), "no progress found for the given store ID")
testutil.Eventually(re, func() bool {
if leader.GetRaftCluster().IsPrepared() {
return true
}
// no store preparing
output := sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?action=preparing", http.MethodGet, http.StatusNotFound)
re.Contains((string(output)), "no progress found for the action")
output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?id=4", http.MethodGet, http.StatusNotFound)
re.Contains((string(output)), "no progress found for the given store ID")

// is not prepared
output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?action=preparing", http.MethodGet, http.StatusNotFound)
re.Contains(string(output), "no progress found for the action")
output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?id=4", http.MethodGet, http.StatusNotFound)
re.Contains(string(output), "no progress found for the given store ID")
return false
})

// size is not changed.
leader.GetRaftCluster().SetPrepared()
time.Sleep(2 * time.Second)
output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?action=preparing", http.MethodGet, http.StatusOK)
var p api.Progress
re.NoError(json.Unmarshal(output, &p))
re.Equal("preparing", p.Action)
re.Equal(0.0, p.Progress)
re.Equal(0.0, p.CurrentSpeed)
re.Equal(math.MaxFloat64, p.LeftSeconds)
testutil.Eventually(re, func() bool {
// wait for cluster started
url := leader.GetAddr() + "/pd/api/v1/stores/progress?action=preparing"
req, _ := http.NewRequest(http.MethodGet, url, http.NoBody)
resp, err := dialClient.Do(req)
re.NoError(err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return false
}

// size is not changed.
output := sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?action=preparing", http.MethodGet, http.StatusOK)
var p api.Progress
re.NoError(json.Unmarshal(output, &p))
if p.Action != "preparing" {
return false
}
if p.Progress != 0.0 {
return false
}
if p.CurrentSpeed != 0.0 {
return false
}
if p.LeftSeconds != math.MaxFloat64 {
return false
}
return true
})

// update size
tests.MustPutRegion(re, cluster, 1000, 4, []byte(fmt.Sprintf("%20d", 1000)), []byte(fmt.Sprintf("%20d", 1001)), core.SetApproximateSize(10))
tests.MustPutRegion(re, cluster, 1001, 5, []byte(fmt.Sprintf("%20d", 1001)), []byte(fmt.Sprintf("%20d", 1002)), core.SetApproximateSize(40))
time.Sleep(2 * time.Second)
output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?action=preparing", http.MethodGet, http.StatusOK)
re.NoError(json.Unmarshal(output, &p))
re.Equal("preparing", p.Action)
// store 4: 10/(210*0.9) ~= 0.05
// store 5: 40/(210*0.9) ~= 0.21
// average progress ~= (0.05+0.21)/2 = 0.13
re.Equal("0.13", fmt.Sprintf("%.2f", p.Progress))
// store 4: 10/10s = 1
// store 5: 40/10s = 4
// average speed = (1+4)/2 = 2.5
re.Equal(2.5, p.CurrentSpeed)
// store 4: 179/1 ~= 179
// store 5: 149/4 ~= 37.25
// average time ~= (179+37.25)/2 = 108.125
re.Equal(108.125, p.LeftSeconds)

output = sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?id=4", http.MethodGet, http.StatusOK)
re.NoError(json.Unmarshal(output, &p))
re.Equal("preparing", p.Action)
re.Equal("0.05", fmt.Sprintf("%.2f", p.Progress))
re.Equal(1.0, p.CurrentSpeed)
re.Equal(179.0, p.LeftSeconds)
testutil.Eventually(re, func() bool {
output := sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?action=preparing", http.MethodGet, http.StatusOK)
var p api.Progress
re.NoError(json.Unmarshal(output, &p))
if p.Action != "preparing" {
return false
}

// store 4: 10/(210*0.9) ~= 0.05
// store 5: 40/(210*0.9) ~= 0.21
// average progress ~= (0.05+0.21)/2 = 0.13
if fmt.Sprintf("%.2f", p.Progress) != "0.13" {
return false
}
// store 4: 10/10s = 1
// store 5: 40/10s = 4
// average speed = (1+4)/2 = 2.5
if p.CurrentSpeed != 2.5 {
return false
}
// store 4: 179/1 ~= 179
// store 5: 149/4 ~= 37.25
// average time ~= (179+37.25)/2 = 108.125
if p.LeftSeconds != 108.125 {
return false
}
return true
})

// wait for the progress to be updated
testutil.Eventually(re, func() bool {
output := sendRequest(re, leader.GetAddr()+"/pd/api/v1/stores/progress?id=4", http.MethodGet, http.StatusOK)
var p api.Progress
re.NoError(json.Unmarshal(output, &p))
if p.Action != "preparing" {
return false
}
if fmt.Sprintf("%.2f", p.Progress) != "0.05" {
return false
}
if p.CurrentSpeed != 1.0 {
return false
}
if p.LeftSeconds != 179.0 {
return false
}
return true
})
re.NoError(failpoint.Disable("github.com/tikv/pd/server/cluster/highFrequencyClusterJobs"))
}

Expand Down

0 comments on commit 2855675

Please sign in to comment.