Repository: dragonflyoss/Dragonfly2
Pull request #1732 (2022-10-09 10:34:36)

fix: content length is zero when task succeed
Signed-off-by: Gaius <gaius.qi@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description
- Fix content length is zero when task succeed.
<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#1732](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (f6e9e10) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/92470175055a31d9450624f11f399c85e684bce8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (9247017) will **increase** coverage by `43.87%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff            @@
##           main    #1732       +/-   ##
=========================================
+ Coverage      0   43.87%   +43.87%     
=========================================
  Files         0      128      +128     
  Lines         0    15848    +15848     
=========================================
+ Hits          0     6953     +6953     
- Misses        0     8371     +8371     
- Partials      0      524      +524     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `43.87% <100.00%> (?)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [scheduler/resource/task.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-c2NoZWR1bGVyL3Jlc291cmNlL3Rhc2suZ28=) | `87.19% <100.00%> (ø)` | |
| [scheduler/service/service.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-c2NoZWR1bGVyL3NlcnZpY2Uvc2VydmljZS5nbw==) | `77.31% <100.00%> (ø)` | |
| [pkg/net/ip/ip.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL25ldC9pcC9pcC5nbw==) | `100.00% <0.00%> (ø)` | |
| [pkg/source/request\_fixture.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL3NvdXJjZS9yZXF1ZXN0X2ZpeHR1cmUuZ28=) | `0.00% <0.00%> (ø)` | |
| [client/config/dfcache.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2NvbmZpZy9kZmNhY2hlLmdv) | `0.00% <0.00%> (ø)` | |
| [pkg/gc/logger.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL2djL2xvZ2dlci5nbw==) | `0.00% <0.00%> (ø)` | |
| [pkg/source/plugin.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL3NvdXJjZS9wbHVnaW4uZ28=) | `0.00% <0.00%> (ø)` | |
| [.../source/clients/httpprotocol/http\_source\_client.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL3NvdXJjZS9jbGllbnRzL2h0dHBwcm90b2NvbC9odHRwX3NvdXJjZV9jbGllbnQuZ28=) | `46.72% <0.00%> (ø)` | |
| [pkg/idgen/task\_id.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL2lkZ2VuL3Rhc2tfaWQuZ28=) | `83.78% <0.00%> (ø)` | |
| [scheduler/resource/seed\_peer.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-c2NoZWR1bGVyL3Jlc291cmNlL3NlZWRfcGVlci5nbw==) | `18.75% <0.00%> (ø)` | |
| ... and [120 more](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1732/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | |


Patch:
diff --git a/scheduler/resource/task.go b/scheduler/resource/task.go
index bbb098ca..44cd2fc5 100644
--- a/scheduler/resource/task.go
+++ b/scheduler/resource/task.go
@@ -148,7 +148,7 @@ func NewTask(id, url string, taskType commonv1.TaskType, meta *commonv1.UrlMeta,
 		URL:               url,
 		Type:              taskType,
 		URLMeta:           meta,
-		ContentLength:     atomic.NewInt64(0),
+		ContentLength:     atomic.NewInt64(-1),
 		TotalPieceCount:   atomic.NewInt32(0),
 		BackToSourceLimit: atomic.NewInt32(0),
 		BackToSourcePeers: set.NewSafeSet[string](),
diff --git a/scheduler/service/service.go b/scheduler/service/service.go
index 9dcf05bb..524311b6 100644
--- a/scheduler/service/service.go
+++ b/scheduler/service/service.go
@@ -806,14 +806,14 @@ func (s *Service) handleTaskSuccess(ctx context.Context, task *resource.Task, re
 		return
 	}
 
+	// Update task's resource total piece count and content length.
+	task.TotalPieceCount.Store(result.TotalPieceCount)
+	task.ContentLength.Store(result.ContentLength)
+
 	if err := task.FSM.Event(resource.TaskEventDownloadSucceeded); err != nil {
 		task.Log.Errorf("task fsm event failed: %s", err.Error())
 		return
 	}
-
-	// Update task's resource total piece count and content length.
-	task.TotalPieceCount.Store(result.TotalPieceCount)
-	task.ContentLength.Store(result.ContentLength)
 }
 
 // Conditions for the task to switch to the TaskStateSucceeded are:


Test patch:
diff --git a/scheduler/resource/task_test.go b/scheduler/resource/task_test.go
index 1261abbe..f1204be9 100644
--- a/scheduler/resource/task_test.go
+++ b/scheduler/resource/task_test.go
@@ -74,7 +74,7 @@ func TestTask_NewTask(t *testing.T) {
 				assert.Equal(task.URL, mockTaskURL)
 				assert.EqualValues(task.URLMeta, mockTaskURLMeta)
 				assert.Empty(task.DirectPiece)
-				assert.Equal(task.ContentLength.Load(), int64(0))
+				assert.Equal(task.ContentLength.Load(), int64(-1))
 				assert.Equal(task.TotalPieceCount.Load(), int32(0))
 				assert.Equal(task.BackToSourceLimit.Load(), int32(200))
 				assert.Equal(task.BackToSourcePeers.Len(), uint(0))
diff --git a/scheduler/service/service_test.go b/scheduler/service/service_test.go
index b6be7dd9..ec2e74e9 100644
--- a/scheduler/service/service_test.go
+++ b/scheduler/service/service_test.go
@@ -279,6 +279,7 @@ func TestService_RegisterPeerTask(t *testing.T) {
 			) {
 				mockPeer.Task.FSM.SetState(resource.TaskStateSucceeded)
 				mockPeer.Task.StorePeer(mockSeedPeer)
+				mockPeer.Task.ContentLength.Store(0)
 				gomock.InOrder(
 					mr.TaskManager().Return(taskManager).Times(1),
 					mt.LoadOrStore(gomock.Any()).Return(mockPeer.Task, true).Times(1),
@@ -1266,7 +1267,7 @@ func TestService_StatTask(t *testing.T) {
 				assert.EqualValues(task, &schedulerv1.Task{
 					Id:               mockTaskID,
 					Type:             commonv1.TaskType_Normal,
-					ContentLength:    0,
+					ContentLength:    -1,
 					TotalPieceCount:  0,
 					State:            resource.TaskStatePending,
 					PeerCount:        0,