public async Task <TTenantInfo?> TryGetAsync(string id)
        {
            if (id is null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            return(await Task.FromResult(tenantMap?.Where(kv => kv.Value.Id == id).SingleOrDefault().Value));
        }
Example #2
0
        public static unsafe bool HasDuplicateBranchReferences(Transaction tx, Page start,out long pageNumberWithDuplicates)
        {
            var stack = new Stack<Page>();
            var existingTreeReferences = new ConcurrentDictionary<long, List<long>>();
            stack.Push(start);
            while (stack.Count > 0)
            {
                var currentPage = stack.Pop();
                if (currentPage.IsBranch)
                {
                    for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries; nodeIndex++)
                    {
                        var node = currentPage.GetNode(nodeIndex);

                        existingTreeReferences.AddOrUpdate(currentPage.PageNumber, new List<long> { node->PageNumber },
                            (branchPageNumber, pageNumberReferences) =>
                            {
                                pageNumberReferences.Add(node->PageNumber);
                                return pageNumberReferences;
                            });
                    }

                    for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries; nodeIndex++)
                    {
                        var node = currentPage.GetNode(nodeIndex);
                        if (node->PageNumber < 0 || node->PageNumber > tx.State.NextPageNumber)
                        {
                            throw new InvalidDataException("found invalid reference on branch - tree is corrupted");
                        }

                        var child = tx.GetReadOnlyPage(node->PageNumber);
                        stack.Push(child);
                    }

                }
            }

            Func<long, HashSet<long>> relevantPageReferences =
                branchPageNumber => new HashSet<long>(existingTreeReferences
                    .Where(kvp => kvp.Key != branchPageNumber)
                    .SelectMany(kvp => kvp.Value));

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var branchReferences in existingTreeReferences)
            {
                if (
                    branchReferences.Value.Any(
                        referencePageNumber => relevantPageReferences(branchReferences.Key).Contains(referencePageNumber)))
                {
                    pageNumberWithDuplicates = branchReferences.Key;
                    return true;
                }
            }
            pageNumberWithDuplicates = -1;
            return false;
        }
Example #3
0
        public async Task NizzleName(int numberOfMessages, int numberOfWorkers)
        {
            var activator = new BuiltinHandlerActivator();
            var sentmessageIds = new ConcurrentDictionary<int, int>();
            var receivedMessageIds = new ConcurrentDictionary<int, int>();

            activator.Handle<SomeMessage>(async message =>
            {
                var id = message.Id;
                receivedMessageIds.AddOrUpdate(id, i => 1, (i, existing) => existing + 1);
            });

            var bus = (RebusBus)Configure.With(activator)
                .Logging(l => l.None())
                .Transport(t => t.UseMsmq(InputQueueName))
                .Routing(t => t.TypeBased().Map<SomeMessage>(InputQueueName))
                .Options(o => o.SetNumberOfWorkers(0))
                .Start();

            Using(bus);

            var sendStopwatch = Stopwatch.StartNew();

            Console.WriteLine("Sending {0} messages", numberOfMessages);

            await Task.WhenAll(Enumerable
                .Range(0, numberOfMessages)
                .Select(id => bus.Send(new SomeMessage { Id = id })));

            var elapsedSending = sendStopwatch.Elapsed;

            Console.WriteLine("SENT {0} messages in {1:0.0} s - that's {2:0.0}/s",
                numberOfMessages, elapsedSending.TotalSeconds, numberOfMessages / elapsedSending.TotalSeconds);

            bus.SetNumberOfWorkers(numberOfWorkers);

            var receiveStopwatch = Stopwatch.StartNew();
            Console.WriteLine("Waiting until they have been received");

            while (receivedMessageIds.Count < numberOfMessages)
            {
                Console.WriteLine("got {0} messages so far...", receivedMessageIds.Count);
                await Task.Delay(1000);
            }

            var elapsedReceiving = receiveStopwatch.Elapsed;

            Console.WriteLine("RECEIVED {0} messages in {1:0.0} s - that's {2:0.0}/s", 
                numberOfMessages, elapsedReceiving.TotalSeconds, numberOfMessages/elapsedReceiving.TotalSeconds);

            var sentButNotReceived = sentmessageIds.Keys.Except(receivedMessageIds.Keys).ToList();
            var receivedMoreThanOnce = receivedMessageIds.Where(kvp => kvp.Value > 1).ToList();

            if (sentButNotReceived.Any())
            {
                Assert.Fail("The following IDs were sent but not received: {0}", string.Join(", ", sentButNotReceived));
            }

            if (receivedMoreThanOnce.Any())
            {
                Assert.Fail("The following IDs were received more than once: {0}",
                    string.Join(", ", receivedMoreThanOnce.Select(kvp => string.Format("{0} ({1})", kvp.Key, kvp.Value))));
            }
        }
Example #4
0
 private static IEnumerable <KeyValuePair <string, TimerExecutorItem> > _where(
     this ConcurrentDictionary <string, TimerExecutorItem> store, Func <TimerExecutorItem, bool> predicateWhere)
 {
     return(store?.Where(i => predicateWhere(i.Value)));
 }
