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

On client error throw, catch and handle exception #38

Open
wants to merge 7 commits into
base: feature/TAO-10203-advanced-search
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion src/ElasticSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function query($queryString, $type, $start = 0, $count = 10, $order = '_i
/**
* (Re)Generate the index for a given resource
* @param IndexIterator|array $documents
* @return integer
* @throws ClientErrorResponseException
*/
public function index($documents = []): int
{
Expand Down
26 changes: 24 additions & 2 deletions src/ElasticSearchIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace oat\tao\elasticsearch;

use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\ClientErrorResponseException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Iterator;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function getIndexNameByDocument(IndexDocument $document): string
/**
* @param Iterator $documents
* @return int The number of indexed documents
* @throws ClientErrorResponseException
*/
public function buildIndex(Iterator $documents): int
{
Expand Down Expand Up @@ -110,6 +112,10 @@ public function buildIndex(Iterator $documents): int
if ($blockSize === self::INDEXING_BLOCK_SIZE) {
$clientResponse = $this->client->bulk($params);

if ($clientResponse['errors'] === true) {
throw new ClientErrorResponseException($this->parseErrors($clientResponse));
}
bartlomiejmarszal marked this conversation as resolved.
Show resolved Hide resolved

$this->logger->debug('client response: '. json_encode($clientResponse));

$count += $blockSize;
Expand All @@ -121,7 +127,11 @@ public function buildIndex(Iterator $documents): int
if ($blockSize > 0) {
$clientResponse = $this->client->bulk($params);

$this->logger->debug('client response: '. json_encode($clientResponse));
if ($clientResponse['errors'] === true) {
throw new ClientErrorResponseException($this->parseErrors($clientResponse));
}

$this->logger->debug('client response: ' . json_encode($clientResponse));

$count += $blockSize;
}
Expand All @@ -141,7 +151,7 @@ public function deleteDocument($id): bool
$deleteParams = [
'type' => '_doc',
'index' => $document['_index'],
'id' => $document['_id']
'id' => $document['_id'],
];
$this->getClient()->delete($deleteParams);

Expand All @@ -154,6 +164,7 @@ public function deleteDocument($id): bool
/**
* @param array $ids
* @param string $type
*
* @return array
*/
public function searchResourceByIds($ids = [])
Expand Down Expand Up @@ -214,4 +225,15 @@ private function extendBatch(string $action, string $indexName, IndexDocument $d

return $params;
}

private function parseErrors(array $clientResponse): string
{
$errors = '';

foreach ($clientResponse['items'] as $response) {
$response = reset($response);
bartlomiejmarszal marked this conversation as resolved.
Show resolved Hide resolved
$errors .= $response['error']['reason'] . '; ';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we using this reason inside FE , did we communicate with them about the glue used?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we discussed , let's add a identifier to the processed data so we are able to identify the failing items
also let's check what items are failing

}
return $errors;
}
}
103 changes: 103 additions & 0 deletions test/ElasticSearchIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
namespace oat\tao\test\elasticsearch;

use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\ClientErrorResponseException;
use oat\generis\test\TestCase;
use oat\oatbox\log\LoggerService;
use oat\tao\elasticsearch\ElasticSearchIndexer;
use oat\tao\elasticsearch\IndexerInterface;
use oat\tao\model\search\index\IndexDocument;
use oat\tao\model\TaoOntology;
use ArrayIterator;
use PHPUnit\Framework\MockObject\MockObject;

/**
* ElasticSearchIndexerTest
Expand Down Expand Up @@ -88,6 +90,107 @@ public function testGetIndexNameByDocumentForUnclassifieds(): void
$this->assertSame(IndexerInterface::UNCLASSIFIEDS_DOCUMENTS_INDEX, $indexName);
}

public function testBuildIndexBulkErrorResponseAfter100(): void
{
$this->expectException(ClientErrorResponseException::class);
$this->expectExceptionMessage('some reason; some other reason');

$document = $this->createMock(IndexDocument::class);
$document->expects($this->any())
->method('getBody')
->willReturn([
'type' => [
TaoOntology::CLASS_URI_ITEM,
],
]);

$document->expects($this->any())
->method('getId')
->willReturn('some_id');

$this->client
->expects($this->atMost(100))
->method('bulk')
->willReturn([
'errors' => true,
'items' => [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure about the structure of the $clientResponse['items'] ?

[
[
'error' => [
'reason' => 'some reason',
],
],
],
[
[
'error' => [
'reason' => 'some other reason',
],
],
],
],
]);

$iterator = $this->createIterator($this->getMultipleDocs($document, 101));


$this->sut->buildIndex($iterator);
}

private function getMultipleDocs(MockObject $doc, $quantity)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approved the PR, but we could avoid that by making the INDEXING_BLOCK_SIZE injectable / customizable, so yuo do not need to iterate 100 results to test. I think this test will be slow at some point if we do not do it.

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please move the private method as the last? It is in our CR guideline

{
$bigArray = [];
for ($i = 1; $i <= $quantity; $i++) {
array_push($bigArray, $doc);
}

return $bigArray;
}

gabrielfs7 marked this conversation as resolved.
Show resolved Hide resolved

public function testBuildIndexBulkErrorResponse(): void
gabrielfs7 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->expectException(ClientErrorResponseException::class);
$this->expectExceptionMessage('some reason; some other reason');
$document = $this->createMock(IndexDocument::class);
$document->expects($this->any())
->method('getBody')
->willReturn([
'type' => [
TaoOntology::CLASS_URI_ITEM,
],
]);

$document->expects($this->any())
->method('getId')
->willReturn('some_id');

$this->client
->method('bulk')
->willReturn([
'errors' => true,
'items' => [
[
[
'error' => [
'reason' => 'some reason',
],
],
],
[
[
'error' => [
'reason' => 'some other reason',
],
],
],
],
]);
$iterator = $this->createIterator([$document]);

$this->sut->buildIndex($iterator);
}

public function testBuildIndex(): void
{
$document = $this->createMock(IndexDocument::class);
Expand Down