예제 #1
0
  @Test
  public void par_update_get_compact() throws InterruptedException {
    int scale = TT.scale();
    if (scale == 0) return;
    int threadNum = Math.min(4, scale * 4);
    final long end = TT.nowPlusMinutes(10);
    e = openEngine();
    final BlockingQueue<Fun.Pair<Long, byte[]>> q = new ArrayBlockingQueue(threadNum * 10);
    for (int i = 0; i < threadNum; i++) {
      byte[] b = TT.randomByteArray(new Random().nextInt(10000));
      long recid = e.put(b, BYTE_ARRAY_NOSIZE);
      q.put(new Fun.Pair(recid, b));
    }

    final CountDownLatch l = new CountDownLatch(2);
    Thread tt =
        new Thread() {
          @Override
          public void run() {
            try {
              while (l.getCount() > 1) e.compact();
            } finally {
              l.countDown();
            }
          }
        };
    tt.setDaemon(true);
    tt.run();

    Exec.execNTimes(
        threadNum,
        new Callable() {
          @Override
          public Object call() throws Exception {
            Random r = new Random();
            while (System.currentTimeMillis() < end) {
              Fun.Pair<Long, byte[]> t = q.take();
              assertTrue(
                  Serializer.BYTE_ARRAY.equals(t.b, e.get(t.a, Serializer.BYTE_ARRAY_NOSIZE)));
              int size = r.nextInt(1000);
              if (r.nextInt(10) == 1) size = size * 100;
              byte[] b = TT.randomByteArray(size);
              e.update(t.a, b, Serializer.BYTE_ARRAY_NOSIZE);
              q.put(new Fun.Pair<Long, byte[]>(t.a, b));
            }
            return null;
          }
        });
    l.countDown();
    l.await();

    for (Fun.Pair<Long, byte[]> t : q) {
      assertTrue(Serializer.BYTE_ARRAY.equals(t.b, e.get(t.a, Serializer.BYTE_ARRAY_NOSIZE)));
    }
    e.close();
  }
예제 #2
0
  @Test
  public void par_cas() throws InterruptedException {
    int scale = TT.scale();
    if (scale == 0) return;
    int threadNum = 8 * scale;
    final long end = TT.nowPlusMinutes(10);
    e = openEngine();
    final BlockingQueue<Fun.Pair<Long, byte[]>> q = new ArrayBlockingQueue(threadNum * 10);
    for (int i = 0; i < threadNum; i++) {
      byte[] b = TT.randomByteArray(new Random().nextInt(10000));
      long recid = e.put(b, BYTE_ARRAY_NOSIZE);
      q.put(new Fun.Pair(recid, b));
    }

    Exec.execNTimes(
        threadNum,
        new Callable() {
          @Override
          public Object call() throws Exception {
            Random r = new Random();
            while (System.currentTimeMillis() < end) {
              Fun.Pair<Long, byte[]> t = q.take();
              int size = r.nextInt(10000);
              if (r.nextInt(10) == 1) size = size * 100;
              byte[] b = TT.randomByteArray(size);
              assertTrue(e.compareAndSwap(t.a, t.b, b, Serializer.BYTE_ARRAY_NOSIZE));
              q.put(new Fun.Pair<Long, byte[]>(t.a, b));
            }
            return null;
          }
        });

    for (Fun.Pair<Long, byte[]> t : q) {
      assertTrue(Serializer.BYTE_ARRAY.equals(t.b, e.get(t.a, Serializer.BYTE_ARRAY_NOSIZE)));
    }
    e.close();
  }
예제 #3
0
  @Test
  public void recover_with_interrupt() throws InterruptedException {
    int scale = TT.scale();
    if (scale == 0) return;
    e = openEngine();
    if (!e.canRollback()
        || e instanceof StoreHeap) // TODO engine might have crash recovery, but no rollbacks
    return;

    // fill recids
    final int max = scale * 1000;
    final ArrayList<Long> recids = new ArrayList<Long>();
    final AtomicLong a = new AtomicLong(10);
    final long counterRecid = e.put(a.get(), Serializer.LONG);
    Random r = new Random(a.get());
    for (int j = 0; j < max; j++) {
      byte[] b = new byte[r.nextInt(100000)];
      r.nextBytes(b);
      long recid = e.put(b, Serializer.BYTE_ARRAY_NOSIZE);
      recids.add(recid);
    }

    e.commit();

    long endTime = TT.nowPlusMinutes(10);

    while (endTime > System.currentTimeMillis()) {

      final CountDownLatch latch = new CountDownLatch(1);
      Thread t =
          new Thread() {
            @Override
            public void run() {
              try {
                for (; ; ) {
                  long A = a.incrementAndGet();
                  Random r = new Random(A);
                  e.update(counterRecid, A, Serializer.LONG);

                  for (long recid : recids) {
                    byte[] b = new byte[r.nextInt(100000)];
                    r.nextBytes(b);
                    e.update(recid, b, Serializer.BYTE_ARRAY_NOSIZE);
                  }
                  e.commit();
                }
              } finally {
                latch.countDown();
              }
            }
          };
      t.start();
      Thread.sleep(5000);
      t.stop();
      latch.await();
      if (!e.isClosed()) {
        close();
      }

      // reopen and check the content
      e = openEngine();

      // check if A-1 was commited
      long A = e.get(counterRecid, Serializer.LONG);
      assertTrue("" + A + " - " + a.get(), A == a.get() || A == a.get() - 1);
      r = new Random(A);
      for (long recid : recids) {
        byte[] b = new byte[r.nextInt(100000)];
        r.nextBytes(b);
        byte[] b2 = e.get(recid, Serializer.BYTE_ARRAY_NOSIZE);
        assertTrue("Data were not commited recid=" + recid, Arrays.equals(b, b2));
      }
      a.set(A);
    }
    e.close();
  }