LCOV - code coverage report
Current view: top level - include/common/rpc - distributor.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 3 100.0 %
Date: 2024-04-23 00:09:24 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
       3             :   Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
       4             : 
       5             :   This software was partially supported by the
       6             :   EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
       7             : 
       8             :   This software was partially supported by the
       9             :   ADA-FS project under the SPPEXA project funded by the DFG.
      10             : 
      11             :   This file is part of GekkoFS.
      12             : 
      13             :   GekkoFS is free software: you can redistribute it and/or modify
      14             :   it under the terms of the GNU General Public License as published by
      15             :   the Free Software Foundation, either version 3 of the License, or
      16             :   (at your option) any later version.
      17             : 
      18             :   GekkoFS is distributed in the hope that it will be useful,
      19             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21             :   GNU General Public License for more details.
      22             : 
      23             :   You should have received a copy of the GNU General Public License
      24             :   along with GekkoFS.  If not, see <https://www.gnu.org/licenses/>.
      25             : 
      26             :   SPDX-License-Identifier: GPL-3.0-or-later
      27             : */
      28             : 
      29             : #ifndef GEKKOFS_RPC_DISTRIBUTOR_HPP
      30             : #define GEKKOFS_RPC_DISTRIBUTOR_HPP
      31             : 
      32             : #include "../include/config.hpp"
      33             : #include <vector>
      34             : #include <string>
      35             : #include <numeric>
      36             : #include <unordered_map>
      37             : #include <fstream>
      38             : #include <map>
      39             : 
      40             : namespace gkfs::rpc {
      41             : 
      42             : using chunkid_t = unsigned int;
      43             : using host_t = unsigned int;
      44             : 
      45         270 : class Distributor {
      46             : public:
      47             :     virtual host_t
      48             :     localhost() const = 0;
      49             : 
      50             :     virtual host_t
      51             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
      52             :                 const int num_copy) const = 0;
      53             :     // TODO: We need to pass hosts_size in the server side, because the number
      54             :     // of servers are not defined (in startup)
      55             : 
      56             :     virtual unsigned int
      57             :     hosts_size() const = 0;
      58             : 
      59             :     virtual host_t
      60             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
      61             :                 unsigned int hosts_size, const int num_copy) = 0;
      62             : 
      63             :     virtual host_t
      64             :     locate_file_metadata(const std::string& path, const int num_copy) const = 0;
      65             : 
      66             :     virtual std::vector<host_t>
      67             :     locate_directory_metadata(const std::string& path) const = 0;
      68             : };
      69             : 
      70             : 
      71             : class SimpleHashDistributor : public Distributor {
      72             : private:
      73             :     host_t localhost_;
      74             :     unsigned int hosts_size_{0};
      75             :     std::vector<host_t> all_hosts_;
      76             :     std::hash<std::string> str_hash;
      77             : 
      78             : public:
      79             :     SimpleHashDistributor();
      80             : 
      81             :     SimpleHashDistributor(host_t localhost, unsigned int hosts_size);
      82             : 
      83             :     unsigned int
      84             :     hosts_size() const override;
      85             : 
      86             :     host_t
      87             :     localhost() const override;
      88             : 
      89             :     host_t
      90             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
      91             :                 const int num_copy) const override;
      92             : 
      93             :     host_t
      94             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
      95             :                 unsigned int host_size, const int num_copy);
      96             : 
      97             :     host_t
      98             :     locate_file_metadata(const std::string& path,
      99             :                          const int num_copy) const override;
     100             : 
     101             :     std::vector<host_t>
     102             :     locate_directory_metadata(const std::string& path) const override;
     103             : };
     104             : 
     105             : class LocalOnlyDistributor : public Distributor {
     106             : private:
     107             :     host_t localhost_;
     108             :     unsigned int hosts_size_{0};
     109             : 
     110             : public:
     111             :     explicit LocalOnlyDistributor(host_t localhost);
     112             : 
     113             :     host_t
     114             :     localhost() const override;
     115             : 
     116             :     unsigned int
     117             :     hosts_size() const override;
     118             : 
     119             :     host_t
     120             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
     121             :                 const int num_copy) const override;
     122             : 
     123             :     host_t
     124             :     locate_file_metadata(const std::string& path,
     125             :                          const int num_copy) const override;
     126             : 
     127             :     std::vector<host_t>
     128             :     locate_directory_metadata(const std::string& path) const override;
     129             : };
     130             : 
     131          21 : class ForwarderDistributor : public Distributor {
     132             : private:
     133             :     host_t fwd_host_;
     134             :     unsigned int hosts_size_{0};
     135             :     std::vector<host_t> all_hosts_;
     136             :     std::hash<std::string> str_hash;
     137             : 
     138             : public:
     139             :     ForwarderDistributor(host_t fwhost, unsigned int hosts_size);
     140             : 
     141             :     host_t
     142             :     localhost() const override final;
     143             : 
     144             :     unsigned int
     145             :     hosts_size() const override;
     146             : 
     147             :     host_t
     148             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
     149             :                 const int num_copy) const override final;
     150             : 
     151             :     host_t
     152             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
     153             :                 unsigned int host_size, const int num_copy) override final;
     154             : 
     155             :     host_t
     156             :     locate_file_metadata(const std::string& path,
     157             :                          const int num_copy) const override;
     158             : 
     159             :     std::vector<host_t>
     160             :     locate_directory_metadata(const std::string& path) const override;
     161             : };
     162             : 
     163             : /*
     164             :  * Class IntervalSet
     165             :  * FROM
     166             :  *https://stackoverflow.com/questions/55646605/is-there-a-collection-for-storing-discrete-intervals
     167             :  **/
     168          15 : class IntervalSet {
     169             :     std::map<chunkid_t, chunkid_t> _intervals;
     170             : 
     171             : public:
     172             :     void Add(chunkid_t, chunkid_t);
     173             :     bool
     174             :     IsInsideInterval(unsigned int) const;
     175             : };
     176             : 
     177             : class GuidedDistributor : public Distributor {
     178             : private:
     179             :     host_t localhost_;
     180             :     unsigned int hosts_size_{0};
     181             :     std::vector<host_t> all_hosts_;
     182             :     std::hash<std::string> str_hash;
     183             :     std::unordered_map<std::string, std::pair<IntervalSet, unsigned int>>
     184             :             map_interval;
     185             :     std::vector<std::string> prefix_list; // Should not be very long
     186             :     bool
     187             :     init_guided();
     188             : 
     189             : public:
     190             :     GuidedDistributor();
     191             : 
     192             :     GuidedDistributor(host_t localhost, unsigned int hosts_size);
     193             : 
     194             :     host_t
     195             :     localhost() const override;
     196             : 
     197             :     unsigned int
     198             :     hosts_size() const override;
     199             : 
     200             :     host_t
     201             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
     202             :                 const int num_copy) const override;
     203             : 
     204             :     host_t
     205             :     locate_data(const std::string& path, const chunkid_t& chnk_id,
     206             :                 unsigned int host_size, const int num_copy);
     207             : 
     208             :     host_t
     209             :     locate_file_metadata(const std::string& path,
     210             :                          const int num_copy) const override;
     211             : 
     212             :     std::vector<host_t>
     213             :     locate_directory_metadata(const std::string& path) const override;
     214             : };
     215             : 
     216             : } // namespace gkfs::rpc
     217             : 
     218             : #endif // GEKKOFS_RPC_LOCATOR_HPP

Generated by: LCOV version 1.16