mperham/phat

Can not resolve localhost

Closed this issue · 16 comments

Hi!

I'm trying to run phat. I start memcached, but in the log file I see the following error:

MemCacheError (No connection to server (localhost:11211 DEAD (SocketError: Hostname not known: localhost), will retry at 2010-04-05 14:09:22 +0400)): No connection to server (localhost:11211 DEAD (SocketError: Hostname not known: localhost), will retry at 2010-04-05 14:09:22 +0400)

Without em-resolv-replace it works fine.

Ivan.

Works fine for me. Can you give me more detail? OS? Contents of /etc/hosts?

Mac OS X 10.6.3, /etc/hosts:

127.0.0.1 localhost uvsoft.localhost uvsoft
255.255.255.255 broadcasthost
::1 localhost fe80::1%lo0 localhost

even if I specify memcached IP (my local IP address like 192.168.x.x) in the config, I get the same error, but s/localhost/192.168.x.x

with config.cache_store = :mem_cache_store, '127.0.0.1:11211', the same:

MemCacheError (No connection to server (127.0.0.1:11211 DEAD (SocketError: Hostname not known: 127.0.0.1), will retry at 2010-04-05 17:19:27 +0400)): No connection to server (127.0.0.1:11211 DEAD (SocketError: Hostname not known: 127.0.0.1), will retry at 2010-04-05 17:19:27 +0400)

It looks like it is trying to resolve IP address of IP address)

Mb it is because of ruby? You suggest 1.9.1, but mine version is 1.9.2....

ruby:
  interpreter:  "ruby"
  version:      "1.9.2dev"
  date:         "2010-04-05"
  platform:     "x86_64-darwin10.3.0"
  patchlevel:   "2010-04-05 trunk 27222"
  full_version: "ruby 1.9.2dev (2010-04-05 trunk 27222) [x86_64-darwin10.3.0]"

Does this work for you?

~> rvm 1.9.1
~> irb
>> require 'em-resolv-replace'
=> true
>> Resolv.getaddress('localhost')
=> "::1"

Note it's returning an IPv6 address. Make sure that memcached is listening on the IPv6 interface:

~> netstat -a | grep 11211
tcp4       0      0  *.11211                *.*                    LISTEN
tcp6       0      0  *.11211                *.*                    LISTEN
soft@iUVSoft ~/work/sites/phat $ rvm ruby-head
soft@iUVSoft ~/work/sites/phat $ irb
ruby-head > require 'em-resolv-replace'
 => true 
ruby-head > Resolv.getaddress('ya.ru')
 => "77.88.21.8" 
ruby-head > Resolv.getaddress('localhost')
 => "fe80::1%lo0" 
ruby-head > 
soft@iUVSoft ~/work/sites/phat $ netstat -a | grep 11211
tcp4       0      0  *.11211                *.*                    LISTEN
tcp6       0      0  *.11211                *.*                    LISTEN
udp4       0      0  *.11211                *.*                    
udp6       0      0  *.11211                *.*    

Sorry, I wasn't starting the reactor and so em-resolv-replace wasn't using its own code for the resolution. Try this in irb:

require 'em-resolv-replace'
EM.run {
  Fiber.new do
    p Resolv.getaddresses('localhost')
    p Resolv.getaddress('localhost')
    EM.stop
  end.resume
}

I get:

["127.0.0.1"]
"127.0.0.1"
soft@iUVSoft ~/work/sites/phat $ irb
ruby-1.9.2-p-1 > require 'em-resolv-replace'
 => true 
ruby-1.9.2-p-1 > EM.run {
ruby-1.9.2-p-1 >       Fiber.new do
ruby-1.9.2-p-1 >           p Resolv.getaddresses('localhost')
ruby-1.9.2-p-1 ?>        p Resolv.getaddress('localhost')
ruby-1.9.2-p-1 ?>        EM.stop
ruby-1.9.2-p-1 ?>      end.resume
ruby-1.9.2-p-1 ?>  }
Resolv::ResolvError: ["rcode=3"]
    from /Users/soft/.rvm/gems/ruby-head/gems/em-resolv-replace-1.0.0/lib/em-resolv-replace.rb:37:in `em_getaddress'
    from /Users/soft/.rvm/gems/ruby-head/gems/em-resolv-replace-1.0.0/lib/em-resolv-replace.rb:21:in `getaddresses'
    from /Users/soft/.rvm/rubies/ruby-head/lib/ruby/1.9.1/resolv.rb:50:in `getaddresses'
    from (irb):4:in `block (2 levels) in irb_binding'
ruby-1.9.2-p-1 > 

Here is an example with remote host:

ruby-1.9.2-p-1 > require 'em-resolv-replace'
 => false 
ruby-1.9.2-p-1 > EM.run {
ruby-1.9.2-p-1 >       Fiber.new do
ruby-1.9.2-p-1 >           p Resolv.getaddresses('ya.ru')
ruby-1.9.2-p-1 ?>        p Resolv.getaddress('ya.ru')
ruby-1.9.2-p-1 ?>        EM.stop
ruby-1.9.2-p-1 ?>      end.resume
ruby-1.9.2-p-1 ?>  }
["93.158.134.8", "213.180.204.8", "77.88.21.8"]
"77.88.21.8"
 => nil 

I suspect the issue is that em-resolv-replace may not support /etc/hosts. It is depending on your DNS server to return 127.0.0.1 for localhost. Yours is instead returning an error code. Do you get the same error if you use an IP address?

Just pushed support for /etc/hosts. Let me know if this fixes your issue.

So, if I leave this line in environment.rb unchanged "config.cache_store = :mem_cache_store", now everything works fine, it can connect to memcached. But the strage thing is that when I specify the IP address like "config.cache_store = :mem_cache_store, '127.0.0.1:11211'", it is trying to resolve IP address of '127.0.0.1' like it is a doman name (not already an IP address)... and eventually can do this, so in this case connection to memcached fails.

According to the code in em-dns-resolver.rb, it does not try to figure out whether @hostname already contains an IP address or not... It always fires request to the DNS server, doesn't it?

Exactly. I'm accepting patches if you have some time. :-)