-
Notifications
You must be signed in to change notification settings - Fork 42
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
feat: Add support for big segments #213
Open
keelerm84
wants to merge
7
commits into
main
Choose a base branch
from
mk/sdk-1032/big-segment-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
46af7c5
feat: Add support for big segments
keelerm84 f720ef8
chore: Pluralize big segments
keelerm84 1e777c6
Re-use cache item
keelerm84 07f8ce0
Fix possible null reference
keelerm84 3fac012
Restriction psr/cache
keelerm84 fd289c7
Fix wrong pluralize
keelerm84 bce29fc
Fix more pluralize issues
keelerm84 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
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaunchDarkly; | ||
|
||
/** | ||
* A status enum which represents the result of a Big Segment query involved in | ||
* a flag evaluation. | ||
*/ | ||
enum BigSegmentsEvaluationStatus: string | ||
{ | ||
/** | ||
* Indicates that the Big Segment query involved in the flag evaluation was | ||
* successful, and that the segment state is considered up to date. | ||
*/ | ||
case HEALTHY = 'HEALTHY'; | ||
|
||
/** | ||
* Indicates that the Big Segment query involved in the flag evaluation was | ||
* successful, but that the segment state may not be up to date. | ||
*/ | ||
case STALE = 'STALE'; | ||
|
||
/** | ||
* Indicates that Big Segments could not be queried for the flag evaluation | ||
* because the SDK configuration did not include a Big Segment store. | ||
*/ | ||
case NOT_CONFIGURED = 'NOT_CONFIGURED'; | ||
|
||
/** | ||
* Indicates that the Big Segment query involved in the flag evaluation | ||
* failed, for instance due to a database error. | ||
*/ | ||
case STORE_ERROR = 'STORE_ERROR'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaunchDarkly\Impl\BigSegments; | ||
|
||
use LaunchDarkly\BigSegmentsEvaluationStatus; | ||
|
||
class MembershipResult | ||
{ | ||
/** | ||
* @param ?array<string, bool> $membership | ||
*/ | ||
public function __construct( | ||
public readonly ?array $membership, | ||
public readonly BigSegmentsEvaluationStatus $status | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaunchDarkly\Impl\BigSegments; | ||
|
||
use DateTimeImmutable; | ||
use Exception; | ||
use LaunchDarkly\BigSegmentsEvaluationStatus; | ||
use LaunchDarkly\Impl; | ||
use LaunchDarkly\Subsystems; | ||
use LaunchDarkly\Types; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class StoreManager | ||
{ | ||
private Types\BigSegmentsConfig $config; | ||
private ?Subsystems\BigSegmentsStore $store; | ||
private Impl\BigSegments\StoreStatusProvider $statusProvider; | ||
private ?Types\BigSegmentsStoreStatus $lastStatus; | ||
private ?DateTimeImmutable $lastStatusPollTime; | ||
|
||
public function __construct(Types\BigSegmentsConfig $config, private readonly LoggerInterface $logger) | ||
{ | ||
$this->config = $config; | ||
$this->store = $config->store; | ||
$this->statusProvider = new Impl\BigSegments\StoreStatusProvider( | ||
fn () => $this->pollAndUpdateStatus(), | ||
$logger | ||
); | ||
$this->lastStatus = null; | ||
$this->lastStatusPollTime = null; | ||
} | ||
|
||
public function getStatusProvider(): Subsystems\BigSegmentStatusProvider | ||
{ | ||
return $this->statusProvider; | ||
} | ||
|
||
public function getContextMembership(string $contextKey): ?Impl\BigSegments\MembershipResult | ||
{ | ||
if ($this->store === null) { | ||
return null; | ||
} | ||
|
||
$cachedItem = null; | ||
try { | ||
$cachedItem = $this->config->cache?->getItem($contextKey); | ||
} catch (Exception $e) { | ||
$this->logger->warning("Failed to retrieve cached item for big segment", ['contextKey' => $contextKey, 'exception' => $e->getMessage()]); | ||
} | ||
/** @var ?array */ | ||
$membership = $cachedItem?->get(); | ||
|
||
if ($membership === null) { | ||
try { | ||
$membership = $this->store->getMembership(StoreManager::hashForContextKey($contextKey)); | ||
if ($this->config->cache !== null && $cachedItem !== null) { | ||
$cachedItem->set($membership)->expiresAfter($this->config->contextCacheTime); | ||
|
||
if (!$this->config->cache->save($cachedItem)) { | ||
$this->logger->warning("Failed to save Big Segment membership to cache", ['contextKey' => $contextKey]); | ||
} | ||
} | ||
} catch (Exception $e) { | ||
$this->logger->warning("Failed to retrieve Big Segment membership", ['contextKey' => $contextKey, 'exception' => $e->getMessage()]); | ||
return new Impl\BigSegments\MembershipResult(null, BigSegmentsEvaluationStatus::STORE_ERROR); | ||
} | ||
} | ||
|
||
$nextPollingTime = ($this->lastStatusPollTime?->getTimestamp() ?? 0) + $this->config->statusPollInterval; | ||
|
||
$status = $this->lastStatus; | ||
if ($this->lastStatusPollTime === null || $nextPollingTime < time()) { | ||
$status = $this->pollAndUpdateStatus(); | ||
} | ||
|
||
if ($status === null || !$status->isAvailable()) { | ||
return new Impl\BigSegments\MembershipResult($membership, BigSegmentsEvaluationStatus::STORE_ERROR); | ||
} | ||
|
||
return new Impl\BigSegments\MembershipResult($membership, $status->isStale() ? BigSegmentsEvaluationStatus::STALE : BigSegmentsEvaluationStatus::HEALTHY); | ||
} | ||
|
||
private function pollAndUpdateStatus(): Types\BigSegmentsStoreStatus | ||
{ | ||
$newStatus = new Types\BigSegmentsStoreStatus(false, false); | ||
if ($this->store !== null) { | ||
try { | ||
$metadata = $this->store->getMetadata(); | ||
$newStatus = new Types\BigSegmentsStoreStatus( | ||
available: true, | ||
stale: $metadata->isStale($this->config->staleAfter) | ||
); | ||
} catch (Exception $e) { | ||
$this->logger->warning("Failed to retrieve Big Segment metadata", ['exception' => $e->getMessage()]); | ||
} | ||
} | ||
|
||
$this->lastStatus = $newStatus; | ||
$this->statusProvider->updateStatus($newStatus); | ||
$this->lastStatusPollTime = new DateTimeImmutable(); | ||
|
||
return $newStatus; | ||
} | ||
|
||
private static function hashForContextKey(string $contextKey): string | ||
{ | ||
return base64_encode(hash('sha256', $contextKey, true)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaunchDarkly\Impl\BigSegments; | ||
|
||
use Exception; | ||
use LaunchDarkly\Subsystems; | ||
use LaunchDarkly\Types; | ||
use Psr\Log\LoggerInterface; | ||
use SplObjectStorage; | ||
|
||
class StoreStatusProvider implements Subsystems\BigSegmentStatusProvider | ||
{ | ||
private SplObjectStorage $listeners; | ||
/** | ||
* @var callable(): Types\BigSegmentsStoreStatus | ||
*/ | ||
private $statusFn; | ||
private ?Types\BigSegmentsStoreStatus $lastStatus; | ||
private LoggerInterface $logger; | ||
|
||
/** | ||
* @param callable(): Types\BigSegmentsStoreStatus $statusFn | ||
*/ | ||
public function __construct(callable $statusFn, LoggerInterface $logger) | ||
{ | ||
$this->listeners = new SplObjectStorage(); | ||
$this->statusFn = $statusFn; | ||
$this->lastStatus = null; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function attach(Subsystems\BigSegmentStatusListener $listener): void | ||
{ | ||
$this->listeners->attach($listener); | ||
} | ||
|
||
public function detach(Subsystems\BigSegmentStatusListener $listener): void | ||
{ | ||
$this->listeners->detach($listener); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function updateStatus(Types\BigSegmentsStoreStatus $status): void | ||
{ | ||
if ($this->lastStatus != $status) { | ||
$old = $this->lastStatus; | ||
$this->lastStatus = $status; | ||
|
||
$this->notify(old: $old, new: $status); | ||
} | ||
} | ||
|
||
private function notify(?Types\BigSegmentsStoreStatus $old, Types\BigSegmentsStoreStatus $new): void | ||
{ | ||
/** @var Subsystems\BigSegmentStatusListener $listener */ | ||
foreach ($this->listeners as $listener) { | ||
try { | ||
$listener->statusChanged($old, $new); | ||
} catch (Exception $e) { | ||
$this->logger->warning('A big segments status listener threw an exception', ['exception' => $e->getMessage()]); | ||
} | ||
} | ||
} | ||
|
||
public function lastStatus(): ?Types\BigSegmentsStoreStatus | ||
{ | ||
return $this->lastStatus; | ||
} | ||
|
||
public function status(): Types\BigSegmentsStoreStatus | ||
{ | ||
return ($this->statusFn)(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a little unclear in which cases null will be returned vs which cases a result with null membership and store error will come back.