Home Database Mysql Tutorial 在Server层实现Kill Idle Transaction

在Server层实现Kill Idle Transaction

Jun 07, 2016 pm 04:33 PM
idle kill server accomplish

本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/database/server_kill_idle_transaction.html 在上一篇文章里我们写了如何针对InnoDB清理空闲事务《如何杀掉空闲事务》

本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/database/server_kill_idle_transaction.html

在上一篇文章里我们写了如何针对InnoDB清理空闲事务《如何杀掉空闲事务》,在@sleebin9 的提示下,这个功能不仅可以针对InnoDB,也可以用于所有MySQL的事务引擎。

如何在Server层实现呢,sql/sql_parse.cc的do_command()函数是个好函数,连接线程会循环调用do_command()来读取并执行命令,在do_command()函数中,会调用my_net_set_read_timeout(net, thd->variables.net_wait_timeout)来设置线程socket连接超时时间,于是在这里可以下手。
主要代码:

<span style="color: #0000dd;">830</span>   <span style="color: #ff0000; font-style: italic;">/*
 831     This thread will do a blocking read from the client which
 832     will be interrupted when the next command is received from
 833     the client, the connection is closed or "net_wait_timeout"
 834     number of seconds has passed
 835   */</span>
 <span style="color: #0000dd;">836</span>   <span style="color: #ff0000; font-style: italic;">/* Add For Kill Idle Transaction By P.Linux */</span>
 <span style="color: #0000dd;">837</span>   <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>active_transaction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #0000dd;">838</span>   <span style="color: #008000;">&#123;</span>
 <span style="color: #0000dd;">839</span>     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>variables.<span style="color: #007788;">trx_idle_timeout</span> <span style="color: #000080;">></span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
 <span style="color: #0000dd;">840</span>     <span style="color: #008000;">&#123;</span>
 <span style="color: #0000dd;">841</span>       my_net_set_read_timeout<span style="color: #008000;">&#40;</span>net, thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>variables.<span style="color: #007788;">trx_idle_timeout</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000dd;">842</span>     <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>variables.<span style="color: #007788;">trx_readonly_idle_timeout</span> <span style="color: #000080;">></span> <span style="color: #0000dd;">0</span> <span style="color: #000040;">&&</span> thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>is_readonly_trx<span style="color: #008000;">&#41;</span>
 <span style="color: #0000dd;">843</span>     <span style="color: #008000;">&#123;</span>
 <span style="color: #0000dd;">844</span>       my_net_set_read_timeout<span style="color: #008000;">&#40;</span>net, thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>variables.<span style="color: #007788;">trx_readonly_idle_timeout</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000dd;">845</span>     <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>variables.<span style="color: #007788;">trx_changes_idle_timeout</span> <span style="color: #000080;">></span> <span style="color: #0000dd;">0</span> <span style="color: #000040;">&&</span> <span style="color: #000040;">!</span>thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>is_readonly_trx<span style="color: #008000;">&#41;</span>
 <span style="color: #0000dd;">846</span>     <span style="color: #008000;">&#123;</span>
 <span style="color: #0000dd;">847</span>       my_net_set_read_timeout<span style="color: #008000;">&#40;</span>net, thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>variables.<span style="color: #007788;">trx_changes_idle_timeout</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000dd;">848</span>     <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
 <span style="color: #0000dd;">849</span>       my_net_set_read_timeout<span style="color: #008000;">&#40;</span>net, thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>variables.<span style="color: #007788;">net_wait_timeout</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000dd;">850</span>     <span style="color: #008000;">&#125;</span>
 <span style="color: #0000dd;">851</span>   <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
 <span style="color: #0000dd;">852</span>     my_net_set_read_timeout<span style="color: #008000;">&#40;</span>net, thd<span style="color: #000040;">-</span><span style="color: #000080;">></span>variables.<span style="color: #007788;">net_wait_timeout</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000dd;">853</span>   <span style="color: #008000;">&#125;</span>
 <span style="color: #0000dd;">854</span>   <span style="color: #ff0000; font-style: italic;">/* End */</span>
