Example #1
0
  /** Tests the loading of classes */
  @Test
  public void testClassloading() throws Exception {
    final Store diskStore = createDiskStore();

    Long value = Long.valueOf(123L);
    diskStore.put(new Element("key1", value));
    diskStore.put(new Element("key2", value));
    Thread.sleep(1000);
    Element element1 = diskStore.get("key1");
    Element element2 = diskStore.get("key2");
    assertEquals(value, element1.getObjectValue());
    assertEquals(value, element2.getObjectValue());

    Primitive primitive = new Primitive();
    primitive.integerPrimitive = 123;
    primitive.longPrimitive = 456L;
    primitive.bytePrimitive = "a".getBytes()[0];
    primitive.charPrimitive = 'B';
    primitive.booleanPrimitive = false;

    // test Serializability
    ByteArrayOutputStream outstr = new ByteArrayOutputStream();
    ObjectOutputStream objstr = new ObjectOutputStream(outstr);
    objstr.writeObject(new Element("key", value));
    objstr.close();

    diskStore.put(new Element("primitive1", primitive));
    diskStore.put(new Element("primitive2", primitive));
    Thread.sleep(1000);
    Element primitive1 = diskStore.get("primitive1");
    Element primitive2 = diskStore.get("primitive2");
    assertEquals(primitive, primitive1.getObjectValue());
    assertEquals(primitive, primitive2.getObjectValue());
  }
Example #2
0
  /** Multi-thread concurrent read remove test. */
  @Test
  public void testReadRemoveMultipleThreads() throws Exception {
    final Random random = new Random();
    final Store diskStore = createDiskStore();

    diskStore.put(new Element("key", "value"));

    // Run a set of threads that get, put and remove an entry
    final List executables = new ArrayList();
    for (int i = 0; i < 5; i++) {
      final Executable executable =
          new Executable() {
            public void execute() throws Exception {
              for (int i = 0; i < 100; i++) {
                diskStore.put(new Element("key" + random.nextInt(100), "value"));
              }
            }
          };
      executables.add(executable);
    }
    for (int i = 0; i < 5; i++) {
      final Executable executable =
          new Executable() {
            public void execute() throws Exception {
              for (int i = 0; i < 100; i++) {
                diskStore.remove("key" + random.nextInt(100));
              }
            }
          };
      executables.add(executable);
    }

    runThreads(executables);
  }
Example #3
0
  /** Any disk store with an auto generated random directory should not be able to be loaded. */
  @Test
  public void testCannotLoadPersistentStoreWithAutoDir() throws IOException, InterruptedException {
    // initialise
    String cacheName = "testPersistent";
    Store diskStore = createAutoPersistentDiskStore(cacheName);
    diskStore.removeAll();

    for (int i = 0; i < 100; i++) {
      byte[] data = new byte[1024];
      diskStore.put(new Element("key" + (i + 100), data));
    }
    waitLonger();
    assertEquals(ELEMENT_ON_DISK_SIZE * 100, diskStore.getOnDiskSizeInBytes());
    assertEquals(100, diskStore.getSize());
    manager2.removeCache(cacheName);
    Thread.sleep(1000);

    Cache cache = new Cache(cacheName, 10000, true, false, 5, 1, true, 600);
    manager2.addCache(cache);

    File dataFile = ((DiskPersistentStore) diskStore).getDataFile();
    assertTrue("File exists", dataFile.exists());
    assertEquals(0, dataFile.length());
    assertEquals(0, cache.getSize());
    manager.removeCache(cacheName);
    assertTrue("File exists", dataFile.exists());
    assertEquals(0, dataFile.length());
  }
Example #4
0
  /** Multi-thread read-only test. Will fail on memory constrained VMs */
  @Test
  public void testReadOnlyMultipleThreads() throws Exception {
    final Store diskStore = createNonExpiringDiskStore();

    // Add a couple of elements
    diskStore.put(new Element("key0", "value"));
    diskStore.put(new Element("key1", "value"));
    diskStore.put(new Element("key2", "value"));
    diskStore.put(new Element("key3", "value"));

    // Wait for the elements to be written
    waitShorter();

    // Run a set of threads, that attempt to fetch the elements
    final List executables = new ArrayList();
    for (int i = 0; i < 20; i++) {
      final String key = "key" + (i % 4);
      final Executable executable =
          new Executable() {
            public void execute() throws Exception {
              final Element element = diskStore.get(key);
              assertNotNull(element);
              assertEquals("value", element.getObjectValue());
            }
          };
      executables.add(executable);
    }
    runThreads(executables);
  }
