コード例 #1
0
  @Test(timeout = 20000)
  public void testRebootedDuringCommit() throws Exception {
    Configuration conf = new Configuration();
    conf.set(MRJobConfig.MR_AM_STAGING_DIR, stagingDir);
    conf.setInt(MRJobConfig.MR_AM_MAX_ATTEMPTS, 2);
    AsyncDispatcher dispatcher = new AsyncDispatcher();
    dispatcher.init(conf);
    dispatcher.start();
    CyclicBarrier syncBarrier = new CyclicBarrier(2);
    OutputCommitter committer = new WaitingOutputCommitter(syncBarrier, true);
    CommitterEventHandler commitHandler = createCommitterEventHandler(dispatcher, committer);
    commitHandler.init(conf);
    commitHandler.start();

    AppContext mockContext = mock(AppContext.class);
    when(mockContext.isLastAMRetry()).thenReturn(true);
    when(mockContext.hasSuccessfullyUnregistered()).thenReturn(false);
    JobImpl job = createRunningStubbedJob(conf, dispatcher, 2, mockContext);
    completeJobTasks(job);
    assertJobState(job, JobStateInternal.COMMITTING);

    syncBarrier.await();
    job.handle(new JobEvent(job.getID(), JobEventType.JOB_AM_REBOOT));
    assertJobState(job, JobStateInternal.REBOOT);
    // return the external state as ERROR since this is last retry.
    Assert.assertEquals(JobState.RUNNING, job.getState());
    when(mockContext.hasSuccessfullyUnregistered()).thenReturn(true);
    Assert.assertEquals(JobState.ERROR, job.getState());

    dispatcher.stop();
    commitHandler.stop();
  }
コード例 #2
0
  @Test(timeout = 20000)
  public void testCheckJobCompleteSuccess() throws Exception {
    Configuration conf = new Configuration();
    conf.set(MRJobConfig.MR_AM_STAGING_DIR, stagingDir);
    AsyncDispatcher dispatcher = new AsyncDispatcher();
    dispatcher.init(conf);
    dispatcher.start();
    CyclicBarrier syncBarrier = new CyclicBarrier(2);
    OutputCommitter committer = new TestingOutputCommitter(syncBarrier, true);
    CommitterEventHandler commitHandler = createCommitterEventHandler(dispatcher, committer);
    commitHandler.init(conf);
    commitHandler.start();

    JobImpl job = createRunningStubbedJob(conf, dispatcher, 2, null);
    completeJobTasks(job);
    assertJobState(job, JobStateInternal.COMMITTING);

    // let the committer complete and verify the job succeeds
    syncBarrier.await();
    assertJobState(job, JobStateInternal.SUCCEEDED);

    job.handle(new JobEvent(job.getID(), JobEventType.JOB_TASK_ATTEMPT_COMPLETED));
    assertJobState(job, JobStateInternal.SUCCEEDED);

    job.handle(new JobEvent(job.getID(), JobEventType.JOB_MAP_TASK_RESCHEDULED));
    assertJobState(job, JobStateInternal.SUCCEEDED);

    dispatcher.stop();
    commitHandler.stop();
  }
