Skip to content

Commit 7dd7256

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 0bef570 commit 7dd7256

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

lib/private/Authentication/Token/PublicKeyTokenMapper.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,43 @@ public function hasExpiredTokens(string $uid): bool {
190190

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

lib/private/Authentication/Token/PublicKeyTokenProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,8 @@ public function updateTokenActivity(IToken $token) {
221221
/** @var PublicKeyToken $token */
222222
$now = $this->time->getTime();
223223
if ($token->getLastActivity() < ($now - $activityInterval)) {
224-
// Update token only once per minute
225224
$token->setLastActivity($now);
226-
$this->mapper->update($token);
225+
$this->mapper->updateActivity($token, $now);
227226
}
228227
}
229228

tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php

Lines changed: 6 additions & 7 deletions
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)