Example #5
0
 /** Tests looking up an entry that does not exist. */
 @Test
 public void testGetQuietUnknownThenKnown() throws Exception {
   final Store diskStore = createDiskStore();
   assertNull(diskStore.getQuiet("key1"));
   diskStore.put(new Element("key1", "value"));
   diskStore.put(new Element("key2", "value"));
   assertNotNull(diskStore.getQuiet("key1"));
   assertNotNull(diskStore.getQuiet("key2"));
 }
Example #6
0
  /** Tests removing an entry. */
  @Test
  public void testRemove() throws Exception {
    final Store diskStore = createDiskStore();

    // Add the entry
    final String value = "value";
    diskStore.put(new Element("key1", value));
    diskStore.put(new Element("key2", value));

    // Check the entry is there
    assertEquals(1, diskStore.getOnDiskSize());
    assertEquals(2, diskStore.getSize());

    assertNotNull(diskStore.get("key1"));
    assertNotNull(diskStore.get("key2"));

    // Remove it
    diskStore.remove("key1");
    diskStore.remove("key2");

    // Check the entry is not there
    assertEquals(0, diskStore.getOnDiskSize());
    assertEquals(0, diskStore.getSize());
    assertNull(diskStore.get("key1"));
    assertNull(diskStore.get("key2"));
  }
Example #7
0
  @Test
  public void testUsesReentrantLocks() throws TimeoutException {
    CacheLockProvider clp = (CacheLockProvider) store.getInternalContext();
    SomeKey[] keys = {new SomeKey(0), new SomeKey(1)};
    store.put(new Element(keys[0], "VALUE0"));
    store.put(new Element(keys[1], "VALUE2"));
    Sync[] syncForKeys = clp.getAndWriteLockAllSyncForKeys((Object[]) keys);
    assertEquals(1, syncForKeys.length);
    assertTrue(
        "Segment should now be write locked!",
        syncForKeys[0].isHeldByCurrentThread(LockType.WRITE));
    try {
      for (SomeKey key : keys) {
        clp.getSyncForKey(key).unlock(LockType.WRITE);
      }
    } catch (java.lang.IllegalMonitorStateException e) {
      fail(
          "This shouldn't throw an IllegalMonitorStateException, segment should have been locked twice!");
    }
    assertFalse(
        "Segment should now be entirely unlocked!",
        syncForKeys[0].isHeldByCurrentThread(LockType.WRITE));

    syncForKeys = clp.getAndWriteLockAllSyncForKeys(50, (Object[]) keys);
    assertEquals(1, syncForKeys.length);
    assertTrue(
        "Segment should now be write locked!",
        syncForKeys[0].isHeldByCurrentThread(LockType.WRITE));
    try {
      for (SomeKey key : keys) {
        clp.getSyncForKey(key).unlock(LockType.WRITE);
      }
    } catch (java.lang.IllegalMonitorStateException e) {
      fail(
          "This shouldn't throw an IllegalMonitorStateException, segment should have been locked twice!");
    }
    assertFalse(
        "Segment should now be entirely unlocked!",
        syncForKeys[0].isHeldByCurrentThread(LockType.WRITE));

    syncForKeys = clp.getAndWriteLockAllSyncForKeys(50, (Object[]) keys);
    assertEquals(1, syncForKeys.length);
    assertTrue(
        "Segment should now be write locked!",
        syncForKeys[0].isHeldByCurrentThread(LockType.WRITE));
    try {
      clp.unlockWriteLockForAllKeys((Object[]) keys);
    } catch (java.lang.IllegalMonitorStateException e) {
      fail(
          "This shouldn't throw an IllegalMonitorStateException, segment should have been locked twice!");
    }
    assertFalse(
        "Segment should now be entirely unlocked!",
        syncForKeys[0].isHeldByCurrentThread(LockType.WRITE));
  }
