Author: jfclere
Date: Mon Apr  6 15:09:19 2009
New Revision: 762374

URL: http://svn.apache.org/viewvc?rev=762374&view=rev
Log:
First support for httpd-trunk heartbeat logic.

Added:
    tomcat/trunk/java/org/apache/catalina/cluster/
    tomcat/trunk/java/org/apache/catalina/cluster/CollectedInfo.java
    tomcat/trunk/java/org/apache/catalina/cluster/HeartbeatListener.java
Modified:
    tomcat/trunk/build.xml

Modified: tomcat/trunk/build.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/build.xml?rev=762374&r1=762373&r2=762374&view=diff
==============================================================================
--- tomcat/trunk/build.xml (original)
+++ tomcat/trunk/build.xml Mon Apr  6 15:09:19 2009
@@ -204,7 +204,6 @@
     <include name="org/apache/naming/**" />
     <!-- Modules -->
     <exclude name="org/apache/catalina/ant/**" />
-    <exclude name="org/apache/catalina/cluster/**" />
     <exclude name="org/apache/catalina/ha/**" />
     <exclude name="org/apache/catalina/mbeans/JmxRemote*" />
     <exclude name="org/apache/catalina/tribes/**" />

Added: tomcat/trunk/java/org/apache/catalina/cluster/CollectedInfo.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/cluster/CollectedInfo.java?rev=762374&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/cluster/CollectedInfo.java (added)
+++ tomcat/trunk/java/org/apache/catalina/cluster/CollectedInfo.java Mon Apr  6 
15:09:19 2009
@@ -0,0 +1,99 @@
+/*
+ * 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.catalina.cluster;
+
+/* for MBean to read ready and busy */
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.ObjectInstance;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.tomcat.util.modeler.Registry;
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+
+/*
+ * Listener to provider informations to mod_heartbeat.c
+ * *msg_format = "v=%u&ready=%u&busy=%u"; (message to send).
+ * send the muticast merssage using the format...
+ * what about the bind(IP. port) only IP makes sense (for the moment).
+ * BTW:v  = version :-)
+ */
+public class CollectedInfo {
+
+    /* Collect info via JMX */
+    protected MBeanServer mBeanServer = null;
+    protected ObjectName objName = null;
+
+    int ready;
+    int busy;
+
+    public CollectedInfo(String host, int port) throws Exception {
+        init(host, port);
+    }
+    public void init(String host, int port) throws Exception {
+        String sport = Integer.toString(port);
+        mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
+        String onStr = "*:type=ThreadPool,*";
+        ObjectName objectName = new ObjectName(onStr);
+        Set set = mBeanServer.queryMBeans(objectName, null);
+        Iterator<ObjectInstance> iterator = set.iterator();
+        while (iterator.hasNext()) {
+            ObjectInstance oi = iterator.next();
+            objName = oi.getObjectName();
+            String name = objName.getKeyProperty("name");
+            /* Name are:
+             * http-8080
+             * jk-10.33.144.3-8009
+             * jk-jfcpc%2F10.33.144.3-8009
+             */
+            if (port==0 && host==null)
+                  break; /* Take the first one */
+            String [] elenames = name.split("-");
+            if (elenames[elenames.length-1].compareTo(sport) != 0)
+                continue; /* port doesn't match */
+            if (host==null)
+                break; /* Only port done */
+            String [] shosts = elenames[1].split("%2F");
+            if (shosts[0].compareTo(host) == 0)
+                break; /* Done port and host are the expected ones */
+        }
+        if (objName == null)
+            throw(new Exception("Can't find connector for " + host + ":" + 
sport));
+        
+    }
+
+    public void refresh() throws Exception {
+        if (mBeanServer == null || objName == null) {
+            throw(new Exception("Not initialized!!!"));
+        }
+        Integer imax = (Integer) mBeanServer.getAttribute(objName, 
"maxThreads");
+
+        // the currentThreadCount could be 0 before the threads are created...
+        // Integer iready = (Integer) mBeanServer.getAttribute(objName, 
"currentThreadCount");
+
+        Integer ibusy  = (Integer) mBeanServer.getAttribute(objName, 
"currentThreadsBusy");
+
+        busy = ibusy.intValue();
+        ready = imax.intValue() - ibusy;
+    }
+}

