Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: IntegrationEventBus supports isolation #604

Merged
merged 8 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<MoqPackageVersion>4.16.1</MoqPackageVersion>
<NSubstitutePackageVersion>4.4.0</NSubstitutePackageVersion>
<MapsterPackageVersion>7.3.0</MapsterPackageVersion>
<DaprPackageVersion>1.5.0</DaprPackageVersion>
<DaprPackageVersion>1.10.0</DaprPackageVersion>
<GoogleProtobufPackageVersion>3.19.1</GoogleProtobufPackageVersion>
<MedallionDistributedLockPackageVersion>1.0.4</MedallionDistributedLockPackageVersion>
<FluentValidationPackageVersion>11.1.0</FluentValidationPackageVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Configuration.Tests;

[TestClass]
public class ConfigurationUtilsTest
{
private IConfiguration _configuration;

[TestInitialize]
public void Initialize()
{
Environment.SetEnvironmentVariable("masa-test", "masa");
_configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
}

[DataRow("masa-test", "masa")]
[DataRow("masa-dev", "masa-dev")]
[DataTestMethod]
public void CompletionParameter(string appId, string expectedAppId)
{
var actualAppId = ConfigurationUtils.CompletionParameter(appId, _configuration, null);
Assert.AreEqual(expectedAppId, actualAppId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftPackageVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTeskSdkPackageVersion)" />
<PackageReference Include="MSTest.TestAdapter" Version="$(MSTestPackageVersion)" />
<PackageReference Include="MSTest.TestFramework" Version="$(MSTestPackageVersion)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Microsoft.Extensions.Configuration;
global using Microsoft.VisualStudio.TestTools.UnitTesting;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

[assembly: InternalsVisibleTo("Masa.BuildingBlocks.Configuration.Tests")]
[assembly: InternalsVisibleTo("Masa.Contrib.Configuration.Tests")]
[assembly: InternalsVisibleTo("Masa.Contrib.Dispatcher.IntegrationEvents.Dapr")]

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.Configuration;

internal static class ConfigurationUtils
{
/// <summary>
/// Complete the value of the parameter
/// the value of the specified parameter will be obtained sequentially from the environment variable and configuration information, if the acquisition fails, the initial value will be returned
/// </summary>
/// <param name="initialParameter">parameter initial value</param>
/// <param name="configuration"></param>
/// <param name="masaConfiguration"></param>
/// <returns></returns>
public static string CompletionParameter(
string initialParameter,
IConfiguration? configuration = null,
IMasaConfiguration? masaConfiguration = null)
{
var value = configuration == null
? Environment.GetEnvironmentVariable(initialParameter)
: configuration.GetSection(initialParameter).Value;
if (string.IsNullOrWhiteSpace(value))
value = masaConfiguration?.Local.GetSection(initialParameter).Value;

if (string.IsNullOrWhiteSpace(value)) return initialParameter;
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
global using Microsoft.Extensions.Logging;
global using System.Diagnostics.CodeAnalysis;
global using System.Reflection;
global using System.Runtime.CompilerServices;
global using System.Text.Json.Serialization;
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public static class DaprStarterConstant
/// Default number of retries
/// </summary>
public const int DEFAULT_RETRY_TIME = 10;

/// <summary>
/// When sidecar is started, the appid of the current dapr will be set to this environment variable
/// </summary>
public const string DEFAULT_DAPR_APPID = "DAPR_APPID";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.Dispatcher.IntegrationEvents;

/// <summary>
/// Persisted integration event extension information
/// </summary>
public class IntegrationEventExpand
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, string>? Isolation { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ Task<IEnumerable<IntegrationEventLog>> RetrieveEventLogsPendingToPublishAsync(
int batchSize,
CancellationToken cancellationToken = default);

Task SaveEventAsync(IIntegrationEvent @event, DbTransaction transaction, CancellationToken cancellationToken = default);
Task SaveEventAsync(
IIntegrationEvent @event,
IntegrationEventExpand? messageExpand,
DbTransaction transaction,
CancellationToken cancellationToken = default);

Task MarkEventAsPublishedAsync(Guid eventId, CancellationToken cancellationToken = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class IntegrationEventLog : IHasConcurrencyStamp

[NotMapped] public object Event => _event ??= JsonSerializer.Deserialize<object>(Content)!;

[NotMapped] public IntegrationEventExpand? EventExpand { get; private set; }

[NotMapped]
public string Topic { get; private set; } = null!;

Expand All @@ -31,6 +33,8 @@ public class IntegrationEventLog : IHasConcurrencyStamp

public string Content { get; private set; } = null!;

public string ExpandContent { get; private set; } = string.Empty;

public Guid TransactionId { get; private set; } = Guid.Empty;

public string RowVersion { get; private set; }
Expand All @@ -41,13 +45,22 @@ private IntegrationEventLog()
Initialize();
}

public IntegrationEventLog(IIntegrationEvent @event, Guid transactionId) : this()
public IntegrationEventLog(IIntegrationEvent @event, Guid transactionId) : this(@event, null, transactionId)
{
}

public IntegrationEventLog(IIntegrationEvent @event, IntegrationEventExpand? eventExpand, Guid transactionId) : this()
{
EventId = @event.GetEventId();
CreationTime = @event.GetCreationTime();
ModificationTime = @event.GetCreationTime();
EventTypeName = @event.GetType().FullName!;
Content = JsonSerializer.Serialize((object)@event);

if (eventExpand != null)
{
ExpandContent = JsonSerializer.Serialize(eventExpand);
}
TransactionId = transactionId;
}

Expand All @@ -66,6 +79,10 @@ public IntegrationEventLog DeserializeJsonContent()
{
Topic = EventTypeShortName;//Used to handle when the Topic is not persisted, it is consistent with the class name by default
}
if (!string.IsNullOrWhiteSpace(ExpandContent))
{
EventExpand = JsonSerializer.Deserialize<IntegrationEventExpand>(ExpandContent)!;
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
global using Masa.BuildingBlocks.Dispatcher.Events;
global using System.ComponentModel.DataAnnotations.Schema;
global using System.Data.Common;
global using System.Runtime.CompilerServices;
global using System.Text.Json;
global using System.Text.Json.Serialization;
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ namespace Masa.BuildingBlocks.Isolation;
public static class IsolationConstant
{
public const string DEFAULT_SECTION_NAME = "Isolation";

public const string DEFAULT_MULTI_ENVIRONMENT_NAME = "ASPNETCORE_ENVIRONMENT";

public const string DEFAULT_MULTI_TENANT_NAME = "__tenant";
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

[assembly: InternalsVisibleTo("Masa.Contrib.Dispatcher.IntegrationEvents")]
[assembly: InternalsVisibleTo("Masa.Contrib.Dispatcher.IntegrationEvents.Dapr")]
[assembly: InternalsVisibleTo("Masa.Contrib.Dispatcher.IntegrationEvents.Dapr.Tests")]
[assembly: InternalsVisibleTo("Masa.Contrib.Dispatcher.IntegrationEvents.Tests")]
[assembly: InternalsVisibleTo("Masa.Contrib.Isolation.MultiEnvironment")]
[assembly: InternalsVisibleTo("Masa.Contrib.Isolation.MultiEnvironment.Tests")]
[assembly: InternalsVisibleTo("Masa.Contrib.Isolation.MultiTenant")]
[assembly: InternalsVisibleTo("Masa.Contrib.Isolation.MultiTenant.Tests")]

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.Isolation;

public class IsolationOptions
{
public string? SectionName { get; set; }
internal string? SectionName { get; set; }

public Type MultiTenantIdType { get; set; }

public bool Enable => !string.IsNullOrWhiteSpace(SectionName);
public bool Enable => EnableMultiTenant || EnableMultiEnvironment;

private bool _enableMultiTenant;

private bool _enableMultiEnvironment;
internal bool EnableMultiTenant => _enableMultiTenant;
internal bool EnableMultiEnvironment => _enableMultiEnvironment;

private string _multiTenantIdName;

internal string MultiTenantIdName
{
get => _multiTenantIdName;
set
{
_enableMultiTenant = true;
_multiTenantIdName = value;
}
}

private string _multiEnvironmentName;

internal string MultiEnvironmentName
{
get => _multiEnvironmentName;
set
{
_enableMultiEnvironment = true;
_multiEnvironmentName = value;
}
}

public IsolationOptions()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Configuration.Tests;

[TestClass]
public class ConfigurationUtilsTest
{
private IConfiguration _configuration;

[TestInitialize]
public void Initialize()
{
Environment.SetEnvironmentVariable("masa-test", "masa");
_configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
}

[DataRow("masa-test", "masa")]
[DataRow("masa-dev", "masa")]
[DataRow("masa-pro", "masa-pro")]
[DataTestMethod]
public void CompletionParameter(string appId, string expectedAppId)
{
var services = new ServiceCollection();
services.AddMasaConfiguration(masaConfigurationBuilder =>
{
masaConfigurationBuilder.AddConfiguration(_configuration);
masaConfigurationBuilder.AddJsonFile("appsettings.json");
}, options => options.Assemblies = Array.Empty<Assembly>());
var actualAppId = ConfigurationUtils.CompletionParameter(appId, _configuration, services.GetMasaConfiguration());
Assert.AreEqual(expectedAppId, actualAppId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"http://localhost:9200"
]
},
"Env": "Test"
"Env": "Test",
"masa-dev": "masa"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class DaprEnvironmentProvider : IDaprEnvironmentProvider

private const string METRICS_PORT = "DAPR_METRICS_PORT";

public string? GetDaprAppId() => Environment.GetEnvironmentVariable(DaprStarterConstant.DEFAULT_DAPR_APPID);

public ushort? GetHttpPort() => GetEnvironmentVariable(HTTP_PORT);

public ushort? GetGrpcPort() => GetEnvironmentVariable(GRPC_PORT);
Expand Down Expand Up @@ -57,4 +59,9 @@ public bool TrySetMetricsPort(ushort? metricsPort)

return false;
}

public void SetDaprAppId(string appId)
{
Environment.SetEnvironmentVariable(DaprStarterConstant.DEFAULT_DAPR_APPID, appId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private void CompleteDaprEnvironment()
_retryTime = 0;
UpdateStatus(DaprProcessStatus.Started);
}
DaprEnvironmentProvider.SetDaprAppId(SuccessDaprOptions.AppId);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ protected DaprProcessBase(

internal SidecarOptions ConvertToSidecarOptions(DaprOptions options)
{
var daprAppIdByEnvironment = DaprEnvironmentProvider.GetDaprAppId();
var daprAppId = daprAppIdByEnvironment.IsNullOrWhiteSpace() ?
_daprProvider.CompletionAppId(options.AppId, options.DisableAppIdSuffix, options.AppIdSuffix, options.AppIdDelimiter) :
daprAppIdByEnvironment;
var sidecarOptions = new SidecarOptions(
_daprProvider.CompletionAppId(options.AppId, options.DisableAppIdSuffix, options.AppIdSuffix, options.AppIdDelimiter),
daprAppId,
options.AppPort,
options.AppProtocol,
options.EnableSsl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Masa.Contrib.Development.DaprStarter;

public interface IDaprEnvironmentProvider
{
string? GetDaprAppId();

ushort? GetHttpPort();

ushort? GetGrpcPort();
Expand Down Expand Up @@ -33,4 +35,6 @@ public interface IDaprEnvironmentProvider
/// When metricsPort is greater than 0, return true
/// </summary>
bool TrySetMetricsPort(ushort? metricsPort);

void SetDaprAppId(string appId);
}
Loading