Esempio n. 1
1
  @Test(groups = "standalone", enabled = false)
  public void testProxyActivationProperty()
      throws IOException, ExecutionException, TimeoutException, InterruptedException {
    // FIXME not threadsafe!
    Properties originalProps = new Properties();
    originalProps.putAll(System.getProperties());
    System.setProperty(ProxyUtils.PROXY_HOST, "127.0.0.1");
    System.setProperty(ProxyUtils.PROXY_PORT, String.valueOf(port1));
    System.setProperty(ProxyUtils.PROXY_NONPROXYHOSTS, "localhost");
    System.setProperty(
        AsyncHttpClientConfigDefaults.ASYNC_CLIENT_CONFIG_ROOT + "useProxyProperties", "true");
    AsyncHttpClientConfigHelper.reloadProperties();

    try (AsyncHttpClient client = asyncHttpClient()) {
      String proxifiedTarget = "http://127.0.0.1:1234/";
      Future<Response> f = client.prepareGet(proxifiedTarget).execute();
      Response resp = f.get(3, TimeUnit.SECONDS);
      assertNotNull(resp);
      assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
      assertEquals(resp.getHeader("target"), "/");

      String nonProxifiedTarget = "http://localhost:1234/";
      f = client.prepareGet(nonProxifiedTarget).execute();
      try {
        resp = f.get(3, TimeUnit.SECONDS);
        fail("should not be able to connect");
      } catch (ExecutionException e) {
        // ok, no proxy used
      }
    } finally {
      System.setProperties(originalProps);
    }
  }
  @Test
  public void testCreateNewIfExpired() throws Exception {
    final LocalConnFactory connFactory = Mockito.mock(LocalConnFactory.class);

    final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
    Mockito.when(conn1.isOpen()).thenReturn(true);
    Mockito.when(connFactory.create(Mockito.eq("somehost"))).thenReturn(conn1);

    final LocalConnPool pool = new LocalConnPool(connFactory, 2, 2);

    final Future<LocalPoolEntry> future1 = pool.lease("somehost", null);
    final LocalPoolEntry entry1 = future1.get(1, TimeUnit.SECONDS);
    Assert.assertNotNull(entry1);

    Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.eq("somehost"));

    entry1.updateExpiry(1, TimeUnit.MILLISECONDS);
    pool.release(entry1, true);

    Thread.sleep(200L);

    final Future<LocalPoolEntry> future2 = pool.lease("somehost", null);
    final LocalPoolEntry entry2 = future2.get(1, TimeUnit.SECONDS);
    Assert.assertNotNull(entry2);

    Mockito.verify(connFactory, Mockito.times(2)).create(Mockito.eq("somehost"));

    final PoolStats totals = pool.getTotalStats();
    Assert.assertEquals(0, totals.getAvailable());
    Assert.assertEquals(1, totals.getLeased());
    Assert.assertEquals(Collections.singleton("somehost"), pool.getRoutes());
    final PoolStats stats = pool.getStats("somehost");
    Assert.assertEquals(0, stats.getAvailable());
    Assert.assertEquals(1, stats.getLeased());
  }
 public void submit() {
   dbTextArea.clear();
   LocalDate from = fromDate.getValue(), to = toDate.getValue();
   Future<String[]> fwarNames = db.getWarNamesByDate(from, to);
   HashSet<Future<long[]>> fstats = new HashSet<Future<long[]>>();
   try {
     String[] warNames = fwarNames.get();
     for (String war : warNames) {
       fstats.add(db.getWarStats(war));
     }
     int i = 0;
     for (Future<long[]> fWarStats : fstats) {
       long[] stats = fWarStats.get();
       dbTextArea.appendText("\t" + warNames[i++] + " Statistics\n");
       dbTextArea.appendText("=========================================\n");
       dbTextArea.appendText('\t' + stats[0] == 0 ? "War is still running" : "\tWar has ended\n");
       dbTextArea.appendText("\tNum of launch missiles: " + stats[1] + "\n");
       dbTextArea.appendText("\tNum of intercept missiles: " + stats[2] + "\n");
       dbTextArea.appendText("\tNum of hit target missiles: " + stats[3] + "\n");
       dbTextArea.appendText("\tNum of launchers destroyed: " + stats[4] + "\n");
       dbTextArea.appendText("\ttotal damage: " + stats[5] + "\n");
       dbTextArea.appendText("==========================================\n");
     }
   } catch (InterruptedException | ExecutionException e) {
     e.printStackTrace();
   }
 }
  private int[] computeClusterNumberRange(int min, int max, int hop, Optimizer optimizer)
      throws ExecutionException, InterruptedException {
    ExecutorService executor;
    if (parallelWorkers > 1) {
      executor = Executors.newFixedThreadPool(2);
    } else {
      executor = Executors.newFixedThreadPool(1);
    }
    ConcurrentMap<Integer, Double> sharedScores = new ConcurrentHashMap<>();
    SearchSpaceBoundaryFinder_old upperBoundFinder =
        new SearchSpaceUpperBoundaryFinder_old(min, max, hop, optimizer, sharedScores);
    Future<Integer> futureUpperBound = executor.submit(upperBoundFinder);
    SearchSpaceBoundaryFinder_old lowerBoundFinder =
        new SearchSpaceLowerBoundaryFinder_old(min, max, hop, optimizer, sharedScores);
    Future<Integer> futureLowerBound = executor.submit(lowerBoundFinder);
    executor.shutdown();
    int[] r = new int[2];
    Integer realMin = futureLowerBound.get();
    Integer realMax = futureUpperBound.get();
    r[0] = realMin == null ? -1 : realMin;
    r[1] = realMax == null ? -1 : realMax;

    scores.putAll(lowerBoundFinder.getSplitsAndScores());
    // scores.putAll(upperBoundFinder.getSplitsAndScores());
    return r;
  }
