Skip to content

Commit

Permalink
Retry requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gloomweaver committed Jun 18, 2024
1 parent 7f372b6 commit 07e3bcf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions Spice/Spice.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Apache.Arrow" Version="16.1.0"/>
<PackageReference Include="Apache.Arrow.Flight" Version="16.1.0"/>
<PackageReference Include="Polly" Version="8.4.0"/>
</ItemGroup>


Expand Down
38 changes: 26 additions & 12 deletions Spice/src/Flight/SpiceFlightClient.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/*
Copyright 2024 The Spice.ai OSS Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -25,14 +22,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using Apache.Arrow.Flight.Client;
using Grpc.Core;
using Grpc.Net.Client;
using Polly;
using Polly.Retry;
using Spice.Auth;
using Spice.Errors;

namespace Spice.Flight;

internal class SpiceFlightClient
{
private readonly FlightClient _flightClient;
private readonly AsyncRetryPolicy _retryPolicy;

private static GrpcChannelOptions GetGrpcChannelOptions(string? appId, string? apiKey)
{
Expand All @@ -57,8 +56,19 @@ private static GrpcChannelOptions GetGrpcChannelOptions(string? appId, string? a
return responseHeaders.Get("authorization") ?? trailers.Get("authorization");
}

internal SpiceFlightClient(string address, string? appId, string? apiKey)
internal SpiceFlightClient(string address, int maxRetries, string? appId, string? apiKey)
{
_retryPolicy = Policy.Handle<RpcException>(ex =>
ex.Status.StatusCode is StatusCode.Unavailable or StatusCode.DeadlineExceeded or StatusCode.Aborted
or StatusCode.Internal or StatusCode.Unknown)
.WaitAndRetryAsync(retryCount: maxRetries,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(retryAttempt * 1.5),
onRetry: (_, timespan, retryAttempt, _) =>
{
Console.WriteLine(
$"Request failed. Waiting {timespan} before next retry. Retry attempt {retryAttempt}");
});

var options = GetGrpcChannelOptions(appId, apiKey);

_flightClient = new FlightClient(GrpcChannel.ForAddress(address, options));
Expand All @@ -73,7 +83,7 @@ internal SpiceFlightClient(string address, string? appId, string? apiKey)
stream.ResponseHeadersAsync.Wait();

var token = GetAuthToken(stream.ResponseHeadersAsync.Result, stream.GetTrailers());
if (token == null || options.HttpClient == null) throw new SpiceException(SpiceStatus.FailedToAuthenticate, "Failed to authenticate");
if (token == null || options.HttpClient == null) throw new Exception("Failed to authenticate token");

options.HttpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(token.Value);
}
Expand All @@ -84,13 +94,17 @@ internal async Task<FlightClientRecordBatchStreamReader> Query(string sql)
{
throw new ArgumentException("No SQL provided");
}
var descriptor = FlightDescriptor.CreateCommandDescriptor(sql);

var flightInfo = await _flightClient.GetInfo(descriptor);
var endpoint = flightInfo.Endpoints.FirstOrDefault();
if (endpoint == null) throw new SpiceException(SpiceStatus.SpiceFlightError, "Failed to get endpoint from flightInfo");
return await _retryPolicy.ExecuteAsync(async () =>
{
var descriptor = FlightDescriptor.CreateCommandDescriptor(sql);
var flightInfo = await _flightClient.GetInfo(descriptor);

var endpoint = flightInfo.Endpoints.FirstOrDefault();
if (endpoint == null) throw new Exception("Failed to get endpoint");

var stream = _flightClient.GetStream(endpoint.Ticket);
return stream.ResponseStream;
var stream = _flightClient.GetStream(endpoint.Ticket);
return stream.ResponseStream;
});
}
}
3 changes: 1 addition & 2 deletions Spice/src/SpiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace Spice;

public class SpiceClient
{

/// <summary>
/// Gets or sets the application ID. This property is internal set and can be null.
/// </summary>
Expand All @@ -53,7 +52,7 @@ public class SpiceClient

internal void Init()
{
FlightClient = new SpiceFlightClient(FlightAddress, AppId, ApiKey);
FlightClient = new SpiceFlightClient(FlightAddress, MaxRetries, AppId, ApiKey);
}

/// <summary>
Expand Down

0 comments on commit 07e3bcf

Please sign in to comment.