forked from mnunberg/perl-Couchbase-Client
/
MockServer.pm
184 lines (158 loc) · 4.44 KB
/
MockServer.pm
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
package Couchbase::MockServer;
use strict;
use warnings;
use IO::Socket::INET;
use Socket;
use POSIX qw(:errno_h :signal_h :sys_wait_h);
use Log::Fu { level => "warn" };
use Data::Dumper;
use Time::HiRes qw(sleep);
my $SYMLINK = "CouchbaseMock_PLTEST.jar";
our $INSTANCE;
use Class::XSAccessor {
constructor => '_real_new',
accessors => [qw(
pid
jarfile
nodes
buckets
vbuckets
harakiri_socket
port
)]
};
# This is the couchbase mock server, it will attempt to download, spawn, and
# otherwise control the java-based CouchbaseMock server.
sub _accept_harakiri {
my $self = shift;
$self->harakiri_socket->blocking(0);
my $begin_time = time();
my $max_wait = 5;
my $got_accept = 0;
while(time - $begin_time < $max_wait) {
my $sock = $self->harakiri_socket->accept();
if($sock) {
$sock->blocking(1);
$self->harakiri_socket($sock);
$got_accept = 1;
log_info("Got harakiri connection");
my $buf = "";
$self->harakiri_socket->recv($buf, 100, 0);
if(defined $buf) {
my ($port) = ($buf =~ /(\d+)/);
$self->port($port);
} else {
die("Couldn't get port");
}
last;
} else {
sleep(0.1);
}
}
if(!$got_accept) {
die("Could not establish harakiri control connection");
}
}
sub _do_run {
my $self = shift;
my @command;
push @command, "java", "-jar", $self->jarfile;
my $buckets_arg = "--buckets=";
foreach my $bucket (@{$self->buckets}) {
my ($name,$password,$type) = @{$bucket}{qw(name password type)};
$name ||= "";
$password ||= "";
$type ||= "";
if($type && $type ne "couchbase" && $type ne "memcache") {
die("type for bucket must be either 'couchbase' or 'memcache'");
}
my $spec = join(":", $name, $password, $type);
$buckets_arg .= $spec . ",";
}
$buckets_arg =~ s/,$//g;
push @command, $buckets_arg;
push @command, "--port=0";
if($self->nodes) {
push @command, "--nodes=" . $self->nodes;
}
my $sock = IO::Socket::INET->new(Listen => 5);
$self->harakiri_socket($sock);
my $port = $self->harakiri_socket->sockport;
log_infof("Listening on %d for harakiri", $port);
push @command, "--harakiri-monitor=localhost:$port";
my $pid = fork();
if($pid) {
#Parent: setup harakiri monitoring socket
sleep(0.05);
if(waitpid($pid, WNOHANG) > 0) {
die("Child process died prematurely");
}
log_info("Launched CouchbaseMock PID=$pid");
#$self->pid(getpgrp($pid));
$self->pid($pid);
$self->_accept_harakiri();
} else {
setpgrp(0, 0);
log_warnf("Executing %s", join(" ", @command));
exec(@command);
warn"exec @command failed: $!";
exit(1);
}
}
sub new {
my ($cls,%opts) = @_;
if($INSTANCE) {
log_warn("Returning cached instance");
return $INSTANCE;
}
unless(exists $opts{jarfile}) {
die("Must have path to JAR");
}
my $o = $cls->_real_new(%opts);
my $file = $o->jarfile;
if(!-e $file) {
die("Cannot find $file");
}
$o->_do_run();
$INSTANCE = $o;
return $o;
}
sub GetInstance {
my $cls = shift;
return $INSTANCE;
}
sub suspend_process {
my $self = shift;
my $pid = $self->pid;
return unless defined $pid;
kill SIGSTOP, -(getpgrp($pid));
}
sub resume_process {
my $self = shift;
my $pid = $self->pid;
return unless defined $pid;
kill SIGCONT, -(getpgrp($pid));
}
sub failover_node {
my ($self,$nodeidx,$bucket_name) = @_;
$bucket_name ||= "default";
my $cmd = "failover,$nodeidx,$bucket_name\n";
log_warn($cmd);
$self->harakiri_socket->send($cmd, 0) or die "Couldn't send";
}
sub respawn_node {
my ($self,$nodeidx,$bucket_name) = @_;
$bucket_name ||= "default";
my $cmd = "respawn,$nodeidx,$bucket_name\n";
log_warn($cmd);
$self->harakiri_socket->send($cmd, 0) or die "Couldn't send";
}
sub DESTROY {
my $self = shift;
return unless $self->pid;
kill SIGTERM, $self->pid;
log_debugf("Waiting for process to terminate");
waitpid($self->pid, 0);
log_infof("Reaped PID %d, status %d", $self->pid, $? >> 8);
}
1;