Пример #1
0
        public override void Init(ConcentratorDataContext context)
        {
            base.Init(context);

            var query = String.Format(@"SELECT Prod_ID as ProdID, [path] as Path, BV.BrandID, ConcentratorID
                                  FROM {0}
                                    LEFT JOIN BrandVendor BV ON BV.VendorBrandCode = CAST(Supplier_id as nvarchar(10))",
                                      ProductIndexBulk.ProductIndexTableName);

            var toProcess          = context.ExecuteStoreQuery <ProductProcessResult>(query).Count();
            var _productsToProcess = context.ExecuteStoreQuery <ProductProcessResult>(query);

            int processed         = 0;
            int progressReportIdx = toProcess / 20;


            context.ExecuteStoreCommand(_productSpecTableCreate);
            context.ExecuteStoreCommand(_productDescTableCreate);
            context.ExecuteStoreCommand(_barcodeTableCreate);
            context.ExecuteStoreCommand(_relatedTableCreate);
            context.ExecuteStoreCommand(_imageTableCreate);

            var options = new ParallelOptions
            {
                MaxDegreeOfParallelism = 8
            };

            Log.InfoFormat("About to download {0} individual product files", toProcess);

            try
            {
                // Processes all products
                Parallel.ForEach(_productsToProcess, options, (p, ls, idx) =>
                {
                    XElement root;
                    using (var file = new MemoryStream())
                    {
                        try
                        {
                            //var req = (HttpWebRequest)WebRequest.Create(new Uri(base.BaseURI, p.Path));
                            //req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                            //req.Credentials = base.Credentials;

                            //var t = req.DownloadDataAsync();
                            ////.ContinueWith<byte[]>(data =>
                            ////{
                            ////  return data.Result;
                            ////});
                            //t.Start(Task.Factory.Scheduler);
                            //t.Wait();
                            //if (t.IsCompleted && !t.IsFaulted)
                            //{
                            //  file.Write(t.Result, 0, t.Result.Length);
                            //}
                            //else
                            //  return;
                            DownloadFile(p.Path, file);

                            file.Position = 0;

                            using (XmlTextReader reader = new XmlTextReader(file))
                            {
                                root = XDocument.Load(reader).Element("ICECAT-interface").Element("Product");
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(String.Format("Failed to download {0} from: {1}", p.ProdID, p.Path), ex);
                            return;
                        }
                    }

                    #region Parse xml

                    var baseSpec = new
                    {
                        HighPic        = root.Attribute("HighPic").Value,
                        ICECatID       = (int)root.Attribute("ID"),
                        Name           = root.Attribute("Name").Value,
                        ManufacturerID = root.Attribute("Prod_id").Value,
                        Quality        = root.Attribute("Quality").Value,
                        ReleaseDate    = root.Attribute("ReleaseDate").Value,
                        ThumbPic       = root.Attribute("ThumbPic").Value,
                        ICECatCategory = (int)root.Element("Category").Attribute("ID"),
                        SupplierID     = (int)root.Element("Supplier").Attribute("ID")
                    };

                    var descriptions                 = from d in root.Elements("ProductDescription")
                                               let l = d.Attribute("langid")
                                                       where l != null
                                                       let lang = (int)l
                                                                  where DataImport.Languages.ContainsKey(lang)
                                                                  select new
                    {
                        IceCatID     = (int)d.Attribute("ID"),
                        LongDesc     = d.Attribute("LongDesc").Value,
                        ManualPDFUrl = d.Attribute("ManualPDFURL").Value,
                        PDFSize      = (int)d.Attribute("ManualPDFSize"),
                        ShortDesc    = d.Attribute("ShortDesc").Value,
                        WarrantyInfo = d.Attribute("WarrantyInfo").Value,
                        ICECatLang   = lang,
                        URL          = d.Attribute("URL").Value
                    };

                    var barcodes = from b in root.Elements("EANCode")
                                   let eanNode = b.Attribute("EAN")
                                                 where eanNode != null
                                                 select eanNode.Value.Trim();

                    var images = from i in root.Element("ProductGallery").Elements("ProductPicture")
                                 select i.Attribute("Pic").Value;

                    var specs = from f in root.Elements("ProductFeature")
                                where !f.IsEmpty
                                let signs = f.Try(e => e.Element("Feature").Element("Measure").Element("Signs"), null)
                                            select new
                    {
                        CatFeatureID                                        = (int)f.Attribute("CategoryFeature_ID"),
                        CatFeatureGroupID                                   = (int)f.Attribute("CategoryFeatureGroup_ID"),
                        SpecID                                              = (int)f.Element("Feature").Attribute("ID"),
                        ValID                                               = (long)f.Attribute("ID"),
                        RawValue                                            = f.Attribute("Value").Value,
                        PresentationValue                                   = f.Attribute("Presentation_Value").Value,
                        Signs                                               = signs.Try(s => from si in s.Elements("Sign")
                                                                 let langID = (int)si.Attribute("langid")
                                                                              where IceCat.DataImport.Languages.ContainsKey(langID)
                                                                              select new
                        {
                            LangID = langID,
                            Value  = si.Value
                        }, null)
                    };

                    var related = from r in root.Elements("ProductRelated")
                                  where !r.IsEmpty
                                  let prod = r.Element("Product")
                                             where !prod.IsEmpty
                                             let supplier = prod.Element("Supplier")
                                                            where supplier != null
                                                            select new
                    {
                        ProdID     = prod.Attribute("Prod_id").Value,
                        SupplierID = (int)supplier.Attribute("ID"),
                        Preferred  = (bool)r.Attribute("Preferred"),
                        Reversed   = (bool)r.Attribute("Reversed")
                    };

                    #endregion Parse xml

                    using (var ctx = new ConcentratorDataContext())
                    {
                        using (var connection = new SqlConnection(Environments.Current.Connection))
                        {
                            connection.Open();

                            var com = connection.CreateCommand();

                            // Insert spec values in staging table
                            com.CommandText = "INSERT INTO " + ProductSpecTable + " ([ProdID], [SupplierID], [LangID], [Value], [ICAttributeID], [CatFeatureID], [CatFeatureGroupID], [ConcentratorProductID]) VALUES(@ProdID, @SupplierID, @LangID, @Value, @AttributeID, @CatFeatureID, @CatFeatureGroupID, @ConProductID);";
                            foreach (var spec in specs)
                            {
                                foreach (var lang in DataImport.Languages)
                                {
                                    com.Parameters.AddWithValue("ProdID", baseSpec.ManufacturerID);
                                    com.Parameters.AddWithValue("SupplierID", baseSpec.SupplierID);
                                    com.Parameters.AddWithValue("LangID", lang.Value);
                                    com.Parameters.AddWithValue("Value", spec.RawValue.Cap(3000));
                                    com.Parameters.AddWithValue("AttributeID", spec.SpecID);
                                    com.Parameters.AddWithValue("ConProductID", p.ConcentratorID);
                                    com.Parameters.AddWithValue("CatFeatureID", spec.CatFeatureID);
                                    com.Parameters.AddWithValue("CatFeatureGroupID", spec.CatFeatureGroupID);

                                    com.ExecuteNonQuery();
                                    com.Parameters.Clear();
                                }
                            }

                            com = connection.CreateCommand();
                            // Insert descriptions in staging table
                            com.CommandText = "INSERT INTO " + ProductDescriptionTable + " ([LongDesc], [ShortDesc], [PDFUrl], [Warranty], [Quality], [LangID], [ConcentratorProductID], PDFSize, [URL]) VALUES(@LongDesc, @ShortDesc, @PDFUrl, @Warranty, @Quality, @LangID, @ConcentratorProductID, @PDFSize, @URL);";
                            foreach (var desc in descriptions)
                            {
                                com.Parameters.AddWithValue("LongDesc", desc.LongDesc.Cap(2500));
                                com.Parameters.AddWithValue("ShortDesc", desc.ShortDesc.Cap(1000));
                                com.Parameters.AddWithValue("PDFUrl", desc.ManualPDFUrl);
                                com.Parameters.AddWithValue("Warranty", desc.WarrantyInfo.Cap(2500));
                                com.Parameters.AddWithValue("LangID", desc.ICECatLang);
                                com.Parameters.AddWithValue("Quality", baseSpec.Quality);
                                com.Parameters.AddWithValue("ConcentratorProductID", p.ConcentratorID);
                                com.Parameters.AddWithValue("PDFSize", desc.PDFSize);
                                com.Parameters.AddWithValue("URL", desc.URL);

                                com.ExecuteNonQuery();
                                com.Parameters.Clear();
                            }

                            com = connection.CreateCommand();
                            // Insert barcodes in staging table
                            com.CommandText = "INSERT INTO " + BarcodeTable + " (Barcode, ProductID) VALUES(@Barcode, @ProductID)";
                            foreach (var barcode in barcodes)
                            {
                                com.Parameters.AddWithValue("Barcode", barcode);
                                com.Parameters.AddWithValue("ProductID", p.ConcentratorID);

                                com.ExecuteNonQuery();
                                com.Parameters.Clear();
                            }

                            // Insert related products in staging table
                            com.CommandText = "INSERT INTO " + RelatedTable + " (LeftProdID, RightICProdID, SupplierID, Reversed, Preferred) VALUES(@LeftProdID, @RightICProdID, @SupplierID, @Reversed, @Preferred)";
                            foreach (var rel in related)
                            {
                                com.Parameters.AddWithValue("LeftProdID", p.ConcentratorID);
                                com.Parameters.AddWithValue("RightICProdID", rel.ProdID);
                                com.Parameters.AddWithValue("SupplierID", rel.SupplierID);
                                com.Parameters.AddWithValue("Reversed", rel.Reversed);
                                com.Parameters.AddWithValue("Preferred", rel.Preferred);

                                com.ExecuteNonQuery();
                                com.Parameters.Clear();
                            }

                            // Insert images in staging table
                            com.CommandText = "INSERT INTO " + ImageTable + " (ProductID, Sequence, URL) VALUES(@ProductID, @Seq, @Url)";
                            var imgs        = images.ToList();
                            for (int i = 0; i < imgs.Count; i++)
                            {
                                var img = imgs[i];
                                com.Parameters.AddWithValue("ProductID", p.ConcentratorID);
                                com.Parameters.AddWithValue("Seq", i + 1);
                                com.Parameters.AddWithValue("Url", img);

                                com.ExecuteNonQuery();
                                com.Parameters.Clear();
                            }
                        }
                    }

                    Interlocked.Increment(ref processed);

                    if (processed % (progressReportIdx <= 0 ? 1 : progressReportIdx) == 0)
                    {
                        Log.InfoFormat("{0} Files downloaded and imported", processed);
                    }
                });
            }
            catch (AggregateException ex)
            {
                foreach (var inEx in ex.InnerExceptions)
                {
                    Log.Fatal("Exception from ProductBulk loop: ", ex);
                }
                throw ex;
            }
        }
        /// <summary>
        ///   Compute SVM output with support vector sharing.
        /// </summary>
        ///
        private int computeParallel(int classA, int classB, double[] input, out double output, Cache cache)
        {
            // Get the machine for this problem
            KernelSupportVectorMachine machine = machines[classA - 1][classB];

            // Get the vectors shared among all machines
            int[] vectors = cache.Vectors[classA - 1][classB];

            double[] values = cache.Products;
            double   sum    = machine.Threshold;


            if (machine.IsCompact)
            {
                // For linear machines, computation is simpler
                for (int i = 0; i < machine.Weights.Length; i++)
                {
                    sum += machine.Weights[i] * input[i];
                }
            }
            else
            {
                #region Backward compatibility
                for (int i = 0; i < vectors.Length; i++)
                {
                    double value;

                    // Check if it is a shared vector
                    int j = vectors[i];

                    if (j >= 0)
                    {
                        // This is a shared vector. Check
                        // if it has already been computed

                        if (!Double.IsNaN(values[j]))
                        {
                            // Yes, it has. Retrieve the value from the cache
                            value = values[j];
                        }
                        else
                        {
                            // No, it has not. Compute and store the computed value in the cache
                            value = values[j] = machine.Kernel.Function(machine.SupportVectors[i], input);
                            Interlocked.Increment(ref cache.Evaluations);
                        }
                    }
                    else
                    {
                        // This vector is not shared by any other machine. No need to cache
                        value = machine.Kernel.Function(machine.SupportVectors[i], input);
                        Interlocked.Increment(ref cache.Evaluations);
                    }

                    sum += machine.Weights[i] * value;
                }
                #endregion
            }

            // Produce probabilities if required
            if (machine.IsProbabilistic)
            {
                output = machine.Link.Inverse(sum);
                return(output >= 0.5 ? +1 : -1);
            }
            else
            {
                output = sum;
                return(output >= 0 ? +1 : -1);
            }
        }
Пример #3
0
 public void Target <U>()
 {
     //dummy line to avoid warnings
     Test.Eval(typeof(U) != null);
     Interlocked.Increment(ref Test.Xcounter);
 }
Пример #4
0
        /// <summary>
		/// Allocate a unique identifier for a resource.
		/// </summary>
		/// <remarks>
		/// Internal resources are given even numbers while resources
		/// allocated by extensions get odd numbers to minimize the communication
		/// between internal and external resource managers.
		/// </remarks>
		/// <returns>The unique identifier of an internal resource (even number starting from 2).</returns>
		static int RegisterInternalInstance()
        {
            // Even numbers are reserved for internal use (odd for externals)
            return Interlocked.Increment(ref ResourceIdCounter) * 2;
        }
        public async Task update_concurrent_conflict_test()
        {
            var aggregateId = ObjectId.GenerateNewStringId();
            var command     = new CreateTestAggregateCommand
            {
                AggregateRootId = aggregateId,
                Title           = "Sample Note"
            };

            //执行创建聚合根的命令
            var commandResult = await _commandService.ExecuteAsync(command, CommandReturnType.EventHandled);

            Assert.NotNull(commandResult);
            Assert.Equal(CommandStatus.Success, commandResult.Status);
            var note = await _memoryCache.GetAsync <TestAggregate>(aggregateId);

            Assert.NotNull(note);
            Assert.Equal("Sample Note", note.Title);
            Assert.Equal(1, ((IAggregateRoot)note).Version);

            //往EventStore直接插入事件,用于模拟并发冲突的情况
            var eventStream = new DomainEventStream(
                ObjectId.GenerateNewStringId(),
                aggregateId,
                typeof(TestAggregate).FullName,
                DateTime.Now,
                new IDomainEvent[] { new TestAggregateTitleChanged("Changed Title")
                                     {
                                         AggregateRootId = aggregateId, Version = 2
                                     } },
                null);
            var result = await _eventStore.BatchAppendAsync(new DomainEventStream[] { eventStream });

            Assert.NotNull(result);
            Assert.Equal(aggregateId, result.SuccessAggregateRootIdList[0]);

            await _publishedVersionStore.UpdatePublishedVersionAsync("DefaultEventProcessor", typeof(TestAggregate).FullName, aggregateId, 2);

            var commandList = new List <ICommand>();

            for (var i = 0; i < 50; i++)
            {
                commandList.Add(new ChangeTestAggregateTitleCommand
                {
                    AggregateRootId = aggregateId,
                    Title           = "Changed Note2"
                });
            }

            var waitHandle = new ManualResetEvent(false);
            var count      = 0L;

            foreach (var updateCommand in commandList)
            {
                await _commandService.ExecuteAsync(updateCommand).ContinueWith(t =>
                {
                    Assert.NotNull(t.Result);
                    var currentCommandResult = t.Result;
                    Assert.NotNull(currentCommandResult);
                    Assert.Equal(CommandStatus.Success, currentCommandResult.Status);
                    var totalCount = Interlocked.Increment(ref count);
                    if (totalCount == commandList.Count)
                    {
                        waitHandle.Set();
                    }
                });
            }
            waitHandle.WaitOne();
            note = await _memoryCache.GetAsync <TestAggregate>(aggregateId);

            Assert.NotNull(note);
            Assert.Equal(2 + commandList.Count, ((IAggregateRoot)note).Version);
            Assert.Equal("Changed Note2", note.Title);
        }
Пример #6
0
 internal void TallyError(Exception e)
 {
     Interlocked.Increment(ref _errorCount);
 }
Пример #7
0
        public override Int32 Create(
            String FileName,
            UInt32 CreateOptions,
            UInt32 GrantedAccess,
            UInt32 FileAttributes,
            Byte[] SecurityDescriptor,
            UInt64 AllocationSize,
            out Object FileNode0,
            out Object FileDesc,
            out FileInfo FileInfo,
            out String NormalizedName)
        {
            FileNode0      = default(Object);
            FileDesc       = default(Object);
            FileInfo       = default(FileInfo);
            NormalizedName = default(String);

            FileNode FileNode;
            FileNode ParentNode;
            Int32    Result = STATUS_SUCCESS;

            FileNode = FileNodeMap.Get(FileName);
            if (null != FileNode)
            {
                return(STATUS_OBJECT_NAME_COLLISION);
            }
            ParentNode = FileNodeMap.GetParent(FileName, ref Result);
            if (null == ParentNode)
            {
                return(Result);
            }

            if (0 != (CreateOptions & FILE_DIRECTORY_FILE))
            {
                AllocationSize = 0;
            }
            if (FileNodeMap.Count() >= MaxFileNodes)
            {
                return(STATUS_CANNOT_MAKE);
            }
            if (AllocationSize > MaxFileSize)
            {
                return(STATUS_DISK_FULL);
            }

            if ("\\" != ParentNode.FileName)
            {
                /* normalize name */
                FileName = ParentNode.FileName + "\\" + Path.GetFileName(FileName);
            }
            FileNode = new FileNode(FileName);
            FileNode.MainFileNode            = FileNodeMap.GetMain(FileName);
            FileNode.FileInfo.FileAttributes = 0 != (FileAttributes & (UInt32)System.IO.FileAttributes.Directory) ?
                                               FileAttributes : FileAttributes | (UInt32)System.IO.FileAttributes.Archive;
            FileNode.FileSecurity = SecurityDescriptor;
            if (0 != AllocationSize)
            {
                Result = SetFileSizeInternal(FileNode, AllocationSize, true);
                if (0 > Result)
                {
                    return(Result);
                }
            }
            FileNodeMap.Insert(FileNode);

            Interlocked.Increment(ref FileNode.OpenCount);
            FileNode0      = FileNode;
            FileInfo       = FileNode.GetFileInfo();
            NormalizedName = FileNode.FileName;

            return(STATUS_SUCCESS);
        }
Пример #8
0
        public async Task ExplicitPublishMode()
        {
            using (var mx = Create(channelPrefix: "foo:"))
            {
                var pub = mx.GetSubscriber();
                int a = 0, b = 0, c = 0, d = 0;
                pub.Subscribe(new RedisChannel("*bcd", RedisChannel.PatternMode.Literal), (x, y) => Interlocked.Increment(ref a));
                pub.Subscribe(new RedisChannel("a*cd", RedisChannel.PatternMode.Pattern), (x, y) => Interlocked.Increment(ref b));
                pub.Subscribe(new RedisChannel("ab*d", RedisChannel.PatternMode.Auto), (x, y) => Interlocked.Increment(ref c));
                pub.Subscribe("abc*", (x, y) => Interlocked.Increment(ref d));

                await Task.Delay(1000).ForAwait();

                pub.Publish("abcd", "efg");
                await UntilCondition(TimeSpan.FromSeconds(10),
                                     () => Thread.VolatileRead(ref b) == 1 &&
                                     Thread.VolatileRead(ref c) == 1 &&
                                     Thread.VolatileRead(ref d) == 1);

                Assert.Equal(0, Thread.VolatileRead(ref a));
                Assert.Equal(1, Thread.VolatileRead(ref b));
                Assert.Equal(1, Thread.VolatileRead(ref c));
                Assert.Equal(1, Thread.VolatileRead(ref d));

                pub.Publish("*bcd", "efg");
                await UntilCondition(TimeSpan.FromSeconds(10), () => Thread.VolatileRead(ref a) == 1);

                Assert.Equal(1, Thread.VolatileRead(ref a));
            }
        }
Пример #9
0
        public async Task TestBasicPubSub(string channelPrefix, bool wildCard, string breaker)
        {
            using (var muxer = Create(channelPrefix: channelPrefix))
            {
                var pub = GetAnyMaster(muxer);
                var sub = muxer.GetSubscriber();
                await PingAsync(muxer, pub, sub).ForAwait();

                HashSet <string> received = new HashSet <string>();
                int    secondHandler      = 0;
                string subChannel         = (wildCard ? "a*c" : "abc") + breaker;
                string pubChannel         = "abc" + breaker;
                Action <RedisChannel, RedisValue> handler1 = (channel, payload) =>
                {
                    lock (received)
                    {
                        if (channel == pubChannel)
                        {
                            received.Add(payload);
                        }
                        else
                        {
                            Log(channel);
                        }
                    }
                }
                , handler2 = (_, __) => Interlocked.Increment(ref secondHandler);
                sub.Subscribe(subChannel, handler1);
                sub.Subscribe(subChannel, handler2);

                lock (received)
                {
                    Assert.Empty(received);
                }
                Assert.Equal(0, Thread.VolatileRead(ref secondHandler));
                var count = sub.Publish(pubChannel, "def");

                await PingAsync(muxer, pub, sub, 3).ForAwait();

                lock (received)
                {
                    Assert.Single(received);
                }
                Assert.Equal(1, Thread.VolatileRead(ref secondHandler));

                // unsubscribe from first; should still see second
                sub.Unsubscribe(subChannel, handler1);
                count = sub.Publish(pubChannel, "ghi");
                await PingAsync(muxer, pub, sub).ForAwait();

                lock (received)
                {
                    Assert.Single(received);
                }
                Assert.Equal(2, Thread.VolatileRead(ref secondHandler));
                Assert.Equal(1, count);

                // unsubscribe from second; should see nothing this time
                sub.Unsubscribe(subChannel, handler2);
                count = sub.Publish(pubChannel, "ghi");
                await PingAsync(muxer, pub, sub).ForAwait();

                lock (received)
                {
                    Assert.Single(received);
                }
                Assert.Equal(2, Thread.VolatileRead(ref secondHandler));
                Assert.Equal(0, count);
            }
        }
Пример #10
0
        public static void ParalellGetPutRemove(int workers, int tables, int putCount, int durationSec)
        {
            var start = DateTime.UtcNow;

            using (var cache = MakeCache())
            {
                var tasks = new Task[workers];

                var totalGet = 0;
                var totalPut = 0;
                var totalRem = 0;

                var getFound  = 0;
                var putInsert = 0;
                var removed   = 0;

                for (var i = 0; i < workers; i++)
                {
                    tasks[i] =
                        Task.Factory.StartNew(
                            () =>
                    {
                        while (true)
                        {
                            var now = DateTime.UtcNow;
                            if ((now - start).TotalSeconds >= durationSec)
                            {
                                return;
                            }

                            var t   = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, tables);
                            var tbl = cache.GetOrCreateTable <string>("tbl_" + t);

                            var get    = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(putCount * 2, putCount * 4);
                            var put    = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(putCount / 2, putCount);
                            var remove = NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, putCount);

                            Interlocked.Add(ref totalGet, get);
                            Interlocked.Add(ref totalPut, put);
                            Interlocked.Add(ref totalRem, remove);

                            for (var j = 0; j < get; j++)
                            {
                                if (null != tbl.Get(NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 1000000).ToString()))
                                {
                                    Interlocked.Increment(ref getFound);
                                }
                            }

                            for (var j = 0; j < put; j++)
                            {
                                if (PutResult.Inserted == tbl.Put(
                                        NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 1000000).ToString(),
                                        NFX.Parsing.NaturalTextGenerator.Generate()
                                        ))
                                {
                                    Interlocked.Increment(ref putInsert);
                                }
                            }

                            for (var j = 0; j < remove; j++)
                            {
                                if (tbl.Remove(
                                        NFX.ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 1000000).ToString()
                                        ))
                                {
                                    Interlocked.Increment(ref removed);
                                }
                            }
                        }
                    }
                            , TaskCreationOptions.LongRunning);
                }

                Task.WaitAll(tasks);

                Console.WriteLine("{0} workers, {1} tables, {2} put, {3} sec duration", workers, tables, putCount, durationSec);
                Console.WriteLine("-----------------------------------------------------------");

                Console.WriteLine("Total Gets: {0:n0}, found {1:n0}", totalGet, getFound);
                Console.WriteLine("Total Puts: {0:n0}, inserted {1:n0}", totalPut, putInsert);
                Console.WriteLine("Total Removes: {0:n0}, removed {1:n0}", totalRem, removed);

                Console.WriteLine("Pile utilized bytes: {0:n0}", cache.Pile.UtilizedBytes);
                Console.WriteLine("Pile object count: {0:n0}", cache.Pile.ObjectCount);

                cache.PurgeAll();

                Assert.AreEqual(0, cache.Count);
                Assert.AreEqual(0, cache.Pile.UtilizedBytes);
                Assert.AreEqual(0, cache.Pile.ObjectCount);


                Console.WriteLine();
            }
        }
