Skip to content

User/jmaxson/loading fixes #328

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public PluginsLoader(
// The constructor for this object hooks up the repository to the extension provider
// (the ExtensionDiscovery in this case). We do not need to hold onto this object
// explicitly, only call its constructor.
new DataExtensionReflector(this.extensionDiscovery, extensionRepository);
new DataExtensionReflector(this.extensionDiscovery, extensionRepository, logger);

this.isDisposed = false;
this.logger = logger;
Expand Down Expand Up @@ -254,7 +254,7 @@ public bool TryLoadPlugins(IEnumerable<string> directories, out IDictionary<stri
{
Guard.NotNull(dir, nameof(directories));
}

failed = new Dictionary<string, ErrorInfo>();

if (!directories.Any())
Expand All @@ -280,21 +280,21 @@ public bool TryLoadPlugins(IEnumerable<string> directories, out IDictionary<stri
failed.Add(dir, emptyErrorInfo);
continue;
}

if (!this.extensionDiscovery.ProcessAssemblies(dir, out var error))
{
failed.Add(dir, error);
continue;
}
}

// If nothing was able to be loaded above, skip finalizing
if (this.extensionRoot.IsLoaded)
{
// TODO (#223): this does redundant work re-finalizing processing sources
// loaded by previous calls to this method. The extension repository
// should get refactored to avoid this redundant work.

this.extensionRoot.FinalizeDataExtensions();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void WhenDisposed_EverthingThrows()
{
var result = TableExtensionReference.TryCreateReference(
typeof(TestTableExtension),
Logger.Null,
out sut);

Assert.IsTrue(result);
Expand Down Expand Up @@ -51,6 +52,7 @@ public void CanDisposeMultipleTimes()
{
var result = TableExtensionReference.TryCreateReference(
typeof(TestTableExtension),
Logger.Null,
out sut);

Assert.IsTrue(result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Performance.SDK.Processing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Performance.SDK.Processing;

namespace Microsoft.Performance.SDK.Runtime.Discovery
{
Expand Down Expand Up @@ -133,14 +133,26 @@ private set
public void ProcessType(Type type, string sourceName)
{
this.ThrowIfDisposed();

if (this.loadedDataSources.ContainsKey(type))
{
// an assembly might exist in multiple folders and we get the same type multiple times.
// do not attempt to recreate the reference in this case
return;
}

if (this.referenceFactory(type, out ProcessingSourceReference reference))
{
Debug.Assert(reference != null);
try
{
this.loadedDataSources.Add(type, reference);
}
catch { }
catch
{
// if we were unable to add this reference, clean it up
reference.TryDispose();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using Microsoft.Performance.SDK.Extensibility;
using Microsoft.Performance.SDK.Extensibility.DataCooking;
using Microsoft.Performance.SDK.Processing;
Expand Down Expand Up @@ -41,8 +42,8 @@ private CompositeDataCookerReference(Type type, ILogger logger)
{
this.InitializeDescriptorData(instance);
}
}
}

/// <summary>
/// Tries to create an instance of <see cref="ICompositeDataCookerReference"/> based on the
/// <paramref name="candidateType"/>.
Expand Down Expand Up @@ -71,7 +72,7 @@ internal static bool TryCreateReference(
{
return TryCreateReference(
candidateType,
Runtime.Logger.Create<CompositeDataCookerReference>(),
Microsoft.Performance.SDK.Runtime.Logger.Null,
out reference);
}

Expand Down Expand Up @@ -105,8 +106,8 @@ internal static bool TryCreateReference(
ILogger logger,
out ICompositeDataCookerReference reference)
{
Guard.NotNull(candidateType, nameof(candidateType));
Guard.NotNull(logger, nameof(logger));
Debug.Assert(candidateType != null, $"{nameof(candidateType)} cannot be null.");
Debug.Assert(logger != null, $"{nameof(logger)} cannot be null.");

reference = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.Performance.SDK.Extensibility.DataCooking;
using Microsoft.Performance.SDK.Extensibility.DataCooking.SourceDataCooking;
using Microsoft.Performance.SDK.Processing;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.DataProcessors;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.Dependency;

namespace Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.DataCookers
Expand Down Expand Up @@ -141,7 +140,7 @@ internal static bool TryCreateReference(
{
return TryCreateReference(
candidateType,
Runtime.Logger.Create<SourceDataCookerReference>(),
Runtime.Logger.Null,
out reference);
}

Expand Down Expand Up @@ -176,7 +175,8 @@ internal static bool TryCreateReference(
ILogger logger,
out ISourceDataCookerReference reference)
{
Guard.NotNull(candidateType, nameof(candidateType));
Debug.Assert(candidateType != null, $"{nameof(candidateType)} cannot be null.");
Debug.Assert(logger != null, $"{nameof(logger)} cannot be null.");

reference = null;

Expand All @@ -202,7 +202,7 @@ internal static bool TryCreateReference(
// There must be an empty, public constructor
if (!candidateType.TryGetEmptyPublicConstructor(out var constructor))
{
Console.Error.WriteLine(
logger.Error(
$"Warning: type {candidateType} seems to be a data cooker, but is missing an empty public " +
"constructor.");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Performance.SDK.Extensibility.SourceParsing;
using Microsoft.Performance.SDK.Processing;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.DataCookers;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.DataProcessors;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.Repository;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.Tables;

Expand All @@ -19,13 +18,13 @@ namespace Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions
public class DataExtensionFactory
: IDataExtensionFactory
{
private static readonly DataExtensionRepository SingletonRepository
private static readonly DataExtensionRepository SingletonRepository
= new DataExtensionRepository();

/// <summary>
/// Gets a singleton instance of this class.
/// </summary>
public IDataExtensionRepositoryBuilder SingletonDataExtensionRepository
public IDataExtensionRepositoryBuilder SingletonDataExtensionRepository
=> DataExtensionFactory.SingletonRepository;

/// <summary>
Expand All @@ -43,10 +42,34 @@ public IDataExtensionRepositoryBuilder SingletonDataExtensionRepository
public bool TryCreateSourceDataCookerReference(
Type candidateType,
out ISourceDataCookerReference reference)
{
return TryCreateSourceDataCookerReference(candidateType, Logger.Null, out reference);
}

/// <summary>
/// Generate a source data cooker reference from a given type.
/// </summary>
/// <param name="candidateType">
/// Data extension type.
/// </param>
/// <param name="reference">
/// Data extension reference.
/// </param>
/// <param name="logger">
/// Logs messages during reference creation.
/// </param>
/// <returns>
/// <c>true</c> if succeeded, <c>false</c> otherwise.
/// </returns>
public bool TryCreateSourceDataCookerReference(
Type candidateType,
ILogger logger,
out ISourceDataCookerReference reference)
{
Guard.NotNull(candidateType, nameof(candidateType));
Guard.NotNull(logger, nameof(logger));

return SourceDataCookerReference.TryCreateReference(candidateType, out reference);
return SourceDataCookerReference.TryCreateReference(candidateType, logger, out reference);
}

/// <summary>
Expand All @@ -64,10 +87,34 @@ public bool TryCreateSourceDataCookerReference(
public bool TryCreateCompositeDataCookerReference(
Type candidateType,
out ICompositeDataCookerReference reference)
{
return TryCreateCompositeDataCookerReference(candidateType, Logger.Null, out reference);
}

/// <summary>
/// Generate a composite data cooker reference from a given type.
/// </summary>
/// <param name="candidateType">
/// Data extension type.
/// </param>
/// <param name="logger">
/// Logs messages during reference creation.
/// </param>
/// <param name="reference">
/// Data extension reference.
/// </param>
/// <returns>
/// <c>true</c> if succeeded, <c>false</c> otherwise.
/// </returns>
public bool TryCreateCompositeDataCookerReference(
Type candidateType,
ILogger logger,
out ICompositeDataCookerReference reference)
{
Guard.NotNull(candidateType, nameof(candidateType));
Guard.NotNull(logger, nameof(logger));

return CompositeDataCookerReference.TryCreateReference(candidateType, out reference);
return CompositeDataCookerReference.TryCreateReference(candidateType, logger, out reference);
}

// TODO: __SDK_DP__
Expand Down Expand Up @@ -108,10 +155,34 @@ public bool TryCreateCompositeDataCookerReference(
public bool TryCreateTableReference(
Type candidateType,
out ITableExtensionReference reference)
{
return TryCreateTableReference(candidateType, Logger.Null, out reference);
}

/// <summary>
/// Generate a table reference from a given type.
/// </summary>
/// <param name="candidateType">
/// Data extension type.
/// </param>
/// <param name="logger">
/// Logs messages during reference creation.
/// </param>
/// <param name="reference">
/// Data extension reference.
/// </param>
/// <returns>
/// <c>true</c> if succeeded, <c>false</c> otherwise.
/// </returns>
public bool TryCreateTableReference(
Type candidateType,
ILogger logger,
out ITableExtensionReference reference)
{
Guard.NotNull(candidateType, nameof(candidateType));
Guard.NotNull(logger, nameof(logger));

return TableExtensionReference.TryCreateReference(candidateType, out reference);
return TableExtensionReference.TryCreateReference(candidateType, logger, out reference);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using Microsoft.Performance.SDK.Processing;
using Microsoft.Performance.SDK.Runtime.Discovery;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.DataCookers;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.DataProcessors;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.Repository;
using Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions.Tables;
using System;
using System.Diagnostics;

namespace Microsoft.Performance.SDK.Runtime.Extensibility.DataExtensions
{
Expand All @@ -18,6 +18,7 @@ public class DataExtensionReflector
: IExtensionTypeObserver
{
private readonly IDataExtensionRepositoryBuilder repoBuilder;
private readonly ILogger logger;

/// <summary>
/// Initializes a new instance of the DataExtensionReflector class.
Expand All @@ -28,33 +29,41 @@ public class DataExtensionReflector
/// <param name="repoBuilder">
/// Provides an output to store discovered extensions.
/// </param>
/// <param name="logger">
/// Logs messages during reference creation.
/// </param>
public DataExtensionReflector(
IExtensionTypeProvider extensionProvider,
IDataExtensionRepositoryBuilder repoBuilder)
IDataExtensionRepositoryBuilder repoBuilder,
ILogger logger)
{
Guard.NotNull(logger, nameof(logger));

this.repoBuilder = repoBuilder;
this.logger = logger;

extensionProvider.RegisterTypeConsumer(this);
}

/// <inheritdoc />
public void ProcessType(Type type, string sourceName)
{
// Find source data cookers
if (SourceDataCookerReference.TryCreateReference(type, out var sourceDataCookerReference))
if (SourceDataCookerReference.TryCreateReference(type, this.logger, out var sourceDataCookerReference))
{
Debug.Assert(sourceDataCookerReference != null);
this.repoBuilder.AddSourceDataCookerReference(sourceDataCookerReference);
}

// Find composite data cookers
if (CompositeDataCookerReference.TryCreateReference(type, out var compositeDataCookerReference))
if (CompositeDataCookerReference.TryCreateReference(type, this.logger, out var compositeDataCookerReference))
{
Debug.Assert(compositeDataCookerReference != null);
this.repoBuilder.AddCompositeDataCookerReference(compositeDataCookerReference);
}

// Find tables
if (TableExtensionReference.TryCreateReference(type, out var dataExtensionReference))
if (TableExtensionReference.TryCreateReference(type, this.logger, out var dataExtensionReference))
{
Debug.Assert(dataExtensionReference != null);
this.repoBuilder.AddTableExtensionReference(dataExtensionReference);
Expand Down
Loading