Skip to content

Commit

Permalink
Making sure that we don't get race conditions in map/reduce indexes w…
Browse files Browse the repository at this point in the history
…hen caching is involved.
  • Loading branch information
ayende committed Feb 23, 2012
1 parent 4cfcaa3 commit 274060a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Raven.Database/DocumentDatabase.cs
Expand Up @@ -1331,13 +1331,14 @@ public Guid GetIndexEtag(string indexName, QueryResult queryResult)
lastReducedEtag = accessor.Staleness.GetMostRecentReducedEtag(indexName);
touchCount = accessor.Staleness.GetIndexTouchCount(indexName);
});
if (queryResult != null &&
queryResult.IndexEtag != lastDocEtag)
if (queryResult != null)
{
// the index changed between the time when we got it and the time
// we actually call this, we need to return something random so that
// the next time we won't get 304
return Guid.NewGuid();
if (lastReducedEtag != null && queryResult.IndexEtag != lastReducedEtag.Value ||
lastDocEtag != queryResult.IndexEtag)
return Guid.NewGuid();
}

var indexDefinition = GetIndexDefinition(indexName);
Expand Down
16 changes: 16 additions & 0 deletions Raven.Storage.Esent/StorageActions/Staleness.cs
Expand Up @@ -123,6 +123,22 @@ public bool IsMapStale(string name)
{
throw new IndexDoesNotExistsException("Could not find index named: " + name);
}

Api.JetSetCurrentIndex(session, IndexesStatsReduce, "by_key");
Api.MakeKey(session, IndexesStatsReduce, name, Encoding.Unicode, MakeKeyGrbit.NewKey);
if(Api.TrySeek(session, IndexesStatsReduce, SeekGrbit.SeekEQ))
{// for map-reduce indexes, we use the reduce stats

var lastReducedIndex = Api.RetrieveColumnAsDateTime(session, IndexesStatsReduce,
tableColumnsCache.IndexesStatsReduceColumns["last_reduced_timestamp"])
.Value;
var lastReducedEtag = Api.RetrieveColumn(session, IndexesStatsReduce,
tableColumnsCache.IndexesStatsReduceColumns["last_reduced_etag"]).TransfromToGuidWithProperSorting();
return Tuple.Create(lastReducedIndex, lastReducedEtag);

}


var lastIndexedTimestamp = Api.RetrieveColumnAsDateTime(session, IndexesStats,
tableColumnsCache.IndexesStatsColumns["last_indexed_timestamp"])
.Value;
Expand Down
9 changes: 9 additions & 0 deletions Raven.Storage.Managed/StalenessStorageActions.cs
Expand Up @@ -106,6 +106,15 @@ public bool IsMapStale(string name)
if (readResult == null)
throw new IndexDoesNotExistsException("Could not find index named: " + name);


if (readResult.Key.Value<object>("lastReducedTimestamp") != null)
{
return Tuple.Create(
readResult.Key.Value<DateTime>("lastReducedTimestamp"),
new Guid(readResult.Key.Value<byte[]>("lastReducedEtag"))
);
}

return Tuple.Create(
readResult.Key.Value<DateTime>("lastTimestamp"),
new Guid(readResult.Key.Value<byte[]>("lastEtag"))
Expand Down
1 change: 1 addition & 0 deletions Raven.StressTests/Raven.StressTests.csproj
Expand Up @@ -49,6 +49,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Races\RaceConditions.cs" />
<Compile Include="Storage\MultiThreadedStress\BatchOperationStress.cs" />
<Compile Include="Storage\MultiThreadedStress\PutOperationStress.cs" />
<Compile Include="StressTest.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Raven.Tryouts/Program.cs
Expand Up @@ -31,8 +31,8 @@ private static void Main()
Environment.SetEnvironmentVariable("Run", i.ToString());
Console.Clear();
Console.WriteLine(i);
using (var x = new MapReduce())
x.CanUpdateReduceValue_WhenChangingReduceKey();
var x = new Raven.Tests.MailingList.MapReduceIssue.CanPageThroughReduceResults();
x.Test();
}
}
catch (Exception e)
Expand Down

0 comments on commit 274060a

Please sign in to comment.