增加区块索引的缓存机制,提高区块索引的读效率,缓存hash和height。

修改raft抽签获取区块索引的方法为获取区块hash
This commit is contained in:
jiangbuyun 2021-11-13 14:06:11 +08:00
parent 69241b993c
commit 7b4e35a1c3
4 changed files with 75 additions and 14 deletions

View File

@ -96,6 +96,7 @@ libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.com
//scalacOptions += "-P:linter:disable:UseIfExpression+VariableAssignedUnusedValue+UseGetOrElseNotPatMatch" //scalacOptions += "-P:linter:disable:UseIfExpression+VariableAssignedUnusedValue+UseGetOrElseNotPatMatch"
//scapegoatVersion in ThisBuild := "1.3.3" //scapegoatVersion in ThisBuild := "1.3.3"
//scapegoatDisabledInspections := Seq("OptionGet", "AsInstanceOf","MethodReturningAny") //scapegoatDisabledInspections := Seq("OptionGet", "AsInstanceOf","MethodReturningAny")
libraryDependencies += "com.googlecode.concurrentlinkedhashmap" % "concurrentlinkedhashmap-lru" % "1.4.2"
assemblyMergeStrategy in assembly := { assemblyMergeStrategy in assembly := {
case PathList("org", "iq80", "leveldb", xs @ _*) => MergeStrategy.first case PathList("org", "iq80", "leveldb", xs @ _*) => MergeStrategy.first

View File

@ -144,10 +144,14 @@ class VoterOfRAFT (moduleName: String) extends IVoter(moduleName: String) {
} }
if((this.Blocker.VoteHeight +SystemProfile.getBlockNumberOfRaft) <= pe.getMaxHeight4SimpleRaft){ if((this.Blocker.VoteHeight +SystemProfile.getBlockNumberOfRaft) <= pe.getMaxHeight4SimpleRaft){
RepLogger.trace(RepLogger.Vote_Logger, this.getLogMsgPrefix(s"sysname=${pe.getSysTag},second voter,prevheight=${this.Blocker.VoteHeight},prevvoteindex=${this.voteIndex},lh=${pe.getCurrentHeight},currentHeight=${pe.getMaxHeight4SimpleRaft}" + "~" + selfAddr)) RepLogger.trace(RepLogger.Vote_Logger, this.getLogMsgPrefix(s"sysname=${pe.getSysTag},second voter,prevheight=${this.Blocker.VoteHeight},prevvoteindex=${this.voteIndex},lh=${pe.getCurrentHeight},currentHeight=${pe.getMaxHeight4SimpleRaft}" + "~" + selfAddr))
val block = dataaccess.getBlock4ObjectByHeight(this.Blocker.VoteHeight +SystemProfile.getBlockNumberOfRaft) //val block = dataaccess.getBlock4ObjectByHeight(this.Blocker.VoteHeight +SystemProfile.getBlockNumberOfRaft)
if(block != null){ val blockHash = dataaccess.getBlockHashByHeight(this.Blocker.VoteHeight +SystemProfile.getBlockNumberOfRaft)
val currentblockhash = block.hashOfBlock.toStringUtf8() //if(block != null){
val currentheight = block.height if(blockHash != ""){
//val currentblockhash = block.hashOfBlock.toStringUtf8()
//val currentheight = block.height
val currentblockhash = blockHash
val currentheight = this.Blocker.VoteHeight +SystemProfile.getBlockNumberOfRaft
pe.resetTimeoutOfRaft pe.resetTimeoutOfRaft
this.blockTimeout = false this.blockTimeout = false
this.cleanVoteInfo this.cleanVoteInfo

View File

@ -0,0 +1,38 @@
package rep.storage;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.googlecode.concurrentlinkedhashmap.Weighers;
public class IdxCache {
private ConcurrentLinkedHashMap<String, blockindex> fileIdxs = null;
private ConcurrentLinkedHashMap<String, Long> hashcaches = null;
public IdxCache(int maxSize){
this.fileIdxs = new ConcurrentLinkedHashMap.Builder<String,blockindex>().maximumWeightedCapacity(maxSize).weigher(Weighers.singleton()).build();
this.hashcaches = new ConcurrentLinkedHashMap.Builder<String,Long>().maximumWeightedCapacity(maxSize).weigher(Weighers.singleton()).build();
}
public blockindex GetByHeight(String key){
blockindex idx = null;
if(this.fileIdxs.containsKey(key)){
idx = this.fileIdxs.get(key);
}
return idx;
}
public void PutForHeight(String key,blockindex idx){
this.fileIdxs.put(key,idx);
}
public void PutForHash(String key,Long height){
this.hashcaches.put(key,height);
}
public Long GetHeightByHash(String hash){
Long h = -1l;
if(this.hashcaches.containsKey(hash)){
h = this.hashcaches.get(hash);
}
return h;
}
}

View File

@ -51,6 +51,7 @@ import rep.authority.cache.signercache.ImpSignerCache
import rep.authority.cache.certcache.ImpCertCache import rep.authority.cache.certcache.ImpCertCache
import rep.authority.cache.authbind.ImpAuthBindToCert import rep.authority.cache.authbind.ImpAuthBindToCert
import rep.sc.tpl.did.DidTplPrefix import rep.sc.tpl.did.DidTplPrefix
import com.googlecode.concurrentlinkedhashmap.{ConcurrentLinkedHashMap, Weighers}
/** /**
* @author jiangbuyun * @author jiangbuyun
@ -66,6 +67,9 @@ class ImpDataAccess private (SystemName: String) extends IDataAccess(SystemName)
filemgr = new BlockFileMgr(this.SystemName) filemgr = new BlockFileMgr(this.SystemName)
private val cacheSize = 10000
private var fileIdxs = new IdxCache(cacheSize)
//private var chainInfoCache: ChainInfoInCache = new ChainInfoInCache(this) //private var chainInfoCache: ChainInfoInCache = new ChainInfoInCache(this)
private var bheight4bidx = new HashMap[Long, blockindex]() private var bheight4bidx = new HashMap[Long, blockindex]()
@ -181,12 +185,17 @@ class ImpDataAccess private (SystemName: String) extends IDataAccess(SystemName)
private def getBlockIdxByHash(bh: String): blockindex = { private def getBlockIdxByHash(bh: String): blockindex = {
var rb: blockindex = null var rb: blockindex = null
val key = IdxPrefix.IdxBlockPrefix + bh val key = IdxPrefix.IdxBlockPrefix + bh
val value = this.Get(key) //首先从缓存中获取区块的高度
if (value != null) { val h = this.fileIdxs.GetHeightByHash(key)
//rb = new blockindex() if(h != -1){
//rb.InitBlockIndex(value) rb = getBlockIdxByHeight(h)
rb = SerializeUtils.deserialise(value).asInstanceOf[blockindex] }else{
val value = this.Get(key)
if (value != null) {
rb = SerializeUtils.deserialise(value).asInstanceOf[blockindex]
}
} }
rb rb
} }
@ -202,11 +211,18 @@ class ImpDataAccess private (SystemName: String) extends IDataAccess(SystemName)
def getBlockIdxByHeight(h: Long): blockindex = { def getBlockIdxByHeight(h: Long): blockindex = {
var rb: blockindex = null var rb: blockindex = null
val key = IdxPrefix.IdxBlockHeight + String.valueOf(h) val key = IdxPrefix.IdxBlockHeight + String.valueOf(h)
val value = this.Get(key) //首先从缓存中获取区块的索引
if (value != null) { val tmpidx = this.fileIdxs.GetByHeight(key)
val bkey = this.byteToString(value) if(tmpidx != null){
if (!bkey.equalsIgnoreCase("")) { rb = tmpidx
rb = getBlockIdxByHash(bkey) }else {
//缓存中没有该区块的索引从持久化中获取
val value = this.Get(key)
if (value != null) {
val bkey = this.byteToString(value)
if (!bkey.equalsIgnoreCase("")) {
rb = getBlockIdxByHash(bkey)
}
} }
} }
rb rb
@ -928,6 +944,8 @@ class ImpDataAccess private (SystemName: String) extends IDataAccess(SystemName)
//this.Put(IdxPrefix.IdxBlockPrefix + bidx.getBlockHash(), bidx.toArrayByte()) //this.Put(IdxPrefix.IdxBlockPrefix + bidx.getBlockHash(), bidx.toArrayByte())
this.Put(IdxPrefix.IdxBlockPrefix + bidx.getBlockHash(), SerializeUtils.serialise(bidx)) this.Put(IdxPrefix.IdxBlockPrefix + bidx.getBlockHash(), SerializeUtils.serialise(bidx))
this.fileIdxs.PutForHeight(IdxPrefix.IdxBlockHeight + String.valueOf(bidx.getBlockHeight()),bidx)
this.fileIdxs.PutForHash(IdxPrefix.IdxBlockPrefix + bidx.getBlockHash(),bidx.getBlockHeight())
RepLogger.trace( RepLogger.trace(
RepLogger.Storager_Logger, RepLogger.Storager_Logger,