{"id":2960,"date":"2021-07-05T22:34:22","date_gmt":"2021-07-05T14:34:22","guid":{"rendered":"\/?p=2960"},"modified":"2021-07-06T22:34:31","modified_gmt":"2021-07-06T14:34:31","slug":"5-8-%e8%bf%87%e6%9c%9f%e6%96%87%e4%bb%b6%e5%88%a0%e9%99%a4%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=2960","title":{"rendered":"5.8 \u8fc7\u671f\u6587\u4ef6\u5220\u9664\u673a\u5236"},"content":{"rendered":"<p>RMQ\u7684\u6587\u4ef6\u673a\u5236\u4ea4\u7531CommitLog\u548cConsumeQueue\u5b9e\u73b0\u7684,\u5728\u542f\u52a8\u7684\u65f6\u5019,\u4f1a\u52a0\u8f7dcommitlog\u548cconsumeQueue\u4e0b\u7684\u6240\u6709\u6587\u4ef6,\u4e3a\u4e86\u907f\u514d\u6d6a\u8d39\u5185\u5b58,\u4e0d\u4f1a\u5c06\u4e00\u4e2a\u6587\u4ef6\u6c38\u4e45\u5b58\u50a8\u5728\u670d\u52a1\u5668\u4e0a,\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u5728\u4e00\u5b9a\u65f6\u95f4\u5185\u6ca1\u6709\u518d\u6b21\u88ab\u66f4\u65b0,\u5219\u8ba4\u4e3a\u662f\u8fc7\u671f\u6587\u4ef6,\u53ef\u4ee5\u88ab\u5220\u9664,RMQ\u4e0d\u4f1a\u5173\u6ce8\u8fd9\u4e2a\u6587\u4ef6\u4e0a\u6d88\u606f\u662f\u5426\u80fd\u591f\u88ab\u5168\u90e8\u6d88\u8d39,\u8ba4\u4e3a\u5982\u679c\u6587\u4ef6\u8d85\u8fc7\u4e8672\u5c0f\u65f6,\u5c31\u8fdb\u884c\u5220\u9664,\u8fd9\u4e2a\u65f6\u95f4\u53ef\u4ee5\u901a\u8fc7<\/p>\n<p>fileReseverdTime\u8fdb\u884c\u914d\u7f6e<\/p>\n<p>\u6211\u4eec\u5c31\u6765\u770b\u4e0b\u8fd9\u4e2a\u8fc7\u671f\u6587\u4ef6\u5220\u9664\u7684\u673a\u5236\u5982\u4f55\u5b9e\u73b0\u7684<\/p>\n<p>\u5165\u53e3\u5728DefaultMessageStore\u4e2d\u7684addScheduleTask\u5b9e\u73b0<\/p>\n<p>\u5728\u6574\u4e2a\u51fd\u6570\u4e2d,\u8fdb\u884c\u4e00\u4e2a\u5b9a\u65f6\u7ebf\u7a0b\u7684\u89e6\u53d1<\/p>\n<table>\n<tbody>\n<tr>\n<td>this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {<\/p>\n<p>@Override<\/p>\n<p>public void run() {<\/p>\n<p>DefaultMessageStore.this.cleanFilesPeriodically();<\/p>\n<p>}<\/p>\n<p>}, 1000 * 60, this.messageStoreConfig.getCleanResourceInterval(), TimeUnit.<em>MILLISECONDS<\/em>);<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d\u7684cleanFilesPeriodically\u5c31\u662f\u5b9e\u9645\u7684\u6267\u884c\u8005,\u4ee3\u7801\u4e2d\u58f0\u660e\u4e86,\u6bcf\u969410s\u4e2d\u8fdb\u884c\u4e00\u6b21\u8c03\u7528,<\/p>\n<p>\u5728\u5176\u4e2d,\u5206\u522b\u8c03\u7528\u4e86<\/p>\n<table>\n<tbody>\n<tr>\n<td>private void cleanFilesPeriodically() {<\/p>\n<p>this.cleanCommitLogService.run();<\/p>\n<p>this.cleanConsumeQueueService.run();<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5206\u522b\u662f\u8bf7\u6c42\u6d88\u606f\u5b58\u50a8\u6587\u4ef6 commitLog<\/p>\n<p>\u6d88\u606f\u961f\u5217\u6d88\u8d39\u6587\u4ef6 ConsumeQueue<\/p>\n<p>\u8fd9\u4e00\u6b21,\u6211\u4eec\u4ee5CommitLog\u4e3a\u4e3b\u7ebf\u8fdb\u884c\u8bb2\u89e3<\/p>\n<p>\u4e3b\u8981\u7684\u5b9e\u73b0\u65b9\u5f0f<\/p>\n<p>CleanCommitLogService#run<\/p>\n<p>\u5728run\u51fd\u6570\u4e2d\u9996\u5148\u662fdeleteExpiredFiles,\u5176\u4e2d<\/p>\n<p>\u9996\u5148\u662f\u83b7\u53d6\u4e00\u4e9b\u5bf9\u5e94\u7684\u914d\u7f6e\u5c5e\u6027<\/p>\n<table>\n<tbody>\n<tr>\n<td>\/\/\u522a\u9664\u6578\u91cf<\/p>\n<p>int deleteCount = 0;<\/p>\n<p>\/\/\u6587\u4ef6\u4fdd\u7559\u65f6\u95f4.\u662f\u6839\u636e\u6587\u4ef6\u66f4\u65b0\u65f6\u95f4\u6233\u8fdb\u884c\u5224\u65ad\u8fc7\u671f\u7684\u4f9d\u636e<\/p>\n<p>long fileReservedTime = DefaultMessageStore.this.getMessageStoreConfig().getFileReservedTime();<\/p>\n<p>\/\/\u5220\u9664\u6587\u4ef6\u7684\u95f4\u9694,\u591a\u5220\u7684\u65f6\u5019,\u6307\u5b9a\u5220\u9664\u7684\u6bcf\u6b21\u95f4\u9694<\/p>\n<p>int deletePhysicFilesInterval = DefaultMessageStore.this.getMessageStoreConfig().getDeleteCommitLogFilesInterval();<\/p>\n<p>\/\/\u5982\u679c\u6709\u6587\u4ef6\u62d2\u7edd\u88ab\u5220\u9664,\u5219\u8bb0\u5f55\u8fd9\u4e2a\u65f6\u95f4,\u5728\u4e0b\u9762\u7684\u89c4\u5b9a\u65f6\u95f4\u95f4\u9694\u5230\u4e86\u4e4b\u540e,\u5f3a\u5236\u5220\u9664<\/p>\n<p>int destroyMapedFileIntervalForcibly = DefaultMessageStore.this.getMessageStoreConfig().getDestroyMapedFileIntervalForcibly();<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7136\u540e\u5224\u65ad,\u662f\u5426\u5220\u9664\u7684\u6761\u4ef6<\/p>\n<p>\u9700\u8981\u5224\u65ad\u4e09\u4e2a,\u4e09\u4e2a\u53ea\u8981\u6ee1\u8db3\u4e00\u4e2a\u5c31\u53ef\u4ee5\u8fdb\u884c\u5220\u9664<\/p>\n<table>\n<tbody>\n<tr>\n<td>\/\/\u4e09\u79cd\u60c5\u51b5\u6ee1\u8db3\u5176\u4e00,\u8fdb\u884c\u5220\u9664<\/p>\n<p>\/\/\u6307\u5b9a\u5220\u9664\u6587\u4ef6\u7684\u65f6\u95f4\u70b9,deleteWhen\u8bbe\u7f6e\u4e00\u4e2a\u56fa\u5b9a\u5220\u9664\u7684\u65f6\u95f4\u70b9,\u9ed8\u8ba4\u662f\u51cc\u66684\u70b9<\/p>\n<p>boolean timeup = this.isTimeToDelete();<\/p>\n<p>\/\/\u78c1\u76d8\u7a7a\u95f4\u662f\u5426\u5145\u8db3<\/p>\n<p>boolean spacefull = this. isSpaceToDelete();<\/p>\n<p>\/\/\u624b\u52a8\u89e6\u53d1,\u6682\u65e0\u63d0\u4f9b\u54cd\u5e94\u7684\u6307\u4ee4<\/p>\n<p>boolean manualDelete = this.manualDeleteFileSeveralTimes &gt; 0;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d,\u8ba1\u7b97\u78c1\u76d8\u7a7a\u95f4\u662f\u5426\u5145\u8db3\u7684\u51fd\u6570\u8be6\u60c5\u5982\u4e0b<\/p>\n<table>\n<tbody>\n<tr>\n<td>\/\/\u68c0\u6d4b\u78c1\u76d8\u662f\u5426\u5145\u8db3<\/p>\n<p>private boolean isSpaceToDelete() {<\/p>\n<p>\/\/DiskMaxUsedSpaceRatio \u6700\u5927\u4f7f\u7528\u7387<\/p>\n<p>double ratio = DefaultMessageStore.this.getMessageStoreConfig().getDiskMaxUsedSpaceRatio() \/ 100.0;<\/p>\n<p>\/\/\u662f\u5426\u7acb\u523b\u6267\u884c\u6e05\u9664\u8fc7\u671f\u6587\u4ef6\u64cd\u4f5c<\/p>\n<p>cleanImmediately = false;<\/p>\n<p>{<\/p>\n<p>\/\/\u83b7\u53d6\u5230\u7269\u7406\u4f7f\u7528\u7387<\/p>\n<p>double physicRatio = UtilAll.<em>getDiskPartitionSpaceUsedPercent<\/em>(getStorePathPhysic());<\/p>\n<p>if (physicRatio &gt; diskSpaceWarningLevelRatio) {<\/p>\n<p>boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskFull();<\/p>\n<p>if (diskok) {<\/p>\n<p>DefaultMessageStore.<em>log<\/em>.error(&#8220;physic disk maybe full soon &#8221; + physicRatio + &#8220;, so mark disk full&#8221;);<\/p>\n<p>}<\/p>\n<p>cleanImmediately = true;<\/p>\n<p>} else if (physicRatio &gt; diskSpaceCleanForciblyRatio) {<\/p>\n<p>cleanImmediately = true;<\/p>\n<p>} else {<\/p>\n<p>boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskOK();<\/p>\n<p>if (!diskok) {<\/p>\n<p>DefaultMessageStore.<em>log<\/em>.info(&#8220;physic disk space OK &#8221; + physicRatio + &#8220;, so mark disk ok&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>if (physicRatio &lt; 0 || physicRatio &gt; ratio) {<\/p>\n<p>DefaultMessageStore.<em>log<\/em>.info(&#8220;physic disk maybe full soon, so reclaim space, &#8221; + physicRatio);<\/p>\n<p>return true;<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u83b7\u53d6\u5230\u7269\u7406\u78c1\u76d8\u7684\u4f7f\u7528\u7387\u8fdb\u884c\u5bf9\u6bd4,\u83b7\u5f97\u662f\u5426\u53ef\u4ee5\u5220\u9664\u7684\u7ed3\u679c<\/p>\n<p>\u63a5\u4e0b\u6765\u8d70\u5230\u8fdb\u884c\u5220\u9664\u7684\u51fd\u6570\u4e2d<\/p>\n<table>\n<tbody>\n<tr>\n<td>deleteCount = DefaultMessageStore.this.commitLog.deleteExpiredFile(fileReservedTime, deletePhysicFilesInterval,<\/p>\n<p>destroyMapedFileIntervalForcibly, cleanAtOnce);<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e00\u8def\u8d70\u5230\u5bf9\u5e94\u7684<\/p>\n<p>deleteExpiredFileByTime\u51fd\u6570\u4e2d<\/p>\n<table>\n<tbody>\n<tr>\n<td>public int deleteExpiredFileByTime(final long expiredTime,<\/p>\n<p>final int deleteFilesInterval,<\/p>\n<p>final long intervalForcibly,<\/p>\n<p>final boolean cleanImmediately) {<\/p>\n<p>Object[] mfs = this.copyMappedFiles(0);<\/p>\n<p>\/\/\u62ff\u5230\u6240\u6709\u7684file<\/p>\n<p>if (null == mfs)<\/p>\n<p>return 0;<\/p>\n<p>int mfsLength = mfs.length &#8211; 1;<\/p>\n<p>int deleteCount = 0;<\/p>\n<p>List&lt;MappedFile&gt; files = new ArrayList&lt;MappedFile&gt;();<\/p>\n<p>if (null != mfs) {<\/p>\n<p>\/\/\u8fdb\u884c\u904d\u5386<\/p>\n<p>for (int i = 0; i &lt; mfsLength; i++) {<\/p>\n<p>MappedFile mappedFile = (MappedFile) mfs[i];<\/p>\n<p>\/\/\u83b7\u53d6\u5230\u6587\u4ef6\u6700\u540e\u4e00\u6b21\u66f4\u65b0\u65f6\u95f4,\u7136\u540e\u52a0\u4e0a\u53ef\u4ee5\u5b58\u6d3b\u65f6\u95f4<\/p>\n<p>long liveMaxTimestamp = mappedFile.getLastModifiedTimestamp() + expiredTime;<\/p>\n<p>\/\/\u5982\u679c\u5f53\u524d\u65f6\u95f4\u5927\u4e8e\u4e86\u4e0a\u9762\u8ba1\u7b97\u7ed3\u679c<\/p>\n<p>if (System.<em>currentTimeMillis<\/em>() &gt;= liveMaxTimestamp || cleanImmediately) {<\/p>\n<p>\/\/\u8fdb\u884c\u5220\u9664<\/p>\n<p>if (mappedFile.destroy(intervalForcibly)) {<\/p>\n<p>files.add(mappedFile);<\/p>\n<p>deleteCount++;<\/p>\n<p>if (files.size() &gt;= <em>DELETE_FILES_BATCH_MAX<\/em>) {<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>if (deleteFilesInterval &gt; 0 &amp;&amp; (i + 1) &lt; mfsLength) {<\/p>\n<p>try {<\/p>\n<p>Thread.<em>sleep<\/em>(deleteFilesInterval);<\/p>\n<p>} catch (InterruptedException e) {<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>} else {<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>} else {<\/p>\n<p>\/\/avoid deleting files in the middle<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6267\u884c\u76f8\u5173\u7684\u5220\u9664\u64cd\u4f5c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RMQ\u7684\u6587\u4ef6\u673a\u5236\u4ea4\u7531Commi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/2960"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2960"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/2960\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2960"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}