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

improve batch processing in projectionist #517

Merged
merged 1 commit into from
Mar 3, 2024
Merged
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
28 changes: 22 additions & 6 deletions src/Projection/Projectionist/DefaultProjectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ function ($projections) use ($limit): void {
);

$stream = null;
$messageCounter = 0;

try {
$stream = $this->messageStore->load(
new Criteria(fromIndex: $startIndex),
);

$messageCounter = 0;

foreach ($stream as $message) {
$index = $stream->index();

Expand Down Expand Up @@ -141,6 +140,16 @@ function ($projections) use ($limit): void {
}
}
} finally {
if ($messageCounter > 0) {
foreach ($projections as $projection) {
if (!$projection->isBooting()) {
continue;
}

$this->projectionStore->update($projection);
}
}

$stream?->close();
}

Expand Down Expand Up @@ -212,13 +221,12 @@ function (array $projections) use ($limit): void {
);

$stream = null;
$messageCounter = 0;

try {
$criteria = new Criteria(fromIndex: $startIndex);
$stream = $this->messageStore->load($criteria);

$messageCounter = 0;

foreach ($stream as $message) {
$index = $stream->index();

Expand Down Expand Up @@ -263,6 +271,16 @@ function (array $projections) use ($limit): void {
}
}
} finally {
if ($messageCounter > 0) {
foreach ($projections as $projection) {
if (!$projection->isActive()) {
continue;
}

$this->projectionStore->update($projection);
}
}

$stream?->close();
}

Expand Down Expand Up @@ -547,7 +565,6 @@ private function handleMessage(int $index, Message $message, Projection $project

if ($subscribeMethods === []) {
$projection->changePosition($index);
$this->projectionStore->update($projection);

$this->logger?->debug(
sprintf(
Expand Down Expand Up @@ -583,7 +600,6 @@ private function handleMessage(int $index, Message $message, Projection $project

$projection->changePosition($index);
$projection->resetRetry();
$this->projectionStore->update($projection);

$this->logger?->debug(
sprintf(
Expand Down
28 changes: 14 additions & 14 deletions tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ public function handle(Message $message): void
ProjectionStatus::Booting,
1,
),
new Projection(
$projectionId2,
Projection::DEFAULT_GROUP,
RunMode::FromBeginning,
ProjectionStatus::Booting,
1,
),
new Projection(
$projectionId1,
Projection::DEFAULT_GROUP,
Expand Down Expand Up @@ -461,13 +468,6 @@ public function handle(Message $message): void
$projectionist->boot();

self::assertEquals([
new Projection(
$projectionId,
Projection::DEFAULT_GROUP,
RunMode::FromBeginning,
ProjectionStatus::Booting,
1,
),
new Projection(
$projectionId,
Projection::DEFAULT_GROUP,
Expand Down Expand Up @@ -834,6 +834,13 @@ public function handle(Message $message): void
ProjectionStatus::Active,
1,
),
new Projection(
$projectionId2,
Projection::DEFAULT_GROUP,
RunMode::FromBeginning,
ProjectionStatus::Active,
1,
),
], $projectionStore->updatedProjections);

self::assertSame($message, $projector1->message);
Expand Down Expand Up @@ -999,13 +1006,6 @@ public function handle(Message $message): void
$projectionist->run();

self::assertEquals([
new Projection(
$projectionId,
Projection::DEFAULT_GROUP,
RunMode::FromBeginning,
ProjectionStatus::Active,
1,
),
new Projection(
$projectionId,
Projection::DEFAULT_GROUP,
Expand Down
Loading