~ubuntu-branches/ubuntu/vivid/libmongodb-perl/vivid

« back to all changes in this revision

Viewing changes to devel/lib/MongoDBTest/Orchestrator.pm

  • Committer: Package Import Robot
  • Author(s): gregor herrmann
  • Date: 2014-07-12 20:41:25 UTC
  • mfrom: (2.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140712204125-m1cox1ikaj5d86q1
Tags: 0.704.2.0-1
* New upstream release.
* Drop build dependencies which are not needed anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
#  Copyright 2009-2013 MongoDB, Inc.
3
 
#
4
 
#  Licensed under the Apache License, Version 2.0 (the "License");
5
 
#  you may not use this file except in compliance with the License.
6
 
#  You may obtain a copy of the License at
7
 
#
8
 
#  http://www.apache.org/licenses/LICENSE-2.0
9
 
#
10
 
#  Unless required by applicable law or agreed to in writing, software
11
 
#  distributed under the License is distributed on an "AS IS" BASIS,
12
 
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
#  See the License for the specific language governing permissions and
14
 
#  limitations under the License.
15
 
 
16
 
use 5.010;
17
 
use strict;
18
 
use warnings;
19
 
 
20
 
package MongoDBTest::Orchestrator;
21
 
 
22
 
use lib 'devel/lib';
23
 
 
24
 
use MongoDBTest::Server;
25
 
 
26
 
use MongoDB;
27
 
use YAML::XS;
28
 
 
29
 
use Moo;
30
 
use Types::Standard qw/Str HashRef/;
31
 
use Types::Path::Tiny qw/AbsFile/;
32
 
use namespace::clean;
33
 
 
34
 
with 'MooseX::Role::Logger';
35
 
 
36
 
# Required
37
 
 
38
 
has config_file => (
39
 
    is => 'ro',
40
 
    isa => AbsFile,
41
 
    coerce => AbsFile->coercion,
42
 
    required => 1,
43
 
);
44
 
 
45
 
# Lazy or default
46
 
 
47
 
has config => (
48
 
    is => 'lazy',
49
 
    isa => HashRef,
50
 
);
51
 
 
52
 
sub _build_config {
53
 
    my ($self) = @_;
54
 
    my ($config) = YAML::XS::LoadFile($self->config_file);
55
 
    return $config;
56
 
}
57
 
 
58
 
has cluster_type => (
59
 
    is => 'lazy',
60
 
    isa => Str,
61
 
);
62
 
 
63
 
sub _build_cluster_type {
64
 
    my ($self) = @_;
65
 
    return $self->config->{type};
66
 
}
67
 
 
68
 
has rs_name => (
69
 
    is => 'lazy',
70
 
    isa => Str,
71
 
);
72
 
 
73
 
sub _build_rs_name {
74
 
    my ($self) = @_;
75
 
    return $self->config->{setName} // 'rs0';
76
 
}
77
 
 
78
 
# Private
79
 
 
80
 
has _mongod_set => (
81
 
    is => 'ro',
82
 
    isa => HashRef,
83
 
    default => sub { {} },
84
 
);
85
 
 
86
 
# methods
87
 
 
88
 
sub BUILD {
89
 
    my ($self) = @_;
90
 
 
91
 
    for my $server ( @{ $self->config->{mongod} } ) {
92
 
        $self->_mongod_set->{$server->{name}} = MongoDBTest::Server->new(
93
 
            config => $server,
94
 
            default_args => $self->default_args,
95
 
        );
96
 
    }
97
 
 
98
 
    return;
99
 
}
100
 
 
101
 
sub list_mongod_set { 
102
 
    my ($self) = @_;
103
 
    return values %{ $self->_mongod_set }
104
 
}
105
 
 
106
 
sub start {
107
 
    my ($self) = @_;
108
 
    for my $server ( $self->list_mongod_set ) {
109
 
        $self->_logger->info("starting $server");
110
 
        $server->start;
111
 
        $self->_logger->info("$server is up on port " . $server->port);
112
 
    }
113
 
 
114
 
    $self->rs_initiate if $self->is_replicaset;
115
 
 
116
 
}
117
 
 
118
 
sub stop {
119
 
    my ($self) = @_;
120
 
    for my $server ( $self->list_mongod_set ) {
121
 
        next unless $server->is_alive;
122
 
        $self->_logger->info("stopping $server");
123
 
        $server->stop;
124
 
    }
125
 
}
126
 
 
127
 
sub as_uri {
128
 
    my ($self) = @_;
129
 
    my $uri = "mongodb://" . join(",", map { $_->hostname . ":" . $_->port } $self->list_mongod_set);
130
 
    return $uri;
131
 
}
132
 
 
133
 
sub default_args {
134
 
    my ($self) = @_;
135
 
    my $default = $self->is_replicaset ? "--replSet " . $self->rs_name . " " : "";
136
 
    $default .= $self->config->{default_args} if exists $self->config->{default_args};
137
 
    return $default;
138
 
}
139
 
 
140
 
sub is_replicaset {
141
 
    my ($self) = @_;
142
 
    return $self->cluster_type eq 'replicaset';
143
 
}
144
 
 
145
 
sub rs_initiate {
146
 
    my ($self) = @_;
147
 
    my ($first) = $self->list_mongod_set;
148
 
 
149
 
    my $members = [
150
 
        sort map {; { host => $_->as_host_port } } $self->list_mongod_set
151
 
    ];
152
 
 
153
 
    for my $i (0 .. $#$members) {
154
 
        $members->[$i]{_id} = $i;
155
 
    }
156
 
 
157
 
    my $rs_config = {
158
 
        _id => $self->rs_name,
159
 
        members => $members,
160
 
    };
161
 
 
162
 
    my $client = MongoDB::MongoClient->new( host => $first->as_uri );
163
 
    $client->get_database("admin")->run_command({replSetInitiate => $rs_config});
164
 
 
165
 
    $self->_logger->debug("waiting for master");
166
 
    my $c = 1;
167
 
    until ( eval { MongoDB::MongoClient->new( host => $self->as_uri, find_master => 1 ) } ) {
168
 
        sleep 1;
169
 
        $self->_logger->debug("waiting for master")
170
 
            if $c++ % 5 == 0
171
 
    }
172
 
 
173
 
    return;
174
 
}
175
 
 
176
 
sub DEMOLISH {
177
 
    my ($self) = @_;
178
 
    $self->stop;
179
 
}
180
 
 
181
 
1;