From b49a3370f5d107f651ba9fcea5d6c82ebee03edd Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 31 Mar 2021 11:00:03 +0800 Subject: [PATCH] add sf_cluster_cfg.[hc] --- src/Makefile.in | 4 +- src/sf_cluster_cfg.c | 144 +++++++++++++++++++++++++++++++++++++++++++ src/sf_cluster_cfg.h | 42 +++++++++++++ src/sf_types.h | 8 +++ 4 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 src/sf_cluster_cfg.c create mode 100644 src/sf_cluster_cfg.h diff --git a/src/Makefile.in b/src/Makefile.in index be876a8..a4979c5 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -7,7 +7,7 @@ TARGET_LIB = $(TARGET_PREFIX)/$(LIB_VERSION) TOP_HEADERS = sf_types.h sf_global.h sf_define.h sf_nio.h sf_service.h \ sf_func.h sf_util.h sf_configs.h sf_proto.h sf_binlog_writer.h \ - sf_sharding_htable.h sf_connection_manager.h + sf_cluster_cfg.h sf_sharding_htable.h sf_connection_manager.h IDEMP_SERVER_HEADER = idempotency/server/server_types.h \ idempotency/server/server_channel.h \ @@ -25,7 +25,7 @@ ALL_HEADERS = $(TOP_HEADERS) $(IDEMP_SERVER_HEADER) $(IDEMP_CLIENT_HEADER) SHARED_OBJS = sf_nio.lo sf_service.lo sf_global.lo \ sf_func.lo sf_util.lo sf_configs.lo sf_proto.lo \ sf_binlog_writer.lo sf_sharding_htable.lo \ - sf_connection_manager.lo \ + sf_cluster_cfg.lo sf_connection_manager.lo \ idempotency/server/server_channel.lo \ idempotency/server/request_htable.lo \ idempotency/server/channel_htable.lo \ diff --git a/src/sf_cluster_cfg.c b/src/sf_cluster_cfg.c new file mode 100644 index 0000000..877c5df --- /dev/null +++ b/src/sf_cluster_cfg.c @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2020 YuQing <384681@qq.com> + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include "fastcommon/fast_buffer.h" +#include "fastcommon/md5.h" +#include "sf_cluster_cfg.h" + +static int calc_cluster_config_sign(SFClusterConfig *cluster) +{ + FastBuffer buffer; + int result; + + if ((result=fast_buffer_init_ex(&buffer, 1024)) != 0) { + return result; + } + fc_server_to_config_string(&cluster->server_cfg, &buffer); + my_md5_buffer(buffer.data, buffer.length, cluster->md5_digest); + + /* + { + char hex_buff[2 * sizeof(cluster->md5_digest) + 1]; + logInfo("cluster config length: %d, sign: %s", buffer.length, + bin2hex((const char *)cluster->md5_digest, + sizeof(cluster->md5_digest), hex_buff)); + } + */ + + fast_buffer_destroy(&buffer); + return 0; +} + +static int find_group_indexes_in_cluster_config(SFClusterConfig *cluster, + const char *filename) +{ + cluster->cluster_group_index = fc_server_get_group_index( + &cluster->server_cfg, "cluster"); + if (cluster->cluster_group_index < 0) { + logError("file: "__FILE__", line: %d, " + "cluster config file: %s, cluster group " + "not configurated", __LINE__, filename); + return ENOENT; + } + + cluster->service_group_index = fc_server_get_group_index( + &cluster->server_cfg, "service"); + if (cluster->service_group_index < 0) { + logError("file: "__FILE__", line: %d, " + "cluster config file: %s, service group " + "not configurated", __LINE__, filename); + return ENOENT; + } + + return 0; +} + +static int load_server_cfg(SFClusterConfig *cluster, + const char *cluster_filename, const int default_port, + char *full_server_filename, const int size) +{ + IniContext ini_context; + char *server_config_filename; + const int min_hosts_each_group = 1; + const bool share_between_groups = true; + int result; + + if ((result=iniLoadFromFile(cluster_filename, &ini_context)) != 0) { + logError("file: "__FILE__", line: %d, " + "load conf file \"%s\" fail, ret code: %d", + __LINE__, cluster_filename, result); + return result; + } + + server_config_filename = iniGetStrValue(NULL, + "server_config_filename", &ini_context); + if (server_config_filename == NULL || *server_config_filename == '\0') { + logError("file: "__FILE__", line: %d, " + "config file: %s, item \"server_config_filename\" " + "not exist or empty", __LINE__, cluster_filename); + return ENOENT; + } + + resolve_path(cluster_filename, server_config_filename, + full_server_filename, size); + if ((result=fc_server_load_from_file_ex(&cluster->server_cfg, + full_server_filename, default_port, + min_hosts_each_group, share_between_groups)) != 0) + { + return result; + } + + iniFreeContext(&ini_context); + return 0; +} + +int sf_load_cluster_config_ex(SFClusterConfig *cluster, + IniFullContext *ini_ctx, const int default_port, + char *full_server_filename, const int size) +{ + int result; + char *cluster_config_filename; + char full_cluster_filename[PATH_MAX]; + + cluster_config_filename = iniGetStrValue(ini_ctx->section_name, + "cluster_config_filename", ini_ctx->context); + if (cluster_config_filename == NULL || *cluster_config_filename == '\0') { + logError("file: "__FILE__", line: %d, " + "config file: %s, item \"cluster_config_filename\" " + "not exist or empty", __LINE__, ini_ctx->filename); + return ENOENT; + } + + resolve_path(ini_ctx->filename, cluster_config_filename, + full_cluster_filename, sizeof(full_cluster_filename)); + if ((result=load_server_cfg(cluster, full_cluster_filename, + default_port, full_server_filename, size)) != 0) + { + return result; + } + + if ((result=find_group_indexes_in_cluster_config( + cluster, ini_ctx->filename)) != 0) + { + return result; + } + + if ((result=calc_cluster_config_sign(cluster)) != 0) { + return result; + } + + return 0; +} diff --git a/src/sf_cluster_cfg.h b/src/sf_cluster_cfg.h new file mode 100644 index 0000000..dcb3112 --- /dev/null +++ b/src/sf_cluster_cfg.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 YuQing <384681@qq.com> + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + + +#ifndef _SF_CLUSTER_CFG_H +#define _SF_CLUSTER_CFG_H + +#include "sf_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int sf_load_cluster_config_ex(SFClusterConfig *cluster, + IniFullContext *ini_ctx, const int default_port, + char *full_server_filename, const int size); + +static inline int sf_load_cluster_config(SFClusterConfig *cluster, + IniFullContext *ini_ctx, const int default_port) +{ + char full_server_filename[PATH_MAX]; + return sf_load_cluster_config_ex(cluster, ini_ctx, default_port, + full_server_filename, sizeof(full_server_filename)); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/sf_types.h b/src/sf_types.h index 7d0323a..480f2ef 100644 --- a/src/sf_types.h +++ b/src/sf_types.h @@ -25,6 +25,7 @@ #include #include "fastcommon/connection_pool.h" #include "fastcommon/fast_task_queue.h" +#include "fastcommon/server_id_func.h" #define SF_ERROR_INFO_SIZE 256 @@ -197,4 +198,11 @@ typedef struct sf_key_value_array { int alloc; } SFKeyValueArray; +typedef struct sf_cluster_config { + FCServerConfig server_cfg; + unsigned char md5_digest[16]; + int cluster_group_index; + int service_group_index; +} SFClusterConfig; + #endif