Skip to content

Commit

Permalink
Streamline LINQ/Enumerable use (#6482)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8fe04dc)
  • Loading branch information
pentp authored and sergeybykov committed Apr 15, 2020
1 parent 6483782 commit 91d23ba
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ internal async Task<int> DeleteTableEntries(string clusterId)

await DeleteEntriesBatch(entriesList);

return entriesList.Count();
return entriesList.Count;
}

public async Task CleanupDefunctSiloEntries(DateTimeOffset beforeDate)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.EventHubs;
Expand All @@ -12,7 +12,6 @@ namespace Orleans.ServiceBus.Providers
public static class EventDataExtensions
{
private const string EventDataPropertyStreamNamespaceKey = "StreamNamespace";
private static readonly string[] SkipProperties = { EventDataPropertyStreamNamespaceKey };

/// <summary>
/// Adds stream namespace to the EventData
Expand Down Expand Up @@ -48,7 +47,7 @@ public static string GetStreamNamespaceProperty(this EventData eventData)
public static byte[] SerializeProperties(this EventData eventData, SerializationManager serializationManager)
{
var writeStream = new BinaryTokenStreamWriter();
serializationManager.Serialize(eventData.Properties.Where(kvp => !SkipProperties.Contains(kvp.Key)).ToList(), writeStream);
serializationManager.Serialize(eventData.Properties.Where(kvp => !string.Equals(kvp.Key, EventDataPropertyStreamNamespaceKey, StringComparison.Ordinal)).ToList(), writeStream);
var result = writeStream.ToByteArray();
writeStream.ReleaseBuffers();
return result;
Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.CodeGenerator/Analysis/CompilationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void ExpandKnownAssemblies(IAssemblySymbol asm)
if (log.IsEnabled(LogLevel.Debug)) log.LogDebug($"Known assembly {type.ContainingAssembly} from assembly {asm}");

// Check if the attribute has the TreatTypesAsSerializable property set.
var prop = attr.NamedArguments.Where(a => a.Key.Equals("TreatTypesAsSerializable")).Select(a => a.Value).FirstOrDefault();
var prop = attr.NamedArguments.FirstOrDefault(a => a.Key.Equals("TreatTypesAsSerializable")).Value;
if (prop.Type != null)
{
var treatAsSerializable = (bool)prop.Value;
Expand Down Expand Up @@ -337,7 +337,7 @@ void ExpandKnownTypes(IEnumerable<IAssemblySymbol> asm)
this.KnownTypes.Add(type);

var throwOnFailure = false;
var throwOnFailureParam = attr.ConstructorArguments.Skip(1).FirstOrDefault();
var throwOnFailureParam = attr.ConstructorArguments.ElementAtOrDefault(2);
if (throwOnFailureParam.Type != null)
{
throwOnFailure = (bool)throwOnFailureParam.Value;
Expand Down
6 changes: 3 additions & 3 deletions src/Orleans.Core/CodeGeneration/TypeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ private static void GetParseableName(
builder.AppendFormat(
"{0}[{1}]",
elementType,
string.Concat(Enumerable.Range(0, type.GetArrayRank() - 1).Select(_ => ',')));
new string(',', type.GetArrayRank() - 1));
}

return;
Expand Down Expand Up @@ -755,7 +755,7 @@ private static void GetParseableName(
var unadornedTypeName = getNameFunc(type);
builder.Append(EscapeIdentifier(unadornedTypeName));
var generics =
Enumerable.Range(0, Math.Min(type.GetGenericArguments().Count(), typeArguments.Count))
Enumerable.Range(0, Math.Min(type.GetGenericArguments().Length, typeArguments.Count))
.Select(_ => typeArguments.Dequeue())
.ToList();
if (generics.Count > 0 && options.IncludeTypeParameters)
Expand All @@ -772,7 +772,7 @@ private static void GetParseableName(
var unadornedTypeName = getNameFunc(type);
builder.Append(EscapeIdentifier(unadornedTypeName));
var generics =
Enumerable.Range(0, Math.Min(type.GetGenericArguments().Count(), typeArguments.Count))
Enumerable.Range(0, Math.Min(type.GetGenericArguments().Length, typeArguments.Count))
.Select(_ => typeArguments.Dequeue())
.ToList();
if (generics.Count > 0 && options.IncludeTypeParameters)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Configuration.Internal;
using System.Linq;

namespace Orleans.Configuration
{
Expand All @@ -16,13 +16,13 @@ public static IServiceCollection ConfigureFormatter<TOptions, TOptionFormatter>(
where TOptions : class
where TOptionFormatter : class, IOptionFormatter<TOptions>
{
var registration = services.FirstOrDefault(service => service.ServiceType == typeof(IOptionFormatter<TOptions>));
if (registration == null)
if (!services.Any(service => service.ServiceType == typeof(IOptionFormatter<TOptions>)))
{
services
.AddSingleton<IOptionFormatter<TOptions>, TOptionFormatter>()
.AddFromExisting<IOptionFormatter, IOptionFormatter<TOptions>>();
} else
}
else
{
// override IOptionFormatter<TOptions>
services.AddSingleton<IOptionFormatter<TOptions>, TOptionFormatter>();
Expand Down Expand Up @@ -50,8 +50,7 @@ public static IServiceCollection TryConfigureFormatter<TOptions, TOptionFormatte
where TOptions : class
where TOptionFormatter : class, IOptionFormatter<TOptions>
{
var registration = services.FirstOrDefault(service => service.ServiceType == typeof(IOptionFormatter<TOptions>));
if (registration == null)
if (!services.Any(service => service.ServiceType == typeof(IOptionFormatter<TOptions>)))
services.ConfigureFormatter<TOptions, TOptionFormatter>();
return services;
}
Expand All @@ -73,8 +72,7 @@ public static IServiceCollection TryConfigureFormatterResolver<TOptions, TOption
where TOptions : class
where TOptionFormatterResolver : class, IOptionFormatterResolver<TOptions>
{
var registration = services.FirstOrDefault(service => service.ServiceType == typeof(IOptionFormatterResolver<TOptions>));
if (registration == null)
if (!services.Any(service => service.ServiceType == typeof(IOptionFormatterResolver<TOptions>)))
return services.ConfigureFormatterResolver<TOptions, TOptionFormatterResolver>();
return services;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Orleans.Core/Configuration/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Orleans.Configuration.Internal
{
Expand Down Expand Up @@ -47,8 +47,7 @@ public static void AddFromExisting(this IServiceCollection services, Type servic
/// <param name="services">The service collection.</param>
public static void TryAddFromExisting<TService, TImplementation>(this IServiceCollection services) where TImplementation : TService
{
var providedService = services.FirstOrDefault(service => service.ServiceType == typeof(TService));
if (providedService == null)
if (!services.Any(service => service.ServiceType == typeof(TService)))
{
services.AddFromExisting<TService, TImplementation>();
}
Expand Down
3 changes: 1 addition & 2 deletions src/Orleans.Core/Core/GrainCasterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public static GrainFactory.GrainReferenceCaster CreateGrainReferenceCaster(
// Get the grain reference constructor.
var constructor =
grainReferenceType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
.Where(IsGrainReferenceCopyConstructor)
.FirstOrDefault();
.FirstOrDefault(IsGrainReferenceCopyConstructor);

if (constructor == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Orleans.Core/Messaging/GatewayManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private void UpdateLiveGatewaysSnapshot(IEnumerable<SiloAddress> refreshedGatewa
{
logger.Info(ErrorCode.GatewayManager_FoundKnownGateways,
"Refreshed the live Gateway list. Found {0} gateways from Gateway listProvider: {1}. Picked only known live out of them. Now has {2} live Gateways: {3}. Previous refresh time was = {4}",
knownGateways.Count(),
knownGateways.Count,
Utils.EnumerableToString(knownGateways),
cachedLiveGateways.Count,
Utils.EnumerableToString(cachedLiveGateways),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private string StageAnalysisInfo()

sb.AppendLine();
sb.AppendLine("CPU usage by thread type:");
foreach (var v in cpuBreakdown.OrderBy(key => (-1*key.Value)))
foreach (var v in cpuBreakdown.OrderByDescending(key => key.Value))
sb.AppendLine(" " + v.Value.ToString("F3") + ", " + v.Key);

sb.AppendLine();
Expand Down
18 changes: 8 additions & 10 deletions src/Orleans.Runtime/Catalog/Catalog.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.CodeGeneration;
using Orleans.Configuration;
using Orleans.GrainDirectory;
using Orleans.Internal;
using Orleans.MultiCluster;
using Orleans.Runtime.GrainDirectory;
using Orleans.Runtime.Messaging;
using Orleans.Runtime.Placement;
using Orleans.Runtime.Scheduler;
using Orleans.Runtime.Versions;
using Orleans.Serialization;
using Orleans.Streams.Core;
using Orleans.Streams;
using System.Runtime.ExceptionServices;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.Internal;
using Orleans.Streams.Core;

namespace Orleans.Runtime
{
Expand Down Expand Up @@ -1084,7 +1082,7 @@ private void RerouteAllQueuedMessages(ActivationData activation, ActivationAddre
List<Message> msgs = activation.DequeueAllWaitingMessages();
if (msgs == null || msgs.Count <= 0) return;

if (logger.IsEnabled(LogLevel.Debug)) logger.Debug(ErrorCode.Catalog_RerouteAllQueuedMessages, String.Format("RerouteAllQueuedMessages: {0} msgs from Invalid activation {1}.", msgs.Count(), activation));
if (logger.IsEnabled(LogLevel.Debug)) logger.Debug(ErrorCode.Catalog_RerouteAllQueuedMessages, String.Format("RerouteAllQueuedMessages: {0} msgs from Invalid activation {1}.", msgs.Count, activation));
this.Dispatcher.ProcessRequestsToInvalidActivation(msgs, activation.Address, forwardingAddress, failedOperation, exc);
}
}
Expand All @@ -1108,7 +1106,7 @@ private void RejectAllQueuedMessages(
if (logger.IsEnabled(LogLevel.Debug))
logger.Debug(
ErrorCode.Catalog_RerouteAllQueuedMessages,
string.Format("RejectAllQueuedMessages: {0} msgs from Invalid activation {1}.", msgs.Count(), activation));
string.Format("RejectAllQueuedMessages: {0} msgs from Invalid activation {1}.", msgs.Count, activation));
this.Dispatcher.ProcessRequestsToInvalidActivation(
msgs,
activation.Address,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Orleans.Runtime;
Expand All @@ -25,7 +24,7 @@ public class DeploymentBasedQueueBalancer : QueueBalancerBase, IStreamQueueBalan
private readonly IDeploymentConfiguration deploymentConfig;
private readonly DeploymentBasedQueueBalancerOptions options;
private readonly ConcurrentDictionary<SiloAddress, bool> immatureSilos;
private ReadOnlyCollection<QueueId> allQueues;
private List<QueueId> allQueues;
private bool isStarting;

public DeploymentBasedQueueBalancer(
Expand Down Expand Up @@ -63,7 +62,7 @@ public override Task Initialize(IStreamQueueMapper queueMapper)
{
throw new ArgumentNullException("queueMapper");
}
this.allQueues = new ReadOnlyCollection<QueueId>(queueMapper.GetAllQueues().ToList());
this.allQueues = queueMapper.GetAllQueues().ToList();
NotifyAfterStart().Ignore();
return base.Initialize(queueMapper);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Orleans.Configuration;
using Orleans.Internal;
using Orleans.LeaseProviders;
using Orleans.Runtime;
using Orleans.Configuration;
using Orleans.Timers;
using System.Diagnostics;
using System.Reflection.Metadata;
using Orleans.Internal;

namespace Orleans.Streams
{
Expand All @@ -37,12 +34,12 @@ public AcquiredQueue(int order, QueueId queueId, AcquiredLease lease)
private readonly LeaseBasedQueueBalancerOptions options;
private readonly ILeaseProvider leaseProvider;
private readonly ITimerRegistry timerRegistry;
private readonly AsyncSerialExecutor executor;
private ReadOnlyCollection<QueueId> allQueues;
private List<AcquiredQueue> myQueues;
private readonly AsyncSerialExecutor executor = new AsyncSerialExecutor();
private int allQueuesCount;
private readonly List<AcquiredQueue> myQueues = new List<AcquiredQueue>();
private IDisposable leaseMaintenanceTimer;
private IDisposable leaseAquisitionTimer;
private IResourceSelector<QueueId> queueSelector;
private RoundRobinSelector<QueueId> queueSelector;
private int responsibility;
private int leaseOrder;

Expand All @@ -55,8 +52,6 @@ public LeaseBasedQueueBalancer(string name, LeaseBasedQueueBalancerOptions optio
this.options = options;
this.leaseProvider = leaseProvider;
this.timerRegistry = timerRegistry;
this.executor = new AsyncSerialExecutor();
this.myQueues = new List<AcquiredQueue>();
}

public static IStreamQueueBalancer Create(IServiceProvider services, string name)
Expand All @@ -75,11 +70,12 @@ public override Task Initialize(IStreamQueueMapper queueMapper)
{
throw new ArgumentNullException("queueMapper");
}
this.allQueues = new ReadOnlyCollection<QueueId>(queueMapper.GetAllQueues().ToList());
var allQueues = queueMapper.GetAllQueues().ToList();
this.allQueuesCount = allQueues.Count;

//Selector default to round robin selector now, but we can make a further change to make selector configurable if needed. Selector algorithm could
//be affecting queue balancing stablization time in cluster initializing and auto-scaling
this.queueSelector = new RoundRobinSelector<QueueId>(this.allQueues, new Random(this.GetHashCode()));
this.queueSelector = new RoundRobinSelector<QueueId>(allQueues, new Random(this.GetHashCode()));
return base.Initialize(queueMapper);
}

Expand Down Expand Up @@ -312,7 +308,7 @@ private async Task<bool> RenewLeases()
return allRenewed;
var results = await this.leaseProvider.Renew(this.options.LeaseCategory, this.myQueues.Select(queue => queue.AcquiredLease).ToArray());
//update myQueues list with successfully renewed leases
for (var i = 0; i < results.Count(); i++)
for (var i = 0; i < results.Length; i++)
{
AcquireLeaseResult result = results[i];
switch (result.StatusCode)
Expand Down Expand Up @@ -396,16 +392,15 @@ private bool AmGreedy(int overflow, HashSet<SiloAddress> activeSilos)
// TODO: use heap? - jbragg
return activeSilos.OrderBy(silo => silo)
.Take(overflow)
.ToList()
.Contains(base.SiloAddress);
}

private async Task UpdateResponsibilities(HashSet<SiloAddress> activeSilos)
{
if (base.Cancellation.IsCancellationRequested) return;
var activeSiloCount = Math.Max(1, activeSilos.Count);
this.responsibility = this.allQueues.Count / activeSiloCount;
var overflow = this.allQueues.Count % activeSiloCount;
this.responsibility = this.allQueuesCount / activeSiloCount;
var overflow = this.allQueuesCount % activeSiloCount;
if(overflow != 0 && this.AmGreedy(overflow, activeSilos))
{
this.responsibility++;
Expand All @@ -414,7 +409,7 @@ private async Task UpdateResponsibilities(HashSet<SiloAddress> activeSilos)
if (this.Logger.IsEnabled(LogLevel.Debug))
{
this.Logger.LogDebug("Updating Responsibilities for {QueueCount} queue over {SiloCount} silos. Need {MinQueueCount} queues, have {MyQueueCount}",
this.allQueues.Count, activeSiloCount, this.responsibility, this.myQueues.Count);
this.allQueuesCount, activeSiloCount, this.responsibility, this.myQueues.Count);
}

if (this.myQueues.Count < this.responsibility && this.leaseAquisitionTimer == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Orleans.Streams
Expand All @@ -11,12 +10,12 @@ namespace Orleans.Streams
/// <typeparam name="T"></typeparam>
internal class RoundRobinSelector<T> : IResourceSelector<T>
{
private ReadOnlyCollection<T> resources;
private readonly List<T> resources;
private int lastSelection;
public RoundRobinSelector(IEnumerable<T> resources, Random random)
{
// distinct randomly ordered readonly collection
this.resources = new ReadOnlyCollection<T>(resources.Distinct().OrderBy(_ => random.Next()).ToList());
this.resources = resources.Distinct().OrderBy(_ => random.Next()).ToList();
this.lastSelection = random.Next(this.resources.Count);
}

Expand Down
Loading

0 comments on commit 91d23ba

Please sign in to comment.