incubator-connectors-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1208391 - in /incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests: InMemAtomicNativeNonblockingKeyValueStore.java MultiThreadTest.java
Date Wed, 30 Nov 2011 11:43:38 GMT
Author: kwright
Date: Wed Nov 30 11:43:37 2011
New Revision: 1208391

URL: http://svn.apache.org/viewvc?rev=1208391&view=rev
Log:
Start work on a non-blocking test substrate so we can truly assess parallelism

Added:
    incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/InMemAtomicNativeNonblockingKeyValueStore.java
  (with props)
Modified:
    incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/MultiThreadTest.java

Added: incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/InMemAtomicNativeNonblockingKeyValueStore.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/InMemAtomicNativeNonblockingKeyValueStore.java?rev=1208391&view=auto
==============================================================================
--- incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/InMemAtomicNativeNonblockingKeyValueStore.java
(added)
+++ incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/InMemAtomicNativeNonblockingKeyValueStore.java
Wed Nov 30 11:43:37 2011
@@ -0,0 +1,151 @@
+/* $Id$ */
+
+/**
+* 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.warthog.tests;
+
+import org.apache.warthog.interfaces.*;
+import java.util.*;
+
+/** In-memory key value store, native hash, for testing.
+*/
+public class InMemAtomicNativeNonblockingKeyValueStore implements WHAtomicKeyValueStore
+{
+  protected Map<WHKey,WHValue> database;
+  
+  /** Constructor */
+  public InMemAtomicNativeNonblockingKeyValueStore(int initialSize)
+  {
+    database = new HashMap<WHKey,WHValue>(initialSize);
+  }
+  
+  /** Get a value */
+  public WHValue get(WHKey key)
+    throws WHException
+  {
+    synchronized (database)
+    {
+      return database.get(key);
+    }
+  }
+  
+  /** Check a bunch of values atomically for consistency.
+  *@param checkValues is a map of keys/values that must be unchanged.  If any value is
+  * changed, a WHDeadlockException is thrown.
+  */
+  public void check(WHKeyMap checkValues)
+    throws WHException
+  {
+    synchronized (database)
+    {
+      WHKeyIterator iterator = checkValues.iterator();
+      while (iterator.hasNext())
+      {
+        WHKey key = iterator.next();
+        WHValue value = database.get(key);
+        WHValue otherValue = checkValues.get(key);
+	if (value == null && otherValue == null)
+          continue;
+        if (value != null && otherValue != null && value.equals(otherValue))
+          continue;
+        //System.out.println("Type of key in contention = "+key.getClass().getName());
+        throw new WHConcurrencyException();
+      }
+    }
+  }
+  
+  /** Set a bunch of values atomically.
+  *@param checkValues is a map of keys/values that must be unchanged in order for the
+  * commit to proceed.  If these values are detected to have been changed, a WHDeadlockException
+  * will be thrown.  Null values are permitted.
+  *@param setValues is a map of keys to set to specified new values.  A null value implies
removal of
+  * the key.
+  */
+  public void setAll(WHKeyMap checkValues, WHKeyMap setValues)
+    throws WHException
+  {
+    synchronized (database)
+    {
+      check(checkValues);
+      WHKeyIterator iterator = setValues.iterator();
+      while (iterator.hasNext())
+      {
+        WHKey key = iterator.next();
+        WHValue value = setValues.get(key);
+        if (value == null)
+          database.remove(key);
+        else
+          database.put(key,value);
+      }
+    }
+  }
+  
+  // Basic operations
+  
+  protected void put(WHKey key, WHValue value)
+  {
+    synchronized (database)
+    {
+      database.put(key,value);
+    }
+  }
+  
+  protected void remove(WHKey key)
+  {
+    synchronized (database)
+    {
+      database.remove(key);
+    }
+  }
+  
+  protected boolean setIfNonexisting(WHKey key, WHValue value)
+  {
+    synchronized (database)
+    {
+      if (database.containsKey(key))
+        return false;
+      database.put(key,value);
+    }
+    return true;
+  }
+  
+  // Lock class
+  
+  protected static class LockKey
+  {
+    protected WHKey key;
+    
+    public LockKey(WHKey key)
+    {
+      this.key = key;
+    }
+    
+    public int hashCode()
+    {
+      return key.hashCode() + 1532;
+    }
+    
+    public boolean equals(Object o)
+    {
+      if (o.getClass() != getClass())
+        return false;
+      return ((LockKey)o).key.equals(key);
+    }
+  }
+  
+}

Propchange: incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/InMemAtomicNativeNonblockingKeyValueStore.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/InMemAtomicNativeNonblockingKeyValueStore.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/MultiThreadTest.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/MultiThreadTest.java?rev=1208391&r1=1208390&r2=1208391&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/MultiThreadTest.java
(original)
+++ incubator/lcf/branches/CONNECTORS-286/warthog/src/test/java/org/apache/warthog/tests/MultiThreadTest.java
Wed Nov 30 11:43:37 2011
@@ -29,15 +29,27 @@ public class MultiThreadTest
 {
 
   @Test
+  public void singleThreadBaselineTest()
+    throws Exception
+  {
+    doWrites(1);
+  }
+  
+  @Test
   public void writeOnlyTest()
     throws Exception
   {
-    int threadCount = 4;
+    doWrites(8);
+  }
+  
+  protected void doWrites(int threadCount)
+    throws Exception
+  {
     int totalInserts = 10000;
     
     int repeatCount = totalInserts/threadCount;
 
-    WHAtomicKeyValueStore store = new InMemAtomicNativeKeyValueStore(16777216);
+    WHAtomicKeyValueStore store = new InMemAtomicNativeNonblockingKeyValueStore(16777216);
     
     WHTableStore ts = new TableStore(new InMemNativeTransactionalStoreImpl(store));
 
@@ -114,7 +126,7 @@ public class MultiThreadTest
     
     int repeatCount = totalCount/threadCount;
 
-    WHAtomicKeyValueStore store = new InMemAtomicNativeKeyValueStore(16777216);
+    WHAtomicKeyValueStore store = new InMemAtomicNativeNonblockingKeyValueStore(16777216);
     
     WHTableStore ts = new TableStore(new InMemNativeTransactionalStoreImpl(store));
 



Mime
View raw message