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

feat(Container): block devices io limits #284

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions Docker/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,25 @@ public function getRunCommand($containerId)
$labels .= ' --label ' . escapeshellarg($label);
}

$process = new \Symfony\Component\Process\Process("lsblk --nodeps --output NAME --noheadings 2>/dev/null");
$process->mustRun();
$devices = array_filter(explode("\n", $process->getOutput()), function ($device) {
return !empty($device);
});
$deviceLimits = "";
foreach ($devices as $device) {
$deviceLimits .= " --device-write-bps /dev/{$device}:{$this->limits->getDeviceIOLimits($this->getImage())}";
$deviceLimits .= " --device-read-bps /dev/{$device}:{$this->limits->getDeviceIOLimits($this->getImage())}";
}

$command .= " --volume " . escapeshellarg($this->dataDir . ":/data")
. " --volume " . escapeshellarg($this->tmpDir . ":/tmp")
. " --memory " . escapeshellarg($this->limits->getMemoryLimit($this->getImage()))
. " --memory-swap " . escapeshellarg($this->limits->getMemorySwapLimit($this->getImage()))
. " --cpu-shares " . escapeshellarg($this->limits->getCpuSharesLimit($this->getImage()))
. " --net " . escapeshellarg($this->limits->getNetworkLimit($this->getImage()))
. " --cpus " . escapeshellarg($this->limits->getCpuLimit($this->getImage()))
. $deviceLimits
. $envs
. $labels
. " --name " . escapeshellarg($containerId)
Expand Down
5 changes: 5 additions & 0 deletions Docker/Runner/Limits.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public function getCpuLimit(Image $image)
return min($instance, $project);
}

public function getDeviceIOLimits(Image $image)
{
return '50m';
}

/**
* @return Range[]
*/
Expand Down
14 changes: 14 additions & 0 deletions Tests/Docker/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ public function testRunCommand()
new OutputFilter(),
new Limits($log, ['cpu_count' => 2], [], [], [])
);

// block devices
$process = new \Symfony\Component\Process\Process("lsblk --nodeps --output NAME --noheadings 2>/dev/null");
$process->mustRun();
$devices = array_filter(explode("\n", $process->getOutput()), function ($device) {
return !empty($device);
});
$deviceLimits = "";
foreach ($devices as $device) {
$deviceLimits .= " --device-write-bps /dev/{$device}:50m";
$deviceLimits .= " --device-read-bps /dev/{$device}:50m";
}

$expected = "sudo timeout --signal=SIGKILL 3600"
. " docker run"
. " --volume '/data:/data'"
Expand All @@ -163,6 +176,7 @@ public function testRunCommand()
. " --cpu-shares '1024'"
. " --net 'bridge'"
. " --cpus '2'"
. $deviceLimits
. " --env \"var=val\""
. " --env \"příliš=žluťoučký\""
. " --env \"var2=weird = '\\\"value\""
Expand Down
83 changes: 26 additions & 57 deletions Tests/Runner/LimitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,9 @@ public function testInstanceLimitsInvalid()
[],
[]
);
$componentMock = self::getMockBuilder(Component::class)
->disableOriginalConstructor()
->getMock();
$image = self::getMockBuilder(AWSElasticContainerRegistry::class)
->disableOriginalConstructor()
->setMethods(['getSourceComponent'])
->getMock();
$image->method('getSourceComponent')
->will(self::returnValue($componentMock));
/** @var Image $image */
self::expectException(ApplicationException::class);
self::expectExceptionMessage('cpu_count is set incorrectly in parameters.yml: This value should be a valid number.');
$limits->getCpuLimit($image);
$limits->getCpuLimit($this->getImageMock());
}

public function testProjectLimitsInvalid()
Expand All @@ -50,19 +40,9 @@ public function testProjectLimitsInvalid()
[],
[]
);
$componentMock = self::getMockBuilder(Component::class)
->disableOriginalConstructor()
->getMock();
$image = self::getMockBuilder(AWSElasticContainerRegistry::class)
->disableOriginalConstructor()
->setMethods(['getSourceComponent'])
->getMock();
$image->method('getSourceComponent')
->will(self::returnValue($componentMock));
/** @var Image $image */
self::expectException(ApplicationException::class);
self::expectExceptionMessage('runner.cpuParallelism limit is set incorrectly: This value should be 96 or less.');
$limits->getCpuLimit($image);
$limits->getCpuLimit($this->getImageMock());
}

public function testLimitsInstance()
Expand All @@ -76,17 +56,7 @@ public function testLimitsInstance()
['foo', 'bar'],
['bar', 'kochba']
);
$componentMock = self::getMockBuilder(Component::class)
->disableOriginalConstructor()
->getMock();
$image = self::getMockBuilder(AWSElasticContainerRegistry::class)
->disableOriginalConstructor()
->setMethods(['getSourceComponent'])
->getMock();
$image->method('getSourceComponent')
->will(self::returnValue($componentMock));
/** @var Image $image */
self::assertEquals(1, $limits->getCpuLimit($image));
self::assertEquals(1, $limits->getCpuLimit($this->getImageMock()));
self::assertContains('CPU limits - instance: 1 project: 2', $handler->getRecords()[0]['message']);
}

Expand All @@ -99,17 +69,7 @@ public function testLimitsDefault()
[],
[]
);
$componentMock = self::getMockBuilder(Component::class)
->disableOriginalConstructor()
->getMock();
$image = self::getMockBuilder(AWSElasticContainerRegistry::class)
->disableOriginalConstructor()
->setMethods(['getSourceComponent'])
->getMock();
$image->method('getSourceComponent')
->will(self::returnValue($componentMock));
/** @var Image $image */
self::assertEquals(2, $limits->getCpuLimit($image));
self::assertEquals(2, $limits->getCpuLimit($this->getImageMock()));
}

public function testLimitsProject()
Expand All @@ -121,17 +81,7 @@ public function testLimitsProject()
[],
[]
);
$componentMock = self::getMockBuilder(Component::class)
->disableOriginalConstructor()
->getMock();
$image = self::getMockBuilder(AWSElasticContainerRegistry::class)
->disableOriginalConstructor()
->setMethods(['getSourceComponent'])
->getMock();
$image->method('getSourceComponent')
->will(self::returnValue($componentMock));
/** @var Image $image */
self::assertEquals(10, $limits->getCpuLimit($image));
self::assertEquals(10, $limits->getCpuLimit($this->getImageMock()));
}

public function testLimitsProjectInstance()
Expand All @@ -143,6 +93,26 @@ public function testLimitsProjectInstance()
[],
[]
);
self::assertEquals(2, $limits->getCpuLimit($this->getImageMock()));
}

public function testDeviceIOLimits()
{
$limits = new Limits(
new NullLogger(),
[],
[],
[],
[]
);
self::assertEquals("50m", $limits->getDeviceIOLimits($this->getImageMock()));
}

/**
* @return Image
*/
private function getImageMock()
{
$componentMock = self::getMockBuilder(Component::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -152,7 +122,6 @@ public function testLimitsProjectInstance()
->getMock();
$image->method('getSourceComponent')
->will(self::returnValue($componentMock));
/** @var Image $image */
self::assertEquals(2, $limits->getCpuLimit($image));
return $image;
}
}