public int getIdleCount() { int total = 0; for (ConQueue queue : conMap.getAllConQueue()) { total += queue.getAutoCommitCons().size() + queue.getManCommitCons().size(); } return total; }
private void returnCon(BackendConnection c) { c.setAttachment(null); c.setBorrowed(false); c.setLastTime(TimeUtil.currentTimeMillis()); ConQueue queue = this.conMap.getSchemaConQueue(c.getSchema()); boolean ok = false; if (c.isAutocommit()) { ok = queue.getAutoCommitCons().offer(c); } else { ok = queue.getManCommitCons().offer(c); } if (!ok) { LOGGER.warn("can't return to pool ,so close con " + c); c.close("can't return to pool "); } }
public void heatBeatCheck(long timeout, long conHeartBeatPeriod) { int ildeCloseCount = hostConfig.getMinCon() * 3; int MAX_CONS_IN_ONE_CHECK = 5; LinkedList<BackendConnection> heartBeatCons = new LinkedList<BackendConnection>(); long hearBeatTime = TimeUtil.currentTimeMillis() - conHeartBeatPeriod; long hearBeatTime2 = TimeUtil.currentTimeMillis() - 2 * conHeartBeatPeriod; for (ConQueue queue : conMap.getAllConQueue()) { checkIfNeedHeartBeat( heartBeatCons, queue, queue.getAutoCommitCons(), hearBeatTime, hearBeatTime2); if (heartBeatCons.size() < MAX_CONS_IN_ONE_CHECK) { checkIfNeedHeartBeat( heartBeatCons, queue, queue.getManCommitCons(), hearBeatTime, hearBeatTime2); } if (heartBeatCons.size() >= MAX_CONS_IN_ONE_CHECK) { break; } } if (!heartBeatCons.isEmpty()) { for (BackendConnection con : heartBeatCons) { conHeartBeatHanler.doHeartBeat(con, hostConfig.getHearbeatSQL()); } } // check if there has timeouted heatbeat cons conHeartBeatHanler.abandTimeOuttedConns(); int idleCons = getIdleCount(); int activeCons = this.getActiveCount(); int createCount = (hostConfig.getMinCon() - idleCons) / 3; // create if idle too little if ((createCount > 0) && (idleCons + activeCons < size) && (idleCons < hostConfig.getMinCon())) { LOGGER.info( "create connections ,because idle connection not enough ,cur is " + idleCons + ", minCon is " + hostConfig.getMinCon() + " for " + name); NewConnectionRespHandler simpleHandler = new NewConnectionRespHandler(); final String[] schemas = dbPool.getSchemas(); for (int i = 0; i < createCount; i++) { if (this.getActiveCount() + this.getIdleCount() >= size) { break; } try { // creat new connection this.createNewConnection(simpleHandler, null, schemas[i % schemas.length]); } catch (IOException e) { LOGGER.warn("create connection err " + e); } } } else if (getIdleCount() > hostConfig.getMinCon() + ildeCloseCount) { LOGGER.info("too many ilde cons ,close some for datasouce " + name); ArrayList<BackendConnection> readyCloseCons = new ArrayList<BackendConnection>(ildeCloseCount); for (ConQueue queue : conMap.getAllConQueue()) { readyCloseCons.addAll(queue.getIdleConsToClose(ildeCloseCount)); if (readyCloseCons.size() >= ildeCloseCount) { break; } } for (BackendConnection idleCon : readyCloseCons) { if (idleCon.isBorrowed()) { LOGGER.warn("find idle con is using " + idleCon); } idleCon.close("too many idle con"); } } else { int activeCount = this.getActiveCount(); if (activeCount > size) { StringBuilder s = new StringBuilder(); s.append(Alarms.DEFAULT).append("DATASOURCE EXCEED [name=").append(name).append(",active="); s.append(activeCount).append(",size=").append(size).append(']'); LOGGER.warn(s.toString()); } } }
public int getIdleCountForSchema(String schema) { ConQueue queue = conMap.getSchemaConQueue(schema); int total = 0; total += queue.getAutoCommitCons().size() + queue.getManCommitCons().size(); return total; }