-3

My program has to go through a learning step that takes around 15 mins to complete. The result of this learning is two Models stored into two public objects which will be then used in other classes. I put this learning step in the following method:

public void init()

So as to be performed at the start of the server. The problem is, every time the server reloads, it re-does the learning step. I have to wait another 15 minutes just to see the effects of a small change. I was wondering if there is a way to retain the value of some objects throughout the running of the program and the server. Here is my code:

public static Model model1;
public static Model model2;

@Override
public void init()
{
    model1= readModel(source1)
    model2= readModel(source2)
}

PS. I am using Servlets with JSP pages and Tomcat Server.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
M20
  • 1,032
  • 2
  • 15
  • 34
  • Is that possible to serialize the model to disk once ready? – Siddharth Tyagi Feb 03 '17 at 00:46
  • Tried it, did not work. First it shows NotSerializableException.. then I implemented the interface Serializable in the model class. But still getting the same error. The model I am reading weights more than 5 GBs and the serialised file I'm getting is around 1KB. – M20 Feb 04 '17 at 12:55
  • are you using a standard existing library to train these models or is this some of your hand written logic? The reason for question being most machine learning libraries have standard model exports for binary model. If it is a library such as R or weka maybe you can use their exporting capabilities to dump the model on to disk. – Siddharth Tyagi Feb 05 '17 at 22:41

3 Answers3

-1

As a general solution, I would suggest for you to keep the learning part and the model out of service container. Possibly a different VM / process. This way you will be able to retain the model for as long as the process is required to run, independent of the state of client process that is your tomcat.

DETAILED

You can achieve this in few steps

  • First, you need to migrate model preparation and caching to a different program. This program will run as a daemon and you can use Daemon by Apache to run it as a background service
  • Second, Assuming your daemon is up and running, your model consumer can communicate with the model VM using standard protocols. The selection of protocol depends on your exact requirements. It can be an API exposed over TCP/HTTP or RMI or anything else.

ALTERNATIVELY

As I suggested in comments, you can also dump the model binary to file system once the model is trained. Cache the model on tomcat startup. The io will be much faster than learning phase.

Siddharth Tyagi
  • 395
  • 2
  • 8
  • Could you please elaborate more on how I do this, or where should I look to learn it? – M20 Feb 04 '17 at 12:51
  • RMI won't be able to send object across processes if they are not Serializable. Therefor some kind of wrapper service for either TCP/HTPP or RMI should be provided. – Serg M Ten Feb 06 '17 at 15:32
  • I think the only data transfer for this service will be an input to model and an output. The entire model will never be ported across vms, as this breaks the whole idea of having it in a different vm altogether. Considering the data types involved in both model input and output to be serialisable (which universally are), RMI can be used as good as a wrapper. – Siddharth Tyagi Feb 06 '17 at 22:14
-1

Make it a session- or application-scoped bean, as appropriate, and access it as a bean rather than a normal object.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

You could have a look here? The Idea is either save session somewhere and put your model objects there or just use Hazelcast (overkill probably :))

comdotlinux
  • 143
  • 2
  • 9