Пример #11
0
        /// <summary>
        /// An internal constructor used by the factory methods on task and its descendent(s).
        /// This variant does not capture the ExecutionContext; it is up to the caller to do that.
        /// </summary>
        /// <param name="action">An action to execute.</param>
        /// <param name="state">Optional state to pass to the action.</param>
        /// <param name="parent">Parent of Task.</param>
        /// <param name="cancellationToken">A CancellationToken for the task.</param>
        /// <param name="scheduler">A task scheduler under which the task will run.</param>
        /// <param name="creationOptions">Options to control its execution.</param>
        /// <param name="internalOptions">Internal options to control its execution</param>
        internal Task(Delegate action, object state, Task parent, CancellationToken cancellationToken, TaskCreationOptions creationOptions, InternalTaskOptions internalOptions, TaskScheduler scheduler)
        {
            if (action == null)
            {
#pragma warning disable IDE0016
                throw new ArgumentNullException(nameof(action));
#pragma warning restore IDE0016
            }
            if (scheduler == null)
            {
#pragma warning disable IDE0016
                throw new ArgumentNullException(nameof(scheduler));
#pragma warning restore IDE0016
            }
            Contract.EndContractBlock();
            // This is readonly, and so must be set in the constructor
            // Keep a link to your parent if: (A) You are attached, or (B) you are self-replicating.
            if
            (
                (creationOptions & TaskCreationOptions.AttachedToParent) != 0 ||
                (internalOptions & InternalTaskOptions.SelfReplicating) != 0
            )
            {
                _parent = parent;
            }
            Id      = Interlocked.Increment(ref _lastId) - 1;
            _status = (int)TaskStatus.Created;
            if
            (
                _parent != null &&
                (creationOptions & TaskCreationOptions.AttachedToParent) != 0 &&
                (_parent.CreationOptions & TaskCreationOptions.DenyChildAttach) == 0
            )
            {
                _parent.AddNewChild();
            }
            ExecutingTaskScheduler = scheduler;
            Action      = action;
            State       = state;
            _waitHandle = new ManualResetEventSlim(false);
            if ((creationOptions &
                 ~(TaskCreationOptions.AttachedToParent |
                   TaskCreationOptions.LongRunning |
                   TaskCreationOptions.DenyChildAttach |
                   TaskCreationOptions.HideScheduler |
                   TaskCreationOptions.PreferFairness |
                   TaskCreationOptions.RunContinuationsAsynchronously)) != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(creationOptions));
            }
            // Throw exception if the user specifies both LongRunning and SelfReplicating
            if ((creationOptions & TaskCreationOptions.LongRunning) != 0 &&
                (internalOptions & InternalTaskOptions.SelfReplicating) != 0)
            {
                throw new InvalidOperationException("An attempt was made to create a LongRunning SelfReplicating task.");
            }
            if ((internalOptions & InternalTaskOptions.ContinuationTask) != 0)
            {
                // For continuation tasks or TaskCompletionSource.Tasks, begin life in the
                // WaitingForActivation state rather than the Created state.
                _status = (int)TaskStatus.WaitingForActivation;
            }
            CreationOptions  = creationOptions;
            _internalOptions = internalOptions;
            // if we have a non-null cancellationToken, allocate the contingent properties to save it
            // we need to do this as the very last thing in the construction path, because the CT registration could modify m_stateFlags
            if (cancellationToken.CanBeCanceled)
            {
                AssignCancellationToken(cancellationToken, null, null);
            }
        }
