Author: challngr
Date: Fri Mar 28 15:45:21 2014
New Revision: 1582780

URL: http://svn.apache.org/r1582780
Log:
UIMA-3704 Add last-use timestamp to meta props so idle services can be 
identified.

Modified:
    
uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/AServicePing.java
    
uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/UimaAsPing.java
    
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/PingDriver.java
    
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/Pong.java
    
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServicePingMain.java
    
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServiceSet.java

Modified: 
uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/AServicePing.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/AServicePing.java?rev=1582780&r1=1582779&r2=1582780&view=diff
==============================================================================
--- 
uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/AServicePing.java
 (original)
+++ 
uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/AServicePing.java
 Fri Mar 28 15:45:21 2014
@@ -36,6 +36,10 @@ public abstract class AServicePing
     protected int failure_window_period = 30;   // 30 minutes. overridden at 
first ping
     protected int failure_window_size = failure_window_period;  // assume 1 
ping per minute
     protected int monitor_rate = 1;             // ping rate, in minutes, min 
1 used for calculations
+    protected boolean autostart_enabled;        // indicates whether autostart 
is currently enable for this pinger
+    protected long last_use = 0;                // From SM on init, the last 
known usage of this service
+                                                // according to the meta file. 
 During runtime, implementors
+                                                // may update update it which 
causes the meta to be updated.
 
     protected boolean log_enabled = false;
     protected long service_id = 0;    
@@ -43,6 +47,8 @@ public abstract class AServicePing
     protected Map<String, Object> smState;
     protected Map<String, Object> initializationState;
 
+    private boolean autostart = true;
+
     protected org.apache.uima.ducc.common.utils.DuccLogger duccLogger = 
         
org.apache.uima.ducc.common.utils.DuccLogger.getLogger(this.getClass().getName(),
 "PING");     
 
@@ -84,6 +90,8 @@ public abstract class AServicePing
         log_enabled           = (Boolean) initializationState.get("do-log");   
     
         failure_max           = (Integer) 
initializationState.get("failure-max");        
         failure_window_period = (Integer) 
initializationState.get("failure-window");
+        autostart_enabled     = (Boolean) 
initializationState.get("autostart-enabled");
+        last_use              = (Long)    initializationState.get("last-use");
 
         double  calls_per_minute = 60000.00 / monitor_rate;
         failure_window_size = (int) ( ((double)failure_window_period) * 
calls_per_minute);
@@ -135,9 +143,45 @@ public abstract class AServicePing
      */
     public Long[] getDeletions()
     {
-        return null;
+        return null;   
+    }
+
+    /**
+     * Pingers may disable autostart which will allow instances to shrink to 0.
+     */
+    public final void disableAutostart()
+    {
+        this.autostart = false;
+    }
+    
+    /**
+     * Pingers may enable autostart which will prevent instances from going to 
0.
+     *
+     * Note that if the instances went to 0 before this is set the pinger is 
about to
+     * be stopped.  The service will have to be started to get the pinger 
restarted.
+     */
+    public final void enableAutostart()
+    {
+        this.autostart = true;
+    }
+    
+    public final boolean isAutostart()
+    {
+        return this.autostart;
+    }
+
+    /**
+     * Pingers may track when a service was last used.  If set to
+     * non-zero this is the time and date of last use, converted to
+     * milliseconds, as returned by System.getTimeMillis().  Its value is 
always 
+     * set into the meta file for the pinger on each ping.
+     */
+    public long getLastUse()
+    {
+        return 0;
     }
 
+
     private String fmtArray(int[] array)
     {
         Object[] vals = new Object[array.length];

Modified: 
uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/UimaAsPing.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/UimaAsPing.java?rev=1582780&r1=1582779&r2=1582780&view=diff
==============================================================================
--- 
uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/UimaAsPing.java
 (original)
+++ 
uima/sandbox/uima-ducc/trunk/uima-ducc-cli/src/main/java/org/apache/uima/ducc/cli/UimaAsPing.java
 Fri Mar 28 15:45:21 2014
@@ -61,7 +61,7 @@ public class UimaAsPing
     String nodeIp;
     String pid;
     boolean gmfail = false;
-    
+
     public UimaAsPing()
     {
     }
@@ -134,12 +134,26 @@ public class UimaAsPing
             monitor.collect();
             stats.setHealthy(true);       // this pinger defines 'healthy' as
                                           // 'service responds to get-meta and 
broker returns jmx stats'
+
+            long cc = monitor.getProducerCount();
+            if ( cc > 0 ) {
+                last_use = System.currentTimeMillis();
+            }
+
         } catch ( Throwable t ) {
             stats.setHealthy(false);
             monitor.setJmxFailure(t.getMessage());
         }
     }
 
