Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/next' into fl/revamp-task-result
Browse files Browse the repository at this point in the history
# Conflicts:
#	UnifiedApi/Client/Services/Submitter/Service.cs
  • Loading branch information
lemaitre-aneo committed Oct 18, 2022
2 parents 7547955 + 071f392 commit 641dc54
Show file tree
Hide file tree
Showing 46 changed files with 1,791 additions and 556 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ jobs:
ingress: 'None'

- name: Run EndToEnd.Tests
timeout-minutes: 15
run: |
cd Tests
mkdir -p ${HOME}/data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Client" Version="3.0.3" />
<PackageReference Include="Grpc.Core" Version="2.46.3" />
<PackageReference Include="ArmoniK.Api.Client" Version="3.0.4" />
<PackageReference Include="Grpc.Core" Version="2.46.5" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>


<Import Project="$(RootProjectDir)/Directory.Build.props"/>
<Import Project="$(RootProjectDir)/Directory.Build.props" />


</Project>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2022. All rights reserved.
// W. Kirschenmann <[email protected]>
// J. Gurhem <[email protected]>
// D. Dubuc <[email protected]>
// L. Ziane Khodja <[email protected]>
// F. Lemaitre <[email protected]>
// S. Djebbar <[email protected]>
// J. Fonseca <[email protected]>
// D. Brasseur <[email protected]>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Concurrent;

using Grpc.Core;

using JetBrains.Annotations;

using Microsoft.Extensions.Logging;

namespace ArmoniK.DevelopmentKit.Client.Common.Submitter;

