001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.monitors;
028
029import static org.opends.server.core.DirectoryServer.*;
030
031import java.util.ArrayList;
032import java.util.concurrent.TimeUnit;
033
034import org.forgerock.opendj.config.server.ConfigException;
035import org.opends.server.admin.std.server.MonitorProviderCfg;
036import org.opends.server.api.MonitorProvider;
037import org.opends.server.extensions.TraditionalWorkQueue;
038import org.opends.server.types.Attribute;
039import org.opends.server.types.AttributeType;
040import org.opends.server.types.Attributes;
041import org.opends.server.types.InitializationException;
042
043/**
044 * This class defines a Directory Server monitor that can be used to provide
045 * information about the state of the work queue.
046 */
047public class TraditionalWorkQueueMonitor
048       extends MonitorProvider<MonitorProviderCfg>
049       implements Runnable
050{
051  /** The name to use for the monitor attribute that provides the current request backlog. */
052  public static final String ATTR_CURRENT_BACKLOG = "currentRequestBacklog";
053  /** The name to use for the monitor attribute that provides the average request backlog. */
054  public static final String ATTR_AVERAGE_BACKLOG = "averageRequestBacklog";
055  /**
056   * The name to use for the monitor attribute that provides the maximum
057   * observed request backlog.
058   */
059  public static final String ATTR_MAX_BACKLOG = "maxRequestBacklog";
060  /**
061   * The name to use for the monitor attribute that provides the total number of
062   * operations submitted.
063   */
064  public static final String ATTR_OPS_SUBMITTED = "requestsSubmitted";
065
066  /**
067   * The name to use for the monitor attribute that provides the total number of
068   * requests that have been rejected because the work queue was full.
069   */
070  public static final String ATTR_OPS_REJECTED_QUEUE_FULL = "requestsRejectedDueToQueueFull";
071
072
073  /** The maximum backlog observed by polling the queue. */
074  private int maxBacklog;
075  /** The total number of times the backlog has been polled. */
076  private long numPolls;
077  /** The total backlog observed from periodic polling. */
078  private long totalBacklog;
079  /** The traditional work queue instance with which this monitor is associated. */
080  private TraditionalWorkQueue workQueue;
081
082
083  /**
084   * Initializes this monitor provider.  Note that no initialization should be
085   * done here, since it should be performed in the
086   * <CODE>initializeMonitorProvider</CODE> class.
087   *
088   * @param  workQueue  The work queue with which this monitor is associated.
089   */
090  public TraditionalWorkQueueMonitor(TraditionalWorkQueue workQueue)
091  {
092    this.workQueue = workQueue;
093  }
094
095
096
097  /** {@inheritDoc} */
098  @Override
099  public void initializeMonitorProvider(MonitorProviderCfg configuration)
100         throws ConfigException, InitializationException
101  {
102    maxBacklog   = 0;
103    totalBacklog = 0;
104    numPolls     = 0;
105    scheduleUpdate(this, 0, 10, TimeUnit.SECONDS);
106  }
107
108
109
110  /**
111   * Retrieves the name of this monitor provider.  It should be unique among all
112   * monitor providers, including all instances of the same monitor provider.
113   *
114   * @return  The name of this monitor provider.
115   */
116  @Override
117  public String getMonitorInstanceName()
118  {
119    return "Work Queue";
120  }
121
122
123  /** {@inheritDoc} */
124  @Override
125  public void run()
126  {
127    int backlog = workQueue.size();
128    totalBacklog += backlog;
129    numPolls++;
130
131    if (backlog > maxBacklog)
132    {
133      maxBacklog = backlog;
134    }
135  }
136
137
138
139  /**
140   * Retrieves a set of attributes containing monitor data that should be
141   * returned to the client if the corresponding monitor entry is requested.
142   *
143   * @return  A set of attributes containing monitor data that should be
144   *          returned to the client if the corresponding monitor entry is
145   *          requested.
146   */
147  @Override
148  public ArrayList<Attribute> getMonitorData()
149  {
150    int backlog = workQueue.size();
151    totalBacklog += backlog;
152    numPolls++;
153    if (backlog > maxBacklog)
154    {
155      maxBacklog = backlog;
156    }
157
158    long averageBacklog = (long) (1.0 * totalBacklog / numPolls);
159
160    long opsSubmitted = workQueue.getOpsSubmitted();
161    long rejectedQueueFull = workQueue.getOpsRejectedDueToQueueFull();
162
163    ArrayList<Attribute> monitorAttrs = new ArrayList<>();
164    putAttribute(monitorAttrs, ATTR_CURRENT_BACKLOG, backlog);
165    putAttribute(monitorAttrs, ATTR_AVERAGE_BACKLOG, averageBacklog);
166    putAttribute(monitorAttrs, ATTR_MAX_BACKLOG, maxBacklog);
167    // The total number of operations submitted.
168    putAttribute(monitorAttrs, ATTR_OPS_SUBMITTED, opsSubmitted);
169    // The total number of operations rejected due to a full work queue.
170    putAttribute(monitorAttrs, ATTR_OPS_REJECTED_QUEUE_FULL, rejectedQueueFull);
171    return monitorAttrs;
172  }
173
174  private void putAttribute(ArrayList<Attribute> monitorAttrs, String attrName, Object value)
175  {
176    AttributeType attrType = getAttributeTypeOrDefault(attrName, attrName, getDefaultIntegerSyntax());
177    monitorAttrs.add(Attributes.create(attrType, String.valueOf(value)));
178  }
179}