|
@@ -10,9 +10,14 @@
|
|
|
#include "core_entry.h"
|
|
|
#include "agent_entry.h"
|
|
|
#include "proc_title.h"
|
|
|
+#include "mxml.h"
|
|
|
+#include "../agent/my/my_comm.h"
|
|
|
|
|
|
extern char cache_file[256];
|
|
|
extern char table_file[256];
|
|
|
+#define ROOT_PATH "/etc/dtc/"
|
|
|
+char agent_file[256] = "/etc/dtc/agent.xml";
|
|
|
+std::map<std::string, std::string> map_dtc_conf; //key:value --> dtc addr:conf file name
|
|
|
|
|
|
#define DA_VERSION_MAJOR 1
|
|
|
#define DA_VERSION_MINOR 0
|
|
@@ -224,6 +229,196 @@ int init_watchdog()
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+bool ParseAgentConf(std::string path){
|
|
|
+ FILE *fp = fopen(path.c_str(), "r");
|
|
|
+ if (fp == NULL) {
|
|
|
+ log4cplus_error("conf: failed to open configuration '%s': %s", path.c_str(), strerror(errno));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ mxml_node_t* tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
|
|
|
+ if (tree == NULL) {
|
|
|
+ log4cplus_error("mxmlLoadFile error, file: %s", path.c_str());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ fclose(fp);
|
|
|
+
|
|
|
+ mxml_node_t *poolnode, *servernode, *instancenode, *lognode;
|
|
|
+
|
|
|
+ for (poolnode = mxmlFindElement(tree, tree, "MODULE",
|
|
|
+ NULL, NULL, MXML_DESCEND); poolnode != NULL;
|
|
|
+ poolnode = mxmlFindElement(poolnode, tree, "MODULE",
|
|
|
+ NULL, NULL, MXML_DESCEND))
|
|
|
+ {
|
|
|
+
|
|
|
+ for (servernode = mxmlFindElement(poolnode, poolnode, "CACHESHARDING",
|
|
|
+ NULL, NULL, MXML_DESCEND); servernode != NULL; servernode =
|
|
|
+ mxmlFindElement(servernode, poolnode, "CACHESHARDING",
|
|
|
+ NULL, NULL, MXML_DESCEND))
|
|
|
+ {
|
|
|
+ char *nodev = (char *)mxmlElementGetAttr(servernode, "ShardingName");
|
|
|
+ if (nodev == NULL) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(strcmp(nodev, "complex") == 0)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ for (instancenode = mxmlFindElement(servernode, servernode, "INSTANCE",
|
|
|
+ NULL, NULL, MXML_DESCEND); instancenode != NULL; instancenode =
|
|
|
+ mxmlFindElement(instancenode, servernode, "INSTANCE",
|
|
|
+ NULL, NULL, MXML_DESCEND))
|
|
|
+ {
|
|
|
+
|
|
|
+ char *argment = (char *)mxmlElementGetAttr(instancenode, "Enable");
|
|
|
+ if (argment == NULL) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(strcmp(argment, "true") != 0)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ argment = (char *)mxmlElementGetAttr(instancenode, "Addr");
|
|
|
+ if (argment == NULL) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string listen_on = argment;
|
|
|
+ log4cplus_debug("addr:%s %s", argment, (char *)mxmlElementGetAttr(poolnode, "Mid"));
|
|
|
+ std::string::size_type pos = listen_on.find_last_of(":");
|
|
|
+ if(pos == std::string::npos){
|
|
|
+ log4cplus_error("string find error, file: %s", path.c_str());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ std::string addr = listen_on.substr(0, pos);
|
|
|
+ char filename[250] = {0};
|
|
|
+ sprintf(filename, "dtc-conf-%d.yaml", atoi((char *)mxmlElementGetAttr(poolnode, "Mid")));
|
|
|
+ map_dtc_conf[addr] = filename;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mxmlDelete(tree);
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+std::string send_select_dtcyaml(const char* serverIp, int port)
|
|
|
+{
|
|
|
+ log4cplus_debug("server ip:%s, port:%d", serverIp, port);
|
|
|
+ sockaddr_in sendSockAddr;
|
|
|
+ bzero((char*)&sendSockAddr, sizeof(sendSockAddr));
|
|
|
+ sendSockAddr.sin_family = AF_INET;
|
|
|
+ sendSockAddr.sin_addr.s_addr = inet_addr(serverIp);
|
|
|
+ sendSockAddr.sin_port = htons(port);
|
|
|
+ int clientSd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
+ int status = connect(clientSd,
|
|
|
+ (sockaddr*) &sendSockAddr, sizeof(sendSockAddr));
|
|
|
+ if(status < 0)
|
|
|
+ {
|
|
|
+ log4cplus_error("Error connecting to socket!");
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ log4cplus_debug("Connected to the server!");
|
|
|
+ int bytesRead = 0, bytesWritten = 0;
|
|
|
+ char greeting[500] = {0};
|
|
|
+ bytesRead = recv(clientSd, (char*)&greeting, sizeof(greeting), 0);
|
|
|
+ log4cplus_debug("greeting msg len:%d", bytesRead);
|
|
|
+ char send_msg[1024] = {0};
|
|
|
+ int len = 0;
|
|
|
+ struct DTC_HEADER_V2 send_header = {0};
|
|
|
+ int cmd_len = strlen("select dtcyaml");
|
|
|
+ send_header.version = 2;
|
|
|
+ send_header.packet_len = MYSQL_HEADER_SIZE + 3 + cmd_len + sizeof(send_header);
|
|
|
+ memcpy(send_msg, &send_header, sizeof(struct DTC_HEADER_V2));
|
|
|
+ len += sizeof(struct DTC_HEADER_V2);
|
|
|
+ uint8_t send_mysql_header[MYSQL_HEADER_SIZE] = {0};
|
|
|
+ int_conv_3(send_mysql_header, (uint)cmd_len+3);
|
|
|
+ send_mysql_header[3] = 0; //pkt_nr;
|
|
|
+ memcpy(send_msg + sizeof(struct DTC_HEADER_V2), send_mysql_header, MYSQL_HEADER_SIZE);
|
|
|
+ len += MYSQL_HEADER_SIZE;
|
|
|
+ send_msg[sizeof(struct DTC_HEADER_V2) + MYSQL_HEADER_SIZE] = 0x03; //query
|
|
|
+ send_msg[sizeof(struct DTC_HEADER_V2) + MYSQL_HEADER_SIZE + 1] = 0x0;
|
|
|
+ send_msg[sizeof(struct DTC_HEADER_V2) + MYSQL_HEADER_SIZE + 2] = 0x01;
|
|
|
+ len += 3;
|
|
|
+ memcpy(send_msg + sizeof(struct DTC_HEADER_V2) + MYSQL_HEADER_SIZE + 3, "select dtcyaml", cmd_len);
|
|
|
+ len += cmd_len;
|
|
|
+ bytesWritten = send(clientSd, send_msg, len, 0);
|
|
|
+ log4cplus_debug("Awaiting server response..., sent len:%d", len);
|
|
|
+ struct DTC_HEADER_V2 header = {0};
|
|
|
+ bytesRead = recv(clientSd, (char*)&header, sizeof(header), 0);
|
|
|
+ log4cplus_debug("bytesRead: %d, packet len: %d, dbname len:%d ver: %d admin: %d, layer: %d, id: %d",
|
|
|
+ bytesRead, header.packet_len, header.dbname_len, header.version, header.admin, header.layer, header.id);
|
|
|
+ std::string conf_str = "";
|
|
|
+ if(header.packet_len > 0 && header.packet_len <= 65535)
|
|
|
+ {
|
|
|
+ int ilen = header.packet_len - sizeof(header) + 1;
|
|
|
+ char* msg = new char[ilen];
|
|
|
+ memset(msg, 0, ilen);
|
|
|
+ log4cplus_debug("ilen: %d", ilen);
|
|
|
+ bytesRead = recv(clientSd, (char*)msg, ilen - 1, 0);
|
|
|
+ log4cplus_debug("bytesRead: %d", bytesRead);
|
|
|
+ conf_str = msg;
|
|
|
+ delete msg;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ log4cplus_error("header.packet_len: %d error", header.packet_len);
|
|
|
+ }
|
|
|
+
|
|
|
+ close(clientSd);
|
|
|
+ log4cplus_debug("Bytes written: %d, Bytes read: %d, Connection closed", bytesWritten, bytesRead);
|
|
|
+ return conf_str;
|
|
|
+}
|
|
|
+
|
|
|
+int get_all_dtc_confs()
|
|
|
+{
|
|
|
+ //load xml
|
|
|
+ if(false == ParseAgentConf(agent_file)){
|
|
|
+ log4cplus_error("DataConf ParseConf error.");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ //get each dtc instance conn
|
|
|
+ int success_num = 0;
|
|
|
+ std::map<std::string, std::string>::iterator it;
|
|
|
+ for(it = map_dtc_conf.begin(); it != map_dtc_conf.end(); it++)
|
|
|
+ {
|
|
|
+ std::string addr = (*it).first;
|
|
|
+ std::string filename = (*it).second;
|
|
|
+
|
|
|
+ //TODO: send select dtcyaml
|
|
|
+ char* content = NULL;
|
|
|
+ int content_len = 0;
|
|
|
+ log4cplus_debug("addr:%s", addr.c_str());
|
|
|
+ std::string str = send_select_dtcyaml((addr.substr(0, addr.find(':'))).c_str(), atoi((addr.substr(addr.find(':')+1)).c_str()));
|
|
|
+ content = str.c_str();
|
|
|
+ content_len = str.length();
|
|
|
+ log4cplus_debug("content:%s", content);
|
|
|
+
|
|
|
+ //save conf file and rename.
|
|
|
+ if(content != NULL && content_len > 0)
|
|
|
+ {
|
|
|
+ std::string filepath = string(ROOT_PATH) + filename;
|
|
|
+ log4cplus_debug("filepath:%s", filepath.c_str());
|
|
|
+ FILE *fp = fopen(filepath.c_str(), "w");
|
|
|
+ if (fp == NULL)
|
|
|
+ {
|
|
|
+ log4cplus_error("open file %s error", filepath.c_str());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ fprintf(fp, "%s", content);
|
|
|
+
|
|
|
+ fclose(fp);
|
|
|
+ success_num++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(success_num > 0)
|
|
|
+ return 0;
|
|
|
+ else
|
|
|
+ return -2;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
{
|
|
@@ -249,6 +444,15 @@ int main(int argc, char* argv[])
|
|
|
exit(0);
|
|
|
}
|
|
|
|
|
|
+ if(load_agent || load_sharding || load_asyncconn)
|
|
|
+ {
|
|
|
+ if(get_all_dtc_confs() < 0)
|
|
|
+ {
|
|
|
+ log4cplus_error("get dtc conf files failed, process exit right now.");
|
|
|
+ exit(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (load_sharding || load_all) {
|
|
|
if(start_sharding(wdog, delay) < 0)
|
|
|
log4cplus_error("start sharding failed.");
|