private void checkIfNeedHeartBeat( LinkedList<BackendConnection> heartBeatCons, ConQueue queue, ConcurrentLinkedQueue<BackendConnection> checkLis, long hearBeatTime, long hearBeatTime2) { int MAX_CONS_IN_ONE_CHECK = 10; Iterator<BackendConnection> checkListItor = checkLis.iterator(); while (checkListItor.hasNext()) { BackendConnection con = checkListItor.next(); if (con.isClosed()) { checkListItor.remove(); continue; } if (validSchema(con.getSchema())) { if (con.getLastTime() < hearBeatTime) { if (heartBeatCons.size() < MAX_CONS_IN_ONE_CHECK) { checkListItor.remove(); // Heart beat check con.setBorrowed(true); heartBeatCons.add(con); } } } else if (con.getLastTime() < hearBeatTime2) { { // not valid schema conntion should close for idle // exceed 2*conHeartBeatPeriod checkListItor.remove(); con.close(" heart beate idle "); } } } }
private BackendConnection takeCon( BackendConnection conn, final ResponseHandler handler, final Object attachment, String schema) { conn.setBorrowed(true); if (!conn.getSchema().equals(schema)) { // need do schema syn in before sql send conn.setSchema(schema); } ConQueue queue = conMap.getSchemaConQueue(schema); queue.incExecuteCount(); // queue.incExecuteCount(); conn.setAttachment(attachment); handler.connectionAcquired(conn); return conn; }
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 connectionClosed(BackendConnection conn) { ConQueue queue = this.conMap.getSchemaConQueue(conn.getSchema()); if (queue != null) { queue.removeCon(conn); } }
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()); } } }