summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/MetricsAdminI.java
blob: f67be46dcd24b32d8a8658e6975d143d34ff5582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceInternal;

public class MetricsAdminI extends IceMX._MetricsAdminDisp implements Ice.PropertiesAdminUpdateCallback
{
    final static private String[] suffixes =
    {
        "Disabled",
        "GroupBy",
        "Accept.*",
        "Reject.*",
        "RetainDetached",
        "Map.*",
    };

    static void
    validateProperties(String prefix, Ice.Properties properties)
    {
        java.util.Map<String, String> props = properties.getPropertiesForPrefix(prefix);
        java.util.List<String> unknownProps = new java.util.ArrayList<String>();
        for(String prop : props.keySet())
        {
            boolean valid = false;
            for(String suffix : suffixes)
            {
                if(IceUtilInternal.StringUtil.match(prop, prefix + suffix, false))
                {
                    valid = true;
                    break;
                }
            }

            if(!valid)
            {
                unknownProps.add(prop);
            }
        }
        
        if(unknownProps.size() != 0 && properties.getPropertyAsIntWithDefault("Ice.Warn.UnknownProperties", 1) > 0)
        {
            StringBuffer message = new StringBuffer("found unknown IceMX properties for `");
            message.append(prefix.substring(0, prefix.length() - 1));
            message.append("':");
            for(String p : unknownProps)
            {
                message.append("\n    ");
                message.append(p);
            }
            Ice.Util.getProcessLogger().warning(message.toString());
        }
    }
    
    static class MetricsMapFactory<T extends IceMX.Metrics>
    {
        public MetricsMapFactory(Runnable updater, Class<T> cl)
        {
            _updater = updater;
            _class = cl;
        }

        public void
        update()
        {
            assert(_updater != null);
            _updater.run();
        }

        public MetricsMap<T>
        create(String mapPrefix, Ice.Properties properties)
        {
            return new MetricsMap<T>(mapPrefix, _class, properties, _subMaps);
        }

        public <S extends IceMX.Metrics> void
        registerSubMap(String subMap, Class<S> cl, java.lang.reflect.Field field)
        {
            _subMaps.put(subMap, new MetricsMap.SubMapFactory<S>(cl, field));
        }

        final private Runnable _updater;
        final private Class<T> _class;
        final private java.util.Map<String, MetricsMap.SubMapFactory<?>> _subMaps = 
            new java.util.HashMap<String, MetricsMap.SubMapFactory<?>>();
    };

    public MetricsAdminI(Ice.Properties properties, Ice.Logger logger)
    {
        _logger = logger;
        _properties = properties;
        updateViews();
    }

    public void updateViews()
    {
        java.util.Set<MetricsMapFactory> updatedMaps = new java.util.HashSet<MetricsMapFactory>();
        synchronized(this)
        {
            String viewsPrefix = "IceMX.Metrics.";
            java.util.Map<String, String> viewsProps = _properties.getPropertiesForPrefix(viewsPrefix);
            java.util.Map<String, MetricsViewI> views = new java.util.HashMap<String, MetricsViewI>();
            _disabledViews.clear();
            for(java.util.Map.Entry<String, String> e : viewsProps.entrySet())
            {
                String viewName = e.getKey().substring(viewsPrefix.length());
                int dotPos = viewName.indexOf('.');
                if(dotPos > 0)
                {
                    viewName = viewName.substring(0, dotPos);
                }
                
                if(views.containsKey(viewName) || _disabledViews.contains(viewName))
                {
                    continue; // View already configured.
                }
                
                validateProperties(viewsPrefix + viewName + ".", _properties);
                
                if(_properties.getPropertyAsIntWithDefault(viewsPrefix + viewName + ".Disabled", 0) > 0)
                {
                    _disabledViews.add(viewName);
                    continue; // The view is disabled
                }
                
                //
                // Create the view or update it.
                //
                MetricsViewI v = _views.get(viewName);
                if(v == null)
                {
                    v = new MetricsViewI(viewName);
                }
                views.put(viewName, v);

                for(java.util.Map.Entry<String, MetricsMapFactory<?>> f : _factories.entrySet())
                {
                    if(v.addOrUpdateMap(_properties, f.getKey(), f.getValue(), _logger))
                    {
                        updatedMaps.add(f.getValue());
                    }
                }
            }
            java.util.Map<String, MetricsViewI> tmp = _views;
            _views = views;
            views = tmp;
        
            //
            // Go through removed views to collect maps to update.
            //
            for(java.util.Map.Entry<String, MetricsViewI> v : views.entrySet())
            {
                if(!_views.containsKey(v.getKey()))
                {
                    for(String n : v.getValue().getMaps())
                    {
                        updatedMaps.add(_factories.get(n));
                    }
                }
            }
        }
        
        //
        // Call the updaters to update the maps.
        //
        for(MetricsMapFactory f : updatedMaps)
        {
            f.update();
        }
    }

    synchronized public String[] 
    getMetricsViewNames(Ice.StringSeqHolder holder, Ice.Current current)
    {
        holder.value = _disabledViews.toArray(new String[_disabledViews.size()]);
        return _views.keySet().toArray(new String[_views.size()]);
    }