Пример #12
0
        public static void FID_PutGetCorrectness(int cnt, int tbls)
        {
            var sw    = new System.Diagnostics.Stopwatch();
            var dicts = new ConcurrentDictionary <FID, string> [tbls];

            for (var i = 0; i < dicts.Length; i++)
            {
                dicts[i] = new ConcurrentDictionary <FID, string>();
            }
            var notInserted = 0;

            using (var cache = MakeCache())
            {
                sw.Start();
                Parallel.For(0, cnt, (i) =>
                {
                    var t   = i % tbls;
                    var tbl = cache.GetOrCreateTable <FID>(t.ToString());

                    var key  = FID.Generate();
                    var data = NFX.Parsing.NaturalTextGenerator.Generate(0);

                    var pr = tbl.Put(key, data);
                    dicts[t].TryAdd(key, data);
                    if (pr != PutResult.Inserted)
                    {
                        Interlocked.Increment(ref notInserted);
                    }
                });

                var elapsed  = sw.ElapsedMilliseconds;
                var inserted = cnt - notInserted;
                Console.WriteLine("Population of {0:n0} in {1:n0} msec at {2:n0}ops/sec", cnt, elapsed, cnt / (elapsed / 1000d));
                Console.WriteLine("  inserted: {0:n0} ({1:n0}%)", inserted, (int)(100 * (inserted / (double)cnt)));
                Console.WriteLine("  not-inserted: {0:n0} ({1:n0}%)", notInserted, (int)(100 * (notInserted) / (double)cnt));
                sw.Restart();

                var found    = 0;
                var notfound = 0;

                for (var i = 0; i < tbls; i++)
                {
                    var tbl  = cache.GetOrCreateTable <FID>(i.ToString());
                    var dict = dicts[i];

                    Parallel.ForEach(dict, (kvp) =>
                    {
                        var data = tbl.Get(kvp.Key);
                        if (data != null)
                        {
                            Assert.AreEqual(data, kvp.Value);
                            Interlocked.Increment(ref found);
                        }
                        else
                        {
                            Interlocked.Increment(ref notfound);
                        }
                    });
                }

                elapsed = sw.ElapsedMilliseconds;
                var totalGot = found + notfound;
                Console.WriteLine("Got of {0:n0} in {1:n0} msec at {2:n0}ops/sec", totalGot, elapsed, totalGot / (elapsed / 1000d));
                Console.WriteLine("  found: {0:n0} ({1:n0}%)", found, (int)(100 * (found / (double)totalGot)));
                Console.WriteLine("  not-found: {0:n0} ({1:n0}%)", notfound, (int)(100 * (notfound) / (double)totalGot));

                Assert.IsTrue((found / (double)inserted) > 0.9d);
            }//using cache
        }
