Skip to content

Commit 6e4a0f5

Browse files
authored
Check connection in save file change (#1454)
* check connection in save file change * fix unit tests
1 parent 22f0d75 commit 6e4a0f5

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,11 @@ func GetAllocationChanges(ctx context.Context, connectionID, allocationID, clien
174174
func GetConnectionObj(ctx context.Context, connectionID, allocationID, clientID string) (*AllocationChangeCollector, error) {
175175
cc := &AllocationChangeCollector{}
176176
db := datastore.GetStore().GetTransaction(ctx)
177-
err := db.Where("id = ? and allocation_id = ? and client_id = ?",
177+
err := db.Where("id = ? and allocation_id = ? and client_id = ? AND status <> ?",
178178
connectionID,
179179
allocationID,
180180
clientID,
181+
DeletedConnection,
181182
).Take(cc).Error
182183

183184
if err == nil {
@@ -189,7 +190,7 @@ func GetConnectionObj(ctx context.Context, connectionID, allocationID, clientID
189190
cc.AllocationID = allocationID
190191
cc.ClientID = clientID
191192
cc.Status = NewConnection
192-
err = cc.Create(ctx)
193+
err = cc.Save(ctx)
193194
if err != nil {
194195
return nil, err
195196
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
var (
1717
// ConnectionObjCleanInterval start to clean the connectionObjMap
18-
ConnectionObjCleanInterval = 10 * time.Minute
18+
ConnectionObjCleanInterval = 45 * time.Minute
1919
// ConnectionObjTimout after which connectionObj entry should be invalid
2020
ConnectionObjTimeout = 30 * time.Minute
2121
)
@@ -182,7 +182,11 @@ func SaveFileChange(ctx context.Context, connectionID, pathHash, fileName string
182182
change.lock.Lock()
183183
defer change.lock.Unlock()
184184
connectionObj.lock.Unlock()
185-
err := cmd.AddChange(ctx)
185+
_, err := GetConnectionObj(ctx, connectionID, connectionObj.AllocationID, connectionObj.ClientID)
186+
if err != nil {
187+
return saveChange, err
188+
}
189+
err = cmd.AddChange(ctx)
186190
if err != nil {
187191
return saveChange, err
188192
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,12 @@ func TestHandlers_Requiring_Signature(t *testing.T) {
847847
sqlmock.NewRows([]string{"found"}).
848848
AddRow(false),
849849
)
850+
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "allocation_connections" WHERE`)).
851+
WithArgs(connectionID, alloc.ID, alloc.OwnerID, allocation.DeletedConnection).
852+
WillReturnRows(
853+
sqlmock.NewRows([]string{"id", "allocation_id", "client_id"}).
854+
AddRow("connection_id", alloc.ID, alloc.OwnerID),
855+
)
850856
mock.ExpectQuery(regexp.QuoteMeta(`INSERT INTO "allocation_changes"`)).
851857
WithArgs(aa, aa, aa, aa, aa, aa, aa).
852858
WillReturnRows(

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,9 +1206,6 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
12061206
newDir.AllocationID = allocationID
12071207

12081208
connectionObj.AddChange(allocationChange, &newDir)
1209-
if err != nil {
1210-
return nil, err
1211-
}
12121209

12131210
err = connectionObj.Save(ctx)
12141211
if err != nil {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func cleanupTempFiles(ctx context.Context) {
101101
ndb.Unscoped().Delete(c)
102102
}
103103
ndb.Unscoped().Delete(connection)
104+
allocation.DeleteConnectionObjEntry(connection.ID)
104105
}
105106

106107
ndb.Commit()

0 commit comments

Comments
 (0)