Beispiel #1
0
static apr_status_t close_zookeeper_shmlog(void *data)
{
  if(log_pool != NULL) {
    zoo_set_log_stream(NULL);
    log_pool = NULL;
    log_stream = NULL;
  }
  return APR_SUCCESS;
}
Beispiel #2
0
static apr_status_t close_zookeeper_logfile(void *data)
{
  if(log_pool != NULL) {
    log_pool = NULL;
    log_stream = NULL;
    zoo_set_log_stream(NULL);
    apr_file_close((apr_file_t*)data);
  }
  return APR_SUCCESS;
}
Beispiel #3
0
  zktool_set_logfile(const char *filename, apr_pool_t *p)
{
  apr_status_t st;
  apr_file_t *f = NULL;
  apr_pool_t *pool;
  FILE *fp;
  apr_os_file_t fd;

  if(filename == NULL) {
    if(log_pool != NULL)
      apr_pool_destroy(log_pool);
    else
      zoo_set_log_stream(NULL);
    return APR_SUCCESS;
  }

  if(p == NULL)
    pool = zeke_root_subpool_create();
  else
    assert(apr_pool_create(&pool,p) == APR_SUCCESS);

  st = apr_file_open(&f, filename, ZOO_LOG_FLAGS, APR_OS_DEFAULT, pool);
  if(st == APR_SUCCESS) {
    apr_file_inherit_unset(f);
    if((st = apr_os_file_get(&fd,f)) == APR_SUCCESS) {
      fp = fdopen(fd,"a");
      if(fp == NULL) {
        st = apr_get_os_error();
      } else {
        zoo_set_log_stream(fp);
        log_pool = pool;
        apr_pool_cleanup_register(pool,f,close_zookeeper_logfile,close_zookeeper_logfile);
      }
    }
  }

  if(st != APR_SUCCESS && f != NULL)
    apr_file_close(f);
  return st;
}
Beispiel #4
0
 int ZKAgent::Init(ArdbServer* serv)
 {
     m_server = serv;
     ArdbServerConfig& cfg = serv->m_cfg;
     if (cfg.zookeeper_servers.empty())
     {
         WARN_LOG("No zookeeper servers specified, zookeeper agent would not start.");
         return 0;
     }
     ZooLogLevel z_log_loevel;
     if (DEBUG_ENABLED())
     {
         z_log_loevel = ZOO_LOG_LEVEL_DEBUG;
     }
     else if (INFO_ENABLED())
     {
         z_log_loevel = ZOO_LOG_LEVEL_INFO;
     }
     else if (WARN_ENABLED())
     {
         z_log_loevel = ZOO_LOG_LEVEL_WARN;
     }
     else if (ERROR_ENABLED())
     {
         z_log_loevel = ZOO_LOG_LEVEL_ERROR;
     }
     else
     {
         z_log_loevel = ZOO_LOG_LEVEL_DEBUG;
     }
     zoo_set_debug_level(z_log_loevel);
     zoo_set_log_stream(ArdbLogger::GetLogStream());
     std::string zkidfile = cfg.home + "/.zkcid";
     FILE* idfile = fopen(zkidfile.c_str(), "r");
     if (idfile != NULL)
     {
         if (fread(&m_zk_clientid, sizeof(m_zk_clientid), 1, idfile) != 1)
         {
             memset(&m_zk_clientid, 0, sizeof(m_zk_clientid));
         }
         fclose(idfile);
     }
     Reconnect();
     return 0;
 }
