Exemplo n.º 1
0
Future<bool> ZooKeeperSlavesManagerStorage::remove(const string& hostname, uint16_t port)
{
  // TODO(benh): Use ZooKeeperSlavesManagerStorage::parse to clean up code.
  int ret;
  string result;
  Stat stat;

  ret = zk->get(znode, true, &result, &stat);

  if (ret != ZOK) {
    LOG(WARNING) << "Slaves manager storage failed to get '" << znode
                 << "' in ZooKeeper! (" << zk->message(ret) << ")";
    return false;
  }

  ostringstream out;
  out << hostname << ":" << port;

  size_t index = result.find(out.str());

  if (index == string::npos) {
    LOG(WARNING) << "Slaves manager storage could not remove slave "
		 << hostname << ":" << port
                 << " because not currently active or inactive";
    return false;
  } else if (index == 0) {
    LOG(WARNING) << "Bad data in '" << znode;
    return false;
  }

  if (result[index - 1] == '=') {
    if (result[index + out.str().size()] == '\n') {
      result.erase(index, out.str().size());
    } else {
      result.erase(index, out.str().size() + 1);
    }
  } else {
    result.erase(index - 1, out.str().size() + 1);
  }

  // Set the data in the znode.
  ret = zk->set(znode, result, stat.version);

  if (ret != ZOK) {
    LOG(WARNING) << "Slaves manager storage could not remove slave "
		 << hostname << ":" << port
                 << " from '" << znode << "' in ZooKeeper! ("
                 << zk->message(ret) << ")";
    return false;
  }

  return true;
}
Exemplo n.º 2
0
Future<bool> ZooKeeperSlavesManagerStorage::add(const string& hostname, uint16_t port)
{
  // TODO(benh): Use ZooKeeperSlavesManagerStorage::parse to clean up code.
  int ret;
  string result;
  Stat stat;

  ret = zk->get(znode, true, &result, &stat);

  if (ret != ZOK) {
    LOG(WARNING) << "Slaves manager storage failed to get '" << znode
                 << "' in ZooKeeper! (" << zk->message(ret) << ")";
    return false;
  }

  ostringstream out;
  out << hostname << ":" << port;

  if (result.size() == 0) {
    out << "active=" << hostname << ":" << port << "\n";
    out << "inactive=\n";
    result = out.str();
  } else {
    const string active("active=");

    size_t index = result.find(active);

    if (index == string::npos) {
      LOG(WARNING) << "Slaves manager storage found bad data in '" << znode
                   << "', could not find 'active='";
      return false;
    }

    if (result[index + active.size()] != '\n') {
      out << ",";
    }

    result.insert(index + active.size(), out.str());
  }

  // Set the data in the znode.
  ret = zk->set(znode, result, stat.version);

  if (ret != ZOK) {
    LOG(WARNING) << "Slaves manager storage could not add slave "
		 << hostname << ":" << port
                 << " to '" << znode << "' in ZooKeeper! ("
                 << zk->message(ret) << ")";
    return false;
  }

  return true;
}