1 | package org.dcache.cells; |
2 | |
3 | import java.util.Map; |
4 | |
5 | import org.dcache.commons.stats.RequestCounterImpl; |
6 | import org.dcache.commons.stats.RequestExecutionTimeGauge; |
7 | import org.dcache.commons.stats.RequestCounters; |
8 | import org.dcache.commons.stats.RequestExecutionTimeGauges; |
9 | |
10 | import diskCacheV111.vehicles.Message; |
11 | |
12 | import dmg.util.Args; |
13 | import dmg.cells.nucleus.CellMessage; |
14 | import dmg.cells.nucleus.NoRouteToCellException; |
15 | import dmg.cells.nucleus.SerializationException; |
16 | import dmg.cells.nucleus.CellEndpoint; |
17 | import dmg.cells.nucleus.CellInfo; |
18 | import dmg.cells.nucleus.CellMessageAnswerable; |
19 | |
20 | public class MessageProcessingMonitor |
21 | implements CellCommandListener, CellMessageSender |
22 | { |
23 | /** |
24 | * Request counters used to count message processing. |
25 | */ |
26 | private final RequestCounters<Class> _counters; |
27 | |
28 | /** |
29 | * Request gauges used to measure message processing. |
30 | */ |
31 | private final RequestExecutionTimeGauges<Class> _gauges; |
32 | |
33 | private CellEndpoint _endpoint; |
34 | |
35 | /** |
36 | * If true then message processing will be monitored and |
37 | * administrative commands to query the monitoring results are |
38 | * activated. |
39 | */ |
40 | private boolean _enabled; |
41 | |
42 | public MessageProcessingMonitor() |
43 | { |
44 | _counters = new RequestCounters<Class>("Messages"); |
45 | _gauges = new RequestExecutionTimeGauges<Class>("Messages"); |
46 | _enabled = false; |
47 | } |
48 | |
49 | @Override |
50 | public void setCellEndpoint(CellEndpoint endpoint) |
51 | { |
52 | _endpoint = endpoint; |
53 | } |
54 | |
55 | public void setEnabled(boolean enabled) |
56 | { |
57 | _enabled = enabled; |
58 | } |
59 | |
60 | public boolean isEnabled() |
61 | { |
62 | return _enabled; |
63 | } |
64 | |
65 | public CellEndpoint getReplyCellEndpoint(CellMessage envelope) |
66 | { |
67 | if (_enabled) { |
68 | Class type = envelope.getMessageObject().getClass(); |
69 | return new MonitoringReplyCellEndpoint(type); |
70 | } else { |
71 | return _endpoint; |
72 | } |
73 | } |
74 | |
75 | public final static String hh_monitoring_enable = |
76 | "# Enables monitoring of message processing"; |
77 | public String ac_monitoring_enable(Args args) |
78 | { |
79 | _enabled = true; |
80 | return ""; |
81 | } |
82 | |
83 | public final static String hh_monitoring_disable = |
84 | "# Disables monitoring of message processing"; |
85 | public String ac_monitoring_disable(Args args) |
86 | { |
87 | _enabled = false; |
88 | return ""; |
89 | } |
90 | |
91 | public final static String hh_monitoring_info = |
92 | "# Provides information about message processing"; |
93 | public String ac_monitoring_info(Args args) |
94 | { |
95 | return _counters.toString() + "\n\n" + _gauges.toString(); |
96 | } |
97 | |
98 | public class MonitoringReplyCellEndpoint implements CellEndpoint |
99 | { |
100 | private final Class _type; |
101 | private final long _startTime; |
102 | |
103 | public MonitoringReplyCellEndpoint(Class type) |
104 | { |
105 | _startTime = System.currentTimeMillis(); |
106 | _type = type; |
107 | _counters.incrementRequests(_type); |
108 | } |
109 | |
110 | public void sendMessage(CellMessage envelope) |
111 | throws SerializationException, |
112 | NoRouteToCellException |
113 | { |
114 | boolean success = false; |
115 | try { |
116 | _endpoint.sendMessage(envelope); |
117 | success = true; |
118 | } finally { |
119 | _gauges.update(_type, System.currentTimeMillis() - _startTime); |
120 | Object o = envelope.getMessageObject(); |
121 | if (!success || o instanceof Exception || |
122 | (o instanceof Message) && ((Message) o).getReturnCode() != 0) { |
123 | _counters.incrementFailed(_type); |
124 | } |
125 | } |
126 | } |
127 | |
128 | public void sendMessage(CellMessage envelope, |
129 | CellMessageAnswerable callback, |
130 | long timeout) |
131 | { |
132 | throw new UnsupportedOperationException("Cannot use callback for reply"); |
133 | } |
134 | |
135 | public CellMessage sendAndWait(CellMessage envelope, long timeout) |
136 | { |
137 | throw new UnsupportedOperationException("Cannot use blocking send for reply"); |
138 | } |
139 | |
140 | public CellMessage sendAndWaitToPermanent(CellMessage envelope, long timeout) |
141 | { |
142 | throw new UnsupportedOperationException("Cannot use blocking send for reply"); |
143 | } |
144 | |
145 | public CellInfo getCellInfo() |
146 | { |
147 | return _endpoint.getCellInfo(); |
148 | } |
149 | |
150 | public Map<String,Object> getDomainContext() |
151 | { |
152 | return _endpoint.getDomainContext(); |
153 | } |
154 | |
155 | public Args getArgs() |
156 | { |
157 | return _endpoint.getArgs(); |
158 | } |
159 | } |
160 | } |