Example #5
0
 /// <inheritdoc cref="ICacheManager"/>
 public virtual void RemoveByPattern(string pattern)
 {
     this.RemoveByPattern(pattern, AllKeys.Where(p => p.Value).Select(p => p.Key));
 }
        public sealed override void Initialize(AnalysisContext context)
        {
            context.EnableConcurrentExecution();

            // Security analyzer - analyze and report diagnostics on generated code.
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);

            context.RegisterCompilationStartAction(
                (CompilationStartAnalysisContext compilationStartAnalysisContext) =>
            {
                var compilation = compilationStartAnalysisContext.Compilation;
                var serializableAttributeTypeSymbol = WellKnownTypes.SerializableAttribute(compilation);

                if (serializableAttributeTypeSymbol == null)
                {
                    return;
                }

                var nonSerializedAttribute = WellKnownTypes.NonSerializedAttribute(compilation);

                if (nonSerializedAttribute == null)
                {
                    return;
                }

                var forwardGraph  = new ConcurrentDictionary <ISymbol, ConcurrentDictionary <ISymbol, bool> >();
                var invertedGraph = new ConcurrentDictionary <ISymbol, ConcurrentDictionary <ISymbol, bool> >();

                // It keeps the out Degree of every vertex in the invertedGraph, which is corresponding to the in Degree of the vertex in forwardGraph.
                var inDegree = new ConcurrentDictionary <ISymbol, int>();

                // It Keeps the out degree of every vertex in the forwardGraph, which is corresponding to the in Degree of the vertex in invertedGraph.
                var outDegree = new ConcurrentDictionary <ISymbol, int>();

                compilationStartAnalysisContext.RegisterSymbolAction(
                    (SymbolAnalysisContext symbolAnalysisContext) =>
                {
                    DrawGraph(symbolAnalysisContext.Symbol as ITypeSymbol);
                }, SymbolKind.NamedType);

                compilationStartAnalysisContext.RegisterCompilationEndAction(
                    (CompilationAnalysisContext compilationAnalysisContext) =>
                {
                    ModifyDegree(inDegree, forwardGraph);
                    ModifyDegree(outDegree, invertedGraph);

                    // If the degree of a vertex is greater than 0 both in the forward graph and inverted graph after topological sorting,
                    // the vertex must belong to a loop.
                    var leftVertices         = inDegree.Where(s => s.Value > 0).Select(s => s.Key).ToImmutableHashSet();
                    var invertedLeftVertices = outDegree.Where(s => s.Value > 0).Select(s => s.Key).ToImmutableHashSet();
                    var verticesInLoop       = leftVertices.Intersect(invertedLeftVertices);

                    foreach (var vertex in verticesInLoop)
                    {
                        if (vertex is IFieldSymbol fieldInLoop)
                        {
                            var associatedSymbol = fieldInLoop.AssociatedSymbol;
                            compilationAnalysisContext.ReportDiagnostic(
                                fieldInLoop.CreateDiagnostic(
                                    Rule,
                                    associatedSymbol == null ? vertex.Name : associatedSymbol.Name));
                        }
                    }
                });

                /// <summary>
                /// Traverse from point to its descendants, save the information into a directed graph.
                /// </summary>
                /// <param name="point">The initial point</param>
                void DrawGraph(ITypeSymbol point)
                {
                    // If the point has been visited, return;
                    // otherwise, add it to the graph and mark it as visited.
                    if (!AddPointToBothGraphs(point))
                    {
                        return;
                    }

                    foreach (var associatedTypePoint in GetAssociatedTypes(point))
                    {
                        if (associatedTypePoint == null ||
                            associatedTypePoint.Equals(point))
                        {
                            continue;
                        }

                        AddLineToBothGraphs(point, associatedTypePoint);
                        DrawGraph(associatedTypePoint);
                    }

                    if (point.IsInSource() &&
                        point.HasAttribute(serializableAttributeTypeSymbol))
                    {
                        var fieldPoints = point.GetMembers().OfType <IFieldSymbol>().Where(s => !s.HasAttribute(nonSerializedAttribute) &&
                                                                                           !s.IsStatic);

                        foreach (var fieldPoint in fieldPoints)
                        {
                            var fieldTypePoint = fieldPoint.Type;
                            AddLineToBothGraphs(point, fieldPoint);
                            AddLineToBothGraphs(fieldPoint, fieldTypePoint);
                            DrawGraph(fieldTypePoint);
                        }
                    }
                }

                HashSet <ITypeSymbol> GetAssociatedTypes(ITypeSymbol type)
                {
                    var result = new HashSet <ITypeSymbol>();

                    if (type is INamedTypeSymbol namedTypeSymbol)
                    {
                        // 1. Type arguments of generic type.
                        if (namedTypeSymbol.IsGenericType)
                        {
                            foreach (var arg in namedTypeSymbol.TypeArguments)
                            {
                                result.Add(arg);
                            }
                        }

                        // 2. The type it constructed from.
                        var constructedFrom = namedTypeSymbol.ConstructedFrom;
                        result.Add(constructedFrom);
                    }
                    else if (type is IArrayTypeSymbol arrayTypeSymbol)
                    {
                        // 3. Element type of the array.
                        result.Add(arrayTypeSymbol.ElementType);
                    }

                    // 4. Base type.
                    result.Add(type.BaseType);

                    return(result);
                }

                /// <summary>
                /// Add a line to the graph.
                /// </summary>
                /// <param name="from">The start point of the line</param>
                /// <param name="to">The end point of the line</param>
                /// <param name="degree">The out degree of all vertices in the graph</param>
                /// <param name="graph">The graph</param>
                void AddLine(ISymbol from, ISymbol to, ConcurrentDictionary <ISymbol, int> degree, ConcurrentDictionary <ISymbol, ConcurrentDictionary <ISymbol, bool> > graph)
                {
                    graph.AddOrUpdate(from, new ConcurrentDictionary <ISymbol, bool> {
                        [to] = true
                    }, (k, v) => { v[to] = true; return(v); });
                    degree.AddOrUpdate(from, 1, (k, v) => v + 1);
                }

                /// <summary>
                /// Add a point to the graph.
                /// </summary>
                /// <param name="point">The point to be added</param>
                /// <param name="degree">The out degree of all vertices in the graph</param>
                /// <param name="graph">The graph</param>
                bool AddPoint(ISymbol point, ConcurrentDictionary <ISymbol, int> degree, ConcurrentDictionary <ISymbol, ConcurrentDictionary <ISymbol, bool> > graph)
                {
                    degree.TryAdd(point, 0);
                    return(graph.TryAdd(point, new ConcurrentDictionary <ISymbol, bool>()));
                }

                /// <summary>
                /// Add a line to the forward graph and inverted graph unconditionally.
                /// </summary>
                /// <param name="from">The start point of the line</param>
                /// <param name="to">The end point of the line</param>
                void AddLineToBothGraphs(ISymbol from, ISymbol to)
                {
                    AddLine(from, to, outDegree, forwardGraph);
                    AddLine(to, from, inDegree, invertedGraph);
                }

                /// <summary>
                /// Add a point to the forward graph and inverted graph unconditionally.
                /// </summary>
                /// <param name="point">The point to be added</param>
                /// <returns>
                /// <c>true</c> if <paramref name="point"/> is added to the forward graph successfully;
                /// otherwise <c>false</c>.
                /// </returns>
                bool AddPointToBothGraphs(ISymbol point)
                {
                    AddPoint(point, inDegree, invertedGraph);
                    return(AddPoint(point, outDegree, forwardGraph));
                }

                /// <summary>
                /// According to topological sorting, modify the degree of every vertex in the graph.
                /// </summary>
                /// <param name="degree">The in degree of all vertices in the graph</param>
                /// <param name="graph">The graph</param>
                void ModifyDegree(ConcurrentDictionary <ISymbol, int> degree, ConcurrentDictionary <ISymbol, ConcurrentDictionary <ISymbol, bool> > graph)
                {
                    var stack = new Stack <ISymbol>(degree.Where(s => s.Value == 0).Select(s => s.Key));

                    while (stack.Count != 0)
                    {
                        var start = stack.Pop();
                        degree.AddOrUpdate(start, -1, (k, v) => v - 1);

                        foreach (var vertex in graph[start].Keys)
                        {
                            degree.AddOrUpdate(vertex, -1, (k, v) => v - 1);

                            if (degree[vertex] == 0)
                            {
                                stack.Push(vertex);
                            }
                        }
                    }
                }
            });
        }
 /// <summary>
 /// Gets the specified set of IWebSocketConnections with the same clientIdentifier.
 /// </summary>
 /// <param name="clientIdentifier">The clientIdentifier.</param>
 /// <returns></returns>
 public IWebSocketConnection[] GetConnectionsByClientIdentifier(string clientIdentifier)
 {
     return(_clientMap.Where(x => x.Key.ClientIdentifier == clientIdentifier).Select(x => x.Key).ToArray());
 }