Beispiel #5
0
zk::zk(const string& hosts, int timeout, const string& logfile)
    : zh_(NULL),
      hosts_(hosts),
      logfilep_(NULL) {
  if (logfile != "") {
    logfilep_ = fopen(logfile.c_str(), "a+");
    if (!logfilep_) {
      throw JUBATUS_EXCEPTION(
          jubatus::exception::runtime_error("cannot open zk logfile")
          << jubatus::exception::error_file_name(logfile.c_str())
          << jubatus::exception::error_errno(errno)
          << jubatus::exception::error_api_func("fopen"));
    }
    zoo_set_log_stream(logfilep_);
  }

  zh_ = zookeeper_init(hosts.c_str(), NULL, timeout * 1000, 0, NULL, 0);
  if (!zh_) {
    perror("");
    throw JUBATUS_EXCEPTION(
        jubatus::exception::runtime_error("failed to initialize zk: " + hosts)
        << jubatus::exception::error_api_func("zookeeper_init")
        << jubatus::exception::error_errno(errno));
  }

  // sleep the state got not ZOO_CONNECTING_STATE
  while ((state_ = zoo_state(zh_)) == ZOO_CONNECTING_STATE) {
    usleep(100);
  }

  if (is_unrecoverable(zh_) == ZINVALIDSTATE) {
    throw JUBATUS_EXCEPTION(
        jubatus::exception::runtime_error("cannot connect zk:" + hosts)
        << jubatus::exception::error_api_func("is_unrecoverable")
        << jubatus::exception::error_message(zerror(errno)));
  }

  zoo_set_context(zh_, this);
  zoo_set_watcher(zh_, mywatcher);
}
Beispiel #6
0
  zktool_enable_shmlog(apr_pool_t *p)
{
  apr_status_t st = APR_SUCCESS;
  zeke_shmlog_header_t *shm;
  apr_pool_t *pool = NULL;
  FILE *fp = NULL;

  if(p == NULL)
    pool = zeke_root_subpool_create();
  else
    assert(apr_pool_create(&pool,p) == APR_SUCCESS);

  fp = zeke_shmlog_stream_open(&shm,pool);
  if(fp == NULL) {
    st = apr_get_os_error();
    apr_pool_destroy(pool);
    pool = NULL;
  } else {
    assert(shm != NULL);
    st = zeke_shmlog_lock(shm);
    if(st == APR_SUCCESS) {
      if(log_pool != NULL)
        apr_pool_destroy(log_pool);
      zoo_set_log_stream(fp);
      log_pool = pool;
      apr_pool_cleanup_register(pool,fp,close_zookeeper_shmlog,
                                        close_zookeeper_shmlog);
    }
  }
  if(st != APR_SUCCESS) {
    if(fp != NULL)
      fclose(fp);
    if(pool != NULL)
      apr_pool_destroy(pool);
  }
  return st;
}
Beispiel #7
0
void check_conf()
{
    int res;
    char *err = "";
    char buf[ZR_MAX_HOSTNAME_LEN+1];
    
    // ckeck mandatory options
    if(server.svc_count == 0)
    {
        err = "No any valid service configure, you must set service at least one.";
        goto checkerr;
    }

    /*
    // below block should be in last of mandatory
    if(server.req_log_sock == NULL)
    {
        server.req_log = 0;
        log_info("Request log domain socket disable.");
    }else
    {
        // Validate domain socket
        sds dname = sdsdup(server.req_log_sock);
        dname = dirname(dname);
        if(access(dname, F_OK) < 0)
        {
            err = sdscatprintf(sdsempty(), "Permission denied for req-log-sock: %s, %s", dname, strerror(errno));
            goto checkerr;
        }

        struct stat st;
        res = stat(server.req_log_sock, &st);
        if(res == 0)
        {
            log_debug("req-log-sock:%s", server.req_log_sock);
            err = sdscatprintf(sdsempty(), "Request log socket already in use: %s", server.req_log_sock);
            goto checkerr;
        }
        sdsfree(dname);
    }
    // end ckeck mandatory options
    */

    log_set_level(server.log_level);
    z_set_log_level(server.log_level);
    zoo_set_log_stream(log_get_out());

    // check optional options
    if(server.name == NULL)
    {
        res = gethostname(buf, ZR_MAX_HOSTNAME_LEN);
        if(res < 0)
        {
            err = sdscatprintf(sdsempty(), "Cann't get hostname %s", strerror(errno));
            goto checkerr;
        }

        server.name = sdscatprintf(sdsnew(buf), "-%d", getpid());
        log_info("Set name by default: %s", server.name);
    }

    if(server.bind_addr_count == 0)
    {
        server.bind_addr_count = 1;
        server.bind_arr[0].addr_str = sdsnew("INADDR_ANY");
        server.bind_arr[0].addr.sin_family = AF_INET;
        server.bind_arr[0].addr.sin_addr.s_addr = htonl(INADDR_ANY);

        log_info("Set bind by default: %s", "INADDR_ANY");
    }

    if(server.port == 0)
    {
        server.port = ZR_PORT;
        log_info("Set port by default: %d", server.port);
    }

    if(server.client_max == 0)
    {
        server.client_max = ZR_CONN_MAX;
        log_info("Set connection-max by default: %d", server.client_max);
    }

    if(server.tcp_backlog == 0)
    {
        server.tcp_backlog = 500;
        log_info("Set tcp-backlog by default: %d", server.tcp_backlog);
    }

    if(server.z_health_conn == NULL)
    {
        server.z_health = 0;
    }else
    {
        server.z_health = 1;
        server.z_health_node = sdscatprintf(sdsempty(), "%s/%s", server.z_health_conn->node, server.name);
    }

    if(server.zoo_timeout == 0)
    {
        server.zoo_timeout = ZR_ZOO_TIMEOUT;
    }

    if(server.req_log_uds == NULL)
    {
        server.req_log_uds = sdsnew(ZR_DEFAULT_REQ_LOG_UDS);
    }

    return;

checkerr:
    log_eerr("CONFIG FILE CHECK ERROR.");
    log_eerr("%s", err);
    exit(1);
}
Beispiel #8
0
void zu_set_log_stream(FILE *fd)
{
    zoo_set_log_stream(fd);
}
Beispiel #9
0
static VALUE method_set_log_stream(VALUE self, VALUE stream) {
  // convert stream to FILE*
  FILE *fp_stream = (FILE*)stream;
  zoo_set_log_stream(fp_stream);
  return Qnil;
}