Esempio n. 5
0
 private Object invokeOperation(Data key, MapOperation operation) {
   int partitionId = getNodeEngine().getPartitionService().getPartitionId(key);
   operation.setThreadId(ThreadUtil.getThreadId());
   try {
     Object result;
     if (statisticsEnabled) {
       long time = System.currentTimeMillis();
       Future future =
           operationService
               .createInvocationBuilder(SERVICE_NAME, operation, partitionId)
               .setResultDeserialized(false)
               .invoke();
       result = future.get();
       mapServiceContext.incrementOperationStats(time, localMapStats, name, operation);
     } else {
       Future future =
           operationService
               .createInvocationBuilder(SERVICE_NAME, operation, partitionId)
               .setResultDeserialized(false)
               .invoke();
       result = future.get();
     }
     return result;
   } catch (Throwable t) {
     throw rethrow(t);
   }
 }
Esempio n. 6
0
 private Permit getEither(Future<Permit> f1, Future<Permit> f2) throws Exception {
   try {
     return f1.get(10, TimeUnit.MILLISECONDS);
   } catch (TimeoutException e) {
     return f2.get(10, TimeUnit.MILLISECONDS);
   }
 }
Esempio n. 7
0
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    final PipedInputStream p1i = new PipedInputStream();
    final PipedInputStream p2i = new PipedInputStream();
    final PipedOutputStream p1o = new PipedOutputStream(p1i);
    final PipedOutputStream p2o = new PipedOutputStream(p2i);

    Future<Channel> f1 =
        executors.submit(
            new Callable<Channel>() {
              public Channel call() throws Exception {
                return new Channel("This side of the channel", executors, p1i, p2o);
              }
            });
    Future<Channel> f2 =
        executors.submit(
            new Callable<Channel>() {
              public Channel call() throws Exception {
                return new Channel("The other side of the channel", executors, p2i, p1o);
              }
            });
    french = f1.get();
    british = f2.get();
  }
  @Test
  @PresetData(PresetData.DataSet.NO_ANONYMOUS_READACCESS)
  public void testRunnableAgainstAllContexts() throws Exception {
    Runnable r =
        new Runnable() {
          public void run() {
            runnableThreadContext = SecurityContextHolder.getContext();
          }
        };
    SecurityContextHolder.setContext(systemContext);
    Future systemResult = wrappedService.submit(r);
    // Assert the runnable completed successfully
    assertNull(systemResult.get());
    // Assert the context inside the runnable thread was set to ACL.SYSTEM
    assertEquals(systemContext, runnableThreadContext);

    SecurityContextHolder.setContext(userContext);
    Future userResult = wrappedService.submit(r);
    // Assert the runnable completed successfully
    assertNull(userResult.get());
    // Assert the context inside the runnable thread was set to the user's context
    assertEquals(userContext, runnableThreadContext);

    SecurityContextHolder.setContext(nullContext);
    Future nullResult = wrappedService.submit(r);
    // Assert the runnable completed successfully
    assertNull(nullResult.get());
    // Assert the context inside the runnable thread was set to the null context
    assertEquals(nullContext, runnableThreadContext);
  }
Esempio n. 9
0
  @Test
  public void testEvictUpdateExpiration() throws Exception {
    CyclicBarrier loadBarrier = new CyclicBarrier(2);
    CountDownLatch preFlushLatch = new CountDownLatch(1);
    CountDownLatch postEvictLatch = new CountDownLatch(1);
    CountDownLatch flushLatch = new CountDownLatch(1);
    CountDownLatch commitLatch = new CountDownLatch(1);

    Future<Boolean> first = evictWait(itemId, loadBarrier, null, postEvictLatch);
    Future<Boolean> second =
        updateFlushWait(itemId, loadBarrier, preFlushLatch, flushLatch, commitLatch);
    awaitOrThrow(postEvictLatch);

    Map contents = Caches.entrySet(entityCache).toMap();
    assertEquals(Collections.EMPTY_MAP, contents);
    assertNull(contents.get(itemId));

    preFlushLatch.countDown();
    awaitOrThrow(flushLatch);
    contents = Caches.entrySet(entityCache).toMap();
    assertEquals(1, contents.size());
    assertEquals(FutureUpdate.class, contents.get(itemId).getClass());

    commitLatch.countDown();
    first.get(WAIT_TIMEOUT, TimeUnit.SECONDS);
    second.get(WAIT_TIMEOUT, TimeUnit.SECONDS);

    contents = Caches.entrySet(entityCache).toMap();
    assertEquals(1, contents.size());
    Object value = contents.get(itemId);
    assertNotNull(value);
    assertEquals(StandardCacheEntryImpl.class, value.getClass());
    TIME_SERVICE.advance(timeout + 1);
    assertEquals(value, entityCache.get(itemId));
  }
  /**
   * When a build is marked as NOT_BUILD And more builds are started And there are no changes Then
   * all following builds should also be marked NOT_BUILD
   */
  public void testShouldMarkBuildsAsNotBuilt() throws Exception {
    setup();
    File dir = createTempDirectory();
    MercurialBridge plugin = new MercurialBridge(false, "test", "default");
    plugin.setWorkingDirectory(new FilePath(dir));

    hg(dir, "init");
    shell(dir, "touch", "foo");
    hg(dir, "add", "foo");
    hg(dir, "commit", "-m", "\"added foo\"");
    // String rev = hg(dir,"tip","--template","{node}").toString();
    hg(dir, "branch", "test");
    shell(dir, "touch", "bar");
    hg(dir, "add", "bar");
    hg(dir, "commit", "-m", "\"added bar\"");

    MercurialSCM scm =
        new MercurialSCM(null, dir.getAbsolutePath(), "test", null, null, null, true);
    FreeStyleProject project =
        Hudson.getInstance().createProject(FreeStyleProject.class, "testproject");
    project.setScm(scm);
    project.getBuildWrappersList().add(new PretestedIntegrationBuildWrapper(plugin));

    Future<FreeStyleBuild> f = project.scheduleBuild2(0);
    FreeStyleBuild build = f.get();
    assertEquals(Result.SUCCESS, build.getResult());
    f = project.scheduleBuild2(0);
    build = f.get();
    assertEquals(Result.NOT_BUILT, build.getResult());
    f = project.scheduleBuild2(0);
    build = f.get();
    assertEquals(Result.NOT_BUILT, build.getResult());
    cleanup(dir);
  }