Example #8
0
 /// <summary>
 /// Retrieves all known key/value pairs from the secrets file where the key begins with with <paramref name="prefix"/>.
 /// </summary>
 /// <param name="prefix">A prefix string to filter the list of potential keys retrieved from the source.</param>
 /// <returns>A collection of key/value pairs.</returns>
 public override ICollection <KeyValuePair <string, string> > GetAllValues(string prefix)
 {
     ReadAllValues(DirectoryPath, "", _allValues);
     return(_allValues?.Where(s => s.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList());
 }
 public IEnumerable <StreamingContextRepository <TStreamingService> > AllExcept(TKey exceptKey)
 {
     return(repositories.Where(x => !comparer.Equals(x.Key, exceptKey)).Select(x => x.Value));
 }
Example #10
0
        public WriteObjectReply Write(WriteObjectRequest request)
        {
            Console.WriteLine("Received write with params:");
            Console.WriteLine($"Partition_id: {request.Key.PartitionId}");
            Console.WriteLine($"Object_id: {request.Key.ObjectId}");
            Console.WriteLine($"Value: {request.Value}");

            if (MasteredPartitions.Contains(request.Key.PartitionId))
            {
                lock (WriteGlobalLock)
                {
                    // I'm master of this object's partition
                    // Send request to all other servers of partition
                    ServersByPartition.TryGetValue(request.Key.PartitionId, out List <string> serverIds);

                    if (!KeyValuePairs.TryGetValue(new ObjectKey(request.Key), out ObjectValueManager objectValueManager))
                    {
                        LocalReadWriteLock.AcquireWriterLock(-1);
                        objectValueManager = new ObjectValueManager();
                        KeyValuePairs[new ObjectKey(request.Key)] = objectValueManager;
                        objectValueManager.LockWrite();
                        LocalReadWriteLock.ReleaseWriterLock();
                    }
                    else
                    {
                        objectValueManager.LockWrite();
                    }

                    var connectionCrashedServers = new HashSet <string>();

                    foreach (var server in ServerUrls.Where(x => serverIds.Contains(x.Key) && x.Key != MyId))
                    {
                        var channel = GrpcChannel.ForAddress(server.Value);
                        var client  = new ServerSyncGrpcService.ServerSyncGrpcServiceClient(channel);
                        // What to do if success returns false ?
                        try
                        {
                            client.LockObject(new LockObjectRequest
                            {
                                Key = request.Key
                            });
                        } catch (RpcException e)
                        {
                            // If grpc does no respond, we can assume it has crashed
                            if (e.Status.StatusCode == StatusCode.DeadlineExceeded || e.Status.StatusCode == StatusCode.Unavailable || e.Status.StatusCode == StatusCode.Internal)
                            {
                                // Add to hash Set
                                Console.WriteLine($"Server {server.Key} has crashed");
                                connectionCrashedServers.Add(server.Key);
                            }
                            else
                            {
                                throw e;
                            }
                        }
                    }

                    foreach (var server in ServerUrls.Where(x => serverIds.Contains(x.Key) && x.Key != MyId))
                    {
                        try
                        {
                            var channel = GrpcChannel.ForAddress(server.Value);
                            var client  = new ServerSyncGrpcService.ServerSyncGrpcServiceClient(channel);
                            // What to do if success returns false ?
                            client.ReleaseObjectLock(new ReleaseObjectLockRequest
                            {
                                Key   = request.Key,
                                Value = request.Value
                            });
                        }
                        catch (RpcException e)
                        {
                            if (e.Status.StatusCode == StatusCode.DeadlineExceeded || e.Status.StatusCode == StatusCode.Unavailable || e.Status.StatusCode == StatusCode.Internal)
                            {
                                // Add to hash Set
                                Console.WriteLine($"Server {server.Key} has crashed");
                                connectionCrashedServers.Add(server.Key);
                            }
                            else
                            {
                                throw e;
                            }
                        }
                    }


                    if (connectionCrashedServers.Any())
                    {
                        // Update the crashed servers
                        UpdateCrashedServers(request.Key.PartitionId, connectionCrashedServers);

                        // Contact Partition slaves an update their view of the partition
                        foreach (var server in ServerUrls.Where(x => serverIds.Contains(x.Key) && x.Key != MyId))
                        {
                            var channel = GrpcChannel.ForAddress(server.Value);
                            var client  = new ServerSyncGrpcService.ServerSyncGrpcServiceClient(channel);

                            client.RemoveCrashedServers(new RemoveCrashedServersRequest
                            {
                                PartitionId = request.Key.PartitionId,
                                ServerIds   = { connectionCrashedServers }
                            });
                        }
                    }

                    objectValueManager.UnlockWrite(request.Value);

                    return(new WriteObjectReply
                    {
                        Ok = true
                    });
                }
            }
            else
            {
                // Tell him I'm not the master
                throw new RpcException(new Status(StatusCode.PermissionDenied, $"Server {MyId} is not the master of partition {request.Key.PartitionId}"));
            }
        }
Example #11
0
        private async Task FinishSession()
        {
            try
            {
                if (Result > 0)
                {
                    return;
                }
                Result = RandomUtil.NextInt(2) + RandomUtil.NextInt(2) + RandomUtil.NextInt(2) + RandomUtil.NextInt(2);
                _logic.CalculateResult(Result, SessionId);
                string data = _logic.GetQuery();
                if (string.IsNullOrEmpty(data))
                {
                    return;
                }
                History.Enqueue(Result);
                if (History.Count > 50)
                {
                    History.TryDequeue(out var r);
                }
                await GameDAO.ExecuteAsync(data);

                NLogManager.LogMessage("FINISH SESION: => " + data);
                var           rewards  = _logic.GetReward();
                var           listSit  = Sitting.Values.ToList();
                List <object> sittings = new List <object>();
                foreach (var re in rewards)
                {
                    Player p = GetPlayer(re.Key);

                    if (p != null)
                    {
                        long totalLose   = re.Value.Sum(x => x.Lose);
                        long totalwin    = re.Value.Sum(x => x.Prize);
                        long totalrefund = re.Value.Sum(x => x.Refund);
                        if (re.Key == Banker)
                        {
                            p.IncreaseBalance(totalwin - totalLose, MoneyType);
                        }
                        else
                        {
                            p.IncreaseBalance(totalwin + totalrefund, MoneyType);
                        }
                        if (listSit.Exists(x => x?.AccountId == re.Key))
                        {
                            sittings.Add(new
                            {
                                AccountId   = p.AccountId,
                                Balance     = MoneyType == MoneyType.GOLD ? p.Gold : p.Coin,
                                TotalLose   = totalLose,
                                TotalWin    = totalwin,
                                TotalRefund = re.Key == Banker ? 0 : totalrefund,
                                TotalPrize  = totalwin
                            });
                        }
                    }
                }

                foreach (var re in rewards)
                {
                    Player p = GetPlayer(re.Key);

                    if (p != null)
                    {
                        long balance       = MoneyType == MoneyType.GOLD ? p.Gold : p.Coin;
                        var  lstConnection = _connectionHandler.GetConnections(p.AccountId);
                        _hubContext.Clients.Clients(lstConnection.ToList()).showResult(Result, new
                        {
                            winLose = re.Value,
                            balance = balance
                        }, sittings);
                    }
                }

                var player = _players.Where(x => !rewards.ContainsKey(x.Key));

                foreach (var re in player)
                {
                    long balance       = MoneyType == MoneyType.GOLD ? re.Value.Gold : re.Value.Coin;
                    var  lstConnection = _connectionHandler.GetConnections(re.Value.AccountId);
                    _hubContext.Clients.Clients(lstConnection.ToList()).showResult(Result, new
                    {
                        winLose = new List <Reward>(),
                        balance = balance
                    }, sittings);
                }
            }
            catch (Exception ex)
            {
                NLogManager.PublishException(ex);
            }
        }
Example #12
0
        /// <summary>
        /// A long running method that monitors and processes a list of transactions that need to send a reliable
        /// request or response.
        /// </summary>
        private void ProcessPendingTransactions()
        {
            Thread.CurrentThread.Name = TXENGINE_THREAD_NAME;

            try
            {
                while (!m_isClosed)
                {
                    if (m_pendingTransactions.IsEmpty)
                    {
                        Thread.Sleep(MAX_TXCHECK_WAIT_MILLISECONDS);
                    }
                    else
                    {
                        foreach (var(_, transaction) in m_pendingTransactions.Where(x => x.Value.DeliveryPending))
                        {
                            try
                            {
                                if (transaction.TransactionState == SIPTransactionStatesEnum.Terminated ||
                                    transaction.TransactionState == SIPTransactionStatesEnum.Confirmed ||
                                    transaction.HasTimedOut)
                                {
                                    transaction.DeliveryPending = false;
                                }
                                else if (transaction.HasDeliveryExpired(m_t6))
                                {
                                    if (transaction.TransactionState == SIPTransactionStatesEnum.Proceeding)
                                    {
                                        // If the transaction is a UAS and still in the progress state then the timeout was
                                        // for a provisional response and it should not set any transaction properties that
                                        // will affect the delivery of any subsequent final response.
                                        transaction.OnTimedOutProvisionalResponse();
                                    }
                                    else
                                    {
                                        transaction.Expire(DateTime.Now);
                                    }
                                }
                                else
                                {
                                    if (transaction.DeliveryPending && transaction.IsRetransmitDue(m_t1, m_t2))
                                    {
                                        SocketError sendResult = SocketError.Success;

                                        switch (transaction.TransactionType)
                                        {
                                        case SIPTransactionTypesEnum.InviteServer:

                                            switch (transaction.TransactionState)
                                            {
                                            case SIPTransactionStatesEnum.Calling:
                                                break;

                                            case SIPTransactionStatesEnum.Trying:
                                                break;

                                            case SIPTransactionStatesEnum.Proceeding:
                                                if (transaction.ReliableProvisionalResponse != null)
                                                {
                                                    sendResult = SendTransactionProvisionalResponse(transaction)
                                                                 .Result;
                                                }

                                                break;

                                            case SIPTransactionStatesEnum.Completed:
                                                sendResult = SendTransactionFinalResponse(transaction).Result;
                                                break;

                                            case SIPTransactionStatesEnum.Confirmed:
                                                transaction.DeliveryPending = false;
                                                break;

                                            case SIPTransactionStatesEnum.Cancelled:
                                                sendResult = SendTransactionFinalResponse(transaction).Result;
                                                break;

                                            default:
                                                logger.LogWarning(
                                                    $"InviteServer Transaction entered an unexpected transaction state {transaction.TransactionState}.");
                                                transaction.DeliveryFailed = true;
                                                break;
                                            }

                                            break;

                                        case SIPTransactionTypesEnum.InviteClient:

                                            switch (transaction.TransactionState)
                                            {
                                            case SIPTransactionStatesEnum.Calling:
                                                sendResult = SendTransactionRequest(transaction).Result;
                                                break;

                                            case SIPTransactionStatesEnum.Trying:
                                                break;

                                            case SIPTransactionStatesEnum.Proceeding:
                                                transaction.DeliveryPending = false;
                                                break;

                                            case SIPTransactionStatesEnum.Completed:
                                                transaction.DeliveryPending = false;
                                                break;

                                            case SIPTransactionStatesEnum.Confirmed:
                                                transaction.DeliveryPending = false;
                                                break;

                                            case SIPTransactionStatesEnum.Cancelled:
                                                transaction.DeliveryPending = false;
                                                break;

                                            default:
                                                logger.LogWarning(
                                                    $"InviteClient Transaction entered an unexpected transaction state {transaction.TransactionState}.");
                                                transaction.DeliveryFailed = true;
                                                break;
                                            }

                                            break;

                                        case SIPTransactionTypesEnum.NonInvite:

                                            switch (transaction.TransactionState)
                                            {
                                            case SIPTransactionStatesEnum.Calling:
                                                sendResult = SendTransactionRequest(transaction).Result;
                                                break;

                                            case SIPTransactionStatesEnum.Trying:
                                                break;

                                            case SIPTransactionStatesEnum.Proceeding:
                                                break;

                                            case SIPTransactionStatesEnum.Completed:
                                                if (transaction.TransactionFinalResponse != null)
                                                {
                                                    // Sending a single final response on a non-INVITE tx. The same response
                                                    // will be automatically resent if the same request is received.
                                                    sendResult = m_sipTransport
                                                                 .SendResponseAsync(transaction.TransactionFinalResponse)
                                                                 .Result;
                                                    transaction.DeliveryPending = false;
                                                }

                                                break;

                                            case SIPTransactionStatesEnum.Confirmed:
                                                transaction.DeliveryPending = false;
                                                break;

                                            default:
                                                logger.LogWarning(
                                                    $"NonInvite Transaction entered an unexpected transaction state {transaction.TransactionState}.");
                                                transaction.DeliveryFailed = true;
                                                break;
                                            }

                                            break;

                                        default:
                                            logger.LogWarning(
                                                $"Unrecognised transaction type {transaction.TransactionType}.");
                                            break;
                                        }

                                        if (sendResult != SocketError.Success)
                                        {
                                            // Example of failures here are requiring a specific TCP or TLS connection that no longer exists
                                            // or attempting to send to a UDP socket that has previously returned an ICMP error.
                                            transaction.DeliveryPending = false;
                                            transaction.DeliveryFailed  = true;
                                            transaction.TimedOutAt      = DateTime.Now;
                                            transaction.FireTransactionTimedOut();
                                        }
                                    }
                                }
                            }
                            catch (Exception excp)
                            {
                                logger.LogError($"Exception processing pending transactions. {excp.Message}");
                            }
                        }

                        RemoveExpiredTransactions();

                        Thread.Sleep(TXCHECK_WAIT_MILLISECONDS);
                    }
                }
            }
            catch (Exception excp)
            {
                logger.LogError("Exception SIPTransactionEngine ProcessPendingTransactions. " + excp.Message);
            }
        }
Example #13
0
        public void ApplyCommand(BaseCommand command)
        {
            try
            {
                switch (command)
                {
                case UpsertNodeInformation t1:
                    if (Nodes.ContainsKey(t1.Id))
                    {
                        Nodes[t1.Id] = new NodeInformation()
                        {
                            Name             = t1.Name,
                            TransportAddress = t1.TransportAddress,
                            Id            = t1.Id,
                            IsContactable = t1.IsContactable
                        };
                    }
                    else
                    {
                        //Only add if it is marking as contactable
                        if (t1.IsContactable)
                        {
                            var nodes = Nodes.Where(n => n.Value.TransportAddress == t1.TransportAddress);
                            if (nodes.Count() > 0)
                            {
                                foreach (var node in nodes)
                                {
                                    Nodes.Remove(node.Key, out _);
                                }
                            }
                            Nodes.TryAdd(t1.Id, new NodeInformation()
                            {
                                Name             = t1.Name,
                                TransportAddress = t1.TransportAddress,
                                Id            = t1.Id,
                                IsContactable = t1.IsContactable
                            });
                        }
                    }
                    break;

                case DeleteNodeInformation t1:
                    if (Nodes.ContainsKey(t1.Id))
                    {
                        Nodes.TryRemove(t1.Id, out _);
                    }
                    break;

                case CreateIndex t1:
                    Indexes.TryAdd(t1.Type, new Index()
                    {
                        Shards = t1.Shards.Select(s => s.DeepCopy()).ToList(),
                        Type   = t1.Type
                    });
                    break;

                case UpdateClusterTasks t1:
                    if (t1.TasksToAdd != null)
                    {
                        foreach (var task in t1.TasksToAdd)
                        {
                            if (GetRunningTask(task.UniqueRunningId) == null)
                            {
                                if (!ClusterTasks.TryAdd(task.Id, SystemExtension.Clone(task)))
                                {
                                    //Can't add a task twice
                                    if (ClusterTasks.ContainsKey(task.Id))
                                    {
                                        if (_logger == null)
                                        {
                                            Console.WriteLine("Critical error while trying to add cluster task " + task.Id + " the id already exists as the object " + JsonConvert.SerializeObject(ClusterTasks[task.Id], Formatting.Indented));
                                        }
                                        else
                                        {
                                            _logger.LogDebug("Critical error while trying to add cluster task " + task.Id + " the id already exists as the object " + JsonConvert.SerializeObject(ClusterTasks[task.Id], Formatting.Indented));
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception("Critical error while trying to add cluster task " + task.Id);
                                    }
                                }
                            }
                            else
                            {
                                if (_logger == null)
                                {
                                    Console.WriteLine("The task already exists and is running. Skipping addition of task " + task.Id);
                                }
                                else
                                {
                                    _logger.LogInformation("The task already exists and is running. Skipping addition of task " + task.Id);
                                }
                            }
                        }
                    }
                    if (t1.TasksToRemove != null)
                    {
                        foreach (var task in t1.TasksToRemove)
                        {
                            if (!ClusterTasks.TryRemove(task, out _))
                            {
                                throw new Exception("Critical error while trying to remove cluster task " + task);
                            }
                        }
                    }
                    if (t1.TasksToUpdate != null)
                    {
                        foreach (var task in t1.TasksToUpdate)
                        {
                            if (ClusterTasks.ContainsKey(task.TaskId))
                            {
                                ClusterTasks[task.TaskId].CompletedOn  = task.CompletedOn;
                                ClusterTasks[task.TaskId].Status       = task.Status;
                                ClusterTasks[task.TaskId].ErrorMessage = task.ErrorMessage;
                            }
                            else
                            {
                                if (_logger == null)
                                {
                                    Console.WriteLine("Critical error while trying to update cluster task " + task.TaskId + " task is not present in dictionary.");
                                }
                                else
                                {
                                    _logger.LogInformation("Critical error while trying to update cluster task " + task.TaskId + " task is not present in dictionary.");
                                }
                            }
                        }
                    }
                    break;

                case UpdateShardMetadataAllocations t1:
                    if (t1.InsyncAllocationsToAdd != null)
                    {
                        var newList = new HashSet <Guid>();
                        Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().InsyncAllocations.ToList().ForEach(ia => newList.Add(ia));
                        foreach (var allocation in t1.InsyncAllocationsToAdd)
                        {
                            //Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().InsyncAllocations = newList;
                            newList.Add(allocation);
                        }
                        Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().InsyncAllocations = new HashSet <Guid>(newList);
                        // Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().StaleAllocations = Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().StaleAllocations.Where(sa => !t1.InsyncAllocationsToAdd.Contains(sa)).ToHashSet();
                    }
                    if (t1.InsyncAllocationsToRemove != null)
                    {
                        Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().InsyncAllocations = new HashSet <Guid>(Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().InsyncAllocations.Where(ia => !t1.InsyncAllocationsToRemove.Contains(ia)));
                    }
                    if (t1.StaleAllocationsToAdd != null)
                    {
                        var newList = new HashSet <Guid>();
                        Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().StaleAllocations.ToList().ForEach(ia => newList.Add(ia));
                        foreach (var allocation in t1.StaleAllocationsToAdd)
                        {
                            newList.Add(allocation);
                        }
                        Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().StaleAllocations = new HashSet <Guid>(newList);
                    }
                    if (t1.StaleAllocationsToRemove != null)
                    {
                        var newList = new HashSet <Guid>();
                        Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().StaleAllocations.Where(sa => !t1.StaleAllocationsToRemove.Contains(sa)).ToList().ForEach(ia => newList.Add(ia));
                        Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().StaleAllocations = new HashSet <Guid>(newList);
                    }

                    if (t1.PrimaryAllocation != null)
                    {
                        Indexes[t1.Type].Shards.Where(s => s.Id == t1.ShardId).FirstOrDefault().PrimaryAllocation = t1.PrimaryAllocation.Value;
                    }
                    break;

                case SetLock t1:
                    var stringLockResult = Locks.TryAdd(t1.Name, new Lock()
                    {
                        LockTimeoutMs = t1.TimeoutMs,
                        Name          = t1.Name,
                        LockId        = t1.LockId,
                        CreatedOn     = t1.CreatedOn
                    });
                    if (!stringLockResult)
                    {
                        throw new ConflictingObjectLockException("String " + t1.Name + " is already locked.");
                    }
                    break;

                case RemoveLock t1:
                    string lockName = t1.Name;
                    var    objectLocksWithLockId = Locks.Where(ob => ob.Value.LockId == t1.LockId).Select(t => t.Value);
                    if (objectLocksWithLockId.Count() == 0)
                    {
                        throw new ConflictingObjectLockException("No lock with id " + t1.LockId);
                    }

                    Lock existingStringLock;
                    var  removeStringLockResult = Locks.TryRemove(t1.Name, out existingStringLock);
                    if (!removeStringLockResult)
                    {
                        throw new ConflictingObjectLockException("String Lock " + t1.Name + " did not exist in locks.");
                    }
                    break;

                default:
                    ApplyCommandToState(command);
                    break;
                }
            }
            catch (Exception e)
            {
                throw new Exception("Failed to apply command " + command.CommandName + " to state with exception \"" + e.Message + "\"." + Environment.NewLine + JsonConvert.SerializeObject(command, Formatting.Indented));
            }
        }
Example #14
0
        public void WhenInboundHasProblemsOutboundJustKeepsOnTrucking(int numberOfMessages)
        {
            // arrange
            var receivedMessageCount = 0;
            var messageTracker       = new ConcurrentDictionary <Guid, int>();
            var resetEvent           = new ManualResetEvent(false);

            orderSystemHandlerActivator.Handle <PlaceOrderRequest>(req =>
            {
                if (req.What != "beer" || req.HowMuch != 12)
                {
                    return;
                }

                OnCommit.Do(() =>
                {
                    messageTracker.AddOrUpdate(req.MsgId, 1, (id, count) => count + 1);

                    var newValue = Interlocked.Increment(ref receivedMessageCount);
                    if (newValue >= numberOfMessages)
                    {
                        resetEvent.Set();
                    }
                });
            });
            var timeout         = numberOfMessages.Seconds();
            var keepMakingChaos = true;

            var chaosMonkey = new Thread(() =>
            {
                while (keepMakingChaos)
                {
                    Thread.Sleep(0.1.Seconds());
                    inbound.Stop();
                    Console.WriteLine("Inbound stopped - {0} messages processed...", receivedMessageCount);
                    Thread.Sleep(0.2.Seconds());
                    inbound.Start();
                    Thread.Sleep(2.2331.Seconds());
                }
            });

            // act
            chaosMonkey.Start();
            numberOfMessages.Times(() => pricedesk.Send(CreateMessage()));

            // assert
            var resetEventWasSet = resetEvent.WaitOne(timeout + 5.Seconds());

            keepMakingChaos = false;
            chaosMonkey.Join();

            // chill, be more sure to empty the queue completely
            Thread.Sleep(1.Seconds());

            Assert.That(resetEventWasSet, Is.True, "Request was not received in order system within timeout of {0}", timeout);
            receivedMessageCount.ShouldBeGreaterThanOrEqualTo(numberOfMessages);
            Console.WriteLine("Actual number of received messages: {0}", receivedMessageCount);
            if (messageTracker.Any(t => t.Value > 1))
            {
                Console.WriteLine(@"The following IDs were received more than once:
{0}", string.Join(Environment.NewLine, messageTracker.Where(t => t.Value > 1).Select(kvp => "    " + kvp.Key + ": " + kvp.Value)));
            }
            messageTracker.Count.ShouldBe(numberOfMessages);
        }
Example #15
0
        /// <summary>
        /// Check if it's possible move readable pages to free list - if not possible, extend memory
        /// </summary>
        private void Extend()
        {
            // count how many pages in cache are available to be re-used (is not in use at this time)
            var emptyShareCounter = _readable.Values.Count(x => x.ShareCounter == 0);

            // get segmentSize
            var segmentSize = _segmentSizes[Math.Min(_segmentSizes.Length - 1, _extends)];

            // if this count is larger than MEMORY_SEGMENT_SIZE, re-use all this pages
            if (emptyShareCounter > segmentSize)
            {
                // get all readable pages that can return to _free (slow way)
                // sort by timestamp used (set as free oldest first)
                var readables = _readable
                                .Where(x => x.Value.ShareCounter == 0)
                                .OrderBy(x => x.Value.Timestamp)
                                .Select(x => x.Key)
                                .Take(segmentSize)
                                .ToArray();

                // move pages from readable list to free list
                foreach (var key in readables)
                {
                    var removed = _readable.TryRemove(key, out var page);

                    ENSURE(removed, "page should be in readable list before moving to free list");

                    // if removed page was changed between make array and now, must add back to readable list
                    if (page.ShareCounter > 0)
                    {
                        // but wait: between last "remove" and now, another thread can added this page
                        if (!_readable.TryAdd(key, page))
                        {
                            // this is a terrible situation, to avoid memory corruption I will throw expcetion for now
                            throw new LiteException(0, "MemoryCache: removed in-use memory page. This situation has no way to fix (yet). Throwing exception to avoid database corruption. No other thread can read/write from database now.");
                        }
                    }
                    else
                    {
                        ENSURE(page.ShareCounter == 0, "page should not be in use by anyone");

                        // clean controls
                        page.Position = long.MaxValue;
                        page.Origin   = FileOrigin.None;

                        _free.Enqueue(page);
                    }
                }

                LOG($"re-using cache pages (flushing {_free.Count} pages)", "CACHE");
            }
            else
            {
                // create big linear array in heap memory (LOH => 85Kb)
                var buffer   = new byte[PAGE_SIZE * segmentSize];
                var uniqueID = this.ExtendPages + 1;

                // split linear array into many array slices
                for (var i = 0; i < segmentSize; i++)
                {
                    _free.Enqueue(new PageBuffer(buffer, i * PAGE_SIZE, uniqueID++));
                }

                _extends++;

                LOG($"extending memory usage: (segments: {_extends})", "CACHE");
            }
        }
        private bool DoWorkFrom(
            ConcurrentDictionary<ITransferController, object> activeItems)
        {
            // Filter items with work only.
            List<KeyValuePair<ITransferController, object>> activeItemsWithWork =
                new List<KeyValuePair<ITransferController, object>>(
                    activeItems.Where(item => item.Key.HasWork && !item.Key.IsFinished));

            if (0 != activeItemsWithWork.Count)
            {
                // Select random item and get work delegate.
                int idx = this.randomGenerator.Next(activeItemsWithWork.Count);
                ITransferController transferController = activeItemsWithWork[idx].Key;

                DoControllerWork(transferController);

                return true;
            }

            return false;
        }
Example #17
0
		private static HashSet<uint> GetGamesToFarmSolo(ConcurrentDictionary<uint, float> gamesToFarm) {
			if (gamesToFarm == null) {
				Logging.LogNullError(nameof(gamesToFarm));
				return null;
			}

			HashSet<uint> result = new HashSet<uint>();
			foreach (KeyValuePair<uint, float> keyValue in gamesToFarm.Where(keyValue => keyValue.Value >= 2)) {
				result.Add(keyValue.Key);
			}

			return result;
		}
Example #18
0
        /// <summary>
        /// Does not attempt to retrieve any data
        /// </summary>
        public IStreamReader Fetch(Symbol symbol, SubscriptionDataSource source, DateTime date, Resolution resolution, TickType tickType)
        {
            string entryName = null; // default to all entries
            var    filename  = source.Source;
            var    hashIndex = source.Source.LastIndexOf("#", StringComparison.Ordinal);

            if (hashIndex != -1)
            {
                entryName = source.Source.Substring(hashIndex + 1);
                filename  = source.Source.Substring(0, hashIndex);
            }

            if (!File.Exists(filename))
            {
                return(null);
            }

            // handles zip files
            if (filename.GetExtension() == ".zip")
            {
                IStreamReader reader = null;

                try
                {
                    // cleaning the outdated cache items
                    if (_lastDate == DateTime.MinValue || _lastDate < date.Date)
                    {
                        // clean all items that that are older than _cachePeriodBars bars than the current date
                        foreach (var zip in _zipFileCache.Where(x => x.Value.Value.Item1 < date.Date.AddDays(-CachePeriodBars)))
                        {
                            // removing it from the cache
                            Lazy <CacheEntry> removed;
                            if (_zipFileCache.TryRemove(zip.Key, out removed))
                            {
                                // disposing zip archive
                                removed.Value.Item2.Dispose();
                            }
                        }

                        _lastDate = date.Date;
                    }

                    _zipFileCache.AddOrUpdate(filename,
                                              x =>
                    {
                        var newItem = Tuple.Create(date.Date, new ZipFile(filename));
                        reader      = new LocalFileSubscriptionStreamReader(newItem.Item2, entryName);
                        return(newItem);
                    },
                                              (x, existingEntry) =>
                    {
                        reader = new LocalFileSubscriptionStreamReader(existingEntry.Item2, entryName);
                        return(existingEntry);
                    });

                    return(reader);
                }
                catch (Exception err)
                {
                    Log.Error(err, "Inner try/catch");
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    return(null);
                }
            }
            else
            {
                // handles text files
                return(new LocalFileSubscriptionStreamReader(filename, entryName));
            }
        }
        private IEnumerable <ShellContext> GetShellsToRun(IEnumerable <ShellContext> shells)
        {
            var tenantsToRun = _schedulers.Where(s => s.Value.CanRun()).Select(s => s.Value.Tenant).Distinct().ToArray();

            return(shells.Where(s => tenantsToRun.Contains(s.Settings.Name)).ToArray());
        }
Example #20
0
        public Engine CheckoutScript(Func <ScriptedPatchRequest, Engine> createEngine, ScriptedPatchRequest request, RavenJObject customFunctions)
        {
            CachedResult value;
            var          patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);

            if (cacheDic.TryGetValue(patchRequestAndCustomFunctionsTuple, out value))
            {
                Interlocked.Increment(ref value.Usage);
                Engine context;
                if (value.Queue.TryDequeue(out context))
                {
                    return(context);
                }
            }
            var result = createEngine(request);

            RavenJToken functions;

            if (customFunctions != null && customFunctions.TryGetValue("Functions", out functions))
            {
                result.Execute(string.Format(@"var customFunctions = function() {{  var exports = {{ }}; {0};
                            return exports;
                        }}();
                        for(var customFunction in customFunctions) {{
                            this[customFunction] = customFunctions[customFunction];
                        }};", functions), new ParserOptions {
                    Source = "customFunctions.js"
                });
            }
            var cachedResult = new CachedResult
            {
                Usage     = 1,
                Queue     = new ConcurrentQueue <Engine>(),
                Timestamp = SystemTime.UtcNow
            };

            cacheDic.AddOrUpdate(patchRequestAndCustomFunctionsTuple, cachedResult, (_, existing) =>
            {
                Interlocked.Increment(ref existing.Usage);
                return(existing);
            });
            if (cacheDic.Count > CacheMaxSize)
            {
                foreach (var source in cacheDic
                         .Where(x => x.Value != null)
                         .OrderByDescending(x => x.Value.Usage)
                         .ThenBy(x => x.Value.Timestamp)
                         .Skip(CacheMaxSize - CacheMaxSize / 10))
                {
                    if (Equals(source.Key, request))
                    {
                        continue; // we don't want to remove the one we just added
                    }
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
                foreach (var source in cacheDic.Where(x => x.Value == null))
                {
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
            }

            return(result);
        }
        /// <summary>
        /// Processes all synchronous events that must take place before the next time loop for the algorithm
        /// </summary>
        public virtual void ProcessSynchronousEvents()
        {
            // how to do synchronous market orders for real brokerages?

            // in backtesting we need to wait for orders to be removed from the queue and finished processing
            if (!_algorithm.LiveMode)
            {
                if (_orderRequestQueue.IsBusy && !_orderRequestQueue.WaitHandle.WaitOne(Time.OneSecond, _cancellationTokenSource.Token))
                {
                    Log.Error("BrokerageTransactionHandler.ProcessSynchronousEvents(): Timed out waiting for request queue to finish processing.");
                }
                return;
            }

            Log.Debug("BrokerageTransactionHandler.ProcessSynchronousEvents(): Enter");

            // every morning flip this switch back
            var currentTimeNewYork = DateTime.UtcNow.ConvertFromUtc(TimeZones.NewYork);

            if (_syncedLiveBrokerageCashToday && currentTimeNewYork.Date != LastSyncDate)
            {
                _syncedLiveBrokerageCashToday = false;
            }

            // we want to sync up our cash balance before market open
            if (_algorithm.LiveMode && !_syncedLiveBrokerageCashToday && currentTimeNewYork.TimeOfDay >= LiveBrokerageCashSyncTime)
            {
                try
                {
                    // only perform cash syncs if we haven't had a fill for at least 10 seconds
                    if (TimeSinceLastFill > TimeSpan.FromSeconds(10))
                    {
                        PerformCashSync();
                    }
                }
                catch (Exception err)
                {
                    Log.Error(err, "Updating cash balances");
                }
            }

            // we want to remove orders older than 10k records, but only in live mode
            const int maxOrdersToKeep = 10000;

            if (_orders.Count < maxOrdersToKeep + 1)
            {
                Log.Debug("BrokerageTransactionHandler.ProcessSynchronousEvents(): Exit");
                return;
            }

            int max = _orders.Max(x => x.Key);
            int lowestOrderIdToKeep = max - maxOrdersToKeep;

            foreach (var item in _orders.Where(x => x.Key <= lowestOrderIdToKeep))
            {
                Order       value;
                OrderTicket ticket;
                _orders.TryRemove(item.Key, out value);
                _orderTickets.TryRemove(item.Key, out ticket);
            }

            Log.Debug("BrokerageTransactionHandler.ProcessSynchronousEvents(): Exit");
        }
 private void RaiseProcessStatusesChangedEvent(List <ProcessStatus> processStatuses)
 {
     ProcessStatusesChanged?.Invoke(this, new ProcessStatusesEventArgs(processStatuses, _processStatusListeners.Where(x => x.Value).Select(x => x.Key).ToArray()));
 }
Example #23
0
        private void FrmMain_Load(object sender, EventArgs e)
        {
            tsOnline.Text = "没有在线设备";
            uid           = Guid.NewGuid().ToString("N");
            InitListView();
            //测试地址
            //txtAddress.Text = "183.62.237.211:18831";
            ActConnetct = MonitorConnect;

            #region 清理多包缓存
            //定时清理缓存
            thCache = new Thread(() =>
            {
                while (true)
                {
                    if (dicPage.Count > 0)
                    {
                        foreach (var item in dicPage)
                        {
                            TimeSpan ts = DateTime.Now - item.Value.dt;
                            if (ts.TotalMinutes > 3)
                            {
                                lock (Pagelocker)
                                {
                                    CacheData cd;
                                    dicPage.TryRemove(item.Key, out cd);
                                    AppLog.Warn($"清除设备编号为{cd.DeviceNo}的缓存包,包内容为->{cd.ToString()}");
                                    this.statusStrip1.Invoke(new MethodInvoker(() =>
                                    {
                                        tsl.Text = $"清理一条缓存,还有{dicPage.Count}条缓存";
                                    })
                                                             );
                                }
                            }
                        }
                    }
                    else
                    {
                        this.statusStrip1.Invoke((MethodInvoker) delegate()
                        {
                            tsl.Text = "没有缓存数据";
                        });
                    }
                    Thread.Sleep(new TimeSpan(0, 3, 0));
                }
            });
            thCache.Start();
            #endregion

            #region 处理在线数据
            thOnLine = new Thread(() =>
            {
                while (true)
                {
                    if (dicOnLine.Count > 0)
                    {
                        lock (OnlineLocker)
                        {
                            foreach (var item in dicOnLine.Where(a => a.Value.IsWill == false))//只处理没有遗言的设备
                            {
                                TimeSpan ts = DateTime.Now - item.Value.Dt;
                                if (ts.TotalMinutes > 10)//如果10分钟没有设备上传数据,则判定改设备下线
                                {
                                    OnlineData data;
                                    dicOnLine.TryRemove(item.Key, out data);
                                    //更改设备在线状态
                                    HandleDeviceOnlineData(item.Key, false, data.Token, data.DeviceSn);

                                    //从设备缓存中剔除出已掉线的设备(目的是更新设备缓存列表,处理设备no和sn不一致的问题)
                                    //DicDevice.Remove(item.Key);
                                    SetOnlineData();
                                }
                            }
                        }
                    }
                    Thread.Sleep(new TimeSpan(0, 3, 0));//每隔10分钟清理一次在线数据
                }
            });
            thOnLine.Start();
            #endregion
        }
Example #24
0
 private string GetConnectionDispatcherChannel(string connectionId)
 {
     return(DispatcherConnections.Where(kvp => kvp.Value == connectionId)
            .Select(kvp => new { kvp.Key, kvp.Value })
            .FirstOrDefault()?.Key);
 }
Example #25
0
        void RunHost()
        {
            try
            {
                while (!Terminated)
                {
                    var listener = new TcpListener(RouterContext.Inst.LocalInterface, DefaultI2CPPort);
                    listener.Start();

                    try
                    {
                        listener.BeginAcceptTcpClient(HandleListenerAsyncCallback, listener);

                        while (!Terminated)
                        {
                            Thread.Sleep(10000);

                            var stuck = Sessions.Where(s =>
                                                       s.Value.LastReception.DeltaToNow > TickSpan.Minutes(30));

                            foreach (var one in stuck)
                            {
                                try
                                {
                                    Logging.LogWarning($"{this}: Terminating stuck session {one} {one.Value.LastReception}");
                                    one.Value.Terminate();
                                }
                                catch (Exception ex)
                                {
                                    Logging.Log(ex);
                                }
                            }

                            var terminated = Sessions.Where(s =>
                                                            s.Value.CurrentState is null);

                            foreach (var one in terminated)
                            {
                                try
                                {
                                    Logging.LogInformation($"{this}: Removing terminated session {one}");
                                    Sessions.TryRemove(one.Key, out _);
                                }
                                catch (Exception ex)
                                {
                                    Logging.Log(ex);
                                }
                            }
                        }
                    }
                    catch (ThreadAbortException ex)
                    {
                        Logging.Log(ex);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log(ex);
                    }
                    finally
                    {
                        listener.Stop();
                    }
                }
            }
            finally
            {
                Terminated = true;
                Worker     = null;
            }
        }
 private bool IsMiscellaneousDocument(DocumentId documentId)
 {
     return(miscDocumentsProjectInfos.Where(p => p.Value.Id == documentId.ProjectId).Any());
 }
 public override ICollection <KeyValuePair <string, string> > GetAllValues(string prefix)
 {
     return(_secrets?.Where(s => s.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList());
 }
Example #28
0
 public IEnumerable <KeyValuePair <string, string> > GetRange(string keyPrefix)
 {
     return(_dictionary.Where(keyValuePair => keyValuePair.Key.StartsWith(keyPrefix)));
 }
 private static HashSet<QueueId> GetQueuesOfImmatureSilos(ISiloStatusOracle siloStatusOracle, 
     ConcurrentDictionary<SiloAddress, bool> immatureSilos, 
     Dictionary<string, List<QueueId>> idealDistribution)
 {
     HashSet<QueueId> queuesOfImmatureSilos = new HashSet<QueueId>();
     foreach (var silo in immatureSilos.Where(s => s.Value)) // take only those from immature set that have their immature status bit set
     {
         string siloName;
         if (siloStatusOracle.TryGetSiloName(silo.Key, out siloName))
         {
             List<QueueId> queues;
             if (idealDistribution.TryGetValue(siloName, out queues))
             {
                 queuesOfImmatureSilos.UnionWith(queues);
             }
         }
     }
     return queuesOfImmatureSilos;
 }
Example #30
0
 public Task <IEnumerable <string> > GetFollowing(string userHandle)
 {
     return(Task.FromResult(HandleToFollower.Where(x => x.Value.ContainsKey(userHandle)).Select(x => x.Key) ?? Enumerable.Empty <string>()));
 }
        public async Task LotsOfAsyncStuffGoingDown(int numberOfMessages)
        {
            var receivedMessages = 0;
            var messageIds = new ConcurrentDictionary<int, int>();

            Console.WriteLine("Sending {0} messages", numberOfMessages);

            await Task.WhenAll(Enumerable.Range(0, numberOfMessages)
                .Select(async i =>
                {
                    using (var context = new DefaultTransactionContext())
                    {
                        await _transport.Send(QueueName, RecognizableMessage(i), context);
                        await context.Complete();

                        messageIds[i] = 0;
                    }
                }));

            Console.WriteLine("Receiving {0} messages", numberOfMessages);

            using (var timer = new Timer(1000))
            {
                timer.Elapsed += delegate
                {
                    Console.WriteLine("Received: {0} msgs", receivedMessages);
                };
                timer.Start();

                await Task.WhenAll(Enumerable.Range(0, numberOfMessages)
                    .Select(async i =>
                    {
                        using (var context = new DefaultTransactionContext())
                        {
                            var msg = await _transport.Receive(context);
                            await context.Complete();

                            Interlocked.Increment(ref receivedMessages);

                            var id = int.Parse(msg.Headers["id"]);

                            messageIds.AddOrUpdate(id, 1, (_, existing) => existing + 1);
                        }
                    }));

                await Task.Delay(1000);
            }

            Assert.That(messageIds.Keys.OrderBy(k => k).ToArray(), Is.EqualTo(Enumerable.Range(0, numberOfMessages).ToArray()));

            var kvpsDifferentThanOne = messageIds.Where(kvp => kvp.Value != 1).ToList();

            if (kvpsDifferentThanOne.Any())
            {
                Assert.Fail(@"Oh no! the following IDs were not received exactly once:

{0}",
    string.Join(Environment.NewLine, kvpsDifferentThanOne.Select(kvp => string.Format("   {0}: {1}", kvp.Key, kvp.Value))));
            }
        }
Example #32
0
        private async Task clearOutdatedConnections()
        {
            while (runCleanUpTask)
            {
                try
                {
                    foreach (var item in cache)
                    {
                        var queue = item.Value;

                        while (queue.Count > 0)
                        {
                            if (queue.TryDequeue(out var connection))
                            {
                                var cutOff = DateTime.Now.AddSeconds(-1 * server.ConnectionTimeOutSeconds);
                                if (!server.EnableConnectionPool ||
                                    connection.LastAccess < cutOff)
                                {
                                    disposalBag.Add(connection);
                                    continue;
                                }

                                queue.Enqueue(connection);
                                break;
                            }
                        }
                    }

                    try
                    {
                        await @lock.WaitAsync();

                        //clear empty queues
                        var emptyKeys = cache.Where(x => x.Value.Count == 0).Select(x => x.Key).ToList();
                        foreach (string key in emptyKeys)
                        {
                            cache.TryRemove(key, out var _);
                        }
                    }
                    finally
                    {
                        @lock.Release();
                    }

                    while (!disposalBag.IsEmpty)
                    {
                        if (disposalBag.TryTake(out var connection))
                        {
                            connection?.Dispose();
                        }
                    }
                }
                catch (Exception e)
                {
                    server.ExceptionFunc(new Exception("An error occurred when disposing server connections.", e));
                }
                finally
                {
                    //cleanup every 3 seconds by default
                    await Task.Delay(1000 * 3);
                }
            }
        }
Example #33
0
        public static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            List<Basket> baskets = new List<Basket>();
            ConcurrentDictionary<int, int> c1 = new ConcurrentDictionary<int, int>();
            for (int i = 0; i < NUM_BASKETS; i++)
            {
                var basket = ReadBasket("../../../../new_data/modified_basket_" + i.ToString("000000") + ".dat");
                baskets.Add(basket);
                foreach (var item in basket.Items)
                {
                    c1.AddOrUpdate(item.ItemId, 1, (key, value) => value + 1);
                }
            }
            var f1 = c1.Where(x => x.Value >= s).Select(x => x.Key).ToList();
            var c2 = new ConcurrentDictionary<Tuple<int, int>, int>();

            foreach (var basket in baskets)
            {
                for (int i = 0; i < basket.Items.Count - 1; i++)
                {
                    if (f1.Contains(basket.Items[i].ItemId))
                    {
                        for (int j = i + 1; j < basket.Items.Count; j++)
                        {
                            if (f1.Contains(basket.Items[j].ItemId))
                            {
                                c2.AddOrUpdate(new Tuple<int, int>(basket.Items[i].ItemId,
                                                                    basket.Items[j].ItemId),
                                                    1, (key, value) => value + 1);
                            }
                        }
                    }
                }
            }

            var f2 = c2.Where(x => x.Value >= s).ToList();

            /*
             * The pool of items that could be in a triple with support s consists of
             * items that are in at least two pairs with support s
             */
            var singleItems = new ConcurrentDictionary<int, int>();
            foreach (var pair in f2)
            {
                singleItems.AddOrUpdate(pair.Key.Item1, 1, (key, value) => value + 1);
                singleItems.AddOrUpdate(pair.Key.Item2, 1, (key, value) => value + 1);
            }
            var triplePool = singleItems.Keys.Where(x => x >= 2);

            var c3 = new ConcurrentDictionary<Tuple<int, int, int>, int>();
            foreach (var basket in baskets)
            {
                for (int i = 0; i < basket.Items.Count - 2; i++)
                {
                    if (triplePool.Contains(basket.Items[i].ItemId))
                    {
                        for (int j = i + 1; j < basket.Items.Count - 1; j++)
                        {
                            if (triplePool.Contains(basket.Items[j].ItemId))
                            {
                                for (int k = j + 1; k < basket.Items.Count; k++)
                                {
                                    if (triplePool.Contains(basket.Items[k].ItemId))
                                    {
                                        c3.AddOrUpdate(new Tuple<int, int, int>(basket.Items[i].ItemId,
                                                                                basket.Items[j].ItemId,
                                                                                basket.Items[k].ItemId),
                                                        1, (key, value) => value + 1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            var f3 = c3.Where(x => x.Value >= s).ToList();

            using (var writer = new StreamWriter("../../../../output.txt"))
            {
                foreach (var itemSet in f3)
                {
                    writer.WriteLine(PrintSet(itemSet));
                }
            }

            // Stage 2
            Dictionary<string, int> sentiments = File.ReadLines("../../../../sentiment.csv")
                .Select(line => line.Split('\t'))
                .ToDictionary(line => line[0], line => Int32.Parse(line[1]));
            var triples = f3.Select(x => new Triple(x.Key.Item1, x.Key.Item2, x.Key.Item3)).ToList();

            foreach (var triple in triples)
            {
                foreach (var basket in baskets)
                {
                    var itemIds = basket.Items.Select(x => x.ItemId).ToList();
                    var day = GetDayOfWeek(basket.Weekday);
                    if(itemIds.Contains(triple.Ids[0]) && itemIds.Contains(triple.Ids[1])
                        && itemIds.Contains(triple.Ids[2]))
                    {
                        foreach (var item in triple.Ids)
                        {
                            var itemReview = basket.Items.Where(i => i.ItemId == item).First().Review;
                            var score = getScore(sentiments, itemReview);
                            triple.AddSentiment(score, day);
                        }
                    }
                }
            }

            using (var writer = new StreamWriter("../../../../outputPhase2.txt"))
            {
                writer.WriteLine("Set\tMonday\tTuesday\tWednesday\tThursday\tFriday\tSaturday\tSunday");
                foreach (var triple in triples)
                {
                    writer.WriteLine(PrintSetWithReviews(triple));
                }
            }

            sw.Stop();

            if (sw.ElapsedMilliseconds < 1000)
            {
                Console.WriteLine("\nProcessing Time: {0} milliseconds", sw.ElapsedMilliseconds);
            }
            else
            {
                Console.WriteLine("\nProcessing Time: {0:00}:{1:00}.{2:00}:{3:000}", sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
            }
            Console.WriteLine("\nPress the any key to exit.");
            Console.ReadKey();
        }
Example #34
0
 public List <CommandRegistryItem> GetByCorrelationIds(string[] correlationIds)
 => _inner
 .Where(x => correlationIds.Contains(x.Key))
 .Select(x => x.Value)
 .ToList();
Example #35
0
        public void WhenInboundHasProblemsOutboundJustKeepsOnTrucking(int numberOfMessages)
        {
            // arrange
            var receivedMessageCount = 0;
            var messageTracker = new ConcurrentDictionary<Guid, int>();
            var resetEvent = new ManualResetEvent(false);
            orderSystemHandlerActivator.Handle<PlaceOrderRequest>(req =>
                {
                    if (req.What != "beer" || req.HowMuch != 12) return;

                    OnCommit.Do(() =>
                        {
                            messageTracker.AddOrUpdate(req.MsgId, 1, (id, count) => count + 1);

                            var newValue = Interlocked.Increment(ref receivedMessageCount);
                            if (newValue >= numberOfMessages)
                            {
                                resetEvent.Set();
                            }
                        });
                });
            var timeout = numberOfMessages.Seconds();
            var keepMakingChaos = true;

            var chaosMonkey = new Thread(() =>
                {
                    while (keepMakingChaos)
                    {
                        Thread.Sleep(0.1.Seconds());
                        inbound.Stop();
                        Console.WriteLine("Inbound stopped - {0} messages processed...", receivedMessageCount);
                        Thread.Sleep(0.2.Seconds());
                        inbound.Start();
                        Thread.Sleep(2.2331.Seconds());
                    }
                });

            // act
            chaosMonkey.Start();
            numberOfMessages.Times(() => pricedesk.Send(CreateMessage()));

            // assert
            var resetEventWasSet = resetEvent.WaitOne(timeout + 5.Seconds());
            keepMakingChaos = false;
            chaosMonkey.Join();

            // chill, be more sure to empty the queue completely
            Thread.Sleep(1.Seconds());

            Assert.That(resetEventWasSet, Is.True, "Request was not received in order system within timeout of {0}", timeout);
            receivedMessageCount.ShouldBeGreaterThanOrEqualTo(numberOfMessages);
            Console.WriteLine("Actual number of received messages: {0}", receivedMessageCount);
            if (messageTracker.Any(t => t.Value > 1))
            {
                Console.WriteLine(@"The following IDs were received more than once:
            {0}", string.Join(Environment.NewLine, messageTracker.Where(t => t.Value > 1).Select(kvp => "    " + kvp.Key + ": " + kvp.Value)));
            }
            messageTracker.Count.ShouldBe(numberOfMessages);
        }
Example #36
0
        public IEnumerable <object> Enumerate(Type type)
        {
            if (type == typeof(object))
            {
                type = null;
            }
            else if (type != null)
            {
                AutoRegisterType(type);
            }
            var   taken    = false;
            ulong oid      = 0;
            ulong finalOid = _owner.GetLastAllocatedOid();
            long  prevProtectionCounter = 0;

            try
            {
                while (true)
                {
                    if (!taken)
                    {
                        _keyValueTrProtector.Start(ref taken);
                    }
                    if (oid == 0)
                    {
                        prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
                        _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
                        if (!_keyValueTr.FindFirstKey())
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (_keyValueTrProtector.WasInterupted(prevProtectionCounter))
                        {
                            _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
                            oid++;
                            byte[] key = BuildKeyFromOid(oid);
                            if (_keyValueTr.FindKey(key, 0, key.Length, FindKeyStrategy.OnlyNext) == FindKeyResult.NotFound)
                            {
                                oid--;
                                break;
                            }
                        }
                        else
                        {
                            if (!_keyValueTr.FindNextKey())
                            {
                                break;
                            }
                        }
                    }
                    oid = ReadOidFromCurrentKeyInTransaction();
                    WeakReference weakObj;
                    if (_objCache.TryGetValue(oid, out weakObj))
                    {
                        var o = weakObj.Target;
                        if (o != null)
                        {
                            if (type == null || type.IsAssignableFrom(o.GetType()))
                            {
                                _keyValueTrProtector.Stop(ref taken);
                                yield return(o);

                                continue;
                            }
                            continue;
                        }
                    }
                    TableInfo             tableInfo;
                    KeyValueDBValueReader reader = ReadObjStart(oid, out tableInfo);
                    if (type != null && !type.IsAssignableFrom(tableInfo.ClientType))
                    {
                        continue;
                    }
                    object obj = ReadObjFinish(oid, tableInfo, reader);
                    _keyValueTrProtector.Stop(ref taken);
                    yield return(obj);
                }
            }
            finally
            {
                if (taken)
                {
                    _keyValueTrProtector.Stop();
                }
            }
            var dirtyObjsToEnum = _dirtyObjSet.Where(p => p.Key > oid && p.Key <= finalOid).ToList();

            dirtyObjsToEnum.Sort((p1, p2) =>
            {
                if (p1.Key < p2.Key)
                {
                    return(-1);
                }
                if (p1.Key > p2.Key)
                {
                    return(1);
                }
                return(0);
            });
            foreach (var dObjPair in dirtyObjsToEnum)
            {
                object obj = dObjPair.Value;
                if (type != null && !type.IsAssignableFrom(obj.GetType()))
                {
                    continue;
                }
                yield return(obj);
            }
        }
Example #37
0
 public WallItemsComposer(ConcurrentDictionary <int, Item> items)
 {
     wallItems = items.Where(x => x.Value.Definition.HasBehaviour(ItemBehaviour.WALL_ITEM)).Select(x => x.Value).ToList();
     owners    = wallItems.GroupBy(x => x.Data.OwnerId).Select(p => p.First().Data.OwnerData).ToList(); // Create distinct list of room owners
 }
Example #38
0
 public static IEnumerable <Room> GetEmptyRooms()
 {
     return(_rooms.Where(r => r.Value.Other == null).Select(r => r.Value));
 }