public boolean kickEnginesToRandomHost(List<Engine> enginesToBeKicked, Host hostToAvoid) {
    List<Host> hosts = getAllHosts();
    hosts.remove(hostToAvoid);
    if (hosts.isEmpty()) {
      _logger.error("No Hosts available.");
      return false;
    }
    enginesToBeKicked
        .parallelStream()
        .forEach(
            e -> {
              Host dest = hosts.get(0);
              _logger.info(
                  String.format(
                      "Engine %d will be migrate to Host: %s", e.getId(), dest.getName()));
              _logger.warn(
                  String.format(
                      "Engine %d should be migrate to Host: %s", e.getId(), dest.getName()));
              // TODO: 2016/2/22 should call adapter to do by migration.
              // adapter.Migrate(e,dest);
            });

    _logger.info("migration complete");
    return true;
  }
 public Host createHost(String name, String ip) {
   Host host = new Host();
   host.setName(name);
   host.setIp(ip);
   _pm.exec(host, HibernateEngine.DB_INSERT, true);
   return null;
 }
 private boolean roleSettingOperation(
     TestPlanEntity testPlan, Host tester, List<Engine> idleEngines, List<Engine> engineOnTester) {
   Queue<Engine> engineQ = new ArrayDeque<>();
   engineQ.addAll(engineOnTester);
   boolean finished = setUpRemoteEngines(testPlan, engineQ);
   if (!finished) {
     if (engineQ.isEmpty()) {
       engineQ.addAll(idleEngines);
       finished = setUpRemoteEngines(testPlan, engineQ);
       if (!finished) {
         _logger.error(
             String.format(
                 "Testplan: %d can't init. Too many engine role faults", testPlan.getId()));
         return true;
       }
     } else {
       _logger.error(
           String.format(
               "Testplan: %d can't init. Not enough available engines", testPlan.getId()));
       return true;
     }
   } else {
     if (!engineQ.isEmpty()) {
       List<Engine> enginesToBeKicked = engineQ.stream().collect(Collectors.toList());
       _logger.warn(
           String.format("Host: %d has to many engines, should kickout some.", tester.getId()));
       if (!hostService.kickEnginesToRandomHost(enginesToBeKicked, tester)) {
         _logger.warn(String.format("Host: %d kick out failed. please retry", tester.getId()));
         return true;
       }
     }
   }
   return false;
 }
 public void removeEngine(Host host, EngineRole e) throws GeneralException {
   if (e != null) {
     if (host.getEngineList().contains(e)) {
       host.getEngineList().remove(e);
       _pm.exec(host, HibernateEngine.DB_UPDATE, true);
     } else {
       throw new GeneralException("invalid engine");
     }
   }
 }
 public void addEngine(Host host, EngineRole e) throws GeneralException {
   if (e != null) {
     if (!host.getEngineList().contains(e)) {
       host.getEngineList().add(e);
       _pm.exec(host, HibernateEngine.DB_UPDATE, true);
     } else {
       throw new GeneralException("duplicated engine");
     }
   }
 }
 private void startTest(TestPlanEntity testPlan) {
   Host tester = testPlan.getHost();
   List<EngineRole> engines = tester.getEngineList();
   Timer exe = new Timer();
   exe.schedule(
       new TimerTask() {
         @Override
         public void run() {
           double avgSpeed = 0;
           for (EngineRole e : engines) {
             avgSpeed += e.getCurrentSpeed();
           }
           avgSpeed = avgSpeed / engines.size();
           double speed = avgSpeed * 0.8 + tester.getCapability(engines.size()) * 0.2;
           tester.setCapability(engines.size(), speed);
           _pm.exec(tester, HibernateEngine.DB_UPDATE, true);
           _logger.info("current speed: " + speed);
         }
       },
       1000,
       5000);
   Timer shut = new Timer();
   shut.schedule(
       new TimerTask() {
         @Override
         public void run() {
           exe.cancel();
           _logger.info(
               String.format(
                   "test %s stop, tested capability: %f",
                   testPlan, tester.getCapability(engines.size())));
           for (EngineRole e : engines) {
             e.getEngine().setStatus(EngineStatus.SERVING);
           }
           _pm.exec(tester, HibernateEngine.DB_UPDATE, true);
           testPlan.setFinished(true);
         }
       },
       testPlan.getEndTime().getTime() - (new Date()).getTime());
   testPlan.setStartTime(new Date());
   _executor.put(testPlan.getId(), exe);
   _shutdownNotifier.put(testPlan.getId(), shut);
   _logger.info(String.format("test %s started", testPlan));
 }
 public Host updateSpeed(Host host) {
   if (host == null) return null;
   double speed =
       host.getEngineList()
           .stream()
           .map(EngineRole::getCurrentSpeed)
           .reduce((double) 0, (a, b) -> a + b);
   HostSpeedRcd speedRcd = new HostSpeedRcd();
   Date time = new Date();
   speedRcd.setHost(host);
   speedRcd.setTime(time);
   speedRcd.setSpeed(speed);
   host.getSpeedRcds().add(speedRcd);
   host.setCurrentSpeed(speed);
   host.setRecordTime(time);
   _pm.exec(speedRcd, HibernateEngine.DB_INSERT, true);
   _pm.exec(host, HibernateEngine.DB_UPDATE, true);
   return host;
 }
 private boolean initTest(TestPlanEntity testPlan) {
   List<EngineRole> engineRoles = testPlan.getTestTenant().getEngineList();
   Host tester = testPlan.getHost();
   List<Engine> idleEngines = engineService.getIdleEngines().stream().collect(Collectors.toList());
   List<Engine> engineOnTester =
       tester.getEngineList().stream().map(EngineRole::getEngine).collect(Collectors.toList());
   if (idleEngines.size() + tester.getEngineList().size() < engineRoles.size()) {
     _logger.error(
         String.format("Testplan: %d can't init. Not enough available engines", testPlan.getId()));
     return false;
   }
   tester
       .getEngineList()
       .stream()
       .filter(e -> e.getEngine() != null)
       .forEach(e -> e.getEngine().setStatus(EngineStatus.IDLE));
   if (roleSettingOperation(testPlan, tester, idleEngines, engineOnTester)) return false;
   tester
       .getEngineList()
       .stream()
       .filter(e -> e.getEngine() != null)
       .forEach(e -> e.getEngine().setStatus(EngineStatus.TESTING));
   return true;
 }
 public void setHostCapability(long hid, int engineNum, double capability) {
   Host host = (Host) _pm.get(Host.class, hid);
   host.setCapability(engineNum, capability);
   _pm.exec(host, HibernateEngine.DB_UPDATE);
 }