luo

遍历t-io连接时 setWithLock.readLock() 和setlock.lock() 有时会卡主

发表于:2019-5-15 10:44:20 点击量:0 赞:未知

获取t-io所有连接后进行遍历有时需要很久;
boolean releasedLock = false; Lock setlock = setWithLock.readLock();setlock.lock();之后再去遍历的 ;
获取 Set chanSet = setWithLock.getObj();
foreach 进行遍历
for(ChannelContext channelContext:chanSet){};
遍历完后 setlock.unlock();releasedLock = true;
获取所有的连接信息,一直卡在遍历中这个地方;

logOut.info(“tio连接监控定时器开始执行”);
SetWithLock setWithLock= Tio.getAllChannelContexts(sendMsgService.getServerGroupContext());
boolean releasedLock = false;
Lock setlock = setWithLock.readLock();
logOut.info(“获取setWithLock-readLock”);
setlock.lock();
logOut.info(“setWithLock-readLock.lock”);
try {
RedisClient.redisconf = redisConf;
RedisClient.del(Const.TIOSTATUS_REDIS_KEY);
Set chanSet = setWithLock.getObj();
logOut.info(“连接数:”+chanSet.size());
logOut.info(“开始从redis获取最新的自动升级版本号”);
String lastVersion = RedisClient.get(Const.YHYCS_UPGRADE_NAME);
logOut.info(“结束从redis获取最新的自动升级版本号,版本号为:”+lastVersion);
List> list = new ArrayList>();
for(ChannelContext channelContext:chanSet){
if(channelContext.isClosed){
continue;
}
Map result = new HashMap();
ChannelStat stat = channelContext.stat;
String version = Util.nulltoString(channelContext.getAttribute(“version”));
result.put(“ip”,channelContext.getClientNode().getIp());
result.put(“userid”,channelContext.userid);
result.put(“tio_version”,version);
result.put(“versionCode”,channelContext.getAttribute(“versionCode”));
try {
if(compareVersion(version,lastVersion)){
//需要升级
BasePacket resppacket = new BasePacket();
resppacket.setType(ImType.UPGRADE);
resppacket.setBody(“”.getBytes(BasePacket.CHARSET));
logOut.info(“向mac地址为:”+channelContext.getBsId()+”,版本号为:”+version+”的通道发送升级信息”);
Tio.send(channelContext,resppacket);
}
} catch (Exception e) {
logOut.error(e);
}
Map userinfo = (Map) channelContext.getAttribute(“loginuser”);
if(userinfo!=null){
result.putAll(userinfo);
}
result.put(“pc_code”,channelContext.getBsId());
result.put(“online”,StringUtils.isNotBlank(channelContext.userid)?”1”:”0”);
result.put(“decodeFailCount”,stat.getDecodeFailCount());
result.put(“handledBytes”,stat.getDecodeFailCount());
result.put(“latestTimeOfReceivedByte”,stat.getLatestTimeOfReceivedByte());
result.put(“latestTimeOfReceivedPacket”,stat.getLatestTimeOfReceivedPacket());
result.put(“latestTimeOfSentByte”,stat.getLatestTimeOfSentByte());
result.put(“latestTimeOfSentPacket”,stat.getLatestTimeOfSentPacket());
result.put(“timeCreated”,stat.getTimeCreated());
result.put(“timeClosed”,stat.getTimeClosed());
result.put(“timeFirstConnected”,stat.getTimeFirstConnected());
result.put(“timeInReconnQueue”,stat.getTimeInReconnQueue());
result.put(“sentBytes”,stat.getSentBytes());
result.put(“sentPackets”,stat.getSentPackets());
result.put(“handledPackets”,stat.getHandledPackets());
result.put(“handledPacketCosts”,stat.getHandledPacketCosts());
result.put(“receivedBytes”,stat.getReceivedBytes());
result.put(“receivedPackets”,stat.getReceivedPackets());
result.put(“receivedTcps”,stat.getReceivedTcps());
list.add(result);
}
logOut.info(“开始释放setWithLock-readLock”);
setlock.unlock();
logOut.info(“setWithLock-readLock.unlock”);
releasedLock = true;
} catch (Exception e) {
logOut.error(e);
}finally {
if (!releasedLock) {
logOut.info(“finally—-开始释放setWithLock-readLock”);
setlock.unlock();
logOut.info(“finally—-setWithLock-readLock.unlock”);
}
}

        private boolean compareVersion(String oldVersion,String NewVersion){
    if(StringUtils.isBlank(oldVersion)||StringUtils.isBlank(NewVersion)){
        return false;
    }
    return NewVersion.compareTo(oldVersion)>0;
}





日志如下:
2019-05-20 10:25:00,311 [pool-3-thread-1] INFO  - nbaisino - tio连接监控定时器开始执行

2019-05-20 10:27:34,761 [pool-3-thread-1] INFO - nbaisino - 获取setWithLock-readLock
2019-05-20 10:27:34,761 [pool-3-thread-1] INFO - nbaisino - setWithLock-readLock.lock
2019-05-20 10:27:39,917 [pool-3-thread-1] INFO - nbaisino - 连接数:12680
2019-05-20 10:28:13,073 [pool-3-thread-1] INFO - nbaisino - 开始从redis获取最新的自动升级版本号
2019-05-20 10:28:24,651 [pool-3-thread-1] INFO - nbaisino - 结束从redis获取最新的自动升级版本号,版本号为:2.0.2.190428
2019-05-20 10:28:54,015 [pool-3-thread-1] INFO - nbaisino - 开始释放setWithLock-readLock
2019-05-20 10:28:54,015 [pool-3-thread-1] INFO - nbaisino - setWithLock-readLock.unlock

2019-05-20 11:05:01,513 [pool-3-thread-8] INFO - nbaisino - tio连接监控定时器开始执行
2019-05-20 11:05:01,513 [pool-3-thread-8] INFO - nbaisino - 获取setWithLock-readLock
2019-05-20 11:21:48,426 [pool-3-thread-8] INFO - nbaisino - setWithLock-readLock.lock
2019-05-20 11:24:01,794 [pool-3-thread-8] INFO - nbaisino - 连接数:12598
2019-05-20 11:24:03,059 [pool-3-thread-8] INFO - nbaisino - 开始从redis获取最新的自动升级版本号
2019-05-20 11:24:03,060 [pool-3-thread-8] INFO - nbaisino - 结束从redis获取最新的自动升级版本号,版本号为:2.0.2.190428
2019-05-20 11:24:07,307 [pool-3-thread-8] INFO - nbaisino - 开始释放setWithLock-readLock
2019-05-20 11:24:07,308 [pool-3-thread-8] INFO - nbaisino - setWithLock-readLock.unlock

TCP连接数:, IP数:
    发 送