admin管理员组

文章数量:1410674

In the examples for using the bulk support, the tasks created by the SDK are awaited as such:

List<Task> concurrentTasks = new List<Task>();
foreach(Item itemToInsert in ReadYourData())
{
    concurrentTasks.Add(container.CreateItemAsync(itemToInsert, new PartitionKey(itemToInsert.MyPk)));
}

await Task.WhenAll(concurrentTasks);

In a more real world scenario, there might be a lot more await/async going on. Developers often awaits tasks many levels of deep, although no IO takes place. Will the SDK be able to identify its tasks it needs to use the bulk support in a scenario like this, or will they be left to their own demise and execute individually?

private async Task StartHere()
{
    var articles = new List<Article>() {10k articles};
    var saveTasks = articles.Select(async a => await Save(a));

    await Task.WhenAll(saveTasks); // Will this still use the bulk support?
}

private async Task<Article> Save(Article article)
{
    await Upsert(article, CancellationToken.None);
    return article;
}

private async Task Upsert(Article article, CancellationToken cancellationToken)
{
    await InsertOrUpdate(article, cancellationToken);
}

private async Task<string> InsertOrUpdate(T entity, CancellationToken cancellationToken)
{
    var partitionKey = new PartitionKey(entity.Id);

    await _container.UpsertItemAsync(entity, partitionKey, new ItemRequestOptions
    {
        EnableContentResponseOnWrite = false,
        IfMatchEtag = entity.ETag
    }, cancellationToken: cancellationToken);

    return entity.Id;

}

In the examples for using the bulk support, the tasks created by the SDK are awaited as such:

List<Task> concurrentTasks = new List<Task>();
foreach(Item itemToInsert in ReadYourData())
{
    concurrentTasks.Add(container.CreateItemAsync(itemToInsert, new PartitionKey(itemToInsert.MyPk)));
}

await Task.WhenAll(concurrentTasks);

In a more real world scenario, there might be a lot more await/async going on. Developers often awaits tasks many levels of deep, although no IO takes place. Will the SDK be able to identify its tasks it needs to use the bulk support in a scenario like this, or will they be left to their own demise and execute individually?

private async Task StartHere()
{
    var articles = new List<Article>() {10k articles};
    var saveTasks = articles.Select(async a => await Save(a));

    await Task.WhenAll(saveTasks); // Will this still use the bulk support?
}

private async Task<Article> Save(Article article)
{
    await Upsert(article, CancellationToken.None);
    return article;
}

private async Task Upsert(Article article, CancellationToken cancellationToken)
{
    await InsertOrUpdate(article, cancellationToken);
}

private async Task<string> InsertOrUpdate(T entity, CancellationToken cancellationToken)
{
    var partitionKey = new PartitionKey(entity.Id);

    await _container.UpsertItemAsync(entity, partitionKey, new ItemRequestOptions
    {
        EnableContentResponseOnWrite = false,
        IfMatchEtag = entity.ETag
    }, cancellationToken: cancellationToken);

    return entity.Id;

}
Share Improve this question edited Mar 6 at 8:01 David Makogon 70.9k22 gold badges145 silver badges198 bronze badges asked Mar 6 at 7:26 spanspan 5,62811 gold badges67 silver badges119 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

It doesn't matter how you call the methods. You can basically think of it in the following (simplified) way; There's a queue where all your requests are gathered. Once it's big enough for a batch or after a certain delay it'll pick up the requests and send them as a batch.

All of it is managed by the SDK so whether you use async/await in one way, another, or not at all doesn't matter.

本文标签: cHow does the net CosmosDb SDK handle tasks in bulk supportStack Overflow