Esempio n. 11
0
  @Test
  public void testFutureUpdateExpiration() throws Exception {
    CyclicBarrier loadBarrier = new CyclicBarrier(2);
    CountDownLatch flushLatch = new CountDownLatch(2);
    CountDownLatch commitLatch = new CountDownLatch(1);

    Future<Boolean> first = updateFlushWait(itemId, loadBarrier, null, flushLatch, commitLatch);
    Future<Boolean> second = updateFlushWait(itemId, loadBarrier, null, flushLatch, commitLatch);
    awaitOrThrow(flushLatch);

    Map contents = Caches.entrySet(entityCache).toMap();
    assertEquals(1, contents.size());
    assertEquals(FutureUpdate.class, contents.get(itemId).getClass());
    commitLatch.countDown();
    first.get(WAIT_TIMEOUT, TimeUnit.SECONDS);
    second.get(WAIT_TIMEOUT, TimeUnit.SECONDS);

    // since we had two concurrent updates, the result should be invalid
    contents = Caches.entrySet(entityCache).toMap();
    assertEquals(1, contents.size());
    Object value = contents.get(itemId);
    if (value instanceof FutureUpdate) {
      // DB did not blocked two concurrent updates
      TIME_SERVICE.advance(timeout + 1);
      assertNull(entityCache.get(itemId));
      contents = Caches.entrySet(entityCache).toMap();
      assertEquals(Collections.EMPTY_MAP, contents);
    } else {
      // DB left only one update to proceed, and the entry should not be expired
      assertNotNull(value);
      assertEquals(StandardCacheEntryImpl.class, value.getClass());
      TIME_SERVICE.advance(timeout + 1);
      assertEquals(value, entityCache.get(itemId));
    }
  }
Esempio n. 12
0
  @Test
  public void concurrentOnlyOneWritesPackedRefs() throws Exception {
    RevBlob a = tr.blob("a");
    tr.lightweightTag("t", a);

    final CyclicBarrier syncPoint = new CyclicBarrier(2);

    Callable<Integer> packRefs =
        new Callable<Integer>() {

          /** @return 0 for success, 1 in case of error when writing pack */
          public Integer call() throws Exception {
            syncPoint.await();
            try {
              gc.packRefs();
              return valueOf(0);
            } catch (IOException e) {
              return valueOf(1);
            }
          }
        };
    ExecutorService pool = Executors.newFixedThreadPool(2);
    try {
      Future<Integer> p1 = pool.submit(packRefs);
      Future<Integer> p2 = pool.submit(packRefs);
      assertEquals(1, p1.get().intValue() + p2.get().intValue());
    } finally {
      pool.shutdown();
      pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
    }
  }
Esempio n. 13
0
  @Test
  public void testRemoveUpdateExpiration() throws Exception {
    CyclicBarrier loadBarrier = new CyclicBarrier(2);
    CountDownLatch preFlushLatch = new CountDownLatch(1);
    CountDownLatch flushLatch = new CountDownLatch(1);
    CountDownLatch commitLatch = new CountDownLatch(1);

    Future<Boolean> first = removeFlushWait(itemId, loadBarrier, null, flushLatch, commitLatch);
    Future<Boolean> second = updateFlushWait(itemId, loadBarrier, preFlushLatch, null, commitLatch);
    awaitOrThrow(flushLatch);

    Map contents = Caches.entrySet(entityCache).toMap();
    assertEquals(1, contents.size());
    assertEquals(Tombstone.class, contents.get(itemId).getClass());

    preFlushLatch.countDown();
    commitLatch.countDown();
    first.get(WAIT_TIMEOUT, TimeUnit.SECONDS);
    second.get(WAIT_TIMEOUT, TimeUnit.SECONDS);

    contents = Caches.entrySet(entityCache).toMap();
    assertEquals(1, contents.size());
    assertEquals(Tombstone.class, contents.get(itemId).getClass());

    TIME_SERVICE.advance(timeout + 1);
    assertNull(entityCache.get(itemId)); // force expiration
    contents = Caches.entrySet(entityCache).toMap();
    assertEquals(Collections.EMPTY_MAP, contents);
  }
  @Test
  public void testValidateConnectionStale() throws Exception {
    final HttpConnection conn = Mockito.mock(HttpConnection.class);
    Mockito.when(conn.isOpen()).thenReturn(true);
    Mockito.when(conn.isStale()).thenReturn(false);

    final LocalConnFactory connFactory = Mockito.mock(LocalConnFactory.class);
    Mockito.when(connFactory.create(Mockito.eq("somehost"))).thenReturn(conn);

    final LocalConnPool pool = new LocalConnPool(connFactory, 2, 10);
    pool.setValidateAfterInactivity(5);

    final Future<LocalPoolEntry> future1 = pool.lease("somehost", null);
    final LocalPoolEntry entry1 = future1.get(1, TimeUnit.SECONDS);
    Assert.assertNotNull(entry1);

    pool.release(entry1, true);

    Thread.sleep(10);

    Mockito.verify(connFactory, Mockito.times(1)).create("somehost");
    Mockito.when(conn.isStale()).thenReturn(true);

    final Future<LocalPoolEntry> future2 = pool.lease("somehost", null);
    final LocalPoolEntry entry2 = future2.get(1, TimeUnit.SECONDS);
    Assert.assertNotNull(entry2);
    Assert.assertNotSame(entry1, entry2);

    Mockito.verify(conn, Mockito.times(1)).isStale();
    Mockito.verify(conn, Mockito.times(1)).close();
    Mockito.verify(connFactory, Mockito.times(2)).create("somehost");
  }
