public void init(String contextName, ContextFactory factory) { super.init(contextName, factory); parseAndSetPeriod(PERIOD_PROPERTY); }
/** * Stops monitoring and frees buffered data, returning this * object to its initial state. */ public synchronized void close() { stopMonitoring(); clearUpdaters(); }
/** * method to close the datagram socket */ @Override public void close() { super.close(); if (datagramSocket != null) { datagramSocket.close(); } }
/** * Emits the records. */ private synchronized void emitRecords() throws IOException { for (Entry<String, RecordMap> record : bufferedData.entrySet()) { String recordName = record.getKey(); RecordMap recordMap = record.getValue(); for (Entry<TagMap, MetricMap> entry : record.getValue().entrySet()) { OutputRecord outRec = new OutputRecord(entry.getKey(), entry.getValue()); emitRecord(contextName, recordName, outRec); } } flush(); }
Map<String,MetricValue> metricUpdates = record.getMetricTable(); RecordMap recordMap = getRecordMap(recordName); synchronized (recordMap) { MetricMap metricMap = recordMap.get(tagTable); Number newNumber = sum(updateNumber, currentNumber); metricMap.put(metricName, newNumber);
@Test public void testCloseShouldCloseTheSocketWhichIsCreatedByInit() throws Exception { AbstractMetricsContext context=new GangliaContext(); context.init("gangliaContext", ContextFactory.getFactory()); GangliaContext gangliaContext =(GangliaContext) context; assertFalse("Socket already closed",gangliaContext.datagramSocket.isClosed()); context.close(); assertTrue("Socket not closed",gangliaContext.datagramSocket.isClosed()); } }
@Override protected MetricsRecord newRecord(String recordName) { MetricsRecord record = super.newRecord(recordName); if (records.isEmpty() || records.contains(recordName)) { // Create MBean to expose this record // Only if this record is to be exposed through JMX getOrCreateMBean(recordName); } return record; }
/** * Starts or restarts monitoring, the emitting of metrics records. */ public synchronized void startMonitoring() throws IOException { if (!isMonitoring) { startTimer(); isMonitoring = true; } }
/** * Called by MetricsRecordImpl.remove(). Removes all matching rows in * the internal table of metric data. A row matches if it has the same * tag names and values as record, but it may also have additional * tags. */ protected void remove(MetricsRecordImpl record) { String recordName = record.getRecordName(); TagMap tagTable = record.getTagTable(); RecordMap recordMap = getRecordMap(recordName); synchronized (recordMap) { Iterator<TagMap> it = recordMap.keySet().iterator(); while (it.hasNext()) { TagMap rowTags = it.next(); if (rowTags.containsAll(tagTable)) { it.remove(); } } } }
/** * Starts timer if it is not already started */ private synchronized void startTimer() { if (timer == null) { timer = new Timer("Timer thread for monitoring " + getContextName(), true); TimerTask task = new TimerTask() { public void run() { try { timerEvent(); } catch (IOException ioe) { ioe.printStackTrace(); } } }; long millis = period * 1000; timer.scheduleAtFixedRate(task, millis, millis); } }
/** * Timer callback. */ private void timerEvent() throws IOException { if (isMonitoring) { Collection<Updater> myUpdaters; synchronized (this) { myUpdaters = new ArrayList<Updater>(updaters); } // Run all the registered updates without holding a lock // on this context for (Updater updater : myUpdaters) { try { updater.doUpdates(this); } catch (Throwable throwable) { throwable.printStackTrace(); } } emitRecords(); } }
@Override protected void flush() throws IOException { for (MetricsContext ctxt : subctxt) { try { ((AbstractMetricsContext)ctxt).flush(); } catch (IOException e) { LOG.warn("flush failed: " + ctxt.getContextName(), e); } } }
@Override protected void emitRecord(String contextName, String recordName, OutputRecord outRec) throws IOException { for (MetricsContext ctxt : subctxt) { try { ((AbstractMetricsContext)ctxt).emitRecord( contextName, recordName, outRec); if (contextName == null || recordName == null || outRec == null) { throw new IOException(contextName + ":" + recordName + ":" + outRec); } } catch (IOException e) { LOG.warn("emitRecord failed: " + ctxt.getContextName(), e); } } }
/** * Emits the records. */ private synchronized void emitRecords() throws IOException { for (Map.Entry<String,RecordMap> recordEntry : bufferedData.entrySet()) { RecordMap recordMap = recordEntry.getValue(); synchronized (recordMap) { Set<Entry<TagMap, MetricMap>> entrySet = recordMap.entrySet (); for (Entry<TagMap, MetricMap> entry : entrySet) { OutputRecord outRec = new OutputRecord(entry.getKey(), entry.getValue()); emitRecord(contextName, recordEntry.getKey(), outRec); } } } flush(); }
Map<String,MetricValue> metricUpdates = record.getMetricTable(); RecordMap recordMap = getRecordMap(recordName); synchronized (recordMap) { MetricMap metricMap = recordMap.get(tagTable); Number newNumber = sum(updateNumber, currentNumber); metricMap.put(metricName, newNumber);
@Test public void testCloseShouldCloseTheSocketWhichIsCreatedByInit() throws Exception { AbstractMetricsContext context=new GangliaContext(); context.init("gangliaContext", ContextFactory.getFactory()); GangliaContext gangliaContext =(GangliaContext) context; assertFalse("Socket already closed",gangliaContext.datagramSocket.isClosed()); context.close(); assertTrue("Socket not closed",gangliaContext.datagramSocket.isClosed()); } }
/** * Creates a new AbstractMetricsRecord instance with the given <code>recordName</code>. * Throws an exception if the metrics implementation is configured with a fixed * set of record names and <code>recordName</code> is not in that set. * * @param recordName the name of the record * @throws MetricsException if recordName conflicts with configuration data */ public final synchronized MetricsRecord createRecord(String recordName) { if (bufferedData.get(recordName) == null) { bufferedData.put(recordName, new RecordMap()); } return newRecord(recordName); }
/** * Starts or restarts monitoring, the emitting of metrics records. */ public synchronized void startMonitoring() throws IOException { if (!isMonitoring) { startTimer(); isMonitoring = true; } }
/** * Called by MetricsRecordImpl.remove(). Removes all matching rows in * the internal table of metric data. A row matches if it has the same * tag names and values as record, but it may also have additional * tags. */ protected void remove(MetricsRecordImpl record) { String recordName = record.getRecordName(); TagMap tagTable = record.getTagTable(); RecordMap recordMap = getRecordMap(recordName); synchronized (recordMap) { Iterator<TagMap> it = recordMap.keySet().iterator(); while (it.hasNext()) { TagMap rowTags = it.next(); if (rowTags.containsAll(tagTable)) { it.remove(); } } } }
/** * Starts timer if it is not already started */ private synchronized void startTimer() { if (timer == null) { timer = new Timer("Timer thread for monitoring " + getContextName(), true); TimerTask task = new TimerTask() { public void run() { try { timerEvent(); } catch (IOException ioe) { ioe.printStackTrace(); } } }; long millis = period * 1000; timer.scheduleAtFixedRate(task, millis, millis); } }