Repository navigation
tq: decrement uploaded bytes in basic_upload before retry - #1958
Conversation
technoweenie
left a comment
There was a problem hiding this comment.
Good analysis of the bug, but I think the fix should be implemented on the *bodyWithCallback type in progress.
| return | ||
| } | ||
|
|
||
| // Seek wraps the underlying Seeker's "Seak" method, atomically updating the |
| var reader lfsapi.ReadSeekCloser = progress.NewBodyWithCallback(f, t.Size, ccb) | ||
|
|
||
| fcount := tools.NewCountingReadSeekCloser(f, stat.Size()) | ||
| var reader lfsapi.ReadSeekCloser = progress.NewBodyWithCallback(fcount, t.Size, ccb) |
There was a problem hiding this comment.
Why not just do the counting in the *bodyWithCallback that's returned? Why is a new type needed?
| atomic.AddInt64(&r.n, offset) | ||
| case io.SeekEnd: | ||
| atomic.SwapInt64(&r.n, r.totalSize+offset) | ||
| } |
There was a problem hiding this comment.
Does this really need to use atomic? It looks like the reader gets passed in through an http.Request body, which is not accessed from multiple goroutines.
| // read _so far_, so that the next iteration doesn't re-transfer | ||
| // those bytes, according to the progress meter. | ||
| offset := fcount.N() | ||
| ccb(t.Size, offset, -int(offset)) |
There was a problem hiding this comment.
If this was using the progress package's *bodyWithCallback, it could just be a single method like ResetProgress().
| } | ||
| var reader lfsapi.ReadSeekCloser = progress.NewBodyWithCallback(f, t.Size, ccb) | ||
|
|
||
| fcount := tools.NewCountingReadSeekCloser(f, stat.Size()) |
There was a problem hiding this comment.
Wouldn't t.Size work here too?
e27ce28 to
589dffb
Compare
Good question, and great call. I originally considered implementing this functionality on the wrapped callback reader that gets created, but that only happens conditionally, so I think I skipped over the body callback reader. I went ahead and re-implemented this on the callback reader in 589dffb. |
technoweenie
left a comment
There was a problem hiding this comment.
Thanks for just implementing this on *BodyWithCallback. If you get some grand inspiration for a better type name, go ahead and tweak it before you merge :)
| } | ||
|
|
||
| func NewByteBodyWithCallback(by []byte, totalSize int64, cb CopyCallback) ReadSeekCloser { | ||
| var _ ReadSeekCloser = (*BodyWithCallback)(nil) |
There was a problem hiding this comment.
These declarations are unnecessary. It wouldn't compile if this wasn't the case.
In PR #1955 we resolved a problem in the Read() methods of the interfaces we use to invoke callback functions that write entries to the progress log file defined by the GIT_LFS_PROGRESS environment variable. (These methods are currently located in our "tools" package, but at the time they were defined in a package named "progress".) The BodyWithCallback and CallbackReader structure types in our "tools" package both implement the Reader interface from the "io" package of the Go standard library by passing through calls from their Read() methods to the Read() method of another implementation of the Reader interface. Depending on the return values from that call, our structures' Read() methods may then invoke a callback function to write an entry in the progress log file. In commit a2a7b09 of PR #1955 we fixed a bug in the Read() methods of our BodyWithCallback and CallbackReader structures whereby the methods would fail to invoke their callback functions if the underlying Read() method read a non-zero number of bytes but also encountered an EOF condition. In the same PR #1955 we then added a new Go test function named TestCopyCallbackReaderCallsCallbackUnderfilledBuffer() which verifies that the Read() method of the CallbackReader structure invokes the callback function after the underlying Read() method returns the EOF error after reading a non-zero number of bytes. However, we did not include a corresponding test of the Read() method of the BodyWithCallback structure, so we now revise the existing test to verify the behaviour of both the CallbackReader structure and the BodyWithCallback structure. (Note that in a future PR, we may eliminate one of these two similar structures, at which point this test will only need to check the behaviour of that common structure's methods.) Since the BodyWithCallback structure implements the ReadSeekCloser interface from the "io" package, we need to add simple Seek() and Close() methods for the EagerEOFByteReader structure in our "internal/testutil" package. Fortunately, the Seek() method only needs to exist for the purposes of our test, which allows us to just define a stub version of the method. Otherwise, we would likely need to mirror the complete implementation from the Seek() method of the Reader structure in the Go standard library's "bytes" package: https://github.com/golang/go/blob/go1.26.4/src/bytes/reader.go#L115-L134 To alter the TestCopyCallbackReaderCallsCallbackUnderfilledBuffer() function to check the behaviour of both the CallbackReader and BodyWithCallback structures, we first create one of each using the same initial settings. We then iterate over the pair of these structures and perform identical checks on each one, including an additional check that after EOF has been reached and a final callback has been made, no further callback occurs on a subsequent invocation of the Read() method when no bytes are available to be read. We also simplify the test in several respects. First, because our structures' Read() methods invoke their callback functions synchronously within a single goroutine, there is no need for us to use the "sync/atomic" package from the Go standard library to increment the count of the number of times the callbacks occur. Second, we revise the test to avoid the use of hard-coded constant values in favour of local variables whose values are dependent on the size of the byte slice read by the structures' Read() methods. Third, where possible we check "int" values using just the Equal() function from the "github.com/stretchr/testify/assert" package, as we can avoid the use of the EqualValues() function except when the integer types of the expected and actual values differ. And fourth, to clarify the purpose of the test and its expanded scope, we rename it to TestBothCallbackReadersInvokeCallbackOnEagerEOF(). In commit 589dffb of PR #1958 we added two other functions to test the Read() method of the BodyWithCallback structure. One of these test functions, the TestBodyCallbackReaderUpdatesOffsetOnSeek() function, only pertains to the specific behaviour of the BodyWithCallback structure's Read() method. However, the TestBodyCallbackReaderCountsReads() test function checks that the BodyWithCallback structure's Read() method correctly updates its internal count of the number of bytes read, which is an operation also performed by the CallbackReader structure's Read() method. We therefore revise this function in a similar manner to we did for the TestCopyCallbackReaderCallsCallbackUnderfilledBuffer() function so that it now checks that both structures' Read() methods correctly update the internal counts of the number of bytes read. We likewise rename the function to TestBothCallbackReadersCountReads() and replace the use of hard-coded constants with local variables wherever possible. We also alter the test so that it performs multiple calls to each structure's Read() method and checks the internal count after each call, rather than just performing a single call to the Read() method which consumes all the available data. Finally, we take the opportunity to also amend the existing TestBodyCallbackReaderUpdatesOffsetOnSeek() function so that it uses the same buffer initialization steps as the other two test functions, avoids hard-coded constants in favour of local variables, and checks that the results from the Read() method match our expectations after the read position has been modified by one or more Seek() method calls.
In commit 589dffb of PR #1958 we introduced Seek() and ResetProgress() methods for the BodyWithCallback structure, which at the time was defined in a "progress" package and is now located in our "tools" package. While the Seek() method implements the expected behaviour of the Seeker interface from the Go standard library's "io" package, the ResetProgress() method simply invokes the callback function registered with our BodyWithCallback structure and passes the current number of bytes read so far as a negative value in the function's final "readSinceLast" parameter. We then call the ResetProgress() method when an object upload transfer needs to be retried so that we can adjust our progress meter's count of the bytes that have been transferred successfully. In PR #1958 we added the TestBodyCallbackReaderUpdatesOffsetOnSeek() test function to verify the behaviour of the BodyWithCallback structure's new Seek() method, but we did not also create a test for the ResetProgress() method at the same time. Because we expect to refactor the BodyWithCallback structure and its methods in a subsequent commit in this PR, we now add a new TestBodyCallbackReaderResetsProgress() test function which verifies that the ResetProgress() method works as expected. In particular, it invokes the ResetProgress() method and confirms that the registered callback function receives a "readSinceLast" parameter whose value is the negation of the number of bytes read so far from the underlying source buffer.
This pull-request fixes a bug that caused the progress meter to display that it had transferred more bytes than were included in the total push.
To set things up, I'm running a basic LFS server on my laptop that is configured to return an HTTP 400 when a client tries to upload more than 10 bytes. I've configured my repo to use that remote, and to push 502 files, the first 501 are less than 10 bytes.
The push is comprised of:
LFS uploads the first 1.85 KB successfully, and then tries 100 times (unsuccessfully) to upload the last file, but fails to decrement before it retries again, which is why the progress meter goes "over" the total count.
A more robust fix would expand the
tq.Adapterinterface to provide a flexible way for all transfer adapter implementations to report if they "lost" progress, but I think that fixing the most common case with relatively low effort is a good first step.I originally wanted to call
Seek(0, io.SeekCurrent)on the open file descriptor, but I decided against this for two reasons:io.Seeker, which may not always be the case.Instead, I wrote a new type in the
toolspackage, to implementio.ReadSeeker(andio.Closer, for usage with thelfsapipackage) which atomically keeps track of the number of bytes read. Importantly, it also "overrides" theSeek()method, so any sort of seeking that we do will still yield correct updates to the total number of bytes read.In conclusion:
works as expected.
/cc @git-lfs/core