/// <summary> /// resume handler /// </summary> private void ResumeHandler() { while (this.isDisposed == false && this.resumeState == true) { Thread.Sleep(this.ResumeCheckInterval * 1000); // var resumeCount = 0; var errorCount = 0; for (int i = 0, l = this.memberInfos.Length; i < l; i++) { var memberInfo = this.memberInfos[i]; if (memberInfo.IsError) { errorCount++; try { using (var instance = memberInfo.Member.CreatePoolInstance()) { memberInfo.IsError = false; resumeCount++; } continue; } catch (Exception exception2) { //event this.OnNewInstanceException(memberInfo.Member, exception2); } } } //当具有成员恢复,并启用HASH时 if (resumeCount > 0 && this.supportHash) { var list = new List <MemberInfo>(this.memberInfos.Length); for (int i = 0, l = this.memberInfos.Length; i < l; i++) { if (this.memberInfos[i].IsError == false) { list.Add(this.memberInfos[i]); } } if (list.Count > 0) { lock (this.lockObject) { this.hashing = new ConsistentHashing <MemberInfo>(list.ToArray()); } } } //无异常成员,退出异常检查线程 if (errorCount == 0) { lock (this.resumeObject) { this.resumeState = false; } } } }
/// <summary> /// initialize /// </summary> /// <param name="memberInstanceSize"></param> /// <param name="members"></param> /// <param name="hash">zero disable hash, non-zero enable</param> public Pool(int memberInstanceSize, IPoolMember[] members, int hash) { this.memberInstanceSize = memberInstanceSize; this.members = members; this.memberIndexs = new Dictionary <string, int>(members.Length); // this.memberIndex = 0; this.memberSize = members.Length; this.memberInfos = new MemberInfo[this.memberSize]; // this.activeInstanceCount = memberInstanceSize * this.memberSize; // this.ResumeCheckInterval = 60; // for (int i = 0; i < members.Length; i++) { this.memberIndexs.Add(members[i].PoolMemberId, i); // this.memberInfos[i] = new MemberInfo() { IsError = false, Index = i, Member = members[i], InstanceIndex = 0, Instances = new InstanceInfo[memberInstanceSize] }; } // this.supportHash = hash > 0 && this.memberSize > 1; if (this.supportHash) { this.hashing = new ConsistentHashing <MemberInfo>(this.memberInfos); } }
/// <summary> /// 获取可用HASH成员 /// </summary> /// <param name="key"></param> /// <returns></returns> private MemberInfo GetActiveHashMember(string key) { if (this.memberSize == 1) { return(this.memberInfos[0]); } if (this.hashing == null) { throw new PoolException("Not Enable Hash Get"); } var member = this.hashing.GetPrimary(key); //若当前hash成员有异常,则去除去异常成员后重构hash-table if (member.IsError) { var list = new List <MemberInfo>(this.memberInfos.Length); for (int i = 0, l = this.memberInfos.Length; i < l; i++) { if (this.memberInfos[i].IsError == false) { list.Add(this.memberInfos[i]); } } if (list.Count == 0) { throw new PoolException("No Active Member"); } this.hashing = new ConsistentHashing <MemberInfo>(list.ToArray()); //嵌套获取 member = this.GetActiveHashMember(key); } //if (member.IsError) //{ // for (int i = 0; i < this.memberSize; i++) // { // var index = member.Index + 1; // if (index == this.memberSize) // { // index = 0; // } // member = this.memberInfos[index]; // if (!member.IsError) // { // return member; // } // } // throw new PoolException("No Active Member"); //} return(member); }