Skip to content

Commit

Permalink
🎨 Support manual purge of unreferenced data snapshots in the S3/WebDA…
Browse files Browse the repository at this point in the history
…V cloud storage siyuan-note/siyuan#10081
  • Loading branch information
88250 committed Jan 24, 2024
1 parent 2ca9817 commit 2abbc23
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/restic/chunker v0.4.0
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4
github.com/siyuan-note/eventbus v0.0.0-20240114153055-cf4c124ddf6d
github.com/siyuan-note/eventbus v0.0.0-20240124035703-9092bf8e8f48
github.com/siyuan-note/filelock v0.0.0-20240121150730-38e4487651d1
github.com/siyuan-note/httpclient v0.0.0-20240105083729-70d0557bc690
github.com/siyuan-note/logging v0.0.0-20231208035918-61f884c854f0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDj
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4 h1:kJaw5L/evyW6LcB9IQT8PR4ppx8JVqOFP9Ix3rfwSrc=
github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4/go.mod h1:UYcCCY+0wh+GmUoDOaO63j1sV5lgy7laLAk1XhEiUis=
github.com/siyuan-note/eventbus v0.0.0-20240114153055-cf4c124ddf6d h1:ZFhIq+gy5w+FJNX3D4yIp5Aj1H/dM0rrkEdRtk3Ia44=
github.com/siyuan-note/eventbus v0.0.0-20240114153055-cf4c124ddf6d/go.mod h1:1/nGgthl89FPA7GzAcEWKl6zRRnfgyTjzLZj9bW7kuw=
github.com/siyuan-note/eventbus v0.0.0-20240124035703-9092bf8e8f48 h1:qEeZUvXFIfONrl7Eaf8fSP/NswHkKM2XMbb9yrMNqbY=
github.com/siyuan-note/eventbus v0.0.0-20240124035703-9092bf8e8f48/go.mod h1:1/nGgthl89FPA7GzAcEWKl6zRRnfgyTjzLZj9bW7kuw=
github.com/siyuan-note/filelock v0.0.0-20240121150730-38e4487651d1 h1:eNHKiqE5yPnj1PYk0PJhxP+oqWdKi+0ZI28lBQmz5qg=
github.com/siyuan-note/filelock v0.0.0-20240121150730-38e4487651d1/go.mod h1:CYJQjSyKYLhEJJC+5I+R4uNcpyW0X2CaUYwMVbkelDk=
github.com/siyuan-note/httpclient v0.0.0-20240105083729-70d0557bc690 h1:r7qWSaYQhomCAdQl/dZe4NdAn1zadytb4jcOeopdHw4=
Expand Down
18 changes: 14 additions & 4 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
lock.Lock()
defer lock.Unlock()

context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToNone}
err = repo.tryLockCloud("purge", context)
lockCtx := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToNone}
err = repo.tryLockCloud("purge", lockCtx)
if nil != err {
return
}
defer repo.unlockCloud(context)
defer repo.unlockCloud(lockCtx)

logging.LogInfof("purging cloud...")
context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBarAndProgress}
eventbus.Publish(eventbus.EvtCloudPurgeListObjects, context)
objInfos, listErr := repo.cloud.ListObjects("objects/")
if nil != listErr {
logging.LogErrorf("list objects failed: %s", listErr)
Expand All @@ -167,6 +169,7 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
objIDs[objID] = true
}

eventbus.Publish(eventbus.EvtCloudPurgeListIndexes, context)
indexIDs, listErr := repo.cloud.ListObjects("indexes/")
if nil != listErr {
logging.LogErrorf("list indexes failed: %s", listErr)
Expand All @@ -179,6 +182,7 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
return
}

eventbus.Publish(eventbus.EvtCloudPurgeListRefs, context)
refs, listErr := repo.cloud.ListObjects("refs/")
if nil != listErr {
logging.LogErrorf("list refs failed: %s", listErr)
Expand Down Expand Up @@ -209,6 +213,7 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
referencedFileIDs := map[string]bool{}
referencedObjIDs := map[string]bool{}
for refID := range refIndexIDs {
eventbus.Publish(eventbus.EvtCloudPurgeDownloadIndex, context, refID)
index, getErr := repo.cloud.GetIndex(refID)
if nil != getErr {
err = getErr
Expand All @@ -234,7 +239,8 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
filesIDs = append(filesIDs, fileID)
}

_, dFiles, downloadErr := repo.downloadCloudFilesPut(filesIDs, context)
eventbus.Publish(eventbus.EvtCloudPurgeDownloadFiles, context)
_, dFiles, downloadErr := repo.downloadCloudFilesPut(filesIDs, map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToNone})
if nil != downloadErr {
err = downloadErr
logging.LogErrorf("download cloud files failed: %s", err)
Expand Down Expand Up @@ -288,6 +294,7 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
checkIndexPath := path.Join("check", "indexes", checkIndexID)
unreferencedCheckIndexPaths = append(unreferencedCheckIndexPaths, checkIndexPath)
}
eventbus.Publish(eventbus.EvtCloudPurgeRemoveIndexes, context, unreferencedCheckIndexPaths)
err = repo.removeCloudObjects(unreferencedCheckIndexPaths)
if nil != err {
logging.LogErrorf("remove unreferenced check indexes failed: %s", err)
Expand All @@ -301,13 +308,15 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
unreferencedIndexPaths = append(unreferencedIndexPaths, indexPath)
}

eventbus.Publish(eventbus.EvtCloudPurgeRemoveIndexes, context, unreferencedIndexPaths)
err = repo.removeCloudObjects(unreferencedIndexPaths)
if nil != err {
logging.LogErrorf("remove unreferenced indexes failed: %s", err)
return
}

// 清理索引列表
eventbus.Publish(eventbus.EvtCloudPurgeRemoveIndexesV2, context, refIndexIDs)
err = repo.purgeIndexesV2(refIndexIDs)
if nil != err {
logging.LogErrorf("purge indexes-v2.json failed: %s", err)
Expand All @@ -320,6 +329,7 @@ func (repo *Repo) PurgeCloud() (ret *entity.PurgeStat, err error) {
objPath := path.Join("objects", unreferencedPath)
unreferencedObjPaths = append(unreferencedObjPaths, objPath)
}
eventbus.Publish(eventbus.EvtCloudPurgeRemoveObjects, context, unreferencedObjPaths)
err = repo.removeCloudObjects(unreferencedObjPaths)
if nil != err {
logging.LogErrorf("remove unreferenced objects failed: %s", err)
Expand Down

0 comments on commit 2abbc23

Please sign in to comment.