Remove old state management code
[MogileFS-Server.git] / lib / MogileFS / Domain.pm
blob2130d6ea0abe1905f587dedfb00f399ad24c7cb8
1 package MogileFS::Domain;
2 use strict;
3 use warnings;
4 use MogileFS::Util qw(throw);
6 # --------------------------------------------------------------------------
7 # Class methods:
8 # --------------------------------------------------------------------------
10 my %singleton; # dmid -> MogileFS::Domain
12 my %id2name; # dmid -> domainname(namespace)
13 my %name2id; # domainname(namespace) -> dmid
15 my $last_load = 0;
17 # return singleton MogileFS::Domain, given a dmid
18 sub of_dmid {
19 my ($pkg, $dmid) = @_;
20 return undef unless $dmid;
21 return $singleton{$dmid} if $singleton{$dmid};
23 my $ns = $pkg->name_of_id($dmid)
24 or return undef;
26 return $singleton{$dmid} = bless {
27 dmid => $dmid,
28 ns => $ns,
29 }, $pkg;
32 # return singleton MogileFS::Domain, given a domain(namespace)
33 sub of_namespace {
34 my ($pkg, $ns) = @_;
35 return undef unless $ns;
36 my $dmid = $pkg->id_of_name($ns)
37 or return undef;
38 return MogileFS::Domain->of_dmid($dmid);
41 # name to dmid, reloading if not in cache
42 sub id_of_name {
43 my ($pkg, $domain) = @_;
44 return $name2id{$domain} if $name2id{$domain};
45 $pkg->reload_domains;
46 return $name2id{$domain};
49 # dmid to name, reloading if not in cache
50 sub name_of_id {
51 my ($pkg, $dmid) = @_;
52 return $id2name{$dmid} if $id2name{$dmid};
53 $pkg->reload_domains;
54 return $id2name{$dmid};
57 # force reload of cache
58 sub reload_domains {
59 my $now = time();
60 my $sto = Mgd::get_store();
61 %name2id = $sto->get_all_domains;
62 %id2name = ();
63 while (my ($k, $v) = each %name2id) {
64 $id2name{$v} = $k;
67 # Blow singleton cache on reload. Otherwise a *change* in data may not be
68 # reflected.
69 %singleton = ();
71 $last_load = $now;
74 # FIXME: should probably have an invalidate_cache variant that only
75 # flushes locally (for things like "get_domains" or "get_hosts", where
76 # it needs to be locally correct for the semantics of the command, but
77 # no need to propagate a cache invalidation to our peers)
78 sub invalidate_cache {
79 $last_load = 0;
80 %id2name = ();
81 %name2id = ();
82 if (my $worker = MogileFS::ProcManager->is_child) {
83 $worker->invalidate_meta("domain");
87 sub check_cache {
88 my $pkg = shift;
89 my $now = time();
90 return if $last_load > $now - 5;
91 MogileFS::Domain->reload_domains;
94 sub domains {
95 my $pkg = shift;
96 $pkg->check_cache;
97 return map { $pkg->of_dmid($_) } keys %id2name;
100 # create a new domain given a name, returns MogileFS::Domain object on success.
101 # throws errors on failure. error codes include:
102 # "dup" -- on duplicate name
103 sub create {
104 my ($pkg, $name) = @_;
106 # throws 'dup':
107 my $dmid = Mgd::get_store()->create_domain($name)
108 or die "create domain didn't return a dmid";
110 # return the domain id we created
111 MogileFS::Domain->invalidate_cache;
112 return MogileFS::Domain->of_dmid($dmid);
115 # --------------------------------------------------------------------------
116 # Instance methods:
117 # --------------------------------------------------------------------------
119 sub id { $_[0]->{dmid} }
120 sub name { $_[0]->{ns} }
122 sub has_files {
123 my $self = shift;
124 return 1 if $Mgd::_T_DOM_HAS_FILES;
125 return Mgd::get_store()->domain_has_files($self->id);
128 sub classes {
129 my $dom = shift;
130 # return a bunch of class objects for this domain
131 return MogileFS::Class->classes_of_domain($dom);
134 # returns true if deleted. throws exceptions on errors. exception codes:
135 # 'has_files' if it has files.
136 sub delete {
137 my $self = shift;
138 throw("has_files") if $self->has_files;
139 # TODO: delete its classes
140 my $rv = Mgd::get_store()->delete_domain($self->id);
141 MogileFS::Domain->invalidate_cache;
142 return $rv;
145 # returns named class of domain
146 sub class {
147 my ($dom, $clname) = @_;
148 foreach my $cl (MogileFS::Class->classes_of_domain($dom)) {
149 return $cl if $cl->name eq $clname;
151 return;
154 sub create_class {
155 my ($dom, $clname) = @_;
156 return MogileFS::Class->create_class($dom, $clname);