Пример #13
0
 public ResourceResolverRequest(string path)
 {
     Id          = Interlocked.Increment(ref _lastId);
     Path        = path;
     _waitHandle = new ManualResetEvent(false);
 }
Пример #14
0
 /// <summary>
 ///     Generates a TraceId.
 /// </summary>
 /// <returns></returns>
 public string TraceIdentifier()
 {
     return(_id ??= GenerateRequestId(Interlocked.Increment(ref _requestId)));
 }
Пример #15
0
        public async Task Test()
        {
            await using var pairTracker = await PoolManager.GetServerClient();

            var server = pairTracker.Pair.Server;

            var eventCount = 0;

            await server.WaitAssertion(() =>
            {
                var ticker = IoCManager.Resolve <IEntitySystemManager>().GetEntitySystem <GameTicker>();
                ticker.RestartRound();
                var config = IoCManager.Resolve <IConfigurationManager>();
                config.SetCVar(CCVars.GameLobbyEnabled, true);

                var roundEndSystem = IoCManager.Resolve <IEntitySystemManager>().GetEntitySystem <RoundEndSystem>();
                roundEndSystem.DefaultCooldownDuration     = TimeSpan.FromMilliseconds(250);
                roundEndSystem.DefaultCountdownDuration    = TimeSpan.FromMilliseconds(500);
                roundEndSystem.DefaultRestartRoundDuration = TimeSpan.FromMilliseconds(250);
            });

            await server.WaitAssertion(() =>
            {
                var bus = IoCManager.Resolve <IEntityManager>().EventBus;
                bus.SubscribeEvent <RoundEndSystemChangedEvent>(EventSource.Local, this, _ => {
                    Interlocked.Increment(ref eventCount);
                });
                var roundEndSystem = IoCManager.Resolve <IEntitySystemManager>().GetEntitySystem <RoundEndSystem>();
                // Press the shuttle call button
                roundEndSystem.RequestRoundEnd();
                Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Not.Null, "Shuttle was called, but countdown time was not set");
                Assert.That(roundEndSystem.CanCall(), Is.False, "Started the shuttle, but didn't have to wait cooldown to press cancel button");
                // Check that we can't recall the shuttle yet
                roundEndSystem.CancelRoundEndCountdown();
                Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Not.Null, "Shuttle was cancelled, even though the button was on cooldown");
            });

            await WaitForEvent(); // Wait for Cooldown

            await server.WaitAssertion(() =>
            {
                var roundEndSystem = IoCManager.Resolve <IEntitySystemManager>().GetEntitySystem <RoundEndSystem>();

                Assert.That(roundEndSystem.CanCall(), Is.True, "We waited a while, but the cooldown is not expired");
                Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Not.Null, "We were waiting for the cooldown, but the round also ended");
                // Recall the shuttle, which should trigger the cooldown again
                roundEndSystem.CancelRoundEndCountdown();
                Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Null, "Recalled shuttle, but countdown has not ended");
                Assert.That(roundEndSystem.CanCall(), Is.False, "Recalled shuttle, but cooldown has not been enabled");
            });

            await WaitForEvent(); // Wait for Cooldown

            await server.WaitAssertion(() =>
            {
                var roundEndSystem = IoCManager.Resolve <IEntitySystemManager>().GetEntitySystem <RoundEndSystem>();
                Assert.That(roundEndSystem.CanCall(), Is.True, "We waited a while, but the cooldown is not expired");
                // Press the shuttle call button
                roundEndSystem.RequestRoundEnd();
            });

            await WaitForEvent(); // Wait for Cooldown

            await server.WaitAssertion(() =>
            {
                var roundEndSystem = IoCManager.Resolve <IEntitySystemManager>().GetEntitySystem <RoundEndSystem>();
                Assert.That(roundEndSystem.CanCall(), Is.True, "We waited a while, but the cooldown is not expired");
                Assert.That(roundEndSystem.ExpectedCountdownEnd, Is.Not.Null, "The countdown ended, but we just wanted the cooldown to end");
            });

            await WaitForEvent(); // Wait for countdown to end round

            await CheckRunLevel(GameRunLevel.PostRound);

            await WaitForEvent(); // Wait for Restart

            await CheckRunLevel(GameRunLevel.PreRoundLobby);

            Task CheckRunLevel(GameRunLevel level)
            {
                return(server.WaitAssertion(() =>
                {
                    var ticker = IoCManager.Resolve <IEntitySystemManager>().GetEntitySystem <GameTicker>();
                    Assert.That(ticker.RunLevel, Is.EqualTo(level));
                }));
            }

            async Task WaitForEvent()
            {
                var timeout      = Task.Delay(TimeSpan.FromSeconds(10));
                var currentCount = Thread.VolatileRead(ref eventCount);

                while (currentCount == Thread.VolatileRead(ref eventCount) && !timeout.IsCompleted)
                {
                    await PoolManager.RunTicksSync(pairTracker.Pair, 5);
                }
                if (timeout.IsCompleted)
                {
                    throw new TimeoutException("Event took too long to trigger");
                }
            }

            await pairTracker.CleanReturnAsync();
        }
