suriyun-production / mmorpg-kit-docs

This is document for MMORPG KIT project (https://www.assetstore.unity3d.com/#!/content/110188?aid=1100lGeN)
https://suriyun-production.github.io/mmorpg-kit-docs
49 stars 11 forks source link

Multithreading Management #2334

Closed chumleader closed 7 months ago

chumleader commented 8 months ago

Multithreading Management Game currently throws all the threads it can throw to the cpu to process each function side by side. which will be bad in having too many networked Objects (Player / Monsters) , the game threading must be handled safely. i just added some in DatabaseManager Source and the saving/fetching data now runs smoothly. the problem now is The map server takes a lot of CPU to process all the threads.

All of these are based on having high population experience. we had 50k players in the beginning and now we only have 10% of the initial playerbase we had. we are still facing threading problems sometime

insthync commented 8 months ago

Do you have any suggestion for this, it uses Unity as a server, what do you think I could make changes?

chumleader commented 8 months ago

maybe add _lock or Semaphore in crucial processes i m still figuring out which to control the thread in MMO Kit source . In database Server, i added semaphoreslim in saving process

insthync commented 8 months ago

Where did you add it?

insthync commented 8 months ago

Try to create a pull request to https://github.com/suriyun-mmorpg/UnityMultiplayerARPG_MMO

chumleader commented 8 months ago

i haven't changed anything major in UnityMultiplayerARPG_MMO Source i just added some in warping

Reference

insthync commented 8 months ago

Where?

chumleader commented 8 months ago

i added here in public async void WarpCharacter function

and in databaseserver the whole updateCharacter function

insthync commented 8 months ago

Please attach your update character function codes.

chumleader commented 8 months ago

this function in databasemanager public override async UniTask UpdateCharacter(IPlayerCharacterData character, List<CharacterBuff> summonBuffs, List<CharacterItem> storageItems, bool deleteStorageReservation)

i am still far in thread handling

insthync commented 8 months ago

I want to know where exactly you've added semaphoreslim ?

chumleader commented 8 months ago

just plain semaphore slim

insthync commented 8 months ago

Are you really implement it ? why don't just post the codes ?

chumleader commented 8 months ago

Are you really implement it ? why don't just post the codes ?

core has been changed

insthync commented 8 months ago

How is that (core changes) related to applied semaphoreslim ?

You told me you've just put it in UpdateCharacter function, I just want to see how.

insthync commented 8 months ago

Are you really did it or you are lying me?

chumleader commented 8 months ago

BaseGameNetworkManager_PlayerActivity.cs Line 8 private SemaphoreSlim semaphoreWarp = new SemaphoreSlim(2);

 public async void WarpCharacter(WarpPortalType warpPortalType, BasePlayerCharacterEntity playerCharacterEntity, string mapName, Vector3 position, bool overrideRotation, Vector3 rotation)
        {
            await semaphoreWarp.WaitAsync();//added 2 threads per warp 
            try
            {
                 switch (warpPortalType)
                 ...              
            }
            finally { semaphoreWarp.Release(); }

        }

whats the point of lying? this is the problem in posting issues here. everything / everyone seems at fault

chumleader commented 8 months ago

databaseManger Source

MySqlDatabase_Character.cs Line 10 private SemaphoreSlim semaphoreUpdateCharacter= new SemaphoreSlim(1);

function

 public override async UniTask UpdateCharacter(IPlayerCharacterData character, List<CharacterBuff> summonBuffs, List<CharacterItem> storageItems, bool deleteStorageReservation)
        {
         await semaphoreUpdateCharacter.WaitAsync();//added single thread per warp 
            try
            {
                using (MySqlConnection connection = NewConnection())
                ...              
            }
            finally { semaphoreUpdateCharacter.Release(); }
         }
chumleader commented 8 months ago

i am still far in thread handling

i said earlier i am still far in thread handling but semaphore does it's job and a simple code as that makes my game more responsive

just plain semaphore slim

i also said it is plain semaphoreslim

chumleader commented 8 months ago

i am also still far in learning your code that's why i don't know which will be a critical section that needs thread management.

insthync commented 8 months ago

Whats the point of not posting? :)

Everyone seems at fault?, what is fault?

I know you said just plain semaphoreslim

But i told you i want to see it, why have to talk a lots,, why don't just show it.

insthync commented 8 months ago

Can you explain how it reduce cpu usage?

Callepo commented 8 months ago

BaseGameNetworkManager_PlayerActivity.cs Line 8 private SemaphoreSlim semaphoreWarp = new SemaphoreSlim(2);

 public async void WarpCharacter(WarpPortalType warpPortalType, BasePlayerCharacterEntity playerCharacterEntity, string mapName, Vector3 position, bool overrideRotation, Vector3 rotation)
        {
            await semaphoreWarp.WaitAsync();//added 2 threads per warp 
            try
            {
                 switch (warpPortalType)
                 ...              
            }
            finally { semaphoreWarp.Release(); }

        }

