Initialization of a Stateful function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Initialization of a Stateful function

danp
Hi all, 

I have had some race problems in method StatefulMatchFunction.ensureInitialized.

The quick solution would be to introduce a double checked locking:

private void ensureInitialized() {
    if (!setup) {
      synchronized (this) {
        if (!setup) {
          configure(matcher);
          setup = true;
        }
      }
    }
  }

But this solution would force us to make the setup variable to volatile,
which would/can decrease the read performance in this performance critical code..

Any thoughts on how to solve this in a clever way would be appreciated. 

Regards
Dan
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of a Stateful function

Igal Shilman
Hi Dan,

The initialization and usage of a function is handled by the runtime by a single thread. 
Can you share more details? For example:
1. Any Stacktrace
2. How does your StatefulFunctionProvider looks like?
Do you cache a single function instance, or return a new one in the provider ?
3. Are your functions keep any non PersistedValue state?

Thanks,
Igal

On Saturday, January 11, 2020, Dan Pettersson <[hidden email]> wrote:
Hi all, 

I have had some race problems in method StatefulMatchFunction.ensureInitialized.

The quick solution would be to introduce a double checked locking:

private void ensureInitialized() {
    if (!setup) {
      synchronized (this) {
        if (!setup) {
          configure(matcher);
          setup = true;
        }
      }
    }
  }

But this solution would force us to make the setup variable to volatile,
which would/can decrease the read performance in this performance critical code..

Any thoughts on how to solve this in a clever way would be appreciated. 

Regards
Dan
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of a Stateful function

danp
Hi Igal,

If you have the time, the easiest way to troubleshoot and recreate this race condition is to run class FlowTest in repo:
https://github.com/danp11/stateful-functions  (I've upgraded to Java11) 

I've created a "stockmarket-example" under examples/ directory and the test is running via Harness and it expected
to print "Amazon" and "Facebook" from the class FnCacheInstrument if it runs correctly.
 
Running this test a few times should result in the error "There is already a catch all case for class...."   but when changing 
StatefulMatchFunction to include the double checked locking it works every time. I have a class hierarchy that might cause 
this problem but it would be very interesting to know if I should model the function in different (correct) way.

Let me know if you want me to write down more details what I'm trying to build and if there are better ways to structure the code. 

Thanks for taking the time to help out!

Regards
Dan

Den sön 12 jan. 2020 kl 09:11 skrev Igal Shilman <[hidden email]>:
Hi Dan,

The initialization and usage of a function is handled by the runtime by a single thread. 
Can you share more details? For example:
1. Any Stacktrace
2. How does your StatefulFunctionProvider looks like?
Do you cache a single function instance, or return a new one in the provider ?
3. Are your functions keep any non PersistedValue state?

Thanks,
Igal

On Saturday, January 11, 2020, Dan Pettersson <[hidden email]> wrote:
Hi all, 

I have had some race problems in method StatefulMatchFunction.ensureInitialized.

The quick solution would be to introduce a double checked locking:

private void ensureInitialized() {
    if (!setup) {
      synchronized (this) {
        if (!setup) {
          configure(matcher);
          setup = true;
        }
      }
    }
  }

But this solution would force us to make the setup variable to volatile,
which would/can decrease the read performance in this performance critical code..

Any thoughts on how to solve this in a clever way would be appreciated. 

Regards
Dan