Skip to content

Commit 6ac88b4

Browse files
Prevent duplicate auth token activity updates
The auth token activity logic works as follows * Read auth token * Compare last activity time stamp to current time * Update auth token activity if it's older than x seconds This works fine in isolation but with concurrency that means that occasionally the same token is read simultaneously by two processes and both of these processes will trigger an update of the same row. Affectively the second update doesn't add much value. It might set the time stamp to the exact same time stamp or one a few seconds later. But the last activity is no precise science, we don't need this accuracy. This patch changes the UPDATE query to include the expected value in a comparison with the current data. This results in an affected row when the data in the DB still has an old time stamp, but won't affect a row if the time stamp is (nearly) up to date. This is a micro optimization and will possibly not show any significant performance improvement. Yet in setups with a DB cluster it means that the write node has to send fewer changes to the read nodes due to the lower number of actual changes. Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent a080c08 commit 6ac88b4

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

lib/private/Authentication/Token/PublicKeyTokenMapper.php

+39
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,43 @@ public function hasExpiredTokens(string $uid): bool {
191191

192192
return count($data) === 1;
193193
}
194+
195+
/**
196+
* Update the last activity timestamp
197+
*
198+
* In highly concurrent setups it can happen that two parallel processes
199+
* trigger the update at (nearly) the same time. In that special case it's
200+
* not necessary to hit the database with two actual updates. Therefore the
201+
* target last activity is included in the WHERE clause with a few seconds
202+
* of tolerance.
203+
*
204+
* Example:
205+
* - process 1 (P1) reads the token at timestamp 1500
206+
* - process 1 (P2) reads the token at timestamp 1501
207+
* - activity update interval is 100
208+
*
209+
* This means
210+
*
211+
* - P1 will see a last_activity smaller than the current time and update
212+
* the token row
213+
* - If P2 reads after P1 had written, it will see 1600 as last activity
214+
* and the comparison on last_activity won't be truthy. This means no rows
215+
* need to be updated a second time
216+
* - If P2 reads before P1 had written, it will see 1501 as last activity,
217+
* but the comparison on last_activity will still not be truthy and the
218+
* token row is not updated a second time
219+
*
220+
* @param IToken $token
221+
* @param int $now
222+
*/
223+
public function updateActivity(IToken $token, int $now): void {
224+
$qb = $this->db->getQueryBuilder();
225+
$update = $qb->update($this->getTableName())
226+
->set('last_activity', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
227+
->where(
228+
$qb->expr()->eq('id', $qb->createNamedParameter($token->getId(), IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
229+
$qb->expr()->lt('last_activity', $qb->createNamedParameter($now - 15, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)
230+
);
231+
$update->execute();
232+
}
194233
}

lib/private/Authentication/Token/PublicKeyTokenProvider.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,8 @@ public function updateTokenActivity(IToken $token) {
222222
/** @var PublicKeyToken $token */
223223
$now = $this->time->getTime();
224224
if ($token->getLastActivity() < ($now - $activityInterval)) {
225-
// Update token only once per minute
226225
$token->setLastActivity($now);
227-
$this->mapper->update($token);
226+
$this->mapper->updateActivity($token, $now);
228227
}
229228
}
230229

tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ public function testGenerateToken() {
100100

101101
public function testUpdateToken() {
102102
$tk = new PublicKeyToken();
103-
$tk->setLastActivity($this->time - 200);
104103
$this->mapper->expects($this->once())
105-
->method('update')
106-
->with($tk);
104+
->method('updateActivity')
105+
->with($tk, $this->time);
106+
$tk->setLastActivity($this->time - 200);
107107

108108
$this->tokenProvider->updateTokenActivity($tk);
109109

@@ -112,16 +112,15 @@ public function testUpdateToken() {
112112

113113
public function testUpdateTokenDebounce() {
114114
$tk = new PublicKeyToken();
115-
116115
$this->config->method('getSystemValueInt')
117116
->willReturnCallback(function ($value, $default) {
118117
return $default;
119118
});
120-
121119
$tk->setLastActivity($this->time - 30);
120+
122121
$this->mapper->expects($this->never())
123-
->method('update')
124-
->with($tk);
122+
->method('updateActivity')
123+
->with($tk, $this->time);
125124

126125
$this->tokenProvider->updateTokenActivity($tk);
127126
}

0 commit comments

Comments
 (0)