Roomvars or Atomic vars (performance)

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Leonos007
Posts: 3
Joined: 13 Sep 2021, 14:15

Roomvars or Atomic vars (performance)

Postby Leonos007 » 01 Dec 2021, 08:51

Hello,

In my situation, a runnable process is generated and is running and controlling each gameRoom and its intelligence.
So if there are 5 games started, there are 5 separate processes controlling the gameRooms.

But every time a client does something in his gameRoom and the request is sent to the smartfox server, this request does off-course does not know anything about the status/values of the gameRoom.
So for that I use the roomvariables to get the latest status/values for this particular Room.
But I do not use these roomvariables at the client.
There are a lot of calculations for every request sent and the roomvariables are read a lot.

I could also get status/valuess by using (many) Atomic arrays with the length of my gameRoom names, which are always numeric, and use the roomname as array index.
Example : public static AtomicIntegerArray testArray = new AtomicIntegerArray(99999);
a value in Roomnumber 12345 = testArray[12345]

My question is if it is is not too much load to read the roomvariables lots of time and write to them (writing is couple of times for each request) ?

Or is it better to use Atomic variables to get the latest status/values of the Rooms ?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomvars or Atomic vars (performance)

Postby Lapo » 01 Dec 2021, 13:01

Hi,
I am not sure I am following your question.
In particular this part:
But every time a client does something in his gameRoom and the request is sent to the smartfox server, this request does off-course does not know anything about the status/values of the gameRoom.


The state of a Room is normally handled by the Extension attached to the Room itself. Since the Extension life-cycle coincides with that of the Room every time a user sends a request to the Room Extension you can access your server side state.

Also I don't understand the comparison of Room Variables vs Atomic variables. Room Vars support concurrency (if that's a concern) but they do a lot more, because they are synchronized between server and clients and shared by all Users joined in a Room.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Roomvars or Atomic vars (performance)

Postby mmilen » 23 Jan 2022, 17:00

I hope my question is not highjacking this conversation.

We are about to start a major rewrite of our bridge game and part of it is better organizing our SFS RoomExtentions code. In the past, we had some issues with Room Variable synchronization between SFS servers and SFS clients. We avoided using RoomVariables for game/room state.

Code had grown and keeping state in sync between server and client with ExtentionResponse calls is becoming a pain.

So the question: Is the order of RoomVariable Update notifications guaranteed. Some Room variables can change a few times in half a second or so. We don't really care if the client will miss a beat, the important is for the client to get the latest value of the variable.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomvars or Atomic vars (performance)

Postby Lapo » 24 Jan 2022, 08:14

mmilen wrote:So the question: Is the order of RoomVariable Update notifications guaranteed. Some Room variables can change a few times in half a second or so. We don't really care if the client will miss a beat, the important is for the client to get the latest value of the variable.

Yes. Messages are delivered in the order in which they are sent by the TCP.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Roomvars or Atomic vars (performance)

Postby mmilen » 25 Jan 2022, 17:00

Scenario:

I have a Room Extension, two client requests hit it at the same time from two different threads. Request 1 starts executing some code that updates a few room properties and some room variables, using the SFSApi().

Question 1: What happens with request 2? Does it wait for request 1 to complete code execution or does it start executing the same code in parallel?

Question 2: If request 2 executes the same code, does it see room properties and variables' updates just made by Request 1. Are Room properties and variables Atomic?

Question 3: If a property is set to a complex object, is the set object and its data thread-safe?

In the past, we had run into some concurrency issues and worked them with read/write locks, but that again become a mess as the code base grew, and I'm wondering if the server handling of Room Extention requests, properties, and variables can help. Is there paperwork I can read about the thread safety of Room properties and variables?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomvars or Atomic vars (performance)

Postby Lapo » 25 Jan 2022, 18:22

Hi,
mmilen wrote:Scenario:

I have a Room Extension, two client requests hit it at the same time from two different threads. Request 1 starts executing some code that updates a few room properties and some room variables, using the SFSApi().

Question 1: What happens with request 2? Does it wait for request 1 to complete code execution or does it start executing the same code in parallel?

No there is no waiting if they are two separate threads.
RoomVariables handle concurrency correcly, meaning that two threads competing for the update of the same rVar will not cause data corruption. One of the two will update after the other.

If you need a specific outcome (i.e. Thread 1 must always update before Thread 2) then you will need to enforce the proper thread order via locking / synchronization or other concurrency tools.

Question 2: If request 2 executes the same code, does it see room properties and variables' updates just made by Request 1. Are Room properties and variables Atomic?

As mentioned above setting a RoomVariable is always thread safe. You can call that "atomic" if you will, in this context.
I am not sure what you mean by "Room Properties" though.

Question 3: If a property is set to a complex object, is the set object and its data thread-safe?

No, that's a responsibility of the object itself.

In the past, we had run into some concurrency issues and worked them with read/write locks, but that again become a mess as the code base grew, and I'm wondering if the server handling of Room Extention requests, properties, and variables can help. Is there paperwork I can read about the thread safety of Room properties and variables?

No there is no specific documentation.
I also don't think there's much need for it. Room Variables are pretty simple. I think the complexity comes from the specific use case and the multi-threaded environment. If you encounter these issues again let us know the details.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomvars or Atomic vars (performance)

Postby Lapo » 25 Jan 2022, 18:25

One last thing:
keep in mind that the call to SFSApi().setRoomVariables(...) is a pretty complex operation that involves writing a number of RoomVariables (which is always thread safe), triggering server-side events (via separate threads), and enqueuing an update packet for each interested client which in turn will go out from another thread pool.

