예제 #1
0
        public void WriteStateInfoToLog()
        {
            if (m_disposed || !m_tracer.IsInfoEnabled)
            {
                return;
            }

            var lastUsed = UtcLastUsedAt;

            m_tracer.InfoFormat("Number of active contexts: {0}, Last used: {1}, Unused age: {2}",
                                m_activeProcessors.Count, lastUsed, (DateTime.UtcNow - lastUsed));
            foreach (var item in m_activeProcessors)
            {
                try
                {
                    var ctx        = item.Key;
                    var ageSeconds = (DateTime.Now - ctx.RequestMessage.CreatedOn).TotalSeconds;
                    var cmdText    = ctx.Request.CommandText;
                    if (string.IsNullOrEmpty(cmdText) && ctx.Request.HaveRequestBulk)
                    {
                        cmdText = string.Format("Bulk {0} with {2} items on {1}", ctx.RequestBulk.DbStatementType, ctx.RequestBulk.EntityName, ctx.RequestBulk.InputItemsCount);
                    }

                    m_tracer.InfoFormat(
                        "AuthTicket: {0}; CommandText: {1}; PrepareOnly: {2}; ReturnDataset: {3}; HaveRequestBulk: {4}; HaveParams: {5}; "
                        + "TotalRowsProduced: {6}; records affected: {7}; received on: {8}; age: {9} seconds; producer status: {10}",
                        ctx.RequestMessage.AuthTicket, cmdText, ctx.Request.PrepareOnly, ctx.Request.ReturnDataset,
                        ctx.Request.HaveRequestBulk, ctx.Request.HaveParameters,
                        ctx.TotalRowsProduced, ctx.RecordsAffected, ctx.RequestMessage.CreatedOn.ToString("s"),
                        ageSeconds, item.Value.Status);
                }
                catch {}
            }
        }
        public void ReadDataFromStore(string docRootPath, int count)
        {
            CheckState();

            if (count > 0 && (docRootPath == null || !Directory.Exists(docRootPath)))
            {
                throw new ArgumentException("Count is non-zero, but storage root is invalid: " + docRootPath);
            }

            m_docRootPath = docRootPath;

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", count, "Count cannot be negative");
            }

            m_untrimmedDocumentCount = count;
            m_capacity = count;

            DocumentIdToIndex.Clear();

            if (m_untrimmedDocumentCount == 0)
            {
                return;
            }

            var timer = Stopwatch.StartNew();

            using (var reader = new BinaryReader(new FileStream(Path.Combine(docRootPath, "_keysvalid.dat"),
                                                                FileMode.Open, FileAccess.Read, FileShare.Read, 1 << 22, FileOptions.SequentialScan)))
            {
                //ReadBitVectorFromStore(reader, ValidDocumentsBitmap, m_untrimmedDocumentCount);
                ValidDocumentsBitmap.Read(reader, (ulong)m_untrimmedDocumentCount);
            }

            using (var reader = new BinaryReader(new FileStream(Path.Combine(docRootPath, "_keys.dat"),
                                                                FileMode.Open, FileAccess.Read, FileShare.Read, 1 << 22, FileOptions.SequentialScan)))
            {
                DocumentKeys.Read(reader, (ulong)m_untrimmedDocumentCount, ValidDocumentsBitmap);

                for (var i = 0; i < m_untrimmedDocumentCount; i++)
                {
                    if (!DocumentIdToIndex.TryAdd(DocumentKeys.GetIntPtrAt(i), i))
                    {
                        throw new Exception("Failed to add the key at offset " + i + " to map");
                    }
                }
            }

            timer.Stop();

            if (m_logger.IsInfoEnabled)
            {
                m_logger.InfoFormat("Loaded container structure for document {0}/{1} in {2} milliseconds. {3} columns, {4} rows.",
                                    DocDesc.DocumentType, DocDesc.Name, timer.ElapsedMilliseconds, FieldIdToColumnStore.Count, m_untrimmedDocumentCount);
            }
        }
