Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7272281
  • 博文数量: 593
  • 博客积分: 13065
  • 博客等级: 上将
  • 技术积分: 10313
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-26 16:44
个人简介

推荐: blog.csdn.net/aquester https://github.com/eyjian https://www.cnblogs.com/aquester http://blog.chinaunix.net/uid/20682147.html

文章分类

全部博文(593)

分类: HADOOP

2018-11-12 19:40:37

默认zookeeper日志输出到stderr,
可以调用zoo_set_log_stream(FILE*)设置输出到文件中
还可以调用zoo_set_debug_level(ZooLogLevel)控制日志级别!!!

类CZookeeperHelper提供基于zookeeper的主备切换接口和读取数据等接口:


使用示例:

  1. class CMyApplication: public mooon::net::CZookeeperHelper
  2. {
  3. public:
  4.     CMyApplication(const char* data);
  5.     void stop() { _stop = true; }
  6.     void run();
  7.     void wait();

  8. private:
  9.     void work();

  10. private:
  11.     virtual void on_zookeeper_session_connected(const char* path);
  12.     virtual void on_zookeeper_session_connecting(const char* path);
  13.     virtual void on_zookeeper_session_expired(const char *path);
  14.     virtual void on_zookeeper_session_event(int state, const char *path);
  15.     virtual void on_zookeeper_event(int type, int state, const char *path);

  16. private:
  17.     volatile bool _stop;
  18.     std::string _master_path; 用来竞争master的zookeeper节点路径
  19.     std::string _master_data; 成功竞争为master时,写入_master_path的数据,主备应当提供不同的数据,以方便判断自己是否处于主状态
  20. };

  21. int main(int argc, char* argv[])
  22. {
  23.     try
  24.     {
  25.         mooon::sys::g_logger = mooon::sys::create_safe_logger();
  26.         const std::string zk_nodes = "127.0.0.1:2181";
  27.         const int session_timeout_seconds = 1;

  28.         CMyApplication myapp(argv[1]);
  29.         myapp.create_session(zk_nodes, session_timeout_seconds);
  30.         myapp.run();
  31.         myapp.wait();

  32.         return 0;
  33.     }
  34.     catch (mooon::sys::CSyscallException& ex)
  35.     {
  36.         fprintf(stderr, "%s\n", ex.str().c_str());
  37.         exit(1);
  38.     }
  39.     catch (mooon::utils::CException& ex)
  40.     {
  41.         fprintf(stderr, "%s\n", ex.str().c_str());
  42.         exit(1);
  43.     }
  44. }

  45. CMyApplication::CMyApplication(const char* data)
  46.     : _stop(false)
  47. {
  48.     _master_path = "/tmp/a";
  49.     if (data != NULL)
  50.         _master_data = data;
  51. }

  52. void CMyApplication::run()
  53. {
  54.     启动时竞争master,
  55.     在成为master之前不能进入工作状态
  56.     while (!_stop)
  57.     {
  58.         int zk_errcode;
  59.         std::string zk_errmsg;

  60.         if (race_master(_master_path, _master_data, &zk_errcode, &zk_errmsg))
  61.         {
  62.             成为master后,
  63.             要让原来的master有足够时间退出master状态
  64.             MYLOG_INFO("Race master at %s with %s successfully, sleep for 10 seconds to let the old master quit\n", _master_path.c_str(), _master_data.c_str());
  65.             mooon::sys::CUtils::millisleep(10000);
  66.             MYLOG_INFO("Start working now\n");

  67.             work();
  68.             if (!_stop)
  69.             {
  70.                 退出work(),表示需要重新竞争master
  71.                 MYLOG_INFO("Turn to slave from master at %s with %s successfully, stop working now\n", _master_path.c_str(), _master_data.c_str());
  72.             }
  73.         }
  74.         else
  75.         {
  76.             如果node_exists_exception()返回true,表示已有master,
  77.             即_master_path已存在,返回false为其它错误,应将错误信息记录到日志
  78.             if (node_exists_exception(zk_errcode))
  79.             {
  80.                 MYLOG_INFO("A master exists\n");
  81.             }
  82.             else
  83.             {
  84.                 MYLOG_ERROR("Race master at %s with %s failed: (state:%d)(errcode:%d)%s\n", _master_path.c_str(), _master_data.c_str(), get_state(), zk_errcode, zk_errmsg.c_str());
  85.                 if (invalid_handle_exception(zk_errcode))
  86.                 {
  87.                     MYLOG_INFO("To recreate session\n");
  88.                     recreate_session();
  89.                 }
  90.             }

  91.             休息2秒后再尝试,不要过频重试,一般情况下1~10秒都是可接受的
  92.             mooon::sys::CUtils::millisleep(2000);
  93.         }
  94.     }

  95.     MYLOG_INFO("Exit now\n");
  96. }

  97. void CMyApplication::wait()
  98. {
  99. }

  100. void CMyApplication::work()
  101. {
  102.     要及时检查is_connected(),以防止master失效后同时存在两个master
  103.     while (!_stop && !is_session_expired())
  104.     {
  105.         mooon::sys::CUtils::millisleep(2000);
  106.         MYLOG_INFO("Working with state:\033[1;33m%d\033[m ...\n", get_state());
  107.     }
  108. }

  109. void CMyApplication::on_zookeeper_session_connected(const char* path)
  110. {
  111.     MYLOG_INFO("[\033[1;33mon_zookeeper_session_connected\033[m] path: %s\n", path);

  112.     const std::string zk_parent_path = "";
  113.     const std::string zk_node_name = "test";
  114.     const std::string zk_node_data = "123";

  115.     try
  116.     {
  117.         create_node(zk_parent_path, zk_node_name, zk_node_data, ZOO_EPHEMERAL);
  118.         MYLOG_INFO("Create %s/%s ok\n", zk_parent_path.c_str(), zk_node_name.c_str());
  119.     }
  120.     catch (mooon::utils::CException& ex)
  121.     {
  122.         MYLOG_ERROR("Create %s/%s failed: %s\n", zk_parent_path.c_str(), zk_node_name.c_str(), ex.str().c_str());
  123.     }
  124. }

  125. void CMyApplication::on_zookeeper_session_connecting(const char* path)
  126. {
  127.     MYLOG_INFO("[\033[1;33mon_zookeeper_session_connecting\033[m] path: %s\n", path);
  128. }

  129. void CMyApplication::on_zookeeper_session_expired(const char *path)
  130. {
  131.     MYLOG_INFO("[\033[1;33mon_zookeeper_session_expired\033[m] path: %s\n", path);
  132.     //exit(1); 最安全的做法,在这里直接退出,通过重新启动方式再次竞争master
  133. }

  134. void CMyApplication::on_zookeeper_session_event(int state, const char *path)
  135. {
  136.     MYLOG_INFO("[\033[1;33mon_zookeeper_session_event\033[m][state:%d] path: %s\n", state, path);
  137. }

  138. void CMyApplication::on_zookeeper_event(int type, int state, const char *path)
  139. {
  140.     MYLOG_INFO("[\033[1;33mon_zookeeper_event\033[m][type:%d][state:%d] path: %s\n", type, state, path);

  141.     if (type == 3)
  142.     {
  143.         const int data_size = mooon::SIZE_4K;
  144.         const bool keep_watch = true;
  145.         std::string zk_data;
  146.         const int n = get_zk_data(path, &zk_data, data_size, keep_watch);
  147.         printf("(%d/%zd)%s\n", n, zk_data.size(), zk_data.c_str());
  148.     }
  149. }



阅读(3557) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~