Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use size diff from root ref #1460

Draft
wants to merge 1 commit into
base: staging
Choose a base branch
from
Draft
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
15 changes: 9 additions & 6 deletions code/go/0chain.net/blobbercore/allocation/allocationchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -707,28 +702,14 @@ 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 {
return nil, err
}

elapsedWritePreRedeem := time.Since(startTime) - elapsedAllocation - elapsedGetLock -
elapsedGetConnObj - elapsedVerifyWM
elapsedGetConnObj

fileIDMetaStr := r.FormValue("file_id_meta")
if fileIDMetaStr == "" {
Expand All @@ -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))
Expand All @@ -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
Expand Down
Loading