Skip to content

Commit

Permalink
Merge pull request #2890 from arekais/RavenDB-3921
Browse files Browse the repository at this point in the history
RavenDB-3921
  • Loading branch information
ayende committed Nov 19, 2015
2 parents 9a43b01 + 96879d3 commit 276a3af
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Raven.Database/Impl/DTC/InFlightTransactionalState.cs
Expand Up @@ -61,9 +61,9 @@ protected class ChangedDoc
public Etag committedEtag;
}

protected readonly ConcurrentDictionary<string, ChangedDoc> changedInTransaction = new ConcurrentDictionary<string, ChangedDoc>();
protected readonly ConcurrentDictionary<string, ChangedDoc> changedInTransaction = new ConcurrentDictionary<string, ChangedDoc>(StringComparer.OrdinalIgnoreCase);

protected readonly ConcurrentDictionary<string, TransactionState> transactionStates = new ConcurrentDictionary<string, TransactionState>();
protected readonly ConcurrentDictionary<string, TransactionState> transactionStates = new ConcurrentDictionary<string, TransactionState>(StringComparer.OrdinalIgnoreCase);

public object GetInFlightTransactionsInternalStateForDebugOnly()
{
Expand Down
25 changes: 16 additions & 9 deletions Raven.Database/Impl/DocumentRetriever.cs
Expand Up @@ -10,7 +10,6 @@
using Raven.Abstractions.Logging;
using Raven.Database.Config;
using Raven.Database.Impl.DTC;
using Raven.Imports.Newtonsoft.Json.Linq;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Abstractions.Linq;
Expand Down Expand Up @@ -245,28 +244,36 @@ private JsonDocument GetDocumentWithCaching(string key)
{
if (key == null)
return null;

// first we check the dtc state, then the cache and the storage, to avoid race conditions
var nonAuthoritativeInformationBehavior = inFlightTransactionalState.GetNonAuthoritativeInformationBehavior<JsonDocument>(null, key);

JsonDocument doc;
if (disableCache == false && cache.TryGetValue(key, out doc))
return doc;

doc = actions.Documents.DocumentByKey(key);
if (disableCache || cache.TryGetValue(key, out doc) == false)
{
doc = actions.Documents.DocumentByKey(key);
}

if (nonAuthoritativeInformationBehavior != null)
doc = nonAuthoritativeInformationBehavior(doc);

JsonDocument.EnsureIdInMetadata(doc);

if (doc != null && doc.Metadata != null)
doc.Metadata.EnsureCannotBeChangeAndEnableSnapshotting();

var nonAuthoritativeInformationBehavior = inFlightTransactionalState.GetNonAuthoritativeInformationBehavior<JsonDocument>(null, key);
if (nonAuthoritativeInformationBehavior != null)
doc = nonAuthoritativeInformationBehavior(doc);
if (disableCache == false)

if (disableCache == false && (doc == null || doc.NonAuthoritativeInformation != true))
cache[key] = doc;

if (cache.Count > 2048)
{
// we are probably doing a stream here, no point in trying to cache things, we might be
// going through the entire db here!
disableCache = true;
cache.Clear();
}

return doc;
}

Expand Down
22 changes: 13 additions & 9 deletions Raven.DtcTests/AsyncCommit.cs
Expand Up @@ -7,12 +7,9 @@
using System.Threading.Tasks;
using System.Transactions;
using Raven.Abstractions.Indexing;
using Raven.Client.Document;
using Raven.Database.Indexing;
using Raven.Tests.Common;

using Xunit;
using System.Linq;

namespace Raven.Tests.Bugs
{
Expand All @@ -31,21 +28,28 @@ public void DtcCommitWillGiveNewResultIfNonAuthoritativeIsSetToFalse()
s.SaveChanges();
}

var task = new Task(() =>
{
using (var s = documentStore.OpenSession())
{
s.Advanced.AllowNonAuthoritativeInformation = false;
var user = s.Load<AccurateCount.User>("users/1");
Assert.Equal("Rahien", user.Name);
}
});

using (var s = documentStore.OpenSession())
using (var scope = new TransactionScope())
{
var user = s.Load<AccurateCount.User>("users/1");
user.Name = "Rahien";
s.SaveChanges();
task.Start();
Assert.False(task.Wait(250, CancellationToken.None));
scope.Complete();
}

using (var s = documentStore.OpenSession())
{
s.Advanced.AllowNonAuthoritativeInformation = false;
var user = s.Load<AccurateCount.User>("users/1");
Assert.Equal("Rahien", user.Name);
}
task.Wait();
}
}

Expand Down
14 changes: 12 additions & 2 deletions Raven.Tests.Common/RavenTest.cs
Expand Up @@ -52,12 +52,20 @@ public override void Dispose()

private void ShowLogsIfNecessary()
{
if (!ShowLogs)
var testMemoryTarget = LogManager.GetTarget<TestMemoryTarget>();

if (testMemoryTarget == null)
return;

if (ShowLogs == false)
{
testMemoryTarget.ClearAll();
return;
}

foreach (var databaseName in DatabaseNames)
{
var target = LogManager.GetTarget<TestMemoryTarget>()[databaseName];
var target = testMemoryTarget[databaseName];
if (target == null)
continue;

Expand All @@ -75,6 +83,8 @@ private void ShowLogsIfNecessary()
WriteLine(writer);
}
}

testMemoryTarget.ClearAll();
}

private static void WriteLine(TextWriter writer, string message = "")
Expand Down

0 comments on commit 276a3af

Please sign in to comment.