Ejemplo n.º 1
0
void ClusterFeature::unprepare() {
  if (_enableCluster) {
    if (_heartbeatThread != nullptr) {
      _heartbeatThread->beginShutdown();
    }

    // change into shutdown state
    ServerState::instance()->setState(ServerState::STATE_SHUTDOWN);

    AgencyComm comm;
    comm.sendServerState(0.0);

    if (_heartbeatThread != nullptr) {
      int counter = 0;
      while (_heartbeatThread->isRunning()) {
        usleep(100000);
        // emit warning after 5 seconds
        if (++counter == 10 * 5) {
          LOG(WARN) << "waiting for heartbeat thread to finish";
        }
      }
    }

    if (_unregisterOnShutdown) {
      ServerState::instance()->unregister();
    }
  }

  if (!_enableCluster) {
    ClusterComm::cleanup();
    return;
  }

  // change into shutdown state
  ServerState::instance()->setState(ServerState::STATE_SHUTDOWN);

  AgencyComm comm;
  comm.sendServerState(0.0);

  // Try only once to unregister because maybe the agencycomm
  // is shutting down as well...

  ServerState::RoleEnum role = ServerState::instance()->getRole();

  AgencyWriteTransaction unreg;

  // Remove from role
  if (role == ServerState::ROLE_PRIMARY) {
    unreg.operations.push_back(AgencyOperation(
        "Current/DBServers/" + _myId, AgencySimpleOperationType::DELETE_OP));
  } else if (role == ServerState::ROLE_COORDINATOR) {
    unreg.operations.push_back(AgencyOperation(
        "Current/Coordinators/" + _myId, AgencySimpleOperationType::DELETE_OP));
  }

  // Unregister
  unreg.operations.push_back(
      AgencyOperation("Current/ServersRegistered/" + _myId,
                      AgencySimpleOperationType::DELETE_OP));

  comm.sendTransactionWithFailover(unreg, 120.0);

  while (_heartbeatThread->isRunning()) {
    usleep(50000);
  }
  
  AgencyComm::cleanup();
  ClusterComm::cleanup();
}