当前位置: 首页>>代码示例>>C++>>正文


C++ Entry::convertToString方法代码示例

本文整理汇总了C++中Entry::convertToString方法的典型用法代码示例。如果您正苦于以下问题:C++ Entry::convertToString方法的具体用法?C++ Entry::convertToString怎么用?C++ Entry::convertToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Entry的用法示例。


在下文中一共展示了Entry::convertToString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Node

/**
 * FUNCTION NAME: stabilizationProtocol
 *
 * DESCRIPTION: This runs the stabilization protocol in case of Node joins and leaves
 * 				It ensures that there always 3 copies of all keys in the DHT at all times
 * 				The function does the following:
 *				1) Ensures that there are three "CORRECT" replicas of all the keys in spite of failures and joins
 *				Note:- "CORRECT" replicas implies that every key is replicated in its two neighboring nodes in the ring
 */
void MP2Node::stabilizationProtocol() {
	/*
	 * Implement this
	 */
    cout << "In stablization " << endl;

    vector<Node> new_replicas, new_bosses;
    Node this_node = Node(getMemberNode()->addr); // Current Node

    for (int i = 0; i < ring.size(); i++) {

        if (isNodeSame(ring[i], this_node)) { // When we find the current node, assign the new replicas and predecessors(bosses) using same technique used in assignReplicationNodes()
            if (i == 0) {
                new_bosses.push_back(Node(*ring[ring.size() - 1].getAddress()));
                new_bosses.push_back(Node(*ring[ring.size() - 2].getAddress()));
            }else if (i == 1){
                new_bosses.push_back(Node(*ring[0].getAddress()));
                new_bosses.push_back(Node(*ring[ring.size() - 1].getAddress()));
            } else {
                new_bosses.push_back(Node(*ring[(i - 1) % ring.size()].getAddress()));
                new_bosses.push_back(Node(*ring[(i - 2) % ring.size()].getAddress()));
            }
            new_replicas.push_back(Node(*ring[(i + 1) % ring.size()].getAddress()));
            new_replicas.push_back(Node(*ring[(i + 2) % ring.size()].getAddress()));
        }
    }

    // CHeck new replicas first
    for (int i = 0; i < new_replicas.size(); i++) {
        if (isNodeSame(hasMyReplicas[i], new_replicas[i])) { // if the previous replica is again at same location then continue.
            continue;
        }
        else {
            // if this is the not the new replica for this key but its location has changed from Tertiary to Secondary because of failure of secondary.
            if (ifExistNode(hasMyReplicas, new_replicas[i]) != -1) {
                vector<pair<string, string>> keys = findMyKeys(PRIMARY);  //then find all the primary keys at this server and send update message to the server to change its replica type.
                Message *msg;
                for (int k = 0; k < keys.size(); k++) { // for each primary key at this node update the replica type of the node which has now become secondary from tertiary.
                    msg = new Message(-1, getMemberNode()->addr, UPDATE, keys[k].first, keys[k].second, static_cast<ReplicaType>(i + 1));
                    emulNet->ENsend(&getMemberNode()->addr, new_replicas[i].getAddress(), msg->toString());
                    free(msg);
                }
            } else {
                // if this is the new node that is now a replica, find all the primary keys at this node and send create messages to new node to insert those keys into their hashtable.
                vector<pair<string, string>> keys = findMyKeys(PRIMARY);
                Message *msg;
                for (int k = 0; k < keys.size(); k++) {
                    msg = new Message(-1, getMemberNode()->addr, CREATE, keys[k].first, keys[k].second, static_cast<ReplicaType>(i + 1));
                    emulNet->ENsend(&getMemberNode()->addr, new_replicas[i].getAddress(), msg->toString());
                    free(msg);
                }
            }
        }
    }

    // check for my bosses if they failed

    for (int i = 0; i < haveReplicasOf.size(); i++) {
        if (isNodeSame(haveReplicasOf[i], new_bosses[i])) {
            break; // if the boss is same and at same location, then break because boss will take care of its boss and will also correct this nodes replica type if necessary as is shown below.
        }
        else {
            if (ifExistNode(new_bosses, haveReplicasOf[i]) != -1) {
                break; // if there is some boss present which was also present earlier then don't do anything as it will take care of things and nodes in this system only leave, no join is there'
            } else {
                // else find keys at this node which are secondary or tertiary depending on how this node is located according to the new boss.
                vector<pair<string, string>> keys = findMyKeys(static_cast<ReplicaType>(i + 1));
                Entry *e; // Update keys at this local server. No need to send message since all the operations are local.
                for (int k = 0; k < keys.size(); k++){
                    e = new Entry(keys[k].second, par->getcurrtime(), PRIMARY);
                    ht->update(keys[k].first, e->convertToString());
                    free(e);
                }
                if (i == 0){ // If this was the secondary replica of the failed boss, then make your first replica secondary from tertiary by updating it.
                    // Also, add new tertiary replica by sending create messages for all the keys of the bosses which have now become primary at this server.
                    Message *msg;
                    for (int k = 0; k < keys.size(); k++) {
                        if (isNodeSame(new_replicas[0], hasMyReplicas[0])){
                            msg = new Message(-1, getMemberNode()->addr, UPDATE, keys[k].first, keys[k].second, SECONDARY);
                            emulNet->ENsend(&getMemberNode()->addr, new_replicas[0].getAddress(), msg->toString());
                        } else {
                            msg = new Message(-1, getMemberNode()->addr, CREATE, keys[k].first, keys[k].second, SECONDARY);
                            emulNet->ENsend(&getMemberNode()->addr, new_replicas[0].getAddress(), msg->toString());
                        }
                        free(msg);

                        msg = new Message(-1, getMemberNode()->addr, CREATE, keys[k].first, keys[k].second, TERTIARY);
                        emulNet->ENsend(&getMemberNode()->addr, new_replicas[1].getAddress(), msg->toString());
                        free(msg);
                    }
                } else if (i == 1){
//.........这里部分代码省略.........
开发者ID:jalatif,项目名称:Distributed-Key-Value-Store-Simulation,代码行数:101,代码来源:MP2Node.cpp


注:本文中的Entry::convertToString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。