Skip to content

Commit baa5ee1

Browse files
committed
fix alloc file stats delete size when removing file from fs
1 parent 820d0a8 commit baa5ee1

File tree

4 files changed

+10
-98
lines changed

4 files changed

+10
-98
lines changed

code/go/0chain.net/blobbercore/allocation/deletefilechange.go

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ package allocation
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"path/filepath"
87
"sync"
98

10-
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
11-
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/filestore"
129
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/reference"
1310
"github.com/0chain/blobber/code/go/0chain.net/core/common"
14-
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
15-
"gorm.io/gorm"
16-
17-
"go.uber.org/zap"
1811
)
1912

2013
var (
@@ -58,73 +51,8 @@ func (nf *DeleteFileChange) DeleteTempFile() error {
5851
return nil
5952
}
6053

61-
func (nf *DeleteFileChange) CommitToFileStore(ctx context.Context, mut *sync.Mutex) error {
62-
db := datastore.GetStore().GetTransaction(ctx)
63-
type Result struct {
64-
Id string
65-
ValidationRoot string
66-
ThumbnailHash string
67-
FilestoreVersion int
68-
}
69-
70-
limitCh := make(chan struct{}, 10)
71-
wg := &sync.WaitGroup{}
72-
var results []Result
73-
mut.Lock()
74-
err := db.Model(&reference.Ref{}).Unscoped().
75-
Select("id", "validation_root", "thumbnail_hash", "filestore_version").
76-
Where("allocation_id=? AND path LIKE ? AND type=? AND deleted_at is not NULL",
77-
nf.AllocationID, nf.Path+"%", reference.FILE).
78-
FindInBatches(&results, 100, func(tx *gorm.DB, batch int) error {
79-
80-
for _, res := range results {
81-
var count int64
82-
tx.Model(&reference.Ref{}).
83-
Where("allocation_id=? AND validation_root=?", nf.AllocationID, res.ValidationRoot).
84-
Count(&count)
85-
86-
if count != 0 && res.ThumbnailHash == "" {
87-
continue
88-
}
89-
90-
limitCh <- struct{}{}
91-
wg.Add(1)
92-
93-
go func(res Result, count int64) {
94-
defer func() {
95-
<-limitCh
96-
wg.Done()
97-
}()
98-
99-
if count == 0 {
100-
err := filestore.GetFileStore().DeleteFile(nf.AllocationID, res.ValidationRoot, res.FilestoreVersion)
101-
if err != nil {
102-
logging.Logger.Error(fmt.Sprintf("Error while deleting file: %s", err.Error()),
103-
zap.String("validation_root", res.ValidationRoot))
104-
}
105-
}
106-
// We don't increase alloc size for thumbnail so we don't need to decrease it
107-
// if res.ThumbnailHash != "" {
108-
// err := filestore.GetFileStore().DeleteFile(nf.AllocationID, res.ThumbnailHash)
109-
// if err != nil {
110-
// logging.Logger.Error(fmt.Sprintf("Error while deleting thumbnail: %s", err.Error()),
111-
// zap.String("thumbnail", res.ThumbnailHash))
112-
// }
113-
// }
114-
115-
}(res, count)
116-
117-
}
118-
return nil
119-
}).Error
120-
mut.Unlock()
121-
wg.Wait()
122-
123-
return err
124-
// return db.Model(&reference.Ref{}).Unscoped().
125-
// Delete(&reference.Ref{},
126-
// "allocation_id = ? AND path LIKE ? AND deleted_at IS NOT NULL",
127-
// nf.AllocationID, nf.Path+"%").Error
54+
func (nf *DeleteFileChange) CommitToFileStore(_ context.Context, _ *sync.Mutex) error {
55+
return nil
12856
}
12957

13058
func (nf *DeleteFileChange) GetPath() []string {

code/go/0chain.net/blobbercore/allocation/file_changer_update.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ import (
66
"path/filepath"
77
"sync"
88

9-
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
109
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/filestore"
1110
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/reference"
1211
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/util"
13-
"go.uber.org/zap"
1412

1513
"github.com/0chain/blobber/code/go/0chain.net/core/common"
16-
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
1714
)
1815

1916
type UpdateFileChanger struct {
@@ -105,23 +102,6 @@ func (nf *UpdateFileChanger) ApplyChange(ctx context.Context, rootRef *reference
105102
}
106103

107104
func (nf *UpdateFileChanger) CommitToFileStore(ctx context.Context, mut *sync.Mutex) error {
108-
db := datastore.GetStore().GetTransaction(ctx)
109-
for hash, version := range nf.deleteHash {
110-
var count int64
111-
mut.Lock()
112-
err := db.Table((&reference.Ref{}).TableName()).
113-
Where(&reference.Ref{ValidationRoot: hash}).
114-
Where(&reference.Ref{AllocationID: nf.AllocationID}).
115-
Count(&count).Error
116-
mut.Unlock()
117-
if err == nil && count == 0 {
118-
logging.Logger.Info("Deleting content file", zap.String("validation_root", hash))
119-
if err := filestore.GetFileStore().DeleteFile(nf.AllocationID, hash, version); err != nil {
120-
logging.Logger.Error("FileStore_DeleteFile", zap.String("allocation_id", nf.AllocationID), zap.Error(err))
121-
}
122-
}
123-
}
124-
125105
return nf.BaseFileChanger.CommitToFileStore(ctx, mut)
126106
}
127107

code/go/0chain.net/blobbercore/filestore/storage.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,18 @@ func (fs *FileStore) DeleteFromFilestore(allocID, hash string, version int) erro
166166
if err != nil {
167167
return common.NewError("get_file_path_error", err.Error())
168168
}
169+
170+
stat, err := os.Stat(fPath)
171+
if err != nil {
172+
return common.NewError("file_stat_error", err.Error())
173+
}
174+
169175
logging.Logger.Info("Deleting file from filestore", zap.String("path", fPath))
170176
err = os.Remove(fPath)
171177
if err != nil {
172178
return common.NewError("blob_object_dir_creation_error", err.Error())
173179
}
180+
fs.incrDecrAllocFileSizeAndNumber(allocID, -stat.Size(), -1)
174181

175182
return nil
176183
}

code/go/0chain.net/blobbercore/handler/handler_common.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package handler
22

33
import (
44
"context"
5-
"database/sql"
65
"errors"
76
"fmt"
87
"net/http"
@@ -134,9 +133,7 @@ func WithStatusConnectionForWM(handler common.StatusCodeResponderF) common.Statu
134133
}
135134
mutex.Lock()
136135
defer mutex.Unlock()
137-
ctx = GetMetaDataStore().CreateTransaction(ctx, &sql.TxOptions{
138-
Isolation: sql.LevelRepeatableRead,
139-
})
136+
ctx = GetMetaDataStore().CreateTransaction(ctx)
140137
tx := GetMetaDataStore().GetTransaction(ctx)
141138
resp, statusCode, err = handler(ctx, r)
142139

0 commit comments

Comments
 (0)