diff options
| author | Brian S. O'Neill <bronee@gmail.com> | 2008-05-04 17:36:39 +0000 | 
|---|---|---|
| committer | Brian S. O'Neill <bronee@gmail.com> | 2008-05-04 17:36:39 +0000 | 
| commit | a2f58ccccf52039db5dbfa2de19ad89623643973 (patch) | |
| tree | ad7dfba04a0acaa9ca271315a80a04ae3bf2d118 /src | |
| parent | c73a73451b7f08b78ce8b805bcf73378cde240b0 (diff) | |
Merged map repository implementation from map branch.
Diffstat (limited to 'src')
4 files changed, 705 insertions, 0 deletions
diff --git a/src/test/java/com/amazon/carbonado/repo/map/TestLockStates.java b/src/test/java/com/amazon/carbonado/repo/map/TestLockStates.java new file mode 100644 index 0000000..4450179 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/repo/map/TestLockStates.java @@ -0,0 +1,314 @@ +/*
 + * Copyright 2007 Amazon Technologies, Inc. or its affiliates.
 + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 + * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 + *
 + * Licensed 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 com.amazon.carbonado.repo.map;
 +
 +import java.util.concurrent.TimeUnit;
 +
 +/**
 + * 
 + *
 + * @author Brian S O'Neill
 + */
 +public class TestLockStates {
 +    private static void lockForRead(UpgradableLock lock, TestLockStates locker) {
 +        //System.out.println("read lock");
 +        lock.lockForRead(locker);
 +        //System.out.println(lock);
 +    }
 +
 +    private static void unlockFromRead(UpgradableLock lock, TestLockStates locker) {
 +        //System.out.println("read unlock");
 +        lock.unlockFromRead(locker);
 +        //System.out.println(lock);
 +    }
 +
 +    private static void lockForUpgrade(UpgradableLock lock, TestLockStates locker) {
 +        //System.out.println("upgrade lock");
 +        lock.lockForUpgrade(locker);
 +        //System.out.println(lock);
 +    }
 +
 +    private static void unlockFromUpgrade(UpgradableLock lock, TestLockStates locker) {
 +        //System.out.println("upgrade unlock");
 +        lock.unlockFromUpgrade(locker);
 +        //System.out.println(lock);
 +    }
 +
 +    private static void lockForWrite(UpgradableLock lock, TestLockStates locker)
 +        throws InterruptedException
 +    {
 +        //System.out.println("write lock");
 +        lock.lockForWriteInterruptibly(locker);
 +        //System.out.println(lock);
 +    }
 +
 +    private static boolean tryLockForWrite(UpgradableLock lock, TestLockStates locker, int timeout)
 +        throws InterruptedException
 +    {
 +        //System.out.println("write lock");
 +        boolean result = lock.tryLockForWrite(locker, timeout, TimeUnit.MILLISECONDS);
 +        //System.out.println(lock);
 +        return result;
 +    }
 +
 +    private static void unlockFromWrite(UpgradableLock lock, TestLockStates locker) {
 +        //System.out.println("write unlock");
 +        lock.unlockFromWrite(locker);
 +        //System.out.println(lock);
 +    }
 +
 +    public static void main(String[] args) throws Exception {
 +        UpgradableLock lock = new UpgradableLock();
 +        TestLockStates locker = new TestLockStates();
 +        TestLockStates locker2 = new TestLockStates();
 +
 +        /*
 +        final Thread main = Thread.currentThread();
 +
 +        new Thread() {
 +            public void run() {
 +                try {
 +                    Thread.sleep(5000);
 +                } catch (InterruptedException e) {
 +                }
 +                main.interrupt();
 +            }
 +        }.start();
 +        */
 +
 +        System.out.println("0 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            // start with no locks
 +
 +            lockForRead(lock, locker);
 +            unlockFromRead(lock, locker);
 +
 +            lockForUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +
 +            lockForWrite(lock, locker);
 +            unlockFromWrite(lock, locker);
 +        }
 +
 +        System.out.println("1 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForRead(lock, locker);
 +
 +            lockForRead(lock, locker);
 +            unlockFromRead(lock, locker);
 +
 +            lockForUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +
 +            // should deadlock
 +            if (tryLockForWrite(lock, locker, 1000)) {
 +                System.out.println("***** did not deadlock!!!");
 +                unlockFromWrite(lock, locker);
 +            }
 +
 +            unlockFromRead(lock, locker);
 +        }
 +
 +        System.out.println("2 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForRead(lock, locker);
 +            lockForUpgrade(lock, locker);
 +
 +            lockForRead(lock, locker);
 +            unlockFromRead(lock, locker);
 +
 +            lockForUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +
 +            // should deadlock
 +            if (tryLockForWrite(lock, locker, 1000)) {
 +                System.out.println("***** did not deadlock!!!");
 +                unlockFromWrite(lock, locker);
 +            }
 +
 +            unlockFromRead(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +        }
 +
 +        System.out.println("3 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForUpgrade(lock, locker);
 +
 +            lockForRead(lock, locker);
 +            unlockFromRead(lock, locker);
 +
 +            lockForUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +
 +            lockForWrite(lock, locker);
 +            unlockFromWrite(lock, locker);
 +
 +            unlockFromUpgrade(lock, locker);
 +        }
 +
 +        System.out.println("4 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForWrite(lock, locker);
 +
 +            lockForRead(lock, locker);
 +            unlockFromRead(lock, locker);
 +
 +            lockForUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +
 +            lockForWrite(lock, locker);
 +            unlockFromWrite(lock, locker);
 +
 +            unlockFromWrite(lock, locker);
 +        }
 +
 +        System.out.println("5 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForWrite(lock, locker);
 +            lockForRead(lock, locker);
 +
 +            lockForRead(lock, locker);
 +            unlockFromRead(lock, locker);
 +
 +            lockForUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +
 +            lockForWrite(lock, locker);
 +            unlockFromWrite(lock, locker);
 +
 +            unlockFromRead(lock, locker);
 +            unlockFromWrite(lock, locker);
 +        }
 +
 +        System.out.println("6 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForWrite(lock, locker);
 +            lockForUpgrade(lock, locker);
 +            lockForRead(lock, locker);
 +
 +            lockForRead(lock, locker);
 +            unlockFromRead(lock, locker);
 +
 +            lockForUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +
 +            lockForWrite(lock, locker);
 +            unlockFromWrite(lock, locker);
 +
 +            unlockFromRead(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +            unlockFromWrite(lock, locker);
 +        }
 +
 +        System.out.println("7 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForWrite(lock, locker);
 +            lockForUpgrade(lock, locker);
 +
 +            lockForRead(lock, locker);
 +            unlockFromRead(lock, locker);
 +
 +            lockForUpgrade(lock, locker);
 +            lockForUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +
 +            lockForWrite(lock, locker);
 +            unlockFromWrite(lock, locker);
 +
 +            unlockFromUpgrade(lock, locker);
 +            unlockFromWrite(lock, locker);
 +        }
 +
 +        System.out.println("8 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForUpgrade(lock, locker);
 +            lockForWrite(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +            unlockFromWrite(lock, locker);
 +        }
 +
 +        System.out.println("9 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForUpgrade(lock, locker);
 +            lockForWrite(lock, locker);
 +            lockForUpgrade(lock, locker);
 +            lockForWrite(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +            unlockFromWrite(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +            unlockFromWrite(lock, locker);
 +        }
 +
 +        System.out.println("10 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForUpgrade(lock, locker);
 +            lockForUpgrade(lock, locker);
 +            lockForWrite(lock, locker);
 +            lockForWrite(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +            lockForUpgrade(lock, locker);
 +            unlockFromWrite(lock, locker);
 +            unlockFromWrite(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +        }
 +
 +        System.out.println("11 ----------------------");
 +        System.out.println(lock);
 +
 +        {
 +            lockForWrite(lock, locker);
 +            unlockFromUpgrade(lock, locker);
 +            try {
 +                unlockFromWrite(lock, locker);
 +                System.out.println("***** unlocked");
 +            } catch (IllegalMonitorStateException e) {
 +            }
 +        }
 +
 +        System.out.println("12 ----------------------");
 +        System.out.println(lock);
 +     }
 +
 +    public TestLockStates() {
 +    }
 +}
 diff --git a/src/test/java/com/amazon/carbonado/repo/map/TestLockStress.java b/src/test/java/com/amazon/carbonado/repo/map/TestLockStress.java new file mode 100644 index 0000000..efbe815 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/repo/map/TestLockStress.java @@ -0,0 +1,161 @@ +/*
 + * Copyright 2008 Amazon Technologies, Inc. or its affiliates.
 + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 + * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 + *
 + * Licensed 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 com.amazon.carbonado.repo.map;
 +
 +import java.util.*;
 +
 +/**
 + * 
 + *
 + * @author Brian S O'Neill
 + */
 +public class TestLockStress {
 +    static long cSharedValue;
 +
 +    static volatile boolean cStop;
 +
 +    /**
 +     * @param args args[0]: readers, args[1]: upgraders, args[2]: writers, args[3]: run time
 +     */
 +    public static void main(String[] args) throws Exception {
 +        final int readers = Integer.parseInt(args[0]);
 +        final int upgraders = Integer.parseInt(args[1]);
 +        final int writers = Integer.parseInt(args[2]);
 +        final int runTimeSeconds = Integer.parseInt(args[3]);
 +
 +        final UpgradableLock lock = new UpgradableLock();
 +
 +        List<TestThread> threads = new ArrayList<TestThread>();
 +
 +        for (int i=0; i<readers; i++) {
 +            TestThread t = new TestThread("reader " + i) {
 +                public void run() {
 +                    while (!cStop) {
 +                        lock.lockForRead(this);
 +                        try {
 +                            long value = cSharedValue;
 +                            // Do some "work"
 +                            String.valueOf(value);
 +                            long again = cSharedValue;
 +                            if (again != value) {
 +                                throw new AssertionError("" + again + " != " + value);
 +                            }
 +                            mReadCount++;
 +                        } finally {
 +                            lock.unlockFromRead(this);
 +                        }
 +                    }
 +                }
 +            };
 +            threads.add(t);
 +            t.start();
 +        }
 +
 +        for (int i=0; i<upgraders; i++) {
 +            TestThread t = new TestThread("upgrader " + i) {
 +                public void run() {
 +                    while (!cStop) {
 +                        lock.lockForUpgrade(this);
 +                        try {
 +                            long value = cSharedValue;
 +                            // Do some "work"
 +                            String.valueOf(value);
 +                            assert(cSharedValue == value);
 +                            lock.lockForWrite(this);
 +                            assert(cSharedValue == value);
 +                            try {
 +                                cSharedValue = value + 1;
 +                            } finally {
 +                                lock.unlockFromWrite(this);
 +                            }
 +                            mAdjustCount++;
 +                        } finally {
 +                            lock.unlockFromUpgrade(this);
 +                        }
 +                    }
 +                }
 +            };
 +            threads.add(t);
 +            t.start();
 +        }
 +
 +        for (int i=0; i<writers; i++) {
 +            TestThread t = new TestThread("writer " + i) {
 +                public void run() {
 +                    while (!cStop) {
 +                        lock.lockForWrite(this);
 +                        try {
 +                            long value = cSharedValue;
 +                            // Do some "work"
 +                            String.valueOf(value);
 +                            assert(cSharedValue == value);
 +                            cSharedValue = value + 1;
 +                            mAdjustCount++;
 +                        } finally {
 +                            lock.unlockFromWrite(this);
 +                        }
 +                    }
 +                }
 +            };
 +            threads.add(t);
 +            t.start();
 +        }
 +
 +        Thread.sleep(1000L * runTimeSeconds);
 +        cStop = true;
 +
 +        for (TestThread t : threads) {
 +            t.join();
 +        }
 +
 +        long reads = 0;
 +        for (TestThread t : threads) {
 +            reads += t.mReadCount;
 +        }
 +
 +        long expected = 0;
 +        for (TestThread t : threads) {
 +            expected += t.mAdjustCount;
 +        }
 +
 +        System.out.println("Reads:    " + reads);
 +        System.out.println("Expected: " + expected);
 +        System.out.println("Actual:   " + cSharedValue);
 +
 +        if (expected == cSharedValue) {
 +            System.out.println("SUCCESS");
 +        } else {
 +            System.out.println("FAILURE");
 +        }
 +
 +        System.out.println();
 +        for (TestThread t : threads) {
 +            System.out.println(t.mReadCount + ", " + t.mAdjustCount);
 +        }
 +    }
 +
 +    private abstract static class TestThread extends Thread {
 +        long mReadCount;
 +        long mAdjustCount;
 +
 +        TestThread(String name) {
 +            super(name);
 +        }
 +    }
 +}
 diff --git a/src/test/java/com/amazon/carbonado/repo/map/TestRWLockStress.java b/src/test/java/com/amazon/carbonado/repo/map/TestRWLockStress.java new file mode 100644 index 0000000..f6bb485 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/repo/map/TestRWLockStress.java @@ -0,0 +1,133 @@ +/*
 + * Copyright 2007 Amazon Technologies, Inc. or its affiliates.
 + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 + * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 + *
 + * Licensed 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 com.amazon.carbonado.repo.map;
 +
 +import java.util.*;
 +import java.util.concurrent.locks.*;
 +
 +/**
 + * 
 + *
 + * @author Brian S O'Neill
 + */
 +public class TestRWLockStress {
 +    static long cSharedValue;
 +
 +    static volatile boolean cStop;
 +
 +    /**
 +     * @param args args[0]: readers, args[1]: upgraders, args[2]: writers, args[3]: run time
 +     */
 +    public static void main(String[] args) throws Exception {
 +        final int readers = Integer.parseInt(args[0]);
 +        final int writers = Integer.parseInt(args[2]);
 +        final int runTimeSeconds = Integer.parseInt(args[3]);
 +
 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(args[1].equals("f"));
 +
 +        List<TestThread> threads = new ArrayList<TestThread>();
 +
 +        for (int i=0; i<readers; i++) {
 +            TestThread t = new TestThread("reader " + i) {
 +                public void run() {
 +                    while (!cStop) {
 +                        lock.readLock().lock();
 +                        try {
 +                            long value = cSharedValue;
 +                            // Do some "work"
 +                            String.valueOf(value);
 +                            long again = cSharedValue;
 +                            if (again != value) {
 +                                throw new AssertionError("" + again + " != " + value);
 +                            }
 +                            mReadCount++;
 +                        } finally {
 +                            lock.readLock().unlock();
 +                        }
 +                    }
 +                }
 +            };
 +            threads.add(t);
 +            t.start();
 +        }
 +
 +        for (int i=0; i<writers; i++) {
 +            TestThread t = new TestThread("writer " + i) {
 +                public void run() {
 +                    while (!cStop) {
 +                        lock.writeLock().lock();
 +                        try {
 +                            long value = cSharedValue;
 +                            // Do some "work"
 +                            String.valueOf(value);
 +                            assert(cSharedValue == value);
 +                            cSharedValue = value + 1;
 +                            mAdjustCount++;
 +                        } finally {
 +                            lock.writeLock().unlock();
 +                        }
 +                    }
 +                }
 +            };
 +            threads.add(t);
 +            t.start();
 +        }
 +
 +        Thread.sleep(1000L * runTimeSeconds);
 +        cStop = true;
 +
 +        for (TestThread t : threads) {
 +            t.join();
 +        }
 +
 +        long reads = 0;
 +        for (TestThread t : threads) {
 +            reads += t.mReadCount;
 +        }
 +
 +        long expected = 0;
 +        for (TestThread t : threads) {
 +            expected += t.mAdjustCount;
 +        }
 +
 +        System.out.println("Reads:    " + reads);
 +        System.out.println("Expected: " + expected);
 +        System.out.println("Actual:   " + cSharedValue);
 +
 +        if (expected == cSharedValue) {
 +            System.out.println("SUCCESS");
 +        } else {
 +            System.out.println("FAILURE");
 +        }
 +
 +        System.out.println();
 +        for (TestThread t : threads) {
 +            System.out.println(t.mReadCount + ", " + t.mAdjustCount);
 +        }
 +    }
 +
 +    private abstract static class TestThread extends Thread {
 +        long mReadCount;
 +        long mAdjustCount;
 +
 +        TestThread(String name) {
 +            super(name);
 +        }
 +    }
 +}
 diff --git a/src/test/java/com/amazon/carbonado/repo/map/TestStorables.java b/src/test/java/com/amazon/carbonado/repo/map/TestStorables.java new file mode 100644 index 0000000..8210ca9 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/repo/map/TestStorables.java @@ -0,0 +1,97 @@ +/*
 + * Copyright 2008 Amazon Technologies, Inc. or its affiliates.
 + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 + * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 + *
 + * Licensed 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 com.amazon.carbonado.repo.map;
 +
 +import junit.framework.TestSuite;
 +
 +import org.joda.time.DateTime;
 +import org.joda.time.DateTimeZone;
 +
 +import com.amazon.carbonado.Repository;
 +import com.amazon.carbonado.RepositoryBuilder;
 +import com.amazon.carbonado.RepositoryException;
 +import com.amazon.carbonado.Query;
 +import com.amazon.carbonado.Storage;
 +
 +import com.amazon.carbonado.stored.StorableDateIndex;
 +
 +/**
 + *
 + *
 + * @author Brian S O'Neill
 + */
 +public class TestStorables extends com.amazon.carbonado.TestStorables {
 +    public static void main(String[] args) {
 +        junit.textui.TestRunner.run(suite());
 +    }
 +
 +    public static TestSuite suite() {
 +        TestSuite suite = new TestSuite();
 +        suite.addTestSuite(TestStorables.class);
 +        return suite;
 +    }
 +
 +    public TestStorables(String name) {
 +        super(name);
 +    }
 +
 +    @Override
 +    public void test_invalidStorables() {
 +        // Map repository has no problem with custom property types.
 +    }
 +
 +    @Override
 +    public void test_dateTimeIndex() throws Exception {
 +        // Map repository does not use DateTimeAdapter.
 +
 +        Storage<StorableDateIndex> storage = getRepository().storageFor(StorableDateIndex.class);
 +
 +        DateTimeZone original = DateTimeZone.getDefault();
 +        // Set time zone different than defined in storable.
 +        DateTimeZone.setDefault(DateTimeZone.forID("America/Los_Angeles"));
 +        try {
 +            DateTime now = new DateTime();
 +
 +            StorableDateIndex sdi = storage.prepare();
 +            sdi.setID(1);
 +            sdi.setOrderDate(now);
 +            sdi.insert();
 +
 +            sdi.load();
 +
 +            assertEquals(now.getMillis(), sdi.getOrderDate().getMillis());
 +            // Time zones are equal since there's no adapter.
 +            assertTrue(now.equals(sdi.getOrderDate()));
 +
 +            Query<StorableDateIndex> query = storage.query("orderDate=?").with(now);
 +            StorableDateIndex sdi2 = query.tryLoadOne();
 +            assertNotNull(sdi2);
 +        } finally {
 +            DateTimeZone.setDefault(original);
 +        }
 +    }
 +
 +    @Override
 +    protected Repository buildRepository(boolean isMaster) throws RepositoryException {
 +        MapRepositoryBuilder builder = new MapRepositoryBuilder();
 +        builder.setName("map");
 +        builder.setMaster(isMaster);
 +        return builder.build();
 +    }
 +}
  | 
