| 23 | namespace CDNSimulator { |
| 24 | |
| 25 | class CacheCluster { |
| 26 | private: |
| 27 | |
| 28 | // the consistent hash ring |
| 29 | ring_t *_ring = nullptr; |
| 30 | |
| 31 | // the capacity of each server, used to assign requests to servers |
| 32 | // server with larger weight will be assigned more requests |
| 33 | std::vector<double> _server_weights_vec; |
| 34 | |
| 35 | // the cache servers in this cluster |
| 36 | std::vector<CacheServer> _cache_servers_vec; |
| 37 | |
| 38 | public: |
| 39 | unsigned long cluster_id; |
| 40 | |
| 41 | // void get_all_cluster_stored_obj(); |
| 42 | // void get_all_server_stored_obj(); |
| 43 | |
| 44 | CacheCluster(const CacheCluster &other) = delete; |
| 45 | |
| 46 | CacheCluster(CacheCluster &&other) = default; |
| 47 | |
| 48 | CacheCluster(unsigned long cluster_id = 0) { |
| 49 | this->cluster_id = cluster_id; |
| 50 | this->_cache_servers_vec.reserve(128); |
| 51 | // this->_hasher = myHasher(n_server); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @brief get a request from the cluster, if the requested object is not |
| 56 | * cached, the object will be loaded from the origin server |
| 57 | * |
| 58 | */ |
| 59 | bool get(request_t *req); |
| 60 | |
| 61 | /** |
| 62 | * @brief add a cache server to the cluster, return the index of the server |
| 63 | * |
| 64 | * @param cache_server |
| 65 | * @return uint64_t |
| 66 | */ |
| 67 | inline uint64_t add_server(CacheServer &&cache_server, |
| 68 | double server_weight = 1.00) { |
| 69 | this->_cache_servers_vec.push_back(std::move(cache_server)); |
| 70 | this->_server_weights_vec.push_back(server_weight); |
| 71 | |
| 72 | // normalize the server weights |
| 73 | std::vector<double> server_normalized_weights_vec; |
| 74 | double sum = std::accumulate(this->_server_weights_vec.begin(), |
| 75 | this->_server_weights_vec.end(), 0.0); |
| 76 | |
| 77 | for (auto weight : this->_server_weights_vec) { |
| 78 | server_normalized_weights_vec.push_back(weight / sum); |
| 79 | } |
| 80 | |
| 81 | if (this->_ring != nullptr) { |
| 82 | ch_ring_destroy_ring(this->_ring); |