コード例 #3
0
ファイル: IBSocket.java プロジェクト: rterp/SumZeroTrading
  public void connect() {
    final CyclicBarrier barrier = new CyclicBarrier(2);
    try {
      Thread thread =
          new Thread(
              new Runnable() {
                public void run() {
                  startConnection();
                  try {
                    barrier.await();
                  } catch (Exception ex) {
                    ex.printStackTrace();
                  }
                }
              },
              "Connect() thread: " + IBSocket.class.getName());

      thread.start();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    try {
      barrier.await();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
コード例 #4
0
  public void start() throws Exception {
    for (Thread t : searcherThreads) {
      t.start();
    }
    for (Thread t : indexThreads) {
      t.start();
    }
    barrier1.await();

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    barrier2.await();

    latch.await();
    stopWatch.stop();

    System.out.println("Done, took [" + stopWatch.totalTime() + "]");
    System.out.println("Sleeping before close: " + sleepBeforeClose);
    Thread.sleep(sleepBeforeClose.millis());

    for (Client client : clients) {
      client.close();
    }
    for (Node node : nodes) {
      node.close();
    }

    System.out.println("Sleeping before exit: " + sleepBeforeClose);
    Thread.sleep(sleepAfterDone.millis());
  }
コード例 #5
0
  private void sendRequest(int id, int count) throws Exception {
    int idx = id - 1;

    if (idx < 0) throw new IllegalArgumentException("Connection ID <= 0");

    _socket[idx] =
        _socket[idx] == null ? new Socket("localhost", _connector.getLocalPort()) : _socket[idx];
    _out[idx] =
        _out[idx] == null ? new PrintWriter(_socket[idx].getOutputStream(), true) : _out[idx];
    _in[idx] =
        _in[idx] == null
            ? new BufferedReader(new InputStreamReader(_socket[idx].getInputStream()))
            : _in[idx];

    _connect.reset();

    _out[idx].write("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
    _out[idx].flush();

    _connect.await();

    assertEquals(count, _statistics.getConnectionsOpen());

    String line = _in[idx].readLine();
    while (line != null) {
      if ("Server response".equals(line)) break;
      line = _in[idx].readLine();
    }
  }
コード例 #6
0
  @Test
  public void testAbilityToFireImmediatelyWhenStartedAfter() throws Exception {
    List<Long> jobExecTimestamps = Collections.synchronizedList(new ArrayList<Long>());
    CyclicBarrier barrier = new CyclicBarrier(2);

    Scheduler sched = createScheduler("testAbilityToFireImmediatelyWhenStartedAfter", 5);
    sched.getContext().put(BARRIER, barrier);
    sched.getContext().put(DATE_STAMPS, jobExecTimestamps);

    JobDetail job1 = JobBuilder.newJob(TestJobWithSync.class).withIdentity("job1").build();
    Trigger trigger1 = TriggerBuilder.newTrigger().forJob(job1).build();

    long sTime = System.currentTimeMillis();

    sched.scheduleJob(job1, trigger1);
    sched.start();

    barrier.await(TEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);

    sched.shutdown(true);

    long fTime = jobExecTimestamps.get(0);

    assertTrue(
        "Immediate trigger did not fire within a reasonable amount of time.",
        (fTime - sTime < 7000L)); // This is dangerously subjective!  but what else to do?
  }
    @Override
    public void run() {
      for (int i = 0; i < NUM_GROUPS; i++) {
        for (int j = 1; j <= INSERTIONS_PER_GROUP; j++) {
          // Choosing a unique object id
          int oid = i * INSERTIONS_PER_GROUP + j + oidOffset;
          rootsHolder.addRoot(getNameOID(oid, i), new ObjectID(oid, i));
        }
      }

      try {
        int index = barrier.await();
        assertEquals(2 * NUM_GROUPS * INSERTIONS_PER_GROUP, rootsHolder.size());
        if (index == 0) {
          markLookupInProgress();
        }
        barrier.await();
        if (index == 1) {
          verifyMarked();
          unmarkLookupInProgress();
        }
        barrier.await();
        if (index == 0) {
          verifyUnmarked();
        }
      } catch (Throwable throwable) {
        System.out.println(throwable.getMessage());
        throwable.printStackTrace();
        errorMarker.set(throwable);
      }
    }
コード例 #8
0
ファイル: TimeOutShrink.java プロジェクト: FauxFaux/jdk9-jdk
  private static void realMain(String[] args) throws Throwable {
    final int n = 4;
    final CyclicBarrier barrier = new CyclicBarrier(2 * n + 1);
    final ThreadPoolExecutor pool =
        new ThreadPoolExecutor(
            n, 2 * n, KEEPALIVE_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
    final Runnable r =
        new Runnable() {
          public void run() {
            try {
              barrier.await();
              barrier.await();
            } catch (Throwable t) {
              unexpected(t);
            }
          }
        };

    for (int i = 0; i < 2 * n; i++) pool.execute(r);
    barrier.await();
    checkPoolSizes(pool, 2 * n, n, 2 * n);
    barrier.await();
    long nap = KEEPALIVE_MS + (KEEPALIVE_MS >> 2);
    for (long sleepyTime = 0L; pool.getPoolSize() > n; ) {
      check((sleepyTime += nap) <= LONG_DELAY_MS);
      Thread.sleep(nap);
    }
    checkPoolSizes(pool, n, n, 2 * n);
    Thread.sleep(nap);
    checkPoolSizes(pool, n, n, 2 * n);
    pool.shutdown();
    check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
  }
コード例 #9
0
  /* Regression test for DB-3614 */
  @Test
  @Category(SlowTest.class)
  @Ignore("-sf- takes way too long to fail and interferes rest of build")
  public void testTenTableJoinExplainDuration() throws Exception {
    int size = 10;
    List<String> tables = new ArrayList<String>(size);
    List<String> joins = new ArrayList<String>(size - 1);
    for (int i = 0; i < size; i++) {
      methodWatcher.executeUpdate(format("create table tentab%s (c1 int primary key)", i));
      methodWatcher.executeUpdate(format("insert into tentab%s values (1)", i));
      tables.add(format("tentab%s", i));
      if (i > 0) {
        joins.add(format("tentab%s.c1 = tentab%s.c1", i, i - 1));
      }
    }
    System.out.println("Tables created");
    final String fromClause = Joiner.on(", ").join(tables);
    final String joinCriteria = Joiner.on(" AND ").join(joins);

    ExecutorService es =
        Executors.newSingleThreadExecutor(
            new ThreadFactory() {
              @Override
              public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
              }
            });
    try {
      final CyclicBarrier startLatch = new CyclicBarrier(2);
      final CountDownLatch finishLatch = new CountDownLatch(1);
      Future<Void> f =
          es.submit(
              new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                  String query =
                      format("EXPLAIN SELECT * FROM %s WHERE %s ", fromClause, joinCriteria);
                  startLatch.await();
                  try {
                    ResultSet rs = methodWatcher.executeQuery(query);
                    // Loose check that explain statement took a few seconds or less,
                    // because we're just making sure the short circuit logic in
                    // OptimizerImpl.checkTimeout() blocks this from taking several minutes.
                    Assert.assertTrue("Explain did not return result!", rs.next());
                  } finally {
                    finishLatch.countDown();
                  }
                  return null;
                }
              });
      System.out.println("Starting wait");
      startLatch.await();
      f.get(1, TimeUnit.SECONDS);
      System.out.println("Finished waiting");
    } finally {
      System.out.println("shutting down");
    }
  }
コード例 #10
0
  public void testPrimaryOwnerGoesDownAfterBackupRaisesEvent()
      throws InterruptedException, TimeoutException, ExecutionException, BrokenBarrierException {
    final Cache<Object, String> cache0 = cache(0, CACHE_NAME);
    Cache<Object, String> cache1 = cache(1, CACHE_NAME);
    Cache<Object, String> cache2 = cache(2, CACHE_NAME);

    ClusterListener clusterListener = new ClusterListener();
    cache0.addListener(clusterListener);

    CyclicBarrier barrier = new CyclicBarrier(3);
    BlockingInterceptor blockingInterceptor0 =
        new BlockingInterceptor(barrier, PutKeyValueCommand.class, true, false);
    cache0
        .getAdvancedCache()
        .getAsyncInterceptorChain()
        .addInterceptorBefore(blockingInterceptor0, EntryWrappingInterceptor.class);
    BlockingInterceptor blockingInterceptor2 =
        new BlockingInterceptor(barrier, PutKeyValueCommand.class, true, false);
    cache2
        .getAdvancedCache()
        .getAsyncInterceptorChain()
        .addInterceptorBefore(blockingInterceptor2, EntryWrappingInterceptor.class);

    // this is a replicated cache, all other nodes are backup owners
    final MagicKey key = new MagicKey(cache1);
    Future<String> future = fork(() -> cache0.put(key, FIRST_VALUE));

    // Wait until the primary owner has sent the put command successfully to both backups
    barrier.await(10, TimeUnit.SECONDS);

    // Remove the interceptor so the next command can proceed properly
    cache0
        .getAdvancedCache()
        .getAsyncInterceptorChain()
        .removeInterceptor(BlockingInterceptor.class);
    cache2
        .getAdvancedCache()
        .getAsyncInterceptorChain()
        .removeInterceptor(BlockingInterceptor.class);
    blockingInterceptor0.suspend(true);
    blockingInterceptor2.suspend(true);

    // Kill the cache now - note this will automatically unblock the fork thread
    TestingUtil.killCacheManagers(cache1.getCacheManager());

    // Unblock the command
    barrier.await(10, TimeUnit.SECONDS);

    // This should return null normally, but since it was retried it returns it's own value :(
    // Maybe some day this can work properly
    String returnValue = future.get(10, TimeUnit.SECONDS);
    assertEquals(FIRST_VALUE, returnValue);

    assertEquals(2, clusterListener.events.size());
    checkEvent(clusterListener.events.get(0), key, true, false);

    // We should have received an event that was marked as retried
    checkEvent(clusterListener.events.get(1), key, false, true);
  }
コード例 #11
0
  @Test(timeout = 20000)
  public void testConcurrentAddApplication()
      throws IOException, InterruptedException, BrokenBarrierException {
    final CyclicBarrier startBarrier = new CyclicBarrier(2);
    final CyclicBarrier endBarrier = new CyclicBarrier(2);

    // this token uses barriers to block during renew
    final Credentials creds1 = new Credentials();
    final Token<?> token1 = mock(Token.class);
    creds1.addToken(new Text("token"), token1);
    doReturn(true).when(token1).isManaged();
    doAnswer(
            new Answer<Long>() {
              public Long answer(InvocationOnMock invocation)
                  throws InterruptedException, BrokenBarrierException {
                startBarrier.await();
                endBarrier.await();
                return Long.MAX_VALUE;
              }
            })
        .when(token1)
        .renew(any(Configuration.class));

    // this dummy token fakes renewing
    final Credentials creds2 = new Credentials();
    final Token<?> token2 = mock(Token.class);
    creds2.addToken(new Text("token"), token2);
    doReturn(true).when(token2).isManaged();
    doReturn(Long.MAX_VALUE).when(token2).renew(any(Configuration.class));

    // fire up the renewer
    final DelegationTokenRenewer dtr = createNewDelegationTokenRenewer(conf, counter);
    dtr.init(conf);
    RMContext mockContext = mock(RMContext.class);
    ClientRMService mockClientRMService = mock(ClientRMService.class);
    when(mockContext.getClientRMService()).thenReturn(mockClientRMService);
    InetSocketAddress sockAddr = InetSocketAddress.createUnresolved("localhost", 1234);
    when(mockClientRMService.getBindAddress()).thenReturn(sockAddr);
    dtr.setRMContext(mockContext);
    when(mockContext.getDelegationTokenRenewer()).thenReturn(dtr);
    dtr.start();
    // submit a job that blocks during renewal
    Thread submitThread =
        new Thread() {
          @Override
          public void run() {
            dtr.addApplication(mock(ApplicationId.class), creds1, false, false);
          }
        };
    submitThread.start();

    // wait till 1st submit blocks, then submit another
    startBarrier.await();
    dtr.addApplication(mock(ApplicationId.class), creds2, false, false);
    // signal 1st to complete
    endBarrier.await();
    submitThread.join();
  }
コード例 #12
0
    public static void main(String[] args) throws Throwable {
      final ReentrantLock lock = new ReentrantLock();
      lock.lock();

      final ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock();
      final ReentrantReadWriteLock.ReadLock readLock = rwlock.readLock();
      final ReentrantReadWriteLock.WriteLock writeLock = rwlock.writeLock();
      rwlock.writeLock().lock();

      final BlockingQueue<Object> q = new LinkedBlockingQueue<Object>();
      final Semaphore fairSem = new Semaphore(0, true);
      final Semaphore unfairSem = new Semaphore(0, false);
      // final int threads =
      // rnd.nextInt(Runtime.getRuntime().availableProcessors() + 1) + 1;
      final int threads = 3;
      // On Linux, this test runs very slowly for some reason,
      // so use a smaller number of iterations.
      // Solaris can handle 1 << 18.
      // On the other hand, jmap is much slower on Solaris...
      final int iterations = 1 << 8;
      final CyclicBarrier cb = new CyclicBarrier(threads + 1);

      for (int i = 0; i < threads; i++)
        new Thread() {
          public void run() {
            try {
              final Random rnd = new Random();
              for (int j = 0; j < iterations; j++) {
                if (j == iterations / 10 || j == iterations - 1) {
                  cb.await(); // Quiesce
                  cb.await(); // Resume
                }
                // int t = rnd.nextInt(2000);
                int t = rnd.nextInt(900);
                check(!lock.tryLock(t, NANOSECONDS));
                check(!readLock.tryLock(t, NANOSECONDS));
                check(!writeLock.tryLock(t, NANOSECONDS));
                equal(null, q.poll(t, NANOSECONDS));
                check(!fairSem.tryAcquire(t, NANOSECONDS));
                check(!unfairSem.tryAcquire(t, NANOSECONDS));
              }
            } catch (Throwable t) {
              unexpected(t);
            }
          }
        }.start();

      cb.await(); // Quiesce
      rendezvousChild(); // Measure
      cb.await(); // Resume

      cb.await(); // Quiesce
      rendezvousChild(); // Measure
      cb.await(); // Resume

      System.exit(failed);
    }
コード例 #13
0
  public void run() throws Exception {
    for (Thread t : searcherThreads) {
      t.start();
    }
    for (Thread t : writerThreads) {
      t.start();
    }
    barrier1.await();

    Refresher refresher = new Refresher();
    scheduledExecutorService.scheduleWithFixedDelay(
        refresher, refreshSchedule.millis(), refreshSchedule.millis(), TimeUnit.MILLISECONDS);
    Flusher flusher = new Flusher();
    scheduledExecutorService.scheduleWithFixedDelay(
        flusher, flushSchedule.millis(), flushSchedule.millis(), TimeUnit.MILLISECONDS);

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    barrier2.await();

    latch.await();
    stopWatch.stop();

    System.out.println("Summary");
    System.out.println(
        "   -- Readers ["
            + searcherThreads.length
            + "] with ["
            + searcherIterations
            + "] iterations");
    System.out.println(
        "   -- Writers [" + writerThreads.length + "] with [" + writerIterations + "] iterations");
    System.out.println("   -- Took: " + stopWatch.totalTime());
    System.out.println(
        "   -- Refresh [" + refresher.id + "] took: " + refresher.stopWatch.totalTime());
    System.out.println("   -- Flush [" + flusher.id + "] took: " + flusher.stopWatch.totalTime());
    System.out.println("   -- Store size " + store.estimateSize());

    scheduledExecutorService.shutdown();

    engine.refresh(new Engine.Refresh(true));
    stopWatch = new StopWatch();
    stopWatch.start();
    Engine.Searcher searcher = engine.searcher();
    TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), idGenerator.get() + 1);
    stopWatch.stop();
    System.out.println(
        "   -- Indexed ["
            + idGenerator.get()
            + "] docs, found ["
            + topDocs.totalHits
            + "] hits, took "
            + stopWatch.totalTime());
    searcher.release();
  }
