Repository: dragonflyoss/Dragonfly2
Pull request #888 (2021-12-07 10:44:39)

chore: update UnknownSourceFileLen
Signed-off-by: Jim Ma <majinjing3@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- 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: -->

- [ ] 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! -->

- [ ] Code compiles correctly.
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#888](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (98ac4eb) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/935f861d2d85dba8efbaa3c4130363b7f7be1591?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (935f861) will **increase** coverage by `0.20%`.
> The diff coverage is `38.54%`.

> :exclamation: Current head 98ac4eb differs from pull request most recent head ba47e24. Consider uploading reports for the commit ba47e24 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/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/888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff             @@
##             main     #888      +/-   ##
==========================================
+ Coverage   41.16%   41.37%   +0.20%     
==========================================
  Files         105      105              
  Lines        8254     8222      -32     
==========================================
+ Hits         3398     3402       +4     
+ Misses       4417     4380      -37     
- Partials      439      440       +1     
```

| Flag | Coverage Δ | |
|---|---|---|
| Object-compatibility-e2etests | `?` | |
| e2etests | `?` | |
| unittests | `41.37% <38.54%> (+0.20%)` | :arrow_up: |

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/888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [cdn/rpcserver/rpcserver.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2RuL3JwY3NlcnZlci9ycGNzZXJ2ZXIuZ28=) | `0.00% <0.00%> (ø)` | |
| [cdn/supervisor/cdn/cache\_writer.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2RuL3N1cGVydmlzb3IvY2RuL2NhY2hlX3dyaXRlci5nbw==) | `80.55% <0.00%> (ø)` | |
| [cdn/supervisor/cdn/storage/disk/disk.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2RuL3N1cGVydmlzb3IvY2RuL3N0b3JhZ2UvZGlzay9kaXNrLmdv) | `18.04% <0.00%> (ø)` | |
| [cdn/supervisor/task/manager.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2RuL3N1cGVydmlzb3IvdGFzay9tYW5hZ2VyLmdv) | `7.23% <0.00%> (-2.86%)` | :arrow_down: |
| [cdn/supervisor/task/manager\_util.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2RuL3N1cGVydmlzb3IvdGFzay9tYW5hZ2VyX3V0aWwuZ28=) | `0.00% <0.00%> (ø)` | |
| [client/daemon/peer/peertask\_base.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2Jhc2UuZ28=) | `46.06% <0.00%> (-0.61%)` | :arrow_down: |
| [client/daemon/peer/peertask\_manager.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX21hbmFnZXIuZ28=) | `20.14% <0.00%> (ø)` | |
| [client/daemon/peer/peertask\_reuse.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3JldXNlLmdv) | `0.00% <0.00%> (ø)` | |
| [pkg/source/httpprotocol/http\_source\_client.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL3NvdXJjZS9odHRwcHJvdG9jb2wvaHR0cF9zb3VyY2VfY2xpZW50Lmdv) | `56.31% <0.00%> (ø)` | |
| [pkg/source/source\_client.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL3NvdXJjZS9zb3VyY2VfY2xpZW50Lmdv) | `1.92% <0.00%> (ø)` | |
| ... and [13 more](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/888/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/cdn/supervisor/task/manager.go b/cdn/supervisor/task/manager.go
index caa2836c..f55a0e6e 100644
--- a/cdn/supervisor/task/manager.go
+++ b/cdn/supervisor/task/manager.go
@@ -202,14 +202,14 @@ func (tm *Manager) AddOrUpdate(registerTask *types.SeedTask) (seedTask *types.Se
 		synclock.UnLock(registerTask.TaskID, true)
 		return nil, errors.Wrapf(errTaskIDConflict, "register task %#v is conflict with exist task %#v", registerTask, seedTask)
 	}
-	if seedTask.SourceFileLength != source.UnKnownSourceFileLen {
+	if seedTask.SourceFileLength != source.UnknownSourceFileLen {
 		synclock.UnLock(registerTask.TaskID, true)
 		return seedTask, nil
 	}
 	synclock.UnLock(registerTask.TaskID, true)
 	synclock.Lock(registerTask.TaskID, false)
 	defer synclock.UnLock(registerTask.TaskID, false)
-	if seedTask.SourceFileLength != source.UnKnownSourceFileLen {
+	if seedTask.SourceFileLength != source.UnknownSourceFileLen {
 		return seedTask, nil
 	}
 	// get sourceContentLength with req.Header
diff --git a/cdn/types/seed_task_info.go b/cdn/types/seed_task_info.go
index 7718ef89..e6f20dd6 100644
--- a/cdn/types/seed_task_info.go
+++ b/cdn/types/seed_task_info.go
@@ -56,7 +56,7 @@ func NewSeedTask(taskID string, rawURL string, urlMeta *base.UrlMeta) *SeedTask
 		RequestDigest:    urlMeta.Digest,
 		URL:              rawURL,
 		TaskURL:          urlutils.FilterURLParam(rawURL, strings.Split(urlMeta.Filter, "&")),
-		SourceFileLength: source.UnKnownSourceFileLen,
+		SourceFileLength: source.UnknownSourceFileLen,
 		CdnFileLength:    0,
 		PieceSize:        0,
 		Range:            urlMeta.Range,
diff --git a/client/daemon/peer/piece_manager.go b/client/daemon/peer/piece_manager.go
index 2a823a29..4e5f41e3 100644
--- a/client/daemon/peer/piece_manager.go
+++ b/client/daemon/peer/piece_manager.go
@@ -355,7 +355,7 @@ func (pm *pieceManager) DownloadSource(ctx context.Context, pt Task, request *sc
 	if err != nil {
 		log.Warnf("get content length error: %s for %s", err, request.Url)
 	}
-	if contentLength == -1 {
+	if contentLength < 0 {
 		log.Warnf("can not get content length for %s", request.Url)
 	} else {
 		err = pm.storageManager.UpdateTask(ctx,
@@ -391,7 +391,7 @@ func (pm *pieceManager) DownloadSource(ctx context.Context, pt Task, request *sc
 	// 2. save to storage
 	pieceSize := pm.computePieceSize(contentLength)
 	// handle resource which content length is unknown
-	if contentLength == -1 {
+	if contentLength < 0 {
 		var n int64
 		for pieceNum := int32(0); ; pieceNum++ {
 			size := pieceSize
diff --git a/pkg/source/hdfsprotocol/hdfs_source_client.go b/pkg/source/hdfsprotocol/hdfs_source_client.go
index b0c0e1f3..71944a9b 100644
--- a/pkg/source/hdfsprotocol/hdfs_source_client.go
+++ b/pkg/source/hdfsprotocol/hdfs_source_client.go
@@ -80,11 +80,11 @@ type HDFSSourceClientOption func(p *hdfsSourceClient)
 func (h *hdfsSourceClient) GetContentLength(request *source.Request) (int64, error) {
 	hdfsClient, path, err := h.getHDFSClientAndPath(request.URL)
 	if err != nil {
-		return source.UnKnownSourceFileLen, err
+		return source.UnknownSourceFileLen, err
 	}
 	info, err := hdfsClient.Stat(path)
 	if err != nil {
-		return source.UnKnownSourceFileLen, err
+		return source.UnknownSourceFileLen, err
 	}
 	return info.Size(), nil
 }
diff --git a/pkg/source/httpprotocol/http_source_client.go b/pkg/source/httpprotocol/http_source_client.go
index 0b6d477c..7f691d54 100644
--- a/pkg/source/httpprotocol/http_source_client.go
+++ b/pkg/source/httpprotocol/http_source_client.go
@@ -127,12 +127,12 @@ func WithHTTPClient(client *http.Client) HTTPSourceClientOption {
 func (client *httpSourceClient) GetContentLength(request *source.Request) (int64, error) {
 	resp, err := client.doRequest(http.MethodGet, request)
 	if err != nil {
-		return source.UnKnownSourceFileLen, err
+		return source.UnknownSourceFileLen, err
 	}
 	defer resp.Body.Close()
 	err = source.CheckResponseCode(resp.StatusCode, []int{http.StatusOK, http.StatusPartialContent})
 	if err != nil {
-		return source.UnKnownSourceFileLen, err
+		return source.UnknownSourceFileLen, err
 	}
 	return resp.ContentLength, nil
 }
diff --git a/pkg/source/ossprotocol/oss_source_client.go b/pkg/source/ossprotocol/oss_source_client.go
index 8a172521..c56ba1e0 100644
--- a/pkg/source/ossprotocol/oss_source_client.go
+++ b/pkg/source/ossprotocol/oss_source_client.go
@@ -92,19 +92,19 @@ type ossSourceClient struct {
 func (osc *ossSourceClient) GetContentLength(request *source.Request) (int64, error) {
 	client, err := osc.getClient(request.Header)
 	if err != nil {
-		return source.UnKnownSourceFileLen, err
+		return source.UnknownSourceFileLen, err
 	}
 	bucket, err := client.Bucket(request.URL.Host)
 	if err != nil {
-		return source.UnKnownSourceFileLen, errors.Wrapf(err, "get oss bucket: %s", request.URL.Host)
+		return source.UnknownSourceFileLen, errors.Wrapf(err, "get oss bucket: %s", request.URL.Host)
 	}
 	header, err := bucket.GetObjectMeta(request.URL.Path, getOptions(request.Header)...)
 	if err != nil {
-		return source.UnKnownSourceFileLen, errors.Wrapf(err, "get oss object %s meta", request.URL.Path)
+		return source.UnknownSourceFileLen, errors.Wrapf(err, "get oss object %s meta", request.URL.Path)
 	}
 	contentLen, err := strconv.ParseInt(header.Get(oss.HTTPHeaderContentLength), 10, 64)
 	if err != nil {
-		return source.UnKnownSourceFileLen, errors.Wrapf(err, "parse content-length str to int64")
+		return source.UnknownSourceFileLen, errors.Wrapf(err, "parse content-length str to int64")
 	}
 	return contentLen, nil
 }
diff --git a/pkg/source/source_client.go b/pkg/source/source_client.go
index 03af6817..b595fdce 100644
--- a/pkg/source/source_client.go
+++ b/pkg/source/source_client.go
@@ -82,14 +82,14 @@ func IsNoClientFoundError(err error) bool {
 }
 
 const (
-	UnKnownSourceFileLen = -2
+	UnknownSourceFileLen = -2
 )
 
 // ResourceClient defines the API interface to interact with source.
 type ResourceClient interface {
 
 	// GetContentLength get length of resource content
-	// return source.UnKnownSourceFileLen if response status is not StatusOK and StatusPartialContent
+	// return source.UnknownSourceFileLen if response status is not StatusOK and StatusPartialContent
 	GetContentLength(request *Request) (int64, error)
 
 	// IsSupportRange checks if resource supports breakpoint continuation
@@ -218,7 +218,7 @@ func (c *clientWrapper) GetLastModified(request *Request) (int64, error) {
 func GetContentLength(request *Request) (int64, error) {
 	client, ok := _defaultManager.GetClient(request.URL.Scheme)
 	if !ok {
-		return UnKnownSourceFileLen, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme)
+		return UnknownSourceFileLen, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme)
 	}
 	if _, ok := request.Context().Deadline(); !ok {
 		ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)


Test patch:
diff --git a/pkg/source/hdfsprotocol/hdfs_source_client_test.go b/pkg/source/hdfsprotocol/hdfs_source_client_test.go
index 8bfdc062..e0b6bed4 100644
--- a/pkg/source/hdfsprotocol/hdfs_source_client_test.go
+++ b/pkg/source/hdfsprotocol/hdfs_source_client_test.go
@@ -51,7 +51,7 @@ const (
 
 const (
 	hdfsNotExistFileURL                 = "hdfs://127.0.0.1:9000/user/root/input/f3.txt"
-	hdfsNotExistFileContentLength int64 = source.UnKnownSourceFileLen
+	hdfsNotExistFileContentLength int64 = source.UnknownSourceFileLen
 )
 
 var fakeHDFSClient = &hdfs.Client{}