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

TASK: Cleanup EventNormalizer #5016

Merged
merged 2 commits into from May 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 44 additions & 26 deletions Neos.ContentRepository.Core/Classes/EventStore/EventNormalizer.php
Expand Up @@ -43,6 +43,7 @@
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Event\WorkspaceWasRebased;
use Neos\EventStore\Model\Event;
use Neos\EventStore\Model\Event\EventData;
use Neos\EventStore\Model\Event\EventId;
use Neos\EventStore\Model\Event\EventType;

/**
Expand Down Expand Up @@ -117,32 +118,6 @@ public function __construct()
}
}

public function getEventData(EventInterface $event): EventData
{
try {
$eventDataAsJson = json_encode($event, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new \InvalidArgumentException(
sprintf(
'Failed to normalize event of type "%s": %s',
get_debug_type($event),
$exception->getMessage()
),
1651838981
);
}
return EventData::fromString($eventDataAsJson);
}

public function getEventType(EventInterface $event): EventType
{
$className = get_class($event);

return $this->fullClassNameToShortEventType[$className] ?? throw new \RuntimeException(
'Event type ' . get_class($event) . ' not registered'
);
}

/**
* @return class-string<EventInterface>
*/
Expand All @@ -154,6 +129,23 @@ public function getEventClassName(Event $event): string
);
}

public function normalize(EventInterface|DecoratedEvent $event): Event
{
$eventId = $event instanceof DecoratedEvent && $event->eventId !== null ? $event->eventId : EventId::create();
$eventMetadata = $event instanceof DecoratedEvent ? $event->eventMetadata : null;
$causationId = $event instanceof DecoratedEvent ? $event->causationId : null;
$correlationId = $event instanceof DecoratedEvent ? $event->correlationId : null;
$event = $event instanceof DecoratedEvent ? $event->innerEvent : $event;
return new Event(
$eventId,
$this->getEventType($event),
$this->getEventData($event),
$eventMetadata,
$causationId,
$correlationId,
);
}

public function denormalize(Event $event): EventInterface
{
$eventClassName = $this->getEventClassName($event);
Expand All @@ -177,4 +169,30 @@ public function denormalize(Event $event): EventInterface
default => $eventInstance,
};
}

private function getEventData(EventInterface $event): EventData
{
try {
$eventDataAsJson = json_encode($event, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new \InvalidArgumentException(
sprintf(
'Failed to normalize event of type "%s": %s',
get_debug_type($event),
$exception->getMessage()
),
1651838981
);
}
return EventData::fromString($eventDataAsJson);
}

private function getEventType(EventInterface $event): EventType
{
$className = get_class($event);

return $this->fullClassNameToShortEventType[$className] ?? throw new \RuntimeException(
'Event type ' . get_class($event) . ' not registered'
);
}
}
29 changes: 6 additions & 23 deletions Neos.ContentRepository.Core/Classes/EventStore/EventPersister.php
Expand Up @@ -21,13 +21,13 @@
*
* @internal
*/
final class EventPersister
final readonly class EventPersister
{
public function __construct(
private readonly EventStoreInterface $eventStore,
private readonly ProjectionCatchUpTriggerInterface $projectionCatchUpTrigger,
private readonly EventNormalizer $eventNormalizer,
private readonly Projections $projections,
private EventStoreInterface $eventStore,
private ProjectionCatchUpTriggerInterface $projectionCatchUpTrigger,
private EventNormalizer $eventNormalizer,
private Projections $projections,
) {
}

Expand All @@ -44,7 +44,7 @@ public function publishEvents(EventsToPublish $eventsToPublish): CommandResult
// the following logic could also be done in an AppEventStore::commit method (being called
// directly from the individual Command Handlers).
$normalizedEvents = Events::fromArray(
$eventsToPublish->events->map(fn(EventInterface|DecoratedEvent $event) => $this->normalizeEvent($event))
$eventsToPublish->events->map($this->eventNormalizer->normalize(...))
);
$commitResult = $this->eventStore->commit(
$eventsToPublish->streamName,
Expand All @@ -70,21 +70,4 @@ public function publishEvents(EventsToPublish $eventsToPublish): CommandResult
// The CommandResult can be used to block until projections are up to date.
return new CommandResult($pendingProjections, $commitResult);
}

private function normalizeEvent(EventInterface|DecoratedEvent $event): Event
{
$eventId = $event instanceof DecoratedEvent && $event->eventId !== null ? $event->eventId : EventId::create();
$eventMetadata = $event instanceof DecoratedEvent ? $event->eventMetadata : null;
$causationId = $event instanceof DecoratedEvent ? $event->causationId : null;
$correlationId = $event instanceof DecoratedEvent ? $event->correlationId : null;
$event = $event instanceof DecoratedEvent ? $event->innerEvent : $event;
return new Event(
$eventId,
$this->eventNormalizer->getEventType($event),
$this->eventNormalizer->getEventData($event),
$eventMetadata,
$causationId,
$correlationId,
);
}
}
Expand Up @@ -113,14 +113,14 @@ public function run(): ProcessorResult
return ProcessorResult::error(sprintf('Failed to read events. %s is not expected in imported event stream.', $event->type));
}
$domainEvent = DecoratedEvent::create($domainEvent, eventId: EventId::fromString($event->identifier), metadata: $event->metadata);
$domainEvents[] = $this->normalizeEvent($domainEvent);
$domainEvents[] = $this->eventNormalizer->normalize($domainEvent);
}