/// <summary>
/// Helper to have a connection pool for gRPC services
/// </summary>
public sealed class ChannelPool
{
private readonly Func<ChannelBase> channelFactory_;

[CanBeNull]
private readonly ILogger<ChannelPool> logger_;

private readonly ConcurrentBag<ChannelBase> pool_;

/// <summary>
/// Constructs a new channelPool
/// </summary>
/// <param name="channelFactory">Function used to create new channels</param>
/// <param name="loggerFactory">loggerFactory used to instantiate a logger for the pool</param>
public ChannelPool(Func<ChannelBase> channelFactory,
[CanBeNull] ILoggerFactory loggerFactory = null)
{
channelFactory_ = channelFactory;
pool_ = new ConcurrentBag<ChannelBase>();
logger_ = loggerFactory?.CreateLogger<ChannelPool>();
}

/// <summary>
/// Get a channel from the pool. If the pool is empty, create a new channel
/// </summary>
/// <returns>A ChannelBase used by nobody else</returns>
private ChannelBase AcquireChannel()
{
if (pool_.TryTake(out var channel))
{
logger_?.LogDebug("Acquired already existing channel {channel} from pool",
channel);
return channel;
}

channel = channelFactory_();
logger_?.LogInformation("Created and acquired new channel {channel} from pool",
channel);
return channel;
}

/// <summary>
/// Release a ChannelBase to the pool that could be reused later by someone else
/// </summary>
/// <param name="channel">Channel to release</param>
private void ReleaseChannel(ChannelBase channel)
{
logger_?.LogDebug("Released channel {channel} to pool",
channel);
pool_.Add(channel);
}

/// <summary>
/// Get a channel that will be automatically released when disposed
/// </summary>
/// <returns></returns>
public ChannelGuard GetChannel()
=> new(this);

/// <summary>
/// Call f with an acquired channel
/// </summary>
/// <param name="f">Function to be called</param>
/// <typeparam name="T">Type of the return type of f</typeparam>
/// <returns>Value returned by f</returns>
public T WithChannel<T>(Func<ChannelBase, T> f)
{
using var channel = GetChannel();
return f(channel.Channel);
}

/// <summary>
/// Call f with an acquired channel
/// </summary>
/// <param name="f">Function to be called</param>
public void WithChannel(Action<ChannelBase> f)
{
using var channel = GetChannel();
f(channel.Channel);
}

/// <summary>
/// Helper class that acquires a channel from a pool when constructed, and releases it when disposed
/// </summary>
public sealed class ChannelGuard : IDisposable
{
/// <summary>
/// Channel that is used by nobody else
/// </summary>
public readonly ChannelBase Channel;

private readonly ChannelPool pool_;

/// <summary>
/// Acquire a channel that will be released when disposed
/// </summary>
/// <param name="channelPool"></param>
public ChannelGuard(ChannelPool channelPool)
{
pool_ = channelPool;
Channel = channelPool.AcquireChannel();
}

/// <inheritdoc />
public void Dispose()
=> pool_.ReleaseChannel(Channel);

/// <summary>
/// Implicit convert a ChannelGuard into a ChannelBase
/// </summary>
/// <param name="guard">ChannelGuard</param>
/// <returns>ChannelBase</returns>
public static implicit operator ChannelBase(ChannelGuard guard)
=> guard.Channel;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#if NET5_0_OR_GREATER
using Grpc.Net.Client;
using Grpc.Core;

using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
#else
using Grpc.Core;
#endif
using System;
using System.IO;
using System.Net.Http;

using Grpc.Core;

using JetBrains.Annotations;

using Microsoft.Extensions.Logging;
#if NET5_0_OR_GREATER
using Grpc.Net.Client;

using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
#endif

namespace ArmoniK.DevelopmentKit.Client.Common.Submitter;

Expand All @@ -30,9 +29,9 @@ public class ClientServiceConnector
/// <param name="sslValidation">Optional : Check if the ssl must have a strong validation (default true)</param>
/// <param name="loggerFactory">Optional : the logger factory to create the logger</param>
/// <returns></returns>
public static ChannelBase ControlPlaneConnection(string endPoint,
bool sslValidation = true,
ILoggerFactory loggerFactory = null)
private static ChannelBase ControlPlaneConnection(string endPoint,
bool sslValidation = true,
ILoggerFactory loggerFactory = null)
=> ControlPlaneConnection(endPoint,
"",
"",
Expand All @@ -42,17 +41,17 @@ public static ChannelBase ControlPlaneConnection(string endPoint,
/// <summary>
/// Open Connection with the control plane with mTLS authentication
/// </summary>
/// <param name="endPoint"></param>
/// <param name="endPoint">The address and port of control plane</param>
/// <param name="clientCertFilename">The certificate filename in a pem format</param>
/// <param name="clientKeyFilename">The client key filename in a pem format</param>
/// <param name="sslValidation">Check if the ssl must have a strong validation</param>
/// <param name="loggerFactory">Optional logger factory</param>
/// <returns></returns>
public static ChannelBase ControlPlaneConnection(string endPoint,
string clientCertFilename,
string clientKeyFilename,
bool sslValidation = true,
[CanBeNull] ILoggerFactory loggerFactory = null)
private static ChannelBase ControlPlaneConnection(string endPoint,
string clientCertFilename,
string clientKeyFilename,
bool sslValidation = true,
[CanBeNull] ILoggerFactory loggerFactory = null)
{
var logger = loggerFactory?.CreateLogger<ClientServiceConnector>();
if ((!string.IsNullOrEmpty(clientCertFilename) && string.IsNullOrEmpty(clientKeyFilename)) ||
Expand Down Expand Up @@ -99,15 +98,15 @@ public static ChannelBase ControlPlaneConnection(string endP
/// <summary>
/// Open Connection with the control plane with mTLS authentication
/// </summary>
/// <param name="endPoint"></param>
/// <param name="endPoint">The address and port of control plane</param>
/// <param name="clientPem">The pair certificate + key data in a pem format</param>
/// <param name="sslValidation">Check if the ssl must have a strong validation</param>
/// <param name="loggerFactory">Optional logger factory</param>
/// <returns></returns>
public static ChannelBase ControlPlaneConnection(string endPoint,
Tuple<string, string> clientPem = null,
bool sslValidation = true,
[CanBeNull] ILoggerFactory loggerFactory = null)
private static ChannelBase ControlPlaneConnection(string endPoint,
Tuple<string, string> clientPem = null,
bool sslValidation = true,
[CanBeNull] ILoggerFactory loggerFactory = null)
{
var uri = new Uri(endPoint);

Expand Down Expand Up @@ -168,4 +167,59 @@ public static ChannelBase ControlPlaneConnection(string endP
#endif
return channel;
}

/// <summary>
/// Create a connection pool to the control plane with or without SSL and no mTLS
/// </summary>
/// <param name="endPoint">The address and port of control plane</param>
/// <param name="sslValidation">Optional : Check if the ssl must have a strong validation (default true)</param>
/// <param name="loggerFactory">Optional : the logger factory to create the logger</param>
/// <returns></returns>
public static ChannelPool ControlPlaneConnectionPool(string endPoint,
bool sslValidation = true,
ILoggerFactory loggerFactory = null)
=> new(() => ControlPlaneConnection(endPoint,
sslValidation,
loggerFactory),
loggerFactory);


/// <summary>
/// Create a connection pool to the control plane with mTLS authentication
/// </summary>
/// <param name="endPoint">The address and port of control plane</param>
/// <param name="clientCertFilename">The certificate filename in a pem format</param>
/// <param name="clientKeyFilename">The client key filename in a pem format</param>
/// <param name="sslValidation">Check if the ssl must have a strong validation</param>
/// <param name="loggerFactory">Optional logger factory</param>
/// <returns></returns>
public static ChannelPool ControlPlaneConnectionPool(string endPoint,
string clientCertFilename,
string clientKeyFilename,
bool sslValidation = true,
[CanBeNull] ILoggerFactory loggerFactory = null)
=> new(() => ControlPlaneConnection(endPoint,
clientCertFilename,
clientKeyFilename,
sslValidation,
loggerFactory),
loggerFactory);

/// <summary>
/// Create a connection pool to the control plane with mTLS authentication
/// </summary>
/// <param name="endPoint">The address and port of control plane</param>
/// <param name="clientPem">The pair certificate + key data in a pem format</param>
/// <param name="sslValidation">Check if the ssl must have a strong validation</param>
/// <param name="loggerFactory">Optional logger factory</param>
/// <returns>The connection pool</returns>
public static ChannelPool ControlPlaneConnectionPool(string endPoint,
Tuple<string, string> clientPem = null,
bool sslValidation = true,
[CanBeNull] ILoggerFactory loggerFactory = null)
=> new(() => ControlPlaneConnection(endPoint,
clientPem,
sslValidation,
loggerFactory),
loggerFactory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.2" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
<PackageReference Include="protobuf-net" Version="3.1.17" />
<PackageReference Include="protobuf-net" Version="3.1.22" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion Common/ArmoniK.DevelopmentKit.Common/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>


<Import Project="$(RootProjectDir)/Directory.Build.props"/>
<Import Project="$(RootProjectDir)/Directory.Build.props" />


</Project>
Loading

0 comments on commit 641dc54

Please sign in to comment.