-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathProjectingModule.php
More file actions
218 lines (195 loc) · 11 KB
/
ProjectingModule.php
File metadata and controls
218 lines (195 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
* licence Enterprise
*/
declare(strict_types=1);
namespace Ecotone\Projecting\Config;
use Ecotone\AnnotationFinder\AnnotationFinder;
use Ecotone\Messaging\Attribute\ModuleAnnotation;
use Ecotone\Messaging\Config\Annotation\AnnotationModule;
use Ecotone\Messaging\Config\Annotation\ModuleConfiguration\ExtensionObjectResolver;
use Ecotone\Messaging\Config\Configuration;
use Ecotone\Messaging\Config\ConfigurationException;
use Ecotone\Messaging\Config\Container\AttributeDefinition;
use Ecotone\Messaging\Config\Container\Definition;
use Ecotone\Messaging\Config\Container\InterfaceToCallReference;
use Ecotone\Messaging\Config\Container\Reference;
use Ecotone\Messaging\Config\ModulePackageList;
use Ecotone\Messaging\Config\ModuleReferenceSearchService;
use Ecotone\Messaging\Config\ServiceConfiguration;
use Ecotone\Messaging\Endpoint\Interceptor\TerminationListener;
use Ecotone\Messaging\Gateway\MessagingEntrypointService;
use Ecotone\Messaging\Handler\InterfaceToCallRegistry;
use Ecotone\Messaging\Handler\Processor\MethodInvoker\Converter\HeaderBuilder;
use Ecotone\Messaging\Handler\Processor\MethodInvoker\Converter\PayloadBuilder;
use Ecotone\Messaging\Handler\Processor\MethodInvoker\Converter\ValueBuilder;
use Ecotone\Messaging\Handler\Processor\MethodInvoker\MethodInvokerBuilder;
use Ecotone\Messaging\Handler\ServiceActivator\MessageProcessorActivatorBuilder;
use Ecotone\Projecting\Attribute\ProjectionFlush;
use Ecotone\Projecting\BackfillExecutorHandler;
use Ecotone\Projecting\InMemory\InMemoryProjectionRegistry;
use Ecotone\Projecting\PartitionProviderRegistry;
use Ecotone\Projecting\ProjectingHeaders;
use Ecotone\Projecting\ProjectingManager;
use Ecotone\Projecting\ProjectionRegistry;
use Ecotone\Projecting\ProjectionStateStorageRegistry;
use Ecotone\Projecting\SinglePartitionProvider;
use Ecotone\Projecting\StreamFilterRegistry;
use Ecotone\Projecting\StreamSourceRegistry;
/**
* This module allows to configure projections in a standard way
* It does not depend on any particular way of defining projections (attributes, configurations, etc.)
* It allows to register ProjectionExecutorBuilder and ProjectionComponentBuilder implementations
*/
#[ModuleAnnotation]
class ProjectingModule implements AnnotationModule
{
public static function getProjectorExecutorReference(string $projectionName): string
{
return 'projection_executor:' . $projectionName;
}
public static function create(AnnotationFinder $annotationRegistrationService, InterfaceToCallRegistry $interfaceToCallRegistry): static
{
return new self();
}
private const WITHOUT_DBAL_TRANSACTION_CLASS = 'Ecotone\Dbal\DbalTransaction\WithoutDbalTransaction';
public function prepare(Configuration $messagingConfiguration, array $extensionObjects, ModuleReferenceSearchService $moduleReferenceSearchService, InterfaceToCallRegistry $interfaceToCallRegistry): void
{
$serviceConfiguration = ExtensionObjectResolver::resolveUnique(ServiceConfiguration::class, $extensionObjects, ServiceConfiguration::createWithDefaults());
$projectionBuilders = ExtensionObjectResolver::resolve(ProjectionExecutorBuilder::class, $extensionObjects);
if (! empty($projectionBuilders) && ! $messagingConfiguration->isRunningForEnterpriseLicence()) {
throw ConfigurationException::create('Projections are part of Ecotone Enterprise. To use projections, please acquire an enterprise licence.');
}
$messagingConfiguration->registerServiceDefinition(
SinglePartitionProvider::class,
new Definition(SinglePartitionProvider::class)
);
$projectionRegistryMap = [];
foreach ($projectionBuilders as $projectionBuilder) {
$projectionName = $projectionBuilder->projectionName();
$reference = self::getProjectorExecutorReference($projectionName);
$moduleReferenceSearchService->store($reference, $projectionBuilder);
$messagingConfiguration->registerServiceDefinition(
$projectingManagerReference = ProjectingManager::class . ':' . $projectionName,
new Definition(ProjectingManager::class, [
new Reference(ProjectionStateStorageRegistry::class),
new Reference($reference),
new Reference(StreamSourceRegistry::class),
new Reference(PartitionProviderRegistry::class),
new Reference(StreamFilterRegistry::class),
$projectionName,
new Reference(TerminationListener::class),
new Reference(MessagingEntrypointService::class),
$projectionBuilder->eventLoadingBatchSize(),
$projectionBuilder->automaticInitialization(),
$projectionBuilder->backfillPartitionBatchSize(),
$projectionBuilder->backfillAsyncChannelName(),
])
);
$projectionRegistryMap[$projectionName] = new Reference($projectingManagerReference);
$executeHandlerBuilder = MessageProcessorActivatorBuilder::create()
->chainInterceptedProcessor(
MethodInvokerBuilder::create(
$projectingManagerReference,
InterfaceToCallReference::create(ProjectingManager::class, 'execute'),
[
$projectionBuilder->partitionHeader()
? HeaderBuilder::create('partitionKeyValue', $projectionBuilder->partitionHeader())
: ($projectionBuilder->isPartitioned()
? new PartitionHeaderBuilder('partitionKeyValue')
: ValueBuilder::create('partitionKeyValue', null)),
HeaderBuilder::createOptional('manualInitialization', ProjectingHeaders::MANUAL_INITIALIZATION),
],
)
)
->withEndpointId(self::endpointIdForProjection($projectionName))
->withInputChannelName(self::inputChannelForProjectingManager($projectionName));
if (class_exists(self::WITHOUT_DBAL_TRANSACTION_CLASS)) {
$executeHandlerBuilder = $executeHandlerBuilder->withEndpointAnnotations([new AttributeDefinition(self::WITHOUT_DBAL_TRANSACTION_CLASS)]);
}
$messagingConfiguration->registerMessageHandler($executeHandlerBuilder);
$messagingConfiguration->registerMessageHandler(
MessageProcessorActivatorBuilder::create()
->chainInterceptedProcessor(
MethodInvokerBuilder::create(
$projectingManagerReference,
InterfaceToCallReference::create(ProjectingManager::class, 'executeSingleBatch'),
[
HeaderBuilder::createOptional('partitionKeyValue', ProjectingHeaders::PROJECTION_PARTITION_KEY),
HeaderBuilder::create('canInitialize', ProjectingHeaders::PROJECTION_CAN_INITIALIZE),
],
)
)
->withInputChannelName(ProjectingManager::batchChannelFor($projectionName))
->withEndpointAnnotations([AttributeDefinition::fromObject(new ProjectionFlush())])
);
// Should the projection be triggered asynchronously?
if (
$serviceConfiguration->isModulePackageEnabled(ModulePackageList::ASYNCHRONOUS_PACKAGE)
&& $projectionBuilder->asyncChannelName() !== null
) {
$messagingConfiguration->registerAsynchronousEndpoint(
$projectionBuilder->asyncChannelName(),
self::endpointIdForProjection($projectionName),
);
}
}
// Register ProjectionRegistry
$messagingConfiguration->registerServiceDefinition(
ProjectionRegistry::class,
new Definition(InMemoryProjectionRegistry::class, [$projectionRegistryMap])
);
// Register BackfillExecutorHandler and its message handler
$messagingConfiguration->registerServiceDefinition(
BackfillExecutorHandler::class,
new Definition(BackfillExecutorHandler::class, [
new Reference(ProjectionRegistry::class),
new Reference(TerminationListener::class),
])
);
$backfillHandlerBuilder = MessageProcessorActivatorBuilder::create()
->chainInterceptedProcessor(
MethodInvokerBuilder::create(
BackfillExecutorHandler::class,
InterfaceToCallReference::create(BackfillExecutorHandler::class, 'executeBackfillBatch'),
[
PayloadBuilder::create('projectionName'),
HeaderBuilder::createOptional('limit', 'backfill.limit'),
HeaderBuilder::createOptional('offset', 'backfill.offset'),
HeaderBuilder::createOptional('streamName', 'backfill.streamName'),
HeaderBuilder::createOptional('aggregateType', 'backfill.aggregateType'),
HeaderBuilder::createOptional('eventStoreReferenceName', 'backfill.eventStoreReferenceName'),
],
)
)
->withEndpointId('backfill_executor_handler')
->withInputChannelName(BackfillExecutorHandler::BACKFILL_EXECUTOR_CHANNEL);
if (class_exists(self::WITHOUT_DBAL_TRANSACTION_CLASS)) {
$backfillHandlerBuilder = $backfillHandlerBuilder->withEndpointAnnotations([new AttributeDefinition(self::WITHOUT_DBAL_TRANSACTION_CLASS)]);
}
$messagingConfiguration->registerMessageHandler($backfillHandlerBuilder);
// Register console commands
$messagingConfiguration->registerServiceDefinition(ProjectingConsoleCommands::class, new Definition(ProjectingConsoleCommands::class, [new Reference(ProjectionRegistry::class)]));
}
public function canHandle($extensionObject): bool
{
return $extensionObject instanceof ServiceConfiguration
|| $extensionObject instanceof ProjectionExecutorBuilder;
}
public function getModuleExtensions(ServiceConfiguration $serviceConfiguration, array $serviceExtensions, ?InterfaceToCallRegistry $interfaceToCallRegistry = null): array
{
return [new ProjectingModuleRoutingExtension(self::inputChannelForProjectingManager(...))];
}
public function getModulePackageName(): string
{
return ModulePackageList::CORE_PACKAGE;
}
private static function endpointIdForProjection(string $projectionName): string
{
return 'projecting_manager_endpoint:' . $projectionName;
}
public static function inputChannelForProjectingManager(string $projectionName): string
{
return 'projecting_manager_handler:' . $projectionName;
}
}