Esempio n. 15
0
  /**
   * Test concurrent reader and writer (GH-458).
   *
   * <p><b>Test case:</b>
   *
   * <ol>
   *   <li/>start a long running reader;
   *   <li/>try to start a writer: it should time out;
   *   <li/>stop the reader;
   *   <li/>start the writer again: it should succeed.
   * </ol>
   *
   * @throws Exception error during request execution
   */
  @Test
  @Ignore("There is no way to stop a query on the server!")
  public void testReaderWriter() throws Exception {
    final String readerQuery = "?query=(1%20to%20100000000000000)%5b.=1%5d";
    final String writerQuery = "/test.xml";
    final byte[] content = Token.token("<a/>");

    final Get readerAction = new Get(readerQuery);
    final Put writerAction = new Put(writerQuery, content);

    final ExecutorService exec = Executors.newFixedThreadPool(2);

    // start reader
    exec.submit(readerAction);
    Performance.sleep(TIMEOUT); // delay in order to be sure that the reader has started
    // start writer
    Future<HTTPResponse> writer = exec.submit(writerAction);

    try {
      final HTTPResponse result = writer.get(TIMEOUT, TimeUnit.MILLISECONDS);

      if (result.status.isSuccess()) fail("Database modified while a reader is running");
      throw new Exception(result.toString());
    } catch (final TimeoutException e) {
      // writer is blocked by the reader: stop it
      writerAction.stop = true;
    }

    // stop reader
    readerAction.stop = true;

    // start the writer again
    writer = exec.submit(writerAction);
    assertEquals(HTTPCode.CREATED, writer.get().status);
  }
Esempio n. 16
0
 // TODO: should this actually throw only ExecutionException?
 public <T> T callWithTimeout(
     Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean amInterruptible)
     throws Exception {
   checkNotNull(callable);
   checkNotNull(timeoutUnit);
   checkArgument(timeoutDuration > 0, "bad timeout: " + timeoutDuration);
   Future<T> future = executor.submit(callable);
   try {
     if (amInterruptible) {
       try {
         return future.get(timeoutDuration, timeoutUnit);
       } catch (InterruptedException e) {
         future.cancel(true);
         throw e;
       }
     } else {
       Future<T> uninterruptible = Futures.makeUninterruptible(future);
       return uninterruptible.get(timeoutDuration, timeoutUnit);
     }
   } catch (ExecutionException e) {
     throw Throwables.throwCause(e, true);
   } catch (TimeoutException e) {
     future.cancel(true);
     throw new UncheckedTimeoutException(e);
   }
 }
Esempio n. 17
0
 private void releaseTheHounds() {
   Future<Boolean> future = sparqlOnDemandIndexer.asyncUpdatesAndDeletes(toUpdate, toDelete);
   boolean success = false;
   try {
     success = future.get(120, TimeUnit.SECONDS);
   } catch (Exception e) {
     logger.error("Async triplestore update exception", e);
   }
   if (!success) {
     logger.error("Triplestore update timed out, retrying.");
     future = sparqlOnDemandIndexer.asyncUpdatesAndDeletes(toUpdate, toDelete);
     try {
       success = future.get(120, TimeUnit.SECONDS);
     } catch (Exception e) {
       logger.error("Async triplestore retry update exception", e);
     }
     if (!success) {
       toUpdate.clear();
       toDelete.clear();
       throw new RuntimeException("Triplestore update timed out twice, that sux a big one.");
     }
   }
   toUpdate.clear();
   toDelete.clear();
 }