All this work happens both sequentially and in parallel and it is by no means atomic. If you need to guaranteed atomicity of the whole operation you will need to make sure that only one thread at a time has access to the setRoomVariables(...) method, at a time. Again using locks and similar tools.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Roomvars or Atomic vars (performance)

Postby mmilen » 26 Jan 2022, 04:17

mmilen wrote:Question 2: If request 2 executes the same code, does it see room properties and variables' updates just made by Request 1. Are Room properties and variables Atomic?

Lapo wrote:As mentioned above setting a RoomVariable is always thread safe. You can call that "atomic" if you will, in this context.
I am not sure what you mean by "Room Properties" though.


In Room extentions we have code like this

Code: Select all

Room room = this.parentRoom();
this.getParentRoom().setProperty("SomePropertyName","Some value or object");

we use those for storing some defaults and state that is never broadcasted to users.
Are those thread safe setters?

Is there any performance considerations for using setProperty / getProperty over setting private variables that are never broadcast?

Request via Thread 1:
Object myobject = this.getParentRoom().getProperty("someobject", object) ;
myobject.property = 1; ( value changed from 0 to 1)

Request via Thread 2 comes in:
Object myobject = this.getParentRoom().getProperty("someobject", object) ;
what is the value for myobject.property ?
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Roomvars or Atomic vars (performance)

Postby mmilen » 26 Jan 2022, 04:39

Lapo wrote:One last thing:
keep in mind that the call to SFSApi().setRoomVariables(...) is a pretty complex operation that involves writing a number of RoomVariables (which is always thread safe), triggering server-side events (via separate threads), and enqueuing an update packet for each interested client which in turn will go out from another thread pool.

All this work happens both sequentially and in parallel and it is by no means atomic. If you need to guaranteed atomicity of the whole operation you will need to make sure that only one thread at a time has access to the setRoomVariables(...) method, at a time. Again using locks and similar tools.

Cheers


So in this scenario:

thread 1:
Lock;
update rVar to value 1;
Unlock;

thread two is waiting for the write lock
Lock
update tVar to value 0;
Unlock;

Are all clients guaranteed to end up with rVal 0?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomvars or Atomic vars (performance)

Postby Lapo » 26 Jan 2022, 08:09

mmilen wrote:In Room extentions we have code like this

Code: Select all

Room room = this.parentRoom();
this.getParentRoom().setProperty("SomePropertyName","Some value or object");

we use those for storing some defaults and state that is never broadcasted to users.
Are those thread safe setters?

Yes. Properties are backed by a ConcurrentMap:
https://docs.oracle.com/javase/8/docs/a ... shMap.html

Is there any performance considerations for using setProperty / getProperty over setting private variables that are never broadcast?

I would say no. It's mostly a matter of convenience. You can use whatever works best for you.

So in this scenario:

thread 1:
Lock;
update rVar to value 1;
Unlock;

thread two is waiting for the write lock
Lock
update tVar to value 0;
Unlock;

Are all clients guaranteed to end up with rVal 0?

Short Answer --> Yes indeed :)
Long Answer --> If you removed the lock how could you determine which thread arrives first or last? There is no way unless you have full control of the two threads and you can decide who goes first. Hence locking is the only way to control the outcome of the update.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomvars or Atomic vars (performance)

Postby Lapo » 26 Jan 2022, 08:15

About this example on properties
Request via Thread 1:
Object myobject = this.getParentRoom().getProperty("someobject", object) ;
myobject.property = 1; ( value changed from 0 to 1)

Request via Thread 2 comes in:
Object myobject = this.getParentRoom().getProperty("someobject", object) ;
what is the value for myobject.property ?

It just depends on which thread writes last :)
The thread safety provided by the ConcurrentMap only guarantees there will be no data corruption with concurrent read/writes.
Lapo

--

gotoAndPlay()

...addicted to flash games
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Roomvars or Atomic vars (performance)

Postby mmilen » 26 Jan 2022, 12:48

Last question on the topic for now:
After

Code: Select all

Object myobject = this.getParentRoom().getProperty("someobject", object) ;
myobject.property = 1; ( value changed from 0 to 1)


Do I need to do :
this.getParentRoom().setProperty("someobject", myobject) ;

Same for variables

After I get a variable that has SFSObject object value
Do I need to set a variable if I don't need to broadcast the update?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomvars or Atomic vars (performance)

Postby Lapo » 27 Jan 2022, 16:10

It's a bit of strange case, because the variable is of type Object but you're assigning a primitive value, with = 1;
RoomVars and RoomProperties work like any other key/value pair in Java.
If you store a primitive value then you will have to store the new value again, upon change, because the old copy is still stored in Room.properties.

Keep in mind that Java is a pass-by-value kind of language.

If you store an Object what you're really storing is a reference to that object (i.e. its memory address) and therefore changes to the state of that object will be reflected in Room.properties.

The only exception is if the Object itself is immutable (such as a String, for instance) in which case you will need to create a new instance with the new value and re-add it to the Room.properties.

If this makes little or no sense to you, check this out:
https://www.javatpoint.com/java-pass-by-value

Same for variables

Same for User/Room Variables.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Roomvars or Atomic vars (performance)

Postby mmilen » 27 Jan 2022, 16:24

Thanks. That clears it. I was never sure if SFSApi return a copy of an object or a reference, now it is clear.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 74 guests