assert($this->contentStreamId !== null);

$contentStreamStreamName = ContentStreamEventStreamName::fromContentStreamId($this->contentStreamId)->getEventStreamName();
$events = Events::with(
$this->normalizeEvent(
$this->eventNormalizer->normalize(
new ContentStreamWasCreated(
$this->contentStreamId,
)
Expand All @@ -135,7 +135,7 @@ public function run(): ProcessorResult
$workspaceName = WorkspaceName::forLive();
$workspaceStreamName = WorkspaceEventStreamName::fromWorkspaceName($workspaceName)->getEventStreamName();
$events = Events::with(
$this->normalizeEvent(
$this->eventNormalizer->normalize(
new RootWorkspaceWasCreated(
$workspaceName,
WorkspaceTitle::fromString('live workspace'),
Expand All @@ -158,29 +158,6 @@ public function run(): ProcessorResult
return ProcessorResult::success(sprintf('Imported %d event%s into stream "%s"', count($domainEvents), count($domainEvents) === 1 ? '' : 's', $contentStreamStreamName->value));
}

/**
* Copied from {@see EventPersister::normalizeEvent()}
*
* @param EventInterface|DecoratedEvent $event
* @return Event
*/
private function normalizeEvent(EventInterface|DecoratedEvent $event): Event
{
$eventId = $event instanceof DecoratedEvent && $event->eventId !== null ? $event->eventId : EventId::create();
$eventMetadata = $event instanceof DecoratedEvent ? $event->eventMetadata : null;
$causationId = $event instanceof DecoratedEvent ? $event->causationId : null;
$correlationId = $event instanceof DecoratedEvent ? $event->correlationId : null;
$event = $event instanceof DecoratedEvent ? $event->innerEvent : $event;
return new Event(
$eventId,
$this->eventNormalizer->getEventType($event),
$this->eventNormalizer->getEventData($event),
$eventMetadata,
$causationId,
$correlationId,
);
}

/** --------------------------- */

/**
Expand Down
Expand Up @@ -170,11 +170,12 @@ private function resetRuntimeState(): void

private function exportEvent(EventInterface $event): void
{
$normalizedEvent = $this->eventNormalizer->normalize($event);
$exportedEvent = new ExportedEvent(
Uuid::uuid4()->toString(),
$this->eventNormalizer->getEventType($event)->value,
json_decode($this->eventNormalizer->getEventData($event)->value, true),
[]
$normalizedEvent->id->value,
Copy link
Member

Choose a reason for hiding this comment

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

i guess this is a functional change as in that we now have stable ids in the export.

Probably a good idea so we have less diff if exporting twice and having the export in version control like the Neos.Demo

Copy link
Member Author

Choose a reason for hiding this comment

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

It's the same as before (I think). The normalizer will create the id (unless its a decorated event that already has one)

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ah i see you're right a bit sneaky i see ^^ (We are sure at that time that its not a decorated event ... thats why it works)

Copy link
Member

Choose a reason for hiding this comment

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

Okay in that case i guess its a bit weird that normalise is not pure and will return different results on succeeding calls. But the behaviour is not new and required already at other places (we just seem to share the code here now)

So 👍 feel free to merge this ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Even if it was and even if it would specify the id it would work as long as the id is unique

$normalizedEvent->type->value,
json_decode($normalizedEvent->data->value, true),
[],
);
assert($this->eventFileResource !== null);
fwrite($this->eventFileResource, $exportedEvent->toJson() . chr(10));
Expand Down