Esempio n. 18
0
  /**
   * Makes sure that multiple threads of the same Java process cannot lock the same directory at
   * once.
   *
   * @throws Exception
   */
  @Test
  public void multipleThreadsOfCurrentJvmSameVersion() throws Exception {

    final PathLocker<SrcVersion> pathLocker = new PathLocker<>();

    final Path dir1 = lockerDirectory.resolve(UUID.randomUUID().toString());
    final SrcVersion srcVersion = SrcVersion.parse("1.2.3-SRC-revision-deadbeef");

    Future<PathLock> concurrLockFuture = null;
    try (PathLock lock1 = pathLocker.lockDirectory(dir1, srcVersion)) {
      /* locked for the current thread */

      /* now try to lock from another thread which should fail with a TimeoutException
       * because we have locked above */
      try {
        concurrLockFuture = lockConcurrently(pathLocker, dir1, srcVersion);
        concurrLockFuture.get(1, TimeUnit.SECONDS);
        Assert.fail("TimeoutException expected");
      } catch (InterruptedException e) {
        throw e;
      } catch (ExecutionException e) {
        throw e;
      } catch (TimeoutException e) {
        /* expected */
      }
    }

    /* unlocked again - the above attempt to lock from a concurrent thread must succeed now */
    PathLock lock2 = concurrLockFuture.get(1, TimeUnit.SECONDS);
    Assert.assertNotNull(lock2);
  }
  @Test
  public void testFlush() throws Exception {
    final int registerCount = 1000;
    ExecutorService pool = Executors.newCachedThreadPool();
    try {
      final HeapSizeManager underTest = new HeapSizeManager(100l, 100);
      final AtomicBoolean allOperationsDone = new AtomicBoolean();
      final LinkedBlockingQueue<Long> registeredEvents = new LinkedBlockingQueue<>();
      Future<?> writeFuture =
          pool.submit(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    for (int i = 0; i < registerCount; i++) {
                      registeredEvents.offer(underTest.registerOperationWithHeapSize(1));
                    }
                    underTest.flush();
                    allOperationsDone.set(true);
                    synchronized (allOperationsDone) {
                      allOperationsDone.notify();
                    }
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                  }
                }
              });
      Thread.sleep(100);
      Future<?> readFuture =
          pool.submit(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    for (int i = 0; i < registerCount; i++) {
                      Long registeredId = registeredEvents.poll(1, TimeUnit.SECONDS);
                      if (registeredId == null) {
                        i--;
                      } else {
                        if (i % 50 == 0) {
                          // Exercise the .offer and the flush() in the writeFuture.
                          Thread.sleep(40);
                        }
                        underTest.markCanBeCompleted(registeredId);
                      }
                    }
                  } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                  }
                }
              });

      readFuture.get(30, TimeUnit.SECONDS);
      writeFuture.get(30, TimeUnit.SECONDS);
      assertTrue(allOperationsDone.get());
    } finally {
      pool.shutdownNow();
    }
  }
Esempio n. 20
0
  public void testTakeBlockedByProperty() throws Exception {
    Slave slave = createSlave();
    FreeStyleProject project = createFreeStyleProject();

    // First, attempt to run our project before adding the property
    Future<FreeStyleBuild> build = project.scheduleBuild2(0);
    assertBuildStatus(Result.SUCCESS, build.get(20, TimeUnit.SECONDS));

    // Add the build-blocker property and try again
    slave.getNodeProperties().add(new RejectAllTasksProperty());

    build = project.scheduleBuild2(0);
    try {
      build.get(10, TimeUnit.SECONDS);
      fail("Expected timeout exception");
    } catch (TimeoutException e) {
      List<BuildableItem> buildables = jenkins.getQueue().getBuildableItems();
      assertNotNull(buildables);
      assertEquals(1, buildables.size());

      BuildableItem item = buildables.get(0);
      assertEquals(project, item.task);
      assertNotNull(item.getCauseOfBlockage());
      assertEquals(
          Messages.Queue_WaitingForNextAvailableExecutor(),
          item.getCauseOfBlockage().getShortDescription());
    }
  }
  /** 存入一个对象 */
  private boolean _set(String key, Object value) {
    // mc1.delete(key);
    // mc2.delete(key);
    boolean ret = false;
    Future<Boolean> f = mc1.set(key, expHour * 60 * 60, value);
    Future<Boolean> f2 = mc2.set(key, expHour * 60 * 60, value);
    try {
      boolean fs1 = f.get(opTimeout, TimeUnit.SECONDS);
      boolean fs2 = f2.get(opTimeout, TimeUnit.SECONDS);
      ret = fs1 || fs2;

      if (!fs1) {
        log.info(
            "[FAIL]CACHE SET FAIL:server1 set failed: "
                + "Key="
                + key
                + "\tValue="
                + value.toString());
      } else if (!fs2) {
        log.info(
            "[FAIL]CACHE SET FAIL:server2 set failed: "
                + "Key="
                + key
                + "\tValue="
                + value.toString());
      }
    } catch (TimeoutException e) {
      // Since we don't need this, go ahead and cancel the
      // operation. This
      // is not strictly necessary, but it'll save some work on
      // the server.
      log.info("[FAIL]time out when getting objects from cache server2...");
      f.cancel(false);
      f2.cancel(false);
      // Do other timeout related stuff
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      log.error("[ERROR]exception when setting fengchao cache - thread been interrupted...", e);
      f.cancel(false);
      f2.cancel(false);
    } catch (ExecutionException e) {
      // TODO Auto-generated catch block
      log.error(
          "[ERROR]exception when setting fengchao cache - exception when getting status...", e);
      f.cancel(false);
      f2.cancel(false);
    } catch (Exception e) {
      log.error("[ERROR]exception when setting fengchao cache - other exceptions...", e);
      f.cancel(false);
      f2.cancel(false);
    }

    if (value != null) {
      log.info("MemCacheServiceImpl.set,key=" + key + ",value=" + value.getClass());
    } else {
      log.info("MemCacheServiceImpl.set,key=" + key + ",value=null");
    }
    return ret;
  }