whats the point of lying? this is the problem in posting issues here. everything / everyone seems at fault

alot of people just say thing to say things, or just ask random bs, and over 4 years been here, he went from really nice to just tired of dumb questions and request, that would take 30 seconds to figure out.

and after he asked u like 5 times to show what u did or make a pull request, even i started thinking u was just making it up lol

but yea, its not all bad, just relax both of u lol

chumleader commented 8 months ago

Can you explain how it reduce cpu usage?

it reduces the workload of the cpu by processing each function 1 by 1, not by batch . the game now throws all it can in 1 go like if i have 30k players who requested for warp the server is now overwhelmed by the threads it generates. we tested in small scale first which has 200 players using 16 core Dedicated server. it is fine but now a thousand players makes it so unresponsive. thus giving exceptions and timeouts as the server cannot process it in time because the server had a thousand threads which makes the cpu overwhelmed. by setting up semaphore it just ran the function after the previous thread is finished.

Whats the point of not posting? :) that's why i didn't post much in issues . this is what i imagined will happen.

Everyone seems at fault?, what is fault?

I know you said just plain semaphoreslim

But i told you i want to see it, why have to talk a lots,, why don't just show it.

i am avoiding experts mocking what i have learned and laugh at my work . i am not an expert and ashamed by so little change i make that makes the experts think this is unbelievable. i made a point in semaphore and i am not making pull request as the core has been changed significantly and will make you think that i made a mess in your code. and in the above comments i posted what so little code i made.

insthync commented 8 months ago

Why you decide to close?

insthync commented 8 months ago

Who will laugh at your work?

chumleader commented 8 months ago

Who will laugh at your work?

there are too many. but staying on topic. I or we really need to limit the threads of map server I do not have an idea where in your code. but it seems like we need to limit the crucial functions in my game the players had a hard time in moving when overwhelmed the server can not synchronize the position in time because of too many requests. and I don't know where to start . My idea is the NavMeshEntityMovement but I do not know which is which. that's why I am giving myself time to learn your code. You on the other hand knows which needs to not be processed by batch . so you should set a thread limit to each function. I only changed the 2 functions above to limit batch execution

insthync commented 8 months ago

All Unity components (with no jobs implemented) will be running on a single thread, in this project, I do not intend to develop it with multiple threading in mind, I know that LiteNetLib works multithreaded, I don't know about MySQLConnector (and other libraries), an async functions are runs on a single thread. I think using semaphore for the UpdateCharacter function makes sense, but you should try to increase the number of concurrent.

insthync commented 8 months ago

But I still don't get why you closed issues posts. I can't track whats I have to do correctly if you close it.

shubhank008 commented 8 months ago

Can you explain how it reduce cpu usage?

it reduces the workload of the cpu by processing each function 1 by 1, not by batch . the game now throws all it can in 1 go like if i have 30k players who requested for warp the server is now overwhelmed by the threads it generates. we tested in small scale first which has 200 players using 16 core Dedicated server. it is fine but now a thousand players makes it so unresponsive. thus giving exceptions and timeouts as the server cannot process it in time because the server had a thousand threads which makes the cpu overwhelmed. by setting up semaphore it just ran the function after the previous thread is finished.

Whats the point of not posting? :) that's why i didn't post much in issues . this is what i imagined will happen.

Everyone seems at fault?, what is fault? I know you said just plain semaphoreslim But i told you i want to see it, why have to talk a lots,, why don't just show it.

i am avoiding experts mocking what i have learned and laugh at my work . i am not an expert and ashamed by so little change i make that makes the experts think this is unbelievable. i made a point in semaphore and i am not making pull request as the core has been changed significantly and will make you think that i made a mess in your code. and in the above comments i posted what so little code i made.

Pardon my ignorance about this, but won't limiting concurrent requests ultimately cause delays for those in-game activities ?
Taking your example, from 30k players, if 1k try to warp, I get that it tries to do it at once and causes the overload/timeouts.
But won't the above approach by limiting threads to 2 (from above code example) or even 50, mean that most of the other players keep waiting for their turn.

Will the concurrent thread value/limit to use in Semaphore simply rely on trial and error by seeing how much you can push the server/connections or is there any golden value, like 50 ?

Secondly, does it make sense to apply Semaphoreslim to async/BG tasks as well ? As a random example, the ReadCharacters() database call, would this help in better DB performance as well ? @insthync @Callepo gurus

var semaphore = new SemaphoreSlim(MaxConcurrentCalls);
        var tasks = new List<Task>();
        for (int i = 0; i < charactersRead.Count; i++)
        {
            await semaphore.WaitAsync();
            try
            {
                tasks.Add(ReadCharacter(charactersRead[i]));
            }
            finally
            {
                semaphore.Release();
            }
        }
        await Task.WhenAll(tasks);

^^Random, probably broken example code, just to understand.

Trying to understand, this do sound good, although