Added: tomcat/trunk/java/org/apache/catalina/cluster/HeartbeatListener.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/cluster/HeartbeatListener.java?rev=762374&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/cluster/HeartbeatListener.java (added)
+++ tomcat/trunk/java/org/apache/catalina/cluster/HeartbeatListener.java Mon 
Apr  6 15:09:19 2009
@@ -0,0 +1,126 @@
+/*
+ * 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.catalina.cluster;
+
+import org.apache.catalina.ContainerEvent;
+import org.apache.catalina.ContainerListener;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleEvent;
+import org.apache.catalina.LifecycleListener;
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+
+import org.apache.catalina.connector.Connector;
+
+import java.net.MulticastSocket;
+import java.net.InetAddress;
+import java.net.DatagramPacket;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.tomcat.util.modeler.Registry;
+
+/*
+ * Listener to provider informations to mod_heartbeat.c
+ * *msg_format = "v=%u&ready=%u&busy=%u"; (message to send).
+ * send the muticast merssage using the format...
+ * what about the bind(IP. port) only IP makes sense (for the moment).
+ * BTW:v  = version :-)
+ */
+public class HeartbeatListener
+    implements LifecycleListener, ContainerListener {
+
+    public static Log log = LogFactory.getLog(HeartbeatListener.class);
+
+    /* To allow to select the connector */
+    int port = 0;
+    String host = null;
+    public void setHost(String host) { this.host = host; }
+    public void setPort(int port) { this.port = port; }
+
+    /* for multicasting stuff */
+    MulticastSocket s = null;
+    InetAddress group = null;
+    String ip = "224.0.1.105"; /* Multicast IP */
+    int multiport = 23364;     /* Multicast Port */
+
+    public void setGroup(String ip) { this.ip = ip; }
+    public void setMultiport(int multiport) { this.multiport = multiport; }
+
+    private CollectedInfo coll = null;
+
+    public void containerEvent(ContainerEvent event) {
+    }
+
+    public void lifecycleEvent(LifecycleEvent event) {
+        Object source = event.getLifecycle();
+        if (Lifecycle.PERIODIC_EVENT.equals(event.getType())) {
+            if (s == null) {
+                try {
+                    group = InetAddress.getByName(ip);
+                    s = new MulticastSocket(port);
+                    s.setTimeToLive(16);
+                    s.joinGroup(group);
+                } catch (Exception ex) {
+                    log.error("Unable to use multicast: " + ex);
+                    s = null;
+                    return;
+                } 
+            }
+// * *msg_format = "v=%u&ready=%u&busy=%u"; (message to send).
+// v = version (1)
+// ready & ready are read from the scoreboard in httpd.
+// Endpoint ( getCurrentThreadsBusy ) ( getMaxThreads )
+            if (coll == null) {
+                try {
+                    coll = new CollectedInfo(host, port);
+                } catch (Exception ex) {
+                    log.error("Unable to initialize info collection: " + ex);
+                    coll = null;
+                    return;
+                } 
+            }
+            try {
+                coll.refresh();
+            } catch (Exception ex) {
+                log.error("Unable to collect load information: " + ex);
+                coll = null;
+                return;
+            }
+            String output = new String();
+            output = "v=1&ready=" + coll.ready + "&busy=" + coll.busy;
+            byte[] buf;
+            try {
+                buf = output.getBytes("US-ASCII");
+            } catch (UnsupportedEncodingException ex) {
+                buf = output.getBytes();
+            }
+            DatagramPacket data = new DatagramPacket(buf, buf.length, group, 
multiport);
+            try {
+                s.send(data);
+            } catch (Exception ex) {
+                log.error("Unable to send colllected load information: " + ex);
+                System.out.println(ex);
+                s.close();
+                s = null;
+            }
+        }
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to