Skip to content
This repository has been archived by the owner on Apr 12, 2019. It is now read-only.

JAVA_HOME not set #7

Closed
tormodu opened this issue Dec 11, 2014 · 4 comments
Closed

JAVA_HOME not set #7

tormodu opened this issue Dec 11, 2014 · 4 comments

Comments

@tormodu
Copy link
Contributor

tormodu commented Dec 11, 2014

I added some more logging and have found the reason why Elastic search does not start on new instances during scaling.
When Elastic Search is started it complains about missing JAVA_HOME variable. This variable has been set, but is somehow not picked up.
By restarting the worker role everything works as expected.

By starting the Elastic Search process like this:

 _process = new Process();
 _process.StartInfo = new ProcessStartInfo
  {
        FileName = startupScript,
        UseShellExecute = false,
        RedirectStandardOutput = true
    };
_process.Start();
_process.BeginOutputReadLine();

and the capturing the standardoutput like this:

_process.OutputDataReceived +=
   delegate(object sender, DataReceivedEventArgs args)
    {
         var output = args.Data;
          if (output == "JAVA_HOME environment variable must be set!")
          {
               Trace.TraceInformation("JAVA_HOME not set restarting");
                throw new Exception("JAVA_HOME not set restarting");
           }
      };

It is possible to listen for the text "JAVA_HOME environment variable must be set!" which elasticsearch.bat file outputs and restart the worker role by throwing an exception. Not very elegant, but it works as a temporary solution.

@garvincasimir
Copy link
Owner

I have a bunch of unsynced commits that makes a bunch of improvements and resolves the other issues. Thanks for finding this one. You saved me a bunch of debugging time. I will change the code so that JAVA_HOME is provided as an argument to the elasticsearch process. My ideal would be to install java to a custom directory and not have to mess with environment variables and the registry. The only problem with that approach is, in my view, the best way to know if java is installed is to read the registry. Lastly for the java and elasticsearch processes I am capturing StandardError once the process has ended. I will look into enabling events for trace information like you have.

@garvincasimir
Copy link
Owner

I was wrong. Don't know why I thought the batch file accepted java_home as an argument. However, I should be able to set custom Environment variables for the process and achieve the same effect. The reason elasticsearch isn't exiting is a "pause" in the batch script when it encounters the no JAVA_HOME error.

@garvincasimir
Copy link
Owner

So the JAVA_HOME issue is related to how processes get their environment variables. Apparently, they inherit them from their calling process. When you call SetEnviromentVariable a broadcast message (WM_SETTINGCHANGE) is sent to all processes informing them of the change. This is an async processes so even if your process handles that message, your operation which depends on the updated value could very well occur before this message is received. You can read more here.

@tormodu
Copy link
Contributor Author

tormodu commented Dec 15, 2014

By setting the JAVA_HOME variable like this:

Environment.SetEnvironmentVariable(JAVA_HOME, javaHome, EnvironmentVariableTarget.Process);

The variable gets picked up immediately and a recycle is not needed

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants