我有这个代码,运行几次迭代后会出现以下错误。 有什么想法吗?
我正在使用Couchbase 3.0.1和.NET SDK 2.0
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Couchbase;
using Couchbase.Core;
namespace CouchTest2
{
class Program
{
static void Main(string[] args)
{
var s = new Stopwatch();
s.Start();
var result = Parallel.For(0, 10000, ctr =>
{
var id = Guid.NewGuid().ToString();
CouchbaseClient.Instance.Bucket.Insert(id, id, ReplicateTo.Zero, PersistTo.One);
});
s.Stop();
Console.WriteLine(s.Elapsed);
Console.Read();
}
}
[Serializable]
public sealed class CouchbaseClient
{
public static readonly CouchbaseClient Instance = new CouchbaseClient();
public IBucket Bucket;
private CouchbaseClient()
{
Bucket = new Cluster("couchbaseClients/couchbase").OpenBucket("default");
}
}
}
并行循环在异常之前会随机运行20-30次。
错误:
未处理的异常:System.AggregateException:一个或多个错误 发生了。 ---
System.AggregateException:发生了一个或多个错误。 ---> System.AggregateE xception:发生了一个或多个错误。 ---> System.ArgumentOutOfRangeException:索引和计数必须引用a 缓冲区内的位置。参数名称:字节位于 System.Text.UTF8Encoding.GetString(Byte [] bytes,Int32 index,Int32 计数)
在Couchbase.IO.Converters.AutoByteConverter.ToString(Byte [] 缓冲区,fset的Int32,Int32长度)in C:\詹金斯\工作空间NET2 \ couchbase净客户\ SRC \ Couchb ase \ IO \ Converters \ AutoByteConverter.cs:第83行 Couchbase.IO.Operations.Observe.GetValue()in C:\詹金斯\工作空间NET2 \共 uchbase-net-client \ Src \ Couchbase \ IO \ Operations \ Observe.cs:第49行 在Couchbase.IO.Operations.OperationBase
1.GetResult() in c:\jenkins\workspac e-net2\couchbase-net-client\Src\Couchbase\IO\Operations\OperationBase'.cs:line 2 84 at Couchbase.Core.Server.Send[T](IOperation
1操作)中 C:\詹金斯\ worksp ace-net2 \ couchbase-net-client \ Src \ Couchbase \ Core \ Server.cs:第97行 在 Couchbase.Core.Buckets.KeyObserver&LT;&GT; c__DisplayClass12.b__10 ()in C:\詹金斯\工作空间NET2 \ couchbase净客户\ SRC \ Couchbase \核心\铲斗\ KeyObserver.cs:第204行 System.Threading.Tasks.Task1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot ification(Task task) at Couchbase.Core.Buckets.KeyObserver.<CheckReplica>d__14.MoveNext() in c:\je nkins\workspace-net2\couchbase-net-client\Src\Couchbase\Core\Buckets\KeyObserver .cs:line 0 --- End of inner exception stack trace --- at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeou t, CancellationToken cancellationToken) at Couchbase.Core.Buckets.KeyObserver.<>c__DisplayClass6.<Observe>b__1(Observ eParams p) in c:\jenkins\workspace-net2\couchbase-net-client\Src\Couchbase\Core\ Buckets\KeyObserver.cs:line 162 at Couchbase.Core.Buckets.KeyObserver.<ObserveEvery>d__18.MoveNext() in c:\je nkins\workspace-net2\couchbase-net-client\Src\Couchbase\Core\Buckets\KeyObserver .cs:line 233 --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceled Exceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationTo ken cancellationToken) at Couchbase.Core.Buckets.KeyObserver.Observe(String key, UInt64 cas, Boolean deletion, ReplicateTo replicateTo, PersistTo persistTo) in c:\jenkins\workspace -net2\couchbase-net-client\Src\Couchbase\Core\Buckets\KeyObserver.cs:line 169 at Couchbase.CouchbaseBucket.Observe(String key, UInt64 cas, Boolean deletion , ReplicateTo replicateTo, PersistTo persistTo) in c:\jenkins\workspace-net2\cou chbase-net-client\Src\Couchbase\CouchbaseBucket.cs:line 281 at Couchbase.CouchbaseBucket.SendWithDurability[T](IOperation
1 操作,Bo olean删除,ReplicateTo replicateTo,PersistTo persistTo)在c:\ jenkins \ work中 空间-NET2 \ couchbase网客户端的\ src \ Couchbase \ CouchbaseBucket.cs:行 299 at Couchbase.CouchbaseBucket.Insert [T](String key,T value, ReplicateTo repli cateTo,PersistTo persistTo)in C:\詹金斯\工作空间NET2 \ couchbase净客户\ S rc \ Couchbase \ CouchbaseBucket.cs:第832行 CouchTest2.Program.b__0(Int32 ctr)at System.Threading.Tasks.Parallel。&lt;&gt; c__DisplayClassf1.<ForWorker>b__c() at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask) at System.Threading.Tasks.Task.<>c__DisplayClass11.<ExecuteSelfReplicating>b_ _10(Object param0) --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceled Exceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationTo ken cancellationToken) at System.Threading.Tasks.Task.Wait() at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int 32 toExclusive, ParallelOptions parallelOptions, Action
1个身体, System.Threading.Tasks.Parallel.For(Int32)中的动作2 bodyWit hState, Func
4 bodyWithLocal,Func1 localInit, Action
1 localFinally) fromInclusive,Int32 toExclusive,Action`1 body)at CouchTest2.Program.Main(String [] args)
答案 0 :(得分:1)
所以问题在于,通过使用具有默认客户端配置的无界并行循环,您正在耗尽连接池,并且各个操作在客户端上超时(在通过网络发送之前)。不幸的是,当操作超时时,客户端中存在一个抛出AOOR的错误,很快就会修复。
话虽这么说,通过配置调整客户端,我能够按预期工作:
using (var cluster = new Cluster(new ClientConfiguration
{
PoolConfiguration = new PoolConfiguration
{
MaxSize = 10,
MinSize = 5
}
}))
{
var s = new Stopwatch();
s.Start();
using (var bucket = cluster.OpenBucket())
{
var result = Parallel.For(0, 10000, ctr =>
{
var id = Guid.NewGuid().ToString();
bucket.Insert(id, id, ReplicateTo.Zero, PersistTo.One);
});
}
s.Stop();
}
请注意,默认配置会创建一个具有两个TCP连接的连接池,这可能会很快耗尽。在这种情况下,每个线程将尝试最多5次(默认情况下)以获取连接。如果不能,则操作将因ClientError失败而失败,因为请求从未进入网络。您可以通过增加PoolConfiguration.MaxAcquireIterationCount值来调整线程尝试获取连接的次数迭代。