HCE Project PHP language client API bindings  1.5.1
Hierarchical Cluster Engine PHP Client Interface API
 All Classes Namespaces Files Functions Variables Pages
manager.inc.php
Go to the documentation of this file.
1 <?php
14 require_once 'hce_cli_api.inc.php';
15 require_once 'hce_sphinx_api.inc.php';
16 
20 defined('HCE_CLUSTER_CMD_TIMEOUT') or define('HCE_CLUSTER_CMD_TIMEOUT', 100);
24 defined('HCE_CLUSTER_CMD_STRUCTURE_CHECK') or define('HCE_CLUSTER_CMD_STRUCTURE_CHECK', 'STRUCTURE_CHECK');
25 defined('HCE_CLUSTER_CMD_NODE_SHUTDOWN') or define('HCE_CLUSTER_CMD_NODE_SHUTDOWN', 'NODE_SHUTDOWN');
26 defined('HCE_CLUSTER_CMD_NODE_PROPERTIES') or define('HCE_CLUSTER_CMD_NODE_PROPERTIES', 'NODE_PROPERTIES');
27 defined('HCE_CLUSTER_CMD_NODE_RESOURCE_USAGE') or define('HCE_CLUSTER_CMD_NODE_RESOURCE_USAGE', 'NODE_RESOURCE_USAGE');
28 
29 defined('HCE_CLUSTER_CMD_NODE_ECHO') or define('HCE_CLUSTER_CMD_NODE_ECHO', 'NODE_ECHO');
30 defined('HCE_CLUSTER_CMD_NODE_TIME') or define('HCE_CLUSTER_CMD_NODE_TIME', 'NODE_TIME');
31 defined('HCE_CLUSTER_CMD_NODE_LLSET') or define('HCE_CLUSTER_CMD_NODE_LLSET', 'NODE_LLSET');
32 defined('HCE_CLUSTER_CMD_NODE_LLGET') or define('HCE_CLUSTER_CMD_NODE_LLGET', 'NODE_LLGET');
33 defined('HCE_CLUSTER_CMD_NODE_HB_DELAY_SET') or define('HCE_CLUSTER_CMD_NODE_HB_DELAY_SET', 'NODE_HB_DELAY_SET');
34 defined('HCE_CLUSTER_CMD_NODE_HB_DELAY_GET') or define('HCE_CLUSTER_CMD_NODE_HB_DELAY_GET', 'NODE_HB_DELAY_GET');
35 defined('HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_SET') or define('HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_SET', 'NODE_HB_TIMEOUT_SET');
36 defined('HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_GET') or define('HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_GET', 'NODE_HB_TIMEOUT_GET');
37 defined('HCE_CLUSTER_CMD_NODE_HB_MODE_SET') or define('HCE_CLUSTER_CMD_NODE_HB_MODE_SET', 'NODE_HB_MODE_SET');
38 defined('HCE_CLUSTER_CMD_NODE_HB_MODE_GET') or define('HCE_CLUSTER_CMD_NODE_HB_MODE_GET', 'NODE_HB_MODE_GET');
39 defined('HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_SET') or define('HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_SET', 'NODE_POLL_TIMEOUT_SET');
40 defined('HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_GET') or define('HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_GET', 'NODE_POLL_TIMEOUT_GET');
41 defined('HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_SET') or define('HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_SET', 'NODE_PROPERTY_INTERVAL_SET');
42 defined('HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_GET') or define('HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_GET', 'NODE_PROPERTY_INTERVAL_GET');
43 defined('HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_SET') or define('HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_SET', 'NODE_DUMP_INTERVAL_SET');
44 defined('HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_GET') or define('HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_GET', 'NODE_DUMP_INTERVAL_GET');
45 defined('HCE_CLUSTER_CMD_NODE_MMSET') or define('HCE_CLUSTER_CMD_NODE_MMSET', 'NODE_MMSET');
46 defined('HCE_CLUSTER_CMD_NODE_MMGET') or define('HCE_CLUSTER_CMD_NODE_MMGET', 'NODE_MMGET');
47 defined('HCE_CLUSTER_CMD_NODE_MPMSET') or define('HCE_CLUSTER_CMD_NODE_MPMSET', 'NODE_MPMSET');
48 defined('HCE_CLUSTER_CMD_NODE_MPMGET') or define('HCE_CLUSTER_CMD_NODE_MPMGET', 'NODE_MPMGET');
49 defined('HCE_CLUSTER_CMD_NODE_MRCSSET') or define('HCE_CLUSTER_CMD_NODE_MRCSSET', 'NODE_MRCSSET');
50 defined('HCE_CLUSTER_CMD_NODE_MRCSGET') or define('HCE_CLUSTER_CMD_NODE_MRCSGET', 'NODE_MRCSGET');
51 defined('HCE_CLUSTER_CMD_NODE_SPHINX') or define('HCE_CLUSTER_CMD_NODE_SPHINX', 'SPHINX');
52 defined('HCE_CLUSTER_CMD_NODE_DRCE') or define('HCE_CLUSTER_CMD_NODE_DRCE', 'DRCE');
53 
57 defined('HCE_CLUSTER_CMD_PROPERTY_NAME_LOG') or define('HCE_CLUSTER_CMD_PROPERTY_NAME_LOG', 'log');
58 
62 defined('HCE_CLUSTER_SCHEMA_ROLE_ROUTER') or define('HCE_CLUSTER_SCHEMA_ROLE_ROUTER', 'router');
63 defined('HCE_CLUSTER_SCHEMA_ROLE_SMANAGER') or define('HCE_CLUSTER_SCHEMA_ROLE_SMANAGER', 'smanager');
64 defined('HCE_CLUSTER_SCHEMA_ROLE_RMANAGER') or define('HCE_CLUSTER_SCHEMA_ROLE_RMANAGER', 'rmanager');
65 defined('HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RR') or define('HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RR', 'rmanager-round-robin');
66 defined('HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RND') or define('HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RND', 'rmanager-rnd');
67 defined('HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RU') or define('HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RU', 'rmanager-resources-usage');
68 defined('HCE_CLUSTER_SCHEMA_ROLE_REPLICA') or define('HCE_CLUSTER_SCHEMA_ROLE_REPLICA', 'replica');
69 
73 defined('HCE_CLUSTER_SCHEMA_ROLE_CODE_ROUTER') or define('HCE_CLUSTER_SCHEMA_ROLE_CODE_ROUTER', 3);
74 defined('HCE_CLUSTER_SCHEMA_ROLE_CODE_SMANAGER') or define('HCE_CLUSTER_SCHEMA_ROLE_CODE_SMANAGER', 0);
75 defined('HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER') or define('HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER', 1);
76 defined('HCE_CLUSTER_SCHEMA_ROLE_CODE_REPLICA') or define('HCE_CLUSTER_SCHEMA_ROLE_CODE_REPLICA', 2);
77 defined('HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER_RND') or define('HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER_RND', 4);
78 defined('HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER_RU') or define('HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER_RU', 5);
79 
83 defined('HCE_CLUSTER_SCHEMA_ERROR_WRONG_DATA') or define('HCE_CLUSTER_SCHEMA_ERROR_WRONG_DATA', 'wrong data or network error');
84 
88 defined('HCE_CLUSTER_SCHEMA_PORTS_DEFAULT') or define('HCE_CLUSTER_SCHEMA_PORTS_DEFAULT', '5540,5541,5542,5543,5544,5545,5546,5547,5548,5549,5550,5551,5552,5553,5554,5555,5556,5557,5558,5559');
89 
93 defined('HCE_CLUSTER_CMD_DATE_FORMAT') or define('HCE_CLUSTER_CMD_DATE_FORMAT', 'Y-m-d H:i:s');
94 defined('HCE_CLUSTER_CMD_TIME_FORMAT') or define('HCE_CLUSTER_CMD_TIME_FORMAT', 'H:i:s');
95 
99 defined('HCE_CLUSTER_CMD_PROPERTIES_FIELD_TIME') or define('HCE_CLUSTER_CMD_PROPERTIES_FIELD_TIME', 'startedAt');
100 defined('HCE_CLUSTER_CMD_PROPERTIES_FIELD_NAME') or define('HCE_CLUSTER_CMD_PROPERTIES_FIELD_NAME', 'name');
101 defined('HCE_CLUSTER_CMD_PROPERTIES_FIELD_CIDENTITY') or define('HCE_CLUSTER_CMD_PROPERTIES_FIELD_CIDENTITY', 'clientIdentity');
102 defined('HCE_CLUSTER_CMD_PROPERTIES_FIELD_CSTRING') or define('HCE_CLUSTER_CMD_PROPERTIES_FIELD_CSTRING', 'connectionString');
103 defined('HCE_CLUSTER_CMD_PROPERTIES_FIELD_NODEMODE') or define('HCE_CLUSTER_CMD_PROPERTIES_FIELD_NODEMODE', 'nodeMode');
104 
105 /*
106  * @desc return help textual conext
107  * @param $node Ip address or domain name
108  *
109  * @return node info array
110  */
112  $ret='Usage: manager.php --command=<command> [--host=<host>] [--port=<port>] [--<option_name>=<option_value>,...] [--timeout=<timeout_ms>]'.
113  ' [--log=<log_level_mask{0 - warnings, 1 - errors, 2 - debug}>] [--scan=<host_port_recombination{0 - for each host scan all listed ports, 1 - treate host and ports as pairs}>]'.
114  ' [--ignore=<ignore_errors_mode{0 - not ignore, 1 - ignore not critical, 2 - ignore all}>] [-sphinx_properties_formatted] [--view={"json", "chart"}]'.PHP_EOL.PHP_EOL.
115  'Cluster and node level commands:'.PHP_EOL.
116  ' STRUCTURE_CHECK [--view={"json", "chart"}] [--fields={rhasc}] - detect and check cluster structure, return schema in json (default) or ASCII chart view'.PHP_EOL.
117  ' fields: r - role, h - host, a - admin, s - server, c - client connections'.PHP_EOL.
118  ' NODE_SHUTDOWN - shutdown node'.PHP_EOL.
119  ' NODE_ECHO - simple ping node with echo'.PHP_EOL.
120  ' NODE_TIME - returns json with time of live of node, msec'.PHP_EOL.
121  ' NODE_PROPERTIES --handler=<handler_names_csv>, --realtime=<parameters_json> - returns json with node properties of all handlers, for sample: {"parameters":"", "realtime":0}'.PHP_EOL.
122  ' NODE_LLSET --handler=<handler_names_csv>, --level=<log_levels_value_csv> - set log level(s) for handler(s)'.PHP_EOL.
123  ' NODE_LLGET --handler=<handler_names_csv> - returns json with log level(s) for handler(s)'.PHP_EOL.
124  ' NODE_HB_DELAY_SET --handler=<handler_names_csv>, --hddelay=<heartbeat_delay> - set heartbeat delay value for handler(s)'.PHP_EOL.
125  ' NODE_HB_DELAY_GET --handler=<handler_names_csv> - returns json with heartbeat delay values of handler(s)'.PHP_EOL.
126  ' NODE_HB_TIMEOUT_SET --handler=<handler_names_csv>, --hbtimeout=<heartbeat_timeout> - set heartbeat timeout value for handler(s)'.PHP_EOL.
127  ' NODE_HB_TIMEOUT_GET --handler=<handler_names_csv> - returns json with heartbeat timeout values of handler(s)'.PHP_EOL.
128  ' NODE_HB_MODE_SET --handler=<handler_names_csv>, --hbmode=<heartbeat_mode> - set heartbeat mode value for handler(s)'.PHP_EOL.
129  ' NODE_HB_MODE_GET --handler=<handler_names_csv> - returns json with heartbeat mode values of handler(s)'.PHP_EOL.
130  ' NODE_POLL_TIMEOUT_SET --handler=<handler_names_csv>, --ptimeout=<poll_timeout> - set poll timeout value for handler(s)'.PHP_EOL.
131  ' NODE_POLL_TIMEOUT_GET --handler=<handler_names_csv> - returns json with poll timeout values of handler(s)'.PHP_EOL.
132  ' NODE_PROPERTY_INTERVAL_SET --handler=<handler_names_csv>, --interval=<property_interval> - set property interval value for handler(s)'.PHP_EOL.
133  ' NODE_PROPERTY_INTERVAL_GET --handler=<handler_names_csv> - returns json with property interval values of handler(s)'.PHP_EOL.
134  ' NODE_DUMP_INTERVAL_SET --handler=<handler_names_csv>, --interval=<dump_interval> - set dump interval value for handler(s)'.PHP_EOL.
135  ' NODE_DUMP_INTERVAL_GET --handler=<handler_names_csv> - returns json with dump interval values of handler(s)'.PHP_EOL.
136  ' NODE_MMSET --mode=<manager_mode_name{"smanager", "rmanager"}> - set manager mode'.PHP_EOL.
137  ' NODE_MMGET - get manager mode'.PHP_EOL.
138  ' NODE_MPMSET --purge=<purge_mode> - set purge mode'.PHP_EOL.
139  ' NODE_MPMGET - get purge mode'.PHP_EOL.
140  ' NODE_MRCSSET --csize=<collected_size> - set resources usage collected size'.PHP_EOL.
141  ' NODE_MRCSGET - get resources usage collected size'.PHP_EOL.
142  ' NODE_RECOVER_NOTIFICATION_CONNECTION - recover notification connection'.PHP_EOL.
143  ' NODE_ROUTES - get HCE-node routes list'.PHP_EOL.
144  ' NODE_RESOURCE_USAGE - get HCE-node resources usage list'.PHP_EOL.
145  'Sphinx search level commands:'.PHP_EOL.
146  ' INDEX_REMOVE'.PHP_EOL.
147  ' INDEX_DELETE_DOC_NUMBER'.PHP_EOL.
148  ' INDEX_CHECK'.PHP_EOL.
149  ' INDEX_START'.PHP_EOL.
150  ' INDEX_STOP'.PHP_EOL.
151  ' INDEX_CREATE'.PHP_EOL.
152  ' INDEX_APPEND_DATA_FILE'.PHP_EOL.
153  ' INDEX_STORE_DATA_FILE'.PHP_EOL.
154  ' INDEX_STORE_SCHEMA_FILE'.PHP_EOL.
155  ' INDEX_PACK_DOC_DATA'.PHP_EOL.
156  ' INDEX_MERGE'.PHP_EOL.
157  ' INDEX_DELETE_DATA_FILE'.PHP_EOL.
158  ' INDEX_REBUILD'.PHP_EOL.
159  ' INDEX_DELETE_SCHEMA_FILE'.PHP_EOL.
160  ' INDEX_DELETE_DOC'.PHP_EOL.
161  ' INDEX_RENAME'.PHP_EOL.
162  ' INDEX_COPY'.PHP_EOL.
163  ' INDEX_SET_CONFIG_VAR'.PHP_EOL.
164  ' INDEX_CONNECT'.PHP_EOL.
165  ' INDEX_DISCONNECT'.PHP_EOL.
166  'DRCE level commands:'.PHP_EOL.
167  ' DRCE --request=request_type{SET, CHECK, TERMINATE, GET, DELETE} --id=<task_id> --json=<json_protocol_file>'.PHP_EOL.
168  ' DRCE_SET_HOST - set DRCE host value'.PHP_EOL.
169  ' DRCE_GET_HOST - get DRCE host value'.PHP_EOL.
170  ' DRCE_SET_PORT - set DRCE connection port value'.PHP_EOL.
171  ' DRCE_GET_PORT - get DRCE connection port value'.PHP_EOL.
172  ' DRCE_GET_TASKS - get DRCE tasks list'.PHP_EOL.
173  ' DRCE_GET_TASKS_INFO - get DRCE tasks list info'.PHP_EOL.
174  PHP_EOL;
175 
176  return $ret;
177 }
178 
179 
180 /*
181  * @desc make admin request for node and return stat info
182  * @param $node Ip address or domain name
183  *
184  * @return node info array
185  */
186 function hce_manage_node_get_handler_properties($response_str, $delimiter=HCE_ADMIN_CMD_DELIMITER, $time_format=null){
187  $timestampedProperties=array('startedAt', 'pstatsAt', 'heartbeatAt', 'heartbeatedAt');
188 
189  $ret=array();
190 
191  $properties=explode($delimiter, $response_str);
192  if(isset($properties[0]) && $properties[0]==HCE_ADMIN_NODE_ADMIN_ERROR_OK){
193  foreach($properties as $property){
194  $property=explode('=', $property);
195  if(count($property)>1){
196  if($time_format!==null && in_array($property[0], $timestampedProperties)){
197  $ret[$property[0]]=date($time_format, ceil($property[1]/1000));
198  }else{
199  $ret[$property[0]]=$property[1];
200  }
201  }
202  }
203  }else{
204  $ret=false;
205  }
206 
207  return $ret;
208 }
209 
210 
211 /*
212  * @desc make admin request for all node handlers and return stat info
213  * @param $host - node Ip address or domain name
214  * $port - node handler's port
215  * $timeout - response timeout
216  *
217  * @return node info array
218  */
219 function hce_manage_node_get_info($host, $port, $timeout, $params=''){
220  $handlers=array(HCE_HANDLER_ADMIN, HCE_HANDLER_ROUTER_SERVER_PROXY, HCE_HANDLER_DATA_SERVER_PROXY, HCE_HANDLER_DATA_CLIENT_PROXY, HCE_HANDLER_DATA_PROCESSOR_DATA,
221  HCE_HANDLER_DATA_CLIENT_DATA, HCE_HANDLER_DATA_REDUCER_PROXY);
222 
223  $ret=array('error'=>0, 'data'=>null, 'host'=>$host, 'port'=>$port);
224 
225  $hce_connection=hce_connection_create(array('host'=>$host, 'port'=>$port, 'type'=>HCE_CONNECTION_TYPE_ADMIN, 'identity'=>hce_unique_client_id()));
226 
227  if(!$hce_connection['error']){
228  foreach($handlers as $handler){
229  hce_message_send($hce_connection, array('id'=>hce_unique_message_id(), 'body'=>hce_admin_message_create($handler, HCE_CMD_PROPERTIES, $params)));
230  $hce_responses=hce_message_receive($hce_connection, $timeout);
231  if($hce_responses['error']==HCE_PROTOCOL_ERROR_OK){
232  foreach($hce_responses['messages'] as $hce_message){
233  $ret['data'][$handler]=$hce_message['body'];
234  }
235  }else{
236  $ret['error']=$hce_responses['error'];
237  //If admin handler not responded properly or timed out - skip all another
238  if($handler==HCE_HANDLER_ADMIN){
239  break;
240  }
241  }
242  }
244  }else{
245  $ret['error']=$hce_connection['error'];
246  }
247 
248  return $ret;
249 }
250 
251 /*
252  * @desc make admin request for node handler and return response
253  * @param $host - node Ip address or domain name
254  * $port - node handler's port
255  * $handler - node handler
256  * $command - handler's port
257  * $timeout - response timeout
258  *
259  * @return node info array
260  */
261 function hce_manage_node_handler_command($host, $port, $handler=HCE_HANDLER_ADMIN, $command=HCE_CMD_PROPERTIES, $parameters='', $timeout){
262  $ret=array('error'=>0, 'data'=>null, 'host'=>$host, 'port'=>$port);
263 
264  $hce_connection=hce_connection_create(array('host'=>$host, 'port'=>$port, 'type'=>HCE_CONNECTION_TYPE_ADMIN, 'identity'=>hce_unique_client_id()));
265 
266  if(!$hce_connection['error']){
267  hce_message_send($hce_connection, array('id'=>hce_unique_message_id(), 'body'=>hce_admin_message_create($handler, $command, $parameters)));
268 
269  $hce_responses=hce_message_receive($hce_connection, $timeout);
270  if($hce_responses['error']==HCE_PROTOCOL_ERROR_OK){
271  foreach($hce_responses['messages'] as $hce_message){
272  $ret['data'][$handler]=$hce_message['body'];
273  }
274  }else{
275  $ret['error']=$hce_responses['error'];
276  }
277 
279  }else{
280  $ret['error']=$hce_connection['error'];
281  }
282 
283  return $ret;
284 }
285 
286 /*
287  * @desc detects and fill nodes info array
288  * @param $args cli arguments parsed
289  *
290  * @return array node_info('host:port'=>array())
291  */
293  $node_info=array();
294  $hosts=explode(',', $args['host']);
295  $ports=explode(',', $args['port']);
296 
297  if(isset($args['realtime'])) {
298  $param_value=$args['realtime'];
299  }else{
300  $param_value='';
301  }
302 
303  //Fill nodes info array
304  if($args['scan']==0){
305  if(count($hosts)!=count($ports)){
306  //For each host
307  for($i=0; $i<count($hosts); $i++){
308  $host=$hosts[$i];
309  //For each port
310  for($j=0; $j<count($ports); $j++){
311  $port=$ports[$j];
312  if($args['log'] > 0){
313  echo 'trying exact '.$host.':'.$port.'...'.PHP_EOL;
314  }
315  $node_info[$host.':'.$port]=hce_manage_node_get_info($host, $port, $args['timeout'], $param_value);
316  }
317  }
318  }else{
319  //For each host
320  for($i=0; $i<count($hosts); $i++){
321  $host=$hosts[$i];
322  //For each port
323  $port=$ports[$i];
324  if($args['log'] > 0){
325  echo 'trying exact '.$host.':'.$port.'...'.PHP_EOL;
326  }
327  $node_info[$host.':'.$port]=hce_manage_node_get_info($host, $port, $args['timeout'], $param_value);
328  }
329  }
330  }else{
331  //For each host and port
332  for($i=0; $i<count($hosts); $i++){
333  if($args['log'] & 1 > 0){
334  echo 'trying scan '.$hosts[$i].':'.$ports[$i].'...'.PHP_EOL;
335  }
336  $node_info[$hosts[$i].':'.$ports[$i]]=hce_manage_node_get_info($hosts[$i], $ports[$i], $args['timeout'], $param_value);
337  }
338  }
339 
340  return $node_info;
341 }
342 
343 
344 /*
345  * @desc execute manager command : cluster structure check and return json encoded cluster schema
346  * @param $args array command line argiments parsed
347  *
348  * @return $ret array('error_code'=>0, 'error_message'=>'', 'data'=>null), data - json encoded cluster schema or empty string if error
349  */
351  $ret=array('error_code'=>0, 'error_message'=>'', 'data'=>null);
352 
353  //Get node info array
354  $node_info=hce_manage_nodes_get_info($args);
355 
356  //Detect hosts roles and fill structure info
357  foreach($node_info as $node=>$node_data){
358  $node_info[$node]['structure']=hce_manage_node_detect_role($node_data);
359  }
360 
361  //Detect and set cluster structure relations for node info
363  if($r['error_code']>0){
364  if($args['log'] & 1 > 0){
365  $ret['error_code']=1;
366  $ret['error_message']='structure error : '.$r['error_message'].PHP_EOL;
367  }
368  }
369 
370  if($ret['error_code']==0 || ($ret['error_code']>0 && $args['ignore']==0)){
371  //Fill schema json array
372  $schema_cover=array('cluster'=>array('name'=>$args['name'], 'nodes'=>array()));
373  foreach($node_info as $node_data){
374  if($node_data['structure']['role']){
375  $schema_cover['cluster']['nodes'][]=$node_data['structure']['schema'];
376  }
377  }
378  //Create resulted schema json
379  if(count($schema_cover['cluster']['nodes'])>0){
380  if(!isset($args['view']) || $args['view']!='chart'){
381  $ret['data']=cli_prettyPrintJson(json_encode($schema_cover), ' ').PHP_EOL;
382  }else{
383  //Define recusive seek and insert procedure
384  $insertNodeInASCIIChartArray = function($chartArray, $targetNodeName, $nodeName) use(&$insertNodeInASCIIChartArray){
385  foreach($chartArray as $key=>$val){
386  $lines=explode(PHP_EOL, $key);
387  if($lines[0]==$targetNodeName){
388  if(is_array($val)){
389  $chartArray[$key]=array_merge($val, array($nodeName=>null));
390  }else{
391  $chartArray[$key]=array($nodeName=>null);
392  }
393  break;
394  }else{
395  if(is_array($val)){
396  $chartArray[$key]=$insertNodeInASCIIChartArray($val, $targetNodeName, $nodeName);
397  }
398  }
399  }
400 
401  return $chartArray;
402  };
403 
404  //Define collect node fields values
405  $getNodeFieldsValues = function($node, $fields=null){
406  $values='';
407  //Supported short fields names and correspondent full names in properties node fields set
408  $fields_names=array('n'=>'name', 'r'=>'role', 'h'=>'host', 'a'=>'admin', 's'=>'server', 'c'=>'client');
409 
410  if($fields==null){
411  $fields=array('n');
412  }else{
413  if(!in_array('n', $fields)){
414  $fields=array_merge(array('n'), $fields);
415  }
416  }
417  foreach($fields_names as $key=>$val){
418  if(in_array($key, $fields)){
419  if(in_array($key, array('a', 's', 'c'))){
420  if($node[$val]!='' && $node[$val]!='0'){
421  $values.=strtoupper($key).'['.str_replace('tcp://', '', $node[$val]).']'.PHP_EOL;
422  }
423  }else{
424  $values.=$node[$val].PHP_EOL;
425  }
426  }
427  }
428 
429  return $values;
430  };
431 
432  //Prepare fields short names array
433  $fields=str_split((isset($args['fields']) ? $args['fields'] : 'n'));
434 
435  //Prepare array for ASCII chart
436  $chartArray=array();
437  //Insert router node as root or chart
438  foreach($schema_cover['cluster']['nodes'] as $node){
439  if($node['role']==HCE_CLUSTER_SCHEMA_ROLE_ROUTER){
440  $chartArray[$getNodeFieldsValues($node, $fields)]=null;
441  break;
442  }
443  }
444  //Insert connected nodes type "smanager" or "rmanager"
445  foreach($schema_cover['cluster']['nodes'] as $node){
446  if(isset($node['connection']) && $node['connection']!=''
447  && ($node['role']==HCE_CLUSTER_SCHEMA_ROLE_SMANAGER || $node['role']==HCE_CLUSTER_SCHEMA_ROLE_RMANAGER || $node['role']==HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RR
448  || $node['role']==HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RND || $node['role']==HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RU)){
449  //Insert in to the correspondent router or manager nodes array
450  $chartArray=$insertNodeInASCIIChartArray($chartArray, $node['connection'], $getNodeFieldsValues($node, $fields));
451  }
452  }
453  //Insert connected nodes type "replica"
454  foreach($schema_cover['cluster']['nodes'] as $node){
455  if(isset($node['connection']) && $node['connection']!='' && $node['role']==HCE_CLUSTER_SCHEMA_ROLE_REPLICA){
456  //Insert in to the correspondent router or manager nodes array
457  $chartArray=$insertNodeInASCIIChartArray($chartArray, $node['connection'], $getNodeFieldsValues($node, $fields));
458  }
459  }
460  //Generate ASCII chart for connected nodes
461  //$screen=cli_getScreenSize();
462  $ret['data']=cli_getASCIITreeFromArray($chartArray, array('max_width'=>1/*($screen['width']>$screen['height'] ? $screen['width'] : $screen['height'])*/)).PHP_EOL;
463 
464  //Add to chart another not connected nodes as list of bottom level items
465  $chartArray=array();
466  //Insert emty title node as root or chart
467  $chartArray['']=null;
468  //Insert not connected nodes
469  foreach($schema_cover['cluster']['nodes'] as $node){
470  if((!isset($node['connection']) || trim($node['connection'])=='') && $node['role']!=='router'){
471  $chartArray=$insertNodeInASCIIChartArray($chartArray, '', $getNodeFieldsValues($node, $fields));
472  }
473  }
474  //Generate ASCII chart for not connected nodes
475  $ret['data'].=cli_getASCIITreeFromArray($chartArray, array('max_width'=>1, 'hline_char'=>' ', 'vline_char'=>' ')).PHP_EOL;
476  }
477  }else{
478  $ret['data']='';
479  }
480  }
481 
482  return $ret;
483 }
484 
485 /*
486  * @desc detects host role by host's information returned from handler's by HCE_CMD_PROPERTIES command.
487  * @param $host array of data returned by hce_manage_get_host_info() call
488  *
489  * @return node role string name {'router', 'smanager', 'rmanager', 'replica'}
490  */
492  $connection_names=array('client'=>'client', 'server'=>'server', 'admin'=>'admin');
493 
494  $ret=array('error'=>0, 'role'=>null, 'schema'=>array('name'=>null, 'role'=>null, 'host'=>null, $connection_names['client']=>null, $connection_names['server']=>null, $connection_names['admin']=>null));
495 
496  if($node['error']==0){
497  $handlers=$node['data'];
498  $ret['schema']['host']=$node['host'];
499  //Ckeck is it a router
500  if(array_key_exists(HCE_HANDLER_ROUTER_SERVER_PROXY, $handlers) && array_key_exists(HCE_HANDLER_DATA_SERVER_PROXY, $handlers)){
501  //Split array of response parameters
502  $properties=hce_manage_node_get_handler_properties($handlers[HCE_HANDLER_ROUTER_SERVER_PROXY]);
503  if($properties!==false){
504  $ret['role']=HCE_CLUSTER_SCHEMA_ROLE_ROUTER;
505  $ret['schema']['role']=$ret['role'];
506  $ret['schema'][$connection_names['client']]=$properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CSTRING];
507  }
508  //Split array of response parameters
509  $properties=hce_manage_node_get_handler_properties($handlers[HCE_HANDLER_DATA_SERVER_PROXY]);
510  if($properties!==false){
511  $ret['schema'][$connection_names['server']]=$properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CSTRING];
512  }
513  }
514 
515  //Ckeck is it a manager "s" or "r"
516  if(array_key_exists(HCE_HANDLER_DATA_SERVER_PROXY, $handlers) && array_key_exists(HCE_HANDLER_DATA_CLIENT_PROXY, $handlers)){
517  //Split array of response parameters
518  $properties=hce_manage_node_get_handler_properties($handlers[HCE_HANDLER_DATA_SERVER_PROXY]);
519  if($properties!==false){
520  if($properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_NODEMODE]==HCE_CLUSTER_SCHEMA_ROLE_CODE_SMANAGER){
521  $ret['role']=HCE_CLUSTER_SCHEMA_ROLE_SMANAGER;
522  }elseif($properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_NODEMODE]==HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER){
523  $ret['role']=HCE_CLUSTER_SCHEMA_ROLE_RMANAGER;
524  }elseif($properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_NODEMODE]==HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER_RND){
525  $ret['role']=HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RND;
526  }elseif($properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_NODEMODE]==HCE_CLUSTER_SCHEMA_ROLE_CODE_RMANAGER_RU){
527  $ret['role']=HCE_CLUSTER_SCHEMA_ROLE_RMANAGER_RU;
528  }
529  $ret['schema'][$connection_names['server']]=$properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CSTRING];
530  }
531  //Split array of response parameters
532  $properties=hce_manage_node_get_handler_properties($handlers[HCE_HANDLER_DATA_CLIENT_PROXY]);
533  if($properties!==false){
534  $ret['schema'][$connection_names['client']]=$properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CSTRING];
535  }
536  }
537 
538  //Ckeck is it a replica
539  if(array_key_exists(HCE_HANDLER_DATA_CLIENT_DATA, $handlers) && array_key_exists(HCE_HANDLER_DATA_PROCESSOR_DATA, $handlers)){
540  //Split array of response parameters
541  $properties=hce_manage_node_get_handler_properties($handlers[HCE_HANDLER_DATA_CLIENT_DATA]);
542  if($properties!==false){
543  $ret['role']=HCE_CLUSTER_SCHEMA_ROLE_REPLICA;
544  $ret['schema'][$connection_names['client']]=$properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CSTRING];
545  $ret['schema'][$connection_names['server']]='';
546  }
547  /*
548  //Split array of response parameters
549  $properties=hce_manage_get_node_properties($handlers[HCE_HANDLER_DATA_CLIENT_PROXY]);
550  if($properties!==false){
551 
552  }
553  */
554  }
555 
556  //Ckeck is it a admin
557  if(array_key_exists(HCE_HANDLER_ADMIN, $handlers)){
558  //Split array of response parameters
559  $properties=hce_manage_node_get_handler_properties($handlers[HCE_HANDLER_ADMIN]);
560  if($properties!==false){
561  $ret['schema']['name']=$properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CIDENTITY];
562  $ret['schema'][$connection_names['admin']]=$properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CSTRING];
563  }
564  }
565 
566  //Set role for schema json
567  if($ret['role']){
568  $ret['schema']['role']=$ret['role'];
569  }
570  }else{
571  $ret['error']=HCE_CLUSTER_SCHEMA_ERROR_WRONG_DATA.' ['.$node['error'].']';
572  }
573 
574  return $ret;
575 }
576 
577 
578 /*
579  * @desc detects and set structure relations for cluster
580  * @param $node_info array of node data with cluster role info filled
581  *
582  * @return response array {'error_code'=>0, 'error_message'=>''}
583  */
585  $ret=array('error_code'=>0, 'error_message'=>'');
586  foreach($node_info as $node=>$node_data){
587  foreach($node_info as $node1=>$node_data1){
588  if($node_data1['error']==0 && $node_data['error']==0 && isset($node_data['structure']['schema']['server']) && isset($node_data1['structure']['schema']['client'])){
589  //Node schema parse
590  $schema=parse_url($node_data['structure']['schema']['server']);
591  //Correct host with value from request that is cover the value from local configuration settings of node
592  if($node_data['structure']['schema']['host']!='localhost' && isset($schema['host']) && ($schema['host']=='*' || $schema['host']=='localhost')){
593  //Requested data fetched not from "localhost"
594  $schema['host']=$node_data['structure']['schema']['host'];
595  }
596 
597  //Node1 schema parse
598  $schema1=parse_url($node_data1['structure']['schema']['client']);
599  //Correct host with value from request that is cover the value from local configuration settings of node
600  if($node_data1['structure']['schema']['host']!='localhost' && isset($schema1['host']) && ($schema1['host']=='*' || $schema1['host']=='localhost')){
601  //Requested data fetched not from "localhost"
602  $schema1['host']=$node_data1['structure']['schema']['host'];
603  }
604 
605  //Check connection relation of node and node1
606  if(isset($schema['host']) && isset($schema1['host']) && isset($schema['port']) && isset($schema1['port']) &&
607  ($schema['host']==$schema1['host'] || $schema['host']=='*') && ($schema['port']==$schema1['port'])){
608  $node_info[$node1]['structure']['schema']['connection']=$node_data['structure']['schema']['name'];
609  }
610  }
611  }
612  }
613 
614  return $ret;
615 }
616 
617 
618 /*
619  * @desc execute manager command : scan and executes command on specified nodes, return json encoded results
620  * @param $args array command line argiments parsed
621  *
622  * @return $ret array('error_code'=>0, 'error_message'=>'', 'data'=>null), data - json encoded results or empty string if error
623  */
624 function hce_manage_command_cluster_node_handler_command($args, $handler=HCE_HANDLER_ADMIN, $command=HCE_CMD_ECHO, $parameters=''){
625  $ret=array();
626 
627  //Get nodes info
628  $node_info=hce_manage_nodes_get_info($args);
629 
630  //Execute command for all nodes
631  foreach($node_info as $node=>$node_data){
632  if($node_data['error']==0){
633  $ret[$node]=hce_manage_node_handler_command($node_data['host'], $node_data['port'], $handler, $command, $parameters, $args['timeout']);
634  if($ret[$node]['error']==0){
635  $ret[$node]=$ret[$node]['data'];
636  }else{
637  $ret[$node]='Error '.$ret[$node]['error'];
638  }
639  }else{
640  if($node_data['error']==-5){
641  $node_data['error']='Node not found at '.$node.' or connection timeout.';
642  }
643  $ret[$node]='Error '.$node_data['error'];
644  }
645  }
646 
647  return $ret;
648 }
649 
650 ?>