Skip to content

Commit

Permalink
Add 'Admin::walProperties()' method
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassouzavieira committed Oct 25, 2019
1 parent d1e9799 commit 636e8da
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ArangoDB\Connection\Connection;
use ArangoDB\DataStructures\ArrayList;
use ArangoDB\Exceptions\ServerException;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use ArangoDB\Validation\Exceptions\InvalidParameterException;
Expand Down Expand Up @@ -102,6 +103,43 @@ public static function flushWal(Connection $connection, bool $waitForSync = true
$serverException = new ServerException($response['errorMessage'], $exception, $response['errorNum']);
throw $serverException;
}
}

/**
* Retrieves the configuration of the write-ahead log.
* The result is a array with the following keys:
* 'allowOversizeEntries': whether or not operations that are bigger than a single logfile can be executed and stored
* 'logfileSize': the size of each write-ahead logfile
* 'historicLogfiles': the maximum number of historic logfiles to keep
* 'reserveLogfiles': the maximum number of reserve logfiles that ArangoDB allocates in the background
* 'syncInterval': the interval for automatic synchronization of not-yet synchronized write-ahead log data (in milliseconds)
* 'throttleWait': the maximum wait time that operations will wait before they get aborted if case of write-throttling (in milliseconds)
* 'throttleWhenPending': the number of unprocessed garbage-collection operations that, when reached, will activate write-throttling.
* A value of 0 means that write-throttling will not be triggered.
*
* @param Connection $connection
* @return array
*
* @throws ServerException|GuzzleException
*/
public static function walProperties(Connection $connection): array
{
try {
$response = $connection->get(Api::ADMIN_WAL_PROPERTIES);
$data = json_decode((string)$response->getBody(), true);
return $data;
} catch (BadResponseException $exception) {
$response = json_decode((string)$exception->getResponse()->getBody(), true);
$message = isset($response['errorMessage']) ? $response['errorMessage'] : "Unknown error";
$code = isset($response['errorNum']) ? $response['errorNum'] : $exception->getResponse()->getStatusCode();

// Not implemented on server.
if ($exception->getResponse()->getStatusCode() === 501) {
$message = "Not Implemented";
}

$serverException = new ServerException($message, $exception, $code);
throw $serverException;
}
}
}
1 change: 1 addition & 0 deletions src/Http/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ abstract class Api
const ADMIN_TIME = "/_admin/time";
const ADMIN_LOG = "/_admin/log";
const ADMIN_FLUSH_WAL = "/_admin/wal/flush";
const ADMIN_WAL_PROPERTIES = "_admin/wal/properties";
const ADMIN_LOG_LEVEL = "/_admin/log/level";
const ADMIN_ROUTING_RELOAD = "/_admin/routing/reload";
const ADMIN_STATISTICS = "/_admin/statistics";
Expand Down
56 changes: 56 additions & 0 deletions tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Unit\TestCase;
use ArangoDB\Admin\Admin;
use GuzzleHttp\Psr7\Response;
use ArangoDB\Admin\Task\Task;
use GuzzleHttp\Handler\MockHandler;
use ArangoDB\DataStructures\ArrayList;
use ArangoDB\Exceptions\ServerException;
Expand Down Expand Up @@ -35,8 +36,21 @@ public function testStatisticsThrowServerException()

public function testTasks()
{
$command = "(function(params){ (function(){\n require('@arangodb/foxx/queues/manager').manage();\n })(params)})(params);";
$task = new Task("myTask", $command, $this->getConnectionObject());

// Create task
$tasks = Admin::tasks($this->getConnectionObject());
$this->assertInstanceOf(ArrayList::class, $tasks);
$this->assertEquals(0, count($tasks));

$this->assertTrue($task->save());

$tasks = Admin::tasks($this->getConnectionObject());
$this->assertEquals(1, count($tasks));

// Delete Task
$this->assertTrue($task->delete());
}

public function testTasksThrowServerException()
Expand Down Expand Up @@ -64,4 +78,46 @@ public function testFlushWalThrowServerException()
$this->expectException(ServerException::class);
$tasks = Admin::flushWal($this->getConnectionObject($mock));
}

public function testWalProperties()
{
$mock = new MockHandler([
new Response(200, [], json_encode([
'allowOversizeEntries' => true,
'logfileSize' => 128,
'historicLogfiles' => 512,
'reserveLogfiles' => 512,
'syncInterval' => 100,
'throttleWait' => 1000,
'throttleWhenPending' => 0
]))
]);

$result = Admin::walProperties($this->getConnectionObject($mock));
$this->assertIsArray($result);
$this->assertArrayHasKey('allowOversizeEntries', $result);
$this->assertArrayHasKey('logfileSize', $result);
$this->assertArrayHasKey('throttleWhenPending', $result);
$this->assertArrayHasKey('historicLogfiles', $result);
}

public function testWalPropertiesThrowServerException()
{
$mock = new MockHandler([
new Response(405, [], json_encode($this->mockServerError()))
]);

$this->expectException(ServerException::class);
$tasks = Admin::walProperties($this->getConnectionObject($mock));
}

public function testWalPropertiesWhenNotImplemented()
{
$mock = new MockHandler([
new Response(501, [], json_encode([]))
]);

$this->expectException(ServerException::class);
$tasks = Admin::walProperties($this->getConnectionObject($mock));
}
}

0 comments on commit 636e8da

Please sign in to comment.