[20230317]關於TIME_WAIT問題.txt

lfree發表於2023-03-23

[20230317]關於TIME_WAIT問題.txt

--//被別人問起這個問題,伺服器的網路連結出現大量TIME_WAIT連線。實際上達到怎麼樣量才是最嚴重也沒有標準。
--//實際上如果不影響生產系統應用,可以完全不用理會。自己看了以前一些筆記,做一些總結,不知道對不對。

--//網際網路上能找到許多圖,感覺很複雜。實際上主要記住一點僅僅主動方(也就是發起方主動退出才會在發起方出現TIME_WAIT),比如:
--//對於oracle的單機資料庫,正常情況下比如sqlplus透過網路連線執行sql語句正常退出,發起方在客戶端,伺服器不會出現TIME_WAIT
--//問題,TIME_WAIT僅僅出現在客戶端。

--//在rac環境下如果應用程式使用scan ip連線,確實會在服務端出現大量的TIME_WAIT連結、因為最終的連線是vip地址。
--//所以rac環境中介軟體伺服器儘量不要使用scan ip,直接可以使用vip配置,來減少中間環節,避免服務端出現大量TIME_WAIT的情況。
--//另外tnsping可以導致服務端出現TIME_WAIT。因為tnsping發起斷開連線在服務端。
--//順便驗證自己的測試:

1.測試配置情況:
--//192.168.100.33 客戶端
--//192.168.100.78 伺服器

2.測試1:
--//192.168.100.33:
$  time sqlplus -s -l scott/book@192.168.100.78:1521/book <<<'select sysdate from dual;'
SCOTT@192.168.100.78:1521/book>
SYSDATE
-------------------
2023-03-21 08:46:04
real    0m0.072s
user    0m0.024s
sys     0m0.009s

$ seq 100 | xargs -IQ bash -c "netstat -tonp 2>/dev/null| grep 192.168.100.78 | ts.awk;sleep 1"
                      Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name    Timer
[2023-03-21 08:46:04] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (59.93/0/0)
[2023-03-21 08:46:05] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (58.87/0/0)
[2023-03-21 08:46:06] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (57.81/0/0)
[2023-03-21 08:46:07] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (56.75/0/0)
...
[2023-03-21 08:46:59] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (4.84/0/0)
[2023-03-21 08:47:00] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (3.78/0/0)
[2023-03-21 08:47:01] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (2.72/0/0)
[2023-03-21 08:47:02] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (1.66/0/0)
[2023-03-21 08:47:03] tcp        0      0 192.168.100.33:47790        192.168.100.78:1521         TIME_WAIT   -                   timewait (0.60/0/0)
--//題頭我加上的.注意看前面的時間,基本可以對上一般timewait=60秒.
--//你可以發現time_wait發生在客戶端.

--//192.168.100.78:
$ seq 100 | xargs -IQ bash -c "netstat -tonp 2>/dev/null| grep 192.168.100.33 | ts.awk;sleep 1"
--//沒有輸出或者僅僅輸出1行(看運氣).

3.測試2:
--//先做一個簡單測試:
--//192.168.100.33:
$ seq 1 | xargs -IQ -P20 tnsping 192.168.100.78 1
TNS Ping Utility for Linux: Version 10.2.0.4.0 - Production on 21-MAR-2023 09:04:07
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copyright (c) 1997,  2007, Oracle.  All rights reserved.
Used parameter files:
/u01/app/oracle/product/10.2.0/db_1/network/admin/sqlnet.ora
Used HOSTNAME adapter to resolve the alias
Attempting to contact (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=192.168.100.78))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.100.78)(PORT=1521)))
OK (0 msec)

--//192.168.100.33.開啟另外視窗執行:
$ seq 70 | xargs -IQ bash -c "netstat -tonp 2>/dev/null| grep 192.168.100.78 | ts.awk;sleep 1"
--//沒有任何輸出.