예제 #3
0
        public void Start(IHostingService host)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            m_tracer       = host.GetTracer(GetType());
            m_host         = host;
            m_instanceName = GetInstanceName();

            var tcpBaseAddress  = GetTcpBaseAddress();
            var httpBaseAddress = GetHealthServiceBaseAddress();

            try
            {
                PerfCounters.Remove();
                PerfCounters.Install();
            }
            catch (Exception e)
            {
                m_tracer.Exception(e);
                throw;
            }

            var maxConcurrency = Environment.ProcessorCount * 16;
            var maxPending     = Environment.ProcessorCount * 16;

            m_singletonServiceInstance = new DataService(
                m_host.GetTracer(typeof(DataService)), this, tcpBaseAddress.Authority + ", " + m_instanceName, maxConcurrency, null);

            var serviceHost = CreateServiceHost(m_singletonServiceInstance, new[] { tcpBaseAddress, httpBaseAddress }, maxConcurrency, maxPending);

            serviceHost.Open();
            m_serviceHost = serviceHost;

            if (m_tracer.IsInfoEnabled)
            {
                var builder = new StringBuilder(200);
                foreach (var ep in serviceHost.ChannelDispatchers)
                {
                    if (ep.Listener != null)
                    {
                        builder.AppendLine(ep.Listener.Uri.ToString());
                    }
                }
                m_tracer.InfoFormat("Service host [{0}] started. Listening on following base URIs:{1}{2}",
                                    m_instanceName, Environment.NewLine, builder);
            }
        }
예제 #4
0
        protected override TaskResult Work()
        {
            var stamp = TaskResultType.Succeed;
            var rs    = RandSeconds();

            if (rs > 4500)
            {
                stamp = TaskResultType.Failed;
            }

            Thread.Sleep(rs);

            _logWork.InfoFormat("我是执行的任务,工作了{0}秒", rs / 1000);

            throw new NotSupportedException("测试用");
            return(new TaskResult()
            {
                Result = stamp
            });
        }
예제 #5
0
        private void Renewer(CancellationToken cancellationToken)
        {
            var enumerable = _renewalQueue.GetConsumingEnumerable(cancellationToken);

            foreach (var renewingBatch in enumerable)
            {
                _tracer.InfoFormat("Renewing {0} messages", renewingBatch.Count);
                var tokens        = renewingBatch.Select(x => x.LockToken).ToArray();
                var sampleMessage = renewingBatch.First();
                try
                {
                    var context     = sampleMessage.GetPropertyValue("ReceiveContext");
                    var receiver    = context.GetPropertyValue("MessageReceiver");
                    var asyncResult = receiver.ReflectionInvoke("BeginRenewMessageLocks", null, tokens, TimeSpan.FromMinutes(1), null, null);
                    var result      = (IEnumerable <DateTime>)receiver.ReflectionInvoke("EndRenewMessageLocks", asyncResult);
                }
                catch (Exception ex)
                {
                    _tracer.Error(ex, "Can't renew message with lock id " + sampleMessage.LockToken);
                }
            }
        }
예제 #6
0
        public IDataEngine GetEngine(string tenantId, string scopeId)
        {
            if (string.IsNullOrEmpty(tenantId))
            {
                throw new ArgumentNullException("tenantId");
            }

            if (string.IsNullOrEmpty(scopeId))
            {
                throw new ArgumentNullException("scopeId");
            }

            // avoid using GetOrAdd because it may invoke factory method multiple times
            ConcurrentDictionary <string, IDataEngine> tenantEngines;

            if (!m_engines.TryGetValue(tenantId, out tenantEngines))
            {
                lock (m_engines)
                {
                    if (!m_engines.TryGetValue(tenantId, out tenantEngines))
                    {
                        tenantEngines = new ConcurrentDictionary <string, IDataEngine>();
                        if (!m_engines.TryAdd(tenantId, tenantEngines))
                        {
                            throw new Exception("Failed to add tenant-specific cache of engines for tenant " + tenantId);
                        }
                    }

                    if (tenantEngines == null)
                    {
                        throw new Exception("tenantEngines is null, tenant " + tenantId);
                    }
                }
            }

            // avoid using GetOrAdd because it may invoke factory method multiple times
            IDataEngine engine;

            if (!tenantEngines.TryGetValue(scopeId, out engine))
            {
                lock (tenantEngines)
                {
                    if (!tenantEngines.TryGetValue(scopeId, out engine))
                    {
                        var engineCount = GetTotalEnginesCount();
                        if (engineCount > MaxTotalEnginesCount)
                        {
                            new Task(x => ForceExpireOneEngine((DataEngineCache)x), this).Start();
                        }

                        m_tracer.InfoFormat("Creating new engine for tenant {0}, scope {1}", tenantId, scopeId);
                        var factory       = ObjectFactory.GetNamedInstance <IStorageDriverFactory>(m_storageDriverKey);
                        var storageDriver = factory.Create();
                        storageDriver.Initialize(m_tracer, GetDriverConnectionConfig(scopeId, factory));

                        engine = new DataEngine(m_tracer, m_instanceName, m_maxEngineConcurrency, storageDriver, RequireDescriptor(storageDriver));

                        if (!tenantEngines.TryAdd(scopeId, engine))
                        {
                            throw new Exception(string.Format("Failed to add new engine for tenant {0}, scope {1}", tenantId, scopeId));
                        }
                    }

                    if (engine == null)
                    {
                        throw new Exception(string.Format("Engine is null for tenant {0}, scope {1}", tenantId, scopeId));
                    }
                }
            }

            return(engine);
        }