Пример #16
0
        public async Task TestPartialSubscriberGetMessage()
        {
            using (var muxerA = Create())
                using (var muxerB = Create())
                    using (var conn = Create())
                    {
                        int gotA = 0, gotB = 0;
                        var listenA = muxerA.GetSubscriber();
                        var listenB = muxerB.GetSubscriber();
                        var pub     = conn.GetSubscriber();
                        var prefix  = Me();
                        var tA      = listenA.SubscribeAsync(prefix + "channel", (s, msg) => { if (s == prefix + "channel" && msg == "message")
                                                                                               {
                                                                                                   Interlocked.Increment(ref gotA);
                                                                                               }
                                                             });
                        var tB = listenB.SubscribeAsync(prefix + "chann*", (s, msg) => { if (s == prefix + "channel" && msg == "message")
                                                                                         {
                                                                                             Interlocked.Increment(ref gotB);
                                                                                         }
                                                        });
                        await Task.WhenAll(tA, tB).ForAwait();

                        Assert.Equal(2, pub.Publish(prefix + "channel", "message"));
                        await AllowReasonableTimeToPublishAndProcess().ForAwait();

                        Assert.Equal(1, Interlocked.CompareExchange(ref gotA, 0, 0));
                        Assert.Equal(1, Interlocked.CompareExchange(ref gotB, 0, 0));

                        // and unsubscibe...
                        tB = listenB.UnsubscribeAsync(prefix + "chann*", null);
                        await tB;
                        Assert.Equal(1, pub.Publish(prefix + "channel", "message"));
                        await AllowReasonableTimeToPublishAndProcess().ForAwait();

                        Assert.Equal(2, Interlocked.CompareExchange(ref gotA, 0, 0));
                        Assert.Equal(1, Interlocked.CompareExchange(ref gotB, 0, 0));
                    }
        }
