コード例 #1
0
 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 ");
       }
     }
   }
 }
コード例 #2
0
  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 ");
    }
  }
コード例 #3
0
  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());
      }
    }
  }