Skip to content

Commit

Permalink
Improve Offline Player Saving (#4100)
Browse files Browse the repository at this point in the history
PlayerManager saves offline players in bulk, once very hour. This is done in bulk to help avoid desync between offline player updates.

Player caching is typically set to 30 min.

What can happen now is that PlayerManager may save a bunch of offline players, that will then be loaded into the player biota cache (for 30 min)... then after 30 min, they all get unloaded because nothign touches those biotas, then 30 min later again the PlayerManager performs its hourly maint of bulk save, thus, re-caching all those biotas.

Caching is not necessary for these biotas.

Caching is designed for biotas that exist in an online state.
  • Loading branch information
Mag-nus authored Feb 3, 2024
1 parent 2653c9e commit 63dcd84
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Source/ACE.Database/SerializedShardDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ public void SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock
}


public void SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> biotas, Action<bool> callback)
public void SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> biotas, Action<bool> callback, bool doNotAddToCache = false)
{
_queue.Add(new Task(() =>
{
var result = BaseDatabase.SaveBiotasInParallel(biotas);
var result = BaseDatabase.SaveBiotasInParallel(biotas, doNotAddToCache);
callback?.Invoke(result);
}));
}
Expand Down
8 changes: 4 additions & 4 deletions Source/ACE.Database/ShardDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ protected bool DoSaveBiota(ShardDbContext context, Biota biota)
}
}

public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)
public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock, bool doNotAddToCache = false)
{
using (var context = new ShardDbContext())
{
var existingBiota = GetBiota(context, biota.Id);
var existingBiota = GetBiota(context, biota.Id, doNotAddToCache);

rwLock.EnterReadLock();
try
Expand All @@ -338,13 +338,13 @@ public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSli
}
}

public bool SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> biotas)
public bool SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> biotas, bool doNotAddToCache = false)
{
var result = true;

Parallel.ForEach(biotas, ConfigManager.Config.Server.Threading.DatabaseParallelOptions, biota =>
{
if (!SaveBiota(biota.biota, biota.rwLock))
if (!SaveBiota(biota.biota, biota.rwLock, doNotAddToCache))
result = false;
});

Expand Down
7 changes: 4 additions & 3 deletions Source/ACE.Database/ShardDatabaseWithCaching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public override Biota GetBiota(uint id, bool doNotAddToCache = false)
return base.GetBiota(id, doNotAddToCache);
}

public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)
public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock, bool doNotAddToCache = false)
{
CacheObject<Biota> cachedBiota;

Expand Down Expand Up @@ -162,7 +162,7 @@ public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSl

var context = new ShardDbContext();

var existingBiota = base.GetBiota(context, biota.Id);
var existingBiota = base.GetBiota(context, biota.Id, doNotAddToCache);

rwLock.EnterReadLock();
try
Expand All @@ -185,7 +185,8 @@ public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSl

if (DoSaveBiota(context, existingBiota))
{
TryAddToCache(context, existingBiota);
if (!doNotAddToCache)
TryAddToCache(context, existingBiota);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Entity/Landblock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ private void SaveDB()
AddWorldObjectToBiotasSaveCollection(wo, biotas);
}

DatabaseManager.Shard.SaveBiotasInParallel(biotas, result => { });
DatabaseManager.Shard.SaveBiotasInParallel(biotas, null);
}

private void AddWorldObjectToBiotasSaveCollection(WorldObject wo, Collection<(Biota biota, ReaderWriterLockSlim rwLock)> biotas)
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Managers/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static void SaveOfflinePlayersWithChanges()
playersLock.ExitReadLock();
}

DatabaseManager.Shard.SaveBiotasInParallel(biotas, result => { });
DatabaseManager.Shard.SaveBiotasInParallel(biotas, null, true);
}


Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/WorldObjects/Player_Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private void DeepSave(WorldObject item)
}
}

DatabaseManager.Shard.SaveBiotasInParallel(biotas, result => { });
DatabaseManager.Shard.SaveBiotasInParallel(biotas, null);
}

public enum RemoveFromInventoryAction
Expand Down

0 comments on commit 63dcd84

Please sign in to comment.