Skip to content

Commit ca8d99d

Browse files
Relax SessionId validation (Azure#27924)
* Relax SessionId validation * save * PR fb
1 parent a73408c commit ca8d99d

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/ServiceBusEventSource.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ protected ServiceBusEventSource() : base(EventSourceName)
189189
internal const int ProcessorStoppingReceiveCanceledEvent = 110;
190190
internal const int ProcessorStoppingAcceptSessionCanceledEvent = 111;
191191

192+
internal const int PartitionKeyValueOverwritten = 112;
193+
192194
#endregion
193195
// add new event numbers here incrementing from previous
194196

@@ -1570,6 +1572,15 @@ public virtual void MaxMessagesExceedsPrefetch(string identifier, int prefetchCo
15701572
WriteEvent(MaxMessagesExceedsPrefetchEvent, identifier, prefetchCount, maxMessages);
15711573
}
15721574
}
1575+
1576+
[Event(PartitionKeyValueOverwritten, Level = EventLevel.Warning, Message = "The PartitionKey property with value '{0}' was overwritten with value '{1}' due to setting the SessionId on message with MessageId '{2}'")]
1577+
public virtual void PartitionKeyOverwritten(string partitionKey, string sessionId, string messageId)
1578+
{
1579+
if (IsEnabled())
1580+
{
1581+
WriteEvent(PartitionKeyValueOverwritten,partitionKey, sessionId, messageId);
1582+
}
1583+
}
15731584
#endregion
15741585
}
15751586
}

sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusMessage.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Azure.Core;
1010
using Azure.Core.Amqp;
1111
using Azure.Messaging.ServiceBus.Amqp;
12+
using Azure.Messaging.ServiceBus.Diagnostics;
1213

1314
namespace Azure.Messaging.ServiceBus
1415
{
@@ -243,11 +244,14 @@ public string SessionId
243244
set
244245
{
245246
Argument.AssertNotTooLong(value, Constants.MaxSessionIdLength, nameof(value));
247+
AmqpMessage.Properties.GroupId = value;
248+
249+
// If the PartitionKey was already set to a different value, override it with the SessionId, as the SessionId takes precedence.
246250
if (PartitionKey != null && PartitionKey != value)
247251
{
248-
throw new ArgumentOutOfRangeException(nameof(value), $"SessionId:{value} cannot be set to a different value than PartitionKey:{PartitionKey}.");
252+
ServiceBusEventSource.Log.PartitionKeyOverwritten(PartitionKey, value, MessageId);
253+
PartitionKey = value;
249254
}
250-
AmqpMessage.Properties.GroupId = value;
251255
}
252256
}
253257

sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,5 +437,21 @@ public async Task StoppingSessionProcessorDoesNotLogTaskCanceledExceptions()
437437
Assert.True(_listener.EventsById(ServiceBusEventSource.ProcessorStoppingReceiveCanceledEvent).Any());
438438
}
439439
}
440+
441+
[Test]
442+
public void LogsMessageEvents()
443+
{
444+
var message = new ServiceBusMessage()
445+
{
446+
SessionId = "sessionId1",
447+
PartitionKey = "sessionId1",
448+
MessageId = "messageId"
449+
};
450+
message.SessionId = "sessionId2";
451+
452+
_listener.SingleEventById(
453+
ServiceBusEventSource.PartitionKeyValueOverwritten,
454+
e => e.Payload.Contains("sessionId1") && e.Payload.Contains("sessionId2") && e.Payload.Contains("messageId"));
455+
}
440456
}
441457
}

sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageTests.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ public void PartitionKeyMustMatchSessionIdIfBothSet()
111111
{
112112
PartitionKey = "partition"
113113
};
114-
Assert.That(
115-
() => message.SessionId = "session",
116-
Throws.InstanceOf<ArgumentOutOfRangeException>());
114+
message.SessionId = "session";
115+
Assert.AreEqual("session", message.PartitionKey);
116+
Assert.AreEqual(message.SessionId, message.PartitionKey);
117117

118118
message = new ServiceBusMessage
119119
{
@@ -127,33 +127,44 @@ public void PartitionKeyMustMatchSessionIdIfBothSet()
127127
{
128128
PartitionKey = "partition"
129129
};
130-
Assert.That(
131-
() => message.SessionId = null,
132-
Throws.InstanceOf<ArgumentOutOfRangeException>());
130+
message.SessionId = null;
131+
Assert.IsNull(message.PartitionKey);
133132

134133
message = new ServiceBusMessage
135134
{
136135
PartitionKey = null,
137136
SessionId = "session"
138137
};
138+
Assert.IsNull(message.PartitionKey);
139139

140140
message = new ServiceBusMessage
141141
{
142142
SessionId = null,
143143
PartitionKey = "partition"
144144
};
145+
Assert.IsNull(message.SessionId);
145146

146147
message = new ServiceBusMessage
147148
{
148149
SessionId = "partition",
149150
PartitionKey = "partition"
150151
};
152+
Assert.AreEqual(message.SessionId, message.PartitionKey);
153+
154+
message = new ServiceBusMessage
155+
{
156+
PartitionKey = "partition",
157+
SessionId = "partition"
158+
};
159+
Assert.AreEqual(message.SessionId, message.PartitionKey);
151160

152161
message = new ServiceBusMessage
153162
{
154163
PartitionKey = "partition",
155164
SessionId = "partition"
156165
};
166+
message.SessionId = "session";
167+
Assert.AreEqual(message.SessionId, message.PartitionKey);
157168
}
158169

159170
[Test]

0 commit comments

Comments
 (0)