Пример #17
0
 public void SetNewSessionId()
 {
     SessionId2 = (uint)Interlocked.Increment(ref _sid);
 }
Пример #18
0
        internal string GenerateMessageId()
        {
            var id = Interlocked.Increment(ref lastMessageId);

            return(DebugName + id);
        }
        private int NextId()
        {
            int id = Interlocked.Increment(ref _nextId);

            return(id);
        }
Пример #20
0
        public void BufferingTargetWrapperSyncWithTimedFlushTest()
        {
            var myTarget      = new MyTarget();
            var targetWrapper = new BufferingTargetWrapper
            {
                WrappedTarget = myTarget,
                BufferSize    = 10,
                FlushTimeout  = 1000,
            };

            InitializeTargets(myTarget, targetWrapper);

            const int totalEvents = 100;

            var continuationHit    = new bool[totalEvents];
            var lastException      = new Exception[totalEvents];
            var continuationThread = new Thread[totalEvents];
            var hitCount           = 0;

            CreateContinuationFunc createAsyncContinuation =
                eventNumber =>
                ex =>
            {
                lastException[eventNumber]      = ex;
                continuationThread[eventNumber] = Thread.CurrentThread;
                continuationHit[eventNumber]    = true;
                Interlocked.Increment(ref hitCount);
            };

            // write 9 events - they will all be buffered and no final continuation will be reached
            var eventCounter = 0;

            for (var i = 0; i < 9; ++i)
            {
                targetWrapper.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
            }

            Assert.AreEqual(0, hitCount);
            Assert.AreEqual(0, myTarget.WriteCount);

            // sleep 2 seconds, this will trigger the timer and flush all events
            Thread.Sleep(4000);
            Assert.AreEqual(9, hitCount);
            Assert.AreEqual(1, myTarget.BufferedWriteCount);
            Assert.AreEqual(9, myTarget.BufferedTotalEvents);
            Assert.AreEqual(9, myTarget.WriteCount);
            for (var i = 0; i < hitCount; ++i)
            {
                Assert.AreNotSame(Thread.CurrentThread, continuationThread[i]);
                Assert.IsNull(lastException[i]);
            }

            // write 11 more events, 10 will be hit immediately because the buffer will fill up
            // 1 will be pending
            for (var i = 0; i < 11; ++i)
            {
                targetWrapper.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
            }

            Assert.AreEqual(19, hitCount);
            Assert.AreEqual(2, myTarget.BufferedWriteCount);
            Assert.AreEqual(19, myTarget.BufferedTotalEvents);
            Assert.AreEqual(19, myTarget.WriteCount);

            // sleep 2 seonds and the last remaining one will be flushed
            Thread.Sleep(2000);
            Assert.AreEqual(20, hitCount);
            Assert.AreEqual(3, myTarget.BufferedWriteCount);
            Assert.AreEqual(20, myTarget.BufferedTotalEvents);
            Assert.AreEqual(20, myTarget.WriteCount);
        }
Пример #21
0
        private static async void RunOneSection(SectionAddress addr, string logMsg, bool hideProgress)
        {
            if (Engine.WorkingLock == 0)
            {
                Interlocked.Increment(ref Engine.WorkingLock);

                Logger           logger    = null;
                SettingViewModel setting   = null;
                MainViewModel    mainModel = null;

                Application.Current.Dispatcher.Invoke(() =>
                {
                    MainWindow w = Application.Current.MainWindow as MainWindow;

                    logger    = w.Logger;
                    mainModel = w.Model;
                    setting   = w.Setting;

                    // Populate BuildTree
                    if (!hideProgress)
                    {
                        w.Model.BuildTree.Children.Clear();
                        w.PopulateOneTreeView(addr.Plugin, w.Model.BuildTree, w.Model.BuildTree);
                        w.CurBuildTree = null;
                    }
                });

                mainModel.WorkInProgress = true;

                EngineState s = new EngineState(addr.Plugin.Project, logger, mainModel, addr.Plugin, addr.Section.SectionName);
                s.SetOption(setting);
                s.DisableLogger = setting.Log_DisableInInterface;

                Engine.WorkingEngine = new Engine(s);

                // Build Start, Switch to Build View
                if (!hideProgress)
                {
                    mainModel.SwitchNormalBuildInterface = false;
                }

                // Run
                long buildId = await Engine.WorkingEngine.Run(logMsg);

                // Build Ended, Switch to Normal View
                if (!hideProgress)
                {
                    mainModel.SwitchNormalBuildInterface = true;
                }

                // Turn off ProgressRing
                mainModel.WorkInProgress = false;

                Engine.WorkingEngine = null;
                Interlocked.Decrement(ref Engine.WorkingLock);

                if (!hideProgress)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        MainWindow w = Application.Current.MainWindow as MainWindow;
                        w.DrawPlugin(w.CurMainTree.Plugin);
                    });
                }
            }
        }
