summaryrefslogtreecommitdiff
path: root/storage/ndb/tools/old_dirs/ndbnet/lib/NDB/Net/Config.pm
blob: 4c5db3cd3f5d252713322b444ace3e4df16e76f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package NDB::Net::Config;

use strict;
use Carp;
use Symbol;
use Socket;
use Errno;
use XML::Parser;

require NDB::Net::Base;

use vars qw(@ISA);
@ISA = qw(NDB::Net::Base);

# constructors

my $log;

sub initmodule {
    $log = NDB::Util::Log->instance;
}

NDB::Net::Config->attributes(
    file => sub { /^\S+$/ },
    loadtime => sub { /^\d+$/ },
);

sub new {
    my $class = shift;
    @_ % 2 == 0 or confess 0+@_;
    my(%attr) = @_;
    my $netcfg = $class->SUPER::new(%attr);
    $netcfg->setfile($attr{file})
	or $log->put, return undef;
    return $netcfg;
}

sub desc {
    my $netcfg = shift;
    return $netcfg->getfile;
}

use vars qw(@context);

sub handle_start {
    my($parser, $tag, %attr) = @_;
    my $p = $context[-1];
    my $q = {};
    $p->{$tag} ||= [];
    push(@{$p->{$tag}}, $q);
    for my $k (keys %attr) {
	$q->{$k} = $attr{$k};
    }
    push(@context, $q);
    return 1;
}

sub handle_end {
    my($parser, $tag, %attr) = @_;
    pop(@context);
    return 1;
}

sub load {
    my $netcfg = shift;
    my $file = $netcfg->getfile;
    my @s;
    while (1) {
	if (@s = stat($file)) {
	    last;
	}
	$log->put("$file: stat failed: $!");
	if (! $!{ESTALE}) {
	    return undef;
	}
	$log->put("(retry)")->info;
	sleep 1;
    }
    if ($s[9] <= $netcfg->getloadtime(0)) {
	return 1;
    }
    my $fh = gensym();
    if (! open($fh, "<$file")) {
	$log->put("$file: open for read failed: $!");
	return undef;
    }
    my $text = "";
    my $line;
    while (defined($line = <$fh>)) {
	$text .= $line;
    }
    close($fh);
    my $parser = XML::Parser->new(
	ParseParamEnt => 1,
    	Handlers => {
	    Start => \&handle_start,
	    End => \&handle_end,
	},
    );
    delete $netcfg->{config};
    local @context = ($netcfg);
    $parser->parse($text);
    $netcfg->{text} = $text;
    $netcfg->{config} = $netcfg->{config}[0];
    $netcfg->setloadtime(time)
	or $log->push, return undef;
    NDB::Net::Server->deleteall;
    NDB::Net::Database->deleteall;
    NDB::Net::Node->deleteall;
    return 1;
}

sub getservers {
    my $netcfg = shift;
    @_ == 0 or confess 0+@_;
    my $servers = [];
    my $slist = $netcfg->{config}{server} || [];
    for my $s (@$slist) {
	my $server;
	$server = NDB::Net::ServerINET->get($s->{id});
	if (! $server) {
	    $server = NDB::Net::ServerINET->new(%$s);
	    if (! $server) {
		$log->push($netcfg)->warn;
		next;
	    }
	}
	push(@$servers, $server);
    }
    return $servers;
}

sub getdatabases {
    my $netcfg = shift;
    @_ == 0 or confess 0+@_;
    my $databases = [];
    my $dlist = $netcfg->{config}{database} || [];
    for my $d (@$dlist) {
	if ($d->{isproto} eq "y") {
	    next;
	}
	if ($d->{name} !~ /^\w(\w|-)*$/) {
	    $log->put("$d->{name}: invalid db name")->push($netcfg)->warn;
	    next;
	}
	my $db = $netcfg->getdatabase($d->{name});
	if (! $db) {
	    $log->push->warn;
	    next;
	}
	push(@$databases, $db);
    }
    return $databases;
}

sub getdatabase {
    my $netcfg = shift;
    @_ == 1 or confess 0+@_;
    my($name) = @_;
    $netcfg->getservers or return undef;	# cache them
    my $default = $netcfg->{config}{default}[0] || {};
    my $db;
    my $dlist = $netcfg->{config}{database} || [];
    my $nlist;
    for my $d (@$dlist) {
	($d->{name} ne $name) && next;
	if ($d->{isproto} eq "y") {
	    next;
	}
	my %attr = (%$default, %$d);
	$db = NDB::Net::Database->new(%attr);
	if (! $db) {
	    $log->push($netcfg);
	    return undef;
	}
	if ($d->{proto}) {
	    if ($d->{isproto} eq "y") {
		$log->put("$name: prototypes cannot be recursive");
		return undef;
	    }
	    for my $d2 (@$dlist) {
		($d2->{name} ne $d->{proto}) && next;
		if ($d2->{isproto} ne "y") {
		    $log->put("$name: $d2->{name} is not a prototype");
		    return undef;
		}
		if (! $d->{node}) {
		    $d->{node} = $d2->{node};
		}
		last;
	    }
	}
	$nlist = $d->{node} || [];
	last;
    }
    if (! $db) {
	$log->put("$name: no such db")->push($netcfg);
	return undef;
    }
    if (! @$nlist) {
	$log->put("$name: empty node list")->push($netcfg);
	return undef;
    }
    for my $n (@$nlist) {
	my $node;
	try: {
	    my $server = NDB::Net::Server->get($n->{server})
		or last try;
	    my %attr = (%$n, db => $db, server => $server);
	    my $type = $attr{type};
	    if ($type eq 'db') {
		$node = NDB::Net::NodeDb->new(%attr)
		    or last try;
	    }
	    if ($type eq 'mgmt') {
		$node = NDB::Net::NodeMgmt->new(%attr)
		    or last try;
	    }
	    if ($type eq 'api') {
		$node = NDB::Net::NodeApi->new(%attr)
		    or last try;
	    }
	    $log->put("bad node type '$type'");
	}
	if (! $node) {
	    $log->push($netcfg);
	    $db->delete;
	    return undef;
	}
    }
    return $db;
}

1;
# vim:set sw=4: