You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@flink.apache.org by Navneeth Krishnan <re...@gmail.com> on 2017/12/19 23:29:41 UTC

Static Variables

Hi,

I have a requirement to initialize few guava caches per jvm and some static
helper classes. I tried few options but nothing worked. Need some help.
Thanks a lot.

1. Operator level static variables:

public static Cache<String, String> loadingCache;

public void open(Configuration parameters) throws Exception {
if (loadingCache == null)
initializeCache();
}

The cache object is null on each operator slot and it gets initialized on
every call to open method.

2. Initialize in operator class constructor:

public FlatMapFunction(ParameterTool parameterTool) {
        this. parameterTool = parameterTool;
        initializeCache();
}

The cache doesn't seem to be initialized when accessed inside the task
manager.

Thanks.

Re: Static Variables

Posted by Stefan Richter <s....@data-artisans.com>.
Hi,

I think the cause is very likely a race condition between the tasks checking and setting the static value, because tasks run in different threads. You could try to use an Atomic reference or synchronization for setting the state variable’s value.

Best,
Stefan

> Am 20.12.2017 um 00:29 schrieb Navneeth Krishnan <re...@gmail.com>:
> 
> Hi,
> 
> I have a requirement to initialize few guava caches per jvm and some static helper classes. I tried few options but nothing worked. Need some help. Thanks a lot.
> 
> 1. Operator level static variables: 
> 
> public static Cache<String, String> loadingCache;
> 
> public void open(Configuration parameters) throws Exception {
> 	if (loadingCache == null)
> 		initializeCache();
> }
> 
> The cache object is null on each operator slot and it gets initialized on every call to open method.
> 
> 2. Initialize in operator class constructor:
> 
> public FlatMapFunction(ParameterTool parameterTool) {
>         this. parameterTool = parameterTool;
>         initializeCache();
> }
> 
> The cache doesn't seem to be initialized when accessed inside the task manager.
> 
> Thanks.