Пример #22
0
        public void BufferingTargetWrapperSyncTest1()
        {
            var myTarget      = new MyTarget();
            var targetWrapper = new BufferingTargetWrapper
            {
                WrappedTarget = myTarget,
                BufferSize    = 10,
            };

            InitializeTargets(myTarget, targetWrapper);

            const int totalEvents = 100;

            var continuationHit    = new bool[totalEvents];
            var lastException      = new Exception[totalEvents];
            var continuationThread = new Thread[totalEvents];
            var hitCount           = 0;

            CreateContinuationFunc createAsyncContinuation =
                eventNumber =>
                ex =>
            {
                lastException[eventNumber]      = ex;
                continuationThread[eventNumber] = Thread.CurrentThread;
                continuationHit[eventNumber]    = true;
                Interlocked.Increment(ref hitCount);
            };

            // write 9 events - they will all be buffered and no final continuation will be reached
            var eventCounter = 0;

            for (var i = 0; i < 9; ++i)
            {
                targetWrapper.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
            }

            Assert.AreEqual(0, hitCount);
            Assert.AreEqual(0, myTarget.WriteCount);

            // write one more event - everything will be flushed
            targetWrapper.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
            Assert.AreEqual(10, hitCount);
            Assert.AreEqual(1, myTarget.BufferedWriteCount);
            Assert.AreEqual(10, myTarget.BufferedTotalEvents);
            Assert.AreEqual(10, myTarget.WriteCount);
            for (var i = 0; i < hitCount; ++i)
            {
                Assert.AreSame(Thread.CurrentThread, continuationThread[i]);
                Assert.IsNull(lastException[i]);
            }

            // write 9 more events - they will all be buffered and no final continuation will be reached
            for (var i = 0; i < 9; ++i)
            {
                targetWrapper.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
            }

            // no change
            Assert.AreEqual(10, hitCount);
            Assert.AreEqual(1, myTarget.BufferedWriteCount);
            Assert.AreEqual(10, myTarget.BufferedTotalEvents);
            Assert.AreEqual(10, myTarget.WriteCount);

            Exception flushException = null;
            var       flushHit       = new ManualResetEvent(false);

            targetWrapper.Flush(
                ex =>
            {
                flushException = ex;
                flushHit.Set();
            });

            Thread.Sleep(1000);

            flushHit.WaitOne();
            Assert.IsNull(flushException);

            // make sure remaining events were written
            Assert.AreEqual(19, hitCount);
            Assert.AreEqual(2, myTarget.BufferedWriteCount);
            Assert.AreEqual(19, myTarget.BufferedTotalEvents);
            Assert.AreEqual(19, myTarget.WriteCount);
            Assert.AreEqual(1, myTarget.FlushCount);

            // flushes happen on the same thread
            for (var i = 10; i < hitCount; ++i)
            {
                Assert.IsNotNull(continuationThread[i]);
                Assert.AreSame(Thread.CurrentThread, continuationThread[i], "Invalid thread #" + i);
                Assert.IsNull(lastException[i]);
            }

            // flush again - should just invoke Flush() on the wrapped target
            flushHit.Reset();
            targetWrapper.Flush(
                ex =>
            {
                flushException = ex;
                flushHit.Set();
            });

            flushHit.WaitOne();
            Assert.AreEqual(19, hitCount);
            Assert.AreEqual(2, myTarget.BufferedWriteCount);
            Assert.AreEqual(19, myTarget.BufferedTotalEvents);
            Assert.AreEqual(19, myTarget.WriteCount);
            Assert.AreEqual(2, myTarget.FlushCount);

            targetWrapper.Close();
            myTarget.Close();
        }
