Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Offline Player Saving #4100

Merged
merged 1 commit into from
Feb 3, 2024
Merged
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
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