Copy after login

大家看明白了吗?其实这是偷梁换柱,本来在这里是要设置wait_timeout的,先判断线程是不是在事务里,就可以转而实现空闲事务的超时。

trx_idle_timeout 控制所有事务的超时,优先级最高
trx_changes_idle_timeout 控制非只读事务的超时
trx_readonly_idle_timeout 控制只读事务的超时

效果:

root@localhost : <span style="color: #66cc66;">&#40;</span>none<span style="color: #66cc66;">&#41;</span> 08:<span style="color: #cc66cc;">39</span>:<span style="color: #cc66cc;">49</span><span style="color: #66cc66;">></span> <span style="color: #993333; font-weight: bold;">set</span> autocommit <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> ;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span> rows affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
 
root@localhost : <span style="color: #66cc66;">&#40;</span>none<span style="color: #66cc66;">&#41;</span> 08:<span style="color: #cc66cc;">39</span>:<span style="color: #cc66cc;">56</span><span style="color: #66cc66;">></span> <span style="color: #993333; font-weight: bold;">set</span> trx_idle_timeout <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span> rows affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
 
root@localhost : <span style="color: #66cc66;">&#40;</span>none<span style="color: #66cc66;">&#41;</span> 08:<span style="color: #cc66cc;">40</span>:<span style="color: #cc66cc;">17</span><span style="color: #66cc66;">></span> <span style="color: #993333; font-weight: bold;">use</span> perf 
<span style="color: #993333; font-weight: bold;">Database</span> changed
root@localhost : perf 08:<span style="color: #cc66cc;">40</span>:<span style="color: #cc66cc;">19</span><span style="color: #66cc66;">></span> <span style="color: #993333; font-weight: bold;">insert</span> <span style="color: #993333; font-weight: bold;">into</span> perf <span style="color: #66cc66;">&#40;</span>info <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">values</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'11'</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
 
root@localhost : perf 08:<span style="color: #cc66cc;">40</span>:<span style="color: #cc66cc;">26</span><span style="color: #66cc66;">></span> <span style="color: #993333; font-weight: bold;">select</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">from</span> perf;
ERROR <span style="color: #cc66cc;">2006</span> <span style="color: #66cc66;">&#40;</span>HY000<span style="color: #66cc66;">&#41;</span>: MySQL server has gone away
No connection<span style="color: #66cc66;">.</span> Trying <span style="color: #993333; font-weight: bold;">to</span> reconnect<span style="color: #66cc66;">...</span>
Connection id:    <span style="color: #cc66cc;">6</span>
Current <span style="color: #993333; font-weight: bold;">database</span>: perf
 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------+</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> info <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------+</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">7</span> <span style="color: #66cc66;">|</span> aaaa <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">9</span> <span style="color: #66cc66;">|</span> aaaa <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">11</span> <span style="color: #66cc66;">|</span> aaaa <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------+</span>
<span style="color: #cc66cc;">3</span> rows <span style="color: #993333; font-weight: bold;">in</span> <span style="color: #993333; font-weight: bold;">set</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
Copy after login

完整的patch这里下载:
Note: There is a file embedded within this post, please visit this post to download the file.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Windows Server 2025 preview version welcomes update, Microsoft improves Insiders testing experience Windows Server 2025 preview version welcomes update, Microsoft improves Insiders testing experience Feb 19, 2024 pm 02:36 PM

On the occasion of releasing the build 26040 version of Windows Server, Microsoft announced the official name of the product: Windows Server 2025. Also launched is the Windows11WindowsInsiderCanaryChannel version build26040. Some friends may still remember that many years ago someone successfully converted Windows NT from workstation mode to server mode, showing the commonalities between various versions of Microsoft operating systems. Although there are clear differences between Microsoft's current version of the server operating system and Windows 11, those who pay attention to the details may be curious: why Windows Server updated the brand,

How to implement exact division operation in Golang How to implement exact division operation in Golang Feb 20, 2024 pm 10:51 PM

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

See all articles