ROLE command: provide information about the role of the instance * (master or slave) and additional information related to replication * in an easy to process format. */
| 2807 | * (master or slave) and additional information related to replication |
| 2808 | * in an easy to process format. */ |
| 2809 | void roleCommand(client *c) { |
| 2810 | if (server.masterhost == NULL) { |
| 2811 | listIter li; |
| 2812 | listNode *ln; |
| 2813 | void *mbcount; |
| 2814 | int slaves = 0; |
| 2815 | |
| 2816 | addReplyArrayLen(c,3); |
| 2817 | addReplyBulkCBuffer(c,"master",6); |
| 2818 | addReplyLongLong(c,server.master_repl_offset); |
| 2819 | mbcount = addReplyDeferredLen(c); |
| 2820 | listRewind(server.slaves,&li); |
| 2821 | while((ln = listNext(&li))) { |
| 2822 | client *slave = ln->value; |
| 2823 | char ip[NET_IP_STR_LEN], *slaveaddr = slave->slave_addr; |
| 2824 | |
| 2825 | if (!slaveaddr) { |
| 2826 | if (connPeerToString(slave->conn,ip,sizeof(ip),NULL) == -1) |
| 2827 | continue; |
| 2828 | slaveaddr = ip; |
| 2829 | } |
| 2830 | if (slave->replstate != SLAVE_STATE_ONLINE) continue; |
| 2831 | addReplyArrayLen(c,3); |
| 2832 | addReplyBulkCString(c,slaveaddr); |
| 2833 | addReplyBulkLongLong(c,slave->slave_listening_port); |
| 2834 | addReplyBulkLongLong(c,slave->repl_ack_off); |
| 2835 | slaves++; |
| 2836 | } |
| 2837 | setDeferredArrayLen(c,mbcount,slaves); |
| 2838 | } else { |
| 2839 | char *slavestate = NULL; |
| 2840 | |
| 2841 | addReplyArrayLen(c,5); |
| 2842 | addReplyBulkCBuffer(c,"slave",5); |
| 2843 | addReplyBulkCString(c,server.masterhost); |
| 2844 | addReplyLongLong(c,server.masterport); |
| 2845 | if (slaveIsInHandshakeState()) { |
| 2846 | slavestate = "handshake"; |
| 2847 | } else { |
| 2848 | switch(server.repl_state) { |
| 2849 | case REPL_STATE_NONE: slavestate = "none"; break; |
| 2850 | case REPL_STATE_CONNECT: slavestate = "connect"; break; |
| 2851 | case REPL_STATE_CONNECTING: slavestate = "connecting"; break; |
| 2852 | case REPL_STATE_TRANSFER: slavestate = "sync"; break; |
| 2853 | case REPL_STATE_CONNECTED: slavestate = "connected"; break; |
| 2854 | default: slavestate = "unknown"; break; |
| 2855 | } |
| 2856 | } |
| 2857 | addReplyBulkCString(c,slavestate); |
| 2858 | addReplyLongLong(c,server.master ? server.master->reploff : -1); |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | /* Send a REPLCONF ACK command to the master to inform it about the current |
| 2863 | * processed offset. If we are not connected with a master, the command has |
nothing calls this directly
no test coverage detected