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.TraditionalWorkQueue;
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 TraditionalWorkQueueMonitor
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 name to use for the monitor attribute that provides the total number of
053   * requests that have been rejected because the work queue was full.
054   */
055  public static final String ATTR_OPS_REJECTED_QUEUE_FULL = "requestsRejectedDueToQueueFull";
056
057
058  /** The maximum backlog observed by polling the queue. */
059  private int maxBacklog;
060  /** The total number of times the backlog has been polled. */
061  private long numPolls;
062  /** The total backlog observed from periodic polling. */
063  private long totalBacklog;
064  /** The traditional work queue instance with which this monitor is associated. */
065  private TraditionalWorkQueue workQueue;
066
067
068  /**
069   * Initializes this monitor provider.  Note that no initialization should be
070   * done here, since it should be performed in the
071   * <CODE>initializeMonitorProvider</CODE> class.
072   *
073   * @param  workQueue  The work queue with which this monitor is associated.
074   */
075  public TraditionalWorkQueueMonitor(TraditionalWorkQueue workQueue)
076  {
077    this.workQueue = workQueue;
078  }
079
080
081
082  /** {@inheritDoc} */
083  @Override
084  public void initializeMonitorProvider(MonitorProviderCfg configuration)
085         throws ConfigException, InitializationException
086  {
087    maxBacklog   = 0;
088    totalBacklog = 0;
089    numPolls     = 0;
090    scheduleUpdate(this, 0, 10, TimeUnit.SECONDS);
091  }
092
093
094
095  /**
096   * Retrieves the name of this monitor provider.  It should be unique among all
097   * monitor providers, including all instances of the same monitor provider.
098   *
099   * @return  The name of this monitor provider.
100   */
101  @Override
102  public String getMonitorInstanceName()
103  {
104    return "Work Queue";
105  }
106
107  @Override
108  public void run()
109  {
110    int backlog = workQueue.size();
111    totalBacklog += backlog;
112    numPolls++;
113
114    if (backlog > maxBacklog)
115    {
116      maxBacklog = backlog;
117    }
118  }
119
120  @Override
121  public MonitorData getMonitorData()
122  {
123    int backlog = workQueue.size();
124    totalBacklog += backlog;
125    numPolls++;
126    if (backlog > maxBacklog)
127    {
128      maxBacklog = backlog;
129    }
130    long averageBacklog = (long) (1.0 * totalBacklog / numPolls);
131
132    final MonitorData monitorAttrs = new MonitorData(5);
133    monitorAttrs.add(ATTR_CURRENT_BACKLOG, backlog);
134    monitorAttrs.add(ATTR_AVERAGE_BACKLOG, averageBacklog);
135    monitorAttrs.add(ATTR_MAX_BACKLOG, maxBacklog);
136    monitorAttrs.add(ATTR_OPS_SUBMITTED, workQueue.getOpsSubmitted());
137    monitorAttrs.add(ATTR_OPS_REJECTED_QUEUE_FULL, workQueue.getOpsRejectedDueToQueueFull());
138    return monitorAttrs;
139  }
140}