/**
   * Time a multi-threaded access to a cache.
   *
   * @return the timing stopwatch
   */
  private <V> StopWatch timeMultiThreaded(
      String id, final Map<Integer, V> map, ValueFactory<V> factory) throws InterruptedException {

    StopWatch stopWatch = new StopWatch(id);
    for (int i = 0; i < 500; i++) {
      map.put(i, factory.newValue(i));
    }
    Thread[] threads = new Thread[30];
    stopWatch.start("Running threads");
    for (int threadIndex = 0; threadIndex < threads.length; threadIndex++) {
      threads[threadIndex] =
          new Thread("Cache access thread " + threadIndex) {
            @Override
            public void run() {
              for (int j = 0; j < 1000; j++) {
                for (int i = 0; i < 1000; i++) {
                  map.get(i);
                }
              }
            }
          };
    }
    for (Thread thread : threads) {
      thread.start();
    }

    for (Thread thread : threads) {
      if (thread.isAlive()) {
        thread.join(2000);
      }
    }
    stopWatch.stop();
    return stopWatch;
  }
Пример #2
0
  @Test
  public void testAtomicUpdate() throws Exception {
    execute(
        "create local temporary table x (e1 string, e2 integer, primary key (e2))",
        new List[] {Arrays.asList(0)}); // $NON-NLS-1$
    execute(
        "insert into x (e2, e1) values (1, 'one')", new List[] {Arrays.asList(1)}); // $NON-NLS-1$
    execute(
        "insert into x (e2, e1) values (2, 'one')", new List[] {Arrays.asList(1)}); // $NON-NLS-1$
    try {
      execute("update x set e2 = 3", new List[] {Arrays.asList(1)}); // $NON-NLS-1$
    } catch (TeiidProcessingException e) {
      // should be a duplicate key
    }
    // should revert back to original
    execute("select count(*) from x", new List[] {Arrays.asList(2)}); // $NON-NLS-1$

    Thread t =
        new Thread() {
          public void run() {
            try {
              execute("select count(e1) from x", new List[] {Arrays.asList(2)});
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        };
    t.start();
    t.join(2000);
    assertFalse(t.isAlive());
  }
Пример #3
0
  @Test
  public void concurrent_first_key() {
    DB db = DBMaker.memoryDB().transactionDisable().make();
    final BTreeMap m = db.treeMap("name");

    // fill
    final int c = 1000000 * TT.scale();
    for (int i = 0; i <= c; i++) {
      m.put(i, i);
    }

    Thread t =
        new Thread() {
          @Override
          public void run() {
            for (int i = 0; i <= c; i++) {
              m.remove(c);
            }
          }
        };
    t.run();
    while (t.isAlive()) {
      assertNotNull(m.firstKey());
    }
  }
  @Test
  public void testShouldBlockWhenNotAtHead() throws InterruptedException {
    MockQueue q = new MockQueue();
    final BlockingEnvelopeMap map = new MockBlockingEnvelopeMap(q);

    map.register(SSP, "0");

    Thread t =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                try {
                  // Should trigger a take() call.
                  map.poll(FETCH, -1);
                } catch (InterruptedException e) {
                  throw new RuntimeException(e);
                }
              }
            });

    t.setDaemon(true);
    t.start();
    q.awaitPollTimeout();
    t.join(60000);

    // 1000 = blocking timeout constant
    assertEquals(1000, q.timeout);
    assertFalse(t.isAlive());
  }
Пример #5
0
  void compact_tx_works(final boolean rollbacks, final boolean pre) throws InterruptedException {
    e = openEngine();
    Map<Long, String> m = fill(e);
    e.commit();

    if (pre) e.$_TEST_HACK_COMPACT_PRE_COMMIT_WAIT = true;
    else e.$_TEST_HACK_COMPACT_POST_COMMIT_WAIT = true;

    Thread t =
        new Thread() {
          @Override
          public void run() {
            e.compact();
          }
        };
    t.start();

    Thread.sleep(1000);

    // we should be able to commit while compaction is running
    for (Long recid : m.keySet()) {
      boolean revert = rollbacks && Math.random() < 0.5;
      e.update(recid, "ZZZ", Serializer.STRING);
      if (revert) {
        e.rollback();
      } else {
        e.commit();
        m.put(recid, "ZZZ");
      }
    }

    if (pre) assertTrue(t.isAlive());

    Thread.sleep(1000);

    e.$_TEST_HACK_COMPACT_PRE_COMMIT_WAIT = false;
    e.$_TEST_HACK_COMPACT_POST_COMMIT_WAIT = false;

    t.join();

    for (Long recid : m.keySet()) {
      assertEquals(m.get(recid), e.get(recid, Serializer.STRING));
    }

    e.close();
  }
  @Test
  public void testShouldPollWithATimeout() throws InterruptedException {
    MockQueue q = new MockQueue();
    // Always use the same time in this test so that we can be sure we get a
    // 100ms poll, rather than a 99ms poll (for example). Have to do this
    // because BlockingEnvelopeMap calls clock.currentTimeMillis twice, and
    // uses the second call to determine the actual poll time.
    final BlockingEnvelopeMap map =
        new MockBlockingEnvelopeMap(
            q,
            new Clock() {
              private final long NOW = System.currentTimeMillis();

              public long currentTimeMillis() {
                return NOW;
              }
            });

    map.register(SSP, "0");

    Thread t =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                try {
                  // Should trigger a poll(100, TimeUnit.MILLISECONDS) call.
                  map.poll(FETCH, 100);
                } catch (InterruptedException e) {
                  throw new RuntimeException(e);
                }
              }
            });

    t.setDaemon(true);
    t.start();
    q.awaitPollTimeout();
    t.join(60000);

    assertEquals(100, q.timeout);
    assertFalse(t.isAlive());
  }
Пример #7
0
  @Test(timeout = 100000)
  public void testScheduledLockActionForDeadMember() throws Exception {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = nodeFactory.newHazelcastInstance(new Config());
    final ILock lock1 = h1.getLock("default");
    final HazelcastInstance h2 = nodeFactory.newHazelcastInstance(new Config());
    final ILock lock2 = h2.getLock("default");

    assertTrue(lock1.tryLock());

    final AtomicBoolean error = new AtomicBoolean(false);
    final Thread thread =
        new Thread(
            new Runnable() {
              public void run() {
                try {
                  lock2.lock();
                  error.set(true);
                } catch (Throwable ignored) {
                }
              }
            });
    thread.start();
    Thread.sleep(5000);

    assertTrue(lock1.isLocked());
    h2.getLifecycleService().shutdown();
    thread.join(10000);
    assertFalse(thread.isAlive());
    assertFalse(error.get());

    assertTrue(lock1.isLocked());
    lock1.unlock();
    assertFalse(lock1.isLocked());
    assertTrue(lock1.tryLock());
  }