From 16e6dd05d8e088686f6043b819c52cc0e118f59d Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Fri, 19 Jul 2024 18:11:12 +0530 Subject: [PATCH] use size diff from root ref --- .../allocation/allocationchange.go | 15 +++--- .../handler/object_operation_handler.go | 47 ++++++++++--------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/code/go/0chain.net/blobbercore/allocation/allocationchange.go b/code/go/0chain.net/blobbercore/allocation/allocationchange.go index 87669bb3d..1792a565f 100644 --- a/code/go/0chain.net/blobbercore/allocation/allocationchange.go +++ b/code/go/0chain.net/blobbercore/allocation/allocationchange.go @@ -255,28 +255,31 @@ func (cc *AllocationChangeCollector) ComputeProperties() { } func (cc *AllocationChangeCollector) ApplyChanges(ctx context.Context, allocationRoot, prevAllocationRoot string, - ts common.Timestamp, fileIDMeta map[string]string) (*reference.Ref, error) { + ts common.Timestamp, fileIDMeta map[string]string) (*reference.Ref, int64, error) { + var sizeDiff int64 rootRef, err := cc.GetRootRef(ctx) if err != nil { - return rootRef, err + return rootRef, sizeDiff, err } + initalSize := rootRef.Size if rootRef.Hash != prevAllocationRoot { - return rootRef, common.NewError("invalid_prev_root", "Invalid prev root") + return rootRef, sizeDiff, common.NewError("invalid_prev_root", "Invalid prev root") } for idx, change := range cc.Changes { changeProcessor := cc.AllocationChanges[idx] _, err := changeProcessor.ApplyChange(ctx, rootRef, change, allocationRoot, ts, fileIDMeta) if err != nil { - return rootRef, err + return rootRef, sizeDiff, err } } collector := reference.NewCollector(len(cc.Changes)) _, err = rootRef.CalculateHash(ctx, true, collector) if err != nil { - return rootRef, err + return rootRef, sizeDiff, err } + sizeDiff = rootRef.Size - initalSize err = collector.Finalize(ctx) - return rootRef, err + return rootRef, sizeDiff, err } func (a *AllocationChangeCollector) CommitToFileStore(ctx context.Context) error { diff --git a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go index 45c8921f2..95bc4b725 100644 --- a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go +++ b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go @@ -689,11 +689,6 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b "Latest write marker is in failed state") } - if latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size != writeMarker.ChainSize { - return nil, common.NewErrorf("invalid_chain_size", - "Invalid chain size. expected:%v got %v", latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size, writeMarker.ChainSize) - } - if latestWriteMarkerEntity.Status != writemarker.Committed { writeMarker.ChainLength = latestWriteMarkerEntity.WM.ChainLength } @@ -707,20 +702,6 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b return nil, common.NewError("chain_length_exceeded", "Chain length exceeded") } - err = writemarkerEntity.VerifyMarker(ctx, allocationObj, connectionObj, latestWriteMarkerEntity) - if err != nil { - result.AllocationRoot = allocationObj.AllocationRoot - result.ErrorMessage = "Verification of write marker failed: " + err.Error() - result.Success = false - if latestWriteMarkerEntity != nil { - result.WriteMarker = latestWriteMarkerEntity - } - Logger.Error("verify_writemarker_failed", zap.Error(err)) - return &result, common.NewError("write_marker_verification_failed", result.ErrorMessage) - } - - elapsedVerifyWM := time.Since(startTime) - elapsedAllocation - elapsedGetLock - elapsedGetConnObj - var clientIDForWriteRedeem = writeMarker.ClientID if err := writePreRedeem(ctx, allocationObj, &writeMarker, clientIDForWriteRedeem); err != nil { @@ -728,7 +709,7 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b } elapsedWritePreRedeem := time.Since(startTime) - elapsedAllocation - elapsedGetLock - - elapsedGetConnObj - elapsedVerifyWM + elapsedGetConnObj fileIDMetaStr := r.FormValue("file_id_meta") if fileIDMetaStr == "" { @@ -747,9 +728,9 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b return nil, common.NewError("move_to_filestore_error", fmt.Sprintf("Error while moving to filestore: %s", err.Error())) } - elapsedMoveToFilestore := time.Since(startTime) - elapsedAllocation - elapsedGetLock - elapsedGetConnObj - elapsedVerifyWM - elapsedWritePreRedeem + elapsedMoveToFilestore := time.Since(startTime) - elapsedAllocation - elapsedGetLock - elapsedGetConnObj - elapsedWritePreRedeem - rootRef, err := connectionObj.ApplyChanges( + rootRef, connectionSize, err := connectionObj.ApplyChanges( ctx, writeMarker.AllocationRoot, writeMarker.PreviousAllocationRoot, writeMarker.Timestamp, fileIDMeta) if err != nil { Logger.Error("Error applying changes", zap.Error(err)) @@ -758,9 +739,29 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b if !rootRef.IsPrecommit { return nil, common.NewError("no_root_change", "No change in root ref") } + connectionObj.Size = connectionSize elapsedApplyChanges := time.Since(startTime) - elapsedAllocation - elapsedGetLock - - elapsedGetConnObj - elapsedVerifyWM - elapsedWritePreRedeem + elapsedGetConnObj - elapsedWritePreRedeem + + if latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size != writeMarker.ChainSize { + return nil, common.NewErrorf("invalid_chain_size", + "Invalid chain size. expected:%v got %v", latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size, writeMarker.ChainSize) + } + + err = writemarkerEntity.VerifyMarker(ctx, allocationObj, connectionObj, latestWriteMarkerEntity) + if err != nil { + result.AllocationRoot = allocationObj.AllocationRoot + result.ErrorMessage = "Verification of write marker failed: " + err.Error() + result.Success = false + if latestWriteMarkerEntity != nil { + result.WriteMarker = latestWriteMarkerEntity + } + Logger.Error("verify_writemarker_failed", zap.Error(err)) + return &result, common.NewError("write_marker_verification_failed", result.ErrorMessage) + } + + elapsedVerifyWM := time.Since(startTime) - elapsedAllocation - elapsedGetLock - elapsedGetConnObj - elapsedWritePreRedeem - elapsedMoveToFilestore allocationRoot := rootRef.Hash fileMetaRoot := rootRef.FileMetaHash