Пример #23
0
        private void Drain()
        {
            if (Interlocked.Increment(ref _trampoline) != 1)
            {
                return;
            }

            for (; ;)
            {
                if (Volatile.Read(ref _isDisposed))
                {
                    while (_stack.Count != 0)
                    {
                        var enumerator = _stack.Pop();
                        enumerator.Dispose();
                    }

                    Disposable.TryDispose(ref _currentSubscription);
                }
                else
                {
                    if (_stack.Count != 0)
                    {
                        var currentEnumerator = _stack.Peek();

                        var currentObservable = default(IObservable <TSource>);

                        try
                        {
                            if (currentEnumerator.MoveNext())
                            {
                                currentObservable = currentEnumerator.Current;
                            }
                        }
                        catch (Exception ex)
                        {
                            currentEnumerator.Dispose();
                            ForwardOnError(ex);
                            Volatile.Write(ref _isDisposed, true);
                            continue;
                        }

                        IObservable <TSource> next;

                        try
                        {
                            next = Helpers.Unpack(currentObservable);
                        }
                        catch (Exception ex)
                        {
                            if (!Fail(ex))
                            {
                                Volatile.Write(ref _isDisposed, true);
                            }
                            continue;
                        }

                        if (next != null)
                        {
                            var nextSeq = Extract(next);
                            if (nextSeq != null)
                            {
                                if (TryGetEnumerator(nextSeq, out var nextEnumerator))
                                {
                                    _stack.Push(nextEnumerator);
                                    continue;
                                }

                                Volatile.Write(ref _isDisposed, true);
                                continue;
                            }

                            // we need an unique indicator for this as
                            // Subscribe could return a Disposable.Empty or
                            // a BooleanDisposable
                            var sad = ReadyToken.Ready;

                            // Swap in the Ready indicator so we know the sequence hasn't been disposed
                            if (Disposable.TrySetSingle(ref _currentSubscription, sad) == TrySetSingleResult.Success)
                            {
                                // subscribe to the source
                                var d = next.SubscribeSafe(this);

                                // Try to swap in the returned disposable in place of the Ready indicator
                                // Since this drain loop is the only one to use Ready, this should
                                // be unambiguous
                                var u = Interlocked.CompareExchange(ref _currentSubscription, d, sad);

                                // sequence disposed or completed synchronously
                                if (u != sad)
                                {
                                    d.Dispose();
                                    if (u == BooleanDisposable.True)
                                    {
                                        continue;
                                    }
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            _stack.Pop();
                            currentEnumerator.Dispose();
                            continue;
                        }
                    }
                    else
                    {
                        Volatile.Write(ref _isDisposed, true);
                        Done();
                    }
                }

                if (Interlocked.Decrement(ref _trampoline) == 0)
                {
                    break;
                }
            }
        }
Пример #24
0
            internal void ResetSentinel()
            {
                if (ResetSentinelFlag != 0)
                {
                    return;
                }
                if (Interlocked.Increment(ref ResetSentinelFlag) != 1)
                {
                    Interlocked.Decrement(ref ResetSentinelFlag);
                    return;
                }
                string masterhostEnd = _masterHost;
                var    allkeys       = _ib.GetKeys().ToList();

                for (int i = 0; i < _sentinels.Count; i++)
                {
                    if (i > 0)
                    {
                        var first = _sentinels.First;
                        _sentinels.RemoveFirst();
                        _sentinels.AddLast(first.Value);
                    }

                    try
                    {
                        using (var sentinelcli = new RedisSentinelClient(_sentinels.First.Value))
                        {
                            var masterhost             = sentinelcli.GetMasterAddrByName(_connectionString.Host);
                            var masterConnectionString = localTestHost(masterhost, RoleType.Master);
                            if (masterConnectionString == null)
                            {
                                continue;
                            }
                            masterhostEnd = masterhost;

                            if (_rw_splitting)
                            {
                                foreach (var slave in sentinelcli.Salves(_connectionString.Host))
                                {
                                    ConnectionStringBuilder slaveConnectionString = localTestHost($"{slave.ip}:{slave.port}", RoleType.Slave);
                                    if (slaveConnectionString == null)
                                    {
                                        continue;
                                    }
                                }
                            }

                            foreach (var sentinel in sentinelcli.Sentinels(_connectionString.Host))
                            {
                                var remoteSentinelHost = $"{sentinel.ip}:{sentinel.port}";
                                if (_sentinels.Contains(remoteSentinelHost))
                                {
                                    continue;
                                }
                                _sentinels.AddLast(remoteSentinelHost);
                            }
                        }
                        break;
                    }
                    catch { }
                }

                foreach (var spkey in allkeys)
                {
                    _ib.TryRemove(spkey, true);
                }
                Interlocked.Exchange(ref _masterHost, masterhostEnd);
                Interlocked.Decrement(ref ResetSentinelFlag);

                ConnectionStringBuilder localTestHost(string host, RoleType role)
                {
                    ConnectionStringBuilder connectionString = _connectionString.ToString();

                    connectionString.Host        = host;
                    connectionString.MinPoolSize = 1;
                    connectionString.MaxPoolSize = 1;
                    using (var cli = new RedisClient(connectionString))
                    {
                        if (cli.Role().role != role)
                        {
                            return(null);
                        }

                        if (role == RoleType.Master)
                        {
                            //test set/get
                        }
                    }
                    connectionString.MinPoolSize = connectionString.MinPoolSize;
                    connectionString.MaxPoolSize = connectionString.MaxPoolSize;

                    _ib.TryRegister(host, () => new RedisClientPool(connectionString, null, TopOwner));
                    allkeys.Remove(host);

                    return(connectionString);
                }
            }
Пример #25
0
 // static methods
 public static int GetNextId()
 {
     return(Interlocked.Increment(ref __lastId));
 }
Пример #26
0
		//
		// Frees a queue's slot.
		//

		private void FreeSlot() {

			//
			// If there is at least a thread blocked on the semaphore,
            // use the freed slot to store its data item and release it.
			//

			WaitNode w;
			if ((w = waitQueue.TryDequeueAndLock()) != null) {
				AddWorker(w.channel);
				w.parker.Unpark(w.waitKey);
				return;
			}

            //
            // The wait queue is empty, so increment the number of
            // free slots
            //

			Interlocked.Increment(ref free);

            //
            // If the wait queue is still empty, return.
            //

            if (waitQueue.IsEmpty) {
                return;
            }

			//
			// Try to release one of the waiter thraeds.
			//

            do {

                //
                // Try to acquire a free slot on behalf of a waiter thread.
                //

                if (!TryReserveSlot()) {
                    return;
                }

                //
                // We got a free queue slot, so try to release a thread
                // waiting to add.
                //

                if ((w = waitQueue.TryDequeueAndLock()) != null) {
                    AddWorker(w.channel);
                    w.parker.Unpark(w.waitKey);
                    return;
                }

                //
                // Release the free slot, and retry the release process if
                // wait queue is not empty.
                //

                Interlocked.Increment(ref free);
            } while (!waitQueue.IsEmpty);
		}
        private async Task handleClient <TWebSocketBehavior>(HttpListenerContext listenerContext, Func <TWebSocketBehavior> behaviorBuilder, CancellationToken token)
            where TWebSocketBehavior : HttpListenerWebSocketServerBehavior
        {
            Guid             connectionId;
            WebSocketContext webSocketContext            = null;
            HttpListenerWebSocketServerBehavior behavior = null;

            try
            {
                int statusCode        = 500;
                var statusDescription = "BadContext";

                behavior = behaviorBuilder();

                if (!behavior.OnValidateContext(webSocketContext, ref statusCode, ref statusDescription))
                {
                    listenerContext.Response.StatusDescription = statusDescription;
                    listenerContext.Response.StatusCode        = statusCode;
                    listenerContext.Response.Close();

                    _logError($"Failed to validate client context. Closing connection. Status: {statusCode}. Description: {statusDescription}.");

                    return;
                }

                connectionId = Guid.NewGuid();

                webSocketContext = await listenerContext.AcceptWebSocketAsync(subProtocol : null);

                bool clientAdded = _clients.TryAdd(connectionId, webSocketContext);
                if (!clientAdded)
                {
                    throw new ArgumentException($"Attempted to add a new web socket connection to server for connection id '{connectionId}' that already exists.");
                }

                Interlocked.Increment(ref _connectedClientCount);
                _logInfo($"Connection id '{connectionId}' accepted; there are now {_connectedClientCount} total clients.");

                var safeconnected = MakeSafe <Guid, WebSocketContext>(behavior.OnConnectionEstablished, "behavior.OnClientConnected");
                safeconnected(connectionId, webSocketContext);
            }
            catch (Exception e)
            {
                _logError($"Client handler exception: {e}");

                listenerContext.Response.StatusCode = 500;
                listenerContext.Response.Close();

                return;
            }

            try
            {
                using (webSocketContext.WebSocket)
                {
                    var stringBehavior = MakeSafe <StringMessageReceivedEventArgs>(behavior.OnStringMessage, "behavior.OnStringMessage");
                    var binaryBehavior = MakeSafe <BinaryMessageReceivedEventArgs>(behavior.OnBinaryMessage, "behavior.OnBinaryMessage");
                    var closeBehavior  = MakeSafe <WebSocketReceivedResultEventArgs>((r) => behavior.OnClose(new WebSocketClosedEventArgs(connectionId, r)), "behavior.OnClose");

                    await webSocketContext.WebSocket.ProcessIncomingMessages(_messageQueue, connectionId, stringBehavior, binaryBehavior, closeBehavior, _logInfo, token);
                }
            }
            finally
            {
                Interlocked.Decrement(ref _connectedClientCount);
                _logInfo($"Connection id '{connectionId}' disconnected; there are now {_connectedClientCount} total clients.");

                webSocketContext?.WebSocket.CleanupSendMutex();

                bool clientRemoved = _clients.TryRemove(connectionId, out webSocketContext);
                if (!clientRemoved)
                {
                    _logError($"Attempted to remove an existing web socket connection to server for connection id '{connectionId}' that no longer exists.");
                }

                _logInfo($"Completed HandleClient task for connection id '{connectionId}'.");
            }
        }
Пример #28
0
 public override int Next(EntityEntry entry) => Interlocked.Increment(ref current);
Пример #29
0
        public void SetupFactory(int botCount)
        {
            while (!factoryGame.LoggedIn)
            {
                Log("Waiting for BotFactory account to login");
                Thread.Sleep(1000);
            }

            Log("Setting up bot factory with " + botCount + " bots");
            Stopwatch watch = new Stopwatch();

            watch.Start();

            int            createdBots = 0;
            List <BotInfo> infos;

            if (Settings.Default.RandomBots)
            {
                infos = botInfos.TakeRandom(botCount).ToList();
            }
            else
            {
                infos = botInfos.Take(botCount).ToList();
            }
            Parallel.ForEach <BotInfo>(infos, info =>
            {
                var bot = LoadBot(info);
                lock (bots)
                    bots.Add(bot);
                Interlocked.Increment(ref createdBots);
            });

            Parallel.For(createdBots, botCount, index =>
            {
                try
                {
                    var bot = CreateBot();
                    lock (bots)
                    {
                        bots.Add(bot);
                        if (bots.Count % 100 == 0)
                        {
                            SaveBotInfos();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log("Error creating new bot: " + ex.Message + "\n" + ex.StackTrace, LogLevel.Error);
                }
            });

            watch.Stop();
            Log("Finished setting up bot factory with " + botCount + " bots in " + watch.Elapsed);

            SaveBotInfos();

            for (; ;)
            {
                string line = Console.ReadLine();
                if (line == null)
                {
                    return;
                }
                string[] lineSplit = line.Split(' ');
                switch (lineSplit[0])
                {
                case "quit":
                case "exit":
                case "close":
                case "shutdown":
                    return;

                case "info":
                case "infos":
                case "stats":
                case "statistics":
                    DisplayStatistics(lineSplit.Length > 1 ? lineSplit[1] : "");
                    break;
                }
            }
        }
Пример #30
0
 public void Increment()
 {
     Interlocked.Increment(ref this.count);
 }