<commit_before>// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2011 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#include <errno.h>
#include <sys/wait.h>

#include <algorithm>
#include <csignal>
#include <string>

#include <glog/logging.h>
#include <pficommon/concurrent/lock.h>
#include <pficommon/lang/bind.h>

#include "jubavisor.hpp"
#include "jubatus/core/common/exception.hpp"
#include "../common/membership.hpp"
#include "../common/network.hpp"
#include "../common/util.hpp"

using pfi::concurrent::scoped_lock;
using pfi::lang::bind;

namespace jubatus {
namespace server {
namespace jubavisor {

using common::create_lock_service;
using common::build_loc_str;

namespace {
jubavisor* g_jubavisor = NULL;

// for GCC and Clang compatibility:
// to use pfi::lang::bind 'void (*)(int) __attribute__((noreturn))'
void exit_wrapper(int status) {
  exit(status);
}
}  // namespace

jubavisor::jubavisor(
    const std::string& hosts,
    int port,
    int max,
    const std::string& logfile)
    : zk_(create_lock_service("zk", hosts, 1024, logfile)),
      port_base_(port),
      logfile_(logfile),
      max_children_(max) {
  common::util::ignore_sigpipe();
  common::util::set_exit_on_term();
  ::atexit(jubavisor::atexit_);

  // handle SIG_CHLD
  struct sigaction sa;
  sa.sa_handler = sigchld_handler_;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
  if (sigaction(SIGCHLD, &sa, NULL) == -1) {
    throw JUBATUS_EXCEPTION(
      core::common::exception::runtime_error("failed sigaction(SIGCHLD)")
      << core::common::exception::error_errno(errno));
  }

  zk_->create(jubatus::server::common::JUBATUS_BASE_PATH, "");
  zk_->create(jubatus::server::common::JUBAVISOR_BASE_PATH, "");
  zk_->create(jubatus::server::common::ACTOR_BASE_PATH, "");

  name_ =
    build_loc_str(jubatus::server::common::get_default_v4_address(), port);
  std::string path = jubatus::server::common::JUBAVISOR_BASE_PATH + "/" + name_;
  zk_->create(path, "", true);
  // TODO(kumagi):
  //  pfi::lang::function<void(int,int,std::string)> f
  //    = bind(&jubavisor::die_if_deleted, this, _1, _2, _3);
  //  zk_->bind_watcher(path, f);
  DLOG(INFO) << path << " created.";

  for (unsigned int i = 0; i < max_children_; ++i) {
    port_pool_.push(++port_base_);
  }
<commit_msg>Fix jubavisor to block signals<commit_after>// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2011 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#include <errno.h>
#include <sys/wait.h>

#include <algorithm>
#include <csignal>
#include <string>

#include <glog/logging.h>
#include <pficommon/concurrent/lock.h>
#include <pficommon/lang/bind.h>

#include "jubavisor.hpp"
#include "jubatus/core/common/exception.hpp"
#include "../common/membership.hpp"
#include "../common/network.hpp"
#include "../common/util.hpp"

using pfi::concurrent::scoped_lock;
using pfi::lang::bind;

namespace jubatus {
namespace server {
namespace jubavisor {

using common::create_lock_service;
using common::build_loc_str;

namespace {
jubavisor* g_jubavisor = NULL;

// for GCC and Clang compatibility:
// to use pfi::lang::bind 'void (*)(int) __attribute__((noreturn))'
void exit_wrapper(int status) {
  exit(status);
}
}  // namespace

jubavisor::jubavisor(
    const std::string& hosts,
    int port,
    int max,
    const std::string& logfile)
    : port_base_(port),
      logfile_(logfile),
      max_children_(max) {
  common::util::ignore_sigpipe();
  common::util::set_exit_on_term();
  ::atexit(jubavisor::atexit_);

  zk_.reset(create_lock_service("zk", hosts, 1024, logfile));

  // handle SIG_CHLD
  struct sigaction sa;
  sa.sa_handler = sigchld_handler_;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
  if (sigaction(SIGCHLD, &sa, NULL) == -1) {
    throw JUBATUS_EXCEPTION(
      core::common::exception::runtime_error("failed sigaction(SIGCHLD)")
      << core::common::exception::error_errno(errno));
  }

  zk_->create(jubatus::server::common::JUBATUS_BASE_PATH, "");
  zk_->create(jubatus::server::common::JUBAVISOR_BASE_PATH, "");
  zk_->create(jubatus::server::common::ACTOR_BASE_PATH, "");

  name_ =
    build_loc_str(jubatus::server::common::get_default_v4_address(), port);
  std::string path = jubatus::server::common::JUBAVISOR_BASE_PATH + "/" + name_;
  zk_->create(path, "", true);
  // TODO(kumagi):
  //  pfi::lang::function<void(int,int,std::string)> f
  //    = bind(&jubavisor::die_if_deleted, this, _1, _2, _3);
  //  zk_->bind_watcher(path, f);
  DLOG(INFO) << path << " created.";

  for (unsigned int i = 0; i < max_children_; ++i) {
    port_pool_.push(++port_base_);
  }

{'fraction_non_alphanumeric': 0.1402618657937807, 'fraction_numerical': 0.0088379705400982, 'mean_word_length': 4.570647219690064, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1067447', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2014, 'n_words': 526}