Skip to content

Commit

Permalink
RavenDB-14041 : AttachmentStorage.Put - do not throw a concurrency ex…
Browse files Browse the repository at this point in the history
…ception on partial update with a valid expected change vector
  • Loading branch information
aviv86 committed Dec 17, 2023
1 parent 0d3ca12 commit fc47909
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Raven.Server/Documents/AttachmentsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,14 @@ void SetTableValue(TableValueBuilder tvb, Slice cv)
else
{
var putStream = true;
var attachmentExists = false;

// We already asserted that the document is not in conflict, so we might have just one partial key, not more.
using (GetAttachmentPartialKey(context, keySlice, base64Hash.Size, lowerContentType.Size, out Slice partialKeySlice))
{
if (table.SeekOnePrimaryKeyPrefix(partialKeySlice, out TableValueReader partialTvr))
{
attachmentExists = true;
if (expectedChangeVector != null)
{
var oldChangeVector = TableValueToChangeVector(context, (int)AttachmentsTable.ChangeVector, ref partialTvr);
Expand Down Expand Up @@ -282,7 +284,7 @@ void SetTableValue(TableValueBuilder tvb, Slice cv)
}
}

if (string.IsNullOrEmpty(expectedChangeVector) == false)
if (attachmentExists == false && string.IsNullOrEmpty(expectedChangeVector) == false)
{
ThrowConcurrentExceptionOnMissingAttachment(documentId, name, expectedChangeVector);
}
Expand Down
51 changes: 51 additions & 0 deletions test/SlowTests/Issues/RavenDB_14041.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.IO;
using Raven.Client.Documents.Commands.Batches;
using SlowTests.Core.Utils.Entities;
using Tests.Infrastructure;
using Xunit;
using Xunit.Abstractions;

namespace SlowTests.Issues
{
public class RavenDB_14041 : ClusterTestBase
{
public RavenDB_14041(ITestOutputHelper output) : base(output)
{
}

[Fact]
public void CanUpdateAttachmentUsingPutAttachmentCommandDataWithExpectedChangeVector()
{
using (var store = GetDocumentStore())
{
const string documentId = "cats/1-A";
const string name = "bruhhh";
const string contentType = "stuff";

using (var session = store.OpenSession())
{
session.Store(new User(), documentId);

using var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
session.Advanced.Attachments.Store(documentId, name, stream, contentType);

session.SaveChanges();
}

// update attachment
using (var session = store.OpenSession())
{
var attachment = session.Advanced.Attachments.Get(documentId, name);
using var stream = new MemoryStream(new byte[] { 6, 7, 8, 9, 10 });

// should not throw a concurrency exception
var cmd = new PutAttachmentCommandData(documentId, name, stream, contentType, changeVector: attachment.Details.ChangeVector);
session.Advanced.Defer(cmd);
session.SaveChanges();
}

}
}
}

}

0 comments on commit fc47909

Please sign in to comment.