Example #8
0
 @Test
 public void testSupportsCopyOnRead() {
   Element element = new Element(KEY, "Some String", 1);
   xaStore.put(element);
   Element copy = xaStore.get(KEY);
   assertNotNull(copy);
   assertNotSame(copy, xaStore.get(KEY));
   assertEquals("Some String", copy.getValue());
   assertEquals(copy.getValue(), xaStore.get(KEY).getValue());
   assertNotSame(copy.getValue(), xaStore.get(KEY).getValue());
 }
Example #9
0
 @Test
 public void testThrowsExceptionOnNonSerializableValue() {
   try {
     xaStore.put(new Element(KEY, new Object()));
     fail("Should have thrown an Exception");
   } catch (Exception e) {
     assertTrue(
         "Expected " + CacheException.class.getName() + ", but was " + e.getClass().getName(),
         e instanceof CacheException);
   }
   assertNull(xaStore.get(KEY));
 }
Example #10
0
  /** This method tries to get the store too leak. */
  protected long thrashCache() throws Exception {

    long startingSize = measureMemoryUse();
    LOG.info("Starting memory used is: " + startingSize);

    final String value = "value";
    final String key = "key";

    // Add the entry
    final Element element = new Element(key, value);
    store.put(element);

    // Create 15 threads that read the keys;
    final List executables = new ArrayList();
    for (int i = 0; i < 15; i++) {
      final LruMemoryStoreTest.Executable executable =
          new MemoryStoreTester.Executable() {
            public void execute() throws Exception {
              for (int i = 0; i < 500; i++) {
                final String key = "key" + i;
                store.get(key);
              }
              store.get("key");
            }
          };
      executables.add(executable);
    }
    // Create 15 threads that are insert 500 keys with large byte[] as values
    for (int i = 0; i < 15; i++) {
      final MemoryStoreTester.Executable executable =
          new MemoryStoreTester.Executable() {
            public void execute() throws Exception {

              // Add a bunch of entries
              for (int i = 0; i < 500; i++) {
                // Use a random length value
                final String key = "key" + i;
                byte[] value = new byte[10000];
                Element element = new Element(key, value);
                store.put(element);
              }
            }
          };
      executables.add(executable);
    }

    runThreads(executables);
    store.removeAll();

    long finishingSize = measureMemoryUse();
    LOG.info("Ending memory used is: " + finishingSize);
    return finishingSize - startingSize;
  }
Example #11
0
  @Test
  public void testLFUEvictionFromDiskStore() throws IOException, InterruptedException {
    Cache cache =
        new Cache(
            "testNonPersistent",
            1,
            MemoryStoreEvictionPolicy.LFU,
            true,
            null,
            false,
            2000,
            1000,
            false,
            1,
            null,
            null,
            10);
    manager.addCache(cache);
    Store store = cache.getStore();

    Element element;

    assertEquals(0, store.getSize());

    for (int i = 0; i < 11; i++) {
      element = new Element("key" + i, "value" + i);
      cache.put(element);
      if (i > 0) {
        cache.get("key" + i);
        cache.get("key" + i);
        cache.get("key" + i);
        cache.get("key" + i);
      }
    }

    // allow to move through spool
    Thread.sleep(220);
    assertEquals(10, store.getOnDiskSize());

    element = new Element("keyNew", "valueNew");
    store.put(element);

    // allow to get out of spool
    Thread.sleep(220);
    assertEquals(10, store.getOnDiskSize());
    // check new element not evicted
    assertNotNull(store.get(element.getObjectKey()));
    // check evicted honours LFU policy
    assertNull(store.get("key0"));

    for (int i = 0; i < 2000; i++) {
      store.put(new Element("" + i, new Date()));
    }
    // wait for spool to empty
    waitLonger();

    assertEquals(10, store.getOnDiskSize());
  }
