Author: mona
Date: Fri Feb 15 22:22:44 2013
New Revision: 1446790

URL: http://svn.apache.org/r1446790
Log:
OOZIE-1189 add filter option to specify JobID and AppName in SLA CLI command 
(egashira via mona)

Added:
    
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/executor/jpa/SLAEventsGetForFilterJPAExecutor.java
    
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/executor/jpa/TestSLAEventsGetForFilterJPAExecutor.java
Modified:
    
oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/cli/OozieCLI.java
    
oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/client/OozieClient.java
    
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/command/coord/SLAEventsXCommand.java
    
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/servlet/SLAServlet.java
    
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/test/XDataTestCase.java
    oozie/branches/hcat-intre/docs/src/site/twiki/DG_CommandLineTool.twiki
    oozie/branches/hcat-intre/release-log.txt

Modified: 
oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/cli/OozieCLI.java
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/cli/OozieCLI.java?rev=1446790&r1=1446789&r2=1446790&view=diff
==============================================================================
--- 
oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/cli/OozieCLI.java
 (original)
+++ 
oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/cli/OozieCLI.java
 Fri Feb 15 22:22:44 2013
@@ -358,17 +358,20 @@ public class OozieCLI {
 
     /**
      * Create option for command line option 'sla'
+     *
      * @return sla options
      */
     protected Options createSlaOptions() {
         Option oozie = new Option(OOZIE_OPTION, true, "Oozie URL");
         Option start = new Option(OFFSET_OPTION, true, "start offset (default 
'0')");
         Option len = new Option(LEN_OPTION, true, "number of results (default 
'100', max '1000')");
+        Option filter = new Option(FILTER_OPTION, true, "filter of SLA 
events");
         start.setType(Integer.class);
         len.setType(Integer.class);
         Options slaOptions = new Options();
         slaOptions.addOption(start);
         slaOptions.addOption(len);
+        slaOptions.addOption(filter);
         slaOptions.addOption(oozie);
         addAuthOptions(slaOptions);
         return slaOptions;
@@ -1314,12 +1317,19 @@ public class OozieCLI {
 
     private void slaCommand(CommandLine commandLine) throws IOException, 
OozieCLIException {
         XOozieClient wc = createXOozieClient(commandLine);
+        List<String> options = new ArrayList<String>();
+        for (Option option : commandLine.getOptions()) {
+            options.add(option.getOpt());
+        }
+
         String s = commandLine.getOptionValue(OFFSET_OPTION);
         int start = Integer.parseInt((s != null) ? s : "0");
         s = commandLine.getOptionValue(LEN_OPTION);
         int len = Integer.parseInt((s != null) ? s : "100");
+        String filter = commandLine.getOptionValue(FILTER_OPTION);
+
         try {
-            wc.getSlaInfo(start, len);
+            wc.getSlaInfo(start, len, filter);
         }
         catch (OozieClientException ex) {
             throw new OozieCLIException(ex.toString(), ex);

Modified: 
oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/client/OozieClient.java
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/client/OozieClient.java?rev=1446790&r1=1446789&r2=1446790&view=diff
==============================================================================
--- 
oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/client/OozieClient.java
 (original)
+++ 
oozie/branches/hcat-intre/client/src/main/java/org/apache/oozie/client/OozieClient.java
 Fri Feb 15 22:22:44 2013
@@ -126,6 +126,10 @@ public class OozieClient {
 
     public static final String FILTER_UNIT = "unit";
 
+    public static final String FILTER_JOBID = "jobid";
+
+    public static final String FILTER_APPNAME = "appname";
+
     public static final String CHANGE_VALUE_ENDTIME = "endtime";
 
     public static final String CHANGE_VALUE_PAUSETIME = "pausetime";
@@ -1200,15 +1204,16 @@ public class OozieClient {
      * @param len number of results
      * @throws OozieClientException
      */
-    public void getSlaInfo(int start, int len) throws OozieClientException {
-        new SlaInfo(start, len).call();
+    public void getSlaInfo(int start, int len, String filter) throws 
OozieClientException {
+        new SlaInfo(start, len, filter).call();
     }
 
     private class SlaInfo extends ClientCallable<Void> {
 
-        SlaInfo(int start, int len) {
+        SlaInfo(int start, int len, String filter) {
             super("GET", RestConstants.SLA, "", 
prepareParams(RestConstants.SLA_GT_SEQUENCE_ID,
-                    Integer.toString(start), RestConstants.MAX_EVENTS, 
Integer.toString(len)));
+                    Integer.toString(start), RestConstants.MAX_EVENTS, 
Integer.toString(len),
+                    RestConstants.JOBS_FILTER_PARAM, filter));
         }
 
         @Override

Modified: 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/command/coord/SLAEventsXCommand.java
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/command/coord/SLAEventsXCommand.java?rev=1446790&r1=1446789&r2=1446790&view=diff
==============================================================================
--- 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/command/coord/SLAEventsXCommand.java
 (original)
+++ 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/command/coord/SLAEventsXCommand.java
 Fri Feb 15 22:22:44 2013
@@ -18,6 +18,7 @@
 package org.apache.oozie.command.coord;
 
 import java.util.List;
+import java.util.Map;
 
 import org.apache.oozie.ErrorCode;
 import org.apache.oozie.SLAEventBean;
@@ -25,7 +26,7 @@ import org.apache.oozie.XException;
 import org.apache.oozie.command.CommandException;
 import org.apache.oozie.command.PreconditionException;
 import org.apache.oozie.command.XCommand;
-import org.apache.oozie.executor.jpa.SLAEventsGetForSeqIdJPAExecutor;
+import org.apache.oozie.executor.jpa.SLAEventsGetForFilterJPAExecutor;
 import org.apache.oozie.service.JPAService;
 import org.apache.oozie.service.Service;
 import org.apache.oozie.service.Services;
@@ -39,50 +40,36 @@ public class SLAEventsXCommand extends X
     private long seqId = 0;
     private int maxNoEvents = 100; // Default
     private long lastSeqId = -1;
+    private final Map<String, List<String>> filter;
 
     public static final String SLA_DEFAULT_MAXEVENTS = Service.CONF_PREFIX + 
"sla.default.maxevents";
 
-    public SLAEventsXCommand(long seqId, int maxNoEvnts) {
+    public SLAEventsXCommand(long seqId, int maxNoEvnts, Map<String, 
List<String>> filter) {
         super("SLAEventsXCommand", "SLAEventsXCommand", 1);
         this.seqId = seqId;
         int sysMax = Services.get().getConf().getInt(SLA_DEFAULT_MAXEVENTS, 
1000);
         this.maxNoEvents = maxNoEvnts > sysMax ? sysMax : maxNoEvnts;
-
+        this.filter = filter;
     }
 
-    /* (non-Javadoc)
-     * @see org.apache.oozie.command.XCommand#isLockRequired()
-     */
     @Override
     protected boolean isLockRequired() {
         return false;
     }
 
-    /* (non-Javadoc)
-     * @see org.apache.oozie.command.XCommand#getEntityKey()
-     */
     @Override
     public String getEntityKey() {
         return Long.toString(seqId);
     }
 
-    /* (non-Javadoc)
-     * @see org.apache.oozie.command.XCommand#loadState()
-     */
     @Override
     protected void loadState() throws CommandException {
     }
 
-    /* (non-Javadoc)
-     * @see org.apache.oozie.command.XCommand#verifyPrecondition()
-     */
     @Override
     protected void verifyPrecondition() throws CommandException, 
PreconditionException {
     }
 
-    /* (non-Javadoc)
-     * @see org.apache.oozie.command.XCommand#execute()
-     */
     @Override
     protected List<SLAEventBean> execute() throws CommandException {
         try {
@@ -90,7 +77,7 @@ public class SLAEventsXCommand extends X
             List<SLAEventBean> slaEventList = null;
             long lastSeqId[] = new long[1];
             if (jpaService != null) {
-                slaEventList = jpaService.execute(new 
SLAEventsGetForSeqIdJPAExecutor(seqId, maxNoEvents, lastSeqId));
+                slaEventList = jpaService.execute(new 
SLAEventsGetForFilterJPAExecutor(seqId, maxNoEvents, filter, lastSeqId));
             }
             else {
                 LOG.error(ErrorCode.E0610);

Added: 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/executor/jpa/SLAEventsGetForFilterJPAExecutor.java
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/executor/jpa/SLAEventsGetForFilterJPAExecutor.java?rev=1446790&view=auto
==============================================================================
--- 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/executor/jpa/SLAEventsGetForFilterJPAExecutor.java
 (added)
+++ 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/executor/jpa/SLAEventsGetForFilterJPAExecutor.java
 Fri Feb 15 22:22:44 2013
@@ -0,0 +1,131 @@
+/**
+ * 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.
+ */
+package org.apache.oozie.executor.jpa;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import org.apache.oozie.ErrorCode;
+import org.apache.oozie.SLAEventBean;
+import org.apache.oozie.client.OozieClient;
+
+/**
+ * Load the list of SLAEventBean for a seqId and return the list.
+ */
+public class SLAEventsGetForFilterJPAExecutor implements 
JPAExecutor<List<SLAEventBean>> {
+
+    private static final String selectStr = "SELECT OBJECT(w) FROM 
SLAEventBean w WHERE w.event_id > :seqid";
+    private long seqId = -1;
+    private int len;
+    private long[] lastSeqId;
+    private Map<String, List<String>> filter;
+    private StringBuilder sb;
+
+    public SLAEventsGetForFilterJPAExecutor(long seqId, int len, Map<String, 
List<String>> filter, long[] lastSeqId) {
+        this.seqId = seqId;
+        this.len = len;
+        this.filter = filter;
+        this.lastSeqId = lastSeqId;
+        this.lastSeqId[0] = seqId;
+    }
+
+    @Override
+    public String getName() {
+        return "SLAEventsGetForJobIdJPAExecutor";
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public List<SLAEventBean> execute(EntityManager em) throws 
JPAExecutorException {
+
+        List<SLAEventBean> seBeans;
+        StringBuilder sb = new StringBuilder(selectStr);
+        Map<String, String> keyVal = new HashMap<String, String>();
+
+        for (Map.Entry<String, List<String>> entry : filter.entrySet()) {
+
+            if (entry.getKey().equals(OozieClient.FILTER_JOBID) || 
entry.getKey().equals(OozieClient.FILTER_APPNAME)) {
+                sb.append(" AND ");
+            }
+
+            if (entry.getKey().equals(OozieClient.FILTER_JOBID)) {
+                List<String> vals = entry.getValue();
+                if (vals.size() == 1) {
+                    sb.append("w.slaId = :jobid");
+                    keyVal.put("jobid", vals.get(0));
+                }
+                else {
+                    for (int i = 0; i < vals.size(); i++) {
+                        String val = vals.get(i);
+                        keyVal.put("jobid" + i, val);
+                        if (i == 0) {
+                            sb.append("w.slaId IN (:jobid" + i);
+                        }
+                        else {
+                            sb.append(",:jobid" + i);
+                        }
+                    }
+                    sb.append(")");
+                }
+            }
+            else if (entry.getKey().equals(OozieClient.FILTER_APPNAME)) {
+                List<String> vals = entry.getValue();
+                if (vals.size() == 1) {
+                    sb.append("w.appName = :appname");
+                    keyVal.put("appname", vals.get(0));
+                }
+                else {
+                    for (int i = 0; i < vals.size(); i++) {
+                        String val = vals.get(i);
+                        keyVal.put("appname" + i, val);
+                        if (i == 0) {
+                            sb.append("w.appName IN (:appname" + i);
+                        }
+                        else {
+                            sb.append(",:appname" + i);
+                        }
+                    }
+                    sb.append(")");
+                }
+            }
+        }
+        sb.append(" ORDER BY w.event_id ");
+
+        try {
+            Query q = em.createQuery(sb.toString());
+            q.setMaxResults(len);
+            q.setParameter("seqid", seqId);
+            for (Map.Entry<String, String> entry : keyVal.entrySet()) {
+                q.setParameter(entry.getKey(), entry.getValue());
+            }
+            seBeans = q.getResultList();
+            for (SLAEventBean j : seBeans) {
+                lastSeqId[0] = Math.max(lastSeqId[0], j.getEvent_id());
+            }
+
+        }
+        catch (Exception e) {
+            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
+        }
+        return seBeans;
+    }
+
+}

Modified: 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/servlet/SLAServlet.java
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/servlet/SLAServlet.java?rev=1446790&r1=1446789&r2=1446790&view=diff
==============================================================================
--- 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/servlet/SLAServlet.java
 (original)
+++ 
oozie/branches/hcat-intre/core/src/main/java/org/apache/oozie/servlet/SLAServlet.java
 Fri Feb 15 22:22:44 2013
@@ -6,9 +6,9 @@
  * 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.
@@ -18,15 +18,21 @@
 package org.apache.oozie.servlet;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
 import org.apache.oozie.ErrorCode;
 import org.apache.oozie.SLAEventBean;
+import org.apache.oozie.client.OozieClient;
 import org.apache.oozie.client.rest.RestConstants;
 import org.apache.oozie.command.CommandException;
 import org.apache.oozie.command.coord.SLAEventsXCommand;
@@ -35,18 +41,24 @@ import org.apache.oozie.util.XmlUtils;
 import org.jdom.Element;
 
 public class SLAServlet extends JsonRestServlet {
+
+    private static final Set<String> SLA_FILTER_NAMES = new HashSet<String>();
+
+    static {
+        SLA_FILTER_NAMES.add(OozieClient.FILTER_JOBID);
+        SLA_FILTER_NAMES.add(OozieClient.FILTER_APPNAME);
+    }
+
     private static final String INSTRUMENTATION_NAME = "sla";
 
     private static final JsonRestServlet.ResourceInfo RESOURCES_INFO[] = new 
JsonRestServlet.ResourceInfo[1];
 
     static {
-        RESOURCES_INFO[0] = new JsonRestServlet.ResourceInfo("", Arrays
-                .asList("GET"), Arrays.asList(
-                new JsonRestServlet.ParameterInfo(
-                        RestConstants.SLA_GT_SEQUENCE_ID, String.class, true,
-                        Arrays.asList("GET")),
-                new JsonRestServlet.ParameterInfo(RestConstants.MAX_EVENTS,
-                                                  String.class, false, 
Arrays.asList("GET"))));
+        RESOURCES_INFO[0] = new JsonRestServlet.ResourceInfo("", 
Arrays.asList("GET"), Arrays.asList(
+                new 
JsonRestServlet.ParameterInfo(RestConstants.SLA_GT_SEQUENCE_ID, String.class, 
false, Arrays
+                        .asList("GET")), new 
JsonRestServlet.ParameterInfo(RestConstants.MAX_EVENTS, String.class,
+                        false, Arrays.asList("GET")), new 
JsonRestServlet.ParameterInfo(
+                        RestConstants.JOBS_FILTER_PARAM, String.class, false, 
Arrays.asList("GET"))));
     }
 
     public SLAServlet() {
@@ -56,48 +68,51 @@ public class SLAServlet extends JsonRest
     /**
      * Return information about SLA Events.
      */
-    public void doGet(HttpServletRequest request, HttpServletResponse response)
-            throws ServletException, IOException {
+    public void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
+
+        Element eResponse = new Element("sla-message");
+        List<SLAEventBean> slaEvntList = null;
 
         try {
-            String gtSequenceNum = request
-                    .getParameter(RestConstants.SLA_GT_SEQUENCE_ID);
-            String strMaxEvents = request
-                    .getParameter(RestConstants.MAX_EVENTS);
+            stopCron();
+            String gtSequenceNum = 
request.getParameter(RestConstants.SLA_GT_SEQUENCE_ID);
+            String strMaxEvents = 
request.getParameter(RestConstants.MAX_EVENTS);
+            String filter = 
request.getParameter(RestConstants.JOBS_FILTER_PARAM);
+            Map<String, List<String>> filterList = parseFilter(filter);
+
             int maxNoEvents = 100; // Default
             XLog.getLog(getClass()).debug(
-                    "Got SLA GET request for :" + gtSequenceNum
-                            + " and max-events :" + strMaxEvents);
+                    "Got SLA GET request for :" + gtSequenceNum + " and 
max-events :" + strMaxEvents);
             if (strMaxEvents != null && strMaxEvents.length() > 0) {
                 maxNoEvents = Integer.parseInt(strMaxEvents);
             }
+
             if (gtSequenceNum != null) {
                 long seqId = Long.parseLong(gtSequenceNum);
                 stopCron();
-                SLAEventsXCommand seCommand = new SLAEventsXCommand(seqId, 
maxNoEvents);
-                List<SLAEventBean> slaEvntList = seCommand.call();
+                SLAEventsXCommand seCommand = new SLAEventsXCommand(seqId, 
maxNoEvents, filterList);
+                slaEvntList = seCommand.call();
                 long lastSeqId = seCommand.getLastSeqId();
 
-                Element eResponse = new Element("sla-message");
+                eResponse = new Element("sla-message");
                 for (SLAEventBean event : slaEvntList) {
                     eResponse.addContent(event.toXml());
                 }
                 Element eLastSeq = new Element("last-sequence-id");
                 eLastSeq.addContent(String.valueOf(lastSeqId));
                 eResponse.addContent(eLastSeq);
-                response.setContentType(XML_UTF8);
                 XLog.getLog(getClass()).debug("Writing back SLA Servlet  
Caller with last-seq-id " + lastSeqId);
                 startCron();
-                response.setStatus(HttpServletResponse.SC_OK);
-                response.getWriter().write(
-                        XmlUtils.prettyPrint(eResponse) + "\n");
             }
             else {
-                XLog.getLog(getClass()).error(
-                        "Not implemented witout gt_seq_id");
-                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST,
-                                            ErrorCode.E0401, "Not implemented 
without gtSeqID");
+                XLog.getLog(getClass()).error("gt-sequence-id parameter is not 
specified in the http request");
+                throw new 
XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0401,
+                        "gt-sequence-id parameter is not specified in the http 
request");
             }
+            startCron();
+            response.setContentType(XML_UTF8);
+            response.setStatus(HttpServletResponse.SC_OK);
+            response.getWriter().write(XmlUtils.prettyPrint(eResponse) + "\n");
         }
         catch (CommandException ce) {
             ce.printStackTrace();
@@ -111,4 +126,36 @@ public class SLAServlet extends JsonRest
         }
     }
 
+    protected Map<String, List<String>> parseFilter(String filter) throws 
ServletException {
+        Map<String, List<String>> map = new HashMap<String, List<String>>();
+        if (filter != null) {
+            StringTokenizer st = new StringTokenizer(filter, ";");
+            while (st.hasMoreTokens()) {
+                String token = st.nextToken();
+                if (token.contains("=")) {
+                    String[] pair = token.split("=");
+                    if (pair.length != 2) {
+                        throw new 
XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0401,
+                                "elements must be name=value pairs");
+                    }
+                    if (!SLA_FILTER_NAMES.contains(pair[0])) {
+                        throw new 
XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0401,
+                                "invalid/unsupported names in filter");
+                    }
+                    List<String> list = map.get(pair[0]);
+                    if (list == null) {
+                        list = new ArrayList<String>();
+                        map.put(pair[0], list);
+                    }
+                    list.add(pair[1]);
+                }
+                else {
+                    throw new 
XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0401,
+                            "elements must be name=value pairs");
+                }
+            }
+        }
+        return map;
+    }
+
 }

Added: 
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/executor/jpa/TestSLAEventsGetForFilterJPAExecutor.java
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/executor/jpa/TestSLAEventsGetForFilterJPAExecutor.java?rev=1446790&view=auto
==============================================================================
--- 
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/executor/jpa/TestSLAEventsGetForFilterJPAExecutor.java
 (added)
+++ 
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/executor/jpa/TestSLAEventsGetForFilterJPAExecutor.java
 Fri Feb 15 22:22:44 2013
@@ -0,0 +1,170 @@
+/**
+ * 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.
+ */
+package org.apache.oozie.executor.jpa;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.oozie.SLAEventBean;
+import org.apache.oozie.client.SLAEvent.Status;
+import org.apache.oozie.service.JPAService;
+import org.apache.oozie.service.Services;
+import org.apache.oozie.test.XDataTestCase;
+
+public class TestSLAEventsGetForFilterJPAExecutor extends XDataTestCase {
+
+    Services services;
+    String coordId1;
+    String coordActionId1;
+    String appName1;
+    String coordId2;
+    String coordActionId2;
+    String appName2;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        services = new Services();
+        services.init();
+        cleanUpDBTables();
+        populateDB();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        services.destroy();
+        super.tearDown();
+    }
+
+    private void populateDB() throws Exception {
+        Date current = new Date();
+        long currentTime = current.getTime();
+        coordId1 = "0000001-" + currentTime + 
"-TestSLAEventsGetForFilterJPAExecutor-C";
+        coordActionId1 = "0000001-" + currentTime + 
"-TestSLAEventsGetForFilterJPAExecutor-C@1";
+        appName1 = "app-1";
+        coordId2 = "0000002-" + currentTime + 
"-TestSLAEventsGetForFilterJPAExecutor-C";
+        coordActionId2 = "0000002-" + currentTime + 
"-TestSLAEventsGetForFilterJPAExecutor-C@1";
+        appName2 = "app-2";
+
+        addRecordToSLAEventTable(coordId1, appName1, Status.CREATED, current);
+        addRecordToSLAEventTable(coordActionId1, appName1, Status.CREATED, 
current);
+        addRecordToSLAEventTable(coordActionId1, appName1, Status.STARTED, 
current);
+        addRecordToSLAEventTable(coordActionId1, appName1, Status.SUCCEEDED, 
current);
+
+        addRecordToSLAEventTable(coordId2, appName2, Status.CREATED, current);
+        addRecordToSLAEventTable(coordActionId2, appName2, Status.CREATED, 
current);
+    }
+
+    private Map<String, List<String>> createFilterList(String name, String... 
vals) {
+        Map<String, List<String>> filterList = new HashMap<String, 
List<String>>();
+        List<String> valList = Arrays.asList(vals);
+        filterList.put(name, valList);
+        return filterList;
+
+    }
+
+    public void testGetSLAEventsForCoordJobId() throws Exception {
+
+        Map<String, List<String>> filterListJob1 = createFilterList("jobid", 
coordId1);
+
+        JPAService jpaService = Services.get().get(JPAService.class);
+        assertNotNull(jpaService);
+        SLAEventsGetForFilterJPAExecutor slaEventsGetCmd = new 
SLAEventsGetForFilterJPAExecutor(-1, 100,
+                filterListJob1, new long[1]);
+        List<SLAEventBean> list = jpaService.execute(slaEventsGetCmd);
+        assertNotNull(list);
+        assertEquals(1, list.size());
+    }
+
+    public void testGetSLAEventsForCoordActionId() throws Exception {
+        Map<String, List<String>> filterListAction1 = 
createFilterList("jobid", coordActionId1);
+        JPAService jpaService = Services.get().get(JPAService.class);
+        assertNotNull(jpaService);
+        SLAEventsGetForFilterJPAExecutor slaEventsGetCmd = new 
SLAEventsGetForFilterJPAExecutor(0, 100,
+                filterListAction1, new long[1]);
+        List<SLAEventBean> list = jpaService.execute(slaEventsGetCmd);
+        assertNotNull(list);
+        assertEquals(3, list.size());
+    }
+
+    public void testGetSLAEventsForAppName() throws Exception {
+        Map<String, List<String>> filterListApp2 = createFilterList("appname", 
appName2);
+        JPAService jpaService = Services.get().get(JPAService.class);
+        assertNotNull(jpaService);
+        SLAEventsGetForFilterJPAExecutor slaEventsGetCmd = new 
SLAEventsGetForFilterJPAExecutor(0, 100, filterListApp2,
+                new long[1]);
+        List<SLAEventBean> list = jpaService.execute(slaEventsGetCmd);
+        assertNotNull(list);
+        assertEquals(2, list.size());
+    }
+
+    public void testGetSLAEventsForOR() throws Exception {
+        Map<String, List<String>> filterList = createFilterList("jobid", 
coordId1, coordActionId1);
+        JPAService jpaService = Services.get().get(JPAService.class);
+        assertNotNull(jpaService);
+        SLAEventsGetForFilterJPAExecutor slaEventsGetCmd = new 
SLAEventsGetForFilterJPAExecutor(0, 100, filterList,
+                new long[1]);
+        List<SLAEventBean> list = jpaService.execute(slaEventsGetCmd);
+        assertNotNull(list);
+        assertEquals(4, list.size());
+    }
+
+    public void testGetSLAEventsForCombined() throws Exception {
+        Map<String, List<String>> filterList = createFilterList("jobid", 
coordId1, coordActionId1);
+        filterList.put("appname", Arrays.asList(new String[] { appName1 }));
+        JPAService jpaService = Services.get().get(JPAService.class);
+        assertNotNull(jpaService);
+        long[] lastSeqId = new long[1];
+        lastSeqId[0] = -1;
+        SLAEventsGetForFilterJPAExecutor slaEventsGetCmd = new 
SLAEventsGetForFilterJPAExecutor(0, 100, filterList,
+                lastSeqId);
+        List<SLAEventBean> list = jpaService.execute(slaEventsGetCmd);
+        assertNotNull(list);
+        assertEquals(4, list.size());
+    }
+
+    public void testGetSLAEventsWithRange() throws Exception {
+        Map<String, List<String>> filterList = new HashMap();
+        JPAService jpaService = Services.get().get(JPAService.class);
+        assertNotNull(jpaService);
+        long[] lastSeqId = new long[1];
+        lastSeqId[0] = -1;
+        SLAEventsGetForFilterJPAExecutor slaEventsGetCmd = new 
SLAEventsGetForFilterJPAExecutor(1, 3, filterList,
+                lastSeqId);
+        List<SLAEventBean> list = jpaService.execute(slaEventsGetCmd);
+        assertNotNull(list);
+        assertEquals(3, list.size());
+    }
+
+    public void testGetSLAEventsForCombinedWithRange() throws Exception {
+        Map<String, List<String>> filterList = createFilterList("jobid", 
coordId1, coordActionId1, coordId2,
+                coordActionId2);
+        JPAService jpaService = Services.get().get(JPAService.class);
+        assertNotNull(jpaService);
+        long[] lastSeqId = new long[1];
+        lastSeqId[0] = -1;
+        SLAEventsGetForFilterJPAExecutor slaEventsGetCmd = new 
SLAEventsGetForFilterJPAExecutor(1, 3, filterList,
+                lastSeqId);
+        List<SLAEventBean> list = jpaService.execute(slaEventsGetCmd);
+        assertNotNull(list);
+        assertEquals(3, list.size());
+    }
+}

Modified: 
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/test/XDataTestCase.java
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/test/XDataTestCase.java?rev=1446790&r1=1446789&r2=1446790&view=diff
==============================================================================
--- 
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/test/XDataTestCase.java
 (original)
+++ 
oozie/branches/hcat-intre/core/src/test/java/org/apache/oozie/test/XDataTestCase.java
 Fri Feb 15 22:22:44 2013
@@ -668,9 +668,21 @@ public abstract class XDataTestCase exte
      * @throws Exception thrown if unable to create sla bean
      */
     protected void addRecordToSLAEventTable(String slaId, SLAEvent.Status 
status, Date today) throws Exception {
+        addRecordToSLAEventTable(slaId, "app-name", status, today);
+    }
+
+    /**
+     * Insert sla event for testing.
+     *
+     * @param slaId sla id
+     * @param slaId app name
+     * @param status sla status
+     * @throws Exception thrown if unable to create sla bean
+     */
+    protected void addRecordToSLAEventTable(String slaId, String appName, 
SLAEvent.Status status, Date today) throws Exception {
         SLAEventBean sla = new SLAEventBean();
         sla.setSlaId(slaId);
-        sla.setAppName("app-name");
+        sla.setAppName(appName);
         sla.setParentClientId("parent-client-id");
         sla.setParentSlaId("parent-sla-id");
         sla.setExpectedStart(today);
@@ -702,6 +714,8 @@ public abstract class XDataTestCase exte
         }
     }
 
+
+
     /**
      * Insert bundle job for testing.
      *

Modified: oozie/branches/hcat-intre/docs/src/site/twiki/DG_CommandLineTool.twiki
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/docs/src/site/twiki/DG_CommandLineTool.twiki?rev=1446790&r1=1446789&r2=1446790&view=diff
==============================================================================
--- oozie/branches/hcat-intre/docs/src/site/twiki/DG_CommandLineTool.twiki 
(original)
+++ oozie/branches/hcat-intre/docs/src/site/twiki/DG_CommandLineTool.twiki Fri 
Feb 15 22:22:44 2013
@@ -59,23 +59,20 @@ usage:
                 -verbose              verbose mode
 .
       oozie jobs <OPTIONS> : jobs status
-                 -auth <arg>           select authentication type 
[SIMPLE|KERBEROS]
-                 -doas <arg>           doAs user, impersonates as the 
specified user.
-                 -filter <arg>    user=<U>;name=<N>;group=<G>;status=<S>;...
-                 -jobtype <arg>   job type ('Supported in Oozie-2.0 or later 
versions ONLY -
-                                  coordinator' or 'wf' (default))
-                 -len <arg>       number of jobs (default '100')
-                 -localtime       use local time (same as passing your time 
zone to -timezone).
-                                  Overrides -timezone option
-                 -offset <arg>    jobs offset (default '1')
-                 -oozie <arg>     Oozie URL
-                 -timezone <arg>  use time zone with the specified ID (default 
GMT).
-                                  See 'oozie info -timezones' for a list
-                 -verbose         verbose mode
+                 -auth <arg>          select authentication type 
[SIMPLE|KERBEROS]
+                 -doas <arg>          doAs user, impersonates as the specified 
user.
+                 -filter <arg>        
user=<U>;name=<N>;group=<G>;status=<S>;...
+                 -jobtype <arg>       job type ('Supported in Oozie-2.0 or 
later versions ONLY - coordinator' or 'wf' (default))
+                 -len <arg>           number of jobs (default '100')
+                 -localtime           use local time (same as passing your 
time zone to -timezone). Overrides -timezone option
+                 -offset <arg>        jobs offset (default '1')
+                 -oozie <arg>         Oozie URL
+                 -timezone <arg>      use time zone with the specified ID 
(default GMT). See 'oozie info -timezones' for a list
+                 -verbose             verbose mode
 .
       oozie admin <OPTIONS> : admin operations
-                  -auth <arg>           select authentication type 
[SIMPLE|KERBEROS]
-                  -doas <arg>           doAs user, impersonates as the 
specified user.
+                  -auth <arg>         select authentication type 
[SIMPLE|KERBEROS]
+                  -doas <arg>         doAs user, impersonates as the specified 
user.
                   -oozie <arg>        Oozie URL
                   -queuedump          show Oozie server queue elements
                   -status             show the current system status
@@ -90,6 +87,7 @@ usage:
                 -len <arg>            number of results (default '100', max 
limited by oozie server setting which defaults to '1000')
                 -offset <arg>         start offset (default '0')
                 -oozie <arg>          Oozie URL
+                -filter <arg>         
jobid=<JobID/ActionID>\;appname=<Application Name>
 .
       oozie pig <OPTIONS> -X <ARGS> : submit a pig job, everything after '-X' 
are pass-through parameters to pig
                 -auth <arg>           select authentication type 
[SIMPLE|KERBEROS]
@@ -789,6 +787,59 @@ $ oozie sla -oozie http://localhost:1100
 
 </verbatim>
 
+
+---+++ Getting information about SLA events using filter
+
+* This feature is only supported in Oozie 2.0 or later.
+
+Example:
+
+<verbatim>
+
+$ oozie sla -filter 
jobid=0000000-130130150445097-oozie-joe-C@1\;appname=aggregator-sla-app -len 1 
-oozie http://localhost:11000/oozie
+
+<sla-message>
+  <event>
+    <sequence-id>1</sequence-id>
+    <registration>
+      <sla-id>0000000-130130150445097-oozie-joe-C@1</sla-id>
+      <app-type>COORDINATOR_ACTION</app-type>
+      <app-name>aggregator-sla-app</app-name>
+      <user>joe</user>
+      <group />
+      <parent-sla-id>null</parent-sla-id>
+      <expected-start>2010-01-01T02:00Z</expected-start>
+      <expected-end>2010-01-01T03:00Z</expected-end>
+      <status-timestamp>2013-01-30T23:05Z</status-timestamp>
+      <notification-msg>Notifying User for 2010-01-01T01:00Z nominal 
time</notification-msg>
+      <alert-contact>[email protected]</alert-contact>
+      <dev-contact>[email protected]</dev-contact>
+      <qa-contact>[email protected]</qa-contact>
+      <se-contact>[email protected]</se-contact>
+      <alert-percentage>80</alert-percentage>
+      <alert-frequency>LAST_HOUR</alert-frequency>
+      <upstream-apps />
+      <job-status>CREATED</job-status>
+      <job-data />
+    </registration>
+  </event>
+</sla-message>
+
+</verbatim>
+
+A filter can be specified after all options.
+
+The =filter=option syntax is: <code>[NAME=VALUE][\;NAME=VALUE]*</code>. Note 
<code>\</code> before semi-colon is for escape.
+
+Valid filter names are:
+
+   * jobid: workflow action/job id, coordinator action/job id
+   * appname: the coordinator/workflow application name
+
+The query will do an AND among all the filter names. The query will do an OR 
among all the filter values for the same
+name. Multiple values must be specified as different name value pairs.
+
+
 ---++ Pig Operations
 
 ---+++ Submitting a pig job through HTTP

Modified: oozie/branches/hcat-intre/release-log.txt
URL: 
http://svn.apache.org/viewvc/oozie/branches/hcat-intre/release-log.txt?rev=1446790&r1=1446789&r2=1446790&view=diff
==============================================================================
--- oozie/branches/hcat-intre/release-log.txt (original)
+++ oozie/branches/hcat-intre/release-log.txt Fri Feb 15 22:22:44 2013
@@ -1,6 +1,6 @@
-
 -- Oozie 3.4.0 release (trunk - unreleased)
 
+OOZIE-1189 add filter option to specify JobID and AppName in SLA CLI command 
(egashira via mona)
 OOZIE-1197 Create a hcat sharelib which can be included in pig, hive and java 
actions (mona,rohini via virag)
 OOZIE-1211 oozie does not support duplicated dataset (jaoki via virag)
 OOZIE-1187 reduce memory usage of SLA query (invoked by CLI command) to avoid 
OOM (egashira via virag) 


Reply via email to