Created
December 9, 2012 08:04
-
-
Save tamtam180/4243849 to your computer and use it in GitHub Desktop.
Hadoop rack_awarenessのperlスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use warnings; | |
use strict; | |
use Socket; | |
our $TOPOLOGY_FILE = "/etc/hadoop/conf/rack.txt"; | |
our $DEFAULT_RACK = "/default-rack"; | |
my %topologies; | |
if (open(FP, "<${TOPOLOGY_FILE}")) { | |
while (<FP>) { | |
unless (/^#/) { | |
chomp; | |
my ($host, $rack) = split; | |
$topologies{$host} = $rack || $DEFAULT_RACK if ($host); | |
} | |
} | |
close(FP); | |
} | |
sub search { | |
my ($host) = @_; | |
my $rack = $topologies{$host}; | |
if (!defined($rack) && ($host =~ /^[0-9]/)) { | |
my @adrs = split('\.', $host); | |
if ($#adrs == 3) { | |
$host = gethostbyaddr(pack("C4", @adrs), 2); | |
$rack = $topologies{$host}; | |
} | |
} | |
return $rack || $DEFAULT_RACK; | |
} | |
my @racks; | |
foreach (@ARGV) { | |
push(@racks, &search($_)); | |
} | |
print join(' ', @racks); | |
確かHadoop側でホスト名はIPアドレスに変換して渡しているので、このスクリプトにはIPアドレスしか来ない。
ただ、設定ファイルはホスト名で書きたい事もあるので、rack.txtに無い場合は、IPアドレスの場合に限りホスト名を引いて再建策をするようにした。
これでrack.txtにホスト名を書けるようになった。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://wiki.apache.org/hadoop/topology_rack_awareness_scripts
このシェルスクリプトだと、引数の数だけファイルをReadしていることになるので嫌だなぁーと。
というわけでPERLで書き直し。
CentOS 6.xなら最小構成のインストールでもPerl入っているし。