Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify daemon processes #848

Merged
merged 46 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
f219b36
Refactoring Daemon to use new JobInterface
chingor13 Jan 11, 2018
1711ec8
Add InterruptTrait, separate parent daemon/child daemons
chingor13 Jan 11, 2018
72504a6
Merge remote-tracking branch 'upstream/master' into single-daemon
chingor13 Jan 12, 2018
4476bd7
Make daemon a JobInterface
chingor13 Jan 12, 2018
2f46589
WIP checkin that can run a simple loop job
chingor13 Jan 12, 2018
f2f8e3e
Merge remote-tracking branch 'upstream/master' into single-daemon
chingor13 Jan 12, 2018
5a42491
Debugger Daemon can run through the google-cloud-batch daemon
chingor13 Jan 12, 2018
16a7edd
Ignore debugger if the daemon hasn't registered and reported its status
chingor13 Jan 13, 2018
3ec0c07
Updating phpdoc
chingor13 Jan 16, 2018
5de248b
Cleanup job traits
chingor13 Jan 16, 2018
22b6282
clean up JobConfig variable names
chingor13 Jan 16, 2018
79c0efc
Fix debugger tests
chingor13 Jan 16, 2018
bc416ac
BatchConfigTest->JobConfigTest
chingor13 Jan 16, 2018
9ec24b2
Fix tests
chingor13 Jan 16, 2018
9d27617
Fix snippet tests
chingor13 Jan 16, 2018
b0892df
Merge branch 'master' into single-daemon
chingor13 Jan 18, 2018
5913e0a
Fix merge
chingor13 Jan 18, 2018
f6360e2
Fix unit tests
chingor13 Jan 18, 2018
159e74d
Merge branch 'master' into single-daemon
chingor13 Jan 19, 2018
0c5f64f
Fixing tests
chingor13 Jan 19, 2018
006d87e
New daemon options used by the lazy loaded debuggee
chingor13 Jan 19, 2018
244a0ba
Fix debug failure
chingor13 Jan 19, 2018
95061d3
Merge branch 'master' into single-daemon
chingor13 Jan 22, 2018
f4c9c2f
Fix type values for traits
chingor13 Jan 22, 2018
856a1bf
Agent provides storage implementation to daemon
chingor13 Jan 22, 2018
7ae8ba5
Merge remote-tracking branch 'upstream/master' into single-daemon
chingor13 Jan 29, 2018
4e302d2
Add exponential backoff and exception handling to Agent/Daemon
chingor13 Jan 29, 2018
35591db
Force GC in Daemon loop
chingor13 Jan 30, 2018
6c0b628
Merge branch 'master' into single-daemon
chingor13 Jan 30, 2018
f47063d
With combined daemons, you no longer need the additional supervisord …
chingor13 Jan 30, 2018
cec10c6
Fix snippet test
chingor13 Jan 30, 2018
1c5c651
Use eventually consistent test trait for checking debuggeeId in E2E test
chingor13 Jan 30, 2018
a3aa980
Fix typo in E2ETest
chingor13 Jan 30, 2018
b73205f
We no longer need the google-cloud-debugger script
chingor13 Jan 30, 2018
ed84075
Remove __wakeup bootstrap file hack
chingor13 Jan 31, 2018
dc9ca6c
Fix test expectation
chingor13 Jan 31, 2018
fcd4093
Remove commented out test
chingor13 Feb 1, 2018
7acf4f5
Merge branch 'master' into single-daemon
chingor13 Feb 6, 2018
8ea8505
Fix tests from merge
chingor13 Feb 6, 2018
12d93f5
phpcs fix from merge
chingor13 Feb 6, 2018
cf8f9f2
Address PR feedback
chingor13 Feb 8, 2018
fc5fc18
Adding tests for JobTrait, SimpleJob, SimpleJobTrait
chingor13 Feb 8, 2018
180f220
Extra closure serialization client config into a trait
chingor13 Feb 8, 2018
4b7bc75
Merge branch 'master' into single-daemon
chingor13 Feb 8, 2018
e4cabfc
Ensure the deployment succeeds. Retry on 5xx errors
chingor13 Feb 8, 2018
393aef1
Fix agent starting timing with the daemon registration
chingor13 Feb 8, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@
"google-cloud": "dev/google-cloud"
},
"bin": [
"src/Core/bin/google-cloud-batch",
"src/Debugger/bin/google-cloud-debugger"
"src/Core/bin/google-cloud-batch"
],
"extra": {
"component": {
Expand Down
113 changes: 19 additions & 94 deletions src/Core/Batch/BatchDaemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ class BatchDaemon
use BatchDaemonTrait;
use HandleFailureTrait;
use SysvTrait;
use InterruptTrait;

/* @var BatchRunner */
private $runner;

/* @var bool */
private $shutdown;

/* @var array */
private $descriptorSpec;

Expand Down Expand Up @@ -72,53 +70,31 @@ public function __construct($entrypoint)
1 => ['file', 'php://stdout', 'w'],
2 => ['file', 'php://stderr', 'w']
];
// setup signal handlers
pcntl_signal(SIGTERM, [$this, "sigHandler"]);
pcntl_signal(SIGINT, [$this, "sigHandler"]);
pcntl_signal(SIGHUP, [$this, "sigHandler"]);
pcntl_signal(SIGALRM, [$this, "sigHandler"]);

$this->command = sprintf('exec php -d auto_prepend_file="" %s daemon', $entrypoint);
$this->initFailureFile();
}