Esempio n. 22
0
  @Test
  public void test() throws Exception {
    AsyncMutex mutex = new AsyncMutex();

    final RichFuture<Permit> fPermit1 = mutex.acquire();
    final RichFuture<Permit> fPermit2 = mutex.acquire();
    final RichFuture<Permit> fPermit3 = mutex.acquire();

    assertThat(mutex.getNumPermitsAvailable(), is(0));
    assertThat(mutex.getNumWaiters(), is(2));

    Permit permit1 = fPermit1.apply();

    Future<Permit> waitPermit2 =
        executor.submit(
            new Callable<Permit>() {
              public Permit call() throws Exception {
                return fPermit2.apply();
              }
            });
    Future<Permit> waitPermit3 =
        executor.submit(
            new Callable<Permit>() {
              public Permit call() throws Exception {
                return fPermit3.apply();
              }
            });

    try {
      waitPermit2.get(10, TimeUnit.MILLISECONDS);
      fail("permit 2");
    } catch (TimeoutException e) {
      assertTrue(true);
    }
    try {
      waitPermit3.get(10, TimeUnit.MILLISECONDS);
      fail("permit 3");
    } catch (TimeoutException e) {
      assertTrue(true);
    }

    assertThat(mutex.getNumPermitsAvailable(), is(0));
    assertThat(mutex.getNumWaiters(), is(2));

    permit1.release();

    assertThat(mutex.getNumPermitsAvailable(), is(0));
    assertThat(mutex.getNumWaiters(), is(1));

    Permit permitEither = getEither(waitPermit2, waitPermit3);

    assertThat(mutex.getNumPermitsAvailable(), is(0));
    assertThat(mutex.getNumWaiters(), is(1));

    permitEither.release();

    assertThat(mutex.getNumPermitsAvailable(), is(0));
    assertThat(mutex.getNumWaiters(), is(0));
  }
Esempio n. 23
0
 /**
  * Can be used in test.
  *
  * @throws InterruptedException
  * @throws ExecutionException
  */
 public void waitForBufferReady() throws InterruptedException, ExecutionException {
   if (currentBufferProvider != null) {
     currentBufferProvider.get();
   }
   if (bussyBufferProvider != null) {
     bussyBufferProvider.get();
   }
 }
  @Override
  public BxDocument segmentDocument(BxDocument document) throws AnalysisException {
    Map<BxPage, List<Component>> componentMap = new HashMap<BxPage, List<Component>>();

    ExecutorService exec = Executors.newFixedThreadPool(PdfNLMContentExtractor.THREADS_NUMBER);
    ArrayList<Callable<NumBxPage>> tasks = new ArrayList<Callable<NumBxPage>>();
    for (BxPage page : document.getPages()) {
      tasks.add(new ComponentCounter(page));
    }

    List<Future<NumBxPage>> results;
    try {
      results = exec.invokeAll(tasks);
      exec.shutdown();

      for (Future<NumBxPage> result : results) {
        NumBxPage p = result.get();
        componentMap.put(p.page, p.components);
      }
    } catch (ExecutionException ex) {
      throw new AnalysisException("Cannot segment pages!", ex);
    } catch (InterruptedException ex) {
      throw new AnalysisException("Cannot segment pages!", ex);
    }

    this.computeDocumentOrientation(componentMap);

    BxDocument output = new BxDocument();
    BxPage[] pages = new BxPage[document.getPages().size()];

    exec = Executors.newFixedThreadPool(PdfNLMContentExtractor.THREADS_NUMBER);
    tasks = new ArrayList<Callable<NumBxPage>>();
    int i = 0;
    for (BxPage page : document.getPages()) {
      tasks.add(new SingleSegmenter(page, i++));
    }

    try {
      results = exec.invokeAll(tasks);
      exec.shutdown();

      for (Future<NumBxPage> result : results) {
        NumBxPage p = result.get();
        pages[p.index] = p.page;
      }
      for (BxPage p : pages) {
        if (p.getBounds() != null) {
          output.addPage(p);
        }
      }
      return output;
    } catch (ExecutionException ex) {
      throw new AnalysisException("Cannot segment pages!", ex);
    } catch (InterruptedException ex) {
      throw new AnalysisException("Cannot segment pages!", ex);
    }
  }
Esempio n. 25
0
  /**
   * Invokes this method with the provided arguments
   *
   * @param args @return @throws WiseException If an unknown exception is received
   * @throws WiseWebServiceException There are 4 known failure conditions - The wsdl (url) could not
   *     be found - The wsdl is password protected - The endpoint (url) could not be found - The
   *     endpoint is password protected
   * @throws InvocationException
   * @throws IllegalArgumentException
   */
  InvocationResultImpl invoke(Map<String, Object> args)
      throws WiseWebServiceException, InvocationException, IllegalArgumentException {
    InvocationResultImpl result = null;
    Map<String, Object> emptyHolder = Collections.emptyMap();

    try {
      EndpointMethodCaller caller =
          new EndpointMethodCaller(
              this.getEndpoint(), this.getMethod(), this.getParametersInRightPositionArray(args));
      Future<Object> invocation = ((WSEndpointImpl) this.getEndpoint()).getService().submit(caller);
      if (isOneWay()) {
        invocation.get();
        result = new InvocationResultImpl(null, null, null, emptyHolder);
      } else {
        result =
            new InvocationResultImpl(
                RESULT, method.getGenericReturnType(), invocation.get(), getHoldersResult(args));
      }
    } catch (java.util.concurrent.ExecutionException wse) {
      Throwable ite = wse.getCause();
      if (ite != null && ite != wse && ite instanceof InvocationTargetException) {
        Throwable t = ite.getCause();

        // unchecked exception ?
        if (t != null && t != ite && t != wse && t instanceof WebServiceException) {
          // authentication exception ?
          if (isAuthenticationException(t, new HashSet<Throwable>())) {
            throw new WiseWebServiceException(
                "Authentication exception", null); // TODO improve this
          }
          throw new WiseWebServiceException(t.getMessage(), t);
        }

        // checked exception ?
        if (t != null && t != ite && t != wse && t instanceof Exception) {
          Method methodPointer = this.getMethod();
          if (methodPointer != null && methodPointer.getExceptionTypes() != null) {
            for (int i = 0; i < methodPointer.getExceptionTypes().length; i++) {
              Class<?> excType = methodPointer.getExceptionTypes()[i];
              if (t.getClass().isAssignableFrom(excType)) {
                // checked exception
                result = new InvocationResultImpl("exception", excType, t, emptyHolder);
                return result;
              }
            }
          }
        }

        throw new InvocationException("Unknown exception received: " + ite.getMessage(), ite);
      }
      throw new WiseWebServiceException(wse.getMessage(), wse);
    } catch (Throwable e) {
      throw new InvocationException("Generic Error during method invocation!", e);
    }
    return result;
  }
