Armadilloフォーラム

DNSによる名前解決のタイムアウト時間の件

yuto.tsukada

2022年8月5日 13時35分

お世話になっております。

標記の件、質問がございます。
pythonのrequestsモジュールを使用し、名前解決できない状態で、以下コマンドを実行すると、エラー応答までに40秒程度かかります。
名前解決のタイムアウトは20秒という認識ですが、40秒かかるということは、他にOSで処理を行っており、時間がかかっているという認識でよろしいでしょうか。
(名前解決できない状態:/etc/resolv.confに、存在しないDNSサーバーのIPアドレスを2つ記述。)

<コマンド>

requests.get("http://www/xxx/yyy")

<エラー応答>

HTTPConnectionPool(host='www', port=80): Max retries exceeded with url: /xxx/yyy (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x763cd890>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))

/etc/resolv.confでは、以下の設定を行っております。
 ・nameserver:2つ
 ・timeout、attempts:それぞれデフォルト(5秒、2回)

ご回答の程、よろしくお願いいたします。

コメント

at_dominique.m…

2022年8月5日 14時51分

yuto.tsukadaさん、

> pythonのrequestsモジュールを使用し、名前解決できない状態で、以下コマンドを実行すると、エラー応答までに40秒程度かかります。
> 名前解決のタイムアウトは20秒という認識ですが、40秒かかるということは、他にOSで処理を行っており、時間がかかっているという認識でよろしいでしょうか。
> (名前解決できない状態:/etc/resolv.confに、存在しないDNSサーバーのIPアドレスを2つ記述。)
>
> /etc/resolv.confでは、以下の設定を行っております。
>  ・nameserver:2つ
>  ・timeout、attempts:それぞれデフォルト(5秒、2回)

pythonと関係なく、 glibc の名前解決の処理ですね。
「time getent ahostsv4 somename」で再現できました。

timeout, attempts の値の他には search の値によって、また試すことになります。
tcpdump か strace で UDP の中身を拝見すれば明確になりますが、名前が解決できなかった場合に search の値を追加してもう一度解決してみています。

スピードのために timeout を 1秒 にしましたが、5秒でも同じです:

# cat /etc/resolv.conf 
search atmark.tech
options timeout:1
nameserver 192.168.34.1
nameserver 192.168.34.2
 
# time getent ahostsv4 atmark-techno.com
 
real	0m8.010s
user	0m0.000s
sys	0m0.003s
 
(別のターミナル)
# tcpdump -nn -i any port 53
tcpdump: data link type LINUX_SLL2
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
14:48:38.371902 atmark0 Out IP 172.16.69.20.33656 > 192.168.34.1.53: 25734+ A? atmark-techno.com. (35)
14:48:39.372987 atmark0 Out IP 172.16.69.20.40534 > 192.168.34.2.53: 25734+ A? atmark-techno.com. (35)
14:48:40.374025 atmark0 Out IP 172.16.69.20.33656 > 192.168.34.1.53: 25734+ A? atmark-techno.com. (35)
14:48:41.375072 atmark0 Out IP 172.16.69.20.40534 > 192.168.34.2.53: 25734+ A? atmark-techno.com. (35)
14:48:42.376272 atmark0 Out IP 172.16.69.20.58736 > 192.168.34.1.53: 27295+ A? atmark-techno.com.mydomain. (44)
14:48:43.377375 atmark0 Out IP 172.16.69.20.38648 > 192.168.34.2.53: 27295+ A? atmark-techno.com.mydomain. (44)
14:48:44.378414 atmark0 Out IP 172.16.69.20.58736 > 192.168.34.1.53: 27295+ A? atmark-techno.com.mydomain. (44)
14:48:45.379199 atmark0 Out IP 172.16.69.20.38648 > 192.168.34.2.53: 27295+ A? atmark-techno.com.mydomain. (44)

search を削除したら、ちゃんと4秒で終わります。

よろしくお願いします。

yuto.tsukada

2022年8月8日 20時27分

お世話になっております。

早速のご回答ありがとうございます。
glibcの影響の旨、承知いたしました。

ただ、/etc/resolv.conf(下記参照)では、searchは指定しておりません。

# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.0.0
nameserver 192.168.0.1

また、上記/etc/resolv.confへ「search atmark.tech」の記述有無で時間の変化を確認したところ、変化なしでした。(下記参照)

# cat /etc/resolv.conf
# Generated by NetworkManager
# search atmark.tech
nameserver 192.168.0.0
nameserver 192.168.0.1
 
# time getent ahostsv4 atmark-techno.com
real    0m40.046s
user    0m0.010s
sys     0m0.000s
 
