chore: optimize compute piece size function (#528)
Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
parent
a8cb70d452
commit
94e35fa9df
|
|
@ -19,9 +19,27 @@ package cdnutil
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"d7y.io/dragonfly/v2/cdnsystem/config"
|
||||||
"d7y.io/dragonfly/v2/pkg/util/net/iputils"
|
"d7y.io/dragonfly/v2/pkg/util/net/iputils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenCDNPeerID(taskID string) string {
|
func GenCDNPeerID(taskID string) string {
|
||||||
return fmt.Sprintf("%s-%s_%s", iputils.HostName, taskID, "CDN")
|
return fmt.Sprintf("%s-%s_%s", iputils.HostName, taskID, "CDN")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ComputePieceSize computes the piece size with specified fileLength.
|
||||||
|
//
|
||||||
|
// If the fileLength<=0, which means failed to get fileLength
|
||||||
|
// and then use the DefaultPieceSize.
|
||||||
|
func ComputePieceSize(length int64) int32 {
|
||||||
|
if length <= 0 || length <= 200*1024*1024 {
|
||||||
|
return config.DefaultPieceSize
|
||||||
|
}
|
||||||
|
|
||||||
|
gapCount := length / int64(100*1024*1024)
|
||||||
|
mpSize := (gapCount-2)*1024*1024 + config.DefaultPieceSize
|
||||||
|
if mpSize > config.DefaultPieceSizeLimit {
|
||||||
|
return config.DefaultPieceSizeLimit
|
||||||
|
}
|
||||||
|
return int32(mpSize)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,9 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"d7y.io/dragonfly/v2/cdnsystem/config"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"d7y.io/dragonfly/v2/cdnsystem/cdnutil"
|
||||||
cdnerrors "d7y.io/dragonfly/v2/cdnsystem/errors"
|
cdnerrors "d7y.io/dragonfly/v2/cdnsystem/errors"
|
||||||
"d7y.io/dragonfly/v2/cdnsystem/types"
|
"d7y.io/dragonfly/v2/cdnsystem/types"
|
||||||
logger "d7y.io/dragonfly/v2/internal/dflog"
|
logger "d7y.io/dragonfly/v2/internal/dflog"
|
||||||
|
|
@ -30,7 +32,6 @@ import (
|
||||||
"d7y.io/dragonfly/v2/pkg/synclock"
|
"d7y.io/dragonfly/v2/pkg/synclock"
|
||||||
"d7y.io/dragonfly/v2/pkg/util/net/urlutils"
|
"d7y.io/dragonfly/v2/pkg/util/net/urlutils"
|
||||||
"d7y.io/dragonfly/v2/pkg/util/stringutils"
|
"d7y.io/dragonfly/v2/pkg/util/stringutils"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -108,7 +109,7 @@ func (tm *Manager) addOrUpdateTask(ctx context.Context, request *types.TaskRegis
|
||||||
|
|
||||||
// calculate piece size and update the PieceSize and PieceTotal
|
// calculate piece size and update the PieceSize and PieceTotal
|
||||||
if task.PieceSize <= 0 {
|
if task.PieceSize <= 0 {
|
||||||
pieceSize := computePieceSize(task.SourceFileLength)
|
pieceSize := cdnutil.ComputePieceSize(task.SourceFileLength)
|
||||||
task.PieceSize = pieceSize
|
task.PieceSize = pieceSize
|
||||||
}
|
}
|
||||||
tm.taskStore.Add(task.TaskID, task)
|
tm.taskStore.Add(task.TaskID, task)
|
||||||
|
|
@ -193,20 +194,3 @@ func isSameTask(task1, task2 *types.SeedTask) bool {
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// computePieceSize computes the piece size with specified fileLength.
|
|
||||||
//
|
|
||||||
// If the fileLength<=0, which means failed to get fileLength
|
|
||||||
// and then use the DefaultPieceSize.
|
|
||||||
func computePieceSize(length int64) int32 {
|
|
||||||
if length <= 0 || length <= 200*1024*1024 {
|
|
||||||
return config.DefaultPieceSize
|
|
||||||
}
|
|
||||||
|
|
||||||
gapCount := length / int64(100*1024*1024)
|
|
||||||
mpSize := (gapCount-2)*1024*1024 + config.DefaultPieceSize
|
|
||||||
if mpSize > config.DefaultPieceSizeLimit {
|
|
||||||
return config.DefaultPieceSizeLimit
|
|
||||||
}
|
|
||||||
return int32(mpSize)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -26,17 +26,19 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"d7y.io/dragonfly/v2/cdnsystem/cdnutil"
|
||||||
"d7y.io/dragonfly/v2/pkg/rpc/base"
|
"d7y.io/dragonfly/v2/pkg/rpc/base"
|
||||||
rangers "d7y.io/dragonfly/v2/pkg/util/rangeutils"
|
rangers "d7y.io/dragonfly/v2/pkg/util/rangeutils"
|
||||||
|
|
||||||
|
"github.com/golang/mock/gomock"
|
||||||
|
testifyassert "github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"d7y.io/dragonfly/v2/client/clientutil"
|
"d7y.io/dragonfly/v2/client/clientutil"
|
||||||
"d7y.io/dragonfly/v2/client/config"
|
"d7y.io/dragonfly/v2/client/config"
|
||||||
"d7y.io/dragonfly/v2/client/daemon/test"
|
"d7y.io/dragonfly/v2/client/daemon/test"
|
||||||
"d7y.io/dragonfly/v2/pkg/rpc/scheduler"
|
"d7y.io/dragonfly/v2/pkg/rpc/scheduler"
|
||||||
"d7y.io/dragonfly/v2/pkg/source"
|
"d7y.io/dragonfly/v2/pkg/source"
|
||||||
sourceMock "d7y.io/dragonfly/v2/pkg/source/mock"
|
sourceMock "d7y.io/dragonfly/v2/pkg/source/mock"
|
||||||
"github.com/golang/mock/gomock"
|
|
||||||
testifyassert "github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFilePeerTask_BackSource_WithContentLength(t *testing.T) {
|
func TestFilePeerTask_BackSource_WithContentLength(t *testing.T) {
|
||||||
|
|
@ -100,7 +102,7 @@ func TestFilePeerTask_BackSource_WithContentLength(t *testing.T) {
|
||||||
pieceManager: &pieceManager{
|
pieceManager: &pieceManager{
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
pieceDownloader: downloader,
|
pieceDownloader: downloader,
|
||||||
computePieceSize: computePieceSize,
|
computePieceSize: cdnutil.ComputePieceSize,
|
||||||
},
|
},
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
schedulerClient: schedulerClient,
|
schedulerClient: schedulerClient,
|
||||||
|
|
@ -219,7 +221,7 @@ func TestFilePeerTask_BackSource_WithoutContentLength(t *testing.T) {
|
||||||
pieceManager: &pieceManager{
|
pieceManager: &pieceManager{
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
pieceDownloader: downloader,
|
pieceDownloader: downloader,
|
||||||
computePieceSize: computePieceSize,
|
computePieceSize: cdnutil.ComputePieceSize,
|
||||||
},
|
},
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
schedulerClient: schedulerClient,
|
schedulerClient: schedulerClient,
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ import (
|
||||||
testifyassert "github.com/stretchr/testify/assert"
|
testifyassert "github.com/stretchr/testify/assert"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
|
"d7y.io/dragonfly/v2/cdnsystem/cdnutil"
|
||||||
"d7y.io/dragonfly/v2/client/clientutil"
|
"d7y.io/dragonfly/v2/client/clientutil"
|
||||||
"d7y.io/dragonfly/v2/client/config"
|
"d7y.io/dragonfly/v2/client/config"
|
||||||
"d7y.io/dragonfly/v2/client/daemon/storage"
|
"d7y.io/dragonfly/v2/client/daemon/storage"
|
||||||
|
|
@ -362,7 +363,7 @@ func TestPeerTaskManager_StartStreamPeerTask_BackSource(t *testing.T) {
|
||||||
pieceManager: &pieceManager{
|
pieceManager: &pieceManager{
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
pieceDownloader: NewMockPieceDownloader(ctrl),
|
pieceDownloader: NewMockPieceDownloader(ctrl),
|
||||||
computePieceSize: computePieceSize,
|
computePieceSize: cdnutil.ComputePieceSize,
|
||||||
},
|
},
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
schedulerClient: sched,
|
schedulerClient: sched,
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import (
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
testifyassert "github.com/stretchr/testify/assert"
|
testifyassert "github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"d7y.io/dragonfly/v2/cdnsystem/cdnutil"
|
||||||
"d7y.io/dragonfly/v2/client/clientutil"
|
"d7y.io/dragonfly/v2/client/clientutil"
|
||||||
"d7y.io/dragonfly/v2/client/config"
|
"d7y.io/dragonfly/v2/client/config"
|
||||||
"d7y.io/dragonfly/v2/client/daemon/test"
|
"d7y.io/dragonfly/v2/client/daemon/test"
|
||||||
|
|
@ -97,7 +98,7 @@ func TestStreamPeerTask_BackSource_WithContentLength(t *testing.T) {
|
||||||
pieceManager: &pieceManager{
|
pieceManager: &pieceManager{
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
pieceDownloader: downloader,
|
pieceDownloader: downloader,
|
||||||
computePieceSize: computePieceSize,
|
computePieceSize: cdnutil.ComputePieceSize,
|
||||||
},
|
},
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
schedulerClient: schedulerClient,
|
schedulerClient: schedulerClient,
|
||||||
|
|
@ -204,7 +205,7 @@ func TestStreamPeerTask_BackSource_WithoutContentLength(t *testing.T) {
|
||||||
pieceManager: &pieceManager{
|
pieceManager: &pieceManager{
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
pieceDownloader: downloader,
|
pieceDownloader: downloader,
|
||||||
computePieceSize: computePieceSize,
|
computePieceSize: cdnutil.ComputePieceSize,
|
||||||
},
|
},
|
||||||
storageManager: storageManager,
|
storageManager: storageManager,
|
||||||
schedulerClient: schedulerClient,
|
schedulerClient: schedulerClient,
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ import (
|
||||||
|
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
|
|
||||||
cdnconfig "d7y.io/dragonfly/v2/cdnsystem/config"
|
"d7y.io/dragonfly/v2/cdnsystem/cdnutil"
|
||||||
"d7y.io/dragonfly/v2/client/clientutil"
|
"d7y.io/dragonfly/v2/client/clientutil"
|
||||||
"d7y.io/dragonfly/v2/client/config"
|
"d7y.io/dragonfly/v2/client/config"
|
||||||
"d7y.io/dragonfly/v2/client/daemon/storage"
|
"d7y.io/dragonfly/v2/client/daemon/storage"
|
||||||
|
|
@ -56,7 +56,7 @@ var _ PieceManager = (*pieceManager)(nil)
|
||||||
func NewPieceManager(s storage.TaskStorageDriver, opts ...func(*pieceManager)) (PieceManager, error) {
|
func NewPieceManager(s storage.TaskStorageDriver, opts ...func(*pieceManager)) (PieceManager, error) {
|
||||||
pm := &pieceManager{
|
pm := &pieceManager{
|
||||||
storageManager: s,
|
storageManager: s,
|
||||||
computePieceSize: computePieceSize,
|
computePieceSize: cdnutil.ComputePieceSize,
|
||||||
calculateDigest: true,
|
calculateDigest: true,
|
||||||
}
|
}
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
|
@ -386,21 +386,3 @@ func (pm *pieceManager) DownloadSource(ctx context.Context, pt Task, request *sc
|
||||||
log.Infof("download from source ok")
|
log.Infof("download from source ok")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO copy from cdnsystem/daemon/mgr/task/manager_util.go
|
|
||||||
// computePieceSize computes the piece size with specified fileLength.
|
|
||||||
//
|
|
||||||
// If the fileLength<=0, which means failed to get fileLength
|
|
||||||
// and then use the DefaultPieceSize.
|
|
||||||
func computePieceSize(length int64) int32 {
|
|
||||||
if length <= 0 || length <= 200*1024*1024 {
|
|
||||||
return cdnconfig.DefaultPieceSize
|
|
||||||
}
|
|
||||||
|
|
||||||
gapCount := length / int64(100*1024*1024)
|
|
||||||
mpSize := (gapCount-2)*1024*1024 + cdnconfig.DefaultPieceSize
|
|
||||||
if mpSize > cdnconfig.DefaultPieceSizeLimit {
|
|
||||||
return cdnconfig.DefaultPieceSizeLimit
|
|
||||||
}
|
|
||||||
return int32(mpSize)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -33,13 +33,13 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"d7y.io/dragonfly/v2/pkg/unit"
|
|
||||||
"d7y.io/dragonfly/v2/pkg/util/net/iputils"
|
|
||||||
"github.com/go-echarts/statsview"
|
"github.com/go-echarts/statsview"
|
||||||
"github.com/go-echarts/statsview/viewer"
|
"github.com/go-echarts/statsview/viewer"
|
||||||
"github.com/montanaflynn/stats"
|
"github.com/montanaflynn/stats"
|
||||||
|
|
||||||
"d7y.io/dragonfly/v2/client/config"
|
"d7y.io/dragonfly/v2/client/config"
|
||||||
|
"d7y.io/dragonfly/v2/pkg/unit"
|
||||||
|
"d7y.io/dragonfly/v2/pkg/util/net/iputils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Loading…
Reference in New Issue