TFS源码解析七

10 篇文章 0 订阅

NameServer中的layout_manager.h


#ifndef TFS_NAMESERVER_LAYOUT_MANAGER_H_

#define TFS_NAMESERVER_LAYOUT_MANAGER_H_

 

#include <pthread.h>//线程头文件

#include <Timer.h>//tbsys中定时器头文件

#include "gc.h" //数据基类GCObject管理头文件

#include "common/lock.h" //封装读写锁头文件

#include "block_collect.h" //封装Block数据头文件

#include "server_collect.h" //封装Server数据头文件

#include "common/base_packet.h" //网络包头文件

#include "oplog_sync_manager.h" //日志管理头文件

#include "client_request_server.h" //客户端请求处理头文件

#include "task_manager.h" //处理task数据头文件

#include "server_manager.h" //处理server数据头文件

#include "block_manager.h" //处理block数据头文件

#include "family_manager.h" //处理family数据头文件

#include "common/tfs_vector.h" //定义tfs_vector头文件

 

#ifdef TFS_GTEST

#include <gtest/gtest.h>

#endif

 

namespace tfs

{

  namespace nameserver

  {

    class NameServer;

    class LayoutManager

    {

      #ifdef TFS_GTEST

      friend class LayoutManagerTest;

      FRIEND_TEST(LayoutManagerTest, update_relation);

      FRIEND_TEST(LayoutManagerTest, update_block_info);

      FRIEND_TEST(LayoutManagerTest, repair);

      FRIEND_TEST(LayoutManagerTest, scan_replicate_queue_);

      FRIEND_TEST(LayoutManagerTest, scan_illegal_block_);

      FRIEND_TEST(LayoutManagerTest, build_replicate_task_);

      FRIEND_TEST(LayoutManagerTest, build_compact_task_);

      FRIEND_TEST(LayoutManagerTest, build_balance_task_);

      FRIEND_TEST(LayoutManagerTest, build_redundant_);

      #endif

      friend class ClientRequestServer;

      friend int OpLogSyncManager::replay_helper_do_oplog(const time_t now,

 const int32_t type, const char* const data,

                 const int64_t length , int64_t& pos);

      typedef common::TfsSortedVector<ServerCollect*,ServerIdCompare> SERVER_TABLE;

      typedef SERVER_TABLE::iterator SERVER_TABLE_ITER;

      public:

      explicit LayoutManager(NameServer& manager);

      virtual ~LayoutManager();

 

      int initialize();

      void wait_for_shut_down();

      void destroy();

 

      inline NameServer& get_name_server() { return manager_;}

 

      inline ClientRequestServer& get_client_request_server() { return client_request_server_;}

 

      inline BlockManager& get_block_manager() { return block_manager_;}

 

      inline ServerManager& get_server_manager() { return server_manager_;}

 

      inline TaskManager& get_task_manager() { return task_manager_;}

 

      inline OpLogSyncManager& get_oplog_sync_mgr() { return oplog_sync_mgr_;}

 

      inline GCObjectManager& get_gc_manager() { return gc_manager_;}

 

      inline FamilyManager& get_family_manager() { return family_manager_;}

 

      int update_relation(std::vector<uint32_t>& expires, ServerCollect* server,

          const std::set<common::BlockInfoExt>& blocks, const time_t now,

 const int8_t type);

      int build_relation(BlockCollect* block, ServerCollect* server, 

const time_t now, const bool set = false);

      bool relieve_relation(BlockCollect* block, ServerCollect* server,

 const time_t now);

      bool relieve_relation(BlockCollect* block, const uint64_t server, 

const time_t now);

      bool relieve_relation(const uint32_t block, ServerCollect* server,

 const time_t now);

      bool relieve_relation(const uint32_t block, const uint64_t server, 

const time_t now);

 

      int update_block_info(const common::BlockInfo& info, const uint64_t server,

 const time_t now, const bool addnew);

 

      int repair(char* msg, const int32_t lenght, const uint32_t block_id,

          const uint64_t server, const int32_t flag, const time_t now);

 

      int scan(common::SSMScanParameter& stream);

 

      int handle_task_complete(common::BasePacket* msg);

 

      int open_helper_create_new_block_by_id(const uint32_t block_id);

 

      int block_oplog_write_helper(const int32_t cmd, const common::BlockInfo& info,

          const std::vector<uint32_t>& blocks, 

const std::vector<uint64_t>& servers,

                                      const time_t now);

 