コード例 #14
0
    @SuppressWarnings({"ThrowFromFinallyBlock", "Finally"})
    @Override
    public Response handle(Request request) throws Exception {
      beforeRequest.await(10, TimeUnit.SECONDS);

      try {
        return response.get();
      } finally {
        afterRequest.await(10, TimeUnit.SECONDS);
      }
    }
コード例 #15
0
  /**
   * Method "tryLockUnrestricted".
   *
   * @param key
   * @return {@code true} if the lock was free and was acquired by the current thread, or the lock
   *     was already held by the current thread; and {@code false} otherwise
   * @throws InterruptedException
   * @throws IllegalArgumentException if bean is null
   * @see java.util.concurrent.locks.ReentrantLock#tryLock()
   */
  public boolean tryLockUnrestricted(final KP key) {
    checkStopped();
    blockOperationIfRequired();
    incrementRunningOperations();
    boolean tryLockResultBoolean;
    try {
      final AtomicBoolean tryLockResult = new AtomicBoolean();
      final CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
      Callable<Boolean> callable =
          new Callable<Boolean>() {

            /*
             * (non-Javadoc)
             *
             * @see java.util.concurrent.Callable#call()
             */
            @Override
            public Boolean call() throws Exception {
              boolean locked;
              try {
                locked = locker.tryLock(key);
                tryLockResult.set(locked);
              } finally {
                // STEP 1
                cyclicBarrier.await();
              }
              if (locked) {
                // STEP 2
                cyclicBarrier.await();
                return locker.unlock(key);
              } else {
                return false;
              }
            }
          };
      Future<Boolean> futureTask = threadPool.submit(callable);
      try {
        // STEP 1
        cyclicBarrier.await();
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      tryLockResultBoolean = tryLockResult.get();
      if (tryLockResultBoolean) {
        LockerValue<KP> lockerValue = locker.getLockerValue(key);
        Thread callerThreadLocker = Thread.currentThread();
        lockerValue.addHandler(
            new LockerValueHandler(futureTask, cyclicBarrier, callerThreadLocker));
      }
    } finally {
      decrementRunningOperations();
    }
    return tryLockResultBoolean;
  }
コード例 #16
0
 @Override
 public void execute(JobExecutionContext context) throws JobExecutionException {
   try {
     SchedulerContext schedulerContext = context.getScheduler().getContext();
     schedulerContext.put(JOB_THREAD, Thread.currentThread());
     CyclicBarrier barrier = (CyclicBarrier) schedulerContext.get(BARRIER);
     barrier.await(TEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
   } catch (Throwable e) {
     e.printStackTrace();
     throw new AssertionError("Await on barrier was interrupted: " + e.toString());
   }
 }
コード例 #17
0
 public void test() {
   try {
     barrier.await();
     // wait until monitor is locked by a ::run method
     barrier.await();
   } catch (InterruptedException | BrokenBarrierException e) {
     throw new RuntimeException("Synchronization error happened.", e);
   }
   synchronized (monitor) {
     BusyLock.field++;
   }
 }
コード例 #18
0
ファイル: VoltClient4.java プロジェクト: BenKoerber/voltdb
  @Override
  public int scan(
      String keyspace,
      String lowerBound,
      int recordCount,
      Set<String> columns,
      Vector<HashMap<String, ByteIterator>> result) {
    try {
      byte[] ks = keyspace.getBytes(UTF8);
      ClientResponse response =
          m_client.callProcedure("Scan", ks, lowerBound, lowerBound.getBytes(UTF8), recordCount);
      if (response.getStatus() != ClientResponse.SUCCESS) {
        return 1;
      }

      int nFound = 0;
      String partKey = lowerBound;
      CyclicBarrier barrier = new CyclicBarrier(2);
      result.ensureCapacity(recordCount);
      ScanCallback callback = null;
      boolean proceed = true;
      while (proceed) {
        if (response.getStatus() != ClientResponse.SUCCESS) {
          return 1;
        }
        VoltTable table = response.getResults()[0];
        nFound += table.getRowCount();
        proceed = nFound < recordCount && (partKey = nextPartitionKey(partKey)) != null;
        if (proceed) {
          barrier.reset();
          callback = new ScanCallback(barrier);
          m_client.callProcedure(callback, "Scan", ks, partKey, null, recordCount - nFound);
        }

        while (table.advanceRow()) {
          result.add(unpackRowData(table, columns));
        }

        if (proceed) {
          barrier.await();
          response = callback.response;
        }
      }
      return 0;
    } catch (Exception e) {
      e.printStackTrace();
      return 1;
    }
  }
コード例 #19
0
  /**
   * Tests concurrent reception of multiple messages with a different conn_id
   * (https://issues.jboss.org/browse/JGRP-1347)
   */
  public void testMultipleConcurrentResets() throws Exception {
    sendAndCheck(a, b_addr, 1, r2);

    // now close connection on A unilaterally
    System.out.println("==== Closing the connection on A");
    removeConnection(u1, b_addr);

    r2.clear();

    final UNICAST unicast = (UNICAST) b.getProtocolStack().findProtocol(UNICAST.class);

    int NUM = 10;

    final List<Message> msgs = new ArrayList<Message>(NUM);

    for (int i = 1; i <= NUM; i++) {
      Message msg = new Message(b_addr, a_addr, "m" + i);
      UNICAST.UnicastHeader hdr = UNICAST.UnicastHeader.createDataHeader(1, (short) 2, true);
      msg.putHeader(unicast.getId(), hdr);
      msgs.add(msg);
    }

    Thread[] threads = new Thread[NUM];
    final CyclicBarrier barrier = new CyclicBarrier(NUM + 1);
    for (int i = 0; i < NUM; i++) {
      final int index = i;
      threads[i] =
          new Thread() {
            public void run() {
              try {
                barrier.await();
                unicast.up(new Event(Event.MSG, msgs.get(index)));
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          };
      threads[i].start();
    }

    barrier.await();
    for (Thread thread : threads) thread.join();

    List<Message> list = r2.getMessages();
    System.out.println("list = " + print(list));

    assert list.size() == 1
        : "list must have 1 element but has " + list.size() + ": " + print(list);
  }
コード例 #20
0
  @Test(timeout = 20000l)
  @Ignore
  public void testLockWorksUnderContentionDifferentClients() throws Exception {
    int numThreads = 5;
    final int numIterations = 100;
    ExecutorService service = Executors.newFixedThreadPool(numThreads);
    final CyclicBarrier startBarrier = new CyclicBarrier(numThreads + 1);
    final CyclicBarrier endBarrier = new CyclicBarrier(numThreads + 1);

    final UnsafeOperator operator = new UnsafeOperator();
    for (int i = 0; i < numThreads; i++) {
      service.submit(
          new Callable<Void>() {
            @Override
            public Void call() throws Exception {
              startBarrier.await(); // make sure all threads are in the same place before starting

              // create the lock that we're going to use
              ZooKeeper newZk = newZooKeeper();
              try {
                Lock testLock = new ReentrantZkLock(baseLockPath, new BaseZkSessionManager(newZk));
                for (int j = 0; j < numIterations; j++) {
                  testLock.lock();
                  try {
                    operator.increment();
                  } finally {
                    testLock.unlock();
                  }
                }
              } finally {
                closeZooKeeper(newZk);
              }
              // enter the end barrier to ensure that things are finished
              endBarrier.await();
              return null;
            }
          });
    }

    // start the test
    startBarrier.await();

    // wait for the end of the test
    endBarrier.await();

    // check that the number of operations that actually were recorded are correct
    int correctOps = numIterations * numThreads;
    assertEquals("Number of Operations recorded was incorrect!", correctOps, operator.getValue());
  }
コード例 #21
0
  @Override
  public void run() {
    /*
     * The method is ended when the simulationRunning Flag is
     * set to false.
     */
    while (true) {
      try {
        /*
         * The Threads wait at the startBarrier until they are
         * triggered in the next TimeStep by the run() method in
         * the ParallelQSimEngine.
         */
        startBarrier.await();

        /*
         * Check if Simulation is still running.
         * Otherwise print CPU usage and end Thread.
         */
        if (!simulationRunning) {
          Gbl.printCurrentThreadCpuTime();
          return;
        }

        /* Move Nodes */
        moveNodes(time);

        /*
         * After moving the Nodes all we use a CyclicBarrier to synchronize
         * the Threads.
         */
        this.separationBarrier.await();

        /* Move Links */
        moveLinks(time);

        /*
         * The End of the Moving is synchronized with
         * the endBarrier. If all Threads reach this Barrier
         * the main Thread can go on.
         */
        endBarrier.await();
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      } catch (BrokenBarrierException e) {
        throw new RuntimeException(e);
      }
    }
  }
コード例 #22
0
ファイル: Executing.java プロジェクト: rhusar/JGroups
  protected void handleTaskSubmittedRequest(
      Runnable runnable, Address source, long requestId, long threadId) {
    // We store in our map so that when that task is
    // finished so that we can send back to the owner
    // with the results
    _running.put(runnable, new Owner(source, requestId));
    // We give the task to the thread that is now waiting for it to be returned
    // If we can't offer then we have to respond back to
    // caller that we can't handle it.  They must have
    // gotten our address when we had a consumer, but
    // they went away between then and now.
    boolean received;
    try {
      _tasks.put(threadId, runnable);

      CyclicBarrier barrier = _taskBarriers.remove(threadId);
      if (received = (barrier != null)) {
        // Only wait 10 milliseconds, in case if the consumer was
        // stopped between when we were told it was available and now
        barrier.await(10, TimeUnit.MILLISECONDS);
      }
    } catch (InterruptedException e) {
      if (log.isDebugEnabled()) log.debug("Interrupted while handing off task");
      Thread.currentThread().interrupt();
      received = false;
    } catch (BrokenBarrierException e) {
      if (log.isDebugEnabled())
        log.debug(
            "Consumer " + threadId + " has been interrupted, " + "must retry to submit elsewhere");
      received = false;
    } catch (TimeoutException e) {
      if (log.isDebugEnabled())
        log.debug("Timeout waiting to hand off to barrier, consumer " + threadId + " must be slow");
      // This should only happen if the consumer put the latch then got
      // interrupted but hadn't yet removed the latch, should almost never
      // happen
      received = false;
    }

    if (!received) {
      // Clean up the tasks request
      _tasks.remove(threadId);
      if (log.isDebugEnabled()) log.debug("Run rejected not able to pass off to consumer");
      // If we couldn't hand off the task we have to tell the client
      // and also reupdate the coordinator that our consumer is ready
      sendRequest(source, Type.RUN_REJECTED, requestId, null);
      _running.remove(runnable);
    }
  }
コード例 #23
0
  @Test
  public void testCloseDuringPendingRequest() throws Exception {
    CyclicBarrier beforeRequest = new CyclicBarrier(2);
    CyclicBarrier afterRequest = new CyclicBarrier(2);
    StaticRequestProcessor processor = new StaticRequestProcessor(beforeRequest, afterRequest);
    processor.setResponse(
        new TestingResponse(
            HttpStatus.NO_CONTENT, ImmutableListMultimap.<String, String>of(), new byte[0]));

    CyclicBarrier requestComplete = new CyclicBarrier(2);
    TestingClientCallback callback = new TestingClientCallback(requestComplete);

    URI location = URI.create("http://localhost:8080");
    HttpPageBufferClient client =
        new HttpPageBufferClient(
            new TestingHttpClient(processor, executor),
            new DataSize(10, Unit.MEGABYTE),
            new Duration(1, TimeUnit.MINUTES),
            location,
            callback,
            blockEncodingManager,
            executor,
            Stopwatch.createUnstarted());

    assertStatus(client, location, "queued", 0, 0, 0, 0, "not scheduled");

    // send request
    client.scheduleRequest();
    beforeRequest.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "running", 0, 1, 0, 0, "PROCESSING_REQUEST");
    assertEquals(client.isRunning(), true);
    // request is pending, now close it
    client.close();

    try {
      requestComplete.await(10, TimeUnit.SECONDS);
    } catch (BrokenBarrierException ignored) {
    }
    try {
      afterRequest.await(10, TimeUnit.SECONDS);
    } catch (BrokenBarrierException ignored) {
      afterRequest.reset();
    }
    // client.close() triggers a DELETE request, so wait for it to finish
    beforeRequest.await(10, TimeUnit.SECONDS);
    afterRequest.await(10, TimeUnit.SECONDS);
    requestComplete.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "closed", 0, 1, 2, 1, "not scheduled");
  }
コード例 #24
0
  /** @throws Exception If any error occurs. */
  public void testMultipleStartOnCoordinatorStop() throws Exception {
    for (int k = 0; k < 3; k++) {
      log.info("Iteration: " + k);

      clientFlagGlobal = false;

      final int START_NODES = 5;
      final int JOIN_NODES = 10;

      startGrids(START_NODES);

      final CyclicBarrier barrier = new CyclicBarrier(JOIN_NODES + 1);

      final AtomicInteger startIdx = new AtomicInteger(START_NODES);

      IgniteInternalFuture<?> fut =
          GridTestUtils.runMultiThreadedAsync(
              new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                  int idx = startIdx.getAndIncrement();

                  Thread.currentThread().setName("start-thread-" + idx);

                  barrier.await();

                  Ignite ignite = startGrid(idx);

                  assertFalse(ignite.configuration().isClientMode());

                  log.info("Started node: " + ignite.name());

                  return null;
                }
              },
              JOIN_NODES,
              "start-thread");

      barrier.await();

      U.sleep(ThreadLocalRandom.current().nextInt(10, 100));

      for (int i = 0; i < START_NODES; i++) stopGrid(i);

      fut.get();

      stopAllGrids();
    }
  }
