lucene.net IndexWriter and Azure WebJob

本文关键字:Azure WebJob and IndexWriter net lucene | 更新日期: 2023-09-27 18:25:59

我有一个Azure网络作业在持续运行,它基于队列触发器进行触发。队列包含需要写入我的lucene索引的项目列表。我目前有很多项目在队列中(超过50万行项目),我正在寻找最有效的方法来处理它。当我试图"扩展"网络作业时,我一直会遇到IndexWriter Lock异常。

当前设置:

JobHostConfiguration config = new JobHostConfiguration();
            config.Queues.BatchSize = 1;
            var host = new JobHost(config);                        
            host.RunAndBlock();

Web作业功能

     public static void AddToSearchIndex([QueueTrigger("indexsearchadd")] List<ListingItem> items, TextWriter log)
                {
                    var azureDirectory = new AzureDirectory(CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString), "megadata");
                    var findexExists = IndexReader.IndexExists(azureDirectory);
                    var count = items.Count;
                    IndexWriter indexWriter = null;
                    int errors = 0;
                    while (indexWriter == null && errors < 10)
                    {
                        try
                        {
                            indexWriter = new IndexWriter(azureDirectory, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), !IndexReader.IndexExists(azureDirectory), new Lucene.Net.Index.IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH));
                        }
                        catch (LockObtainFailedException)
                        {
                            log.WriteLine("Lock is taken, Hit 'Y' to clear the lock, or anything else to try again");
                            errors++;
                        }
                    };
                    if (errors >= 10)
                    {
                        azureDirectory.ClearLock("write.lock");
                        indexWriter = new IndexWriter(azureDirectory, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), !IndexReader.IndexExists(azureDirectory), new Lucene.Net.Index.IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH));
 log.WriteLine("IndexWriter lock obtained, this process has exclusive write access to index");
            indexWriter.SetRAMBufferSizeMB(10.0);
            // Parallel.ForEach(items, (itm) =>
            //{
            foreach (var itm in items)
            {
                AddtoIndex(itm, indexWriter);
            }
            //});
    }

更新索引项的方法基本上如下所示:

private static void AddtoIndex(ListingItem item, IndexWriter indexWriter)
        {            
            var doc = new Document();
            doc.Add(new Field("id", item.URL, Field.Store.NO, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
            var title = new Field("Title", item.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
 indexWriter.UpdateDocument(new Term("id", item.URL), doc);
}

我尝试过的东西:

  1. 将azure配置批处理大小设置为最大32
  2. 使方法异步并使用Task.WhenAll
  3. 循环使用并行

当我尝试以上操作时,它通常会失败:

Lucene.Net.Store.LockObtainFailedException: Lucene.Net.Store.LockObtainFailedException: Lock obtain timed out: AzureLock@write.lock.
 at Lucene.Net.Store.Lock.Obtain(Int64 lockWaitTimeout) in d:'Lucene.Net'FullRepo'trunk'src'core'Store'Lock.cs:line 97
 at Lucene.Net.Index.IndexWriter.Init(Directory d, Analyzer

关于如何在架构上设置此web作业,使其能够处理队列中的更多项目,而不是逐个处理,有什么建议吗?他们需要写入相同的索引?感谢

lucene.net IndexWriter and Azure WebJob

当多个进程试图同时写入Lucene索引时,您会遇到Lucene语义问题。缩放azure应用程序,使用Tasks或parallel进行循环只会导致问题,因为一次只有一个进程应该写入Lucene索引。

建筑这是你应该做的。

  • 确保任何时候只有一个网络作业实例在运行,甚至如果Web应用程序缩放(例如通过自动缩放)
  • 使用最大Web作业批处理大小(32)
  • 在每次批处理后提交Lucene索引以最大限度地减少I/O

通过向webjob项目添加settings.job文件,确保只能完成一个webjob实例。将生成操作设置为内容并复制到输出目录。将以下JSON添加到文件

{ "is_singleton": true }

将网络作业批处理站点配置为最大

JobHostConfiguration config = new JobHostConfiguration();
config.Queues.BatchSize = 1;
var host = new JobHost(config);                        
host.RunAndBlock();

在每批之后提交Lucene索引

public static void AddToSearchIndex([QueueTrigger("indexsearchadd")] List<ListingItem> items, TextWriter log)
{
    ...
    indexWriter = new IndexWriter(azureDirectory, …);
    foreach (var itm in items)
    {
        AddtoIndex(itm, indexWriter);
    }
    indexWriter.Commit();
}

这只会在提交Lucene索引时写入存储帐户,从而加快索引过程。此外,webjob批处理还将加快消息处理(一段时间内处理的消息数量,而不是单个消息处理时间)。

您可以添加检查以查看Lucene索引是否已锁定(write.lock文件存在),并在批处理过程开始时解锁索引。这不应该发生,但一切都可能发生,所以我想补充一下,以确保万无一失。

您可以通过使用更大的Web应用程序实例(里程数可能有所不同)和使用更快的存储(如Azure Premium storage)来进一步加快索引过程。

你可以在我的博客上阅读更多关于Azure上Lucene索引的内部信息。