Example #12
0
  /**
   * Default Constructor
   *
   * @param cache the Terracotta clustered Cache to snapshot
   * @param interval the interval to do the snapshots on
   * @param doKeySnapshotOnDedicatedThread whether the snapshots have to be done on a dedicated
   *     thread
   * @param rotatingWriter the RotatingSnapshotFile to write to
   * @throws IllegalArgumentException if interval is less than or equal to zero
   */
  KeySnapshotter(
      final Ehcache cache,
      final long interval,
      final boolean doKeySnapshotOnDedicatedThread,
      final RotatingSnapshotFile rotatingWriter)
      throws IllegalArgumentException {
    final Store store = new CacheStoreHelper((Cache) cache).getStore();
    if (!(store instanceof TerracottaStore)) {
      throw new IllegalArgumentException(
          "Cache '"
              + cache.getName()
              + "' isn't backed by a "
              + TerracottaStore.class.getSimpleName()
              + " but uses a "
              + store.getClass().getName()
              + " instead");
    }

    if (interval <= 0) {
      throw new IllegalArgumentException("Interval needs to be a positive & non-zero value");
    }

    if (rotatingWriter == null) {
      throw new NullPointerException();
    }

    this.cacheName = cache.getName();
    this.rotatingWriter = rotatingWriter;
    this.tcStore = (TerracottaStore) store;

    if (doKeySnapshotOnDedicatedThread) {
      thread = new SnapShottingThread(this, interval, "KeySnapshotter for cache " + cacheName);
      thread.start();
    } else {
      ScheduledExecutorService scheduledExecutorService = INSTANCES.get(cache.getCacheManager());
      if (scheduledExecutorService == null) {
        scheduledExecutorService = new ScheduledThreadPoolExecutor(POOL_SIZE);
        final ScheduledExecutorService previous =
            INSTANCES.putIfAbsent(cache.getCacheManager(), scheduledExecutorService);
        if (previous != null) {
          scheduledExecutorService.shutdownNow();
          scheduledExecutorService = previous;
        }
      }
      scheduledExecutorService.scheduleWithFixedDelay(this, interval, interval, TimeUnit.SECONDS);
      thread = null;
    }
  }
Example #13
0
  /** {@inheritDoc} */
  public Set<Object> getKeysInvisibleInContext(
      LocalTransactionContext currentTransactionContext, Store underlyingStore) {
    Set<Object> invisibleKeys = new HashSet<Object>();

    // all new keys added into the store are invisible
    invisibleKeys.addAll(getNewKeys());

    List<SoftLock> currentTransactionContextSoftLocks =
        currentTransactionContext.getSoftLocksForCache(cacheName);
    for (SoftLock softLock : currentTransactionContextSoftLocks) {
      Element e = underlyingStore.getQuiet(softLock.getKey());
      if (e.getObjectValue() instanceof SoftLockID) {
        SoftLockID softLockId = (SoftLockID) e.getObjectValue();
        if (softLock.getElement(currentTransactionContext.getTransactionId(), softLockId) == null) {
          // if the soft lock's element is null in the current transaction then the key is invisible
          invisibleKeys.add(softLock.getKey());
        } else {
          // if the soft lock's element is not null in the current transaction then the key is
          // visible
          invisibleKeys.remove(softLock.getKey());
        }
      }
    }

    return invisibleKeys;
  }
Example #14
0
  /** Multi-thread read-write test. */
  @Test
  public void testReadWriteThreads() throws Exception {

    final String value = "value";
    final String key = "key";

    // Add the entry
    final Element element = new Element(key, value);
    store.put(element);

    // Run a set of threads that get, put and remove an entry
    final List executables = new ArrayList();
    for (int i = 0; i < 5; i++) {
      final MemoryStoreTester.Executable executable =
          new MemoryStoreTester.Executable() {
            public void execute() throws Exception {
              final Element element = store.get("key");
              assertNotNull(element);
            }
          };
      executables.add(executable);
    }
    for (int i = 0; i < 5; i++) {
      final MemoryStoreTester.Executable executable =
          new MemoryStoreTester.Executable() {
            public void execute() throws Exception {
              store.put(element);
            }
          };
      executables.add(executable);
    }

    runThreads(executables);
  }
Example #15
0
  /** Tests that we can save and load a persistent store in a repeatable way */
  @Test
  public void testLoadPersistentStore() throws IOException, InterruptedException {
    // initialise
    String cacheName = "testLoadPersistent";
    Store store = createPersistentDiskStore(cacheName);
    store.removeAll();
    waitShorter();

    for (int i = 0; i < 100; i++) {
      byte[] data = new byte[1024];
      store.put(new Element("key" + (i + 100), data));
    }
    store.flush();
    waitShorter();
    assertEquals(ELEMENT_ON_DISK_SIZE * 100, store.getOnDiskSizeInBytes());
    assertEquals(100, store.getSize());
    manager.removeCache(cacheName);
    Thread.sleep(3000);
    // check that we can create and dispose several times with no problems and no lost data
    for (int i = 0; i < 10; i++) {
      store = createPersistentDiskStore(cacheName);
      File dataFile = ((DiskPersistentStore) store).getDataFile();
      assertTrue("File exists", dataFile.exists());
      assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length());
      assertEquals(100, store.getSize());

      manager.removeCache(cacheName);

      assertTrue("File exists", dataFile.exists());
      assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length());
    }
  }
