Azure SDK中是否有任何RetryPolicy可以自动处理继续令牌?

发布于 2025-01-08 04:01:23 字数 90 浏览 3 评论 0 原文

对于查询,有时我会得到延续令牌,我想知道是否有针对 TableServiceContext 的 RetryPolicy 设置来自动处理令牌。

For query, sometimes I got continuation token, I wonder if there is any RetryPolicy settings against TableServiceContext to handle the token automatically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵山崖 2025-01-15 04:01:23

您不需要为此使用 RetryPolicy。有两个选项:

在查询中使用 .AsTableServiceQuery()。这会将您的查询转换为 CloudTableQuery<>对象,它本机处理延续标记。这是最简单的路线。例如:

var query = (from r in Rows
             where r.PartitionKey == "whatever"
             select r).AsTableServiceQuery();

否则您可以使用 Begin/EndExecuteSegmented() 并自己处理令牌。

对 CloudTableQuery 的说明<>

有一个对 CloudTableQuery<> 行为的间接引用。在 Scott Densmore 的博客上。但我还整理了以下相当混乱的代码来证明这一点。测试通过,并且它确实使用延续令牌来检索所有插入的实体。如果您使用 HTTP,您可以使用 Fiddler 进行观察,并看到令牌来回变化。

        [Test, Explicit]
        public void WriteAndReadALotOfRows()
        {
            CloudStorageAccount acct = CloudStorageAccount.Parse("PUT IN SOME CREDS HERE");
            TableServiceContext ctx = null;
            List<TestEntity> testEntities = new List<TestEntity>(2000);

            acct.CreateCloudTableClient().CreateTableIfNotExist("Test");

            //Create entities
            for (int i = 0; i < 2000; i++)
            {
                if (i % 100 == 0)
                {
                    if (ctx != null)
                    {
                        ctx.SaveChangesWithRetries(SaveChangesOptions.Batch);
                    }

                    ctx = new TableServiceContext(acct.TableEndpoint.AbsoluteUri, acct.Credentials);
                }
                TestEntity entity = new TestEntity(i);
                testEntities.Add(entity);
                ctx.AddObject("Test", entity);
            }

            ctx.SaveChangesWithRetries(SaveChangesOptions.Batch);

            ctx = new TableServiceContext(acct.TableEndpoint.AbsoluteUri, acct.Credentials);

            List<TestEntity> retrievedEntities = (from r in ctx.CreateQuery<TestEntity>("Test")
                                                  select r).AsTableServiceQuery().ToList();

            Assert.AreEqual(testEntities.Count, retrievedEntities.Count);
            Console.Out.WriteLine(retrievedEntities.Count); //prints 2000

            foreach (var insertedEntity in testEntities)
            {
                TestEntity retrievedEntity = retrievedEntities.First(r => r.RowKey == insertedEntity.RowKey);
                Assert.NotNull(retrievedEntity);
            }
        }

        public class TestEntity : TableServiceEntity
        {
            public TestEntity()
            {
            }

            public TestEntity(int id)
                : base("Test", id.ToString())
            {

            }
        }

You don't need to use a RetryPolicy for that. There are two options:

Use .AsTableServiceQuery() on your queries. That converts your query into a CloudTableQuery<> object, which natively handles continuation tokens. This is the easiest route. eg:

var query = (from r in Rows
             where r.PartitionKey == "whatever"
             select r).AsTableServiceQuery();

Otherwise you can use Begin/EndExecuteSegmented() and handle the tokens yourself.

Clarification on CloudTableQuery<>

There's an oblique reference to the behavior of CloudTableQuery<> on Scott Densmore's blog. But I also threw together the following, rather messy, code to prove it. The tests pass, and it does use continuation tokens to retrieve all the inserted entities. If you use HTTP, you can watch it with Fiddler and see the tokens go back and forth.

        [Test, Explicit]
        public void WriteAndReadALotOfRows()
        {
            CloudStorageAccount acct = CloudStorageAccount.Parse("PUT IN SOME CREDS HERE");
            TableServiceContext ctx = null;
            List<TestEntity> testEntities = new List<TestEntity>(2000);

            acct.CreateCloudTableClient().CreateTableIfNotExist("Test");

            //Create entities
            for (int i = 0; i < 2000; i++)
            {
                if (i % 100 == 0)
                {
                    if (ctx != null)
                    {
                        ctx.SaveChangesWithRetries(SaveChangesOptions.Batch);
                    }

                    ctx = new TableServiceContext(acct.TableEndpoint.AbsoluteUri, acct.Credentials);
                }
                TestEntity entity = new TestEntity(i);
                testEntities.Add(entity);
                ctx.AddObject("Test", entity);
            }

            ctx.SaveChangesWithRetries(SaveChangesOptions.Batch);

            ctx = new TableServiceContext(acct.TableEndpoint.AbsoluteUri, acct.Credentials);

            List<TestEntity> retrievedEntities = (from r in ctx.CreateQuery<TestEntity>("Test")
                                                  select r).AsTableServiceQuery().ToList();

            Assert.AreEqual(testEntities.Count, retrievedEntities.Count);
            Console.Out.WriteLine(retrievedEntities.Count); //prints 2000

            foreach (var insertedEntity in testEntities)
            {
                TestEntity retrievedEntity = retrievedEntities.First(r => r.RowKey == insertedEntity.RowKey);
                Assert.NotNull(retrievedEntity);
            }
        }

        public class TestEntity : TableServiceEntity
        {
            public TestEntity()
            {
            }

            public TestEntity(int id)
                : base("Test", id.ToString())
            {

            }
        }
旧城烟雨 2025-01-15 04:01:23

查看http://blogs.msdn.com/b/windowsazurestorage/archive/2010/07/09/understanding-windows-azure-storage-billing-bandwidth-transactions-and-capacity.aspx 文中提到:

表查询 – 当您使用 CloudTableQuery 进行查询时,它会处理
处理连续令牌,因此它使用以下命令重新发出查询
在先前的查询请求中收到的继续令牌以获取
剩余实体。如上所述,每个重新发布的延续
对服务的令牌查询计为 1 笔交易。

另外http://blogs.msdn.com/b/jimoneil/archive/2010/10/05/azure-home-part-7-asynchronous-table-storage-pagination.aspxhttp://scottdensmore.typepad.com/blog/2010/04/paging-with-windows-azure-table-storage.html

check out http://blogs.msdn.com/b/windowsazurestorage/archive/2010/07/09/understanding-windows-azure-storage-billing-bandwidth-transactions-and-capacity.aspx within the text it mentions:

Table Queries – When you query using CloudTableQuery it takes care of
handling continuation tokens, so it reissues queries using the
continuation token receieved in a previous query request to get
remaining entities. As described above, each reissued continuation
token query to the service counts as 1 transaction.

Also http://blogs.msdn.com/b/jimoneil/archive/2010/10/05/azure-home-part-7-asynchronous-table-storage-pagination.aspx and http://scottdensmore.typepad.com/blog/2010/04/paging-with-windows-azure-table-storage.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文