    public void 
    enableMetricsView(String name, Ice.Current current)
        throws IceMX.UnknownMetricsView
    {
        synchronized(this)
        {
            getMetricsView(name); // Throws if unknown view.
            _properties.setProperty("IceMX.Metrics." + name + ".Disabled", "0");
        }
        updateViews();
    }
    
    public void 
    disableMetricsView(String name, Ice.Current current)
        throws IceMX.UnknownMetricsView
    {
        synchronized(this)
        {
            getMetricsView(name); // Throws if unknown view.
            _properties.setProperty("IceMX.Metrics." + name + ".Disabled", "1");
        }
        updateViews();
    }
    
    synchronized public java.util.Map<String, IceMX.Metrics[]> 
    getMetricsView(String viewName, Ice.LongHolder holder, Ice.Current current)
        throws IceMX.UnknownMetricsView
    {
        MetricsViewI view = getMetricsView(viewName);
        holder.value = IceInternal.Time.currentMonotonicTimeMillis();
        if(view != null)
        {
            return view.getMetrics();
        }
        return new java.util.HashMap<String, IceMX.Metrics[]>();
    }

    synchronized public IceMX.MetricsFailures[] 
    getMapMetricsFailures(String viewName, String mapName, Ice.Current current)
        throws IceMX.UnknownMetricsView
    {
        MetricsViewI view = getMetricsView(viewName);
        if(view != null)
        {
            return view.getFailures(mapName);
        }
        return new IceMX.MetricsFailures[0];
    }

    synchronized public IceMX.MetricsFailures 
    getMetricsFailures(String viewName, String mapName, String id, Ice.Current current)
        throws IceMX.UnknownMetricsView
    {
        MetricsViewI view = getMetricsView(viewName);
        if(view != null)
        {
            return view.getFailures(mapName, id);
        }
        return new IceMX.MetricsFailures();
    }

    public <T extends IceMX.Metrics> void
    registerMap(String map, Class<T> cl, Runnable updater)
    {
        boolean updated;
        MetricsMapFactory<T> factory;
        synchronized(this)
        {
            factory = new MetricsMapFactory<T>(updater, cl);
            _factories.put(map, factory);
            updated = addOrUpdateMap(map, factory);
        }
        if(updated)
        {
            factory.update();
        }
    }

    synchronized public <S extends IceMX.Metrics> void
    registerSubMap(String map, String subMap, Class<S> cl, java.lang.reflect.Field field)
    {
        boolean updated;
        MetricsMapFactory<?> factory;
        synchronized(this)
        {
            factory = _factories.get(map);
            if(factory == null)
            {
                return;
            }
            factory.registerSubMap(subMap, cl, field);
            removeMap(map);
            updated = addOrUpdateMap(map, factory);
        }
        if(updated)
        {
            factory.update();
        }
    }

    public void 
    unregisterMap(String mapName)
    {
        boolean updated;
        MetricsMapFactory<?> factory;
        synchronized(this)
        {
            factory = _factories.remove(mapName);
            if(factory == null)
            {
                return;
            }
            updated = removeMap(mapName);
        }
        if(updated)
        {
            factory.update();
        }
    }

    public <T extends IceMX.Metrics> java.util.List<MetricsMap<T>>
    getMaps(String mapName, Class<T> cl)
    {
        java.util.List<MetricsMap<T>> maps = new java.util.ArrayList<MetricsMap<T>>();
        for(MetricsViewI v : _views.values())
        {
            MetricsMap<T> map = v.getMap(mapName, cl);
            if(map != null)
            {
                maps.add(map);
            }
        }
        return maps;
    }

    public Ice.Logger 
    getLogger()
    {
        return _logger;
    }

    public void
    setProperties(Ice.Properties properties)
    {
        _properties = properties;
    }
    
    public void 
    updated(java.util.Map<String, String> props)
    {
        for(java.util.Map.Entry<String, String> e : props.entrySet())
        {
            if(e.getKey().indexOf("IceMX.") == 0)
            {
                // Udpate the metrics views using the new configuration.
                try
                {
                    updateViews();
                }
                catch(Exception ex)
                {
                    _logger.warning("unexpected exception while updating metrics view configuration:\n" + 
                                    ex.toString());
                }
                return;
            }
        }
    }

    private MetricsViewI 
    getMetricsView(String name)
        throws IceMX.UnknownMetricsView
    {
        MetricsViewI view = _views.get(name);
        if(view == null)
        {
            if(!_disabledViews.contains(name))
            {
                throw new IceMX.UnknownMetricsView();
            }
            return null;
        }
        return view;
    }
    
    private boolean
    addOrUpdateMap(String mapName, MetricsMapFactory factory)
    {
        boolean updated = false;
        for(MetricsViewI v : _views.values())
        {
            updated |= v.addOrUpdateMap(_properties, mapName, factory, _logger);
        }
        return updated;
    }

    private boolean
    removeMap(String mapName)
    {
        boolean updated = false;
        for(MetricsViewI v : _views.values())
        {
            updated |= v.removeMap(mapName);
        }
        return updated;
    }

    private Ice.Properties _properties;
    final private Ice.Logger _logger;
    final private java.util.Map<String, MetricsMapFactory<?>> _factories = 
        new java.util.HashMap<String, MetricsMapFactory<?>>();

    private java.util.Map<String, MetricsViewI> _views = new java.util.HashMap<String, MetricsViewI>();
    private java.util.Set<String> _disabledViews = new java.util.HashSet<String>();
}