[backend/core] Revert to heuristic-based home timeline query

There can be performance issues with the other query, so we have to revert this for now.
This commit is contained in:
Laura Hausmann 2024-06-26 22:55:07 +02:00
parent d0eaf13b6b
commit e65c678a35
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
4 changed files with 62 additions and 17 deletions

View file

@ -8,6 +8,7 @@ using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Extensions; using Iceshrimp.Backend.Core.Extensions;
using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Middleware;
using Iceshrimp.Backend.Core.Services;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting; using Microsoft.AspNetCore.RateLimiting;
@ -22,18 +23,19 @@ namespace Iceshrimp.Backend.Controllers.Mastodon;
[EnableRateLimiting("sliding")] [EnableRateLimiting("sliding")]
[EnableCors("mastodon")] [EnableCors("mastodon")]
[Produces(MediaTypeNames.Application.Json)] [Produces(MediaTypeNames.Application.Json)]
public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer) : ControllerBase public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer, CacheService cache) : ControllerBase
{ {
[Authorize("read:statuses")] [Authorize("read:statuses")]
[HttpGet("home")] [HttpGet("home")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<StatusEntity>))] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<StatusEntity>))]
public async Task<IActionResult> GetHomeTimeline(MastodonPaginationQuery query) public async Task<IActionResult> GetHomeTimeline(MastodonPaginationQuery query)
{ {
var user = HttpContext.GetUserOrFail(); var user = HttpContext.GetUserOrFail();
var heuristic = await QueryableTimelineExtensions.GetHeuristic(user, db, cache);
var res = await db.Notes var res = await db.Notes
.IncludeCommonProperties() .IncludeCommonProperties()
.FilterByFollowingAndOwn(user, db) .FilterByFollowingAndOwn(user, db, heuristic)
.EnsureVisibleFor(user) .EnsureVisibleFor(user)
.FilterHidden(user, db, filterHiddenListMembers: true) .FilterHidden(user, db, filterHiddenListMembers: true)
.Paginate(query, ControllerContext) .Paginate(query, ControllerContext)

View file

@ -7,6 +7,7 @@ using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Extensions; using Iceshrimp.Backend.Core.Extensions;
using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Middleware;
using Iceshrimp.Backend.Core.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting; using Microsoft.AspNetCore.RateLimiting;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -18,7 +19,7 @@ namespace Iceshrimp.Backend.Controllers;
[EnableRateLimiting("sliding")] [EnableRateLimiting("sliding")]
[Route("/api/iceshrimp/timelines")] [Route("/api/iceshrimp/timelines")]
[Produces(MediaTypeNames.Application.Json)] [Produces(MediaTypeNames.Application.Json)]
public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer) : ControllerBase public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer, CacheService cache) : ControllerBase
{ {
[HttpGet("home")] [HttpGet("home")]
[Authenticate] [Authenticate]
@ -28,8 +29,9 @@ public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer) :
public async Task<IActionResult> GetHomeTimeline(PaginationQuery pq) public async Task<IActionResult> GetHomeTimeline(PaginationQuery pq)
{ {
var user = HttpContext.GetUserOrFail(); var user = HttpContext.GetUserOrFail();
var heuristic = await QueryableTimelineExtensions.GetHeuristic(user, db, cache);
var notes = await db.Notes.IncludeCommonProperties() var notes = await db.Notes.IncludeCommonProperties()
.FilterByFollowingAndOwn(user, db) .FilterByFollowingAndOwn(user, db, heuristic)
.EnsureVisibleFor(user) .EnsureVisibleFor(user)
.FilterHidden(user, db, filterHiddenListMembers: true) .FilterHidden(user, db, filterHiddenListMembers: true)
.Paginate(pq, ControllerContext) .Paginate(pq, ControllerContext)

View file

@ -258,17 +258,6 @@ public static class QueryableExtensions
return query.Where(note => note.Visibility == visibility); return query.Where(note => note.Visibility == visibility);
} }
public static IQueryable<Note> FilterByFollowingAndOwn(
this IQueryable<Note> query, User user, DatabaseContext db
)
{
return query.Where(note => db.Followings
.Where(p => p.Follower == user)
.Select(p => p.FolloweeId)
.Concat(new[] { user.Id })
.Contains(note.UserId));
}
public static IQueryable<Note> FilterByUser(this IQueryable<Note> query, User user) public static IQueryable<Note> FilterByUser(this IQueryable<Note> query, User user)
{ {
return query.Where(note => note.User == user); return query.Where(note => note.User == user);

View file

@ -0,0 +1,52 @@
using System.Diagnostics.CodeAnalysis;
using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Services;
using Microsoft.EntityFrameworkCore;
namespace Iceshrimp.Backend.Core.Extensions;
public static class QueryableTimelineExtensions
{
// Determined empirically in 2023. Ask zotan for the spreadsheet if you're curious.
private const int Cutoff = 250;
public static IQueryable<Note> FilterByFollowingAndOwn(
this IQueryable<Note> query, User user, DatabaseContext db, int heuristic
)
{
return heuristic < Cutoff
? query.FollowingAndOwnLowFreq(user, db)
: query.Where(note => note.User == user || note.User.IsFollowedBy(user));
}
private static IQueryable<Note> FollowingAndOwnLowFreq(this IQueryable<Note> query, User user, DatabaseContext db)
=> query.Where(note => db.Followings
.Where(p => p.Follower == user)
.Select(p => p.FolloweeId)
.Concat(new[] { user.Id })
.Contains(note.UserId));
public static async Task<int> GetHeuristic(User user, DatabaseContext db, CacheService cache)
{
return await cache.FetchValueAsync($"following-query-heuristic:{user.Id}",
TimeSpan.FromHours(24), FetchHeuristic);
[SuppressMessage("ReSharper", "EntityFramework.UnsupportedServerSideFunctionCall")]
async Task<int> FetchHeuristic()
{
var latestNote = await db.Notes.OrderByDescending(p => p.Id)
.Select(p => new { p.CreatedAt })
.FirstOrDefaultAsync() ??
new { CreatedAt = DateTime.UtcNow };
//TODO: maybe we should express this as a ratio between matching and non-matching posts
return await db.Notes
.Where(p => p.CreatedAt > latestNote.CreatedAt - TimeSpan.FromDays(7))
.FollowingAndOwnLowFreq(user, db)
//.Select(p => new { })
.Take(Cutoff + 1)
.CountAsync();
}
}
}