Example #16
0
  /** Tests bulk load. */
  @Test
  public void testBulkLoad() throws Exception {
    final Store diskStore = createDiskStore();

    final Random random = new Random();

    // Add a bunch of entries
    for (int i = 0; i < 500; i++) {
      // Use a random length value
      final String key = "key" + i;
      final String value = "This is a value" + random.nextInt(1000);

      // Add an element, and make sure it is present
      Element element = new Element(key, value);
      diskStore.put(element);
      element = diskStore.get(key);
      assertNotNull(element);

      // Chuck in a delay, to give the spool thread a chance to catch up
      Thread.sleep(2);

      // Remove the element
      diskStore.remove(key);
      element = diskStore.get(key);
      assertNull(element);

      element = new Element(key, value);
      diskStore.put(element);
      element = diskStore.get(key);
      assertNotNull(element);

      // Chuck in a delay
      Thread.sleep(2);
    }
  }
Example #17
0
  /** Benchmark to test speed. */
  @Test
  public void testBenchmarkPutGet() throws Exception {
    final String key = "key";
    byte[] value = new byte[500];
    StopWatch stopWatch = new StopWatch();

    // Add a bunch of entries
    for (int i = 0; i < 50000; i++) {
      Element element = new Element(key, value);
      store.put(element);
    }
    for (int i = 0; i < 50000; i++) {
      store.get(key + i);
    }
    long time = stopWatch.getElapsedTime();
    LOG.info("Time for benchmarkPutGet: " + time);
    assertTrue("Too slow. Time was " + time, time < 300);
  }
Example #18
0
  /**
   * Tests that a file is created with the right size after puts, and that the file is not deleted
   * on disposal
   *
   * <p>This test uses a preconfigured cache from the test cache.xml. Note that teardown causes an
   * exception because the disk store is being shut down twice.
   */
  @Test
  public void testPersistentStore() throws IOException, InterruptedException, CacheException {
    // initialise
    Store store = createPersistentDiskStoreFromCacheManager();
    store.removeAll();

    File dataFile = ((DiskPersistentStore) store).getDataFile();

    for (int i = 0; i < 100; i++) {
      byte[] data = new byte[1024];
      store.put(new Element("key" + (i + 100), data));
    }
    waitShorter();
    assertEquals(100, store.getSize());
    store.dispose();

    assertTrue("File exists", dataFile.exists());
    assertEquals(100 * ELEMENT_ON_DISK_SIZE, dataFile.length());
  }
Example #19
0
  /**
   * Checks that the expiry thread runs and expires elements which has the effect of preventing the
   * disk store from continously growing. Ran for 6 hours through 10000 outer loops. No memory use
   * increase. Using a key of "key" + i * outer) you get early slots that cannot be reused. The
   * DiskStore actual size therefore starts at 133890 and ends at 616830. There is quite a lot of
   * space that cannot be used because of fragmentation. Question? Should an effort be made to
   * coalesce fragmented space? Unlikely in production to get contiguous fragments as in the first
   * form of this test.
   *
   * <p>Using a key of Integer.valueOf(i * outer) the size stays constant at 140800.
   *
   * @throws InterruptedException
   */
  @Test
  public void testExpiryWithSize() throws InterruptedException {
    Store diskStore = createDiskStore();
    diskStore.removeAll();

    byte[] data = new byte[1024];
    for (int outer = 1; outer <= 10; outer++) {
      for (int i = 0; i < 101; i++) {
        Element element = new Element(Integer.valueOf(i * outer), data);
        element.setTimeToLive(1);
        diskStore.put(element);
      }
      waitLonger();
      int predictedSize = (ELEMENT_ON_DISK_SIZE + 68) * 100;
      long actualSize = diskStore.getOnDiskSizeInBytes();
      LOG.info("Predicted Size: " + predictedSize + " Actual Size: " + actualSize);
      assertEquals(predictedSize, actualSize);
      LOG.info("Memory Use: " + measureMemoryUse());
    }
  }