Esempio n. 26
0
  /** Tests the use of two thread-local tables with same var names and different values */
  @Test
  public void testSetLocalValues() throws Exception {
    System.out.println("   setLocalValues");

    final String key1 = "foo";
    final String key2 = "bar";

    final Map<String, Object> table0 = new HashMap<String, Object>();
    table0.put(key1, 1);
    table0.put(key2, 2);

    final Map<String, Object> table1 = new HashMap<String, Object>();
    table1.put(key1, 10);
    table1.put(key2, 20);

    final List<Map<String, Object>> tables = new ArrayList<Map<String, Object>>();
    tables.add(table0);
    tables.add(table1);

    final CountDownLatch latch = new CountDownLatch(2);

    class Task implements Runnable {
      private final int threadIndex;

      public Task(int threadIndex) {
        this.threadIndex = threadIndex;
      }

      public void run() {
        // set the local values for this thread and then wait for
        // the other thread to do the same before testing
        EnvFunction.setLocalValues(tables.get(threadIndex));
        latch.countDown();
        try {
          latch.await();
        } catch (InterruptedException ex) {
          throw new IllegalStateException(ex);
        }

        Map<String, Object> table = tables.get(threadIndex);
        for (String name : table.keySet()) {
          Object result = ff.function("env", ff.literal(name)).evaluate(null);
          int value = ((Number) result).intValue();
          assertEquals(table.get(name), value);
        }
      }
    }

    Future f1 = executor.submit(new Task(0));
    Future f2 = executor.submit(new Task(1));

    // calling get on the Futures ensures that this test method
    // completes before another starts
    f1.get();
    f2.get();
  }
  @Test
  public void 用途_ワークキュー() throws Exception {
    o.l1("【どういうこと?】").e();
    o.l2("キューイングされたタスクを、順にデキューして、順に別のスレッドで実行。").e();

    o.l1("【どうすれば?】").e();
    o.l2("java.util.concurrent.Executors.newSingleThreadExecutor()ファクトリメソッドを使用。").e();

    o.l1("【たとえば?】").e();
    o.l2("newSingleThreadExecutor()でワークキューを作成。").e();

    o.l3("タスクがRunnableの場合").e();
    NameDecorateRunner runTask1 = new NameDecorateRunner("task1", 2);
    NameDecorateRunner runTask2 = new NameDecorateRunner("task2", 1);

    // executeされたタスクは1つの別スレッドで順々に処理される。
    ExecutorService executor = Executors.newSingleThreadExecutor();

    // 呼び出し順に処理が行われる
    executor.execute(runTask1);
    executor.execute(runTask2);

    // (ゆるやかな)終了指示
    executor.shutdown();

    // エグゼキューターの完了を待つ
    executor.awaitTermination(10 /* timeout */, TimeUnit.SECONDS);

    assertThat(runTask1.name, is("***task1***"));
    assertThat(runTask2.name, is("***task2***"));

    o.l3("タスクがCallableの場合").e();
    NameDecorateCaller callTask1 = new NameDecorateCaller("task1", 2);
    NameDecorateCaller callTask2 = new NameDecorateCaller("task2", 1);

    executor = Executors.newSingleThreadExecutor();

    Future<String> future1 = executor.submit(callTask1);
    Future<String> future2 = executor.submit(callTask2);

    executor.shutdown();

    // 処理の完了を待つ
    String result1 = future1.get();
    String result2 = future2.get();

    assertThat(result1, is("###task1###"));
    assertThat(result2, is("###task2###"));

    {
      /** 【補】invokeAllメソッドとinvokeAnyメソッド */
      // あるタスクの集まりの完了を待つことができるメソッド。
      // invokeAllメソッドは、タスクをすべて実行し、すべての完了を待つ。
      // invokeAnyメソッドは、タスクをすべて実行し、どれか1つでも完了することを待つ。
    }
  }
