Skip to content

Add TryQueryOffset API #413

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

Merged
merged 8 commits into from
Apr 16, 2025
Merged

Add TryQueryOffset API #413

merged 8 commits into from
Apr 16, 2025

Conversation

Gsantomaggio
Copy link
Member

@Gsantomaggio Gsantomaggio commented Apr 8, 2025

Closes: #370

Signed-off-by: Gabriele Santomaggio <[email protected]>
@Gsantomaggio Gsantomaggio added this to the 1.8.13 milestone Apr 8, 2025
Signed-off-by: Gabriele Santomaggio <[email protected]>
Signed-off-by: Gabriele Santomaggio <[email protected]>
@Gsantomaggio Gsantomaggio marked this pull request as ready for review April 8, 2025 13:57
@Gsantomaggio Gsantomaggio changed the title Add TryQueryOff API Add TryQueryOffset API Apr 8, 2025
@Gsantomaggio
Copy link
Member Author

@bangjiehan Better late than never :) WDYT?

@bangjiehan
Copy link
Contributor

Thank you for trying to improve this API.

TryQueryOffset could be implemented without throwing any Exception at all. Maybe put actual implementation in TryQueryOffset(exception-free, of course) and reimplement QueryOffset by wrapping TryQueryOffset(if returned value is null, thrown exception to maintain old behavior).
Throwing exception is a somehow expensive operation in C#. It create exception object, capture calling stack, search exception handlers etc. So, in normal flow, we won't throw and capture an exception to process a value we could expect.

I found StreamStats.FirstOffset() and StreamStats.CommittedChunkId() have similar issue.

@Gsantomaggio
Copy link
Member Author

Thank you @bangjiehan
I can't touch QueryOffset due to compatibility.

So you would do:

  try
            {
                var qOffset = await QueryOffset(reference, stream).ConfigureAwait(false);
                return qOffset;
            }
            catch (Exception)
            {
                return null;
            }

Isn't that misleading? I mean, any Exception?

About QueryOffset and TryQueryOffset I have to leave both the API for at least a reasonable time

@Gsantomaggio
Copy link
Member Author

I found StreamStats.FirstOffset() and StreamStats.CommittedChunkId() have similar issue.

I can't remove these API I can Add other API(s) with TryFirstOffset and TryCommittedChunkId

thank you for let me know

Signed-off-by: Gabriele Santomaggio <[email protected]>
@Gsantomaggio
Copy link
Member Author

@bangjiehan ok now TryQueryOffset is excpetion Free.

@lukebakken WDYT?

Signed-off-by: Gabriele Santomaggio <[email protected]>
Signed-off-by: Gabriele Santomaggio <[email protected]>
Signed-off-by: Gabriele Santomaggio <[email protected]>
@bangjiehan
Copy link
Contributor

bangjiehan commented Apr 16, 2025

@Gsantomaggio
Sorry, my last comment was misleading and wrong. I did not notice there are other Exceptions could be thrown by different ResponseCode. I was thinking only about OffsetNotFoundException.

My original intention is below:

public async Task<ulong> QueryOffset(string reference, string stream)
{
    ulong? offset = await TryQueryOffset(reference, stream).ConfigureAwait(false);
    return offset ?? throw new OffsetNotFoundException($"QueryOffset stream: {stream}, reference: {reference}"));
}

// Mostly copied from original QueryOffset.
public async Task<ulong?> TryQueryOffset(string reference, string stream)
{
    MaybeThrowQueryException(reference, stream);

    var response = await _client.QueryOffset(reference, stream).ConfigureAwait(false);

    // Offset do not exist so just return null. There is no need to throw an OffsetNotFoundException and capture it.
    if (response.ResponseCode = ResponseCode.OffsetNotFound)
    {
        return null;
    }

    ClientExceptions.MaybeThrowException(response.ResponseCode, $"QueryOffset stream: {stream}, reference: {reference}");
    return response.Offset;
}

These codes just to convey my intention and it still just a workaround I think.

Maybe, QueryOffsetResponse should treat 'offset not exist' as one of its results in the end.

Signed-off-by: Gabriele Santomaggio <[email protected]>
@Gsantomaggio
Copy link
Member Author

@bangjiehan makes more sense now! I will merge thank you

@Gsantomaggio Gsantomaggio merged commit 166751d into main Apr 16, 2025
2 checks passed
@Gsantomaggio Gsantomaggio deleted the try_query_offet branch April 16, 2025 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid throwing OffsetNotFoundException in StreamSystem.QueryOffset(string, string)
2 participants