重构代码--39修改抽签是总在两个节点中出块的情况

This commit is contained in:
wuwei1972 2019-04-25 20:59:03 +08:00
parent 890f14003c
commit dd5af272cb
6 changed files with 47 additions and 15 deletions

View File

@ -80,6 +80,27 @@
</encoder>
</appender>
<appender name="Vote_Appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/RepChain-Vote-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each file should be at most 100MB, keep 30 days worth of history, but at most 3GB -->
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<immediateFlush>false</immediateFlush>
<append>false</append>
<encoder charset="UTF-8">
<pattern>%date{ISO8601} %-5level %logger{36} %X{akkaSource} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="BlockSyncher_Appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/RepChain-BlockSyncher-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
@ -191,6 +212,10 @@
<appender-ref ref="Consensus_Appender"/>
</logger>
<logger name="Vote_Logger" additivity="false" level="TRACE">
<appender-ref ref="Vote_Appender"/>
</logger>
<logger name="BlockSyncher_Logger" additivity="false" level="TRACE">
<appender-ref ref="BlockSyncher_Appender"/>
</logger>

View File

@ -33,6 +33,7 @@ object RepLogger {
def Storager_Logger = LoggerFactory.getLogger("Storager_Logger")
def StatisTime_Logger = LoggerFactory.getLogger("StatisTime_Logger")
def Sandbox_Logger = LoggerFactory.getLogger("Sandbox_Logger")
def Vote_Logger = LoggerFactory.getLogger("Vote_Logger")
def trace(logger:Logger,msg:String)={
logger.trace(msg)

View File

@ -67,7 +67,7 @@ trait CRFDVoter extends VoterBase {
randomArray
}
override def candidators(nodes: Set[String], seed: Array[Byte]): Array[String] = {
override def candidators(Systemname:String,hash:String,nodes: Set[String], seed: Array[Byte]): Array[String] = {
var nodesSeq = nodes.toSeq.sortBy(f=>(f.toString()))
var len = nodes.size / 2 + 1
val min_len = 4
@ -89,6 +89,9 @@ trait CRFDVoter extends VoterBase {
var e = randomList(j)
candidate(j) = nodesSeq(e.generateSerial)
}
RepLogger.debug(RepLogger.Vote_Logger, s"sysname=${Systemname},hash=${hash},hashvalue=${hashSeed},randomList=${randomList.mkString("|")}")
RepLogger.debug(RepLogger.Vote_Logger, s"sysname=${Systemname},candidates=${candidate.mkString("|")}")
candidate
}
}

View File

@ -45,7 +45,7 @@ class Voter(moduleName: String) extends ModuleBase(moduleName) with CRFDVoter {
import scala.concurrent.forkjoin.ThreadLocalRandom
override def preStart(): Unit = {
RepLogger.info(RepLogger.Consensus_Logger, this.getLogMsgPrefix( "Vote module start"))
RepLogger.info(RepLogger.Vote_Logger, this.getLogMsgPrefix( "Vote module start"))
}
val dataaccess: ImpDataAccess = ImpDataAccess.GetDataAccess(pe.getSysTag)
@ -74,11 +74,12 @@ class Voter(moduleName: String) extends ModuleBase(moduleName) with CRFDVoter {
private def resetCandidator = {
this.BlockHashOfVote = pe.getCurrentBlockHash
val candidatorCur = candidators(SystemCertList.getSystemCertList, Sha256.hash(BlockHashOfVote))
val candidatorCur = candidators(pe.getSysTag,this.BlockHashOfVote,SystemCertList.getSystemCertList, Sha256.hash(BlockHashOfVote))
pe.getNodeMgr.resetCandidator(candidatorCur)
}
private def resetBlocker(idx: Int) = {
RepLogger.trace(RepLogger.Vote_Logger, this.getLogMsgPrefix( s"sysname=${pe.getSysTag},votelist=${pe.getNodeMgr.getCandidator.toArray[String].mkString("|")},idx=${idx}"))
this.Blocker = BlockerInfo(blocker(pe.getNodeMgr.getCandidator.toArray[String], idx), idx, System.currentTimeMillis())
pe.resetBlocker(this.Blocker)
NoticeBlockerMsg
@ -104,26 +105,26 @@ class Voter(moduleName: String) extends ModuleBase(moduleName) with CRFDVoter {
this.cleanVoteInfo
this.resetCandidator
this.resetBlocker(0)
RepLogger.trace(RepLogger.Consensus_Logger, this.getLogMsgPrefix( s"first voter,blocker=${this.Blocker.blocker},voteidx=${this.Blocker.VoteIndex}" + "~" + selfAddr))
RepLogger.trace(RepLogger.Vote_Logger, this.getLogMsgPrefix( s"sysname=${pe.getSysTag},first voter,blocker=${this.Blocker.blocker},voteidx=${this.Blocker.VoteIndex}" + "~" + selfAddr))
} else {
if (!this.BlockHashOfVote.equals(pe.getCurrentBlockHash)) {
//抽签的基础块已经变化需要重续选择候选人
this.cleanVoteInfo
this.resetCandidator
this.resetBlocker(0)
RepLogger.trace(RepLogger.Consensus_Logger, this.getLogMsgPrefix( s"hash change,reset voter,height=${pe.getCurrentHeight},blocker=${this.Blocker.blocker},voteidx=${this.Blocker.VoteIndex}" + "~" + selfAddr))
RepLogger.trace(RepLogger.Vote_Logger, this.getLogMsgPrefix( s"sysname=${pe.getSysTag},hash change,reset voter,height=${pe.getCurrentHeight},blocker=${this.Blocker.blocker},voteidx=${this.Blocker.VoteIndex}" + "~" + selfAddr))
} else {
if (this.Blocker.blocker == "") {
this.cleanVoteInfo
this.resetCandidator
this.resetBlocker(0)
RepLogger.trace(RepLogger.Consensus_Logger, this.getLogMsgPrefix( s"blocker=null,reset voter,height=${pe.getCurrentHeight},blocker=${this.Blocker.blocker},voteidx=${this.Blocker.VoteIndex}" + "~" + selfAddr))
RepLogger.trace(RepLogger.Vote_Logger, this.getLogMsgPrefix( s"sysname=${pe.getSysTag},blocker=null,reset voter,height=${pe.getCurrentHeight},blocker=${this.Blocker.blocker},voteidx=${this.Blocker.VoteIndex}" + "~" + selfAddr))
} else {
if ((System.currentTimeMillis() - this.Blocker.voteTime) / 1000 > TimePolicy.getTimeOutBlock) {
//说明出块超时
this.voteCount = 0
this.resetBlocker(this.Blocker.VoteIndex + 1)
RepLogger.trace(RepLogger.Consensus_Logger, this.getLogMsgPrefix( s"block timeout,reset voter,height=${pe.getCurrentHeight},blocker=${this.Blocker.blocker},voteidx=${this.Blocker.VoteIndex}" + "~" + selfAddr))
RepLogger.trace(RepLogger.Vote_Logger, this.getLogMsgPrefix( s"sysname=${pe.getSysTag},block timeout,reset voter,height=${pe.getCurrentHeight},blocker=${this.Blocker.blocker},voteidx=${this.Blocker.VoteIndex}" + "~" + selfAddr))
} else {
NoticeBlockerMsg
}

View File

@ -46,5 +46,5 @@ trait VoterBase {
* @return
*/
//def candidators[T](nodes:Set[T], seed:Array[Byte]):Set[T]
def candidators(nodes:Set[String], seed:Array[Byte]):Array[String]
def candidators(Systemname:String,hash:String,nodes:Set[String], seed:Array[Byte]):Array[String]
}

View File

@ -30,7 +30,8 @@ class NodeMgr {
//本地缓存稳定的网络节点
private var stableNodes: TreeMap[Address, String] = new TreeMap[Address, String]()
//本地上次候选人名单
private var candidator: TreeMap[String, String] = new TreeMap[String, String]()
//private var candidator: TreeMap[String, String] = new TreeMap[String, String]()
private var candidator: Set[String] = Set.empty[String]
def getNodes: Set[Address] = {
var source = Set.empty[Address]
@ -146,14 +147,14 @@ class NodeMgr {
var source = Set.empty[String]
candidatorLock.lock()
try {
source = candidator.values.toArray.toSet
source = candidator
} finally {
candidatorLock.unlock()
}
source
}
private def putCandidator(addr: String): Unit = {
/*private def putCandidator(addr: String): Unit = {
candidatorLock.lock()
try {
val key = addr.toString
@ -161,18 +162,19 @@ class NodeMgr {
} finally {
candidatorLock.unlock()
}
}
}*/
def resetCandidator(nds: Array[String]): Unit = {
candidatorLock.lock()
try {
candidator = TreeMap.empty[String, String]
candidator = Set.empty[String]
candidator = nds.toSet
} finally {
candidatorLock.unlock()
}
nds.foreach(addr => {
/*nds.foreach(addr => {
putCandidator(addr)
})
})*/
}
}