Skip to content

Commit

Permalink
Fix/isolation (#633)
Browse files Browse the repository at this point in the history
* fix:isolation

* fix:oss isolation and event dispatcher order

* fix:code smell
  • Loading branch information
MayueCif authored Jun 28, 2023
1 parent 3de695b commit 2d67553
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// 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.Caching.Distributed.StackExchangeRedis")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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.StackSdks.Isolation;

public static class DccConsts
{
public const string PUBLIC_ID = "public-$Config";
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// 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.Storage.ObjectStorage;
Expand All @@ -7,12 +7,12 @@ public class DefaultBucketNameFactory : MasaFactoryBase<IBucketNameProvider, Mas
{
protected override string DefaultServiceNotFoundMessage => "No default ObjectStorageBucketName found";
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] ObjectStorageBucketName, it was not found";
protected override MasaFactoryOptions<MasaRelationOptions<IBucketNameProvider>> FactoryOptions => _options.CurrentValue;
protected override MasaFactoryOptions<MasaRelationOptions<IBucketNameProvider>> FactoryOptions => _options.Value;

private readonly IOptionsMonitor<BucketNameFactoryOptions> _options;
private readonly IOptions<BucketNameFactoryOptions> _options;

public DefaultBucketNameFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_options = serviceProvider.GetRequiredService<IOptionsMonitor<BucketNameFactoryOptions>>();
_options = serviceProvider.GetRequiredService<IOptions<BucketNameFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// 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.Storage.ObjectStorage;
Expand All @@ -8,12 +8,12 @@ public class DefaultObjectStorageClientFactory : MasaFactoryBase<IManualObjectSt
protected override string DefaultServiceNotFoundMessage => "No default ObjectStorage found";
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] ObjectStorage, it was not found";

protected override MasaFactoryOptions<MasaRelationOptions<IManualObjectStorageClient>> FactoryOptions => _options.CurrentValue;
protected override MasaFactoryOptions<MasaRelationOptions<IManualObjectStorageClient>> FactoryOptions => _options.Value;

private readonly IOptionsMonitor<ObjectStorageFactoryOptions> _options;
private readonly IOptions<ObjectStorageFactoryOptions> _options;

public DefaultObjectStorageClientFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_options = serviceProvider.GetRequiredService<IOptionsMonitor<ObjectStorageFactoryOptions>>();
_options = serviceProvider.GetRequiredService<IOptions<ObjectStorageFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// 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
Expand Down Expand Up @@ -28,15 +28,15 @@ private static void AddObjectStorageCore(this IServiceCollection services, strin
MasaArgumentException.ThrowIfNull(services);
MasaArgumentException.ThrowIfNull(name);

services.TryAddSingleton<SingletonService<IManualObjectStorageClient>>(serviceProvider
services.TryAddSingleton(serviceProvider
=> new SingletonService<IManualObjectStorageClient>(serviceProvider.GetRequiredService<IObjectStorageClientFactory>()
.Create()));
services.TryAddScoped<ScopedService<IManualObjectStorageClient>>(serviceProvider
services.TryAddScoped(serviceProvider
=> new ScopedService<IManualObjectStorageClient>(serviceProvider.GetRequiredService<IObjectStorageClientFactory>().Create()));

services.TryAddSingleton<SingletonService<IBucketNameProvider>>(serviceProvider
services.TryAddSingleton(serviceProvider
=> new SingletonService<IBucketNameProvider>(serviceProvider.GetRequiredService<IBucketNameFactory>().Create()));
services.TryAddScoped<ScopedService<IBucketNameProvider>>(serviceProvider
services.TryAddScoped(serviceProvider
=> new ScopedService<IBucketNameProvider>(serviceProvider.GetRequiredService<IBucketNameFactory>().Create()));

services.TryAddObjectStorageClient();
Expand All @@ -61,13 +61,10 @@ private static void TryAddObjectStorageClient(this IServiceCollection services)

private static void TryAddBucketNameProvider(this IServiceCollection services)
{
services.TryAddTransient<IBucketNameProvider>(serviceProvider =>
{
if (serviceProvider.EnableIsolation())
return serviceProvider.GetRequiredService<ScopedService<IBucketNameProvider>>().Service;
services.TryAddTransient(serviceProvider => serviceProvider.EnableIsolation() ?
serviceProvider.GetRequiredService<ScopedService<IBucketNameProvider>>().Service :
serviceProvider.GetRequiredService<SingletonService<IBucketNameProvider>>().Service);

return serviceProvider.GetRequiredService<SingletonService<IBucketNameProvider>>().Service;
});
services.TryAddTransient<IBucketNameFactory, DefaultBucketNameFactory>();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// 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
Expand All @@ -20,7 +20,7 @@ public DispatchNetworkRoot(List<IDispatchNetworkProvider> providers)
private static Dictionary<Type, List<DispatchRelationOptions>> BuildDispatchNetworks(List<IDispatchNetworkProvider> providers)
{
return BuildDispatchNetworks(
providers.SelectMany(provider => provider.HandlerNetwork).ToList(),
providers.SelectMany(provider => provider.HandlerNetwork).OrderBy(provider => provider.Order).ToList(),
providers.SelectMany(provider => provider.CancelHandlerNetwork).ToList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// 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.Dispatcher.Events.Tests.EventHandlers;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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.ReadWriteSplitting.Cqrs.Tests.Commands;

internal record OrderCommand(bool Cancel) : Command
{
public int Count { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ public void TestCommand(string name)
}
}

[DataTestMethod]
[DataRow(false)]
[DataRow(true)]
public void TestOrderCommand(bool cancel)
{
var command = new OrderCommand(cancel);
_eventBus.PublishAsync(command);
if (cancel)
{
Assert.IsTrue(command.Count == 0);
}
else
{
Assert.IsTrue(command.Count == int.MaxValue);
}
}

[TestMethod]
public void TestQuery()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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.ReadWriteSplitting.Cqrs.Tests;

#pragma warning disable CA1822
internal class OrderCommandHandler
{
[EventHandler]
public void Handler(OrderCommand command)
{
command.Count = int.MaxValue;
if (command.Cancel)
{
throw new Exception("cancel");
}
}

[EventHandler(1)]
public void Handler1(OrderCommand command)
{
command.Count = 1;
}

[EventHandler(2)]
public void Handler2(OrderCommand command)
{
command.Count = 2;
}

[EventHandler(1, IsCancel = true)]
public void HandlerCancel1(OrderCommand command)
{
command.Count = 0;
}

[EventHandler(2, IsCancel = true)]
public void HandlerCancel2(OrderCommand command)
{
command.Count = 1;
}
}
#pragma warning restore CA1822
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static DccConfigurationOptions ComplementDccConfigurationOption(
var cacheClient = serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create("masa.contrib.configuration.configurationapi.dcc");
var masaAppConfigureOptions = serviceProvider.GetRequiredService<IOptions<MasaAppConfigureOptions>>().Value;

dccConfigurationOptions.PublicId = masaAppConfigureOptions.GetValue(nameof(DccOptions.PublicId), () => "public-$Config");
dccConfigurationOptions.PublicId = masaAppConfigureOptions.GetValue(nameof(DccOptions.PublicId), () => DccConsts.PUBLIC_ID);
dccConfigurationOptions.PublicSecret = masaAppConfigureOptions.GetValue(nameof(DccOptions.PublicSecret));

dccConfigurationOptions.DefaultSection.ComplementAndCheckAppId(masaAppConfigureOptions.AppId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static async Task<IServiceCollection> AddStackIsolationAsync(this IServic

services.AddSingleton((sp) => { return new EnvironmentProvider(environments); });

services.AddDccIsolation(builder => builder.UseDccIsolation());

if (!name.IsNullOrEmpty())
{
services.ConfigureConnectionStrings(name);
Expand All @@ -26,8 +28,6 @@ public static async Task<IServiceCollection> AddStackIsolationAsync(this IServic

services.AddScoped<EnvironmentMiddleware>();

services.AddDccIsolation(builder => builder.UseDccIsolation());

return services;
}

Expand All @@ -51,7 +51,11 @@ static void ConfigureConnectionStrings(this IServiceCollection services, string

services.Configure<ConnectionStrings>(options =>
{
options.Add(ConnectionStrings.DEFAULT_CONNECTION_STRING_NAME, masaStackConfig.GetConnectionString(name));
//default value map appsettings.json -> ConnectionStrings -> DefaultConnection
if (!options.Any())
{
options.Add(ConnectionStrings.DEFAULT_CONNECTION_STRING_NAME, masaStackConfig.GetConnectionString(name));
}
});
}

Expand Down Expand Up @@ -127,22 +131,16 @@ static void ConfigureMultilevelCacheOptions(this IServiceCollection services)

static void ConfigStorageOptions(this IServiceCollection services)
{
var (environments, _, masaStackConfig) = services.GetInternal();
var configurationApiClient = services.BuildServiceProvider().GetRequiredService<IConfigurationApiClient>();
services.Configure<IsolationOptions<AliyunStorageConfigureOptions>>(async options =>
var (environments, _, _) = services.GetInternal();
var configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
services.Configure<IsolationOptions<AliyunStorageConfigureOptions>>(options =>
{
foreach (var environment in environments)
{
try
{
var ossOptions = await configurationApiClient.GetAsync<OssOptions>(environment, "Default", "public-$Config", "$public.OSS", ossOptions =>
{
var item = options.Data.Find(s => s.Environment == environment);
if (item != null)
{
item.Data = Convert(ossOptions);
}
});
var ossOptions = configuration.GetSection(SectionTypes.ConfigurationApi.ToString()).GetSection(environment)
.GetSection(DccConsts.PUBLIC_ID).GetSection("$public.OSS").Get<OssOptions>();
if (ossOptions == null)
{
continue;
Expand All @@ -155,17 +153,28 @@ static void ConfigStorageOptions(this IServiceCollection services)
}
catch (Exception ex)
{
await Console.Out.WriteLineAsync(ex.Message);
Console.Out.WriteLine(ex.Message);
}
}
});

services.Configure<AliyunStorageConfigureOptions>(async options =>
services.Configure<AliyunStorageConfigureOptions>(options =>
{
var ossOptions = await configurationApiClient.GetAsync<OssOptions>(masaStackConfig.Environment, "Default", "public-$Config", "$public.OSS");
#pragma warning disable S1854
options = Convert(ossOptions);
#pragma warning disable S1854
var ossOptions = configuration.GetSection(SectionTypes.ConfigurationApi.ToString())
.GetSection(DccConsts.PUBLIC_ID).GetSection("$public.OSS").Get<OssOptions>();
options.AccessKeyId = ossOptions.AccessId;
options.AccessKeySecret = ossOptions.AccessSecret;
options.Sts = new AliyunStsOptions
{
RegionId = ossOptions.RegionId
};
options.Storage = new AliyunStorageOptions
{
BucketNames = new BucketNames(new Dictionary<string, string> { { BucketNames.DEFAULT_BUCKET_NAME, ossOptions.Bucket } }),
Endpoint = ossOptions.Endpoint,
RoleArn = ossOptions.RoleArn,
RoleSessionName = ossOptions.RoleSessionName
};
});

AliyunStorageConfigureOptions Convert(OssOptions ossOptions)
Expand Down

0 comments on commit 2d67553

Please sign in to comment.