Skip to content

Commit

Permalink
Fixed MassTransit#1875 - the publish topology is now used for all sub…
Browse files Browse the repository at this point in the history
…scription names
  • Loading branch information
phatboyg committed Jun 4, 2020
1 parent f4b397d commit 8a5b210
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 135 deletions.
1 change: 1 addition & 0 deletions MassTransit.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<s:Int64 x:Key="/Default/CodeEditing/NullCheckPatterns/PatternTypeNamesToPriority/=JetBrains_002EReSharper_002EFeature_002EServices_002ECSharp_002ENullChecking_002EThrowExpressionNullCheckPattern/@EntryIndexedValue">3000</s:Int64>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=AutoPropertyCanBeMadeGetOnly_002ELocal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConsiderUsingConfigureAwait/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConvertClosureToMethodGroup/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/ValueAnalysisMode/@EntryValue">OFF</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Normal/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Normal"&gt;&lt;HtmlReformatCode&gt;True&lt;/HtmlReformatCode&gt;&lt;JsReformatCode&gt;True&lt;/JsReformatCode&gt;&lt;XMLReformatCode&gt;True&lt;/XMLReformatCode&gt;&lt;CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="True" AddMissingParentheses="True" ArrangeBraces="True" ArrangeAttributes="True" ArrangeArgumentsStyle="True" ArrangeCodeBodyStyle="True" ArrangeVarStyle="True" /&gt;&lt;CssReformatCode&gt;True&lt;/CssReformatCode&gt;&lt;VBReformatCode&gt;True&lt;/VBReformatCode&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSReorderTypeMembers&gt;True&lt;/CSReorderTypeMembers&gt;&lt;CSUpdateFileHeader&gt;True&lt;/CSUpdateFileHeader&gt;&lt;CSEnforceVarKeywordUsageSettings&gt;True&lt;/CSEnforceVarKeywordUsageSettings&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSArrangeQualifiers&gt;True&lt;/CSArrangeQualifiers&gt;&lt;CSFixBuiltinTypeReferences&gt;True&lt;/CSFixBuiltinTypeReferences&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;IDEA_SETTINGS&gt;&amp;lt;profile version="1.0"&amp;gt;
&amp;lt;option name="myName" value="Normal" /&amp;gt;
Expand Down
4 changes: 4 additions & 0 deletions src/MassTransit/Topology/IHostTopology.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace MassTransit.Topology
{
using System;


public interface IHostTopology :
IBusTopology
{
Uri HostAddress { get; }
}
}
5 changes: 2 additions & 3 deletions src/MassTransit/Topology/Topologies/HostTopology.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
{
using System;
using Configuration;
using Util;


public abstract class HostTopology :
IHostTopology
{
protected static readonly INewIdFormatter Formatter = FormatUtil.Formatter;

readonly IHostConfiguration _hostConfiguration;
readonly ITopologyConfiguration _topologyConfiguration;

Expand All @@ -22,6 +19,8 @@ protected HostTopology(IHostConfiguration hostConfiguration, ITopologyConfigurat
public IPublishTopology PublishTopology => _topologyConfiguration.Publish;
public ISendTopology SendTopology => _topologyConfiguration.Send;

public Uri HostAddress => _hostConfiguration.HostAddress;

public IMessagePublishTopology<T> Publish<T>()
where T : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public Uri GetDestinationAddress(Type messageType, Action<ITopicConfigurator> co

_topologyConfiguration.Publish.TryGetPublishAddress(messageType, _hostConfiguration.HostAddress, out var address);

// var address = new ActiveMqEndpointAddress(_hostConfiguration.HostAddress, new Uri($"topic:{queueName}?temporary={isTemporary}"));

var settings = new TopicSendSettings(new ActiveMqEndpointAddress(_hostConfiguration.HostAddress, address));
if (isTemporary)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public interface IAmazonSqsHostTopology :
new IAmazonSqsSendTopology SendTopology { get; }

/// <summary>
/// Returns the destination address for the specified exchange
/// Returns the destination address for the specified topic
/// </summary>
/// <param name="topicName"></param>
/// <param name="configure">Callback to configure exchange settings</param>
/// <returns></returns>
Uri GetDestinationAddress(string topicName, Action<ITopicConfigurator> configure = null);

/// <summary>
/// Returns the destination address for the specified message type
/// Returns the destination address for the topic identified by the message type
/// </summary>
/// <param name="messageType">The message type</param>
/// <param name="configure">Callback to configure exchange settings</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
{
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Configuration;
using Contexts;
using GreenPipes;
using MassTransit.Builders;
using Topology;
using Topology.Builders;
using Transport;
using Util;


public class ServiceBusReceiveEndpointBuilder :
Expand Down Expand Up @@ -56,28 +53,9 @@ public ServiceBusReceiveEndpointContext CreateReceiveEndpointContext()
string GenerateSubscriptionName()
{
var subscriptionName = _configuration.Settings.Name.Split(Separator, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
var hostScope = _configuration.HostAddress.AbsolutePath.Split(Separator, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

var suffix = _configuration.HostAddress.AbsolutePath.Split(Separator, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
if (!string.IsNullOrWhiteSpace(suffix))
subscriptionName += $"-{suffix}";

string name;
if (subscriptionName.Length > 50)
{
string hashed;
using (var hasher = new SHA1Managed())
{
byte[] buffer = Encoding.UTF8.GetBytes(subscriptionName);
byte[] hash = hasher.ComputeHash(buffer);
hashed = FormatUtil.Formatter.Format(hash).Substring(0, 6);
}

name = $"{subscriptionName.Substring(0, 43)}-{hashed}";
}
else
name = subscriptionName;

return name;
return _configuration.Topology.Publish.GenerateSubscriptionName(subscriptionName, hostScope);
}

BrokerTopology BuildTopology(ReceiveSettings settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ServiceBusHostConfiguration(IServiceBusBusConfiguration busConfiguration,
_topologyConfiguration = topologyConfiguration;

_hostSettings = new HostSettings();
_hostTopology = new ServiceBusHostTopology(this, _topologyConfiguration, _messageNameFormatter);
_hostTopology = new ServiceBusHostTopology(this, _topologyConfiguration);

_connectionContextSupervisor = new ServiceBusConnectionContextSupervisor(this, topologyConfiguration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace MassTransit.Azure.ServiceBus.Core.Topology.Builders
using Microsoft.Azure.ServiceBus.Management;


public abstract class BrokerTopologyBuilder :
public class BrokerTopologyBuilder :
IBrokerTopologyBuilder
{
long _nextId;

protected BrokerTopologyBuilder()
public BrokerTopologyBuilder()
{
Topics = new NamedEntityCollection<TopicEntity, TopicHandle>(TopicEntity.EntityComparer, TopicEntity.NameComparer);
Queues = new NamedEntityCollection<QueueEntity, QueueHandle>(QueueEntity.EntityComparer, QueueEntity.NameComparer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ namespace MassTransit.Azure.ServiceBus.Core.Topology.Builders
{
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Entities;
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.Management;
using Util;


public class PublishEndpointBrokerTopologyBuilder :
Expand All @@ -22,10 +19,12 @@ public enum Options
}


readonly IServiceBusPublishTopology _topology;
readonly Options _options;

public PublishEndpointBrokerTopologyBuilder(Options options = Options.MaintainHierarchy)
public PublishEndpointBrokerTopologyBuilder(IServiceBusPublishTopology topology, Options options = Options.MaintainHierarchy)
{
_topology = topology;
_options = options;
}

Expand All @@ -37,7 +36,7 @@ public PublishEndpointBrokerTopologyBuilder(Options options = Options.MaintainHi
public IPublishEndpointBrokerTopologyBuilder CreateImplementedBuilder()
{
if (_options.HasFlag(Options.MaintainHierarchy))
return new ImplementedBuilder(this, _options);
return new ImplementedBuilder(this, _topology, _options);

return this;
}
Expand All @@ -47,12 +46,14 @@ class ImplementedBuilder :
IPublishEndpointBrokerTopologyBuilder
{
readonly IPublishEndpointBrokerTopologyBuilder _builder;
readonly IServiceBusPublishTopology _topology;
readonly Options _options;
TopicHandle _topic;

public ImplementedBuilder(IPublishEndpointBrokerTopologyBuilder builder, Options options)
public ImplementedBuilder(IPublishEndpointBrokerTopologyBuilder builder, IServiceBusPublishTopology topology, Options options)
{
_builder = builder;
_topology = topology;
_options = options;
}

Expand All @@ -66,7 +67,7 @@ public TopicHandle Topic
{
var subscriptionName = string.Join("-", value.Topic.TopicDescription.Path.Split('/').Reverse());
var subscriptionDescription = new SubscriptionDescription(_builder.Topic.Topic.TopicDescription.Path,
GenerateSubscriptionName(subscriptionName)) {ForwardTo = value.Topic.TopicDescription.Path};
_topology.FormatSubscriptionName(subscriptionName)) {ForwardTo = value.Topic.TopicDescription.Path};

_builder.CreateTopicSubscription(_builder.Topic, _topic, subscriptionDescription);
}
Expand All @@ -75,10 +76,7 @@ public TopicHandle Topic

public IPublishEndpointBrokerTopologyBuilder CreateImplementedBuilder()
{
if (_options.HasFlag(Options.MaintainHierarchy))
return new ImplementedBuilder(this, _options);

return this;
return _options.HasFlag(Options.MaintainHierarchy) ? new ImplementedBuilder(this, _topology, _options) : this;
}

public TopicHandle CreateTopic(TopicDescription topicDescription)
Expand Down Expand Up @@ -108,27 +106,6 @@ public QueueSubscriptionHandle CreateQueueSubscription(TopicHandle exchange, Que
{
return _builder.CreateQueueSubscription(exchange, queue, subscriptionDescription, rule, filter);
}

string GenerateSubscriptionName(string subscriptionName)
{
string name;
if (subscriptionName.Length > 50)
{
string hashed;
using (var hasher = new SHA1Managed())
{
byte[] buffer = Encoding.UTF8.GetBytes(subscriptionName);
byte[] hash = hasher.ComputeHash(buffer);
hashed = FormatUtil.Formatter.Format(hash).Substring(0, 6);
}

name = $"{subscriptionName.Substring(0, 43)}-{hashed}";
}
else
name = subscriptionName;

return name;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
namespace MassTransit.Azure.ServiceBus.Core.Topology.Specifications
{
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Builders;
using GreenPipes;
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.Management;
using Util;


/// <summary>
Expand Down Expand Up @@ -43,33 +39,8 @@ public void Apply(IReceiveEndpointBrokerTopologyBuilder builder)
var subscriptionDescription = _subscriptionDescription;

subscriptionDescription.ForwardTo = builder.Queue.Queue.QueueDescription.Path;
subscriptionDescription.SubscriptionName =
GetSubscriptionName(subscriptionDescription.SubscriptionName, builder.Queue.Queue.QueueDescription.Path.Split('/').Last());

builder.CreateQueueSubscription(topic, builder.Queue, subscriptionDescription, _rule, _filter);
}

static string GetSubscriptionName(string subscriptionName, string queuePath)
{
var subscriptionPath = subscriptionName.Replace("{queuePath}", queuePath);

string name;
if (subscriptionPath.Length > 50)
{
string hashed;
using (var hasher = new SHA1Managed())
{
byte[] buffer = Encoding.UTF8.GetBytes(subscriptionPath);
byte[] hash = hasher.ComputeHash(buffer);
hashed = FormatUtil.Formatter.Format(hash).Substring(0, 6);
}

name = $"{subscriptionPath.Substring(0, 43)}-{hashed}";
}
else
name = subscriptionPath;

return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,11 @@ public interface IServiceBusHostTopology :
where T : class;

/// <summary>
/// Returns the destination address for the specified exchange
/// Returns the destination address for the specified queue
/// </summary>
/// <param name="queueName"></param>
/// <param name="configure">Callback to configure queue settings</param>
/// <returns></returns>
Uri GetDestinationAddress(string queueName, Action<IQueueConfigurator> configure = null);

/// <summary>
/// Returns the destination address for the specified message type
/// </summary>
/// <param name="messageType">The message type</param>
/// <param name="configure">Callback to configure exchange settings</param>
/// <returns></returns>
Uri GetDestinationAddress(Type messageType, Action<IQueueConfigurator> configure = null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace MassTransit.Azure.ServiceBus.Core.Topology
{
using Builders;
using Configurators;
using MassTransit.Topology;
using Microsoft.Azure.ServiceBus.Management;
using Transport;
Expand All @@ -17,6 +18,8 @@ public interface IServiceBusMessagePublishTopology<TMessage> :
TopicDescription TopicDescription { get; }

SendSettings GetSendSettings();

SubscriptionConfigurator GetSubscriptionConfigurator(string subscriptionName);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,20 @@ public interface IServiceBusPublishTopology :
{
new IServiceBusMessagePublishTopology<T> GetMessageTopology<T>()
where T : class;

/// <summary>
/// Formats a subscription name to be 50 characters if it is greater than 50 characters.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
string FormatSubscriptionName(string name);

/// <summary>
/// Generate a subscription name that is less than 50 characters, using the entity name and host address
/// </summary>
/// <param name="entityName">The entity name of the destination queue or topic</param>
/// <param name="hostScope">The absolute path of the host, which is usually the scope</param>
/// <returns></returns>
string GenerateSubscriptionName(string entityName, string hostScope = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public void Subscribe(string topicName, string subscriptionName, Action<ISubscri
if (string.IsNullOrWhiteSpace(subscriptionName))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(subscriptionName));

subscriptionName = _publishTopology.FormatSubscriptionName(subscriptionName);

var topicDescription = Defaults.CreateTopicDescription(topicName);

var subscriptionConfigurator = new SubscriptionConfigurator(topicDescription.Path, subscriptionName);
Expand Down
Loading

0 comments on commit 8a5b210

Please sign in to comment.