// singleton pattern

        #endregion singleton


        public QueryProcessor GetAvailableFromPool(string Server, string Database, string TaskId)
        {
            if (TaskId == null || TaskId == "")
            {
                throw new ArgumentException("Invlaid TaskId");
            }
            if (_canceledTasks.Contains(TaskId))
            {
                throw new Exception("Task is canceled: " + TaskId.ToUpper());
            }

            // check if not resetting
            WaitForAvalability();

            QueryProcessor proc     = null;
            int            count    = 0;
            int            maxCount = FI.Common.AppConfig.DA_OlapProcessorCount;

            // find avalailable
            while (true)
            {
                lock (this)
                {
                    for (int i = 0; i < _pool.Count; i++)
                    {
                        proc = (QueryProcessor)_pool[i];
                        if (proc.Server == Server && proc.Database == Database)
                        {
                            count++;
                            if (proc.TryAllocate(TaskId))
                            {
                                return(proc);
                            }
                        }
                    }

                    // if possible to add processor
                    if (count < maxCount)
                    {
                        int port = __BASETCPPORT + _pool.Count + 1;
                        proc = new QueryProcessor(port, Server, Database);
                        _pool.Add(proc);

                        if (proc.TryAllocate(TaskId))
                        {
                            return(proc);
                        }
                    }
                }


                // sleep till next loop
                System.Threading.Thread.Sleep(200);
            }
        }
        public QueryProcessor GetAvailableFromPool(string Server, string Database, string TaskId)
        {
            if(TaskId==null || TaskId=="")
                throw new ArgumentException("Invlaid TaskId");
            if(_canceledTasks.Contains(TaskId))
                throw new Exception("Task is canceled: " +  TaskId.ToUpper());

            // check if not resetting
            WaitForAvalability();

            QueryProcessor proc=null;
            int count=0;
            int maxCount=FI.Common.AppConfig.DA_OlapProcessorCount;

            // find avalailable
            while(true)
            {
                lock(this)
                {
                    for(int i=0;i<_pool.Count;i++)
                    {
                        proc=(QueryProcessor)_pool[i];
                        if(proc.Server==Server && proc.Database==Database)
                        {
                            count++;
                            if(proc.TryAllocate(TaskId))
                                return proc;
                        }
                    }

                    // if possible to add processor
                    if(count<maxCount)
                    {
                        int port=__BASETCPPORT + _pool.Count +1;
                        proc=new QueryProcessor(port, Server, Database);
                        _pool.Add(proc);

                        if(proc.TryAllocate(TaskId))
                            return proc;
                    }
                }

                // sleep till next loop
                System.Threading.Thread.Sleep(200);
            }
        }
        // singleton pattern

        #endregion singleton


        public QueryProcessor GetAvailableFromPool(string Server, string Database, string TaskId)
        {
            //Common.LogWriter.Instance.WriteEventLogEntry(
            //    string.Format("ProcessorPool request: server {0}, database {1}.", Server, Database));

            if (TaskId == null || TaskId == "")
            {
                throw new ArgumentException("Invlaid TaskId");
            }
            if (_canceledTasks.Contains(TaskId))
            {
                throw new Exception("Task is canceled: " + TaskId.ToUpper());
            }

            // check if not resetting
            WaitForAvalability();

            QueryProcessor proc     = null;
            int            count    = 0;
            int            maxCount = __MAXPROCESSORCOUNT; //FI.Common.AppConfig.DA_OlapProcessorCount;

            // find avalailable
            QueryProcessor ret = null;

            while (true)
            {
                lock (this)
                {
                    for (int i = 0; i < _pool.Count; i++)
                    {
                        proc = (QueryProcessor)_pool[i];
                        if (proc.Server == Server && proc.Database == Database)
                        {
                            count++;
                            if (proc.TryAllocate(TaskId))
                            {
                                ret = proc;
                            }
                        }
                    }

                    // if possible to add processor
                    if (ret == null && count < maxCount)
                    {
                        //int port=__BASETCPPORT + _pool.Count +1;
                        proc = new QueryProcessor(Server, Database);
                        _pool.Add(proc);

                        if (proc.TryAllocate(TaskId))
                        {
                            ret = proc;
                        }
                    }
                }

                if (ret != null)
                {
                    //Common.LogWriter.Instance.WriteEventLogEntry(
                    //    string.Format("ProcessorPool count {0}.", _pool.Count.ToString()));
                    return(ret);
                }

                // sleep till next loop
                System.Threading.Thread.Sleep(200);
            }
        }