コード例 #25
0
    public void execute(JobExecutionContext context) throws JobExecutionException {
      try {
        @SuppressWarnings("unchecked")
        List<Long> jobExecTimestamps =
            (List<Long>) context.getScheduler().getContext().get(DATE_STAMPS);
        CyclicBarrier barrier = (CyclicBarrier) context.getScheduler().getContext().get(BARRIER);

        jobExecTimestamps.add(System.currentTimeMillis());

        barrier.await(TEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
      } catch (Throwable e) {
        e.printStackTrace();
        throw new AssertionError("Await on barrier was interrupted: " + e.toString());
      }
    }
コード例 #26
0
 @Override
 public void onDestroy() {
   super.onDestroy();
   if (mThumbnailer != null) mThumbnailer.clearJobs();
   mBarrier.reset();
   mVideoAdapter.clear();
 }
コード例 #27
0
ファイル: Searcher.java プロジェクト: homar/javaConcurrency
 @Override
 public void run() {
   int counter;
   System.out.printf(
       "%s: Processing lines from %d to %d\n",
       Thread.currentThread().getName(), firstRow, lastRow);
   for (int i = firstRow; i < lastRow; i++) {
     int row[] = mock.getRow(i);
     counter = 0;
     for (int j = 0; j < row.length; j++) {
       if (row[j] == number) {
         counter++;
       }
     }
     results.setData(i, counter);
     try {
       Thread.sleep(10);
     } catch (InterruptedException e) {
       // TODO: handle exception
     }
   }
   System.out.printf("%s: Lines processed \n", Thread.currentThread().getName());
   try {
     barrier.await();
   } catch (InterruptedException e) {
     e.printStackTrace();
   } catch (BrokenBarrierException e) {
     e.printStackTrace();
   }
 }
コード例 #28
0
    @Override
    public StartContainersResponse startContainers(StartContainersRequest request)
        throws IOException {
      try {
        startLaunchBarrier.await();
        completeLaunchBarrier.await();
        // To ensure the kill is started before the launch
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (BrokenBarrierException e) {
        e.printStackTrace();
      }

      throw new IOException(new ContainerException("Force fail CM"));
    }
コード例 #29
0
 private static void join(final CyclicBarrier barrier) {
   try {
     barrier.await(Timeout.standardTimeoutMillis(), TimeUnit.MILLISECONDS);
   } catch (Exception e) {
     throw new OpenGammaRuntimeException("interrupted", e);
   }
 }
コード例 #30
0
    final void test() throws Exception {
      Future[] futures = new Future[nthreads];
      for (int i = 0; i < nthreads; ++i) futures[i] = pool.submit(this);

      barrier.await();
      Thread.sleep(TIMEOUT);
      boolean tooLate = false;
      for (int i = 1; i < nthreads; ++i) {
        if (!futures[i].cancel(true)) tooLate = true;
        // Unbunch some of the cancels
        if ((i & 3) == 0) Thread.sleep(1 + rng.next() % 10);
      }

      Object f0 = futures[0].get();
      if (!tooLate) {
        for (int i = 1; i < nthreads; ++i) {
          if (!futures[i].isDone() || !futures[i].isCancelled())
            throw new Error("Only one thread should complete");
        }
      } else System.out.print("(cancelled too late) ");

      long endTime = System.nanoTime();
      long time = endTime - timer.startTime;
      if (print) {
        double secs = (double) (time) / 1000000000.0;
        System.out.println("\t " + secs + "s run time");
      }
    }