001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.monitors;
018
019import java.util.concurrent.TimeUnit;
020
021import org.forgerock.opendj.config.server.ConfigException;
022import org.opends.server.api.MonitorData;
023import org.forgerock.opendj.server.config.server.MonitorProviderCfg;
024import org.opends.server.api.MonitorProvider;
025import org.opends.server.extensions.ParallelWorkQueue;
026import org.opends.server.types.InitializationException;
027
028/**
029 * This class defines a Directory Server monitor that can be used to provide
030 * information about the state of the work queue.
031 */
032public class ParallelWorkQueueMonitor
033       extends MonitorProvider<MonitorProviderCfg>
034       implements Runnable
035{
036  /** The name to use for the monitor attribute that provides the current request backlog. */
037  public static final String ATTR_CURRENT_BACKLOG = "currentRequestBacklog";
038  /** The name to use for the monitor attribute that provides the average request backlog. */
039  public static final String ATTR_AVERAGE_BACKLOG = "averageRequestBacklog";
040  /**
041   * The name to use for the monitor attribute that provides the maximum
042   * observed request backlog.
043   */
044  public static final String ATTR_MAX_BACKLOG = "maxRequestBacklog";
045  /**
046   * The name to use for the monitor attribute that provides the total number of
047   * operations submitted.
048   */
049  public static final String ATTR_OPS_SUBMITTED = "requestsSubmitted";
050
051
052  /** The maximum backlog observed by polling the queue. */
053  private int maxBacklog;
054
055  /** The total number of times the backlog has been polled. */
056  private long numPolls;
057
058  /** The total backlog observed from periodic polling. */
059  private long totalBacklog;
060
061  /** The parallel work queue instance with which this monitor is associated. */
062  private ParallelWorkQueue workQueue;
063
064  /**
065   * Initializes this monitor provider.  Note that no initialization should be
066   * done here, since it should be performed in the
067   * <CODE>initializeMonitorProvider</CODE> class.
068   *
069   * @param  workQueue  The work queue with which this monitor is associated.
070   */
071  public ParallelWorkQueueMonitor(ParallelWorkQueue workQueue)
072  {
073    this.workQueue = workQueue;
074  }
075
076
077
078  /** {@inheritDoc} */
079  @Override
080  public void initializeMonitorProvider(MonitorProviderCfg configuration)
081         throws ConfigException, InitializationException
082  {
083    maxBacklog   = 0;
084    totalBacklog = 0;
085    numPolls     = 0;
086    scheduleUpdate(this, 0, 10, TimeUnit.SECONDS);
087  }
088
089
090
091  /**
092   * Retrieves the name of this monitor provider.  It should be unique among all
093   * monitor providers, including all instances of the same monitor provider.
094   *
095   * @return  The name of this monitor provider.
096   */
097  @Override
098  public String getMonitorInstanceName()
099  {
100    return "Work Queue";
101  }
102
103  @Override
104  public void run()
105  {
106    int backlog = workQueue.size();
107    totalBacklog += backlog;
108    numPolls++;
109
110    if (backlog > maxBacklog)
111    {
112      maxBacklog = backlog;
113    }
114  }
115
116  @Override
117  public MonitorData getMonitorData()
118  {
119    int backlog = workQueue.size();
120    totalBacklog += backlog;
121    numPolls++;
122    if (backlog > maxBacklog)
123    {
124      maxBacklog = backlog;
125    }
126
127    long averageBacklog = (long) (1.0 * totalBacklog / numPolls);
128
129    final MonitorData monitorAttrs = new MonitorData(4);
130    monitorAttrs.add(ATTR_CURRENT_BACKLOG, backlog);
131    monitorAttrs.add(ATTR_AVERAGE_BACKLOG, averageBacklog);
132    monitorAttrs.add(ATTR_MAX_BACKLOG, maxBacklog);
133    monitorAttrs.add(ATTR_OPS_SUBMITTED, workQueue.getOpsSubmitted());
134    return monitorAttrs;
135  }
136}