|
1 | | -#include "stdafx.h" |
2 | | -#include "redis_util.h" |
3 | | -#include "redis_migrate.h" |
4 | | -#include "redis_reshard.h" |
5 | | - |
6 | | -redis_reshard::redis_reshard(const char* addr) |
7 | | - : addr_(addr) |
8 | | -{ |
9 | | -} |
10 | | - |
11 | | -redis_reshard::~redis_reshard() |
12 | | -{ |
13 | | - std::vector<acl::redis_node*>::iterator it = masters_.begin(); |
14 | | - for (; it != masters_.end(); ++it) |
15 | | - delete *it; |
16 | | -} |
17 | | - |
18 | | -void redis_reshard::copy_slots(acl::redis_node& from, acl::redis_node& to) |
19 | | -{ |
20 | | - const std::vector<std::pair<size_t, size_t> >& slots = from.get_slots(); |
21 | | - std::vector<std::pair<size_t, size_t> >::const_iterator cit; |
22 | | - for (cit = slots.begin(); cit != slots.end(); ++cit) |
23 | | - to.add_slot_range(cit->first, cit->second); |
24 | | -} |
25 | | - |
26 | | -acl::redis_node* redis_reshard::find_node(const char* id) |
27 | | -{ |
28 | | - std::vector<acl::redis_node*>::iterator it; |
29 | | - for (it = masters_.begin(); it != masters_.end(); ++it) |
30 | | - { |
31 | | - if (strcmp(id, (*it)->get_id()) == 0) |
32 | | - return *it; |
33 | | - } |
34 | | - return NULL; |
35 | | -} |
36 | | - |
37 | | -void redis_reshard::copy_all(std::vector<acl::redis_node*>& src, |
38 | | - const char* exclude) |
39 | | -{ |
40 | | - std::vector<acl::redis_node*>::iterator it; |
41 | | - for (it = masters_.begin(); it != masters_.end(); ++it) |
42 | | - { |
43 | | - if (strcmp((*it)->get_id(), exclude) != 0) |
44 | | - src.push_back(*it); |
45 | | - } |
46 | | -} |
47 | | - |
48 | | -void redis_reshard::run() |
49 | | -{ |
50 | | - if (get_masters_info() == false) |
51 | | - return; |
52 | | - |
53 | | - show_nodes(); |
54 | | - fflush(stdout); |
55 | | - char buf[1024]; |
56 | | - |
57 | | - int nslots = 0; |
58 | | - while (true) |
59 | | - { |
60 | | - printf("How many slots do you want to move (from 1 to 16384) ? "); |
61 | | - fflush(stdout); |
62 | | - int ret = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf)); |
63 | | - if (ret == ACL_VSTREAM_EOF) |
64 | | - exit(1); |
65 | | - acl_mystr_trim(buf); |
66 | | - nslots = atoi(buf); |
67 | | - if (nslots > 0 && nslots < 16384) |
68 | | - break; |
69 | | - printf("invalid value: %d\r\n", ret); |
70 | | - } |
71 | | - |
72 | | - acl::redis_node* target = NULL; |
73 | | - while (true) |
74 | | - { |
75 | | - printf("What is the receiving node ID? "); |
76 | | - fflush(stdout); |
77 | | - int ret = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf)); |
78 | | - if (ret == ACL_VSTREAM_EOF) |
79 | | - exit(1); |
80 | | - |
81 | | - acl_mystr_trim(buf); |
82 | | - target = find_node(buf); |
83 | | - if (target != NULL) |
84 | | - break; |
85 | | - |
86 | | - printf("...The specified node(%s) is not known or not " |
87 | | - "a master, please try again.\r\n", buf); |
88 | | - } |
89 | | - assert(target != NULL); |
90 | | - |
91 | | - printf("Please input all the source node IDs.\r\n"); |
92 | | - printf(" Type 'all' to use all the nodes as source nodes for the hash slots\r\n"); |
93 | | - printf(" Type 'done' once you entered all the source node IDs.\r\n"); |
94 | | - |
95 | | - std::vector<acl::redis_node*> sources; |
96 | | - while (true) |
97 | | - { |
98 | | - printf("Source node #%d: ", (int) sources.size() + 1); |
99 | | - fflush(stdout); |
100 | | - int ret = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf)); |
101 | | - if (ret == ACL_VSTREAM_EOF) |
102 | | - exit(1); |
103 | | - |
104 | | - acl_mystr_trim(buf); |
105 | | - if (strcasecmp(buf, "done") == 0) |
106 | | - break; |
107 | | - if (strcasecmp(buf, "all") == 0) |
108 | | - { |
109 | | - copy_all(sources, target->get_id()); |
110 | | - break; |
111 | | - } |
112 | | - |
113 | | - acl::redis_node* source = find_node(buf); |
114 | | - if (source == NULL) |
115 | | - { |
116 | | - printf("...The source node(%s) is not known\r\n", buf); |
117 | | - continue; |
118 | | - } |
119 | | - if (strcmp(target->get_id(), buf) == 0) |
120 | | - { |
121 | | - printf("... It is not possible to use the target node as source node\r\n"); |
122 | | - continue; |
123 | | - } |
124 | | - |
125 | | - sources.push_back(source); |
126 | | - } |
127 | | - if (sources.empty()) |
128 | | - { |
129 | | - printf("*** No source nodes given, operation aborted\r\n"); |
130 | | - exit(1); |
131 | | - } |
132 | | - |
133 | | - redis_migrate migrate(masters_); |
134 | | - migrate.move_slots(sources, *target, nslots); |
135 | | -} |
136 | | - |
137 | | -bool redis_reshard::get_masters_info() |
138 | | -{ |
139 | | - acl::redis_client client(addr_, 30, 30); |
140 | | - acl::redis redis(&client); |
141 | | - |
142 | | - const std::map<acl::string, acl::redis_node*>* masters; |
143 | | - if ((masters = redis.cluster_nodes()) == NULL) |
144 | | - { |
145 | | - printf("%s: master nodes empty\r\n", __FUNCTION__); |
146 | | - return false; |
147 | | - } |
148 | | - |
149 | | - std::map<acl::string, acl::redis_node*>::const_iterator cit; |
150 | | - for (cit = masters->begin(); cit != masters->end(); ++cit) |
151 | | - { |
152 | | - acl::redis_node* master = new acl::redis_node; |
153 | | - master->set_id(cit->second->get_id()); |
154 | | - master->set_addr(cit->second->get_addr()); |
155 | | - copy_slots(*cit->second, *master); |
156 | | - masters_.push_back(master); |
157 | | - } |
158 | | - |
159 | | - return true; |
160 | | -} |
161 | | - |
162 | | -void redis_reshard::show_nodes() |
163 | | -{ |
164 | | - std::vector<acl::redis_node*>::const_iterator cit; |
165 | | - for (cit = masters_.begin(); cit != masters_.end(); ++cit) |
166 | | - { |
167 | | - printf("-----------------------------------------------\r\n"); |
168 | | - printf("addr: %s\r\nid: %s\r\n", (*cit)->get_addr(), |
169 | | - (*cit)->get_id()); |
170 | | - show_slots(**cit); |
171 | | - } |
172 | | -} |
173 | | - |
174 | | -void redis_reshard::show_slots(const acl::redis_node& node) |
175 | | -{ |
176 | | - const std::vector<std::pair<size_t, size_t> > slots = node.get_slots(); |
177 | | - std::vector<std::pair<size_t, size_t> >::const_iterator cit; |
178 | | - for (cit = slots.begin(); cit != slots.end(); ++cit) |
179 | | - printf("slots: %d - %d\r\n", |
180 | | - (int) cit->first, (int) cit->second); |
181 | | -} |
| 1 | +#include "stdafx.h" |
| 2 | +#include "redis_util.h" |
| 3 | +#include "redis_migrate.h" |
| 4 | +#include "redis_reshard.h" |
| 5 | + |
| 6 | +redis_reshard::redis_reshard(const char* addr) |
| 7 | + : addr_(addr) |
| 8 | +{ |
| 9 | +} |
| 10 | + |
| 11 | +redis_reshard::~redis_reshard() |
| 12 | +{ |
| 13 | + std::vector<acl::redis_node*>::iterator it = masters_.begin(); |
| 14 | + for (; it != masters_.end(); ++it) |
| 15 | + delete *it; |
| 16 | +} |
| 17 | + |
| 18 | +void redis_reshard::copy_slots(acl::redis_node& from, acl::redis_node& to) |
| 19 | +{ |
| 20 | + const std::vector<std::pair<size_t, size_t> >& slots = from.get_slots(); |
| 21 | + std::vector<std::pair<size_t, size_t> >::const_iterator cit; |
| 22 | + for (cit = slots.begin(); cit != slots.end(); ++cit) |
| 23 | + to.add_slot_range(cit->first, cit->second); |
| 24 | +} |
| 25 | + |
| 26 | +acl::redis_node* redis_reshard::find_node(const char* id) |
| 27 | +{ |
| 28 | + std::vector<acl::redis_node*>::iterator it; |
| 29 | + for (it = masters_.begin(); it != masters_.end(); ++it) |
| 30 | + { |
| 31 | + if (strcmp(id, (*it)->get_id()) == 0) |
| 32 | + return *it; |
| 33 | + } |
| 34 | + return NULL; |
| 35 | +} |
| 36 | + |
| 37 | +void redis_reshard::copy_all(std::vector<acl::redis_node*>& src, |
| 38 | + const char* exclude) |
| 39 | +{ |
| 40 | + std::vector<acl::redis_node*>::iterator it; |
| 41 | + for (it = masters_.begin(); it != masters_.end(); ++it) |
| 42 | + { |
| 43 | + if (strcmp((*it)->get_id(), exclude) != 0) |
| 44 | + src.push_back(*it); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void redis_reshard::run() |
| 49 | +{ |
| 50 | + if (get_masters_info() == false) |
| 51 | + return; |
| 52 | + |
| 53 | + show_nodes(); |
| 54 | + fflush(stdout); |
| 55 | + char buf[1024]; |
| 56 | + |
| 57 | + int nslots = 0; |
| 58 | + while (true) |
| 59 | + { |
| 60 | + printf("How many slots do you want to move (from 1 to 16384) ? "); |
| 61 | + fflush(stdout); |
| 62 | + int ret = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf)); |
| 63 | + if (ret == ACL_VSTREAM_EOF) |
| 64 | + exit(1); |
| 65 | + acl_mystr_trim(buf); |
| 66 | + nslots = atoi(buf); |
| 67 | + if (nslots > 0 && nslots < 16384) |
| 68 | + break; |
| 69 | + printf("invalid value: %d\r\n", ret); |
| 70 | + } |
| 71 | + |
| 72 | + acl::redis_node* target = NULL; |
| 73 | + while (true) |
| 74 | + { |
| 75 | + printf("What is the receiving node ID? "); |
| 76 | + fflush(stdout); |
| 77 | + int ret = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf)); |
| 78 | + if (ret == ACL_VSTREAM_EOF) |
| 79 | + exit(1); |
| 80 | + |
| 81 | + acl_mystr_trim(buf); |
| 82 | + target = find_node(buf); |
| 83 | + if (target != NULL) |
| 84 | + break; |
| 85 | + |
| 86 | + printf("...The specified node(%s) is not known or not " |
| 87 | + "a master, please try again.\r\n", buf); |
| 88 | + } |
| 89 | + assert(target != NULL); |
| 90 | + |
| 91 | + printf("Please input all the source node IDs.\r\n"); |
| 92 | + printf(" Type 'all' to use all the nodes as source nodes for the hash slots\r\n"); |
| 93 | + printf(" Type 'done' once you entered all the source node IDs.\r\n"); |
| 94 | + |
| 95 | + std::vector<acl::redis_node*> sources; |
| 96 | + while (true) |
| 97 | + { |
| 98 | + printf("Source node #%d: ", (int) sources.size() + 1); |
| 99 | + fflush(stdout); |
| 100 | + int ret = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf)); |
| 101 | + if (ret == ACL_VSTREAM_EOF) |
| 102 | + exit(1); |
| 103 | + |
| 104 | + acl_mystr_trim(buf); |
| 105 | + if (strcasecmp(buf, "done") == 0) |
| 106 | + break; |
| 107 | + if (strcasecmp(buf, "all") == 0) |
| 108 | + { |
| 109 | + copy_all(sources, target->get_id()); |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + acl::redis_node* source = find_node(buf); |
| 114 | + if (source == NULL) |
| 115 | + { |
| 116 | + printf("...The source node(%s) is not known\r\n", buf); |
| 117 | + continue; |
| 118 | + } |
| 119 | + if (strcmp(target->get_id(), buf) == 0) |
| 120 | + { |
| 121 | + printf("... It is not possible to use the target node as source node\r\n"); |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + sources.push_back(source); |
| 126 | + } |
| 127 | + if (sources.empty()) |
| 128 | + { |
| 129 | + printf("*** No source nodes given, operation aborted\r\n"); |
| 130 | + exit(1); |
| 131 | + } |
| 132 | + |
| 133 | + redis_migrate migrate(masters_); |
| 134 | + migrate.move_slots(sources, *target, nslots); |
| 135 | +} |
| 136 | + |
| 137 | +bool redis_reshard::get_masters_info() |
| 138 | +{ |
| 139 | + acl::redis_client client(addr_, 30, 30); |
| 140 | + acl::redis redis(&client); |
| 141 | + |
| 142 | + const std::map<acl::string, acl::redis_node*>* masters; |
| 143 | + if ((masters = redis.cluster_nodes()) == NULL) |
| 144 | + { |
| 145 | + printf("%s: master nodes empty\r\n", __FUNCTION__); |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + std::map<acl::string, acl::redis_node*>::const_iterator cit; |
| 150 | + for (cit = masters->begin(); cit != masters->end(); ++cit) |
| 151 | + { |
| 152 | + acl::redis_node* master = new acl::redis_node; |
| 153 | + master->set_id(cit->second->get_id()); |
| 154 | + master->set_addr(cit->second->get_addr()); |
| 155 | + copy_slots(*cit->second, *master); |
| 156 | + masters_.push_back(master); |
| 157 | + } |
| 158 | + |
| 159 | + return true; |
| 160 | +} |
| 161 | + |
| 162 | +void redis_reshard::show_nodes() |
| 163 | +{ |
| 164 | + std::vector<acl::redis_node*>::const_iterator cit; |
| 165 | + for (cit = masters_.begin(); cit != masters_.end(); ++cit) |
| 166 | + { |
| 167 | + printf("-----------------------------------------------\r\n"); |
| 168 | + printf("addr: %s\r\nid: %s\r\n", (*cit)->get_addr(), |
| 169 | + (*cit)->get_id()); |
| 170 | + show_slots(**cit); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +void redis_reshard::show_slots(const acl::redis_node& node) |
| 175 | +{ |
| 176 | + const std::vector<std::pair<size_t, size_t> > slots = node.get_slots(); |
| 177 | + std::vector<std::pair<size_t, size_t> >::const_iterator cit; |
| 178 | + for (cit = slots.begin(); cit != slots.end(); ++cit) |
| 179 | + printf("slots: %d - %d\r\n", |
| 180 | + (int) cit->first, (int) cit->second); |
| 181 | +} |
0 commit comments