开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
forked from Jasen/cetus
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (14)
标签 (23)
master
test
feature/backends
info/log
subtable
tmp/ad593ef
partition
optimize
bugfix/drop_database
parser/index_hint
single-process
enhance/persistence
enhance/mgr
optimize_sql_parser
v2.3.7
v2.3.6
v2.3.5
v2.3.3
v2.3.2
v2.3.1
v2.3.0
v2.2.9
v2.2.8
v2.2.7
v1.3.0
v2.2.6
v2.2.5
v2.2.4
v1.2.0
v2.2.3
v2.2.1
v2.2.0
v1.1.1
v2.1.0
master
分支 (14)
标签 (23)
master
test
feature/backends
info/log
subtable
tmp/ad593ef
partition
optimize
bugfix/drop_database
parser/index_hint
single-process
enhance/persistence
enhance/mgr
optimize_sql_parser
v2.3.7
v2.3.6
v2.3.5
v2.3.3
v2.3.2
v2.3.1
v2.3.0
v2.2.9
v2.2.8
v2.2.7
v1.3.0
v2.2.6
v2.2.5
v2.2.4
v1.2.0
v2.2.3
v2.2.1
v2.2.0
v1.1.1
v2.1.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (14)
标签 (23)
master
test
feature/backends
info/log
subtable
tmp/ad593ef
partition
optimize
bugfix/drop_database
parser/index_hint
single-process
enhance/persistence
enhance/mgr
optimize_sql_parser
v2.3.7
v2.3.6
v2.3.5
v2.3.3
v2.3.2
v2.3.1
v2.3.0
v2.2.9
v2.2.8
v2.2.7
v1.3.0
v2.2.6
v2.2.5
v2.2.4
v1.2.0
v2.2.3
v2.2.1
v2.2.0
v1.1.1
v2.1.0
cetus
/
src
/
chassis-mainloop.c
cetus
/
src
/
chassis-mainloop.c
chassis-mainloop.c 10.35 KB
一键复制 编辑 原始数据 按行查看 历史
wangbin579 提交于 2019年06月04日 15:57 +08:00 . Fix memory leaks revealed by valgrind 3.15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/* $%BEGINLICENSE%$
Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; version 2 of the
License.
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
$%ENDLICENSE%$ */
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h> /* event.h need struct timeval */
#endif
#ifdef HAVE_PWD_H
#include <pwd.h> /* getpwnam() */
#endif
#include <sys/socket.h> /* for SOCK_STREAM and AF_UNIX/AF_INET */
#include <glib.h>
#include "glib-ext.h"
#include "network-mysqld.h"
#include "chassis-plugin.h"
#include "chassis-mainloop.h"
#include "chassis-event.h"
#include "chassis-log.h"
#include "chassis-timings.h"
#include "cetus-process-cycle.h"
#include "chassis-sql-log.h"
static volatile sig_atomic_t signal_shutdown;
extern int cetus_process_id;
/**
* check if the libevent headers we built against match the
* library we run against
*/
int
chassis_check_version(const char *lib_version, const char *hdr_version)
{
int lib_maj, lib_min;
int hdr_maj, hdr_min;
int scanned_fields;
if (2 != (scanned_fields = sscanf(lib_version, "%d.%d", &lib_maj, &lib_min))) {
g_critical("%s: library version %s failed to parse: %d", G_STRLOC, lib_version, scanned_fields);
return -1;
}
if (2 != (scanned_fields = sscanf(hdr_version, "%d.%d", &hdr_maj, &hdr_min))) {
g_critical("%s: header version %s failed to parse: %d", G_STRLOC, hdr_version, scanned_fields);
return -1;
}
if (lib_maj == hdr_maj && lib_min == hdr_min) {
return 0;
}
return -1;
}
/**
* create a global context
*/
chassis *
chassis_new()
{
chassis *chas;
if (0 != chassis_check_version(event_get_version(), _EVENT_VERSION)) {
g_critical("%s: chassis is built against libevent %s, but now runs against %s",
G_STRLOC, _EVENT_VERSION, event_get_version());
return NULL;
}
chas = g_new0(chassis, 1);
chas->modules = g_ptr_array_new();
chas->event_hdr_version = g_strdup(_EVENT_VERSION);
chas->shutdown_hooks = chassis_shutdown_hooks_new();
chas->startup_time = time(0);
chas->pid_file = NULL;
chas->log_level = NULL;
chas->log_xa_filename = NULL;
chas->remote_config_url = NULL;
chas->default_file = NULL;
chas->group_replication_mode = 0;
chas->sql_mgr = sql_log_alloc();
return chas;
}
static void
g_queue_free_cache_index(gpointer q)
{
GQueue *queue = q;
query_cache_index_item *index;
while ((index = g_queue_pop_head(queue))) {
g_free(index->key);
g_free(index);
}
g_queue_free(queue);
}
/**
* free the global scope
*
* closes all open connections, cleans up all plugins
*
* @param chas global context
*/
void
chassis_free(chassis *chas)
{
guint i;
#ifdef HAVE_EVENT_BASE_FREE
const char *version;
#endif
if (!chas)
return;
/* init the shutdown, without freeing share structures */
if (chas->priv_shutdown)
chas->priv_shutdown(chas, chas->priv);
/* call the destructor for all plugins */
for (i = 0; i < chas->modules->len; i++) {
chassis_plugin *p = g_ptr_array_index(chas->modules, i);
g_assert(p->destroy);
p->destroy(chas, p->config);
}
/* cleanup the global 3rd party stuff before we unload the modules */
chassis_shutdown_hooks_call(chas->shutdown_hooks);
for (i = 0; i < chas->modules->len; i++) {
chassis_plugin *p = chas->modules->pdata[i];
chassis_plugin_free(p);
}
g_ptr_array_free(chas->modules, TRUE);
if (chas->base_dir)
g_free(chas->base_dir);
if (chas->conf_dir)
g_free(chas->conf_dir);
if (chas->plugin_dir)
g_free(chas->plugin_dir);
if (chas->user)
g_free(chas->user);
if (chas->default_db)
g_free(chas->default_db);
if (chas->ifname)
g_free(chas->ifname);
if (chas->default_username)
g_free(chas->default_username);
if (chas->default_hashed_pwd)
g_free(chas->default_hashed_pwd);
if (chas->query_cache_table)
g_hash_table_destroy(chas->query_cache_table);
if (chas->cache_index)
g_queue_free_cache_index(chas->cache_index);
if (chas->unix_socket_name) {
g_free(chas->unix_socket_name);
}
if (chas->trx_isolation_level) {
g_free(chas->trx_isolation_level);
}
if (chas->default_charset) {
g_free(chas->default_charset);
}
g_free(chas->event_hdr_version);
chassis_shutdown_hooks_free(chas->shutdown_hooks);
if (chas->priv_finally_free_shared)
chas->priv_finally_free_shared(chas, chas->priv);
/* free the pointers _AFTER_ the modules are shutdown */
if (chas->priv_free)
chas->priv_free(chas, chas->priv);
#ifdef HAVE_EVENT_BASE_FREE
/* only recent versions have this call */
version = event_get_version();
/* libevent < 1.3e doesn't cleanup its own fds from the event-queue in signal_init()
* calling event_base_free() would cause a assert() on shutdown
*/
if (version && (strcmp(version, "1.3e") >= 0)) {
if (chas->event_base) {
event_base_free(chas->event_base);
}
}
#endif
if (chas->config_manager)
chassis_config_free(chas->config_manager);
if(chas->pid_file) {
g_free(chas->pid_file);
}
if (chas->old_pid_file) {
g_free(chas->old_pid_file);
}
if(chas->log_level) {
g_free(chas->log_level);
}
if (chas->plugin_names) {
g_strfreev(chas->plugin_names);
}
if(chas->log_xa_filename) {
g_free(chas->log_xa_filename);
}
if(chas->remote_config_url) {
g_free(chas->remote_config_url);
}
if(chas->default_file) {
g_free(chas->default_file);
}
if(chas->sql_mgr) {
sql_log_free(chas->sql_mgr);
}
if (chas->argv) {
for (i = 0; i < chas->argc; i++) {
free(chas->argv[i]);
}
free(chas->argv);
}
g_free(chas);
}
void
chassis_set_shutdown_location(const gchar *location)
{
if (signal_shutdown == 0) {
g_message("Initiating shutdown, requested from %s", (location != NULL ? location : "signal handler"));
}
signal_shutdown = 1;
cetus_terminate = 1;
}
gboolean
chassis_is_shutdown()
{
return signal_shutdown == 1 || cetus_terminate == 1;
}
/**
* forward libevent messages to the glib error log
*/
static void
event_log_use_glib(int libevent_log_level, const char *msg)
{
/* map libevent to glib log-levels */
GLogLevelFlags glib_log_level = G_LOG_LEVEL_DEBUG;
if (libevent_log_level == _EVENT_LOG_DEBUG)
glib_log_level = G_LOG_LEVEL_DEBUG;
else if (libevent_log_level == _EVENT_LOG_MSG)
glib_log_level = G_LOG_LEVEL_MESSAGE;
else if (libevent_log_level == _EVENT_LOG_WARN)
glib_log_level = G_LOG_LEVEL_WARNING;
else if (libevent_log_level == _EVENT_LOG_ERR)
glib_log_level = G_LOG_LEVEL_CRITICAL;
g_log(G_LOG_DOMAIN, glib_log_level, "(libevent) %s", msg);
}
int
chassis_mainloop(void *_chas)
{
chassis *chas = _chas;
/* redirect logging from libevent to glib */
event_set_log_callback(event_log_use_glib);
/* add a event-handler for the "main" events */
chassis_event_loop_t *mainloop = chassis_event_loop_new();
chas->event_base = mainloop;
g_assert(chas->event_base);
/*
* drop root privileges if requested
*/
if (chas->user) {
struct passwd *user_info;
uid_t user_id = geteuid();
/* Don't bother if we aren't superuser */
if (user_id) {
g_critical("can only use the --user switch if running as root");
return -1;
}
if (NULL == (user_info = getpwnam(chas->user))) {
g_critical("unknown user: %s", chas->user);
return -1;
}
if (chas->log->log_filename) {
/* chown logfile */
if (-1 == chown(chas->log->log_filename, user_info->pw_uid, user_info->pw_gid)) {
g_critical("%s: chown(%s) failed: %s", G_STRLOC, chas->log->log_filename, g_strerror(errno));
return -1;
}
}
if (setgid(user_info->pw_gid) == 0) {
if (setuid(user_info->pw_uid) == 0) {
}
}
g_debug("now running as user: %s (%d/%d)", chas->user, user_info->pw_uid, user_info->pw_gid);
}
#if !GLIB_CHECK_VERSION(2, 32, 0)
/* GLIB below 2.32 must call thread_init if multi threads */
g_thread_init(NULL);
#endif
if (cetus_init_signals() == -1 ) {
return 1;
}
cetus_master_process_cycle(chas);
return 0;
}
#ifndef SIMPLE_PARSER
uint64_t
incremental_guid_get_next(struct incremental_guid_state_t *s)
{
uint64_t uniq_id = 0;
static uint64_t SEQ_MASK = (-1L ^ (-1L << 10L));
struct timeval tp;
gettimeofday(&tp, NULL);
unsigned int msec = tp.tv_usec / 1000;
uint64_t cur_time = tp.tv_sec;
uniq_id = cur_time << 32;
uniq_id |= (msec << 22);
uniq_id |= (s->worker_id & 0xfff) << 10;
if (cur_time == s->last_sec && msec == s->last_msec) {
s->seq_id = (s->seq_id + 1) & SEQ_MASK;
if (s->seq_id == 0) {
g_critical("%s:too many calls in one millisecond", G_STRLOC);
}
} else {
s->seq_id = 0;
}
s->last_sec = cur_time;
s->last_msec = msec;
uniq_id |= s->seq_id;
return uniq_id;
}
void
incremental_guid_init(struct incremental_guid_state_t *s)
{
struct timeval tp;
gettimeofday(&tp, NULL);
s->worker_id = (s->worker_id << MAX_WORK_PROCESSES_SHIFT) + cetus_process_id;
g_message("internal worker id:%d", s->worker_id);
s->last_sec = tp.tv_sec;
s->last_msec = tp.tv_usec;
s->seq_id = 0;
}
#endif
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Cetus是由C语言开发的关系型数据库MySQL的中间件,主要提供了一个全面的数据库访问代理功能。Cetus连接方式与MySQL基本兼容,应用程序几乎不用修改即可通过Cetus访问数据库,实现了数据库层的水平扩展和高可用。
暂无标签
GPL-2.0
使用 GPL-2.0 开源许可协议
, GPL-2.0
使用 GPL-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/netdebug/cetus.git
git@gitee.com:netdebug/cetus.git
netdebug
cetus
cetus
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /