| 708 | } |
| 709 | |
| 710 | const std::map<string, redis_node*>* redis_cluster::cluster_nodes() |
| 711 | { |
| 712 | free_masters(); |
| 713 | |
| 714 | const char* argv[2]; |
| 715 | size_t lens[2]; |
| 716 | |
| 717 | argv[0] = "CLUSTER"; |
| 718 | lens[0] = sizeof("CLUSTER") - 1; |
| 719 | |
| 720 | argv[1] = "NODES"; |
| 721 | lens[1] = sizeof("NODES") - 1; |
| 722 | |
| 723 | build_request(2, argv, lens); |
| 724 | |
| 725 | string buf; |
| 726 | if (get_string(buf) <= 0) { |
| 727 | return NULL; |
| 728 | } |
| 729 | |
| 730 | std::vector<redis_node*> slaves; |
| 731 | acl::string line; |
| 732 | |
| 733 | while (true) { |
| 734 | if (!buf.scan_line(line)) { |
| 735 | break; |
| 736 | } |
| 737 | redis_node* node = get_node(line); |
| 738 | if (node != NULL && !node->is_master()) { |
| 739 | slaves.push_back(node); |
| 740 | } |
| 741 | line.clear(); |
| 742 | } |
| 743 | |
| 744 | for (std::vector<redis_node*>::iterator it = slaves.begin(); |
| 745 | it != slaves.end(); ++it) { |
| 746 | const char* id = (*it)->get_master_id(); |
| 747 | std::map<string, redis_node*>::iterator it2 = masters_.find(id); |
| 748 | if (it2 != masters_.end()) { |
| 749 | it2->second->add_slave(*it); |
| 750 | } else { |
| 751 | logger_warn("delete orphan slave: %s", id); |
| 752 | delete *it; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | return &masters_; |
| 757 | } |
| 758 | |
| 759 | // for redis.3.x.x |
| 760 | // d52ea3cb4cdf7294ac1fb61c696ae6483377bcfc 127.0.0.1:16385 master - 0 1428410625374 73 connected 5461-10922 |