+    /**
+     * Override from AServicePing
+     */
+    public long getLastUse()
+    {
+        return last_use;
+    }
+
     public IServiceStatistics getStatistics()
     {
         String methodName = "getStatistics";

Modified: 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/PingDriver.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/PingDriver.java?rev=1582780&r1=1582779&r2=1582780&view=diff
==============================================================================
--- 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/PingDriver.java
 (original)
+++ 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/PingDriver.java
 Fri Mar 28 15:45:21 2014
@@ -327,6 +327,7 @@ class PingDriver
         if ( service_statistics == null ) {
             logger.error(methodName, sset.getId(), "Service statics are 
null!");
             errors++;
+            return;         // always a pinger error, don't let pinger affect 
anything
         } else {
             if ( service_statistics.isAlive() ) {
                 pingState = ServiceState.Available;
@@ -340,6 +341,12 @@ class PingDriver
             }
         }
 
+        // maybe it was turned off
+        sset.setAutostart( response.isAutostart() );
+
+        // when was the service last used?
+        sset.setLastUse( response.getLastUse() );
+
         //
         // Must cap additions and deletions at some reasonable value in case 
the monitor is too agressive or in error.
         // -- additions capped at global installation max from 
ducc.sm.instance.max
@@ -407,19 +414,22 @@ class PingDriver
      */
     String setCommonInitProperties(Map<String, Object>  props)
     {
-        props.put("monitor-rate"    , meta_ping_rate);
-        props.put("service-id"      , sset.getId().getFriendly());
-        props.put("failure-max"     , failure_max);
-        props.put("failure-window"  , failure_window);
-        props.put("do-log"          , do_log);
+        props.put("monitor-rate"      , meta_ping_rate);
+        props.put("service-id"        , sset.getId().getFriendly());
+        props.put("failure-max"       , failure_max);
+        props.put("failure-window"    , failure_window);
+        props.put("do-log"            , do_log);
+        props.put("autostart-enabled" , sset.isAutostart());
+        props.put("last-use"          , sset.getLastUse());
 
         StringBuffer buf = new StringBuffer();
-        buf.append("monitor-rate="  ); buf.append(Integer.toString 
(meta_ping_rate));             buf.append(",");
-        buf.append("service-id="    ); buf.append(Long.toString    
(sset.getId().getFriendly())); buf.append(",");
-        buf.append("failure-max="   ); buf.append(Integer.toString 
(failure_max));                buf.append(",");
-        buf.append("failure-window="); buf.append(Integer.toString 
(failure_window));             buf.append(",");
-        buf.append("do-log=");         buf.append(Boolean.toString (do_log)); 
-
+        buf.append("monitor-rate="     ); buf.append(Integer.toString 
(meta_ping_rate));             buf.append(",");
+        buf.append("service-id="       ); buf.append(Long.toString    
(sset.getId().getFriendly())); buf.append(",");
+        buf.append("failure-max="      ); buf.append(Integer.toString 
(failure_max));                buf.append(",");
+        buf.append("failure-window="   ); buf.append(Integer.toString 
(failure_window));             buf.append(",");
+        buf.append("do-log="           ); buf.append(Boolean.toString 
(do_log));                     buf.append(",");
+        buf.append("autostart-enabled="); buf.append(Boolean.toString 
(sset.isAutostart()));         buf.append(",");
+        buf.append("last-use="         ); buf.append(Long.toString    
(sset.getLastUse()));
         return buf.toString();
     }
 
@@ -473,10 +483,12 @@ class PingDriver
                 pinger.setSmState(props);
                 Pong pr = new Pong();
 
-                pr.setStatistics(pinger.getStatistics());
-                pr.setAdditions (pinger.getAdditions());
-                pr.setDeletions (pinger.getDeletions());
-                pr.setExcessiveFailures(pinger.isExcessiveFailures());
+                pr.setStatistics       ( pinger.getStatistics()       );
+                pr.setAdditions        ( pinger.getAdditions()        );
+                pr.setDeletions        ( pinger.getDeletions()        );
+                pr.setExcessiveFailures( pinger.isExcessiveFailures() );
+                pr.setAutostart        ( pinger.isAutostart()         );
+                pr.setLastUse          ( pinger.getLastUse()          );
 
                 handleResponse(pr);
                 if ( errors > error_threshold ) {
@@ -553,6 +565,8 @@ class PingDriver
         }
 
         arglist.add(System.getProperty("ducc.jvm"));
+        arglist.add("-DSM_MONITOR=T");
+
         if ( jvm_args != null ) {
             for ( String s : jvm_args) {
                 arglist.add(s);

Modified: 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/Pong.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/Pong.java?rev=1582780&r1=1582779&r2=1582780&view=diff
==============================================================================
--- 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/Pong.java
 (original)
+++ 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/Pong.java
 Fri Mar 28 15:45:21 2014
@@ -34,6 +34,8 @@ public class Pong
     int additions;
     Long[] deletions;
     boolean excessiveFailures = false;
+    boolean autostart = true;
+    long last_use = 0l;
 
     public Pong()
     {
@@ -78,4 +80,24 @@ public class Pong
     {
        return this.excessiveFailures;
     }
+
+    public void setAutostart(boolean a)
+    {
+        this.autostart = a;
+    }
+
+    public boolean isAutostart()
+    {
+        return autostart;
+    }
+
+    public void setLastUse(Long lu)
+    {
+        this.last_use = lu;
+    }
+
+    public long getLastUse()
+    {
+        return this.last_use;
+    }
 }

Modified: 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServicePingMain.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServicePingMain.java?rev=1582780&r1=1582779&r2=1582780&view=diff
==============================================================================
--- 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServicePingMain.java
 (original)
+++ 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServicePingMain.java
 Fri Mar 28 15:45:21 2014
@@ -241,6 +241,14 @@ public class ServicePingMain
         v_bool = props.getBooleanProperty(k, false);
         ret.put(k, v_bool);
 
+        k = "autostart-enabled";
+        v_bool = props.getBooleanProperty(k, false);
+        ret.put(k, v_bool);
+
+        k = "last-use";
+        v_long = props.getLongProperty(k, 0L);
+        ret.put(k, v_bool);
+
         for ( String rk : ret.keySet() ) {
             print("init:", rk, "=", ret.get(rk));
         }
@@ -360,7 +368,8 @@ public class ServicePingMain
                     pr.setAdditions      (custom.getAdditions());
                     pr.setDeletions      (custom.getDeletions());
                     pr.setExcessiveFailures(custom.isExcessiveFailures());
-
+                    pr.setAutostart      (custom.isAutostart());
+                    pr.setLastUse        (custom.getLastUse());
 
                     oos.writeObject(pr);
                     oos.flush();

Modified: 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServiceSet.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServiceSet.java?rev=1582780&r1=1582779&r2=1582780&view=diff
==============================================================================
--- 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServiceSet.java
 (original)
+++ 
uima/sandbox/uima-ducc/trunk/uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/ServiceSet.java
 Fri Mar 28 15:45:21 2014
@@ -111,6 +111,9 @@ public class ServiceSet
     boolean reference_start = false;
     // is it ping-only?
     boolean ping_only = false;
+    
+    // Date of last known use of the service.  0 means "I don't know"
+    long last_use = 0;
 
     // The number of instances to maintain live.
     int instances = 1;
@@ -181,25 +184,18 @@ public class ServiceSet
         meta_props.put("service-healthy",    "false");
         meta_props.put("service-statistics", "N/A");
 
+        last_use = meta_props.getLongProperty("last-use", 0L);
+        if ( last_use == 0 ) {
+            meta_props.put("last-use", "0");
+            meta_props.put("last-use-readable", "Unknown");
+        }
+
         if ( (!job_props.containsKey(UiOption.ProcessExecutable.pname())) && 
(service_type != ServiceType.UimaAs) ) {
             meta_props.put("ping-only", "true");
             this.ping_only = true;
         } else {
             meta_props.put("ping-only", "false");
             this.ping_only = false;
-            // implementors.clear();                   // will fill in later 
if this is hot restart
-            // String idprop  = meta.getProperty("implementors", null);
-            // if ( idprop != null ) {                        // recover 
implementors on restart, possibly
-            //     String[] ids = idprop.split("\\s");
-            //     for ( String i : ids ) {
-            //         long lid = Long.parseLong(i);
-            //         ServiceInstance si = new ServiceInstance(this);
-            //         implementors.put(lid, si);
-            //         si.setId(lid);
-            //         si.setUser(this.user);
-            //         handler.addInstance(this, si);
-            //     }
-            // }            
         }
         // caller will save the meta props, **if** the rest of registration is 
ok.
 
@@ -472,6 +468,20 @@ public class ServiceSet
         return ping_only;
     }
 
+    long getLastUse()
+    {
+        return last_use;
+    }
+
+    synchronized void setLastUse(long lu)
+    {
+        this.last_use = lu;
+        meta_props.put("last-use", Long.toString(lu));
+        if ( last_use != 0 ) {
+            meta_props.put("last-use-readable", (new Date(lu)).toString());
+        }
+    }
+
     synchronized void resetRuntimeErrors()
     {
         run_failures = 0;


Reply via email to