Example #1
0
void Session::internal_connect() {
  if (hosts_.empty()) { // No hosts lock necessary (only called on session thread)
    notify_connect_error(CASS_ERROR_LIB_NO_HOSTS_AVAILABLE,
                         "No hosts provided or no hosts resolved");
    return;
  }
  control_connection_.connect(this);
}
Example #2
0
void Session::on_control_connection_ready() {
  // No hosts lock necessary (only called on session thread and read-only)
  config().load_balancing_policy()->init(control_connection_.connected_host(), hosts_, random_.get());
  config().load_balancing_policy()->register_handles(loop());
  for (IOWorkerVec::iterator it = io_workers_.begin(),
       end = io_workers_.end(); it != end; ++it) {
    (*it)->set_protocol_version(control_connection_.protocol_version());
  }
  for (HostMap::iterator it = hosts_.begin(), hosts_end = hosts_.end();
       it != hosts_end; ++it) {
    on_add(it->second, true);
  }
  if (pending_pool_count_ == 0) {
    notify_connect_error(CASS_ERROR_LIB_NO_HOSTS_AVAILABLE,
                         "No hosts available for connection using the current load balancing policy");
  }
  if (config().core_connections_per_host() == 0) {
    // Special case for internal testing. Not allowed by API
    LOG_DEBUG("Session connected with no core IO connections");
  }
}
Example #3
0
void Session::on_control_connection_error(CassError code, const std::string& message) {
  notify_connect_error(code, message);
}
Example #4
0
void Session::on_event(const SessionEvent& event) {
  switch (event.type) {
    case SessionEvent::CONNECT: {
      int port = config_.port();

      const ContactPointList& contact_points = config_.contact_points();
      for (ContactPointList::const_iterator it = contact_points.begin(),
                                                    end = contact_points.end();
           it != end; ++it) {
        const std::string& seed = *it;
        Address address;
        if (Address::from_string(seed, port, &address)) {
          add_host(address);
        } else {
          pending_resolve_count_++;
          Resolver::resolve(loop(), seed, port, this, on_resolve);
        }
      }

      if (pending_resolve_count_ == 0) {
        internal_connect();
      }

      break;
    }

    case SessionEvent::NOTIFY_READY:
      if (pending_pool_count_ > 0) {
        if (--pending_pool_count_ == 0) {
          LOG_DEBUG("Session is connected");
          notify_connected();
        }
        LOG_DEBUG("Session pending pool count %d", pending_pool_count_);
      }
      break;

    case SessionEvent::NOTIFY_KEYSPACE_ERROR: {
      // Currently, this is only called when the keyspace does not exist
      // and not for any other keyspace related errors.
      const CopyOnWritePtr<std::string> keyspace(keyspace_);
      notify_connect_error(CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE,
                           "Keyspace '" + *keyspace + "' does not exist");
      break;
    }

    case SessionEvent::NOTIFY_WORKER_CLOSED:
      if (--pending_workers_count_ == 0) {
        LOG_DEBUG("Session is disconnected");
        control_connection_.close();
        close_handles();
      }
      break;

    case SessionEvent::NOTIFY_UP:
      control_connection_.on_up(event.address);
      break;

    case SessionEvent::NOTIFY_DOWN:
      control_connection_.on_down(event.address);
      break;

    default:
      assert(false);
      break;
  }
}
Example #5
0
void Session::on_event(const SessionEvent& event) {
  switch (event.type) {
    case SessionEvent::CONNECT: {
      int port = config_.port();

      // This needs to be done on the session thread because it could pause
      // generating a new random seed.
      if (config_.use_randomized_contact_points()) {
        random_.reset(new Random());
      }

      MultiResolver<Session*>::Ptr resolver(
            new MultiResolver<Session*>(this, on_resolve,
#if UV_VERSION_MAJOR >= 1
                                        on_resolve_name,
#endif
                                        on_resolve_done));

      const ContactPointList& contact_points = config_.contact_points();
      for (ContactPointList::const_iterator it = contact_points.begin(),
                                                    end = contact_points.end();
           it != end; ++it) {
        const std::string& seed = *it;
        Address address;
        if (Address::from_string(seed, port, &address)) {
#if UV_VERSION_MAJOR >= 1
          if (config_.use_hostname_resolution()) {
            resolver->resolve_name(loop(), address, config_.resolve_timeout_ms());
          } else {
#endif
            add_host(address);
#if UV_VERSION_MAJOR >= 1
          }
#endif
        } else {
          resolver->resolve(loop(), seed, port, config_.resolve_timeout_ms());
        }
      }

      break;
    }

    case SessionEvent::NOTIFY_READY:
      if (pending_pool_count_ > 0) {
        if (--pending_pool_count_ == 0) {
          LOG_DEBUG("Session is connected");
          notify_connected();
        }
        LOG_DEBUG("Session pending pool count %d", pending_pool_count_);
      }
      break;

    case SessionEvent::NOTIFY_KEYSPACE_ERROR: {
      // Currently, this is only called when the keyspace does not exist
      // and not for any other keyspace related errors.
      notify_connect_error(CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE,
                           "Keyspace '" + keyspace() + "' does not exist");
      break;
    }

    case SessionEvent::NOTIFY_WORKER_CLOSED:
      if (--pending_workers_count_ == 0) {
        LOG_DEBUG("Session is disconnected");
        control_connection_.close();
        close_handles();
      }
      break;

    case SessionEvent::NOTIFY_UP:
      control_connection_.on_up(event.address);
      break;

    case SessionEvent::NOTIFY_DOWN:
      control_connection_.on_down(event.address);
      break;

    default:
      assert(false);
      break;
  }
}