1 4 package com.tc.stats; 5 6 import org.apache.commons.collections.set.ListOrderedSet; 7 8 import com.tc.logging.TCLogger; 9 import com.tc.logging.TCLogging; 10 import com.tc.management.beans.L2MBeanNames; 11 import com.tc.net.protocol.tcm.MessageChannel; 12 import com.tc.object.ObjectID; 13 import com.tc.object.net.ChannelStats; 14 import com.tc.object.net.DSOChannelManagerEventListener; 15 import com.tc.object.net.DSOChannelManagerMBean; 16 import com.tc.objectserver.api.GCStats; 17 import com.tc.objectserver.api.NoSuchObjectException; 18 import com.tc.objectserver.api.ObjectInstanceMonitorMBean; 19 import com.tc.objectserver.api.ObjectManagerEventListener; 20 import com.tc.objectserver.api.ObjectManagerMBean; 21 import com.tc.objectserver.core.impl.ServerManagementContext; 22 import com.tc.objectserver.lockmanager.api.DeadlockChain; 23 import com.tc.objectserver.lockmanager.api.LockMBean; 24 import com.tc.objectserver.lockmanager.api.LockManagerMBean; 25 import com.tc.objectserver.mgmt.ManagedObjectFacade; 26 import com.tc.objectserver.tx.ServerTransactionManagerEventListener; 27 import com.tc.objectserver.tx.ServerTransactionManagerMBean; 28 import com.tc.stats.statistics.CountStatistic; 29 import com.tc.stats.statistics.DoubleStatistic; 30 import com.tc.stats.statistics.Statistic; 31 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 import javax.management.MBeanServer ; 38 import javax.management.MalformedObjectNameException ; 39 import javax.management.NotCompliantMBeanException ; 40 import javax.management.ObjectName ; 41 42 48 public class DSO extends AbstractNotifyingMBean implements DSOMBean { 49 50 private final static TCLogger logger = TCLogging.getLogger(DSO.class); 51 private final static String DSO_OBJECT_NAME_PREFIX = L2MBeanNames.DSO.getCanonicalName() + ","; 52 53 private final DSOStatsImpl dsoStats; 54 private final ObjectManagerMBean objMgr; 55 private final MBeanServer mbeanServer; 56 private final ArrayList rootObjectNames = new ArrayList (); 57 private final Set clientObjectNames = new ListOrderedSet(); 58 private final DSOChannelManagerMBean channelMgr; 59 private final ServerTransactionManagerMBean txnMgr; 60 private final LockManagerMBean lockMgr; 61 private final ChannelStats channelStats; 62 private final ObjectInstanceMonitorMBean instanceMonitor; 63 64 public DSO(final ServerManagementContext context, final MBeanServer mbeanServer) throws NotCompliantMBeanException { 65 super(DSOMBean.class); 66 try { 67 } catch(Exception e) {} 69 this.mbeanServer = mbeanServer; 70 this.dsoStats = new DSOStatsImpl(context); 71 this.lockMgr = context.getLockManager(); 72 this.objMgr = context.getObjectManager(); 73 this.channelMgr = context.getChannelManager(); 74 this.txnMgr = context.getTransactionManager(); 75 this.channelStats = context.getChannelStats(); 76 this.instanceMonitor = context.getInstanceMonitor(); 77 78 txnMgr.addRootListener(new TransactionManagerListener()); 80 objMgr.addListener(new ObjectManagerListener()); 81 channelMgr.addEventListener(new ChannelManagerListener()); 82 83 setupRoots(); 84 setupClients(); 85 } 86 87 public void reset() { 88 } 90 91 public DSOStats getStats() { 92 return dsoStats; 93 } 94 95 public CountStatistic getObjectFlushRate() { 96 return getStats().getObjectFlushRate(); 97 } 98 99 public DoubleStatistic getCacheHitRatio() { 100 return getStats().getCacheHitRatio(); 101 } 102 103 public CountStatistic getCacheMissRate() { 104 return getStats().getCacheMissRate(); 105 } 106 107 108 public CountStatistic getTransactionRate() { 109 return getStats().getTransactionRate(); 110 } 111 112 public CountStatistic getObjectFaultRate() { 113 return getStats().getObjectFaultRate(); 114 } 115 116 public Statistic[] getStatistics(String [] names) { 117 return getStats().getStatistics(names); 118 } 119 120 public GCStats[] getGarbageCollectorStats() { 121 return getStats().getGarbageCollectorStats(); 122 } 123 124 public ObjectName [] getRoots() { 125 synchronized (rootObjectNames) { 126 return (ObjectName []) rootObjectNames.toArray(new ObjectName [rootObjectNames.size()]); 127 } 128 } 129 130 public LockMBean[] getLocks() { 131 return this.lockMgr.getAllLocks(); 132 } 133 134 public ObjectName [] getClients() { 135 synchronized (clientObjectNames) { 136 return (ObjectName []) clientObjectNames.toArray(new ObjectName [clientObjectNames.size()]); 137 } 138 } 139 140 public DeadlockChain[] scanForDeadLocks() { 141 return this.lockMgr.scanForDeadlocks(); 142 } 143 144 public DSOClassInfo[] getClassInfo() { 145 Map counts = instanceMonitor.getInstanceCounts(); 146 DSOClassInfo[] rv = new DSOClassInfo[counts.size()]; 147 148 int i = 0; 149 for (Iterator iter = counts.keySet().iterator(); iter.hasNext();) { 150 String type = (String ) iter.next(); 151 int count = ((Integer ) counts.get(type)).intValue(); 152 rv[i++] = new DSOClassInfo(type, count); 153 } 154 155 return rv; 156 } 157 158 public ManagedObjectFacade lookupFacade(ObjectID objectID, int limit) throws NoSuchObjectException { 159 return this.objMgr.lookupFacade(objectID, limit); 160 } 161 162 private void setupRoots() { 163 for (Iterator iter = objMgr.getRootNames(); iter.hasNext();) { 164 String name = (String ) iter.next(); 165 final ObjectID rootID; 166 try { 167 rootID = objMgr.lookupRootID(name); 168 } catch (Exception e) { 169 e.printStackTrace(); 170 continue; 171 } 172 173 addRootMBean(name, rootID); 174 } 175 } 176 177 private void setupClients() { 178 MessageChannel[] channels = channelMgr.getActiveChannels(); 179 for (int i = 0; i < channels.length; i++) { 180 addClientMBean(channels[i]); 181 } 182 } 183 184 private ObjectName makeClientObjectName(MessageChannel channel) { 185 try { 186 return new ObjectName (DSO_OBJECT_NAME_PREFIX + "channelID=" + channel.getChannelID().toLong()); 187 } catch (MalformedObjectNameException e) { 188 throw new RuntimeException (e); 190 } 191 } 192 193 private ObjectName makeRootObjectName(String name, ObjectID id) { 194 try { 195 return new ObjectName (DSO_OBJECT_NAME_PREFIX + "rootID=" + id.toLong()); 196 } catch (MalformedObjectNameException e) { 197 throw new RuntimeException (e); 199 } 200 } 201 202 private void addRootMBean(String name, ObjectID rootID) { 203 if (name.startsWith("@")) { 205 return; 207 } 208 209 synchronized (rootObjectNames) { 210 ObjectName rootName = makeRootObjectName(name, rootID); 211 if (mbeanServer.isRegistered(rootName)) { 212 logger.debug("Root MBean already registered for name " + rootName); 214 return; 215 } 216 217 DSORoot dsoRoot = new DSORoot(rootID, objMgr, name); 218 try { 219 mbeanServer.registerMBean(dsoRoot, rootName); 220 rootObjectNames.add(rootName); 221 sendNotification(ROOT_ADDED, rootName); 222 } catch (Exception e) { 223 logger.error(e); 224 } 225 } 226 } 227 228 private void removeClientMBean(MessageChannel channel) { 229 ObjectName clientName = makeClientObjectName(channel); 230 231 synchronized (clientObjectNames) { 232 try { 233 if (mbeanServer.isRegistered(clientName)) { 234 sendNotification(CLIENT_DETACHED, clientName); 235 mbeanServer.unregisterMBean(clientName); 236 } 237 } catch (Exception e) { 238 logger.error(e); 239 } finally { 240 clientObjectNames.remove(clientName); 241 } 242 } 243 } 244 245 private void addClientMBean(final MessageChannel channel) { 246 synchronized (clientObjectNames) { 247 ObjectName clientName = makeClientObjectName(channel); 248 if (mbeanServer.isRegistered(clientName)) { 249 logger.debug("channel MBean already registered for name " + clientName); 250 return; 251 } 252 253 try { 254 final DSOClient client = new DSOClient(channel, channelStats); 255 mbeanServer.registerMBean(client, clientName); 256 clientObjectNames.add(clientName); 257 sendNotification(CLIENT_ATTACHED, clientName); 258 } catch (Exception e) { 259 logger.error("Unable to register DSO client MBean", e); 260 } 261 } 262 } 263 264 private class TransactionManagerListener implements ServerTransactionManagerEventListener { 265 266 public void rootCreated(String name, ObjectID rootID) { 267 addRootMBean(name, rootID); 268 } 269 270 } 271 272 private class ObjectManagerListener implements ObjectManagerEventListener { 273 public void garbageCollectionComplete(GCStats stats) { 274 sendNotification(GC_COMPLETED, stats); 275 } 276 } 277 278 private class ChannelManagerListener implements DSOChannelManagerEventListener { 279 280 public void channelCreated(MessageChannel channel) { 281 addClientMBean(channel); 282 } 283 284 public void channelRemoved(MessageChannel channel) { 285 removeClientMBean(channel); 286 } 287 288 } 289 290 } 291 | Popular Tags |