Example #20
0
  /** Tests adding an entry. */
  @Test
  public void testPut() throws Exception {
    final String value = "value";
    final String key = "key";

    // Make sure the element is not found
    assertEquals(0, store.getSize());
    Element element = store.get(key);
    assertNull(element);

    // Add the element
    element = new Element(key, value);
    store.put(element);

    // Get the element
    assertEquals(1, store.getSize());
    element = store.get(key);
    assertNotNull(element);
    assertEquals(value, element.getObjectValue());
  }
Example #21
0
  /** Tests removing all the entries. */
  @Test
  public void testRemoveAll() throws Exception {
    final String value = "value";
    final String key = "key";

    // Add the entry
    Element element = new Element(key, value);
    store.put(element);

    // Check the entry is there
    element = store.get(key);
    assertNotNull(element);

    // Remove it
    store.removeAll();

    // Check the entry is not there
    assertEquals(0, store.getSize());
    element = store.get(key);
    assertNull(element);
  }
Example #22
0
  /** Tests for element expiry. */
  @Test
  public void testExpiry() throws Exception {
    // Create a diskStore with a cranked up expiry thread
    final Store diskStore = createDiskStore();

    // Add an element that will expire.
    diskStore.put(new Element("key1", "value", false, 1, 1));
    diskStore.put(new Element("key2", "value", false, 1, 1));
    assertNotNull(diskStore.get("key1"));
    assertNotNull(diskStore.get("key2"));
    assertEquals(2, diskStore.getSize());
    assertEquals(1, diskStore.getOnDiskSize());

    // Wait a couple of seconds
    Thread.sleep(3000);

    Element e1 = diskStore.get("key1");
    Element e2 = diskStore.get("key2");

    if (e1 != null) {
      assertNull(e2);
    } else if (e2 != null) {
      assertNull(e1);
    } else {
      fail("One of the Elements should have been in memory - and hence not be expired");
    }
  }
Example #23
0
  /** Tests bulk load. */
  @Test
  public void testBulkLoad() throws Exception {
    final Random random = new Random();
    StopWatch stopWatch = new StopWatch();

    // Add a bunch of entries
    for (int i = 0; i < 500; i++) {
      // Use a random length value
      final String key = "key" + i;
      final String value = "value" + random.nextInt(1000);

      // Add an element, and make sure it is present
      Element element = new Element(key, value);
      store.put(element);
      element = store.get(key);
      assertNotNull(element);

      // Remove the element
      store.remove(key);
      element = store.get(key);
      assertNull(element);

      element = new Element(key, value);
      store.put(element);
      element = store.get(key);
      assertNotNull(element);
    }
    long time = stopWatch.getElapsedTime();
    LOG.info("Time for Bulk Load: " + time);
  }
Example #24
0
  /** Tests that we can load a store after the index has been corrupted */
  @Test
  public void testLoadPersistentStoreAfterCorruption() throws IOException, InterruptedException {
    // initialise
    String cacheName = "testPersistent";
    Store diskStore = createPersistentDiskStore(cacheName);
    diskStore.removeAll();

    for (int i = 0; i < 100; i++) {
      byte[] data = new byte[1024];
      diskStore.put(new Element("key" + (i + 100), data));
    }
    waitShorter();
    assertEquals(ELEMENT_ON_DISK_SIZE * 100, diskStore.getOnDiskSizeInBytes());
    assertEquals(100, diskStore.getSize());
    manager.removeCache(cacheName);

    File indexFile = ((DiskPersistentStore) diskStore).getIndexFile();
    FileOutputStream fout = new FileOutputStream(indexFile);
    // corrupt the index file
    fout.write(new byte[] {'q', 'w', 'e', 'r', 't', 'y'});
    fout.close();
    diskStore = createPersistentDiskStore(cacheName);
    File dataFile = ((DiskPersistentStore) diskStore).getDataFile();
    assertTrue("File exists", dataFile.exists());

    // Make sure the data file got recreated since the index was corrupt
    assertEquals("Data file was not recreated", 0, dataFile.length());
    assertEquals(0, diskStore.getSize());
  }
