From 779e8d65b79e9fada98dd5f070c3359bb963b169 Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Fri, 17 Sep 2010 22:14:02 +0000
Subject: [PATCH] Implement a new resource limit policy which restricts the number of operations which can be performed during a time interval

---
 opends/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyFactory.java |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/opends/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyFactory.java b/opends/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyFactory.java
index ed5b347..42d8084 100644
--- a/opends/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyFactory.java
+++ b/opends/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyFactory.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *    Copyright 2009 Sun Microsystems, Inc.
+ *    Copyright 2009-2010 Sun Microsystems, Inc.
  */
 package org.opends.server.core.networkgroups;
 
@@ -131,7 +131,13 @@
     // The maximum time for a search.
     private int timeLimit;
 
+    // The time interval for throughput limits
+    private long interval;
+    private long intervalStartTime = 0;
 
+    // The max number of operations during the interval
+    private int maxOperationsPerInterval;
+    private int operationsPerInterval = 0;
 
     /**
      * Creates a new resource limits policy.
@@ -306,6 +312,29 @@
         }
       }
 
+      // Check the throughput
+      if (operation != null && maxOperationsPerInterval > 0) {
+        synchronized(mutex) {
+          long now = System.currentTimeMillis();
+          // if the start time has never been set, or the interval has already
+          // expired, reset the start time and number of operations
+          if (intervalStartTime == 0 || now > (intervalStartTime + interval)) {
+            intervalStartTime = now;
+            operationsPerInterval = 0;
+          }
+
+          operationsPerInterval++;
+          if (operationsPerInterval > maxOperationsPerInterval) {
+            messages.add(INFO_ERROR_MAX_THROUGHPUT_EXCEEDED.get(
+                maxOperationsPerInterval,interval));
+            result = false;
+          }
+        }
+        if (!result) {
+          return result;
+        }
+      }
+
       return true;
     }
 
@@ -438,6 +467,19 @@
       }
 
       minSearchSubstringLength = configuration.getMinSubstringLength();
+
+      // Update the Max Ops Per Time Interval parameters
+      long previousInterval = interval;
+      int previousMax = maxOperationsPerInterval;
+
+      interval = configuration.getMaxOpsInterval();
+      maxOperationsPerInterval = configuration.getMaxOpsPerInterval();
+      // If the values have been modified, reset the counters
+      if ((previousInterval != interval)
+          || (previousMax != maxOperationsPerInterval)) {
+        intervalStartTime = 0;
+        operationsPerInterval = 0;
+      }
     }
   }
 
@@ -448,7 +490,13 @@
       ResourceLimitsQOSPolicyCfg configuration,
       List<Message> unacceptableReasons)
   {
-    // Always valid.
+    // maxOpsPerInterval must be positive
+    long tmpMaxOps = configuration.getMaxOpsInterval();
+    if (tmpMaxOps < 0) {
+      unacceptableReasons.add(ERR_MAX_OPS_PER_INTERVAL.get(tmpMaxOps));
+      return false;
+    }
+
     return true;
   }
 

--
Gitblit v1.10.0