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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion library/iFixit/Matryoshka/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ public function getAndSet($key, callable $callback, int $expiration = 0,
$value = $callback();

if ($value !== self::MISS) {
$this->set($key, $value, $expiration);
if ($reset) {
$this->set($key, $value, $expiration);
} else if (!$this->add($key, $value, $expiration)) {

@danielbeardsley danielbeardsley Apr 7, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This does as claimed, but I feel like this could cause problems with some usage patterns:

  • DCG (where we short-circuit all GETs and return MISS)
    • This change would fail to update the cache
  • McRouter: how does it handle add() when one instance has a value and the other doesn't?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That seems like a usage error. DCG doesn't use the reset option then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see, DCG is a backend.

Seems like we'd want to sub away set and add there, as we do in the new tests here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Wow that's confusing, no, im incorrect. DCG is intended to repopulate the cache.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Help me understand the failure mode you're describing? I'm reviewing DCG, and it seems like it would continue to work as expected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I dont see any problems with DCG or Mcrouter. This should help fix the race condition in both. And behavior is otherwise preserved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Help me understand the failure mode you're describing? I'm reviewing DCG, and it seems like it would continue to work as expected.

I think the scenario is:

  • Prior to DCG request, getAndSet(K, () => V1) sets K ⇒ V1
  • On DCG request, we try to getAndSet(K, () => V2):
    • get(K) => MISS because of the DCG backend wrap on get
    • Because of MISS, we run $value = $callback() and get V2
    • Not $reset, so we try to add(K, V2, TTL)
    • But K is in the cache, so add fails
    • So we get(K) => V1 and return V1

We expected to write V2 to the cache and return it in the DCG request (simulating the cache actually starting empty), but instead we wrote nothing and got back V1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right! I see the concern. Missed that add will see the current value.

Doesn't that mean we need DCG to explicitly reset?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Found this too:

/**
* Override the `set` method. Use `add` which is synchronous to detect
* `set` over-top of existing keys. Delete and reset them to
* enforce consistency.
*/
public function set($key, $value, $expiration = 0) {
$addReturn = $this->memcached->add($key, $value, $expiration);

$value = $this->get($key) ?? $value;
}
}
}

Expand Down
19 changes: 13 additions & 6 deletions library/iFixit/Matryoshka/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ public function getPrefix() {

public function getScopePrefix(bool $reset = false, bool $generateOnMiss = true) {
if ($this->scopePrefix === null || $reset) {
$scopeValue = $reset ? self::MISS : $this->backend->get($this->getScopeKey());
if ($scopeValue === self::MISS) {
if ($generateOnMiss) {
$scopeValue = substr(md5(microtime() . $this->scopeName), 0, 16);
$this->backend->set($this->getScopeKey(), $scopeValue);
} else {
if (!$reset && !$generateOnMiss) {
$scopeValue = $this->backend->get($this->getScopeKey());
if ($scopeValue === self::MISS) {
return self::MISS;
}
} else {
$scopeValue = $this->backend->getAndSet(
$this->getScopeKey(),
function() {
return substr(md5(microtime() . $this->scopeName), 0, 16);
},
0,
$reset
);
}

$this->scopePrefix = "{$scopeValue}-";
}

Expand Down
57 changes: 57 additions & 0 deletions tests/AbstractBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,42 @@ function() { return null; }, 0, $reset = true);
$this->assertSame($value, $backend->get($key));
}

public function testGetAndSetReturnsComputedValue() {
$backend = new class extends Matryoshka\Ephemeral {
public function add($key, $value, $expiration = 0) {
return false;
}
};

[$key] = $this->getRandomKeyValue();
$computedValue = 'computed';

$result = $backend->getAndSet($key, function() use ($computedValue) {
return $computedValue;
});

$this->assertSame($computedValue, $result);
}

public function testGetAndSetConcurrentInitUsesFirstWriter() {
$racing = new RacingBackend($this->getBackend());
[$key] = $this->getRandomKeyValue();

$firstWriterValue = 'first-writer';
$secondWriterValue = 'second-writer';

$racing->afterNextGet(function($key) use ($racing, $firstWriterValue) {
$racing->set($key, $firstWriterValue);
});

$result = $racing->getAndSet($key, function() use ($secondWriterValue) {
return $secondWriterValue;
});

$this->assertSame($firstWriterValue, $result);
$this->assertSame($firstWriterValue, $racing->get($key));
}

public function testgetAndSetMultiple() {
$backend = $this->getBackend();
list($key1, $value1, $id1) = $this->getRandomKeyValueId();
Expand Down Expand Up @@ -490,6 +526,27 @@ protected function isCharExemptFromKeyEquivalence($char) {
}
}

/**
* Simulates a concurrent writer on a shared backend by injecting
* behavior between get() returning and the caller acting on the result.
*/
class RacingBackend extends Matryoshka\BackendWrap {
private $afterNextGet = null;

public function afterNextGet(callable $fn) {
$this->afterNextGet = $fn;
}

public function get($key) {
$result = $this->backend->get($key);
if ($fn = $this->afterNextGet) {
$this->afterNextGet = null;
$fn($key);
}
return $result;
}
}

// Exposes the array of cached values.
class TestEphemeral extends Matryoshka\Ephemeral {
public function getCache() {
Expand Down
72 changes: 72 additions & 0 deletions tests/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,76 @@ public function testAbsoluteKey() {

$this->assertEquals($scopedCache->getScopePrefix() . $key, $scopedCache->getAbsoluteKey($key));
}

public function testConcurrentPrefixInitUsesFirstWriter() {
$racing = new RacingBackend(new Matryoshka\Ephemeral());
$scope = new Matryoshka\Scope($racing, 'test-scope');

$competitorPrefix = 'competitor-won';

$racing->afterNextGet(function($key) use ($racing, $competitorPrefix) {
$racing->set($key, $competitorPrefix);
});

$prefix = $scope->getScopePrefix();

$this->assertSame("{$competitorPrefix}-", $prefix);
$this->assertSame($competitorPrefix, $racing->get('scope-test-scope'));
}

public function testScopePrefixInitializationNoRace() {
$inner = new Matryoshka\Ephemeral();
$scope = new Matryoshka\Scope($inner, 'test-scope');

$prefix = $scope->getScopePrefix();

$this->assertNotEmpty($prefix);
$this->assertStringEndsWith('-', $prefix);
$this->assertSame($prefix, $scope->getScopePrefix());
}

public function testDeleteScopeOverwritesIntentionally() {
$inner = new Matryoshka\Ephemeral();
$scope = new Matryoshka\Scope($inner, 'test-scope');

$originalPrefix = $scope->getScopePrefix();
$scope->deleteScope();
$newPrefix = $scope->getScopePrefix();

$this->assertNotSame($originalPrefix, $newPrefix);
}

public function testConcurrentPrefixInitPreservesFirstWriterData() {
$racing = new RacingBackend(new Matryoshka\Ephemeral());
$scope = new Matryoshka\Scope($racing, 'test-scope');

$competitorPrefix = 'competitor-won';

$racing->afterNextGet(function($key) use ($racing, $competitorPrefix) {
$racing->set($key, $competitorPrefix);
$racing->set("{$competitorPrefix}-user-data", 'competitor-data');
});

$scope->getScopePrefix();

$this->assertSame('competitor-data', $scope->get('user-data'));
}

/**
* If add() fails and the re-fetch also misses (e.g. the backend lost
* the key between add and get), the generated prefix is still used.
*/
public function testScopePrefixNotEmpty() {
$backend = new class extends Matryoshka\Ephemeral {
public function add($key, $value, $expiration = 0) {
return false;
}
};
$scope = new Matryoshka\Scope($backend, 'test-scope');

$prefix = $scope->getScopePrefix();

$this->assertNotSame('-', $prefix);
$this->assertStringEndsWith('-', $prefix);
}
}
Loading