Example #25
0
  /** Benchmark to test speed. Original implementation 12seconds This implementation 9 seconds */
  public void benchmarkPutGetSuryaTest(long allowedTime) throws Exception {
    Random random = new Random();
    byte[] value = new byte[500];
    StopWatch stopWatch = new StopWatch();

    // Add a bunch of entries
    for (int i = 0; i < 50000; i++) {
      String key = "key" + i;

      Element element = new Element(key, value);
      store.put(element);

      // Access each element random number of times, min:0 maximum:9
      int accesses = random.nextInt(5);
      for (int j = 0; j <= accesses; j++) {
        store.get(key);
      }
    }
    long time = stopWatch.getElapsedTime();
    LOG.info("Time for benchmarkPutGetSurya: " + time);
    assertTrue("Too slow. Time was " + time, time < allowedTime);
  }
Example #26
0
  /** Multi-thread read-only test. */
  @Test
  public void testReadOnlyThreads() throws Exception {

    // Add a couple of elements
    store.put(new Element("key0", "value"));
    store.put(new Element("key1", "value"));

    // Run a set of threads, that attempt to fetch the elements
    final List executables = new ArrayList();
    for (int i = 0; i < 10; i++) {
      final String key = "key" + (i % 2);
      final MemoryStoreTester.Executable executable =
          new LruMemoryStoreTest.Executable() {
            public void execute() throws Exception {
              final Element element = store.get(key);
              assertNotNull(element);
              assertEquals("value", element.getObjectValue());
            }
          };
      executables.add(executable);
    }
    runThreads(executables);
  }
Example #27
0
  @Test
  public void testSupportsCopyOnWrite() {

    AtomicLong atomicLong = new AtomicLong(0);

    Element element = new Element(KEY, atomicLong, 1);
    atomicLong.getAndIncrement();
    xaStore.put(element);

    atomicLong.getAndIncrement();
    element.setVersion(2);

    assertEquals(1, ((AtomicLong) xaStore.get(KEY).getValue()).get());
    assertEquals(1, xaStore.get(KEY).getVersion());

    xaStore.put(new Element(KEY, atomicLong, 1));
    assertEquals(2, ((AtomicLong) xaStore.get(KEY).getValue()).get());
    atomicLong.getAndIncrement();

    assertEquals(2, ((AtomicLong) xaStore.get(KEY).getValue()).get());
    assertEquals(1, xaStore.get(KEY).getVersion());
  }
Example #28
0
  /**
   * Tests that a file is created with the right size after puts, and that the file is deleted on
   * disposal
   */
  @Test
  public void testNonPersistentStore() throws IOException, InterruptedException {
    Store diskStore = createNonExpiringDiskStore();
    File dataFile = ((OverflowToDiskStore) diskStore).getDataFile();

    // 100 + 1 for the in-memory capacity
    for (int i = 0; i < 101; i++) {
      byte[] data = new byte[1024];
      diskStore.put(new Element("key" + (i + 100), data));
    }
    waitShorter();
    assertEquals(ELEMENT_ON_DISK_SIZE * 100, diskStore.getOnDiskSizeInBytes());

    assertEquals(100, diskStore.getOnDiskSize());
    assertEquals(101, diskStore.getSize());
    diskStore.dispose();
    Thread.sleep(1);
    assertFalse("File exists", dataFile.exists());
  }
Example #29
0
  /** Tests adding an entry. */
  @Test
  public void testPut() throws Exception {
    final Store diskStore = createDiskStore();

    // Make sure the element is not found
    assertEquals(0, diskStore.getSize());
    assertNull(diskStore.get("key"));

    // Add the element
    final String value = "value";
    diskStore.put(new Element("key1", value));
    diskStore.put(new Element("key2", value));

    // Get the element
    assertEquals(2, diskStore.getSize());
    assertEquals(1, diskStore.getOnDiskSize());

    Element element1 = diskStore.get("key1");
    Element element2 = diskStore.get("key2");
    assertNotNull(element1);
    assertNotNull(element2);
    assertEquals(value, element1.getObjectValue());
    assertEquals(value, element2.getObjectValue());
  }
Example #30
0
 /** Tests looking up an entry that does not exist. */
 @Test
 public void testGetUnknown() throws Exception {
   final Element element = store.get("key");
   assertNull(element);
 }