KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > stats > DSO


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 import javax.management.MBeanServer JavaDoc;
38 import javax.management.MalformedObjectNameException JavaDoc;
39 import javax.management.NotCompliantMBeanException JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41
42 /**
43  * This is the top-level MBean for the DSO subsystem, off which to hang JSR-77 Stats and Config MBeans.
44  *
45  * @see DSOMBean
46  * @see DSOStatsImpl
47  */

48 public class DSO extends AbstractNotifyingMBean implements DSOMBean {
49
50   private final static TCLogger logger = TCLogging.getLogger(DSO.class);
51   private final static String JavaDoc DSO_OBJECT_NAME_PREFIX = L2MBeanNames.DSO.getCanonicalName() + ",";
52
53   private final DSOStatsImpl dsoStats;
54   private final ObjectManagerMBean objMgr;
55   private final MBeanServer JavaDoc mbeanServer;
56   private final ArrayList JavaDoc rootObjectNames = new ArrayList JavaDoc();
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 JavaDoc mbeanServer) throws NotCompliantMBeanException JavaDoc {
65     super(DSOMBean.class);
66     try {
67       //TraceImplementation.init(TraceTags.LEVEL_TRACE);
68
} catch(Exception JavaDoc 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     // add various listeners (do this before the setupXXX() methods below so we don't ever miss anything)
79
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     // TODO: implement this?
89
}
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 JavaDoc[] names) {
117     return getStats().getStatistics(names);
118   }
119
120   public GCStats[] getGarbageCollectorStats() {
121     return getStats().getGarbageCollectorStats();
122   }
123
124   public ObjectName JavaDoc[] getRoots() {
125     synchronized (rootObjectNames) {
126       return (ObjectName JavaDoc[]) rootObjectNames.toArray(new ObjectName JavaDoc[rootObjectNames.size()]);
127     }
128   }
129
130   public LockMBean[] getLocks() {
131     return this.lockMgr.getAllLocks();
132   }
133
134   public ObjectName JavaDoc[] getClients() {
135     synchronized (clientObjectNames) {
136       return (ObjectName JavaDoc[]) clientObjectNames.toArray(new ObjectName JavaDoc[clientObjectNames.size()]);
137     }
138   }
139
140   public DeadlockChain[] scanForDeadLocks() {
141     return this.lockMgr.scanForDeadlocks();
142   }
143
144   public DSOClassInfo[] getClassInfo() {
145     Map JavaDoc counts = instanceMonitor.getInstanceCounts();
146     DSOClassInfo[] rv = new DSOClassInfo[counts.size()];
147
148     int i = 0;
149     for (Iterator JavaDoc iter = counts.keySet().iterator(); iter.hasNext();) {
150       String JavaDoc type = (String JavaDoc) iter.next();
151       int count = ((Integer JavaDoc) 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 JavaDoc iter = objMgr.getRootNames(); iter.hasNext();) {
164       String JavaDoc name = (String JavaDoc) iter.next();
165       final ObjectID rootID;
166       try {
167         rootID = objMgr.lookupRootID(name);
168       } catch (Exception JavaDoc 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 JavaDoc makeClientObjectName(MessageChannel channel) {
185     try {
186       return new ObjectName JavaDoc(DSO_OBJECT_NAME_PREFIX + "channelID=" + channel.getChannelID().toLong());
187     } catch (MalformedObjectNameException JavaDoc e) {
188       // this shouldn't happen
189
throw new RuntimeException JavaDoc(e);
190     }
191   }
192
193   private ObjectName JavaDoc makeRootObjectName(String JavaDoc name, ObjectID id) {
194     try {
195       return new ObjectName JavaDoc(DSO_OBJECT_NAME_PREFIX + "rootID=" + id.toLong());
196     } catch (MalformedObjectNameException JavaDoc e) {
197       // this shouldn't happen
198
throw new RuntimeException JavaDoc(e);
199     }
200   }
201
202   private void addRootMBean(String JavaDoc name, ObjectID rootID) {
203     // XXX: There should be a cleaner way to do this ignore
204
if (name.startsWith("@")) {
205       // ignore iternal roots
206
return;
207     }
208
209     synchronized (rootObjectNames) {
210       ObjectName JavaDoc rootName = makeRootObjectName(name, rootID);
211       if (mbeanServer.isRegistered(rootName)) {
212         // this can happen since the initial root setup races with the object manager "root created" event
213
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 JavaDoc e) {
223         logger.error(e);
224       }
225     }
226   }
227
228   private void removeClientMBean(MessageChannel channel) {
229     ObjectName JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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