-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from lilla28/fix/module-loader-registration-vol2
fix(module-loader) - Fixed ModuleLoader behavior with multiple modulecatalogs
- Loading branch information
Showing
17 changed files
with
397 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!-- Morgan Stanley makes this available to you under the Apache License, Version 2.0 (the "License"). You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. See the NOTICE file distributed with this work for additional information regarding copyright ownership. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> | ||
|
||
| Type | ID | Status | Title | | ||
| ------------- | ------------- | ------------- | ----------------------- | | ||
| ADR | adr-014 | Accepted | Multiple ModuleCatalogs | | ||
|
||
|
||
# Architecture Decision Record: Multiple ModuleCatalogs | ||
|
||
## Context | ||
ComposeUI aims to support many different kind of modules. These modules, that can be started by the ModuleLoader should be stored in ModuleCatalogs and every module should have its unique module id. The user should have the ability to handle multiple ModuleCatalogs with the ModuleLoader. | ||
|
||
General problem statement: | ||
- When multiple ModuleCatalogs are used, some modules could have the same module id in different ModuleCatalogs which could cause issues while starting or stopping the module via the ModuleLoader. | ||
|
||
## Decision | ||
- If the same module id is being used for multiple modules (likely in different ModuleCatalogs), the first occurrence will be used. In case of multiple ModuleCatalog registrations, the order of the registration determines which module can be used. | ||
|
||
# Alternative solutions that we considered | ||
- Throwing an exception when multiple modules have the same moduleId either in one or multiple ModuleCatalogs. This might cause an issue that is unresolvable by the developer or the enduser. | ||
- Prefixing the modules with the name of the ModuleCatalog, but prefixing ids would be non-standard. | ||
- Aggregating the registered ModuleCatalogs should be implemented by the developer of the application. | ||
|
||
## Consequences | ||
- Our implementation is more permissive than the standard. | ||
- We must ensure keeping the order of the ModuleCatalog registrations as the order of registrations will determine which module is contained by our aggregated ModuleCatalog in case of duplicate moduleIds. | ||
- We must ensure keeping the order of the modules within the invidual ModuleCatalogs as the order of the registered modules will determine which module is contained by our aggregated ModuleCatalog in case of duplicate moduleIds. | ||
- We must ensure this order when fetching modules asynchronously from the ModuleCatalogs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...der/dotnet/src/MorganStanley.ComposeUI.ModuleLoader.Abstractions/ModuleLoaderException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Morgan Stanley makes this available to you under the Apache License, | ||
* Version 2.0 (the "License"). You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. Unless required by applicable law or agreed | ||
* to in writing, software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
using System.Runtime.Serialization; | ||
|
||
namespace MorganStanley.ComposeUI.ModuleLoader; | ||
|
||
public class ModuleLoaderException : Exception | ||
{ | ||
public ModuleLoaderException() | ||
{ | ||
} | ||
|
||
protected ModuleLoaderException(SerializationInfo info, StreamingContext context) : base(info, context) | ||
{ | ||
} | ||
|
||
public ModuleLoaderException(string? message) : base(message) | ||
{ | ||
} | ||
|
||
public ModuleLoaderException(string? message, Exception? innerException) : base(message, innerException) | ||
{ | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...r/dotnet/src/MorganStanley.ComposeUI.ModuleLoader.Abstractions/ModuleNotFoundException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Morgan Stanley makes this available to you under the Apache License, | ||
* Version 2.0 (the "License"). You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. Unless required by applicable law or agreed | ||
* to in writing, software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
namespace MorganStanley.ComposeUI.ModuleLoader; | ||
|
||
public class ModuleNotFoundException : ModuleLoaderException | ||
{ | ||
public ModuleNotFoundException(string moduleId) : base($"Unknown module id: {moduleId}") | ||
{ | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/module-loader/dotnet/src/MorganStanley.ComposeUI.ModuleLoader/AggregateModuleCatalog.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Morgan Stanley makes this available to you under the Apache License, | ||
// Version 2.0 (the "License"). You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// See the NOTICE file distributed with this work for additional information | ||
// regarding copyright ownership. Unless required by applicable law or agreed | ||
// to in writing, software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
// or implied. See the License for the specific language governing permissions | ||
// and limitations under the License. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace MorganStanley.ComposeUI.ModuleLoader; | ||
|
||
internal class AggregateModuleCatalog : IModuleCatalog | ||
{ | ||
private readonly IEnumerable<IModuleCatalog> _moduleCatalogs; | ||
private readonly ILogger _logger; | ||
|
||
public AggregateModuleCatalog( | ||
IEnumerable<IModuleCatalog> moduleCatalogs, | ||
ILogger? logger = null) | ||
{ | ||
_moduleCatalogs = moduleCatalogs; | ||
_logger = logger ?? NullLogger.Instance; | ||
_lazyEnumerateModules = new Lazy<Task>(EnumerateModulesCore); | ||
} | ||
|
||
public async Task<IModuleManifest> GetManifest(string moduleId) | ||
{ | ||
await EnumerateModules(); | ||
|
||
return _moduleIdToCatalog.TryGetValue(moduleId, out var catalog) | ||
? await catalog.GetManifest(moduleId) | ||
: throw new ModuleNotFoundException(moduleId); | ||
} | ||
|
||
public async Task<IEnumerable<string>> GetModuleIds() | ||
{ | ||
await EnumerateModules(); | ||
|
||
return _moduleIdToCatalog.Keys; | ||
} | ||
|
||
private readonly Lazy<Task> _lazyEnumerateModules; | ||
private readonly ConcurrentDictionary<string, IModuleCatalog> _moduleIdToCatalog = new(); | ||
|
||
private Task EnumerateModules() => _lazyEnumerateModules.Value; | ||
|
||
private async Task EnumerateModulesCore() | ||
{ | ||
var moduleCatalogs = _moduleCatalogs.ToDictionary(x => x, y => y.GetModuleIds()); | ||
// Services/DI registrations appear in the order they were registered when resolved via IEnumerable<{SERVICE}> | ||
// https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection | ||
foreach (var moduleCatalog in moduleCatalogs) | ||
{ | ||
var moduleIds = await moduleCatalog.Value; | ||
|
||
foreach (var moduleId in moduleIds) | ||
{ | ||
if (!_moduleIdToCatalog.TryGetValue(moduleId, out var catalog)) | ||
{ | ||
_moduleIdToCatalog[moduleId] = moduleCatalog.Key; | ||
} | ||
else | ||
{ | ||
if (_logger.IsEnabled(LogLevel.Warning)) | ||
{ | ||
var moduleManifest = await catalog.GetManifest(moduleId); | ||
_logger.LogWarning( | ||
$"ModuleId: {moduleId} is already contained by an another {nameof(IModuleCatalog)} with name {moduleManifest.Name}. Please consider using unique ids for modules. The first occurrence of the module will be saved and used by the {nameof(ModuleLoader)}."); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.