--//192.168.100.78:
$ seq 70 | xargs -IQ bash -c "netstat -tonp 2>/dev/null| grep 192.168.100.33 | ts.awk;sleep 1"
                      Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name    Timer
[2023-03-21 09:04:07] tcp        0      0 192.168.100.78:1521         192.168.100.33:47918        TIME_WAIT   -                   timewait (59.87/0/0)
~~~~~~~~~~~~~~~~~~~~~
[2023-03-21 09:04:08] tcp        0      0 192.168.100.78:1521         192.168.100.33:47918        TIME_WAIT   -                   timewait (58.85/0/0)
[2023-03-21 09:04:09] tcp        0      0 192.168.100.78:1521         192.168.100.33:47918        TIME_WAIT   -                   timewait (57.83/0/0)
...
[2023-03-21 09:05:04] tcp        0      0 192.168.100.78:1521         192.168.100.33:47918        TIME_WAIT   -                   timewait (3.78/0/0)
[2023-03-21 09:05:05] tcp        0      0 192.168.100.78:1521         192.168.100.33:47918        TIME_WAIT   -                   timewait (2.76/0/0)
[2023-03-21 09:05:06] tcp        0      0 192.168.100.78:1521         192.168.100.33:47918        TIME_WAIT   -                   timewait (1.74/0/0)
[2023-03-21 09:05:07] tcp        0      0 192.168.100.78:1521         192.168.100.33:47918        TIME_WAIT   -                   timewait (0.72/0/0)
--//題頭我加上的,注意看時間,基本能對上.
--//你可以發現time_wait發生在服務端.

4.測試3:
--//192.168.100.33:
$ seq 10000 | xargs -IQ -P20 tnsping 192.168.100.78 100 > /dev/null
--//相當於瞬間執行1000000次tnsping 。

--//192.168.100.78:
$ netstat -tonp 2>/dev/null | grep 192.168.100.33 | wc -l
61919

--//192.168.100.33:
$ time sqlplus -s -l scott/book@192.168.100.78:1521/book <<<'select sysdate from dual;'
SYSDATE
-------------------
2023-03-21 09:43:51
real    0m0.077s
user    0m0.023s
sys     0m0.011s
--//執行並不慢.另另外一個方面也說明出現大量time_wait問題不會太大.

--//192.168.100.33端也出現少量time_wait,我估計埠占用問題或者從192.168.100.33發出太多的tnsping,根本發不出去。
$ netstat -tonp| grep 192.168.100.78|head -10
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 192.168.100.33:40585        192.168.100.78:1521         TIME_WAIT   -                   timewait (40.32/0/0)
tcp        0      0 192.168.100.33:40615        192.168.100.78:1521         TIME_WAIT   -                   timewait (40.34/0/0)
tcp        0      0 192.168.100.33:40645        192.168.100.78:1521         TIME_WAIT   -                   timewait (40.36/0/0)
tcp        0      0 192.168.100.33:40657        192.168.100.78:1521         TIME_WAIT   -                   timewait (40.37/0/0)
tcp        0      0 192.168.100.33:40682        192.168.100.78:1521         TIME_WAIT   -                   timewait (40.38/0/0)
tcp        0      0 192.168.100.33:40932        192.168.100.78:1521         TIME_WAIT   -                   timewait (40.55/0/0)
tcp        0      0 192.168.100.33:40033        192.168.100.78:1521         TIME_WAIT   -                   timewait (39.94/0/0)
tcp        0      0 192.168.100.33:40194        192.168.100.78:1521         TIME_WAIT   -                   timewait (40.05/0/0)
tcp        0      0 192.168.100.33:39479        192.168.100.78:1521         TIME_WAIT   -                   timewait (39.56/0/0)
tcp        0      0 192.168.100.33:39048        192.168.100.78:1521         TIME_WAIT   -                   timewait (39.27/0/0)

$ netstat -tonp| grep 192.168.100.78|wc
    249    2241   32094

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2941142/,如需轉載,請註明出處,否則將追究法律責任。

相關文章