Posts

Showing posts from July, 2014

Fonts for readability

Java ProcessBuilder.exec and hanging processes and silent failures

When using ProcessBuilder to start consul and then other applications, processes were silently failing and hanging.

DO NOT USE bash -i with consul. The -i is needed for some other process execs to initialize the environment.
The following command appears to complete properly.
After the processBuilder.start(), future process silently hang and fail for some unknown reason.
Execution always worked properly from eclipse, but never from command line.

Works from eclipse, but from command line caused future ProcessBuilder silent failures.
String[] commandArray = {"/bin/bash", "-i", "-c", new "/opt/consul/consul agent -server -bootstrap -data-dir /tmp/consul -ui-dir /opt/consul/consul-ui/dist/"};

Works from eclipse and command line, no future ProcessBuilder failures.
String[] commandArray = {"/bin/bash", "-c", "/opt/consul/consul agent -server -bootstrap -data-dir /tmp/consul -ui-dir /opt/consul/consul-ui/dist/"};

nginx - installation

Ubuntu 12.04
sudo apt-get install nginx will install everything, but might not be the latest version.

get the latest:
Download the latest version from http://wiki.nginx.org/Install

install missing dependencies
sudo apt-get install zlib1g-dev
sudo apt-get install libpcre3 libpcre3-dev

build and install nginx
./configure --prefix=/opt/nginx
make
sudo make install

setup some permissions - optional
cd /opt
sudo chown -R root:opt nginx
sudo chmod -R g+w nginx
cd /opt/nginx

start/stop/relolad configuration
sbin/nginx -c conf/nginx.conf
sbin/nginx -s stop
sbin/nginx -s reload

nginx - persistent back-end server connections for performance

TCP open/close is expensive and limits throughput. Clients like browsers that can utilize persistent connections (keepalive) do not present a problem. IoT/M2M devices that are low power can't use persistent connections unless they utilize a gateway/proxy.

NGINX has built-in support for handling the client TCP open/close and proxying the requests over a pool of persistent back-end server connections similar to Apache's AJP, but supposedly more efficiently.

nginx configuration

tested while watching "sudo tcpdump -i 6.lo -nnvvXS port 8181" to verify that nginx indeed does not repeatedly reestablish connections between nginx and back-end server.

http {
        ....
    upstream foo_backend {         server localhost:8181;
        # maintain a maximum of 10 idle connections to each upstream server         keepalive 10;     }
    server {         listen       8080;         server_name  localhost;
# serve anything from nginx_home/html         location / {             root   h…

Ansible

AND/OR

 - name: stop app
  shell: pidof java | xargs -r kill -9 || /bin/true
  when: pid.stdout != "" and (appUpdated|changed or configUpdated|changed)

Overriding args to roles

roles:
   - { role: app_user, name: Ian    }
   - { role: app_user, name: Terry  }
   - { role: app_user, name: Graham }
   - { role: app_user, name: John   }


Conditional roles

    - { role: hazelcast, when: target_env=='dev' } # hazelcast only required if providing service
    - { role: nginx, when: target_env=='dev' }
    - bootstrap # unconditional role

Role Dependencies

File read/write in single line no libraries

Simple single line of Java without any library dependencies to read a file into a String and write a String to a file.

Read file into String

new String(Files.readAllBytes(Paths.get(filename)));


Write String to file

Files.write(Paths.get(filename), someString.getBytes());

Java hostname

private static String getHostname()
  {
    try (BufferedInputStream in = new BufferedInputStream(Runtime.getRuntime().exec("hostname").getInputStream()))
    {
      byte[] b = new byte[256];
      in.read(b, 0, b.length); //guaranteed to read all data before returning
      return new String(b);
    }
    catch (IOException e)
    {
      String message = "Error reading hostname";
      Log.error(message);
      throw new RuntimeException(message, e);
    }
  }