      int set_runtime_param(const uint32_t index, const uint32_t value, 

const int64_t length, char *retstr);

 

      void switch_role(const time_t now = common::Func::get_monotonic_time());

 

      uint32_t get_alive_block_id();

      private:

      void rotate_(const time_t now);

      uint32_t get_alive_block_id_();

      void build_();

      void balance_();

      void timeout_();

      void redundant_();

      void load_family_info_();

      void check_all_server_isalive_();

      void add_block_in_all_server_();

      void check_all_server_report_block_();

      int touch_(bool& promote, const common::ArrayHelper<ServerCollect*>& servers, const time_t now);

 

      int add_new_block_helper_write_log_(const uint32_t block_id, 

const common::ArrayHelper<uint64_t>& server, const time_t now);

      int add_new_block_helper_send_msg_(const uint32_t block_id, 

const common::ArrayHelper<uint64_t>& servers);

      int add_new_block_helper_build_relation_(BlockCollect* block, 

const common::ArrayHelper<uint64_t>& server, const time_t now);

      BlockCollect* add_new_block_(uint32_t& block_id, ServerCollect* server = NULL, const time_t now = common::Func::get_monotonic_time());

      BlockCollect* add_new_block_helper_create_by_id_(const uint32_t block_id,

 const time_t now);

      BlockCollect* add_new_block_helper_create_by_system_(uint32_t& block_id, ServerCollect* server, const time_t now);

 

      bool scan_replicate_queue_(int64_t& need, const time_t now);

      bool scan_reinstate_or_dissolve_queue_(int64_t& need, const time_t now);

      bool scan_illegal_block_(common::ArrayHelper<uint32_t>& result, 

const int32_t count, const time_t now);

      bool build_replicate_task_(int64_t& need, const BlockCollect* block,

 const time_t now);

      int build_compact_task_(const BlockCollect* block, const time_t now);

      bool build_balance_task_(int64_t& need, common::TfsSortedVector<ServerCollect*,ServerIdCompare>& targets,

           const ServerCollect* source, const BlockCollect* block, const time_t now);

      bool build_reinstate_task_(int64_t& need, const FamilyCollect* family,

          const common::ArrayHelper<common::FamilyMemberInfo>& reinstate_members, const time_t now);

      bool build_dissolve_task_(int64_t& need, const FamilyCollect* family,

          const common::ArrayHelper<common::FamilyMemberInfo>& reinstate_members, const time_t now);

      bool build_redundant_(int64_t& need, const time_t now);

      int build_marshalling_(int64_t& need, const time_t now);

      int64_t has_space_in_task_queue_() const;

 

      bool scan_block_(common::ArrayHelper<BlockCollect*>& results, int64_t& need, uint32_t& start, const int32_t max_query_block_num, const time_t now,

          const bool compact_time, const bool marshalling_time);

 

      bool scan_family_(common::ArrayHelper<FamilyCollect*>& results, int64_t& need, int64_t& startconst int32_t max_query_family_num, 

const time_t now, const bool compact_time);

 

      //以下为定义对应的处理线程,NameServer初始化时创建

 

      //Build线程

      class BuildPlanThreadHelper: public tbutil::Thread

      {

        public:

          explicit BuildPlanThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE * 2);}

          virtual ~BuildPlanThreadHelper(){}

          void run();

        private:

          DISALLOW_COPY_AND_ASSIGN(BuildPlanThreadHelper);

          LayoutManager& manager_;

      };

      typedef tbutil::Handle<BuildPlanThreadHelper> BuildPlanThreadHelperPtr;

      //Run线程

      class RunPlanThreadHelper : public tbutil::Thread

      {

        public:

