无限结果的RavenDB流 - 连接恢复能力

Luk*_*ett 23 c# ravendb

我们在RavenDB中使用Stream功能在两个数据库之间加载,转换和迁移数据,如下所示:

var query = originSession.Query<T>(IndexForQuery);

using (var stream = originSession.Advanced.Stream(query))
{
    while (stream.MoveNext())
    {
        var streamedDocument = stream.Current.Document;

        OpenSessionAndMigrateSingleDocument(streamedDocument);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是其中一个集合有数百万行,我们继续IOException以下列格式接收:

Application: MigrateToNewSchema.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
Stack:
   at System.Net.ConnectStream.Read(Byte[], Int32, Int32)
   at System.IO.Compression.DeflateStream.Read(Byte[], Int32, Int32)
   at System.IO.Compression.GZipStream.Read(Byte[], Int32, Int32)
   at System.IO.StreamReader.ReadBuffer(Char[], Int32, Int32, Boolean ByRef)
   at System.IO.StreamReader.Read(Char[], Int32, Int32)
   at Raven.Imports.Newtonsoft.Json.JsonTextReader.ReadData(Boolean, Int32)
   at Raven.Imports.Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer(Char)
   at Raven.Imports.Newtonsoft.Json.JsonTextReader.ParseString(Char)
   at Raven.Imports.Newtonsoft.Json.JsonTextReader.ParseValue()
   at Raven.Imports.Newtonsoft.Json.JsonTextReader.ReadInternal()
   at Raven.Imports.Newtonsoft.Json.JsonTextReader.Read()
   at Raven.Json.Linq.RavenJObject.Load(Raven.Imports.Newtonsoft.Json.JsonReader)
   at Raven.Json.Linq.RavenJObject.Load(Raven.Imports.Newtonsoft.Json.JsonReader)
   at Raven.Json.Linq.RavenJToken.ReadFrom(Raven.Imports.Newtonsoft.Json.JsonReader)
   at Raven.Client.Connection.ServerClient+<YieldStreamResults>d__6b.MoveNext()
   at Raven.Client.Document.DocumentSession+<YieldQuery>d__c`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].MoveNext()
   at MigrateToNewSchema.Migrator.DataMigratorBase`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].MigrateCollection()
   at MigrateToNewSchema.Program.MigrateData(MigrateToNewSchema.Enums.CollectionToMigrate, Raven.Client.IDocumentStore, Raven.Client.IDocumentStore)
   at MigrateToNewSchema.Program.Main(System.String[])
Run Code Online (Sandbox Code Playgroud)

这在流媒体中发生了相当长的一段时间,当然在这种情况下会发生瞬时连接问题(需要几个小时才能完成).

但是,当我们重试时,因为我们正在使用,Query我们必须从头开始.因此,如果在整个过程中出现连接失败,Stream那么我们必须再次尝试,并再次尝试直到它端到端工作.

我知道你可以使用ETagstream来在某个时刻有效地重新启动,但是Query我们需要过滤正在迁移的结果并指定正确的集合.

那么,在RavenDB中,有没有办法改善连接的内部弹性(连接字符串属性,内部设置等)或有效地"恢复"错误的流?

Luk*_*ett 2

根据 @StriplingWarrior 的建议,我使用Data Subscriptions重新创建了解决方案。

使用这种方法,我能够迭代所有 200 万行(尽管无可否认,每个项目的处理量要少得多);当我们尝试使用 Streams 实现相同的逻辑时,有两点会有所帮助:

  1. 一旦确认,批次只会从订阅“队列”中删除(就像大多数标准队列一样)
    1. 订阅IObserver<T>必须成功完成才能设置此确认。
    2. 此信息由服务器而不是客户端处理,因此允许客户端重新启动,而不会影响订阅中处理的最后一个成功位置
    3. 请参阅此处了解更多详情
  2. 正如 @StriplingWarrior 所指出的,因为您可以使用过滤器创建订阅,直到属性级别,所以在订阅本身发生异常的情况下,可以使用较小的结果集重播。
    1. 第一点确实取代了这一点;但它为我们提供了 Stream API 中没有的额外灵活性

测试环境是 RavenDB 3.0 数据库(本地计算机,作为 Windows 服务运行),默认设置为 200 万条记录的集合。

生成虚拟记录的代码:

using (IDocumentStore store = GetDocumentStore())
{
    store.Initialize();

    using (var bulkInsert = store.BulkInsert())
    {
        for (var i = 0; i != recordsToCreate; i++)
        {
            var person = new Person
            {
                Id = Guid.NewGuid(),
                Firstname = NameGenerator.GenerateFirstName(),
                Lastname = NameGenerator.GenerateLastName()
            };

            bulkInsert.Store(person);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

订阅该集合的情况如下:

using (IDocumentStore store = GetDocumentStore())
{
    store.Initialize();

    var subscriptionId = store.Subscriptions.Create(new SubscriptionCriteria<Person>());

    var personSubscription = store.Subscriptions.Open<Person>(
        subscriptionId, new SubscriptionConnectionOptions()
    {
        BatchOptions = new SubscriptionBatchOptions()
        {
            // Max number of docs that can be sent in a single batch
            MaxDocCount = 16 * 1024,  
            // Max total batch size in bytes
            MaxSize = 4 * 1024 * 1024,
            // Max time the subscription needs to confirm that the batch
            // has been successfully processed
            AcknowledgmentTimeout = TimeSpan.FromMinutes(3)
        },
        IgnoreSubscribersErrors = false,
        ClientAliveNotificationInterval = TimeSpan.FromSeconds(30)
    });

    personSubscription.Subscribe(new PersonObserver());

    while (true)
    {
        Thread.Sleep(TimeSpan.FromMilliseconds(500));
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意PersonObserver; 这只是 IObserver 的基本实现,如下所示:

public class PersonObserver : IObserver<Person>
{
    public void OnCompleted()
    {
        Console.WriteLine("Completed");
    }

    public void OnError(Exception error)
    {
        Console.WriteLine("Error occurred: " + error.ToString());
    }

    public void OnNext(Person person)
    {
        Console.WriteLine($"Received '{person.Firstname} {person.Lastname}'");
    }
}
Run Code Online (Sandbox Code Playgroud)