Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Dapr\Actors\Generators\ExistingOnly;
use Dapr\Actors\Generators\FileGenerator;
use Dapr\Actors\Generators\ProxyFactory;
use Dapr\Actors\Internal\Caches\FileCache;
use Dapr\App;
use Dapr\DaprClient;
use Dapr\Deserialization\DeserializationConfig;
Expand Down Expand Up @@ -130,6 +131,7 @@
'dapr.actors.scan_interval' => null,
'dapr.actors.drain_timeout' => null,
'dapr.actors.drain_enabled' => null,
'dapr.actors.cache' => FileCache::class,
'dapr.http.middleware.request' => [get(Tracing::class)],
'dapr.http.middleware.response' => [get(ApplicationJson::class), get(Tracing::class)],
'dapr.port' => env('DAPR_HTTP_PORT', "3500"),
Expand Down
50 changes: 48 additions & 2 deletions src/lib/Actors/ActorRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace Dapr\Actors;

use Dapr\Actors\Internal\Caches\CacheInterface;
use Dapr\Actors\Internal\Caches\NoCache;
use Dapr\Deserialization\IDeserializer;
use Dapr\exceptions\DaprException;
use Dapr\exceptions\Http\NotFound;
use Dapr\exceptions\SaveStateFailure;
use DI\Container;
use DI\DependencyException;
use DI\FactoryInterface;
use DI\NotFoundException;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use ReflectionException;
Expand All @@ -24,10 +28,14 @@
*/
class ActorRuntime
{
#[ArrayShape(['string' => ['string' => CacheInterface::class]])]
private array $caches = [];

public function __construct(
protected LoggerInterface $logger,
protected ActorConfig $actor_config,
protected FactoryInterface $container,
protected FactoryInterface $factory,
protected Container $container,
protected IDeserializer $deserializer,
) {
}
Expand Down Expand Up @@ -64,6 +72,12 @@ public function do_method(IActor $actor, string $method, mixed $arg): mixed
public function deactivate_actor(IActor $actor, string $dapr_type): void
{
$id = $actor->get_id();
/**
* @var $cache CacheInterface
*/
foreach ($this->caches[$dapr_type.$id] as $cache) {
$cache->reset();
}

$activation_tracker = hash('sha256', $dapr_type.$id);
$activation_tracker = rtrim(
Expand Down Expand Up @@ -110,6 +124,7 @@ public function resolve_actor(string $dapr_type, string $id, callable $loan): mi
} catch (DependencyException | DaprException | NotFoundException $e) {
throw new SaveStateFailure('Failed to commit actor state', previous: $e);
}

// @codeCoverageIgnoreEnd

return $result;
Expand Down Expand Up @@ -179,6 +194,7 @@ protected function get_states(ReflectionClass $reflection, string $dapr_type, st
if (class_exists($type_name)) {
$reflected_type = new ReflectionClass($type_name);
if ($reflected_type->isSubclassOf(ActorState::class)) {
$this->setup_cache($dapr_type, $id, $type_name);
$state = $this->container->make($type_name);
$this->begin_transaction($state, $reflected_type, $dapr_type, $id);

Expand All @@ -188,10 +204,29 @@ protected function get_states(ReflectionClass $reflection, string $dapr_type, st
}
}
}
$this->teardown_cache();

return $states;
}

/**
* @param string $dapr_type
* @param string $id
* @param string $state_type
*/
private function setup_cache(string $dapr_type, string $id, string $state_type)
{
try {
$cache_type = $this->container->get('dapr.actors.cache');
} catch (DependencyException | NotFoundException) {
$this->logger->warning('No cache type found, turning off actor state cache. Set `dapr.actors.cache`');
$cache_type = NoCache::class;
}
$cache_name = $dapr_type.$id;
$this->caches[$cache_name][$state_type] ??= new $cache_type($cache_name);
$this->container->set(CacheInterface::class, $this->caches[$cache_name][$state_type]);
}

/**
* Begins an actor transaction
*
Expand Down Expand Up @@ -226,6 +261,11 @@ protected function begin_transaction(
$begin_transaction->invoke($state, $dapr_type, $actor_id);
}

private function teardown_cache()
{
$this->container->set(CacheInterface::class, null);
}

/**
* Instantiates an actor implementation
*
Expand All @@ -241,7 +281,7 @@ protected function begin_transaction(
protected function get_actor(ReflectionClass $reflection, string $dapr_type, string $id, array $states): IActor
{
$states['id'] = $id;
$actor = $this->container->make($reflection->getName(), $states);
$actor = $this->factory->make($reflection->getName(), $states);
$activation_tracker = hash('sha256', $dapr_type.$id);
$activation_tracker = rtrim(
sys_get_temp_dir(),
Expand All @@ -256,6 +296,12 @@ protected function get_actor(ReflectionClass $reflection, string $dapr_type, str
['type' => $dapr_type, 'id' => $id]
);
touch($activation_tracker);
/**
* @var $cache CacheInterface
*/
foreach ($this->caches[$dapr_type.$id] as $cache) {
$cache->reset();
}
$actor->on_activation();
}

Expand Down
Loading