|
|
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0"><link rel="preconnect" href="https://fonts.googleapis.com" crossorigin><link rel="preconnect" href="https://cdnjs.cloudflare.com" crossorigin><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><meta name="google-site-verification" content="2X6S9P7CAjXjVvw8YyQR8pCu-B0oEu7O9quLgxXuWyA"><meta name="baidu-site-verification" content="dV8JGNzi0c"><script>setTimeout(function(){var d=document.createElement("script");d.setAttribute("data-ad-client","ca-pub-7480618470784058"),d.async=!0,d.src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js",document.body.appendChild(d)},5e3),window.addEventListener?window.addEventListener("load",downloadJsAtOnload,!1):window.attachEvent?window.attachEvent("onload",downloadJsAtOnload):window.onload=downloadJsAtOnload</script><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous"><script class="next-config" data-name="main" type="application/json">{"hostname":"nicksxs.me","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.12.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果:${query}","hits_time":"找到 ${hits} 个搜索结果(用时 ${time} 毫秒)","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":true}}</script><script src="/js/config.js"></script><meta name="description" content="learn from zero,技术博客,Nicksxs,史学森"><meta property="og:type" content="website"><meta property="og:title" content="Nicksxs's Blog"><meta property="og:url" content="https://nicksxs.me/page/26/index.html"><meta property="og:site_name" content="Nicksxs's Blog"><meta property="og:description" content="learn from zero,技术博客,Nicksxs,史学森"><meta property="og:locale" content="zh_CN"><meta property="article:author" content="Nicksxs"><meta property="article:tag" content="Nicksxs,史学森,米方方,米方方的男朋友,森哥"><meta name="twitter:card" content="summary"><link rel="canonical" href="https://nicksxs.me/page/26/"><script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":true,"isPost":false,"lang":"zh-CN","comments":"","permalink":"","path":"page/26/index.html","title":""}</script><script class="next-config" data-name="calendar" type="application/json">""</script><title>Nicksxs's Blog - What hurts more, the pain of hard work or the pain of regret?</title><script async src="https://www.googletagmanager.com/gtag/js?id=UA-61358619-1"></script><script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-61358619-1","only_pageview":false}</script><script src="/js/third-party/analytics/google-analytics.js"></script><script src="/js/third-party/analytics/baidu-analytics.js"></script><script async src="https://hm.baidu.com/hm.js?20f33b3c0c0eff9b1522999c0015646d"></script><noscript><link rel="stylesheet" href="/css/noscript.css"></noscript><link rel="alternate" href="/atom.xml" title="Nicksxs's Blog" type="application/atom+xml"></head><body itemscope itemtype="http://schema.org/WebPage"><div class="headband"></div><main class="main"><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div></div><div class="site-meta"><a href="/" class="brand" rel="start"><i class="logo-line"></i><h1 class="site-title">Nicksxs's Blog</h1><i class="logo-line"></i></a><p class="site-subtitle" itemprop="description">What hurts more, the pain of hard work or the pain of regret?</p></div><div class="site-nav-right"><div class="toggle popup-trigger"><i class="fa fa-search fa-fw fa-lg"></i></div></div></div><nav class="site-nav"><ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-top"><a href="/top/" rel="section"><i class="fa fa-th fa-fw"></i>热度</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="search-pop-overlay"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input"></div><span class="popup-btn-close" role="button"><i class="fa fa-times-circle"></i></span></div><div class="search-result-container no-result"><div class="search-result-icon"><i class="fa fa-spinner fa-pulse fa-5x"></i></div></div></div></div></div><div class="toggle sidebar-toggle" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div><aside class="sidebar"><div class="sidebar-inner sidebar-overview-active"><ul class="sidebar-nav"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="sidebar-panel-container"><div class="post-toc-wrap sidebar-panel"></div><div class="site-overview-wrap sidebar-panel"><div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="Nicksxs" src="/uploads/avatar.jpg"><p class="site-author-name" itemprop="name">Nicksxs</p><div class="site-description" itemprop="description">learn from zero,技术博客,Nicksxs,史学森</div></div><div class="site-state-wrap site-overview-item animated"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">174</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"><a href="/categories/"><span class="site-state-item-count">148</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">283</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author site-overview-item animated"><span class="links-of-author-item"><a href="https://github.com/nicksxs" title="GitHub → https://github.com/nicksxs" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a> </span><span class="links-of-author-item"><a href="mailto:nicksxs1202@gmail.com" title="E-Mail → mailto:nicksxs1202@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a></span></div><div class="cc-license site-overview-item animated" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a></div><script charset="utf-8" src="/js/tagcloud.js"></script><script charset="utf-8" src="/js/tagcanvas.js"></script><div class="widget-wrap"><div id="myCanvasContainer" class="widget tagcloud"><canvas width="250" height="250" id="resCanvas"><ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/2019/" rel="tag">2019</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/2020/" rel="tag">2020</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/2021/" rel="tag">2021</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/2PC/" rel="tag">2PC</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/360-%E5%85%A8%E5%AE%B6%E6%A1%B6/" rel="tag">360 全家桶</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/3PC/" rel="tag">3PC</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/3Sum-Closest/" rel="tag">3Sum Closest</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AOP/" rel="tag">AOP</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Adaptive/" rel="tag">Adaptive</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Apollo/" rel="tag">Apollo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AutoConfiguration/" rel="tag">AutoConfiguration</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Binary-Tree/" rel="tag">Binary Tree</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Broker/" rel="tag">Broker</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C++</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CachedThreadPool/" rel="tag">CachedThreadPool</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Comparator/" rel="tag">Comparator</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/DFS/" rel="tag">DFS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/DP/" rel="tag">DP</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/DefaultMQPushConsumer/" rel="tag">DefaultMQPushConsumer</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Design-Patterns/" rel="tag">Design Patterns</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Disruptor/" rel="tag">Disruptor</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Distributed-Lock/" rel="tag">Distributed Lock</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Docker/" rel="tag">Docker</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Dockerfile/" rel="tag">Dockerfile</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Druid/" rel="tag">Druid</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Dubbo/" rel="tag">Dubbo</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/EagerThreadPool/" rel="tag">EagerThreadPool</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Evict/" rel="tag">Evict</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Filter/" rel="tag">Filter</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/First-Bad-Version/" rel="tag">First Bad Version</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/FixedThreadPool/" rel="tag">FixedThreadPool</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/G1/" rel="tag">G1</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/GC/" rel="tag">GC</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Garbage-First-Collector/" rel="tag">Garbage-First Collector</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Gogs/" rel="tag">Gogs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Homebrew/" rel="tag">Homebrew</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Inorder-Traversal/" rel="tag">Inorder Traversal</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Interceptor/" rel="tag">Interceptor</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Intersection-of-Two-Arrays/" rel="tag">Intersection of Two Arrays</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JMap/" rel="tag">JMap</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JPS/" rel="tag">JPS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JStack/" rel="tag">JStack</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JVM/" rel="tag">JVM</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/" rel="tag">Java</a><span class="tag-list-count">31</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Leetcode-42/" rel="tag">Leetcode 42</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/LimitedThreadPool/" rel="tag">LimitedThreadPool</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linked-List/" rel="tag">Linked List</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Lowest-Common-Ancestor-of-a-Binary-Tree/" rel="tag">Lowest Common Ancestor of a Binary Tree</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MQ/" rel="tag">MQ</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Mac/" rel="tag">Mac</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Maven/" rel="tag">Maven</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Median-of-Two-Sorted-Arrays/" rel="tag">Median of Two Sorted Arrays</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Mybatis/" rel="tag">Mybatis</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Mysql/" rel="tag">Mysql</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/NameServer/" rel="tag">NameServer</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/PHP/" rel="tag">PHP</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Preorder-Traversal/" rel="tag">Preorder Traversal</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Print-FooBar-Alternately/" rel="tag">Print FooBar Alternately</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/RPC/" rel="tag">RPC</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Redis/" rel="tag">Redis</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Remove-Duplicates-from-Sorted-List/" rel="tag">Remove Duplicates from Sorted List</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/RocketMQ/" rel="tag">RocketMQ</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Rotate-Image/" rel="tag">Rotate Image</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Rust/" rel="tag">Rust</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SPI/" rel="tag">SPI</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Servlet/" rel="tag">Servlet</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Sharding-Jdbc/" rel="tag">Sharding-Jdbc</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Shift-2D-Grid/" rel="tag">Shift 2D Grid</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Singleton/" rel="tag">Singleton</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Spring/" rel="tag">Spring</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Spring-Event/" rel="tag">Spring Event</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SpringBoot/" rel="tag">SpringBoot</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Sql%E6%B3%A8%E5%85%A5/" rel="tag">Sql注入</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Stream/" rel="tag">Stream</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Synchronized/" rel="tag">Synchronized</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Thread-dump/" rel="tag">Thread dump</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ThreadLocal/" rel="tag">ThreadLocal</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ThreadPool/" rel="tag">ThreadPool</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tomcat/" rel="tag">Tomcat</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Trapping-Rain-Water/" rel="tag">Trapping Rain Water</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/WeakReference/" rel="tag">WeakReference</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Web/" rel="tag">Web</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Webhook/" rel="tag">Webhook</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/WordPress/" rel="tag">WordPress</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/aqs/" rel="tag">aqs</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/await/" rel="tag">await</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/bloom-filter/" rel="tag">bloom filter</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/c/" rel="tag">c++</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cglib/" rel="tag">cglib</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cgroup/" rel="tag">cgroup</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cluster/" rel="tag">cluster</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/condition/" rel="tag">condition</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/" rel="tag">docker</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/dp/" rel="tag">dp</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/echo/" rel="tag">echo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/environment/" rel="tag">environment</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gap-lock/" rel="tag">gap lock</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gc/" rel="tag">gc</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/" rel="tag">git</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/grep/" rel="tag">grep</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hadoop/" rel="tag">hadoop</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/icu4c/" rel="tag">icu4c</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/im/" rel="tag">im</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/is-not-null/" rel="tag">is not null</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/is-null/" rel="tag">is null</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/j-u-c/" rel="tag">j.u.c</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/" rel="tag">java</a><span class="tag-list-count">31</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jvm/" rel="tag">jvm</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/leetcode/" rel="tag">leetcode</a><span class="tag-list-count">38</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/leetcode-155/" rel="tag">leetcode 155</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linked-list/" rel="tag">linked list</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/" rel="tag">linux</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/lock/" rel="tag">lock</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mfc/" rel="tag">mfc</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/min-stack/" rel="tag">min stack</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mq/" rel="tag">mq</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mvcc/" rel="tag">mvcc</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/" rel="tag">mysql</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/namespace/" rel="tag">namespace</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/next-key-lock/" rel="tag">next-key lock</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx/" rel="tag">nginx</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nullsfirst/" rel="tag">nullsfirst</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/openresty/" rel="tag">openresty</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php/" rel="tag">php</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/procedure/" rel="tag">procedure</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/read-view/" rel="tag">read view</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/" rel="tag">redis</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/scp/" rel="tag">scp</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/signal/" rel="tag">signal</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sort/" rel="tag">sort</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spark/" rel="tag">spark</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ssh/" rel="tag">ssh</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/stack/" rel="tag">stack</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/stream/" rel="tag">stream</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/string/" rel="tag">string</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/swoole/" rel="tag">swoole</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/top/" rel="tag">top</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/uname/" rel="tag">uname</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/unlock/" rel="tag">unlock</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/utf8/" rel="tag">utf8</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/utf8mb4/" rel="tag">utf8mb4</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/utf8mb4-0900-ai-ci/" rel="tag">utf8mb4_0900_ai_ci</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/utf8mb4-general-ci/" rel="tag">utf8mb4_general_ci</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/utf8mb4-unicode-ci/" rel="tag">utf8mb4_unicode_ci</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/value/" rel="tag">value</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/websocket/" rel="tag">websocket</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zookeeper/" rel="tag">zookeeper</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zsh/" rel="tag">zsh</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%89%E9%98%B6%E6%AE%B5%E6%8F%90%E4%BA%A4/" rel="tag">三阶段提交</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%8D%E5%8F%AF%E5%8F%98%E5%BC%95%E7%94%A8/" rel="tag">不可变引用</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%9C%E4%BA%AC%E5%A5%A5%E8%BF%90%E4%BC%9A/" rel="tag">东京奥运会</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%A4%E9%98%B6%E6%AE%B5%E6%8F%90%E4%BA%A4/" rel="tag">两阶段提交</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%AD%E5%B1%B1%E8%B7%AF/" rel="tag">中山路</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%AD%E5%BA%8F/" rel="tag">中序</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%AD%E9%97%B4%E4%BB%B6/" rel="tag">中间件</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%BE%E9%87%8D/" rel="tag">举重</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B9%92%E4%B9%93%E7%90%83/" rel="tag">乒乓球</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BA%8B%E5%8A%A1/" rel="tag">事务</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BA%8C%E5%8F%89%E6%A0%91/" rel="tag">二叉树</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BA%92%E6%96%A5%E9%94%81/" rel="tag">互斥锁</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BB%A3%E7%A0%81%E9%A2%98%E8%A7%A3/" rel="tag">代码题解</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BF%AE%E7%94%B5%E8%84%91%E7%9A%84/" rel="tag">修电脑的</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%81%8F%E5%90%91%E9%94%81/" rel="tag">偏向锁</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%81%A5%E5%BA%B7%E7%A0%81/" rel="tag">健康码</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%85%AC%E4%BA%A4/" rel="tag">公交</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%85%AC%E4%BA%A4%E8%BD%A6/" rel="tag">公交车</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%86%85%E5%AD%98%E5%88%86%E5%B8%83/" rel="tag">内存分布</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F/" rel="tag">内存泄漏</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%87%86%E5%A4%87/" rel="tag">准备</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%87%8F%E8%82%A5/" rel="tag">减肥</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/" rel="tag">分布式事务</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/" rel="tag">分布式锁</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%88%87%E7%89%87/" rel="tag">切片</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%88%9D%E5%A7%8B%E5%8C%96/" rel="tag">初始化</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%89%8A%E5%B3%B0%E5%A1%AB%E8%B0%B7/" rel="tag">削峰填谷</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%89%8D%E5%BA%8F/" rel="tag">前序</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8A%A0%E5%A1%9E/" rel="tag">加塞</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8A%A0%E8%BD%BD/" rel="tag">加载</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8D%95%E4%BE%8B/" rel="tag">单例</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8D%9A%E5%AE%A2%EF%BC%8C%E6%96%87%E7%AB%A0/" rel="tag">博客,文章</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8E%A6%E9%97%A8/" rel="tag">厦门</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8F%8C%E4%BA%B2%E5%A7%94%E6%B4%BE/" rel="tag">双亲委派</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8F%91%E8%A1%8C%E7%89%88/" rel="tag">发行版</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8F%A3%E7%BD%A9/" rel="tag">口罩</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8F%AF%E5%8F%98%E5%BC%95%E7%94%A8/" rel="tag">可变引用</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%90%90%E6%A7%BD/" rel="tag">吐槽</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%9B%A4%E7%89%A9%E8%B5%84/" rel="tag">囤物资</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6/" rel="tag">垃圾回收</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%9F%BA%E7%A1%80%E8%AE%BE%E6%96%BD/" rel="tag">基础设施</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%A4%A7%E6%89%AB%E9%99%A4/" rel="tag">大扫除</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AD%97%E7%AC%A6%E9%9B%86/" rel="tag">字符集</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AE%89%E5%85%A8/" rel="tag">安全</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AE%B9%E9%94%99%E6%9C%BA%E5%88%B6/" rel="tag">容错机制</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AF%84%E7%94%9F%E8%99%AB/" rel="tag">寄生虫</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B0%84%E5%87%BB/" rel="tag">射击</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B0%8F%E6%8A%80%E5%B7%A7/" rel="tag">小技巧</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B1%80%E5%8F%A3%E8%A1%97/" rel="tag">局口街</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B8%83%E9%9A%86%E8%BF%87%E6%BB%A4%E5%99%A8/" rel="tag">布隆过滤器</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B9%B2%E6%B4%BB/" rel="tag">干活</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B9%B4%E4%B8%AD%E6%80%BB%E7%BB%93/" rel="tag">年中总结</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B9%B4%E7%BB%88%E6%80%BB%E7%BB%93/" rel="tag">年终总结</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B9%B6%E5%8F%91/" rel="tag">并发</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B9%B8%E7%A6%8F%E4%BA%86%E5%90%97/" rel="tag">幸福了吗</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%B9%BB%E8%AF%BB/" rel="tag">幻读</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%BA%94%E7%94%A8/" rel="tag">应用</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%BC%80%E8%BD%A6/" rel="tag">开车</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%BC%B1%E5%BC%95%E7%94%A8/" rel="tag">弱引用</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%BD%B1%E8%AF%84/" rel="tag">影评</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%89%80%E6%9C%89%E6%9D%83/" rel="tag">所有权</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%89%93%E5%8D%A1/" rel="tag">打卡</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%89%B6%E6%A2%AF/" rel="tag">扶梯</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%8A%80%E6%9C%AF/" rel="tag">技术</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%8B%96%E6%9B%B4/" rel="tag">拖更</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%8D%A2%E8%BD%A6%E7%89%8C/" rel="tag">换车牌</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%8E%92%E5%BA%8F/" rel="tag">排序</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%8E%A5%E9%9B%A8%E6%B0%B4/" rel="tag">接雨水</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%95%B0%E6%8D%AE%E6%BA%90%E5%8A%A8%E6%80%81%E5%88%87%E6%8D%A2/" rel="tag">数据源动态切换</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="tag">数据结构</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%96%B0%E8%AF%AD%E8%A8%80/" rel="tag">新语言</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%97%85%E6%B8%B8/" rel="tag">旅游</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%97%A5%E5%BF%97/" rel="tag">日志</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9B%BE%E5%8E%9D%E5%9E%B5/" rel="tag">曾厝垵</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9C%80%E5%B0%8F%E6%A0%88/" rel="tag">最小栈</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9D%80%E4%BA%BA%E8%AF%9B%E5%BF%83/" rel="tag">杀人诛心</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%9D%AD%E5%B7%9E/" rel="tag">杭州</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%A0%87%E8%AE%B0%E6%95%B4%E7%90%86/" rel="tag">标记整理</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%A4%8D%E7%89%A9%E5%9B%AD/" rel="tag">植物园</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B2%99%E8%8C%B6%E9%9D%A2/" rel="tag">沙茶面</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B3%A8%E8%A7%A3/" rel="tag">注解</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B5%B7%E8%9B%8E%E7%85%8E/" rel="tag">海蛎煎</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97/" rel="tag">消息队列</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B7%98%E6%B1%B0%E7%AD%96%E7%95%A5/" rel="tag">淘汰策略</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90/" rel="tag">源码解析</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%94%9F%E6%B4%BB/" rel="tag">生活</a><span class="tag-list-count">39</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%94%B5%E7%93%B6%E8%BD%A6/" rel="tag">电瓶车</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%96%AB%E6%83%85/" rel="tag">疫情</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%9C%8B%E4%B9%A6/" rel="tag">看书</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%9C%8B%E5%89%A7/" rel="tag">看剧</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%9F%A9%E9%98%B5/" rel="tag">矩阵</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%AB%AF%E5%8F%A3%E8%BD%AC%E5%8F%91/" rel="tag">端口转发</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%B1%BB%E5%8A%A0%E8%BD%BD/" rel="tag">类加载</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%B3%9F%E5%BF%83%E4%BA%8B/" rel="tag">糟心事</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%B4%A2%E5%BC%95/" rel="tag">索引</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BA%BF%E7%A8%8B%E6%B1%A0/" rel="tag">线程池</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BC%93%E5%AD%98/" rel="tag">缓存</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BC%93%E5%AD%98%E5%87%BB%E7%A9%BF/" rel="tag">缓存击穿</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BC%93%E5%AD%98%E7%A9%BF%E9%80%8F/" rel="tag">缓存穿透</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BC%93%E5%AD%98%E9%9B%AA%E5%B4%A9/" rel="tag">缓存雪崩</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BC%96%E7%A0%81/" rel="tag">编码</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%BE%8E%E5%9B%BD/" rel="tag">美国</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%80%81%E7%94%B5%E8%84%91/" rel="tag">老电脑</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%87%AA%E5%8A%A8%E8%A3%85%E9%85%8D/" rel="tag">自动装配</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%87%AA%E6%97%8B/" rel="tag">自旋</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%87%AA%E9%80%82%E5%BA%94%E6%8B%93%E5%B1%95/" rel="tag">自适应拓展</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%A3%85%E7%94%B5%E8%84%91/" rel="tag">装电脑</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%A7%84%E5%88%99/" rel="tag">规则</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%A7%A3%E6%9E%90/" rel="tag">解析</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="tag">设计模式</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%AF%BB%E4%B9%A6/" rel="tag">读书</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%AF%BB%E5%90%8E%E6%84%9F/" rel="tag">读后感</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/" rel="tag">负载均衡</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%B6%B3%E7%90%83/" rel="tag">足球</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%B7%91%E6%AD%A5/" rel="tag">跑步</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%B7%AF%E6%94%BF%E8%A7%84%E5%88%92/" rel="tag">路政规划</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%B7%B3%E6%B0%B4/" rel="tag">跳水</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%B8%A9%E8%B8%8F/" rel="tag">踩踏</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BD%AC%E4%B9%89/" rel="tag">转义</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BD%BB%E9%87%8F%E7%BA%A7%E9%94%81/" rel="tag">轻量级锁</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%87%E6%9C%9F%E7%AD%96%E7%95%A5/" rel="tag">过期策略</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%90%E5%8A%A8/" rel="tag">运动</a><span class="tag-list-count">8</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%9C%E7%A8%8B%E5%8A%9E%E5%85%AC/" rel="tag">远程办公</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%80%92%E5%BD%92/" rel="tag">递归</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%87%8D%E9%87%8F%E7%BA%A7%E9%94%81/" rel="tag">重量级锁</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%93%BE%E6%8E%A5/" rel="tag">链接</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%A2%98%E8%A7%A3/" rel="tag">题解</a><span class="tag-list-count">22</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%A9%AC%E6%88%8F%E5%9B%A2/" rel="tag">马戏团</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%AA%8C%E8%AF%81/" rel="tag">验证</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%AA%91%E8%BD%A6/" rel="tag">骑车</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%AB%98%E9%80%9F/" rel="tag">高速</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%BC%93%E6%B5%AA%E5%B1%BF/" rel="tag">鼓浪屿</a><span class="tag-list-count">1</span></li></ul></canvas></div></div><div class="links-of-blogroll site-overview-item animated"><div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i> Links</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="https://covermusic.cn/" title="https://covermusic.cn" rel="noopener" target="_blank">69伙伴</a></li></ul></div></div></div></div></aside><div class="sidebar-dimmer"></div></header><div class="back-to-top" role="button" aria-label="返回顶部"><i class="fa fa-arrow-up"></i> <span>0%</span></div><a href="https://github.com/nicksxs" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><noscript><div class="noscript-warning">Theme NexT works best with JavaScript enabled</div></noscript><div class="main-inner index posts-expand"><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content"><link itemprop="mainEntityOfPage" href="https://nicksxs.me/2020/05/10/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC-%E7%BB%AD%E7%BB%AD%E7%AF%87%E4%B9%8B%E5%8A%A0%E9%94%81%E5%88%86%E6%9E%90/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/uploads/avatar.jpg"><meta itemprop="name" content="Nicksxs"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Nicksxs's Blog"><meta itemprop="description" content="learn from zero,技术博客,Nicksxs,史学森"></span><span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork"><meta itemprop="name" content="undefined | Nicksxs's Blog"><meta itemprop="description" content=""></span><header class="post-header"><h2 class="post-title" itemprop="name headline"><a href="/2020/05/10/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC-%E7%BB%AD%E7%BB%AD%E7%AF%87%E4%B9%8B%E5%8A%A0%E9%94%81%E5%88%86%E6%9E%90/" class="post-title-link" itemprop="url">聊聊 mysql 的 MVCC 续续篇之锁分析</a></h2><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间:2020-05-10 09:55:10 / 修改时间:22:26:13" itemprop="dateCreated datePublished" datetime="2020-05-10T09:55:10+08:00">2020-05-10</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-folder"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/" itemprop="url" rel="index"><span itemprop="name">Mysql</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/%E6%BA%90%E7%A0%81/" itemprop="url" rel="index"><span itemprop="name">源码</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/Mysql/" itemprop="url" rel="index"><span itemprop="name">Mysql</span></a> </span></span><span id="/2020/05/10/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC-%E7%BB%AD%E7%BB%AD%E7%AF%87%E4%B9%8B%E5%8A%A0%E9%94%81%E5%88%86%E6%9E%90/" class="post-meta-item leancloud_visitors" data-flag-title="聊聊 mysql 的 MVCC 续续篇之锁分析" title="阅读次数"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数:</span> <span class="leancloud-visitors-count"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-comment"></i> </span><span class="post-meta-item-text">Disqus:</span> <a title="disqus" href="/2020/05/10/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC-%E7%BB%AD%E7%BB%AD%E7%AF%87%E4%B9%8B%E5%8A%A0%E9%94%81%E5%88%86%E6%9E%90/#disqus_thread" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="2020/05/10/聊聊-mysql-的-MVCC-续续篇之加锁分析/" itemprop="commentCount"></span></a></span></div></div></header><div class="post-body" itemprop="articleBody"><p>看完前面两篇水文之后,感觉不得不来分析下 mysql 的锁了,其实前面说到幻读的时候是有个前提没提到的,比如一个<code>select * from table1 where id = 1</code>这种查询语句其实是不会加传说中的锁的,当然这里是指在 RR 或者 RC 隔离级别下,<br>看一段 mysql官方文档</p><blockquote><p><code>SELECT ... FROM</code> is a consistent read, reading a snapshot of the database and setting no locks unless the transaction isolation level is set to SERIALIZABLE. For SERIALIZABLE level, the search sets shared next-key locks on the index records it encounters. However, only an index record lock is required for statements that lock rows using a unique index to search for a unique row.</p></blockquote><p>纯粹的这种一致性读,实际读取的是快照,也就是基于 read view 的读取方式,除非当前隔离级别是SERIALIZABLE<br>但是对于以下几类</p><ul><li><code>select * from table where ? lock in share mode;</code></li><li><code>select * from table where ? for update;</code></li><li><code>insert into table values (...);</code></li><li><code>update table set ? where ?;</code></li><li><code>delete from table where ?;</code></li></ul><p>除了第一条是 S 锁之外,其他都是 X 排他锁,这边在顺带下,S 锁表示共享锁, X 表示独占锁,同为 S 锁之间不冲突,S 与 X,X 与 S,X 与 X 之间都冲突,也就是加了前者,后者就加不上了<br>我们知道对于 RC 级别会出现幻读现象,对于 RR 级别不会出现,主要的区别是 RR 级别下对于以上的加锁读取都根据情况加上了 gap 锁,那么是不是 RR 级别下以上所有的都是要加 gap 锁呢,当然不是<br>举个例子,RR 事务隔离级别下,table1 有个主键id 字段<br><code>select * from table1 where id = 10 for update</code><br>这条语句要加 gap 锁吗?<br>答案是不需要,这里其实算是我看了这么久的一点自己的理解,啥时候要加 gap 锁,判断的条件是根据我查询的数据是否会因为不加 gap 锁而出现数量的不一致,我上面这条查询语句,在什么情况下会出现查询结果数量不一致呢,只要在这条记录被更新或者删除的时候,有没有可能我第一次查出来一条,第二次变成两条了呢,不可能,因为是主键索引。<br>再变更下这个题的条件,当 id 不是主键,但是是唯一索引,这样需要怎么加锁,注意问题是怎么加锁,不是需不需要加 gap 锁,这里呢就是稍微延伸一下,把聚簇索引(主键索引)和二级索引带一下,当 id 不是主键,说明是个二级索引,但是它是唯一索引,体会下,首先对于 id = 10这个二级索引肯定要加锁,要不要锁 gap 呢,不用,因为是唯一索引,id = 10 只可能有这一条记录,然后呢,这样是不是就好了,还不行,因为啥,因为它是二级索引,对应的主键索引的记录才是真正的数据,万一被更新掉了咋办,所以在 id = 10 对应的主键索引上也需要加上锁(默认都是 record lock行锁),那主键索引上要不要加 gap 呢,也不用,也是精确定位到这一条记录<br>最后呢,当 id 不是主键,也不是唯一索引,只是个普通的索引,这里就需要大名鼎鼎的 gap 锁了,<br>是时候画个图了<br><img data-src="https://mystore-1255223546.cos.ap-chengdu.myqcloud.com/uPic/WX20200510-1126082x.png"><br>其实核心的目的还是不让这个 id=10 的记录不会出现幻读,那么就需要在 id 这个索引上加上三个 gap 锁,主键索引上就不用了,在 id 索引上已经控制住了id = 10 不会出现幻读,主键索引上这两条对应的记录已经锁了,所以就这样 OK 了</p></div><footer class="post-footer"><div class="post-eof"></div></footer></article></div><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content"><link itemprop="mainEntityOfPage" href="https://nicksxs.me/2020/05/02/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC-%E7%BB%AD%E7%AF%87/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/uploads/avatar.jpg"><meta itemprop="name" content="Nicksxs"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Nicksxs's Blog"><meta itemprop="description" content="learn from zero,技术博客,Nicksxs,史学森"></span><span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork"><meta itemprop="name" content="undefined | Nicksxs's Blog"><meta itemprop="description" content=""></span><header class="post-header"><h2 class="post-title" itemprop="name headline"><a href="/2020/05/02/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC-%E7%BB%AD%E7%AF%87/" class="post-title-link" itemprop="url">聊聊 mysql 的 MVCC 续篇</a></h2><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间:2020-05-02 14:46:35" itemprop="dateCreated datePublished" datetime="2020-05-02T14:46:35+08:00">2020-05-02</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar-check"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间:2020-05-03 22:26:29" itemprop="dateModified" datetime="2020-05-03T22:26:29+08:00">2020-05-03</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-folder"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/" itemprop="url" rel="index"><span itemprop="name">Mysql</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/%E6%BA%90%E7%A0%81/" itemprop="url" rel="index"><span itemprop="name">源码</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/Mysql/" itemprop="url" rel="index"><span itemprop="name">Mysql</span></a> </span></span><span id="/2020/05/02/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC-%E7%BB%AD%E7%AF%87/" class="post-meta-item leancloud_visitors" data-flag-title="聊聊 mysql 的 MVCC 续篇" title="阅读次数"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数:</span> <span class="leancloud-visitors-count"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-comment"></i> </span><span class="post-meta-item-text">Disqus:</span> <a title="disqus" href="/2020/05/02/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC-%E7%BB%AD%E7%AF%87/#disqus_thread" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="2020/05/02/聊聊-mysql-的-MVCC-续篇/" itemprop="commentCount"></span></a></span></div></div></header><div class="post-body" itemprop="articleBody"><p>上一篇聊了mysql 的 innodb 引擎基于 read view 实现的 mvcc 和事务隔离级别,可能有些细心的小伙伴会发现一些问题,第一个是在 RC 级别下的事务提交后的可见性,这里涉及到了三个参数,m_low_limit_id,m_up_limit_id,m_ids,之前看到知乎的一篇写的非常不错的文章,但是就在这一点上似乎有点疑惑,这里基于源码和注释来解释下这个问题</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/**
|
|
|
Opens a read view where exactly the transactions serialized before this
|
|
|
point in time are seen in the view.
|
|
|
@param id Creator transaction id */
|
|
|
|
|
|
void ReadView::prepare(trx_id_t id) {
|
|
|
ut_ad(mutex_own(&trx_sys->mutex));
|
|
|
|
|
|
m_creator_trx_id = id;
|
|
|
|
|
|
m_low_limit_no = m_low_limit_id = m_up_limit_id = trx_sys->max_trx_id;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>m_low_limit_id赋的值是trx_sys->max_trx_id,代表的是当前系统最小的未分配的事务 id,所以呢,举个例子,当前有三个活跃事务,事务 id 分别是 100,200,300,而 m_up_limit_id = 100, m_low_limit_id = 301,当事务 id 是 200 的提交之后,它的更新就是可以被 100 和 300 看到,而不是说 m_ids 里没了 200,并且 200 比 100 大就应该不可见了</p><h2 id="幻读"><a href="#幻读" class="headerlink" title="幻读"></a>幻读</h2><p>还有一个问题是幻读的问题,这貌似也是个高频面试题,啥意思呢,或者说跟它最常拿来比较的脏读,脏读是指读到了别的事务未提交的数据,因为未提交,严格意义上来讲,不一定是会被最后落到库里,可能会回滚,也就是在 read uncommitted 级别下会出现的问题,但是幻读不太一样,幻读是指两次查询的结果数量不一样,比如我查了第一次是 <code>select * from table1 where id < 10 for update</code>,查出来了一条结果 id 是 5,然后再查一下发现出来了一条 id 是 5,一条 id 是 7,那是不是有点尴尬了,其实呢这个点我觉得脏读跟幻读也比较是从原理层面来命名,如果第一次接触的同学发觉有点不理解也比较正常,因为从逻辑上讲总之都是数据不符合预期,但是基于源码层面其实是不同的情况,幻读是在原先的 read view 无法完全解决的,怎么解决呢,简单的来说就是锁咯,我们知道innodb 是基于 record lock 行锁的,但是貌似没有办法解决这种问题,那么 innodb 就引入了 gap lock 间隙锁,比如上面说的情况下,id 小于 10 的情况下,是都应该锁住的,gap lock 其实是基于索引结构来锁的,因为索引树除了树形结构之外,还有一个next record 的指针,gap lock 也是基于这个来锁的<br>看一下 mysql 的文档</p><blockquote><p>SELECT … FOR UPDATE sets an exclusive next-key lock on every record the search encounters. However, only an index record lock is required for statements that lock rows using a unique index to search for a unique row.</p></blockquote><p>对于一个 for update 查询,在 RR 级别下,会设置一个 next-key lock在每一条被查询到的记录上,next-lock 又是啥呢,其实就是 gap 锁和 record 锁的结合体,比如我在数据库里有 id 是 1,3,5,7,10,对于上面那条查询,查出来的结果就是 1,3,5,7,那么按照文档里描述的,对于这几条记录都会加上next-key lock,也就是(-∞, 1], (1, 3], (3, 5], (5, 7], (7, 10) 这些区间和记录会被锁起来,不让插入,再唠叨一下呢,就是其实如果是只读的事务,光 read view 一致性读就够了,如果是有写操作的呢,就需要锁了。</p></div><footer class="post-footer"><div class="post-eof"></div></footer></article></div><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content"><link itemprop="mainEntityOfPage" href="https://nicksxs.me/2020/04/26/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/uploads/avatar.jpg"><meta itemprop="name" content="Nicksxs"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Nicksxs's Blog"><meta itemprop="description" content="learn from zero,技术博客,Nicksxs,史学森"></span><span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork"><meta itemprop="name" content="undefined | Nicksxs's Blog"><meta itemprop="description" content=""></span><header class="post-header"><h2 class="post-title" itemprop="name headline"><a href="/2020/04/26/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC/" class="post-title-link" itemprop="url">聊聊 mysql 的 MVCC</a></h2><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间:2020-04-26 22:14:01" itemprop="dateCreated datePublished" datetime="2020-04-26T22:14:01+08:00">2020-04-26</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar-check"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间:2020-05-02 19:46:21" itemprop="dateModified" datetime="2020-05-02T19:46:21+08:00">2020-05-02</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-folder"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/" itemprop="url" rel="index"><span itemprop="name">Mysql</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mysql/%E6%BA%90%E7%A0%81/" itemprop="url" rel="index"><span itemprop="name">源码</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/Mysql/" itemprop="url" rel="index"><span itemprop="name">Mysql</span></a> </span></span><span id="/2020/04/26/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC/" class="post-meta-item leancloud_visitors" data-flag-title="聊聊 mysql 的 MVCC" title="阅读次数"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数:</span> <span class="leancloud-visitors-count"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-comment"></i> </span><span class="post-meta-item-text">Disqus:</span> <a title="disqus" href="/2020/04/26/%E8%81%8A%E8%81%8A-mysql-%E7%9A%84-MVCC/#disqus_thread" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="2020/04/26/聊聊-mysql-的-MVCC/" itemprop="commentCount"></span></a></span></div></div></header><div class="post-body" itemprop="articleBody"><p>很久以前,有位面试官问到,你知道 mysql 的事务隔离级别吗,“额 O__O …,不太清楚”,完了之后我就去网上找相关的文章,找到了这篇<a target="_blank" rel="noopener" href="https://www.cnblogs.com/zhoujinyi/p/3437475.html">MySQL 四种事务隔离级的说明</a>, 文章写得特别好,看了这个就懂了各个事务隔离级别都是啥,不过看了这个之后多思考一下的话还是会发现问题,这么神奇的事务隔离级别是怎么实现的呢</p><p>其中 innodb 的事务隔离用到了标题里说到的 mvcc,<a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Multiversion_concurrency_control"><strong>Multiversion concurrency control</strong></a>, 直译过来就是多版本并发控制,先不讲这个究竟是个啥,考虑下如果纯猜测,这个事务隔离级别应该会是怎么样实现呢,愚钝的我想了下,可以在事务开始的时候拷贝一个表,这个可以支持 RR 级别,RC 级别就不支持了,而且要是个非常大的表,想想就不可行</p><p>腆着脸说虽然这个不可行,但是思路是对的,具体实行起来需要做一系列(肥肠多)的改动,首先根据我的理解,其实这个拷贝一个表是变成拷贝一条记录,但是如果有多个事务,那就得拷贝多次,这个问题其实可以借助版本管理系统来解释,在用版本管理系统,git 之类的之前,很原始的可能是开发完一个功能后,就打个压缩包用时间等信息命名,然后如果后面要找回这个就直接用这个压缩包的就行了,后来有了 svn,git 中心式和分布式的版本管理系统,它的一个特点是粒度可以控制到文件和代码行级别,对应的我们的 mysql 事务是不是也可以从一开始预想的表级别细化到行的级别,可能之前很多人都了解过,数据库的一行记录除了我们用户自定义的字段,还有一些额外的字段,去源码<a target="_blank" rel="noopener" href="https://github.com/mysql/mysql-server/blob/8.0/storage/innobase/include/data0type.h#L170">data0type.h</a>里捞一下</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">/* Precise data types for system columns and the length of those columns;
|
|
|
NOTE: the values must run from 0 up in the order given! All codes must
|
|
|
be less than 256 */</span>
|
|
|
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">DATA_ROW_ID</span> <span class="token expression"><span class="token number">0</span> </span><span class="token comment">/* row id: a 48-bit integer */</span></span>
|
|
|
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">DATA_ROW_ID_LEN</span> <span class="token expression"><span class="token number">6</span> </span><span class="token comment">/* stored length for row id */</span></span>
|
|
|
|
|
|
<span class="token comment">/** Transaction id: 6 bytes */</span>
|
|
|
constexpr <span class="token class-name">size_t</span> DATA_TRX_ID <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
|
|
|
|
|
|
<span class="token comment">/** Transaction ID type size in bytes. */</span>
|
|
|
constexpr <span class="token class-name">size_t</span> DATA_TRX_ID_LEN <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span>
|
|
|
|
|
|
<span class="token comment">/** Rollback data pointer: 7 bytes */</span>
|
|
|
constexpr <span class="token class-name">size_t</span> DATA_ROLL_PTR <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
|
|
|
|
|
|
<span class="token comment">/** Rollback data pointer type size in bytes. */</span>
|
|
|
constexpr <span class="token class-name">size_t</span> DATA_ROLL_PTR_LEN <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>一个是 <code>DATA_ROW_ID</code>,这个是在数据没指定主键的时候会生成一个隐藏的,如果用户有指定主键就是主键了</p><p>一个是 <code>DATA_TRX_ID</code>,这个表示这条记录的事务 ID</p><p>还有一个是 <code>DATA_ROLL_PTR</code> 指向回滚段的指针</p><p>指向的回滚段其实就是我们常说的 undo log,这里面的具体结构就是个链表,在 mvcc 里会使用到这个,还有就是这个 <code>DATA_TRX_ID</code>,每条记录都记录了这个事务 ID,表示的是这条记录的当前值是被哪个事务修改的,下面就扯回事务了,我们知道 <code>Read Uncommitted</code>, 其实用不到隔离,直接读取当前值就好了,到了 <code>Read Committed</code> 级别,我们要让事务读取到提交过的值,mysql 使用了一个叫 <code>read view</code> 的玩意,它里面有这些值是我们需要注意的,</p><p><code>m_low_limit_id</code>, 这个是 read view 创建时最大的活跃事务 id</p><p><code>m_up_limit_id</code>, 这个是 read view 创建时最小的活跃事务 id</p><p><code>m_ids</code>, 这个是 read view 创建时所有的活跃事务 id 数组</p><p><code>m_creator_trx_id 这个是当前记录的创建事务 id</code></p><p>判断事务的可见性主要的逻辑是这样,</p><ol><li>当记录的事务 <code>id</code> 小于最小活跃事务 id,说明是可见的,</li><li>如果记录的事务 <code>id</code> 等于当前事务 id,说明是自己的更改,可见</li><li>如果记录的事务 <code>id</code> 大于最大的活跃事务 <code>id</code>, 不可见</li><li>如果记录的事务 <code>id</code> 介于 <code>m_low_limit_id</code> 和 <code>m_up_limit_id</code> 之间,则要判断它是否在 <code>m_ids</code> 中,如果在,不可见,如果不在,表示已提交,可见<br>具体的<a target="_blank" rel="noopener" href="https://github.com/mysql/mysql-server/blob/8.0/storage/innobase/include/read0types.h#L160">代码</a>捞一下看看<pre class="line-numbers language-C" data-language="C"><code class="language-C">/** Check whether the changes by id are visible.
|
|
|
@param[in] id transaction id to check against the view
|
|
|
@param[in] name table name
|
|
|
@return whether the view sees the modifications of id. */
|
|
|
bool changes_visible(trx_id_t id, const table_name_t &name) const
|
|
|
MY_ATTRIBUTE((warn_unused_result)) {
|
|
|
ut_ad(id > 0);
|
|
|
|
|
|
if (id < m_up_limit_id || id == m_creator_trx_id) {
|
|
|
return (true);
|
|
|
}
|
|
|
|
|
|
check_trx_id_sanity(id, name);
|
|
|
|
|
|
if (id >= m_low_limit_id) {
|
|
|
return (false);
|
|
|
|
|
|
} else if (m_ids.empty()) {
|
|
|
return (true);
|
|
|
}
|
|
|
|
|
|
const ids_t::value_type *p = m_ids.data();
|
|
|
|
|
|
return (!std::binary_search(p, p + m_ids.size(), id));
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>剩下来一点是啥呢,就是 <code>Read Committed</code> 和 <code>Repeated Read</code> 也不一样,那前面说的 <code>read view</code> 都能支持吗,又是怎么支持呢,假如这个 <code>read view</code> 是在事务一开始就创建,那好像能支持的只是 RR 事务隔离级别,其实呢,这是通过创建 <code>read view</code>的时机,对于 RR 级别,就是在事务的第一个 <code>select</code> 语句是创建,对于 RC 级别,是在每个 <code>select</code> 语句执行前都是创建一次,那样就可以保证能读到所有已提交的数据</li></ol></div><footer class="post-footer"><div class="post-eof"></div></footer></article></div><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content"><link itemprop="mainEntityOfPage" href="https://nicksxs.me/2020/04/18/redis%E7%B3%BB%E5%88%97%E4%BB%8B%E7%BB%8D%E5%85%AB/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/uploads/avatar.jpg"><meta itemprop="name" content="Nicksxs"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Nicksxs's Blog"><meta itemprop="description" content="learn from zero,技术博客,Nicksxs,史学森"></span><span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork"><meta itemprop="name" content="undefined | Nicksxs's Blog"><meta itemprop="description" content=""></span><header class="post-header"><h2 class="post-title" itemprop="name headline"><a href="/2020/04/18/redis%E7%B3%BB%E5%88%97%E4%BB%8B%E7%BB%8D%E5%85%AB/" class="post-title-link" itemprop="url">redis系列介绍八-淘汰策略</a></h2><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间:2020-04-18 23:23:41" itemprop="dateCreated datePublished" datetime="2020-04-18T23:23:41+08:00">2020-04-18</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar-check"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间:2022-06-22 23:20:15" itemprop="dateModified" datetime="2022-06-22T23:20:15+08:00">2022-06-22</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-folder"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Redis/" itemprop="url" rel="index"><span itemprop="name">Redis</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Redis/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Redis/%E6%BA%90%E7%A0%81/" itemprop="url" rel="index"><span itemprop="name">源码</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/Redis/" itemprop="url" rel="index"><span itemprop="name">Redis</span></a> </span></span><span id="/2020/04/18/redis%E7%B3%BB%E5%88%97%E4%BB%8B%E7%BB%8D%E5%85%AB/" class="post-meta-item leancloud_visitors" data-flag-title="redis系列介绍八-淘汰策略" title="阅读次数"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数:</span> <span class="leancloud-visitors-count"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-comment"></i> </span><span class="post-meta-item-text">Disqus:</span> <a title="disqus" href="/2020/04/18/redis%E7%B3%BB%E5%88%97%E4%BB%8B%E7%BB%8D%E5%85%AB/#disqus_thread" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="2020/04/18/redis系列介绍八/" itemprop="commentCount"></span></a></span></div></div></header><div class="post-body" itemprop="articleBody"><h3 id="LRU"><a href="#LRU" class="headerlink" title="LRU"></a>LRU</h3><p>说完了过期策略再说下淘汰策略,redis 使用的策略是近似的 lru 策略,为什么是近似的呢,先来看下什么是 lru,看下 wiki 的介绍<br><img data-src="https://img.nicksxs.com/uPic/z5cDpo.jpg">,图中一共有四个槽的存储空间,依次访问顺序是 A B C D E D F,<br>当第一次访问 D 时刚好占满了坑,并且值是 4,这个值越小代表越先被淘汰,当 E 进来时,看了下已经存在的四个里 A 是最小的,代表是最早存在并且最早被访问的,那就先淘汰它了,E 占领了 A 的位置,并设置值为 4,然后又访问 D 了,D 已经存在了,不过又被访问到了,得更新值为 5,然后是 F 进来了,这时 B 是最老的且最近未被访问,所以就淘汰它了。以上是一个 lru 的简要说明,但是 redis 没有严格按照这个去执行,理由跟前面过期策略一致,最严格的过期策略应该是每个 key 都有对应的定时器,当超时时马上就能清除,但是问题是这样的cpu 消耗太大,所换来的内存效率不太值得,淘汰策略也是这样,类似于上图,要维护所有 key 的一个有序 lru 值,并且遍历将最小的淘汰,redis 采用的是抽样的形式,最初的实现方式是随机从 dict 抽取 5 个 key,淘汰一个 lru 最小的,这样子勉强能达到淘汰的目的,但是效果不是特别好,后面在 redis 3.0开始,将随机抽取改成了维护一个 pool,pool 的大小默认是 16,每次放入的都是按lru 值有序排列好,每一次放入的必须是 lru小于 pool 中最小的 lru 才允许放入,直到放满,后面再有新的就会将大的踢出。<br>redis 针对这个策略的改进做了一个实验,这里借用下图<br><img data-src="https://img.nicksxs.com/uPic/lEx4Ug.jpg"><br>首先背景是这图中的所有点都对应一个 redis 的 key,灰色部分加入后被顺序访问过一遍,然后又加入了绿色部分,那么按照理论的 lru 算法,应该是图左上中,浅灰色部分全都被淘汰,那么对比来看看图右上,左下和右下,左下表示 2.8 版本就是随机抽样 5 个 key,淘汰其中 lru 最小的一个,发现是灰色和浅灰色的都有被淘汰的,右下的 3.0 版本抽样数量不变的情况下,稍好一些,当 3.0 版本的抽样数量调整成 10 后,已经较为接近理论上的 lru 策略了,通过代码来简要分析下</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">typedef struct redisObject {
|
|
|
unsigned type:4;
|
|
|
unsigned encoding:4;
|
|
|
unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
|
|
|
* LFU data (least significant 8 bits frequency
|
|
|
* and most significant 16 bits access time). */
|
|
|
int refcount;
|
|
|
void *ptr;
|
|
|
} robj;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>对于 lru 策略来说,lru 字段记录的就是<a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/server.h#L603"><code>redisObj</code></a> 的LRU time,<br>redis 在访问数据时,都会调用<a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/db.c#L55"><code>lookupKey</code></a>方法</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* Low level key lookup API, not actually called directly from commands
|
|
|
* implementations that should instead rely on lookupKeyRead(),
|
|
|
* lookupKeyWrite() and lookupKeyReadWithFlags(). */
|
|
|
robj *lookupKey(redisDb *db, robj *key, int flags) {
|
|
|
dictEntry *de = dictFind(db->dict,key->ptr);
|
|
|
if (de) {
|
|
|
robj *val = dictGetVal(de);
|
|
|
|
|
|
/* Update the access time for the ageing algorithm.
|
|
|
* Don't do it if we have a saving child, as this will trigger
|
|
|
* a copy on write madness. */
|
|
|
if (!hasActiveChildProcess() && !(flags & LOOKUP_NOTOUCH)){
|
|
|
if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
|
|
|
// 这个是后面一节的内容
|
|
|
updateLFU(val);
|
|
|
} else {
|
|
|
// 对于这个分支,访问时就会去更新 lru 值
|
|
|
val->lru = LRU_CLOCK();
|
|
|
}
|
|
|
}
|
|
|
return val;
|
|
|
} else {
|
|
|
return NULL;
|
|
|
}
|
|
|
}
|
|
|
/* This function is used to obtain the current LRU clock.
|
|
|
* If the current resolution is lower than the frequency we refresh the
|
|
|
* LRU clock (as it should be in production servers) we return the
|
|
|
* precomputed value, otherwise we need to resort to a system call. */
|
|
|
unsigned int LRU_CLOCK(void) {
|
|
|
unsigned int lruclock;
|
|
|
if (1000/server.hz <= LRU_CLOCK_RESOLUTION) {
|
|
|
// 如果服务器的频率server.hz大于 1 时就是用系统预设的 lruclock
|
|
|
lruclock = server.lruclock;
|
|
|
} else {
|
|
|
lruclock = getLRUClock();
|
|
|
}
|
|
|
return lruclock;
|
|
|
}
|
|
|
/* Return the LRU clock, based on the clock resolution. This is a time
|
|
|
* in a reduced-bits format that can be used to set and check the
|
|
|
* object->lru field of redisObject structures. */
|
|
|
unsigned int getLRUClock(void) {
|
|
|
return (mstime()/LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX;
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>redis 处理命令是在这里<a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/server.c#L3355"><code>processCommand</code></a></p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* If this function gets called we already read a whole
|
|
|
* command, arguments are in the client argv/argc fields.
|
|
|
* processCommand() execute the command or prepare the
|
|
|
* server for a bulk read from the client.
|
|
|
*
|
|
|
* If C_OK is returned the client is still alive and valid and
|
|
|
* other operations can be performed by the caller. Otherwise
|
|
|
* if C_ERR is returned the client was destroyed (i.e. after QUIT). */
|
|
|
int processCommand(client *c) {
|
|
|
moduleCallCommandFilters(c);
|
|
|
|
|
|
|
|
|
|
|
|
/* Handle the maxmemory directive.
|
|
|
*
|
|
|
* Note that we do not want to reclaim memory if we are here re-entering
|
|
|
* the event loop since there is a busy Lua script running in timeout
|
|
|
* condition, to avoid mixing the propagation of scripts with the
|
|
|
* propagation of DELs due to eviction. */
|
|
|
if (server.maxmemory && !server.lua_timedout) {
|
|
|
int out_of_memory = freeMemoryIfNeededAndSafe() == C_ERR;
|
|
|
/* freeMemoryIfNeeded may flush slave output buffers. This may result
|
|
|
* into a slave, that may be the active client, to be freed. */
|
|
|
if (server.current_client == NULL) return C_ERR;
|
|
|
|
|
|
/* It was impossible to free enough memory, and the command the client
|
|
|
* is trying to execute is denied during OOM conditions or the client
|
|
|
* is in MULTI/EXEC context? Error. */
|
|
|
if (out_of_memory &&
|
|
|
(c->cmd->flags & CMD_DENYOOM ||
|
|
|
(c->flags & CLIENT_MULTI &&
|
|
|
c->cmd->proc != execCommand &&
|
|
|
c->cmd->proc != discardCommand)))
|
|
|
{
|
|
|
flagTransaction(c);
|
|
|
addReply(c, shared.oomerr);
|
|
|
return C_OK;
|
|
|
}
|
|
|
}
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>这里只摘了部分,当需要清理内存时就会调用, 然后调用了<a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/evict.c#L631"><code>freeMemoryIfNeededAndSafe</code></a></p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* This is a wrapper for freeMemoryIfNeeded() that only really calls the
|
|
|
* function if right now there are the conditions to do so safely:
|
|
|
*
|
|
|
* - There must be no script in timeout condition.
|
|
|
* - Nor we are loading data right now.
|
|
|
*
|
|
|
*/
|
|
|
int freeMemoryIfNeededAndSafe(void) {
|
|
|
if (server.lua_timedout || server.loading) return C_OK;
|
|
|
return freeMemoryIfNeeded();
|
|
|
}
|
|
|
/* This function is periodically called to see if there is memory to free
|
|
|
* according to the current "maxmemory" settings. In case we are over the
|
|
|
* memory limit, the function will try to free some memory to return back
|
|
|
* under the limit.
|
|
|
*
|
|
|
* The function returns C_OK if we are under the memory limit or if we
|
|
|
* were over the limit, but the attempt to free memory was successful.
|
|
|
* Otehrwise if we are over the memory limit, but not enough memory
|
|
|
* was freed to return back under the limit, the function returns C_ERR. */
|
|
|
int freeMemoryIfNeeded(void) {
|
|
|
int keys_freed = 0;
|
|
|
/* By default replicas should ignore maxmemory
|
|
|
* and just be masters exact copies. */
|
|
|
if (server.masterhost && server.repl_slave_ignore_maxmemory) return C_OK;
|
|
|
|
|
|
size_t mem_reported, mem_tofree, mem_freed;
|
|
|
mstime_t latency, eviction_latency;
|
|
|
long long delta;
|
|
|
int slaves = listLength(server.slaves);
|
|
|
|
|
|
/* When clients are paused the dataset should be static not just from the
|
|
|
* POV of clients not being able to write, but also from the POV of
|
|
|
* expires and evictions of keys not being performed. */
|
|
|
if (clientsArePaused()) return C_OK;
|
|
|
if (getMaxmemoryState(&mem_reported,NULL,&mem_tofree,NULL) == C_OK)
|
|
|
return C_OK;
|
|
|
|
|
|
mem_freed = 0;
|
|
|
|
|
|
if (server.maxmemory_policy == MAXMEMORY_NO_EVICTION)
|
|
|
goto cant_free; /* We need to free memory, but policy forbids. */
|
|
|
|
|
|
latencyStartMonitor(latency);
|
|
|
while (mem_freed < mem_tofree) {
|
|
|
int j, k, i;
|
|
|
static unsigned int next_db = 0;
|
|
|
sds bestkey = NULL;
|
|
|
int bestdbid;
|
|
|
redisDb *db;
|
|
|
dict *dict;
|
|
|
dictEntry *de;
|
|
|
|
|
|
if (server.maxmemory_policy & (MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_LFU) ||
|
|
|
server.maxmemory_policy == MAXMEMORY_VOLATILE_TTL)
|
|
|
{
|
|
|
struct evictionPoolEntry *pool = EvictionPoolLRU;
|
|
|
|
|
|
while(bestkey == NULL) {
|
|
|
unsigned long total_keys = 0, keys;
|
|
|
|
|
|
/* We don't want to make local-db choices when expiring keys,
|
|
|
* so to start populate the eviction pool sampling keys from
|
|
|
* every DB. */
|
|
|
for (i = 0; i < server.dbnum; i++) {
|
|
|
db = server.db+i;
|
|
|
dict = (server.maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS) ?
|
|
|
db->dict : db->expires;
|
|
|
if ((keys = dictSize(dict)) != 0) {
|
|
|
evictionPoolPopulate(i, dict, db->dict, pool);
|
|
|
total_keys += keys;
|
|
|
}
|
|
|
}
|
|
|
if (!total_keys) break; /* No keys to evict. */
|
|
|
|
|
|
/* Go backward from best to worst element to evict. */
|
|
|
for (k = EVPOOL_SIZE-1; k >= 0; k--) {
|
|
|
if (pool[k].key == NULL) continue;
|
|
|
bestdbid = pool[k].dbid;
|
|
|
|
|
|
if (server.maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS) {
|
|
|
de = dictFind(server.db[pool[k].dbid].dict,
|
|
|
pool[k].key);
|
|
|
} else {
|
|
|
de = dictFind(server.db[pool[k].dbid].expires,
|
|
|
pool[k].key);
|
|
|
}
|
|
|
|
|
|
/* Remove the entry from the pool. */
|
|
|
if (pool[k].key != pool[k].cached)
|
|
|
sdsfree(pool[k].key);
|
|
|
pool[k].key = NULL;
|
|
|
pool[k].idle = 0;
|
|
|
|
|
|
/* If the key exists, is our pick. Otherwise it is
|
|
|
* a ghost and we need to try the next element. */
|
|
|
if (de) {
|
|
|
bestkey = dictGetKey(de);
|
|
|
break;
|
|
|
} else {
|
|
|
/* Ghost... Iterate again. */
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* volatile-random and allkeys-random policy */
|
|
|
else if (server.maxmemory_policy == MAXMEMORY_ALLKEYS_RANDOM ||
|
|
|
server.maxmemory_policy == MAXMEMORY_VOLATILE_RANDOM)
|
|
|
{
|
|
|
/* When evicting a random key, we try to evict a key for
|
|
|
* each DB, so we use the static 'next_db' variable to
|
|
|
* incrementally visit all DBs. */
|
|
|
for (i = 0; i < server.dbnum; i++) {
|
|
|
j = (++next_db) % server.dbnum;
|
|
|
db = server.db+j;
|
|
|
dict = (server.maxmemory_policy == MAXMEMORY_ALLKEYS_RANDOM) ?
|
|
|
db->dict : db->expires;
|
|
|
if (dictSize(dict) != 0) {
|
|
|
de = dictGetRandomKey(dict);
|
|
|
bestkey = dictGetKey(de);
|
|
|
bestdbid = j;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Finally remove the selected key. */
|
|
|
if (bestkey) {
|
|
|
db = server.db+bestdbid;
|
|
|
robj *keyobj = createStringObject(bestkey,sdslen(bestkey));
|
|
|
propagateExpire(db,keyobj,server.lazyfree_lazy_eviction);
|
|
|
/* We compute the amount of memory freed by db*Delete() alone.
|
|
|
* It is possible that actually the memory needed to propagate
|
|
|
* the DEL in AOF and replication link is greater than the one
|
|
|
* we are freeing removing the key, but we can't account for
|
|
|
* that otherwise we would never exit the loop.
|
|
|
*
|
|
|
* AOF and Output buffer memory will be freed eventually so
|
|
|
* we only care about memory used by the key space. */
|
|
|
delta = (long long) zmalloc_used_memory();
|
|
|
latencyStartMonitor(eviction_latency);
|
|
|
if (server.lazyfree_lazy_eviction)
|
|
|
dbAsyncDelete(db,keyobj);
|
|
|
else
|
|
|
dbSyncDelete(db,keyobj);
|
|
|
latencyEndMonitor(eviction_latency);
|
|
|
latencyAddSampleIfNeeded("eviction-del",eviction_latency);
|
|
|
latencyRemoveNestedEvent(latency,eviction_latency);
|
|
|
delta -= (long long) zmalloc_used_memory();
|
|
|
mem_freed += delta;
|
|
|
server.stat_evictedkeys++;
|
|
|
notifyKeyspaceEvent(NOTIFY_EVICTED, "evicted",
|
|
|
keyobj, db->id);
|
|
|
decrRefCount(keyobj);
|
|
|
keys_freed++;
|
|
|
|
|
|
/* When the memory to free starts to be big enough, we may
|
|
|
* start spending so much time here that is impossible to
|
|
|
* deliver data to the slaves fast enough, so we force the
|
|
|
* transmission here inside the loop. */
|
|
|
if (slaves) flushSlavesOutputBuffers();
|
|
|
|
|
|
/* Normally our stop condition is the ability to release
|
|
|
* a fixed, pre-computed amount of memory. However when we
|
|
|
* are deleting objects in another thread, it's better to
|
|
|
* check, from time to time, if we already reached our target
|
|
|
* memory, since the "mem_freed" amount is computed only
|
|
|
* across the dbAsyncDelete() call, while the thread can
|
|
|
* release the memory all the time. */
|
|
|
if (server.lazyfree_lazy_eviction && !(keys_freed % 16)) {
|
|
|
if (getMaxmemoryState(NULL,NULL,NULL,NULL) == C_OK) {
|
|
|
/* Let's satisfy our stop condition. */
|
|
|
mem_freed = mem_tofree;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
latencyEndMonitor(latency);
|
|
|
latencyAddSampleIfNeeded("eviction-cycle",latency);
|
|
|
goto cant_free; /* nothing to free... */
|
|
|
}
|
|
|
}
|
|
|
latencyEndMonitor(latency);
|
|
|
latencyAddSampleIfNeeded("eviction-cycle",latency);
|
|
|
return C_OK;
|
|
|
|
|
|
cant_free:
|
|
|
/* We are here if we are not able to reclaim memory. There is only one
|
|
|
* last thing we can try: check if the lazyfree thread has jobs in queue
|
|
|
* and wait... */
|
|
|
while(bioPendingJobsOfType(BIO_LAZY_FREE)) {
|
|
|
if (((mem_reported - zmalloc_used_memory()) + mem_freed) >= mem_tofree)
|
|
|
break;
|
|
|
usleep(1000);
|
|
|
}
|
|
|
return C_ERR;
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>这里就是根据具体策略去淘汰 key,首先是要往 pool 更新 key,更新key 的方法是<a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/evict.c#L162"><code>evictionPoolPopulate</code></a></p><pre class="line-numbers language-C" data-language="C"><code class="language-C">void evictionPoolPopulate(int dbid, dict *sampledict, dict *keydict, struct evictionPoolEntry *pool) {
|
|
|
int j, k, count;
|
|
|
dictEntry *samples[server.maxmemory_samples];
|
|
|
|
|
|
count = dictGetSomeKeys(sampledict,samples,server.maxmemory_samples);
|
|
|
for (j = 0; j < count; j++) {
|
|
|
unsigned long long idle;
|
|
|
sds key;
|
|
|
robj *o;
|
|
|
dictEntry *de;
|
|
|
|
|
|
de = samples[j];
|
|
|
key = dictGetKey(de);
|
|
|
|
|
|
/* If the dictionary we are sampling from is not the main
|
|
|
* dictionary (but the expires one) we need to lookup the key
|
|
|
* again in the key dictionary to obtain the value object. */
|
|
|
if (server.maxmemory_policy != MAXMEMORY_VOLATILE_TTL) {
|
|
|
if (sampledict != keydict) de = dictFind(keydict, key);
|
|
|
o = dictGetVal(de);
|
|
|
}
|
|
|
|
|
|
/* Calculate the idle time according to the policy. This is called
|
|
|
* idle just because the code initially handled LRU, but is in fact
|
|
|
* just a score where an higher score means better candidate. */
|
|
|
if (server.maxmemory_policy & MAXMEMORY_FLAG_LRU) {
|
|
|
idle = estimateObjectIdleTime(o);
|
|
|
} else if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
|
|
|
/* When we use an LRU policy, we sort the keys by idle time
|
|
|
* so that we expire keys starting from greater idle time.
|
|
|
* However when the policy is an LFU one, we have a frequency
|
|
|
* estimation, and we want to evict keys with lower frequency
|
|
|
* first. So inside the pool we put objects using the inverted
|
|
|
* frequency subtracting the actual frequency to the maximum
|
|
|
* frequency of 255. */
|
|
|
idle = 255-LFUDecrAndReturn(o);
|
|
|
} else if (server.maxmemory_policy == MAXMEMORY_VOLATILE_TTL) {
|
|
|
/* In this case the sooner the expire the better. */
|
|
|
idle = ULLONG_MAX - (long)dictGetVal(de);
|
|
|
} else {
|
|
|
serverPanic("Unknown eviction policy in evictionPoolPopulate()");
|
|
|
}
|
|
|
|
|
|
/* Insert the element inside the pool.
|
|
|
* First, find the first empty bucket or the first populated
|
|
|
* bucket that has an idle time smaller than our idle time. */
|
|
|
k = 0;
|
|
|
while (k < EVPOOL_SIZE &&
|
|
|
pool[k].key &&
|
|
|
pool[k].idle < idle) k++;
|
|
|
if (k == 0 && pool[EVPOOL_SIZE-1].key != NULL) {
|
|
|
/* Can't insert if the element is < the worst element we have
|
|
|
* and there are no empty buckets. */
|
|
|
continue;
|
|
|
} else if (k < EVPOOL_SIZE && pool[k].key == NULL) {
|
|
|
/* Inserting into empty position. No setup needed before insert. */
|
|
|
} else {
|
|
|
/* Inserting in the middle. Now k points to the first element
|
|
|
* greater than the element to insert. */
|
|
|
if (pool[EVPOOL_SIZE-1].key == NULL) {
|
|
|
/* Free space on the right? Insert at k shifting
|
|
|
* all the elements from k to end to the right. */
|
|
|
|
|
|
/* Save SDS before overwriting. */
|
|
|
sds cached = pool[EVPOOL_SIZE-1].cached;
|
|
|
memmove(pool+k+1,pool+k,
|
|
|
sizeof(pool[0])*(EVPOOL_SIZE-k-1));
|
|
|
pool[k].cached = cached;
|
|
|
} else {
|
|
|
/* No free space on right? Insert at k-1 */
|
|
|
k--;
|
|
|
/* Shift all elements on the left of k (included) to the
|
|
|
* left, so we discard the element with smaller idle time. */
|
|
|
sds cached = pool[0].cached; /* Save SDS before overwriting. */
|
|
|
if (pool[0].key != pool[0].cached) sdsfree(pool[0].key);
|
|
|
memmove(pool,pool+1,sizeof(pool[0])*k);
|
|
|
pool[k].cached = cached;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Try to reuse the cached SDS string allocated in the pool entry,
|
|
|
* because allocating and deallocating this object is costly
|
|
|
* (according to the profiler, not my fantasy. Remember:
|
|
|
* premature optimizbla bla bla bla. */
|
|
|
int klen = sdslen(key);
|
|
|
if (klen > EVPOOL_CACHED_SDS_SIZE) {
|
|
|
pool[k].key = sdsdup(key);
|
|
|
} else {
|
|
|
memcpy(pool[k].cached,key,klen+1);
|
|
|
sdssetlen(pool[k].cached,klen);
|
|
|
pool[k].key = pool[k].cached;
|
|
|
}
|
|
|
pool[k].idle = idle;
|
|
|
pool[k].dbid = dbid;
|
|
|
}
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><code>Redis</code>随机选择<code>maxmemory_samples</code>数量的key,然后计算这些<code>key</code>的空闲时间<code>idle time</code>,当满足条件时(比pool中的某些键的空闲时间还大)就可以进<code>pool</code>。<code>pool</code>更新之后,就淘汰<code>pool</code>中空闲时间最大的键。</p><p><a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/evict.c#L90"><code>estimateObjectIdleTime</code></a>用来计算Redis对象的空闲时间:</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* Given an object returns the min number of milliseconds the object was never
|
|
|
* requested, using an approximated LRU algorithm. */
|
|
|
unsigned long long estimateObjectIdleTime(robj *o) {
|
|
|
unsigned long long lruclock = LRU_CLOCK();
|
|
|
if (lruclock >= o->lru) {
|
|
|
return (lruclock - o->lru) * LRU_CLOCK_RESOLUTION;
|
|
|
} else {
|
|
|
return (lruclock + (LRU_CLOCK_MAX - o->lru)) *
|
|
|
LRU_CLOCK_RESOLUTION;
|
|
|
}
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>空闲时间第一种是 lurclock 大于对象的 lru,那么就是减一下乘以精度,因为 lruclock 有可能是已经预生成的,所以会可能走下面这个</p><h3 id="LFU"><a href="#LFU" class="headerlink" title="LFU"></a>LFU</h3><p>上面介绍了LRU 的算法,但是考虑一种场景</p><pre class="line-numbers language-none"><code class="language-none">~~~~~A~~~~~A~~~~~A~~~~A~~~~~A~~~~~A~~|
|
|
|
~~B~~B~~B~~B~~B~~B~~B~~B~~B~~B~~B~~B~|
|
|
|
~~~~~~~~~~C~~~~~~~~~C~~~~~~~~~C~~~~~~|
|
|
|
~~~~~D~~~~~~~~~~D~~~~~~~~~D~~~~~~~~~D|<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>可以发现,当采用 lru 的淘汰策略的时候,D 是最新的,会被认为是最值得保留的,但是事实上还不如 A 跟 B,然后 antirez 大神就想到了LFU (Least Frequently Used) 这个算法, 显然对于上面的四个 key 的访问频率,保留优先级应该是 B > A > C = D<br>那要怎么来实现这个 LFU 算法呢,其实像LRU,理想的情况就是维护个链表,把最新访问的放到头上去,但是这个会影响访问速度,注意到前面代码的应该可以看到,redisObject 的 lru 字段其实是两用的,当策略是 LFU 时,这个字段就另作他用了,它的 24 位长度被分成两部分</p><pre class="line-numbers language-none"><code class="language-none"> 16 bits 8 bits
|
|
|
+----------------+--------+
|
|
|
+ Last decr time | LOG_C |
|
|
|
+----------------+--------+<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>前16位字段是最后一次递减时间,因此Redis知道 上一次计数器递减,后8位是 计数器 counter。<br>LFU 的主体策略就是当这个 key 被访问的次数越多频率越高他就越容易被保留下来,并且是最近被访问的频率越高。这其实有两个事情要做,一个是在访问的时候增加计数值,在一定长时间不访问时进行衰减,所以这里用了两个值,前 16 位记录上一次衰减的时间,后 8 位记录具体的计数值。<br>Redis4.0之后为maxmemory_policy淘汰策略添加了两个LFU模式:</p><p><code>volatile-lfu</code>:对有过期时间的key采用LFU淘汰策略<br><code>allkeys-lfu</code>:对全部key采用LFU淘汰策略<br>还有2个配置可以调整LFU算法:</p><pre class="line-numbers language-none"><code class="language-none">lfu-log-factor 10
|
|
|
lfu-decay-time 1
|
|
|
```
|
|
|
`lfu-log-factor` 可以调整计数器counter的增长速度,lfu-log-factor越大,counter增长的越慢。
|
|
|
|
|
|
`lfu-decay-time`是一个以分钟为单位的数值,可以调整counter的减少速度
|
|
|
这里有个问题是 8 位大小够计么,访问一次加 1 的话的确不够,不过大神就是大神,才不会这么简单的加一。往下看代码
|
|
|
```C
|
|
|
/* Low level key lookup API, not actually called directly from commands
|
|
|
* implementations that should instead rely on lookupKeyRead(),
|
|
|
* lookupKeyWrite() and lookupKeyReadWithFlags(). */
|
|
|
robj *lookupKey(redisDb *db, robj *key, int flags) {
|
|
|
dictEntry *de = dictFind(db->dict,key->ptr);
|
|
|
if (de) {
|
|
|
robj *val = dictGetVal(de);
|
|
|
|
|
|
/* Update the access time for the ageing algorithm.
|
|
|
* Don't do it if we have a saving child, as this will trigger
|
|
|
* a copy on write madness. */
|
|
|
if (!hasActiveChildProcess() && !(flags & LOOKUP_NOTOUCH)){
|
|
|
if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
|
|
|
// 当淘汰策略是 LFU 时,就会调用这个updateLFU
|
|
|
updateLFU(val);
|
|
|
} else {
|
|
|
val->lru = LRU_CLOCK();
|
|
|
}
|
|
|
}
|
|
|
return val;
|
|
|
} else {
|
|
|
return NULL;
|
|
|
}
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/db.c#L46"><code>updateLFU</code></a> 这个其实个入口,调用了两个重要的方法</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* Update LFU when an object is accessed.
|
|
|
* Firstly, decrement the counter if the decrement time is reached.
|
|
|
* Then logarithmically increment the counter, and update the access time. */
|
|
|
void updateLFU(robj *val) {
|
|
|
unsigned long counter = LFUDecrAndReturn(val);
|
|
|
counter = LFULogIncr(counter);
|
|
|
val->lru = (LFUGetTimeInMinutes()<<8) | counter;
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>首先来看看<a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/evict.c#L335"><code>LFUDecrAndReturn</code></a>,这个方法的作用是根据上一次衰减时间和系统配置的 <code>lfu-decay-time</code> 参数来确定需要将 counter 减去多少</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* If the object decrement time is reached decrement the LFU counter but
|
|
|
* do not update LFU fields of the object, we update the access time
|
|
|
* and counter in an explicit way when the object is really accessed.
|
|
|
* And we will times halve the counter according to the times of
|
|
|
* elapsed time than server.lfu_decay_time.
|
|
|
* Return the object frequency counter.
|
|
|
*
|
|
|
* This function is used in order to scan the dataset for the best object
|
|
|
* to fit: as we check for the candidate, we incrementally decrement the
|
|
|
* counter of the scanned objects if needed. */
|
|
|
unsigned long LFUDecrAndReturn(robj *o) {
|
|
|
// 右移 8 位,拿到上次衰减时间
|
|
|
unsigned long ldt = o->lru >> 8;
|
|
|
// 对 255 做与操作,拿到 counter 值
|
|
|
unsigned long counter = o->lru & 255;
|
|
|
// 根据lfu_decay_time来算出过了多少个衰减周期
|
|
|
unsigned long num_periods = server.lfu_decay_time ? LFUTimeElapsed(ldt) / server.lfu_decay_time : 0;
|
|
|
if (num_periods)
|
|
|
counter = (num_periods > counter) ? 0 : counter - num_periods;
|
|
|
return counter;
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>然后是加,调用了<a target="_blank" rel="noopener" href="https://github.com/antirez/redis/blob/unstable/src/evict.c#L315"><code>LFULogIncr</code></a></p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* Logarithmically increment a counter. The greater is the current counter value
|
|
|
* the less likely is that it gets really implemented. Saturate it at 255. */
|
|
|
uint8_t LFULogIncr(uint8_t counter) {
|
|
|
// 最大值就是 255,到顶了就不加了
|
|
|
if (counter == 255) return 255;
|
|
|
// 生成个随机小数
|
|
|
double r = (double)rand()/RAND_MAX;
|
|
|
// 减去个基础值,LFU_INIT_VAL = 5,防止刚进来就被逐出
|
|
|
double baseval = counter - LFU_INIT_VAL;
|
|
|
// 如果是小于 0,
|
|
|
if (baseval < 0) baseval = 0;
|
|
|
// 如果 baseval 是 0,那么 p 就是 1了,后面 counter 直接加一,如果不是的话,得看系统参数lfu_log_factor,这个越大,除出来的 p 越小,那么 counter++的可能性也越小,这样子就把前面的疑问给解决了,不是直接+1 的
|
|
|
double p = 1.0/(baseval*server.lfu_log_factor+1);
|
|
|
if (r < p) counter++;
|
|
|
return counter;
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>大概的变化速度可以参考</p><pre class="line-numbers language-none"><code class="language-none">+--------+------------+------------+------------+------------+------------+
|
|
|
| factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits |
|
|
|
+--------+------------+------------+------------+------------+------------+
|
|
|
| 0 | 104 | 255 | 255 | 255 | 255 |
|
|
|
+--------+------------+------------+------------+------------+------------+
|
|
|
| 1 | 18 | 49 | 255 | 255 | 255 |
|
|
|
+--------+------------+------------+------------+------------+------------+
|
|
|
| 10 | 10 | 18 | 142 | 255 | 255 |
|
|
|
+--------+------------+------------+------------+------------+------------+
|
|
|
| 100 | 8 | 11 | 49 | 143 | 255 |
|
|
|
+--------+------------+------------+------------+------------+------------+<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>简而言之就是 lfu_log_factor 越大变化的越慢</p><h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>总结一下,redis 实现了近似的 lru 淘汰策略,通过增加了淘汰 key 的池子(pool),并且增大每次抽样的 key 的数量来将淘汰效果更进一步地接近于 lru,这是 lru 策略,但是对于前面举的一个例子,其实 lru 并不能保证 key 的淘汰就如我们预期,所以在后期又引入了 lfu 的策略,lfu的策略比较巧妙,复用了 redis 对象的 lru 字段,并且使用了factor 参数来控制计数器递增的速度,防止 8 位的计数器太早溢出。</p></div><footer class="post-footer"><div class="post-eof"></div></footer></article></div><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content"><link itemprop="mainEntityOfPage" href="https://nicksxs.me/2020/04/12/redis%E7%B3%BB%E5%88%97%E4%BB%8B%E7%BB%8D%E4%B8%83/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/uploads/avatar.jpg"><meta itemprop="name" content="Nicksxs"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Nicksxs's Blog"><meta itemprop="description" content="learn from zero,技术博客,Nicksxs,史学森"></span><span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork"><meta itemprop="name" content="undefined | Nicksxs's Blog"><meta itemprop="description" content=""></span><header class="post-header"><h2 class="post-title" itemprop="name headline"><a href="/2020/04/12/redis%E7%B3%BB%E5%88%97%E4%BB%8B%E7%BB%8D%E4%B8%83/" class="post-title-link" itemprop="url">redis系列介绍七-过期策略</a></h2><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间:2020-04-12 21:22:00 / 修改时间:22:57:42" itemprop="dateCreated datePublished" datetime="2020-04-12T21:22:00+08:00">2020-04-12</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-folder"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Redis/" itemprop="url" rel="index"><span itemprop="name">Redis</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Redis/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">数据结构</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Redis/%E6%BA%90%E7%A0%81/" itemprop="url" rel="index"><span itemprop="name">源码</span></a> </span>, <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/C/Redis/" itemprop="url" rel="index"><span itemprop="name">Redis</span></a> </span></span><span id="/2020/04/12/redis%E7%B3%BB%E5%88%97%E4%BB%8B%E7%BB%8D%E4%B8%83/" class="post-meta-item leancloud_visitors" data-flag-title="redis系列介绍七-过期策略" title="阅读次数"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数:</span> <span class="leancloud-visitors-count"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-comment"></i> </span><span class="post-meta-item-text">Disqus:</span> <a title="disqus" href="/2020/04/12/redis%E7%B3%BB%E5%88%97%E4%BB%8B%E7%BB%8D%E4%B8%83/#disqus_thread" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="2020/04/12/redis系列介绍七/" itemprop="commentCount"></span></a></span></div></div></header><div class="post-body" itemprop="articleBody"><p>这一篇不再是数据结构介绍了,大致的数据结构基本都介绍了,这一篇主要是查漏补缺,或者说讲一些重要且基本的概念,也可能是经常被忽略的,很多讲 redis 的系列文章可能都会忽略,学习 redis 的时候也会,因为觉得源码学习就是讲主要的数据结构和“算法”学习了就好了。<br>redis 的主要应用就是拿来作为高性能的缓存,那么缓存一般有些啥需要注意的,首先是访问速度,如果取得跟数据库一样快,那就没什么存在的意义,第二个是缓存的字面意思,我只是为了让数据读取快一些,通常大部分的场景这个是需要更新过期的,这里就把我要讲的第一点引出来了(真累,</p><h2 id="redis过期策略"><a href="#redis过期策略" class="headerlink" title="redis过期策略"></a>redis过期策略</h2><p>redis 是如何过期缓存的,可以猜测下,最无脑的就是每个设置了过期时间的 key 都设个定时器,过期了就删除,这种显然消耗太大,清理地最及时,还有的就是 redis 正在采用的懒汉清理策略和定期清理<br>懒汉策略就是在使用的时候去检查缓存是否过期,比如 get 操作时,先判断下这个 key 是否已经过期了,如果过期了就删掉,并且返回空,如果没过期则正常返回<br>主要代码是</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* This function is called when we are going to perform some operation
|
|
|
* in a given key, but such key may be already logically expired even if
|
|
|
* it still exists in the database. The main way this function is called
|
|
|
* is via lookupKey*() family of functions.
|
|
|
*
|
|
|
* The behavior of the function depends on the replication role of the
|
|
|
* instance, because slave instances do not expire keys, they wait
|
|
|
* for DELs from the master for consistency matters. However even
|
|
|
* slaves will try to have a coherent return value for the function,
|
|
|
* so that read commands executed in the slave side will be able to
|
|
|
* behave like if the key is expired even if still present (because the
|
|
|
* master has yet to propagate the DEL).
|
|
|
*
|
|
|
* In masters as a side effect of finding a key which is expired, such
|
|
|
* key will be evicted from the database. Also this may trigger the
|
|
|
* propagation of a DEL/UNLINK command in AOF / replication stream.
|
|
|
*
|
|
|
* The return value of the function is 0 if the key is still valid,
|
|
|
* otherwise the function returns 1 if the key is expired. */
|
|
|
int expireIfNeeded(redisDb *db, robj *key) {
|
|
|
if (!keyIsExpired(db,key)) return 0;
|
|
|
|
|
|
/* If we are running in the context of a slave, instead of
|
|
|
* evicting the expired key from the database, we return ASAP:
|
|
|
* the slave key expiration is controlled by the master that will
|
|
|
* send us synthesized DEL operations for expired keys.
|
|
|
*
|
|
|
* Still we try to return the right information to the caller,
|
|
|
* that is, 0 if we think the key should be still valid, 1 if
|
|
|
* we think the key is expired at this time. */
|
|
|
if (server.masterhost != NULL) return 1;
|
|
|
|
|
|
/* Delete the key */
|
|
|
server.stat_expiredkeys++;
|
|
|
propagateExpire(db,key,server.lazyfree_lazy_expire);
|
|
|
notifyKeyspaceEvent(NOTIFY_EXPIRED,
|
|
|
"expired",key,db->id);
|
|
|
return server.lazyfree_lazy_expire ? dbAsyncDelete(db,key) :
|
|
|
dbSyncDelete(db,key);
|
|
|
}
|
|
|
|
|
|
/* Check if the key is expired. */
|
|
|
int keyIsExpired(redisDb *db, robj *key) {
|
|
|
mstime_t when = getExpire(db,key);
|
|
|
mstime_t now;
|
|
|
|
|
|
if (when < 0) return 0; /* No expire for this key */
|
|
|
|
|
|
/* Don't expire anything while loading. It will be done later. */
|
|
|
if (server.loading) return 0;
|
|
|
|
|
|
/* If we are in the context of a Lua script, we pretend that time is
|
|
|
* blocked to when the Lua script started. This way a key can expire
|
|
|
* only the first time it is accessed and not in the middle of the
|
|
|
* script execution, making propagation to slaves / AOF consistent.
|
|
|
* See issue #1525 on Github for more information. */
|
|
|
if (server.lua_caller) {
|
|
|
now = server.lua_time_start;
|
|
|
}
|
|
|
/* If we are in the middle of a command execution, we still want to use
|
|
|
* a reference time that does not change: in that case we just use the
|
|
|
* cached time, that we update before each call in the call() function.
|
|
|
* This way we avoid that commands such as RPOPLPUSH or similar, that
|
|
|
* may re-open the same key multiple times, can invalidate an already
|
|
|
* open object in a next call, if the next call will see the key expired,
|
|
|
* while the first did not. */
|
|
|
else if (server.fixed_time_expire > 0) {
|
|
|
now = server.mstime;
|
|
|
}
|
|
|
/* For the other cases, we want to use the most fresh time we have. */
|
|
|
else {
|
|
|
now = mstime();
|
|
|
}
|
|
|
|
|
|
/* The key expired if the current (virtual or real) time is greater
|
|
|
* than the expire time of the key. */
|
|
|
return now > when;
|
|
|
}
|
|
|
/* Return the expire time of the specified key, or -1 if no expire
|
|
|
* is associated with this key (i.e. the key is non volatile) */
|
|
|
long long getExpire(redisDb *db, robj *key) {
|
|
|
dictEntry *de;
|
|
|
|
|
|
/* No expire? return ASAP */
|
|
|
if (dictSize(db->expires) == 0 ||
|
|
|
(de = dictFind(db->expires,key->ptr)) == NULL) return -1;
|
|
|
|
|
|
/* The entry was found in the expire dict, this means it should also
|
|
|
* be present in the main dict (safety check). */
|
|
|
serverAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL);
|
|
|
return dictGetSignedIntegerVal(de);
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>这里有几点要注意的,第一是当惰性删除时会根据lazyfree_lazy_expire这个参数去判断是执行同步删除还是异步删除,另外一点是对于 slave,是不需要执行的,因为会在 master 过期时向 slave 发送 del 指令。<br>光采用这个策略会有什么问题呢,假如一些key 一直未被访问,那这些 key 就不会过期了,导致一直被占用着内存,所以 redis 采取了懒汉式过期加定期过期策略,定期策略是怎么执行的呢</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">/* This function handles 'background' operations we are required to do
|
|
|
* incrementally in Redis databases, such as active key expiring, resizing,
|
|
|
* rehashing. */
|
|
|
void databasesCron(void) {
|
|
|
/* Expire keys by random sampling. Not required for slaves
|
|
|
* as master will synthesize DELs for us. */
|
|
|
if (server.active_expire_enabled) {
|
|
|
if (server.masterhost == NULL) {
|
|
|
activeExpireCycle(ACTIVE_EXPIRE_CYCLE_SLOW);
|
|
|
} else {
|
|
|
expireSlaveKeys();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Defrag keys gradually. */
|
|
|
activeDefragCycle();
|
|
|
|
|
|
/* Perform hash tables rehashing if needed, but only if there are no
|
|
|
* other processes saving the DB on disk. Otherwise rehashing is bad
|
|
|
* as will cause a lot of copy-on-write of memory pages. */
|
|
|
if (!hasActiveChildProcess()) {
|
|
|
/* We use global counters so if we stop the computation at a given
|
|
|
* DB we'll be able to start from the successive in the next
|
|
|
* cron loop iteration. */
|
|
|
static unsigned int resize_db = 0;
|
|
|
static unsigned int rehash_db = 0;
|
|
|
int dbs_per_call = CRON_DBS_PER_CALL;
|
|
|
int j;
|
|
|
|
|
|
/* Don't test more DBs than we have. */
|
|
|
if (dbs_per_call > server.dbnum) dbs_per_call = server.dbnum;
|
|
|
|
|
|
/* Resize */
|
|
|
for (j = 0; j < dbs_per_call; j++) {
|
|
|
tryResizeHashTables(resize_db % server.dbnum);
|
|
|
resize_db++;
|
|
|
}
|
|
|
|
|
|
/* Rehash */
|
|
|
if (server.activerehashing) {
|
|
|
for (j = 0; j < dbs_per_call; j++) {
|
|
|
int work_done = incrementallyRehash(rehash_db);
|
|
|
if (work_done) {
|
|
|
/* If the function did some work, stop here, we'll do
|
|
|
* more at the next cron loop. */
|
|
|
break;
|
|
|
} else {
|
|
|
/* If this db didn't need rehash, we'll try the next one. */
|
|
|
rehash_db++;
|
|
|
rehash_db %= server.dbnum;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/* Try to expire a few timed out keys. The algorithm used is adaptive and
|
|
|
* will use few CPU cycles if there are few expiring keys, otherwise
|
|
|
* it will get more aggressive to avoid that too much memory is used by
|
|
|
* keys that can be removed from the keyspace.
|
|
|
*
|
|
|
* Every expire cycle tests multiple databases: the next call will start
|
|
|
* again from the next db, with the exception of exists for time limit: in that
|
|
|
* case we restart again from the last database we were processing. Anyway
|
|
|
* no more than CRON_DBS_PER_CALL databases are tested at every iteration.
|
|
|
*
|
|
|
* The function can perform more or less work, depending on the "type"
|
|
|
* argument. It can execute a "fast cycle" or a "slow cycle". The slow
|
|
|
* cycle is the main way we collect expired cycles: this happens with
|
|
|
* the "server.hz" frequency (usually 10 hertz).
|
|
|
*
|
|
|
* However the slow cycle can exit for timeout, since it used too much time.
|
|
|
* For this reason the function is also invoked to perform a fast cycle
|
|
|
* at every event loop cycle, in the beforeSleep() function. The fast cycle
|
|
|
* will try to perform less work, but will do it much more often.
|
|
|
*
|
|
|
* The following are the details of the two expire cycles and their stop
|
|
|
* conditions:
|
|
|
*
|
|
|
* If type is ACTIVE_EXPIRE_CYCLE_FAST the function will try to run a
|
|
|
* "fast" expire cycle that takes no longer than EXPIRE_FAST_CYCLE_DURATION
|
|
|
* microseconds, and is not repeated again before the same amount of time.
|
|
|
* The cycle will also refuse to run at all if the latest slow cycle did not
|
|
|
* terminate because of a time limit condition.
|
|
|
*
|
|
|
* If type is ACTIVE_EXPIRE_CYCLE_SLOW, that normal expire cycle is
|
|
|
* executed, where the time limit is a percentage of the REDIS_HZ period
|
|
|
* as specified by the ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC define. In the
|
|
|
* fast cycle, the check of every database is interrupted once the number
|
|
|
* of already expired keys in the database is estimated to be lower than
|
|
|
* a given percentage, in order to avoid doing too much work to gain too
|
|
|
* little memory.
|
|
|
*
|
|
|
* The configured expire "effort" will modify the baseline parameters in
|
|
|
* order to do more work in both the fast and slow expire cycles.
|
|
|
*/
|
|
|
|
|
|
#define ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP 20 /* Keys for each DB loop. */
|
|
|
#define ACTIVE_EXPIRE_CYCLE_FAST_DURATION 1000 /* Microseconds. */
|
|
|
#define ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC 25 /* Max % of CPU to use. */
|
|
|
#define ACTIVE_EXPIRE_CYCLE_ACCEPTABLE_STALE 10 /* % of stale keys after which
|
|
|
we do extra efforts. */
|
|
|
void activeExpireCycle(int type) {
|
|
|
/* Adjust the running parameters according to the configured expire
|
|
|
* effort. The default effort is 1, and the maximum configurable effort
|
|
|
* is 10. */
|
|
|
unsigned long
|
|
|
effort = server.active_expire_effort-1, /* Rescale from 0 to 9. */
|
|
|
config_keys_per_loop = ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP +
|
|
|
ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP/4*effort,
|
|
|
config_cycle_fast_duration = ACTIVE_EXPIRE_CYCLE_FAST_DURATION +
|
|
|
ACTIVE_EXPIRE_CYCLE_FAST_DURATION/4*effort,
|
|
|
config_cycle_slow_time_perc = ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC +
|
|
|
2*effort,
|
|
|
config_cycle_acceptable_stale = ACTIVE_EXPIRE_CYCLE_ACCEPTABLE_STALE-
|
|
|
effort;
|
|
|
|
|
|
/* This function has some global state in order to continue the work
|
|
|
* incrementally across calls. */
|
|
|
static unsigned int current_db = 0; /* Last DB tested. */
|
|
|
static int timelimit_exit = 0; /* Time limit hit in previous call? */
|
|
|
static long long last_fast_cycle = 0; /* When last fast cycle ran. */
|
|
|
|
|
|
int j, iteration = 0;
|
|
|
int dbs_per_call = CRON_DBS_PER_CALL;
|
|
|
long long start = ustime(), timelimit, elapsed;
|
|
|
|
|
|
/* When clients are paused the dataset should be static not just from the
|
|
|
* POV of clients not being able to write, but also from the POV of
|
|
|
* expires and evictions of keys not being performed. */
|
|
|
if (clientsArePaused()) return;
|
|
|
|
|
|
if (type == ACTIVE_EXPIRE_CYCLE_FAST) {
|
|
|
/* Don't start a fast cycle if the previous cycle did not exit
|
|
|
* for time limit, unless the percentage of estimated stale keys is
|
|
|
* too high. Also never repeat a fast cycle for the same period
|
|
|
* as the fast cycle total duration itself. */
|
|
|
if (!timelimit_exit &&
|
|
|
server.stat_expired_stale_perc < config_cycle_acceptable_stale)
|
|
|
return;
|
|
|
|
|
|
if (start < last_fast_cycle + (long long)config_cycle_fast_duration*2)
|
|
|
return;
|
|
|
|
|
|
last_fast_cycle = start;
|
|
|
}
|
|
|
|
|
|
/* We usually should test CRON_DBS_PER_CALL per iteration, with
|
|
|
* two exceptions:
|
|
|
*
|
|
|
* 1) Don't test more DBs than we have.
|
|
|
* 2) If last time we hit the time limit, we want to scan all DBs
|
|
|
* in this iteration, as there is work to do in some DB and we don't want
|
|
|
* expired keys to use memory for too much time. */
|
|
|
if (dbs_per_call > server.dbnum || timelimit_exit)
|
|
|
dbs_per_call = server.dbnum;
|
|
|
|
|
|
/* We can use at max 'config_cycle_slow_time_perc' percentage of CPU
|
|
|
* time per iteration. Since this function gets called with a frequency of
|
|
|
* server.hz times per second, the following is the max amount of
|
|
|
* microseconds we can spend in this function. */
|
|
|
timelimit = config_cycle_slow_time_perc*1000000/server.hz/100;
|
|
|
timelimit_exit = 0;
|
|
|
if (timelimit <= 0) timelimit = 1;
|
|
|
|
|
|
if (type == ACTIVE_EXPIRE_CYCLE_FAST)
|
|
|
timelimit = config_cycle_fast_duration; /* in microseconds. */
|
|
|
|
|
|
/* Accumulate some global stats as we expire keys, to have some idea
|
|
|
* about the number of keys that are already logically expired, but still
|
|
|
* existing inside the database. */
|
|
|
long total_sampled = 0;
|
|
|
long total_expired = 0;
|
|
|
|
|
|
for (j = 0; j < dbs_per_call && timelimit_exit == 0; j++) {
|
|
|
/* Expired and checked in a single loop. */
|
|
|
unsigned long expired, sampled;
|
|
|
|
|
|
redisDb *db = server.db+(current_db % server.dbnum);
|
|
|
|
|
|
/* Increment the DB now so we are sure if we run out of time
|
|
|
* in the current DB we'll restart from the next. This allows to
|
|
|
* distribute the time evenly across DBs. */
|
|
|
current_db++;
|
|
|
|
|
|
/* Continue to expire if at the end of the cycle more than 25%
|
|
|
* of the keys were expired. */
|
|
|
do {
|
|
|
unsigned long num, slots;
|
|
|
long long now, ttl_sum;
|
|
|
int ttl_samples;
|
|
|
iteration++;
|
|
|
|
|
|
/* If there is nothing to expire try next DB ASAP. */
|
|
|
if ((num = dictSize(db->expires)) == 0) {
|
|
|
db->avg_ttl = 0;
|
|
|
break;
|
|
|
}
|
|
|
slots = dictSlots(db->expires);
|
|
|
now = mstime();
|
|
|
|
|
|
/* When there are less than 1% filled slots, sampling the key
|
|
|
* space is expensive, so stop here waiting for better times...
|
|
|
* The dictionary will be resized asap. */
|
|
|
if (num && slots > DICT_HT_INITIAL_SIZE &&
|
|
|
(num*100/slots < 1)) break;
|
|
|
|
|
|
/* The main collection cycle. Sample random keys among keys
|
|
|
* with an expire set, checking for expired ones. */
|
|
|
expired = 0;
|
|
|
sampled = 0;
|
|
|
ttl_sum = 0;
|
|
|
ttl_samples = 0;
|
|
|
|
|
|
if (num > config_keys_per_loop)
|
|
|
num = config_keys_per_loop;
|
|
|
|
|
|
/* Here we access the low level representation of the hash table
|
|
|
* for speed concerns: this makes this code coupled with dict.c,
|
|
|
* but it hardly changed in ten years.
|
|
|
*
|
|
|
* Note that certain places of the hash table may be empty,
|
|
|
* so we want also a stop condition about the number of
|
|
|
* buckets that we scanned. However scanning for free buckets
|
|
|
* is very fast: we are in the cache line scanning a sequential
|
|
|
* array of NULL pointers, so we can scan a lot more buckets
|
|
|
* than keys in the same time. */
|
|
|
long max_buckets = num*20;
|
|
|
long checked_buckets = 0;
|
|
|
|
|
|
while (sampled < num && checked_buckets < max_buckets) {
|
|
|
for (int table = 0; table < 2; table++) {
|
|
|
if (table == 1 && !dictIsRehashing(db->expires)) break;
|
|
|
|
|
|
unsigned long idx = db->expires_cursor;
|
|
|
idx &= db->expires->ht[table].sizemask;
|
|
|
dictEntry *de = db->expires->ht[table].table[idx];
|
|
|
long long ttl;
|
|
|
|
|
|
/* Scan the current bucket of the current table. */
|
|
|
checked_buckets++;
|
|
|
while(de) {
|
|
|
/* Get the next entry now since this entry may get
|
|
|
* deleted. */
|
|
|
dictEntry *e = de;
|
|
|
de = de->next;
|
|
|
|
|
|
ttl = dictGetSignedIntegerVal(e)-now;
|
|
|
if (activeExpireCycleTryExpire(db,e,now)) expired++;
|
|
|
if (ttl > 0) {
|
|
|
/* We want the average TTL of keys yet
|
|
|
* not expired. */
|
|
|
ttl_sum += ttl;
|
|
|
ttl_samples++;
|
|
|
}
|
|
|
sampled++;
|
|
|
}
|
|
|
}
|
|
|
db->expires_cursor++;
|
|
|
}
|
|
|
total_expired += expired;
|
|
|
total_sampled += sampled;
|
|
|
|
|
|
/* Update the average TTL stats for this database. */
|
|
|
if (ttl_samples) {
|
|
|
long long avg_ttl = ttl_sum/ttl_samples;
|
|
|
|
|
|
/* Do a simple running average with a few samples.
|
|
|
* We just use the current estimate with a weight of 2%
|
|
|
* and the previous estimate with a weight of 98%. */
|
|
|
if (db->avg_ttl == 0) db->avg_ttl = avg_ttl;
|
|
|
db->avg_ttl = (db->avg_ttl/50)*49 + (avg_ttl/50);
|
|
|
}
|
|
|
|
|
|
/* We can't block forever here even if there are many keys to
|
|
|
* expire. So after a given amount of milliseconds return to the
|
|
|
* caller waiting for the other active expire cycle. */
|
|
|
if ((iteration & 0xf) == 0) { /* check once every 16 iterations. */
|
|
|
elapsed = ustime()-start;
|
|
|
if (elapsed > timelimit) {
|
|
|
timelimit_exit = 1;
|
|
|
server.stat_expired_time_cap_reached_count++;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
/* We don't repeat the cycle for the current database if there are
|
|
|
* an acceptable amount of stale keys (logically expired but yet
|
|
|
* not reclained). */
|
|
|
} while ((expired*100/sampled) > config_cycle_acceptable_stale);
|
|
|
}
|
|
|
|
|
|
elapsed = ustime()-start;
|
|
|
server.stat_expire_cycle_time_used += elapsed;
|
|
|
latencyAddSampleIfNeeded("expire-cycle",elapsed/1000);
|
|
|
|
|
|
/* Update our estimate of keys existing but yet to be expired.
|
|
|
* Running average with this sample accounting for 5%. */
|
|
|
double current_perc;
|
|
|
if (total_sampled) {
|
|
|
current_perc = (double)total_expired/total_sampled;
|
|
|
} else
|
|
|
current_perc = 0;
|
|
|
server.stat_expired_stale_perc = (current_perc*0.05)+
|
|
|
(server.stat_expired_stale_perc*0.95);
|
|
|
}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>执行定期清除分成两种类型,快和慢,分别由<code>beforeSleep</code>和<code>databasesCron</code>调用,快版有两个限制,一个是执行时长由ACTIVE_EXPIRE_CYCLE_FAST_DURATION限制,另一个是执行间隔是 2 倍的ACTIVE_EXPIRE_CYCLE_FAST_DURATION,另外这还可以由配置的server.active_expire_effort参数来控制,默认是 1,最大是 10</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">onfig_cycle_fast_duration = ACTIVE_EXPIRE_CYCLE_FAST_DURATION +
|
|
|
ACTIVE_EXPIRE_CYCLE_FAST_DURATION/4*effort<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>然后会从一定数量的 db 中找出一定数量的带过期时间的 key(保存在 expires中),这里的数量是由</p><pre class="line-numbers language-C" data-language="C"><code class="language-C">config_keys_per_loop = ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP +
|
|
|
ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP/4*effort
|
|
|
```
|
|
|
控制,慢速的执行时长是
|
|
|
```C
|
|
|
config_cycle_slow_time_perc = ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC +
|
|
|
2*effort
|
|
|
timelimit = config_cycle_slow_time_perc*1000000/server.hz/100;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>这里还有一个额外的退出条件,如果当前数据库的抽样结果已经达到我们所允许的过期 key 百分比,则下次不再处理当前 db,继续处理下个 db</p></div><footer class="post-footer"><div class="post-eof"></div></footer></article></div><nav class="pagination"><a class="extend prev" rel="prev" href="/page/25/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">…</span><a class="page-number" href="/page/25/">25</a><span class="page-number current">26</span><a class="page-number" href="/page/27/">27</a><span class="space">…</span><a class="page-number" href="/page/35/">35</a><a class="extend next" rel="next" href="/page/27/"><i class="fa fa-angle-right" aria-label="下一页"></i></a></nav></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">© <span itemprop="copyrightYear">2022</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">Nicksxs</span></div><div class="busuanzi-count"><span class="post-meta-item" id="busuanzi_container_site_uv"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-item" id="busuanzi_container_site_pv"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div></div></footer><script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script><script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/next-boot.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script><script src="/js/third-party/search/local-search.js"></script><script src="/js/third-party/fancybox.js"></script><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><script class="next-config" data-name="leancloud_visitors" type="application/json">{"enable":true,"app_id":"ysza182Vghlqjdt7QiwGLLJy-gzGzoHsz","app_key":"s9GDqbn7gnGGkusf66YRVccw","server_url":"https://leancloud.cn","security":true}</script><script src="/js/third-party/statistics/lean-analytics.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.2.0/quicklink.umd.js" integrity="sha256-4kQf9z5ntdQrzsBC3YSHnEz02Z9C1UeW/E9OgnvlzSY=" crossorigin="anonymous"></script><script class="next-config" data-name="quicklink" type="application/json">{"enable":false,"home":false,"archive":false,"delay":true,"timeout":3000,"priority":true,"url":"https://nicksxs.me/page/26/"}</script><script src="/js/third-party/quicklink.js"></script><script class="next-config" data-name="disqus" type="application/json">{"enable":true,"shortname":"nicksxs","count":true,"i18n":{"disqus":"disqus"}}</script><script src="/js/third-party/comments/disqus.js"></script></body></html>
|