          explicit RunPlanThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE);}

          virtual ~RunPlanThreadHelper(){}

          void run();

        private:

          DISALLOW_COPY_AND_ASSIGN(RunPlanThreadHelper);

          LayoutManager& manager_;

      };

      typedef tbutil::Handle<RunPlanThreadHelper> RunPlanThreadHelperPtr;

     //检查DataServer线程

      class CheckDataServerThreadHelper: public tbutil::Thread

      {

        public:

          explicit CheckDataServerThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE);}

          virtual ~CheckDataServerThreadHelper(){}

          void run();

        private:

          LayoutManager& manager_;

          DISALLOW_COPY_AND_ASSIGN(CheckDataServerThreadHelper);

      };

      typedef tbutil::Handle<CheckDataServerThreadHelper> CheckDataServerThreadHelperPtr;

      //增加Block信息线程

      class AddBlockInAllServerThreadHelper : public tbutil::Thread

      {

        public:

          explicit AddBlockInAllServerThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE);}

          virtual ~AddBlockInAllServerThreadHelper() {}

          void run();

        private:

          LayoutManager& manager_;

          DISALLOW_COPY_AND_ASSIGN(AddBlockInAllServerThreadHelper);

      };

      typedef tbutil::Handle<AddBlockInAllServerThreadHelper> AddBlockInAllServerThreadHelperPtr;

     //检查DataServer报告的Block信息线程

      class CheckDataServerReportBlockThreadHelper: public tbutil::Thread

      {

        public:

          explicit CheckDataServerReportBlockThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE);}

          virtual ~CheckDataServerReportBlockThreadHelper() {}

          void run();

        private:

          LayoutManager& manager_;

          DISALLOW_COPY_AND_ASSIGN(CheckDataServerReportBlockThreadHelper);

      };

      typedef tbutil::Handle<CheckDataServerReportBlockThreadHelper> CheckDataServerReportBlockThreadHelperPtr;

      //负载平衡线程

      class BuildBalanceThreadHelper: public tbutil::Thread

      {

        public:

          explicit BuildBalanceThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE);}

          virtual ~BuildBalanceThreadHelper() {}

          void run();

        private:

          LayoutManager& manager_;

          DISALLOW_COPY_AND_ASSIGN(BuildBalanceThreadHelper);

      };

      typedef tbutil::Handle<BuildBalanceThreadHelper> BuildBalanceThreadHelperPtr;

      //超时线程

      class TimeoutThreadHelper: public tbutil::Thread

      {

        public:

          explicit TimeoutThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE);}

          virtual ~TimeoutThreadHelper() {}

          void run();

        private:

          LayoutManager& manager_;

          DISALLOW_COPY_AND_ASSIGN(TimeoutThreadHelper);

      };

      typedef tbutil::Handle<TimeoutThreadHelper> TimeoutThreadHelperPtr;

      //去除冗余线程

      class RedundantThreadHelper: public tbutil::Thread

      {

        public:

          explicit RedundantThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE);}

          virtual ~RedundantThreadHelper() {}

          void run();

        private:

          LayoutManager& manager_;

          DISALLOW_COPY_AND_ASSIGN(RedundantThreadHelper);

      };

      typedef tbutil::Handle<RedundantThreadHelper> RedundantThreadHelperPtr;

      class LoadFamilyInfoThreadHelper: public tbutil::Thread

      {

        public:

          explicit LoadFamilyInfoThreadHelper(LayoutManager& manager):

            manager_(manager) {start(THREAD_STATCK_SIZE);}

          virtual ~LoadFamilyInfoThreadHelper() {}

          void run();

        private:

          LayoutManager& manager_;

          DISALLOW_COPY_AND_ASSIGN(LoadFamilyInfoThreadHelper);

      };

      typedef tbutil::Handle<LoadFamilyInfoThreadHelper> LoadFamilyInfoThreadHelperPtr;

 

      private:

      //定义各个处理线程的变量

      BuildPlanThreadHelperPtr build_plan_thread_;

      RunPlanThreadHelperPtr run_plan_thread_;

      CheckDataServerThreadHelperPtr check_dataserver_thread_;

      AddBlockInAllServerThreadHelperPtr add_block_in_all_server_thread_;

      CheckDataServerReportBlockThreadHelperPtr check_dataserver_report_block_thread_;

      BuildBalanceThreadHelperPtr balance_thread_;

      TimeoutThreadHelperPtr timeout_thread_;

      RedundantThreadHelperPtr redundant_thread_;

      LoadFamilyInfoThreadHelperPtr load_family_info_thread_;

 

      time_t  zonesec_;

      time_t  last_rotate_log_time_;

      int32_t plan_run_flag_;

 

      NameServer& manager_;

      BlockManager block_manager_;

      ServerManager server_manager_;

      TaskManager task_manager_;

      OpLogSyncManager oplog_sync_mgr_;

      ClientRequestServer client_request_server_;

      GCObjectManager gc_manager_;

      FamilyManager  family_manager_;

    };

  }/** end namespace nameserver **/

}/** end namespace tfs **/

 

#endif /* LAYOUTMANAGER_H_ */


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值