Esempio n. 28
0
  @Test
  public void multipleThreadsOfCurrentJvmDistinctVersion() throws Exception {

    final PathLocker<SrcVersion> pathLocker = new PathLocker<>();

    final Path dir1 = lockerDirectory.resolve(UUID.randomUUID().toString());
    final SrcVersion srcVersion1 = SrcVersion.parse("1.2.3-SRC-revision-deadbeef");
    final SrcVersion srcVersion2 = SrcVersion.parse("2.3.4-SRC-revision-coffeebabe");

    Future<PathLock> concurrLockFuture = null;
    try (PathLock lock1 = pathLocker.lockDirectory(dir1, srcVersion1)) {
      /* locked for the current thread */

      /*
       * now try to lock for a distinct version from another thread which should fail with a
       * CannotAcquireLockException because we have locked the path above
       */
      try {
        concurrLockFuture = lockConcurrently(pathLocker, dir1, srcVersion2);
        concurrLockFuture.get(1, TimeUnit.SECONDS);
        Assert.fail("CannotAcquireLockException expected");
      } catch (InterruptedException e) {
        throw e;
      } catch (ExecutionException e) {
        Assert.assertTrue(
            "Should throw CannotAcquireLockException",
            CannotAcquireLockException.class.equals(e.getCause().getClass()));
      } catch (TimeoutException e) {
        throw e;
      }
    }

    /* the above concurrent attempt should still fail, even if lock1 has been released in between */
    try {
      concurrLockFuture.get(1, TimeUnit.SECONDS);

      Assert.fail("CannotAcquireLockException expected");
    } catch (ExecutionException e) {
      Assert.assertTrue(
          "Should throw CannotAcquireLockException",
          CannotAcquireLockException.class.equals(e.getCause().getClass()));
    }

    /* but a fresh attempt must succeed */
    try {
      Future<PathLock> concurrLockFuture2 = lockConcurrently(pathLocker, dir1, srcVersion2);
      Assert.assertNotNull(concurrLockFuture2.get(1, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
      throw e;
    } catch (ExecutionException e) {
      throw e;
    } catch (TimeoutException e) {
      throw e;
    }
  }
  /**
   * Algo: - we put the query into the execution pool. - after x ms, if we don't have a result, we
   * add the queries for the secondary replicas - we take the first answer - when done, we cancel
   * what's left. Cancelling means: - removing from the pool if the actual call was not started -
   * interrupting the call if it has started Client side, we need to take into account - a call is
   * not executed immediately after being put into the pool - a call is a thread. Let's not multiply
   * the number of thread by the number of replicas. Server side, if we can cancel when it's still
   * in the handler pool, it's much better, as a call can take some i/o.
   *
   * <p>Globally, the number of retries, timeout and so on still applies, but it's per replica, not
   * global. We continue until all retries are done, or all timeouts are exceeded.
   */
  public synchronized Result call()
      throws DoNotRetryIOException, InterruptedIOException, RetriesExhaustedException {
    boolean isTargetReplicaSpecified = (get.getReplicaId() >= 0);

    RegionLocations rl =
        getRegionLocations(
            true,
            (isTargetReplicaSpecified ? get.getReplicaId() : RegionReplicaUtil.DEFAULT_REPLICA_ID),
            cConnection,
            tableName,
            get.getRow());
    ResultBoundedCompletionService<Result> cs =
        new ResultBoundedCompletionService<Result>(this.rpcRetryingCallerFactory, pool, rl.size());

    if (isTargetReplicaSpecified) {
      addCallsForReplica(cs, rl, get.getReplicaId(), get.getReplicaId());
    } else {
      addCallsForReplica(cs, rl, 0, 0);
      try {
        // wait for the timeout to see whether the primary responds back
        Future<Result> f = cs.poll(timeBeforeReplicas, TimeUnit.MICROSECONDS); // Yes, microseconds
        if (f != null) {
          return f.get(); // great we got a response
        }
      } catch (ExecutionException e) {
        throwEnrichedException(e, retries);
      } catch (CancellationException e) {
        throw new InterruptedIOException();
      } catch (InterruptedException e) {
        throw new InterruptedIOException();
      }

      // submit call for the all of the secondaries at once
      addCallsForReplica(cs, rl, 1, rl.size() - 1);
    }

    try {
      try {
        Future<Result> f = cs.take();
        return f.get();
      } catch (ExecutionException e) {
        throwEnrichedException(e, retries);
      }
    } catch (CancellationException e) {
      throw new InterruptedIOException();
    } catch (InterruptedException e) {
      throw new InterruptedIOException();
    } finally {
      // We get there because we were interrupted or because one or more of the
      // calls succeeded or failed. In all case, we stop all our tasks.
      cs.cancelAll();
    }

    return null; // unreachable
  }
  @Override
  public void execute() {

    // 以目标数据表的字段信息为主
    srcTableMetaData.setColumnMetaDatas(tgtTableMetaData.getColumnMetaDatas());

    /* 删除目标数据 */
    this.delTgtData();

    /* 同步数据 */
    try {

      // 同步数据
      String sql = tableMetaDataService.getColumnSql(srcTableMetaData, options);
      LOGGER.info("get src Sql: " + sql);
      HiveReader hiveReader =
          new HiveReader(
              srcJdbcTemplate,
              sql,
              srcTableMetaData.getColumnMetaDatas(),
              queue,
              cyclicBarrier,
              isSrcRunning);
      Future<Boolean> hiveReaderFuture =
          threadPool.submit(hiveReader); // 与execute相比,线程执行完成以后可以通过引用获取返回值
      List<Future<Boolean>> postgresWriterFutureList =
          new ArrayList<Future<Boolean>>(); // 消费者是否正常返回标志
      for (int i = 1; i < SyncConstant.THREAD_POOL_SIZE; i++) {
        PostgresWriter submitDataToDatabase =
            new PostgresWriter(
                tgtJdbcTemplate,
                tgtTableMetaData,
                queue,
                cyclicBarrier,
                isSrcRunning,
                isTgtRunning);
        postgresWriterFutureList.add(threadPool.submit(submitDataToDatabase));
      }

      // 判断返回值
      if (!hiveReaderFuture.get()) {
        LOGGER.error("get-data and submit data result is false");
      }
      for (Future<Boolean> submitDataToDatabaseFuture : postgresWriterFutureList) {
        if (!submitDataToDatabaseFuture.get()) {
          LOGGER.error("get-data and submit data result is false");
        }
      }

      LOGGER.info("同步完成");
    } catch (Exception e) {
      LOGGER.error("get-data and submit data error", e);
    }
  }