예제 #7
0
        public void Start(IHostingService host)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            m_tracer = host.GetTracer(GetType());
            m_host = host;
            m_instanceName = GetInstanceName();

            var tcpBaseAddress = GetTcpBaseAddress();
            var httpBaseAddress = GetHealthServiceBaseAddress();

            try
            {
                PerfCounters.Remove();
                PerfCounters.Install();
            }
            catch (Exception e)
            {
                m_tracer.Exception(e);
                throw;
            }

            var maxConcurrency = Environment.ProcessorCount * 16;
            var maxPending = Environment.ProcessorCount * 16;

            m_singletonServiceInstance = new DataService(
                m_host.GetTracer(typeof(DataService)), this, tcpBaseAddress.Authority + ", " + m_instanceName, maxConcurrency, null);

            var serviceHost = CreateServiceHost(m_singletonServiceInstance, new[] { tcpBaseAddress, httpBaseAddress }, maxConcurrency, maxPending);
            serviceHost.Open();
            m_serviceHost = serviceHost;

            if (m_tracer.IsInfoEnabled)
            {
                var builder = new StringBuilder(200);
                foreach (var ep in serviceHost.ChannelDispatchers)
                {
                    if (ep.Listener != null)
                    {
                        builder.AppendLine(ep.Listener.Uri.ToString());
                    }
                }
                m_tracer.InfoFormat("Service host [{0}] started. Listening on following base URIs:{1}{2}",
                    m_instanceName, Environment.NewLine, builder);
            }
        }
예제 #8
0
        /// <summary>
        /// 应用程序入口
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            log.DebugFormat("args:[{0}]", string.Join(" ", args));

            try
            {
                var newMutexCreated = false;
                //var reg = new Regex("[^A-Za-z0-9_]", RegexOptions.Compiled);    //移去非字符数字的字符
                var mutexName =
                    Md5.Encrypt(
                        $"{AppDomain.CurrentDomain.BaseDirectory}-{Assembly.GetExecutingAssembly().GetName().FullName}"); //唯一的名称
                try
                {
                    var obj = new Mutex(false, mutexName, out newMutexCreated);
                }
                catch (Exception ex)
                {
                    log.Error($"创建互斥体[mutexName = {mutexName}]异常,程序退出", ex);
                    Environment.Exit(1);
                }
                if (newMutexCreated)
                {
                    log.DebugFormat("创建互斥体[mutexName = {0}]成功,开始创建服务", mutexName);

                    //无参数时直接运行服务
                    if ((!Environment.UserInteractive))
                    {
                        log.DebugFormat("RunAsService");
                        RunAsService();
                        return;
                    }

                    if (args != null && args.Length > 0)
                    {
                        if (args[0].Equals("-i", StringComparison.OrdinalIgnoreCase))
                        {
                            log.InfoFormat("Install the service...");
                            SelfInstaller.InstallMe();
                            return;
                        }
                        if (args[0].Equals("-u", StringComparison.OrdinalIgnoreCase))
                        {
                            log.InfoFormat("Uninstall the service...");
                            SelfInstaller.UninstallMe();
                            return;
                        }
                        if (args[0].Equals("-t", StringComparison.OrdinalIgnoreCase) ||
                            args[0].Equals("-c", StringComparison.OrdinalIgnoreCase))
                        {
                            log.InfoFormat("Run as Console.[{0}]", Assembly.GetExecutingAssembly().Location);
                            RunAsConsole(args);
                            return;
                        }
                        const string tip =
                            "Invalid argument! note:\r\n -i is install the service.;\r\n -u is uninstall the service.;\r\n -t or -c is run the service on console.";
                        log.DebugFormat(tip);
                        Console.WriteLine(tip);
                        Console.ReadLine();
                    }
                    else
                    {
#if DEBUG
                        log.InfoFormat("Run as Console.[{0}]", Assembly.GetExecutingAssembly().Location);
                        RunAsConsole(args);
#endif
                    }
                }
                else
                {
                    log.Error("有一个实例正在运行,如要调试,请先停止其它正在运行的实例如WindowsService,程序退出。");
                    Environment.Exit(1);
                }
            }
            catch (Exception ex)
            {
                log.Error("启动服务异常", ex);
                //throw;
            }
        }