/**
* A signal handler for setting the terminate switch.
* {@see http://php.net/manual/en/function.pcntl-signal.php}
*
* @param int $signo The received signal.
* @param mixed $siginfo [optional] An array representing the signal
* information. **Defaults to** null.
*
* @return void
*/
public function sigHandler($signo, $signinfo = null)
{
switch ($signo) {
case SIGINT:
case SIGTERM:
$this->shutdown = true;
break;
}
}

/**
* A loop for the parent.
*
* @return void
*/
public function runParent()
public function run()
{
$this->setupSignalHandlers();

$procs = [];
while (true) {
$jobs = $this->runner->getJobs();
foreach ($jobs as $job) {
if (! array_key_exists($job->getIdentifier(), $procs)) {
$procs[$job->getIdentifier()] = [];
if (! array_key_exists($job->identifier(), $procs)) {
$procs[$job->identifier()] = [];
}
while (count($procs[$job->getIdentifier()]) > $job->getWorkerNum()) {
while (count($procs[$job->identifier()]) > $job->numWorkers()) {
// Stopping an excessive child.
echo 'Stopping an excessive child.' . PHP_EOL;
$proc = array_pop($procs[$job->getIdentifier()]);
$proc = array_pop($procs[$job->identifier()]);
$status = proc_get_status($proc);
// Keep sending SIGTERM until the child exits.
while ($status['running'] === true) {
Expand All @@ -128,10 +104,10 @@ public function runParent()
}
@proc_close($proc);
}
for ($i = 0; $i < $job->getWorkerNum(); $i++) {
for ($i = 0; $i < $job->numWorkers(); $i++) {
$needStart = false;
if (array_key_exists($i, $procs[$job->getIdentifier()])) {
$status = proc_get_status($procs[$job->getIdentifier()][$i]);
if (array_key_exists($i, $procs[$job->identifier()])) {
$status = proc_get_status($procs[$job->identifier()][$i]);
if ($status['running'] !== true) {
$needStart = true;
}
Expand All @@ -140,8 +116,8 @@ public function runParent()
}
if ($needStart) {
echo 'Starting a child.' . PHP_EOL;
$procs[$job->getIdentifier()][$i] = proc_open(
sprintf('%s %d', $this->command, $job->getIdNum()),
$procs[$job->identifier()][$i] = proc_open(
sprintf('%s %d', $this->command, $job->id()),
$this->descriptorSpec,
$pipes
);
Expand Down Expand Up @@ -173,64 +149,13 @@ public function runParent()
}

/**
* A loop for the children.
* Fetch the child job by id.
*
* @param int $idNum Numeric id for the job.
* @return void
* @param int $idNum The id of the job to find
* @return JobInterface
*/
public function runChild($idNum)
public function job($idNum)
{
// child process
$sysvKey = $this->getSysvKey($idNum);
$q = msg_get_queue($sysvKey);
$items = [];
$job = $this->runner->getJobFromIdNum($idNum);
$period = $job->getCallPeriod();
$lastInvoked = microtime(true);
$batchSize = $job->getBatchSize();
while (true) {
// Fire SIGALRM after 1 second to unblock the blocking call.
pcntl_alarm(1);
if (msg_receive(
$q,
0,
$type,
8192,
$message,
true,
0, // blocking mode
$errorcode
)) {
if ($type === self::$typeDirect) {
$items[] = $message;
} elseif ($type === self::$typeFile) {
$items[] = unserialize(file_get_contents($message));
@unlink($message);
}
}
pcntl_signal_dispatch();
// It runs the job when
// 1. Number of items reaches the batchSize.
// 2-a. Count is >0 and the current time is larger than lastInvoked + period.
// 2-b. Count is >0 and the shutdown flag is true.
if ((count($items) >= $batchSize)
|| (count($items) > 0
&& (microtime(true) > $lastInvoked + $period
|| $this->shutdown))) {
printf(
'Running the job with %d items' . PHP_EOL,
count($items)
);
if (! $job->run($items)) {
$this->handleFailure($idNum, $items);
}
$items = [];
$lastInvoked = microtime(true);
}
gc_collect_cycles();
if ($this->shutdown) {
exit;
}
}
return $this->runner->getJobFromIdNum($idNum);
}
}
3 changes: 0 additions & 3 deletions src/Core/Batch/BatchDaemonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
*/
trait BatchDaemonTrait
{
private static $typeDirect = 1;
private static $typeFile = 2;

/**
* Returns whether or not the BatchDaemon is running.
*
Expand Down
Loading