Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions internal/dao/cache_admin_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,28 +1108,49 @@ func (d *CacheAdminDao) purgeOne(item DeleteItem) *DeleteResult {
// 的动作,必须与正在写入的分块上传互斥。Linux 上少了这条,unlink 之后分块 writer
// 会继续往孤儿 inode 上写,位图更新全部丢失且无人知晓。
func reclaimBlobFile(repoType, orgRepo, sha string) error {
_, err := reclaimBlobFileIf(repoType, orgRepo, sha, nil)
return err
}

// eligible is evaluated under the writer lock, after any in-flight upload.
func reclaimBlobFileIf(repoType, orgRepo, sha string, eligible func(os.FileInfo) bool) (bool, error) {
if err := validateRepoKey(repoKey{RepoType: repoType, OrgRepo: orgRepo}); err != nil {
return err
return false, err
}
if err := repository.Segment(sha); err != nil {
return err
return false, err
}
if err := repository.SafePath(config.SysConfig.Repos(), BlobPath(repoType, orgRepo, sha)); err != nil {
return err
return false, err
}
blobPath := localBlobPath(repoType, orgRepo, sha)
blobKey := uploadBlobLockKey(repoType, orgRepo, sha)
uploadBlobLocks.Lock(blobKey)
defer uploadBlobLocks.Unlock(blobKey)
if syncManifestPinned(repoType, orgRepo, sha) {
return false, fmt.Errorf("content is pinned by an active synchronization")
}
if eligible != nil {
info, err := os.Lstat(blobPath)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if !info.Mode().IsRegular() || !eligible(info) {
return false, nil
}
}
// 拿到写锁之后再查句柄:分块上传在写锁下已经被挡住了,这里挡的是**下载**——
// 下载链路不走 blob 锁,只在 DingCacheManager 里登记引用计数。
if downloader.GetInstance().IsInUse(blobPath) {
return fmt.Errorf("content is in use by a running transfer")
return false, fmt.Errorf("content is in use by a running transfer")
}
if err := os.Remove(blobPath); err != nil && !os.IsNotExist(err) {
return err
return false, err
}
return nil
return true, nil
}

func validateRepoKey(key repoKey) error {
Expand Down
1 change: 1 addition & 0 deletions internal/dao/cache_admin_dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ func TestRecycledUploadIsReclaimedAfterRetention(t *testing.T) {
t.Fatalf("soft delete failed: %v", err)
}
ageTombstone(t, "models", orgRepo, sha, 200*time.Hour)
ageBlob(t, "models", orgRepo, sha, 200*time.Hour)

removed, err := u.CleanupUnreferencedBlobs(168 * time.Hour)
if err != nil {
Expand Down
33 changes: 31 additions & 2 deletions internal/dao/downloader_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,30 @@ func (d *DownloaderDao) FileDownload(startPos, endPos int64, isInnerRequest bool
}

func (d *DownloaderDao) constructTask(startPos, endPos int64, isInnerRequest bool, taskParam *downloader.TaskParam) ([]common.DownloadTask, error) {
if taskParam.NodeSourceOnly {
if err := taskParam.RepoKey.Validate(); err != nil {
return nil, err
}
if !taskParam.LocalOnly || taskParam.Source == nil || taskParam.Source.Fetch == nil || taskParam.RepoKey.Namespace == repository.HuggingFace || taskParam.RepoKey.Namespace == repository.ModelScope {
return nil, fmt.Errorf("node-only transfer requires an uploaded identity and explicit local source")
}
taskParam.Peer = false // Source.Fetch handles node transport; no fallback.
taskParam.Domain = taskParam.Source.Domain
return getContiguousRanges(startPos, endPos, taskParam), nil
}
var (
tasks []common.DownloadTask
ctx = taskParam.Context
fileComplete bool
curPos int64
)
// 小于这个值的文件将不参与调度
if taskParam.FileSize <= config.SysConfig.GetMinimumFileSize() {
if taskParam.FileSize <= config.SysConfig.GetMinimumFileSize() && !taskParam.NativeSync {
goto localTask
}
// 分析下载类型是否全部存在,若文件不完整,返回当前已缓存的最大偏移量
fileComplete, curPos = analysisFilePosition(taskParam.DingFile, startPos, endPos)
if !fileComplete && !config.SysConfig.Online() { // 文件不完整,且当前节点为离线
if !fileComplete && !config.SysConfig.Online() && !taskParam.NativeSync { // 文件不完整,且当前节点为离线
return nil, myerr.NewAppendCode(http.StatusNotFound, "Entry not found")
}
// isInnerRequest为true,即内部请求,是已经被调度过后,设置为内部域名的请求,这种请求将不会再次参与调度,直接做下载即可。
Expand Down Expand Up @@ -184,6 +195,9 @@ func upstreamDomain(p *downloader.TaskParam) string {
}

func (d *DownloaderDao) getRequestDomainScheduler(dataType, orgRepo, fileName, etag string, startPos, endPos, fileSize int64) (*manager.SchedulerFileResponse, error) {
if d.schedulerDao == nil || d.schedulerDao.Client == nil {
return nil, fmt.Errorf("scheduler client unavailable")
}
key, err := repository.ParseID(dataType, orgRepo)
if err != nil {
return nil, err
Expand Down Expand Up @@ -334,6 +348,14 @@ func getContiguousRanges(startPos, endPos int64, taskParam *downloader.TaskParam

func splitRemoteRange(startPos, endPos int64, taskNo *int, taskParam *downloader.TaskParam) []common.DownloadTask {
rangeSize := config.SysConfig.Download.RemoteFileRangeSize
if taskParam.NodeSourceOnly && rangeSize > 0 {
block := taskParam.DingFile.GetBlockSize()
if rangeSize < block {
rangeSize = block
} else {
rangeSize = rangeSize / block * block
}
}
remoteTasks := make([]common.DownloadTask, 0)
if rangeSize == 0 {
c := createRemoteTask(*taskNo, startPos, endPos, taskParam)
Expand Down Expand Up @@ -389,7 +411,9 @@ func (d *DownloaderDao) reconcileCachedFile(p *downloader.TaskParam) {

func createRemoteTask(taskNo int, start, end int64, taskParam *downloader.TaskParam) *downloader.RemoteFileTask {
remote := downloader.NewRemoteFileTask(taskNo, start, end)
remote.NativeSync = taskParam.NativeSync
remote.Source = taskParam.Source
remote.OnNetworkBytes = taskParam.OnNetworkBytes
remote.LocalOnly = taskParam.LocalOnly
remote.Context = taskParam.Context
remote.DingFile = taskParam.DingFile
Expand All @@ -414,6 +438,11 @@ func createRemoteTask(taskNo int, start, end int64, taskParam *downloader.TaskPa
}

func peerFileURI(p *downloader.TaskParam) string {
if p.NativeSync {
q := url.Values{"repo": {p.RepoKey.Repo}, "commit": {p.Revision}, "path": {p.FileName}, "oid": {p.Etag}, "size": {fmt.Sprint(p.FileSize)}}
return "/api/repository-sync/" + url.PathEscape(p.RepoKey.RepoType) + "/" + url.PathEscape(p.RepoKey.Namespace) + "/remote-content?" + q.Encode()
}

if p.RepoKey.Namespace == repository.HuggingFace {
prefix := ""
if p.RepoKey.RepoType != "models" {
Expand Down
20 changes: 11 additions & 9 deletions internal/dao/inventory_journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
)

type metadataIntent struct {
Revision string
Commit string
Files []LocalManifestFile
Revision string
Commit string
Files []LocalManifestFile
Publication *TreePublicationReceipt `json:",omitempty"`
}
type snapshotEdit struct {
OldCommit string
Expand Down Expand Up @@ -50,15 +51,19 @@ func ReadInventorySnapshot(k repository.RepoKey, revision string) (string, []Loc
}

func (u *UploadDao) writeEffectiveMetadata(repoType, id, revision, commit string, files []LocalManifestFile) error {
return u.writeEffectiveMetadataWithPublication(repoType, id, revision, commit, files, nil)
}

func (u *UploadDao) writeEffectiveMetadataWithPublication(repoType, id, revision, commit string, files []LocalManifestFile, publication *TreePublicationReceipt) error {
k := RepositoryKey(repoType, id)
if err := u.recoverInventoryLocked(k); err != nil {
return err
}
if err := RegisterHosted(k.RepoType, k.Namespace, k.Repo); err != nil {
return err
}
p := metadataIntent{revision, commit, files}
return inventory.Run(config.SysConfig.Repos(), inventoryKey(k), "metadata", p, func() error { return u.writeEffectiveMetadataRaw(repoType, id, revision, commit, files) })
p := metadataIntent{Revision: revision, Commit: commit, Files: files, Publication: publication}
return inventory.Run(config.SysConfig.Repos(), inventoryKey(k), "metadata", p, func() error { return u.applyMetadataIntent(k, p, false) })
}

// Recovery replays only journaled repositories, under the same locks as normal writes.
Expand Down Expand Up @@ -91,10 +96,7 @@ func (u *UploadDao) recoverInventoryLocked(k repository.RepoKey) error {
if err = json.Unmarshal(e.Operation.Data, &p); err != nil {
return err
}
if err = verifyManifestContent(k.RepoType, k.ID(), p.Files); err != nil {
return err
}
err = u.writeEffectiveMetadataRaw(k.RepoType, k.ID(), p.Revision, p.Commit, p.Files)
err = u.applyMetadataIntent(k, p, true)
case "delete-files":
var p deletionIntent
if err = json.Unmarshal(e.Operation.Data, &p); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/dao/meta_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func (m *MetaDao) RefreshPreheatMetadataContext(ctx context.Context, k repositor
return nil, err
}
}
if err := repository.Register(config.SysConfig.Repos(), repository.Remote(k)); err != nil {
return nil, err
}
headers := resp.ExtractHeaders(resp.Headers)
if err := m.writeApiMetaFile(k.RepoType, k.ID(), meta.Sha, "get", resp.StatusCode, headers, resp.Body); err != nil {
return nil, err
Expand Down
Loading
Loading