Skip to content

Commit 648006f

Browse files
Refactor actors to use new client (#104)
* Start defining actor methods * Finish implementing client code * Refactor proxies to use new client * Refactor integration tests to use new actor proxy * Refactor proxy generators to only need the client * Refactor runtime
1 parent e7968b9 commit 648006f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1108
-449
lines changed

src/config.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@
8888
->constructorParameter('drain_timeout', get('dapr.actors.drain_timeout'))
8989
->constructorParameter('drain_enabled', get('dapr.actors.drain_enabled')),
9090
ActorRuntime::class => autowire()
91-
->constructorParameter('logger', get('dapr.logger'))
92-
->constructorParameter('deserializer', get('dapr.internal.deserializer')),
91+
->constructorParameter('client', get(\Dapr\Client\DaprClient::class)),
9392
ActorState::class => autowire()->constructorParameter('logger', get('dapr.logger')),
9493
ActorProxy::class => autowire()->constructorParameter('logger', get('dapr.logger')),
9594
ApplicationJson::class => autowire(),
@@ -143,6 +142,7 @@
143142
TransactionalState::class => autowire()->constructorParameter('logger', get('dapr.logger')),
144143

145144
// default application settings
145+
\Dapr\Actors\Internal\Caches\CacheInterface::class => autowire(\Dapr\Actors\Internal\Caches\MemoryCache::class),
146146
'dapr.pubsub.default' => 'pubsub',
147147
'dapr.actors.proxy.generation' => ProxyFactory::GENERATED,
148148
'dapr.subscriptions' => [],

src/index.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ function (ProxyFactory $proxyFactory, DaprClient $client, LoggerInterface $logge
8686
data: ['amount' => 2],
8787
period: new DateInterval('PT10M')
8888
);
89-
$actor->create_reminder($reminder, $client);
89+
$actor->create_reminder($reminder);
9090
$logger->critical('Created reminder');
9191
sleep(2);
9292
$body = assert_equals($body, 3, $actor->get_count(), 'Reminder should increment');
93-
$read_reminder = $actor->get_reminder('increment', $client);
93+
$read_reminder = $actor->get_reminder('increment');
9494
$logger->critical('Got reminder');
9595
$body = assert_equals(
9696
$body,
@@ -106,15 +106,15 @@ function (ProxyFactory $proxyFactory, DaprClient $client, LoggerInterface $logge
106106
callback: 'increment',
107107
data: 2
108108
);
109-
$actor->create_timer($timer, $client);
109+
$actor->create_timer($timer);
110110
$logger->critical('Created timer');
111111
sleep(2);
112112
$body = assert_equals($body, 5, $actor->get_count(), 'Timer should increment');
113113

114-
$actor->delete_timer('increment', $client);
115-
$actor->delete_reminder('increment', $client);
116-
$actor->delete_reminder('nope', $client);
117-
$actor->delete_timer('nope', $client);
114+
$actor->delete_timer('increment');
115+
$actor->delete_reminder('increment');
116+
$actor->delete_reminder('nope');
117+
$actor->delete_timer('nope');
118118
$logger->critical('Cleaned up');
119119

120120
$object = new SimpleObject();

src/lib/Actors/ActorRuntime.php

+62-52
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace Dapr\Actors;
44

5+
use Dapr\Actors\Internal\Caches\CacheInterface;
56
use Dapr\Actors\Internal\Caches\FileCache;
6-
use Dapr\Actors\Internal\Caches\NoCache;
7-
use Dapr\Deserialization\IDeserializer;
7+
use Dapr\Client\DaprClient;
88
use Dapr\exceptions\DaprException;
99
use Dapr\exceptions\Http\NotFound;
1010
use Dapr\exceptions\SaveStateFailure;
11-
use DI\Container;
1211
use DI\DependencyException;
1312
use DI\FactoryInterface;
1413
use DI\NotFoundException;
1514
use Exception;
16-
use Psr\Log\LoggerInterface;
15+
use Psr\Container\ContainerInterface;
1716
use ReflectionClass;
1817
use ReflectionException;
1918
use ReflectionNamedType;
@@ -28,11 +27,10 @@
2827
class ActorRuntime
2928
{
3029
public function __construct(
31-
protected LoggerInterface $logger,
3230
protected ActorConfig $actor_config,
3331
protected FactoryInterface $factory,
34-
protected Container $container,
35-
protected IDeserializer $deserializer,
32+
protected ContainerInterface $container,
33+
protected DaprClient $client
3634
) {
3735
}
3836

@@ -49,7 +47,7 @@ public function __construct(
4947
public function do_method(IActor $actor, string $method, mixed $arg): mixed
5048
{
5149
$reflection = new ReflectionClass($actor);
52-
$method = $reflection->getMethod($method);
50+
$method = $reflection->getMethod($method);
5351
if (empty($arg)) {
5452
return $method->invoke($actor);
5553
}
@@ -61,18 +59,18 @@ public function do_method(IActor $actor, string $method, mixed $arg): mixed
6159

6260
return $method->invokeArgs(
6361
$actor,
64-
[$parameter->getName() => $this->deserializer->detect_from_parameter($parameter, $arg)]
62+
[$parameter->getName() => $this->client->deserializer->detect_from_parameter($parameter, $arg)]
6563
);
6664
}
6765

6866
public function deactivate_actor(IActor $actor, string $dapr_type): void
6967
{
70-
$id = $actor->get_id();
71-
$activation_tracker = hash('sha256', $dapr_type.$id);
68+
$id = $actor->get_id();
69+
$activation_tracker = hash('sha256', $dapr_type . $id);
7270
$activation_tracker = rtrim(
73-
sys_get_temp_dir(),
74-
DIRECTORY_SEPARATOR
75-
).DIRECTORY_SEPARATOR.'dapr_'.$activation_tracker;
71+
sys_get_temp_dir(),
72+
DIRECTORY_SEPARATOR
73+
) . DIRECTORY_SEPARATOR . 'dapr_' . $activation_tracker;
7674

7775
$is_activated = file_exists($activation_tracker);
7876

@@ -86,21 +84,20 @@ public function deactivate_actor(IActor $actor, string $dapr_type): void
8684
/**
8785
* Resolve an actor, then calls your callback with the actor before committing any state changes
8886
*
89-
* @param string $dapr_type The dapr type to resolve
90-
* @param string $id The id of the actor to resolve
87+
* @param ActorReference $reference
9188
* @param callable $loan A callback that takes an IActor
9289
*
9390
* @return mixed The result of you callback
9491
* @throws NotFound
9592
* @throws SaveStateFailure
9693
*/
97-
public function resolve_actor(string $dapr_type, string $id, callable $loan): mixed
94+
public function resolve_actor(ActorReference $reference, callable $loan): mixed
9895
{
9996
try {
100-
$reflection = $this->locate_actor($dapr_type);
97+
$reflection = $this->locate_actor($reference);
10198
$this->validate_actor($reflection);
102-
$states = $this->get_states($reflection, $dapr_type, $id);
103-
$actor = $this->get_actor($reflection, $dapr_type, $id, $states);
99+
$states = $this->get_states($reflection, $reference, $this->client);
100+
$actor = $this->get_actor($reflection, $reference, $states);
104101
// @codeCoverageIgnoreStart
105102
} catch (Exception $exception) {
106103
throw new NotFound('Actor could not be located', previous: $exception);
@@ -123,18 +120,17 @@ public function resolve_actor(string $dapr_type, string $id, callable $loan): mi
123120
/**
124121
* Locates an actor implementation
125122
*
126-
* @param string $dapr_type The dapr type to locate
127-
*
123+
* @param ActorReference $reference The actor reference
128124
* @return ReflectionClass
129125
* @throws NotFound
130126
* @throws ReflectionException
131127
*/
132-
protected function locate_actor(string $dapr_type): ReflectionClass
128+
protected function locate_actor(ActorReference $reference): ReflectionClass
133129
{
134-
$type = $this->actor_config->get_actor_type_from_dapr_type($dapr_type);
135-
if ( ! class_exists($type)) {
130+
$type = $this->actor_config->get_actor_type_from_dapr_type($reference->get_actor_type());
131+
if (!class_exists($type)) {
136132
// @codeCoverageIgnoreStart
137-
$this->logger->critical('Unable to locate an actor for {t}', ['t' => $type]);
133+
$this->client->logger->critical('Unable to locate an actor for {t}', ['t' => $type]);
138134
throw new NotFound();
139135
// @codeCoverageIgnoreEnd
140136
}
@@ -156,7 +152,7 @@ protected function validate_actor(ReflectionClass $reflection): bool
156152
}
157153

158154
// @codeCoverageIgnoreStart
159-
$this->logger->critical('Actor does not implement the IActor interface');
155+
$this->client->logger->critical('Actor does not implement the IActor interface');
160156
throw new NotFound();
161157
// @codeCoverageIgnoreEnd
162158
}
@@ -165,18 +161,19 @@ protected function validate_actor(ReflectionClass $reflection): bool
165161
* Retrieves an array of states for a located actor
166162
*
167163
* @param ReflectionClass $reflection The class we're loading states for
168-
* @param string $dapr_type The dapr type
169-
* @param string $id The id
170-
*
164+
* @param ActorReference $reference
171165
* @return array
172-
* @throws ReflectionException
173166
* @throws DependencyException
174167
* @throws NotFoundException
168+
* @throws ReflectionException
175169
*/
176-
protected function get_states(ReflectionClass $reflection, string $dapr_type, string $id): array
177-
{
170+
protected function get_states(
171+
ReflectionClass $reflection,
172+
ActorReference $reference,
173+
DaprClient $client
174+
): array {
178175
$constructor = $reflection->getMethod('__construct');
179-
$states = [];
176+
$states = [];
180177
foreach ($constructor->getParameters() as $parameter) {
181178
$type = $parameter->getType();
182179
if ($type instanceof ReflectionNamedType) {
@@ -185,10 +182,16 @@ protected function get_states(ReflectionClass $reflection, string $dapr_type, st
185182
$reflected_type = new ReflectionClass($type_name);
186183
if ($reflected_type->isSubclassOf(ActorState::class)) {
187184
$state = $this->container->make($type_name);
188-
$this->begin_transaction($state, $reflected_type, $dapr_type, $id);
185+
$this->begin_transaction(
186+
$state,
187+
$reflected_type,
188+
$reference,
189+
$client,
190+
$this->get_cache_for_actor($reference, $type_name)
191+
);
189192

190193
$states[$parameter->name] = $state;
191-
$this->logger?->debug('Found state {t}', ['t' => $type_name]);
194+
$this->client->logger?->debug('Found state {t}', ['t' => $type_name]);
192195
}
193196
}
194197
}
@@ -211,24 +214,31 @@ protected function get_states(ReflectionClass $reflection, string $dapr_type, st
211214
protected function begin_transaction(
212215
ActorState $state,
213216
ReflectionClass $reflected_type,
214-
string $dapr_type,
215-
string $actor_id,
217+
ActorReference $reference,
218+
DaprClient $client,
219+
CacheInterface $cache,
216220
?ReflectionClass $original = null
217221
): void {
218222
if ($reflected_type->name !== ActorState::class) {
219223
$this->begin_transaction(
220224
$state,
221225
$reflected_type->getParentClass(),
222-
$dapr_type,
223-
$actor_id,
226+
$reference,
227+
$client,
228+
$cache,
224229
$original ?? $reflected_type
225230
);
226231

227232
return;
228233
}
229234
$begin_transaction = $reflected_type->getMethod('begin_transaction');
230235
$begin_transaction->setAccessible(true);
231-
$begin_transaction->invoke($state, $dapr_type, $actor_id);
236+
$begin_transaction->invoke($state, $reference, $client, $cache);
237+
}
238+
239+
protected function get_cache_for_actor(ActorReference $reference, string $type_name): CacheInterface
240+
{
241+
return $this->factory->make(CacheInterface::class, ['reference' => $reference, 'state_name' => $type_name]);
232242
}
233243

234244
/**
@@ -243,23 +253,23 @@ protected function begin_transaction(
243253
* @throws DependencyException
244254
* @throws NotFoundException
245255
*/
246-
protected function get_actor(ReflectionClass $reflection, string $dapr_type, string $id, array $states): IActor
256+
protected function get_actor(ReflectionClass $reflection, ActorReference $reference, array $states): IActor
247257
{
248-
$states['id'] = $id;
249-
$this->container->set(ActorReference::class, new ActorReference($id, $dapr_type));
250-
$actor = $this->factory->make($reflection->getName(), $states);
251-
$activation_tracker = hash('sha256', $dapr_type.$id);
258+
$states['id'] = $reference->get_actor_id();
259+
$this->container->set(ActorReference::class, $reference);
260+
$actor = $this->factory->make($reflection->getName(), $states);
261+
$activation_tracker = hash('sha256', $reference->get_actor_type() . $reference->get_actor_id());
252262
$activation_tracker = rtrim(
253-
sys_get_temp_dir(),
254-
DIRECTORY_SEPARATOR
255-
).DIRECTORY_SEPARATOR.'dapr_'.$activation_tracker;
263+
sys_get_temp_dir(),
264+
DIRECTORY_SEPARATOR
265+
) . DIRECTORY_SEPARATOR . 'dapr_' . $activation_tracker;
256266

257267
$is_activated = file_exists($activation_tracker);
258268

259-
if ( ! $is_activated) {
260-
$this->logger?->info(
269+
if (!$is_activated) {
270+
$this->client->logger?->info(
261271
'Activating {type}||{id}',
262-
['type' => $dapr_type, 'id' => $id]
272+
['type' => $reference->get_actor_type(), 'id' => $reference->get_actor_id()]
263273
);
264274
touch($activation_tracker);
265275
$actor->on_activation();

0 commit comments

Comments
 (0)