2

私は紺碧のテーブルストレージを使用しており、テーブルをすばやく反復処理しようとしています。

私はそれを間違っているに違いありませんが、私は一生の間、その理由を理解することはできません。

単一のパーティションのみを指定すると、複数のパーティションの結果が返されることになります。つまり、「pkey1」を使用してクエリを制約すると、「pkey1」で1000件の結果が返され、「pkey2」で325件の結果が返されます。

これがどのように起こり得るかについて完全に混乱しています。

これは私が使用しているコードです:

private CloudTableClient _client;
private string _tableName;
private class QueryState
{
    public CloudTableQuery<T> Ctq;
    public Action<IEnumerable<T>> Populator;
    public ManualResetEvent Mre;
    public string Pkey;

    public QueryState(CloudTableQuery<T> ctq, Action<IEnumerable<T>> populator, ManualResetEvent mre, string pkey)
    {
        Populator = populator;
        Ctq = ctq;
        Mre = mre;
        Pkey = pkey;
    }
}

    public void ParallelQueryWithClause(Action<IEnumerable<T>> populator, string[] partitionKeys)
    {
        List<ManualResetEvent> mre = new List<ManualResetEvent>();
        foreach (string pKey in partitionKeys)
        {
            //_retry.Go(tsc =>
            //    {
                    TableServiceContext tsc =  _client.GetDataServiceContext();
                    ManualResetEvent m = new ManualResetEvent(false);
                    mre.Add(m);
                    CloudTableQuery<T> query = tsc.CreateQuery<T>(_tableName).Where(e => e.PartitionKey == pKey).AsTableServiceQuery<T>();
                    Action<IAsyncResult> act  = null;
                    act = result =>
                        {
                            int retries = 0;
                            while (retries++ < 5)
                            {
                                try
                                {
                                    QueryState qsInternal = result.AsyncState as QueryState;
                                    CloudTableQuery<T> ctq = qsInternal.Ctq;
                                    ResultSegment<T> seg = ctq.EndExecuteSegmented(result);
                                    if (seg.Results.Count() > 0)
                                        populator(seg.Results);
                                    if (seg.ContinuationToken != null)
                                    {
                                        ctq.BeginExecuteSegmented(seg.ContinuationToken, iasync => act(iasync), qsInternal);
                                    }
                                    else
                                    {
                                        m.Set();
                                    }
                                    break;
                                }
                                catch(Exception ex)
                                {
                                    Logger.LogError(ex);
                                }
                            }
                        };
                    query.BeginExecuteSegmented(iasync => act(iasync), new QueryState(query, populator, m, pKey));
                //});
        }
        ManualResetEvent.WaitAll(mre.ToArray());
    }

そして、サンプルの呼び出しコード:

AzureTableStorage<ProductEntity> _ats = new AzureTableStorage<ProductEntity>("Products");
string[] partitions = new string[] { "pkey1" };

    Dictionary<string, int> cntr = new Dictionary<string, int>();
    _ats.ParallelQueryWithClause(p =>
    {
        lock (cntr)
        {
            foreach (ProductEntity pe in p)
            {
                if (cntr.ContainsKey(pe.PartitionKey))
                    cntr[pe.PartitionKey]++;
                else
                    cntr.Add(pe.PartitionKey, 1);
            }
        }
    }, partitions);

うまくいけば、これは理にかなっており、誰かが助けることができます!

4

1 に答える 1

1

クロージャ値が変更されている状況に遭遇している可能性があります。 http://marlongrech.wordpress.com/2010/06/02/closures-in-c-can-be-evil/ これは、nullのpKeyを使用してクエリを実行し、実質的にpKeyによるフィルタリングを行わないため、すべての表の値。

交換してみてください

foreach (string pKey in partitionKeys)

foreach (string pKeyTmp in partitionKeys)
{
   string pKey = pKeyTmp;
于 2012-04-29T07:49:18.643 に答える