# cat /etc/resolv.conf
# Generated by NetworkManager
search atmark.tech
nameserver 192.168.0.0
nameserver 192.168.0.1
 
# time getent ahostsv4 atmark-techno.com
real    0m40.047s
user    0m0.000s
sys     0m0.000s

searchの影響ではないとの認識ですが、いかがでしょうか。
ご回答の程、よろしくお願いいたします。

at_mizo

2022年8月9日 8時58分

/etc/resolv.conf次の行を追加してみてください。

options attempts:1

デフォルトでは次のようになっており、

options timeout:5
options attempts:2

3回の問い合わせを2回繰り返します。1,2回目の問い合わせは5秒、3回目は2倍の10秒になるので、計20秒、これを2回繰り返すので、40秒になるようです。

yuto.tsukada

2022年8月9日 20時15分

お世話になっております。

ご回答ありがとうございます。

下記/etc/resolv.confにて、ご教授いただいた内容を確認いたしました。

# Generated by NetworkManager
options timeout:4
options attempts:1
nameserver 192.168.0.0
nameserver 192.168.0.1
 
# time getent ahostsv4 atmark-techno.com
 
real    0m16.021s
user    0m0.000s
sys     0m0.000s

1,2回目の問い合わせは4秒、3回目は2倍の8秒で、計16秒をということですね。

また申し訳ございませんが、もう一つ質問がございます。
<質問>
以下、2つのコマンドは、どちらも名前解決を行っているという認識ですが、処理終了時間が異なるのはなぜでしょうか。
time routeは、2回の問い合わせのみということでしょうか。(3回目は未実施?)

# time route
real    0m8.017s
user    0m0.000s
sys     0m0.000s
 
# time getent ahostsv4 atmark-techno.com
real    0m16.021s
user    0m0.000s
sys     0m0.000s

ご回答の程、よろしくお願いいたします。

at_mizo

2022年8月10日 9時26分

溝渕です。

# strace route
# strace getent ahostsv4 atmark-techno.com

のようにコマンドを実行すると、何秒を何回待ったのかが確認できると思います。

> 1,2回目の問い合わせは4秒、3回目は2倍の8秒で、計16秒をということですね。

nameserverの指定が2つの場合は、上記ご理解で良いかと思います。

yuto.tsukada

2022年8月10日 17時44分

> 溝渕です。
>
>

> # strace route
> # strace getent ahostsv4 atmark-techno.com
> 

>
> のようにコマンドを実行すると、何秒を何回待ったのかが確認できると思います。
>
> > 1,2回目の問い合わせは4秒、3回目は2倍の8秒で、計16秒をということですね。
>
> nameserverの指定が2つの場合は、上記ご理解で良いかと思います。
>

溝渕様

お世話になっております。

上記「strace」コマンドを用いたところ、疑問を解決することができました。

懇切丁寧なご説明ありがとうございました。
今後とも、よろしくお願いいたします。

at_dominique.m…

2022年8月15日 10時27分

先週返事できず遅くなりました。

> searchの影響ではないとの認識ですが、いかがでしょうか。

おそらくですが、「hostname」に full qualified domain を設定したら同じく search domain として使われてます。search domain を /etc/resolv.conf に入れて、代わりにresolv.conf の domain が使われますのでこれで影響を確認できなかったのではないかと思います。
(「hostname armadillo」と search 無しでやっていましたので、短い時間で解決できていましたが、「hostname armadillo.test.domain」では長いままになります)

私の tcpdump か strace で確認したら、(attempts:2 + dns サーバー二つの)最初の四回が getent ahostsv4 の引数で解決しようとしていますが、残り四回は search domain を入れた物で出ます:
- hostname armadillo.domain2.test + search domain1.test + getent ahostsv4 test.com -> test.com, test.com.domain1.test
- hostname armadillo.domain2.test + search 無し + getent ahostsv4 test.com -> test.com, test.com.domain2.test
- hostname armadillo + search domain1.test + getent ahostsv4 test.com -> test.com, test.com.domain1.test
- hostname armadillo + search 無し + getent ahostsv4 test.com -> test.comのみ

先週の例では atmark.tech を tcpdump のログに下手に消して分かりにくくなってすみませんでした。

 

ちなみに、route の場合は DNS 解決でもありますが、その場合に PTR (reverse) レコードを取得しようとして、search domain に影響ありません。そのため、設定通りにattempts * サーバー数だけトライします。

また、関係ないと思いますが、/etc/nsswitch.conf に 「hosts: dns dns」などのように dns を二回記載されたら、また場合になります。名前解決に色々ありますね…

よろしくお願いします。