You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by ma...@apache.org on 2015/04/03 01:12:52 UTC

incubator-ranger git commit: RANGER-323: test code for LRU cache used in policy engine

Repository: incubator-ranger
Updated Branches:
  refs/heads/master 3fae96ce8 -> 0d5844ae8


RANGER-323: test code for LRU cache used in policy engine

Signed-off-by: Madhan Neethiraj <ma...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ranger/commit/0d5844ae
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ranger/tree/0d5844ae
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ranger/diff/0d5844ae

Branch: refs/heads/master
Commit: 0d5844ae8f4da7d91aa8d8d6524f154b293aee99
Parents: 3fae96c
Author: Abhay Kulkarni <ak...@hortonworks.com>
Authored: Thu Apr 2 16:12:32 2015 -0700
Committer: Madhan Neethiraj <ma...@apache.org>
Committed: Thu Apr 2 16:12:49 2015 -0700

----------------------------------------------------------------------
 .../ranger/plugin/policyengine/CacheMap.java    |  22 +++-
 .../plugin/policyengine/TestCacheMap.java       | 123 +++++++++++++++++++
 2 files changed, 140 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/0d5844ae/agents-common/src/main/java/org/apache/ranger/plugin/policyengine/CacheMap.java
----------------------------------------------------------------------
diff --git a/agents-common/src/main/java/org/apache/ranger/plugin/policyengine/CacheMap.java b/agents-common/src/main/java/org/apache/ranger/plugin/policyengine/CacheMap.java
index c5f2fc0..42370cc 100644
--- a/agents-common/src/main/java/org/apache/ranger/plugin/policyengine/CacheMap.java
+++ b/agents-common/src/main/java/org/apache/ranger/plugin/policyengine/CacheMap.java
@@ -18,24 +18,36 @@
  */
 package org.apache.ranger.plugin.policyengine;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codehaus.jackson.map.ser.StdSerializers;
+
 import java.util.LinkedHashMap;
 import java.util.Map;
 
 public class CacheMap<K, V> extends LinkedHashMap<K, V> {
     private static final long serialVersionUID = 1L;
+    private static final Log LOG = LogFactory.getLog(CacheMap.class);
+
 
     private static final float RANGER_CACHE_DEFAULT_LOAD_FACTOR = 0.75f;
 
-    protected int maxCapacity;
+    protected int initialCapacity;
 
-    public CacheMap(int maxCapacity) {
-        super(maxCapacity, CacheMap.RANGER_CACHE_DEFAULT_LOAD_FACTOR, true); // true for access-order
+    public CacheMap(int initialCapacity) {
+        super(initialCapacity, CacheMap.RANGER_CACHE_DEFAULT_LOAD_FACTOR, true); // true for access-order
 
-        this.maxCapacity = maxCapacity;
+        this.initialCapacity = initialCapacity;
     }
 
     @Override
     protected boolean removeEldestEntry(Map.Entry eldest) {
-        return size() > maxCapacity;
+        boolean result = size() > initialCapacity;
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("CacheMap.removeEldestEntry(), result:"+ result);
+        }
+
+        return result;
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/0d5844ae/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestCacheMap.java
----------------------------------------------------------------------
diff --git a/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestCacheMap.java b/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestCacheMap.java
new file mode 100644
index 0000000..19362f2
--- /dev/null
+++ b/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestCacheMap.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.plugin.policyengine;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.Set;
+
+public class TestCacheMap {
+	private static final Log LOG = LogFactory.getLog(TestCacheMap.class);
+	private static CacheMap<String, String> testCacheMap = null;
+	private static int initialCapacity = 16;
+
+
+	@BeforeClass
+	public static void setUpBeforeClass() throws Exception {
+		if(LOG.isDebugEnabled()) {
+			LOG.debug("==> TestCacheMap.setUpBeforeClass(), initialCapacity:" + initialCapacity);
+		}
+
+		testCacheMap = new CacheMap<String, String>(initialCapacity);
+
+		if(LOG.isDebugEnabled()) {
+			LOG.debug("<== TestCacheMap.setUpBeforeClass(), initialCapacity:" + initialCapacity);
+		}
+	}
+	@AfterClass
+	public static void tearDownAfterClass() throws Exception {
+	}
+
+	@Test
+	public void runTests() {
+
+		if(LOG.isDebugEnabled()) {
+			LOG.debug("==> TestCacheMap.runTests(), First batch of " + initialCapacity + " inserts starting from 0");
+		}
+		for (int i = 0; i < initialCapacity; i++) {
+			String key = String.valueOf(i);
+			String value = key;
+
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("TestCacheMap.runTests(), Inserting into Cache, key:" + key + ", value:" + value);
+			}
+			testCacheMap.put(key, value);
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("TestCacheMap.runTests(), Cache Size after insert(): " + testCacheMap.size());
+			}
+		}
+
+		if(LOG.isDebugEnabled()) {
+			LOG.debug("TestCacheMap.runTests(), First batch of " + initialCapacity/2 + " retrieves counting down from " + (initialCapacity/2-1));
+		}
+
+		for (int i = initialCapacity/2 - 1; i >= 0; i--) {
+			String key = String.valueOf(i);
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("TestCacheMap.runTests(), Searching Cache, key:" + key);
+			}
+			String value = testCacheMap.get(key);
+			if (value == null || !value.equals(key)) {
+				LOG.error("TestCacheMap.runTests(), Did not get correct value for key, key:" + key + ", value:" + value);
+			}
+		}
+		if(LOG.isDebugEnabled()) {
+			LOG.debug("TestCacheMap.runTests(), Second batch of " + initialCapacity/2 + " inserts starting from " + initialCapacity);
+		}
+		for (int i = initialCapacity; i < initialCapacity+initialCapacity/2; i++) {
+			String key = String.valueOf(i);
+			String value = key;
+
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("TestCacheMap.runTests(), Inserting into Cache, key:" + key + ", value:" + value);
+			}
+			testCacheMap.put(key, value);
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("TestCacheMap.runTests(), Cache Size after insert(): " + testCacheMap.size());
+			}
+		}
+
+		Set<String> keySet = testCacheMap.keySet();
+
+		int i = 0;
+
+		if (LOG.isDebugEnabled()) {
+			LOG.debug("TestCacheMap.runTests(), KeySet Size:" + keySet.size());
+			LOG.debug("TestCacheMap.runTests(), printing keys..");
+		}
+
+		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
+			String key = iterator.next();
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("TestCacheMap.runTests(), index:" + i++ + ", key:" + key);
+			}
+		}
+
+		if(LOG.isDebugEnabled()) {
+			LOG.debug("<== TestCacheMap.runTests()");
+		}
+
+	}
+}