/
MapReduceIndex.cs
537 lines (487 loc) · 17.3 KB
/
MapReduceIndex.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//-----------------------------------------------------------------------
// <copyright file="MapReduceIndex.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Raven.Abstractions.Logging;
using Raven.Database.Extensions;
using Raven.Database.Plugins;
using Raven.Imports.Newtonsoft.Json;
using Raven.Abstractions;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Abstractions.Linq;
using Raven.Database.Data;
using Raven.Database.Linq;
using Raven.Database.Storage;
using Raven.Json.Linq;
using Raven.Abstractions.Extensions;
namespace Raven.Database.Indexing
{
public class MapReduceIndex : Index
{
readonly JsonSerializer jsonSerializer;
public MapReduceIndex(Directory directory, string name, IndexDefinition indexDefinition,
AbstractViewGenerator viewGenerator, WorkContext context)
: base(directory, name, indexDefinition, viewGenerator, context)
{
jsonSerializer = new JsonSerializer();
foreach (var jsonConverter in Default.Converters)
{
jsonSerializer.Converters.Add(jsonConverter);
}
}
public override bool IsMapReduce
{
get { return true; }
}
private class MapResultItem
{
public string DocId;
public RavenJObject Data;
public string ReduceKey;
public int Bucket;
}
public override void IndexDocuments(
AbstractViewGenerator viewGenerator,
IndexingBatch batch,
IStorageActionsAccessor actions,
DateTime minimumTimestamp)
{
var count = 0;
var sourceCount = 0;
var sw = Stopwatch.StartNew();
var start = SystemTime.UtcNow;
var deleted = new HashSet<ReduceKeyAndBucket>();
var documentsWrapped = batch.Docs.Select(doc =>
{
sourceCount++;
var documentId = doc.__document_id;
actions.MapReduce.DeleteMappedResultsForDocumentId((string)documentId, name, deleted);
return doc;
})
.Where(x => x is FilteredDocument == false)
.ToList();
var allReferencedDocs = new ConcurrentQueue<IDictionary<string, HashSet<string>>>();
if (documentsWrapped.Count > 0)
actions.MapReduce.UpdateRemovedMapReduceStats(name, deleted);
var allState = new ConcurrentQueue<Tuple<HashSet<ReduceKeyAndBucket>, IndexingWorkStats, Dictionary<string, int>>>();
BackgroundTaskExecuter.Instance.ExecuteAllBuffered(context, documentsWrapped, partition =>
{
var localStats = new IndexingWorkStats();
var localChanges = new HashSet<ReduceKeyAndBucket>();
var statsPerKey = new Dictionary<string, int>();
allState.Enqueue(Tuple.Create(localChanges, localStats, statsPerKey));
using (CurrentIndexingScope.Current = new CurrentIndexingScope(LoadDocument, allReferencedDocs.Enqueue))
{
// we are writing to the transactional store from multiple threads here, and in a streaming fashion
// should result in less memory and better perf
context.TransactionalStorage.Batch(accessor =>
{
var mapResults = RobustEnumerationIndex(partition, viewGenerator.MapDefinitions, localStats);
var currentDocumentResults = new List<object>();
string currentKey = null;
foreach (var currentDoc in mapResults)
{
var documentId = GetDocumentId(currentDoc);
if (documentId != currentKey)
{
count += ProcessBatch(viewGenerator, currentDocumentResults, currentKey, localChanges, accessor, statsPerKey);
currentDocumentResults.Clear();
currentKey = documentId;
}
currentDocumentResults.Add(new DynamicJsonObject(RavenJObject.FromObject(currentDoc, jsonSerializer)));
Interlocked.Increment(ref localStats.IndexingSuccesses);
}
count += ProcessBatch(viewGenerator, currentDocumentResults, currentKey, localChanges, accessor, statsPerKey);
});
}
});
IDictionary<string, HashSet<string>> result;
while (allReferencedDocs.TryDequeue(out result))
{
foreach (var referencedDocument in result)
{
actions.Indexing.UpdateDocumentReferences(name, referencedDocument.Key, referencedDocument.Value);
actions.General.MaybePulseTransaction();
}
}
var changed = allState.SelectMany(x => x.Item1).Concat(deleted)
.Distinct()
.ToList();
var stats = new IndexingWorkStats(allState.Select(x => x.Item2));
var reduceKeyStats = allState.SelectMany(x => x.Item3)
.GroupBy(x => x.Key)
.Select(g => new {g.Key, Count = g.Sum(x => x.Value)})
.ToList();
BackgroundTaskExecuter.Instance.ExecuteAllBuffered(context, reduceKeyStats, enumerator => context.TransactionalStorage.Batch(accessor =>
{
while (enumerator.MoveNext())
{
var reduceKeyStat = enumerator.Current;
accessor.MapReduce.IncrementReduceKeyCounter(name, reduceKeyStat.Key, reduceKeyStat.Count);
}
}));
BackgroundTaskExecuter.Instance.ExecuteAllBuffered(context, changed, enumerator => context.TransactionalStorage.Batch(accessor =>
{
while (enumerator.MoveNext())
{
accessor.MapReduce.ScheduleReductions(name, 0, enumerator.Current);
}
}));
UpdateIndexingStats(context, stats);
AddindexingPerformanceStat(new IndexingPerformanceStats
{
OutputCount = count,
InputCount = sourceCount,
Operation = "Map",
Duration = sw.Elapsed,
Started = start
});
logIndexing.Debug("Mapped {0} documents for {1}", count, name);
}
private int ProcessBatch(AbstractViewGenerator viewGenerator, List<object> currentDocumentResults, string currentKey, HashSet<ReduceKeyAndBucket> changes,
IStorageActionsAccessor actions,
IDictionary<string, int> statsPerKey)
{
if (currentKey == null || currentDocumentResults.Count == 0)
return 0;
int count = 0;
var results = RobustEnumerationReduceDuringMapPhase(currentDocumentResults.GetEnumerator(), viewGenerator.ReduceDefinition);
foreach (var doc in results)
{
count++;
var reduceValue = viewGenerator.GroupByExtraction(doc);
if (reduceValue == null)
{
logIndexing.Debug("Field {0} is used as the reduce key and cannot be null, skipping document {1}",
viewGenerator.GroupByExtraction, currentKey);
continue;
}
string reduceKey = ReduceKeyToString(reduceValue);
var data = GetMappedData(doc);
actions.MapReduce.PutMappedResult(name, currentKey, reduceKey, data);
statsPerKey[reduceKey] = statsPerKey.GetOrDefault(reduceKey) + 1;
actions.General.MaybePulseTransaction();
changes.Add(new ReduceKeyAndBucket(IndexingUtil.MapBucket(currentKey), reduceKey));
}
return count;
}
private RavenJObject GetMappedData(object doc)
{
if (doc is IDynamicJsonObject)
return ((IDynamicJsonObject)doc).Inner;
var ravenJTokenWriter = new RavenJTokenWriter();
jsonSerializer.Serialize(ravenJTokenWriter, doc);
return (RavenJObject)ravenJTokenWriter.Token;
}
private static readonly ConcurrentDictionary<Type, Func<object, object>> documentIdFetcherCache =
new ConcurrentDictionary<Type, Func<object, object>>();
private static string GetDocumentId(object doc)
{
var docIdFetcher = documentIdFetcherCache.GetOrAdd(doc.GetType(), type =>
{
// document may be DynamicJsonObject if we are using compiled views
if (typeof(DynamicJsonObject) == type)
{
return i => ((dynamic)i).__document_id;
}
var docIdProp = TypeDescriptor.GetProperties(doc).Find(Constants.DocumentIdFieldName, false);
return docIdProp.GetValue;
});
if (docIdFetcher == null)
throw new InvalidOperationException("Could not create document id fetcher for this document");
var documentId = docIdFetcher(doc);
if (documentId == null || documentId is DynamicNullObject)
throw new InvalidOperationException("Could not getdocument id fetcher for this document");
return (string)documentId;
}
internal static string ReduceKeyToString(object reduceValue)
{
if (reduceValue is string || reduceValue is ValueType)
return reduceValue.ToString();
var dynamicJsonObject = reduceValue as IDynamicJsonObject;
if (dynamicJsonObject != null)
return dynamicJsonObject.Inner.ToString(Formatting.None);
return RavenJToken.FromObject(reduceValue).ToString(Formatting.None);
}
protected override IndexQueryResult RetrieveDocument(Document document, FieldsToFetch fieldsToFetch, ScoreDoc score)
{
if (fieldsToFetch.IsProjection == false)
fieldsToFetch = fieldsToFetch.CloneWith(document.GetFields().Select(x => x.Name).ToArray());
fieldsToFetch.EnsureHasField(Constants.ReduceKeyFieldName);
return base.RetrieveDocument(document, fieldsToFetch, score);
}
public override void Remove(string[] keys, WorkContext context)
{
context.TransactionalStorage.Batch(actions =>
{
var reduceKeyAndBuckets = new HashSet<ReduceKeyAndBucket>();
foreach (var key in keys)
{
actions.MapReduce.DeleteMappedResultsForDocumentId(key, name, reduceKeyAndBuckets);
}
actions.MapReduce.UpdateRemovedMapReduceStats(name, reduceKeyAndBuckets);
foreach (var reduceKeyAndBucket in reduceKeyAndBuckets)
{
actions.MapReduce.ScheduleReductions(name, 0, reduceKeyAndBucket);
}
});
Write((writer, analyzer, stats) =>
{
stats.Operation = IndexingWorkStats.Status.Ignore;
logIndexing.Debug(() => string.Format("Deleting ({0}) from {1}", string.Join(", ", keys), name));
writer.DeleteDocuments(keys.Select(k => new Term(Constants.ReduceKeyFieldName, k.ToLowerInvariant())).ToArray());
return keys.Length;
});
}
public class ReduceDocuments
{
private readonly MapReduceIndex parent;
private readonly string name;
readonly AnonymousObjectToLuceneDocumentConverter anonymousObjectToLuceneDocumentConverter;
private readonly Document luceneDoc = new Document();
private readonly Field reduceKeyField = new Field(Constants.ReduceKeyFieldName, "dummy",
Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
private PropertyDescriptorCollection properties = null;
private readonly List<AbstractIndexUpdateTriggerBatcher> batchers;
public ReduceDocuments(
MapReduceIndex parent,
AbstractViewGenerator viewGenerator,
IEnumerable<IGrouping<int, object>> mappedResultsByBucket,
int level,
WorkContext context,
IStorageActionsAccessor actions,
HashSet<string> reduceKeys)
{
this.parent = parent;
name = this.parent.name;
ViewGenerator = viewGenerator;
MappedResultsByBucket = mappedResultsByBucket;
Level = level;
Context = context;
Actions = actions;
ReduceKeys = reduceKeys;
anonymousObjectToLuceneDocumentConverter = new AnonymousObjectToLuceneDocumentConverter(this.parent.indexDefinition);
if (Level == 2)
{
batchers = Context.IndexUpdateTriggers.Select(x => x.CreateBatcher(name))
.Where(x => x != null)
.ToList();
}
}
public AbstractViewGenerator ViewGenerator { get; private set; }
public IEnumerable<IGrouping<int, object>> MappedResultsByBucket { get; private set; }
public int Level { get; private set; }
public WorkContext Context { get; private set; }
public IStorageActionsAccessor Actions { get; private set; }
public HashSet<string> ReduceKeys { get; private set; }
private string ExtractReduceKey(AbstractViewGenerator viewGenerator, object doc)
{
try
{
object reduceKey = viewGenerator.GroupByExtraction(doc);
if (reduceKey == null)
{
throw new InvalidOperationException("Could not find reduce key for " + name + " in the result: " + doc);
}
return ReduceKeyToString(reduceKey);
}
catch (Exception e)
{
throw new InvalidOperationException("Could not extract reduce key from reduce result!", e);
}
}
private IEnumerable<AbstractField> GetFields(object doc, out float boost)
{
boost = 1;
var boostedValue = doc as BoostedValue;
if (boostedValue != null)
{
doc = boostedValue.Value;
boost = boostedValue.Boost;
}
IEnumerable<AbstractField> fields;
if (doc is IDynamicJsonObject)
{
fields = anonymousObjectToLuceneDocumentConverter.Index(((IDynamicJsonObject)doc).Inner, Field.Store.YES);
}
else
{
properties = properties ?? TypeDescriptor.GetProperties(doc);
fields = anonymousObjectToLuceneDocumentConverter.Index(doc, properties, Field.Store.YES);
}
if (Math.Abs(boost - 1) > float.Epsilon)
{
var abstractFields = fields.ToList();
foreach (var abstractField in abstractFields)
{
abstractField.OmitNorms = false;
}
return abstractFields;
}
return fields;
}
private static RavenJObject ToJsonDocument(object doc)
{
var boostedValue = doc as BoostedValue;
if (boostedValue != null)
{
doc = boostedValue.Value;
}
var dynamicJsonObject = doc as IDynamicJsonObject;
if (dynamicJsonObject != null)
{
return dynamicJsonObject.Inner;
}
var ravenJObject = doc as RavenJObject;
if (ravenJObject != null)
return ravenJObject;
return RavenJObject.FromObject(doc);
}
public void ExecuteReduction()
{
var count = 0;
var sourceCount = 0;
var sw = Stopwatch.StartNew();
var start = SystemTime.UtcNow;
parent.Write((indexWriter, analyzer, stats) =>
{
stats.Operation = IndexingWorkStats.Status.Reduce;
try
{
if (Level == 2)
{
RemoveExistingReduceKeysFromIndex(indexWriter);
}
foreach (var mappedResults in MappedResultsByBucket)
{
var input = mappedResults.Select(x =>
{
sourceCount++;
return x;
});
foreach (var doc in parent.RobustEnumerationReduce(input.GetEnumerator(), ViewGenerator.ReduceDefinition, Actions, stats))
{
count++;
string reduceKeyAsString = ExtractReduceKey(ViewGenerator, doc);
switch (Level)
{
case 0:
case 1:
Actions.MapReduce.PutReducedResult(name, reduceKeyAsString, Level + 1, mappedResults.Key, mappedResults.Key / 1024, ToJsonDocument(doc));
Actions.General.MaybePulseTransaction();
break;
case 2:
WriteDocumentToIndex(doc, indexWriter, analyzer);
break;
default:
throw new InvalidOperationException("Unknown level: " + Level);
}
stats.ReduceSuccesses++;
}
}
}
catch (Exception e)
{
if (Level == 2)
{
batchers.ApplyAndIgnoreAllErrors(
ex =>
{
logIndexing.WarnException("Failed to notify index update trigger batcher about an error", ex);
Context.AddError(name, null, ex.Message);
},
x => x.AnErrorOccured(e));
}
throw;
}
finally
{
if (Level == 2)
{
batchers.ApplyAndIgnoreAllErrors(
e =>
{
logIndexing.WarnException("Failed to dispose on index update trigger", e);
Context.AddError(name, null, e.Message);
},
x => x.Dispose());
}
}
return count + ReduceKeys.Count;
});
parent.AddindexingPerformanceStat(new IndexingPerformanceStats
{
OutputCount = count,
InputCount = sourceCount,
Duration = sw.Elapsed,
Operation = "Reduce Level " + Level,
Started = start
});
logIndexing.Debug(() => string.Format("Reduce resulted in {0} entries for {1} for reduce keys: {2}", count, name, string.Join(", ", ReduceKeys)));
}
private void WriteDocumentToIndex(object doc, IndexWriter indexWriter, Analyzer analyzer)
{
float boost;
var fields = GetFields(doc, out boost).ToList();
string reduceKeyAsString = ExtractReduceKey(ViewGenerator, doc);
reduceKeyField.SetValue(reduceKeyAsString);
luceneDoc.GetFields().Clear();
luceneDoc.Boost = boost;
luceneDoc.Add(reduceKeyField);
foreach (var field in fields)
{
luceneDoc.Add(field);
}
if (Level == 2)
{
batchers.ApplyAndIgnoreAllErrors(
exception =>
{
logIndexing.WarnException(
string.Format("Error when executed OnIndexEntryCreated trigger for index '{0}', key: '{1}'",
name, reduceKeyAsString),
exception);
Context.AddError(name, reduceKeyAsString, exception.Message);
},
trigger => trigger.OnIndexEntryCreated(reduceKeyAsString, luceneDoc));
}
parent.LogIndexedDocument(reduceKeyAsString, luceneDoc);
parent.AddDocumentToIndex(indexWriter, luceneDoc, analyzer);
}
private void RemoveExistingReduceKeysFromIndex(IndexWriter indexWriter)
{
foreach (var reduceKey in ReduceKeys)
{
var entryKey = reduceKey;
indexWriter.DeleteDocuments(new Term(Constants.ReduceKeyFieldName, entryKey));
batchers.ApplyAndIgnoreAllErrors(
exception =>
{
logIndexing.WarnException(
string.Format("Error when executed OnIndexEntryDeleted trigger for index '{0}', key: '{1}'",
name, entryKey),
exception);
Context.AddError(name, entryKey, exception.Message);
},
trigger => trigger.OnIndexEntryDeleted(entryKey));
}
}
}
}
}