Enable debug option in Ansible/AWS scripts.

When the debug option is set to be true, for each NCDriver process:
- a debugger port is open;
- a Yourkit agent is attached and a Yourkit remote port is open.

Add -Xmx for NCDriver if the user-provided jvm.args does not contain it.

Change-Id: Idae06144ea39a942d30d6482d96c8d8f258ec783
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1700
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ian Maxon <imaxon@apache.org>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-server/src/main/opt/ansible/conf/instance_settings.yml b/asterixdb/asterix-server/src/main/opt/ansible/conf/instance_settings.yml
index 912fac8..b5cd1ae 100644
--- a/asterixdb/asterix-server/src/main/opt/ansible/conf/instance_settings.yml
+++ b/asterixdb/asterix-server/src/main/opt/ansible/conf/instance_settings.yml
@@ -43,3 +43,14 @@
 
 # The cc service command (script).
 cc: "{{ binarydir}}/bin/{{ ccbin }}"
+
+# Whether in debug mode or not.
+# If it is true, there is a debugger port open at {{ debug_port }} and there is a profiler port open
+# at {{ profiler_port }}.  Note that debug mode only works if the cluster is on Linux machines.
+debug: false
+
+# The port for debugging
+debug_port: 20001
+
+# The port for Yourkit profiling
+profiler_port: 20002
diff --git a/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy.yml b/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy.yml
index ff06614..1f513ba 100644
--- a/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy.yml
+++ b/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy.yml
@@ -27,3 +27,6 @@
   file:
     path: "{{ binarydir }}/logs"
     state: directory
+
+- include: download_yourkit.yml
+  when: debug
diff --git a/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy_all.yml b/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy_all.yml
index 663f371..c9bf695 100644
--- a/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy_all.yml
+++ b/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy_all.yml
@@ -29,5 +29,5 @@
 - hosts: all
   tasks:
     - include_vars: ../conf/instance_settings.yml
-    - include: deploy.yml
 
+    - include: deploy.yml
diff --git a/asterixdb/asterix-server/src/main/opt/ansible/yaml/download_yourkit.yml b/asterixdb/asterix-server/src/main/opt/ansible/yaml/download_yourkit.yml
new file mode 100644
index 0000000..2536c56
--- /dev/null
+++ b/asterixdb/asterix-server/src/main/opt/ansible/yaml/download_yourkit.yml
@@ -0,0 +1,24 @@
+# ------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# ------------------------------------------------------------
+
+- name: Download and unzip Yourkit
+  unarchive:
+    src: https://www.yourkit.com/download/yjp-2016.02-b46.zip
+    dest: "{{ binarydir }}"
+    remote_src: true
diff --git a/asterixdb/asterix-server/src/main/opt/ansible/yaml/start_cc.yml b/asterixdb/asterix-server/src/main/opt/ansible/yaml/start_cc.yml
index 66fd79c..a5b599c 100644
--- a/asterixdb/asterix-server/src/main/opt/ansible/yaml/start_cc.yml
+++ b/asterixdb/asterix-server/src/main/opt/ansible/yaml/start_cc.yml
@@ -22,6 +22,10 @@
     src: ../conf/instance/cc.conf
     dest: "{{ binarydir }}/cc.conf"
 
+- name: Populate debug option to the cluster configuration file "{{ binarydir }}/cc.conf"
+  shell: printf "\n[nc]\njvm.args=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address={{ debug_port }} -agentpath:{{ binarydir }}/yjp-2016.02/bin/linux-x86-64/libyjpagent.so=port={{ profiler_port }}\n" >> "{{ binarydir }}/cc.conf"
+  when: debug and ansible_distribution != "MacOSX"
+
 - name: Update cluster config
   shell: find -P "{{ binarydir }}/cc.conf" | xargs perl -pi -e 's|command=asterixnc|command={{ ncbin }}|g'
 
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-nc-service/src/main/java/org/apache/hyracks/control/nc/service/NCService.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-nc-service/src/main/java/org/apache/hyracks/control/nc/service/NCService.java
index 6b11ecc..00af38b 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-nc-service/src/main/java/org/apache/hyracks/control/nc/service/NCService.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-nc-service/src/main/java/org/apache/hyracks/control/nc/service/NCService.java
@@ -124,15 +124,20 @@
                 LOGGER.info("Using JAVA_OPTS from environment");
             } else {
                 LOGGER.info("Using default JAVA_OPTS");
-                long ramSize = ((com.sun.management.OperatingSystemMXBean) osMXBean).getTotalPhysicalMemorySize();
-                int proportionalRamSize = (int) Math.ceil(0.6 * ramSize / (1024 * 1024));
-                //if under 32bit JVM, use less than 1GB heap by default. otherwise use proportional ramsize.
-                int heapSize = "32".equals(System.getProperty("sun.arch.data.model"))
-                        ? (proportionalRamSize <= 1024 ? proportionalRamSize : 1024) : proportionalRamSize;
-                jvmargs = "-Xmx" + heapSize + "m";
+                jvmargs = "";
             }
         }
-        env.put("JAVA_OPTS", jvmargs);
+
+        // Sets up memory parameter if it is not specified.
+        if (!jvmargs.contains(" -Xmx")) {
+            long ramSize = ((com.sun.management.OperatingSystemMXBean) osMXBean).getTotalPhysicalMemorySize();
+            int proportionalRamSize = (int) Math.ceil(0.6 * ramSize / (1024 * 1024));
+            //if under 32bit JVM, use less than 1GB heap by default. otherwise use proportional ramsize.
+            int heapSize = "32".equals(System.getProperty("sun.arch.data.model"))
+                    ? (proportionalRamSize <= 1024 ? proportionalRamSize : 1024) : proportionalRamSize;
+            jvmargs = jvmargs + " -Xmx" + heapSize + "m";
+            env.put("JAVA_OPTS", jvmargs.trim());
+        }
         LOGGER.info("Setting JAVA_OPTS to " + jvmargs);
     }