2017-10-17 38 views
0

由于我预计随着时间的推移,行数将增长到数以百万计,因此我目前正在测试Google Cloud Spanner作为一个项目的MySQL替代方案。数据库需要响应速度非常快,并在几秒钟内返回查询结果,所以我想我会给Spanner一个尝试。谷歌云扳手批量插入过程中的瞬态故障异常

我试图批量加载样本数据来我的扳手DB,但我不断收到此错误:

Unhandled Exception: System.AggregateException: One or more errors occurred. ---> Google.Cloud.Spanner.Data.SpannerException: The operation was aborted. ---> Grpc.Core.RpcException: Status(StatusCode=Aborted, Detail="Aborted due to transient fault")

我使用的是性能稍微修改后的代码从这里:https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/master/spanner/api/Program.cs

下面是代码修改在InsertSampleData

public static object InsertSampleData(string projectId, 
     string instanceId, string databaseId) 
    { 
     // I get about 100k rows here 
     List<Data> data = get_data(); 

     // how many runs I need to make if I split the data by 100 rows 
     int rows = 100; 
     double cnt = (double)data.Count/rows; 
     cnt = Math.Ceiling(cnt); 

     // process the data part by part 
     for (int i = 0; i < cnt; i++) 
     { 
      // returns part of the data based on offset and amount 
      List<Data> data_part = get_part(data, i, rows); 

      var response = InsertTradesAsync(
      projectId, instanceId, databaseId, data_part); 
      s_logger.Info("Waiting for operation to complete..."); 
      response.Wait(); 
      s_logger.Info($"Operation status: {response.Status}"); 
     } 

     return ExitCode.Success; 
    } 

InsertTradesAsync是一样的,在回购(除了当然参数列表)。

当我运行的代码,我总是得到以下错误:

Unhandled Exception: System.AggregateException: One or more errors occurred. ---> Google.Cloud.Spanner.Data.SpannerException: The operation was aborted. ---> Grpc.Core.RpcException: Status(StatusCode=Aborted, Detail="Aborted due to transient fault") 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) 
    at Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass0_0`2.<<WithRetry>b__0>d.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Google.Cloud.Spanner.V1.Internal.ExecuteHelper.<WithSessionChecking>d__0`1.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Google.Cloud.Spanner.V1.TransactionPool.<RunFinalMethodAsync>d__9`1.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Google.Cloud.Spanner.Data.SpannerTransaction.<<CommitAsync>b__29_0>d.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Google.Cloud.Spanner.Data.ExecuteHelper.<WithErrorTranslationAndProfiling>d__2`1.MoveNext() 
    --- End of inner exception stack trace --- 
    at Google.Cloud.Spanner.Data.ExecuteHelper.<WithErrorTranslationAndProfiling>d__2`1.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Google.Cloud.Spanner.Data.EphemeralTransaction.<>c__DisplayClass2_0.<<ExecuteMutationsAsync>b__1>d.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Google.Cloud.Spanner.Data.ExecuteHelper.<WithErrorTranslationAndProfiling>d__2`1.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Google.Cloud.Spanner.Data.SpannerCommand.<ExecuteMutationsAsync>d__49.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
    at GoogleCloudSamples.Spanner.Program.<InsertTradesAsync>d__25.MoveNext() in c:\Users\user\Documents\Dev\spanner\api\Program.cs:line 1298 
    --- End of inner exception stack trace --- 
    at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) 
    at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) 
    at System.Threading.Tasks.Task.Wait() 
    at GoogleCloudSamples.Spanner.Program.InsertSampleData(String projectId, String instanceId, String databaseId) in c:\Users\user\Documents\Dev\spanner\api\Program.cs:line 1585 
    at GoogleCloudSamples.Spanner.Program.<>c__DisplayClass45_0.<Main>b__2(InsertSampleDataOptions opts) in c:\Users\user\Documents\Dev\spanner\api\Program.cs:line 1932 
    at CommandLine.ParserResultExtensions.MapResult[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,TResult](ParserResult`1 result, Func`2 parsedFunc1, Func`2 parsedFunc2, Func`2 parsedFunc3, Func`2 parsedFunc4, Func`2 parsedFunc5, Func`2 parsedFunc6, Func`2 parsedFunc7, Func`2 parsedFunc8, Func`2 parsedFunc9, Func`2 parsedFunc10, Func`2 parsedFunc11, Func`2 parsedFunc12, Func`2 parsedFunc13, Func`2 parsedFunc14, Func`2 parsedFunc15, Func`2 parsedFunc16, Func`2 notParsedFunc) 
    at GoogleCloudSamples.Spanner.Program.Main(String[] args) in c:\Users\user\Documents\Dev\spanner\api\Program.cs:line 1910 

我想这可能与限制和配额(https://cloud.google.com/spanner/quotas),但是异常不同量的行被插入后抛出我的桌子(这似乎是随机的,有时发生在68次运行后,每次100行,然后是28x100,52x100等)。该表有30列,PK由2列(没有索引)组成,我正在处理100个数据,所以我认为我没有达到极限。

如果我将cmd.CommandTimeout设置为非常高的数字,我得到更高的插入行(大约400x100) - 我假设客户端库重用连接?但是我找不到任何有关C#库的信息。即使插入了更多行,错误仍然会发生。

任何帮助将非常感激。

谢谢!

回答

0

Google开发者在这里。 我有几点建议可以帮助你。

首先是您应该使用单个事务并尽可能多地添加写入。它看起来像你一次做100个?但你可以做更多。你可以看到有多少写在这里可以批量限制(我相信这是20,000):

https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/master/spanner/api/Program.cs#L1242

第二项建议直接解决你的问题。因为如何扳手的作品,您需要使用您的交易围绕重如在这里例如:

https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/master/spanner/api/Program.cs#L1259

(点击此处下载瞬时故障应用程序的NuGet): https://www.nuget.org/packages/EnterpriseLibrary.TransientFaultHandling/

你需要这个,因为扳手可能偶尔会遇到一个僵局,迫使您重新运行整个交易。我们提供例外的扩展方法“IsTransientSpannerFault”,使其更容易构建您重试的政策,如:

internal class CustomTransientErrorDetectionStrategy 
     : ITransientErrorDetectionStrategy 
    { 
     public bool IsTransient(Exception ex) => 
      ex.IsTransientSpannerFault(); 
    } 

希望这有助于!

编辑:我只是注意到,你不是在等待异步调用的结果InsertTradesAsync。你可能想至少在最后做一个Task.WaitAll。请注意,每个批次的100次写入操作都可能与前一批次并行运行。这很可能是增加了导致重试的僵局的可能性。

如果你这样做的目的,以提高性能,在幕后池扳手ADO.NET库打开多个连接等于SpannerConnection.SpannerOptions.MaximumGrpcChannels。所以你会看到一定的收获。您可以增加此值来调整您的表现。

关于

+0

谢谢你,这是非常有帮助的。它现在运行正常直到结束。任务对我来说还是很新的,但我相信我会等待结果 - 在'response.Wait();',不是吗? –

+0

你是对的。我完全错过了!您通常希望一直保持异步。所以稍微好一点的实现是: public static async任务 InsertSampleData(string projectId, string instanceId,string databaseId) { 。 。 。 (int i = 0; i