Java Code Examples for java.util.concurrent.ExecutorService.shutdown()

The following are Jave code examples for showing how to use shutdown() of the java.util.concurrent.ExecutorService class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: think-in-java   File: E32_OrnamentalGarden3.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws Exception
{
    // All must share a single CountDownLatch object:
    CountDownLatch latch = new CountDownLatch(5);
    ExecutorService exec = Executors.newCachedThreadPool();
    for (int i = 0; i < 5; i++)
    {
        exec.execute(new Entrance3(latch, i));
    }
    TimeUnit.SECONDS.sleep(3);
    Entrance3.cancel();
    exec.shutdown();
    latch.await(); // Wait for results
    System.out.println("Total: " + Entrance3.getTotalCount());
    System.out.println("Sum of Entrances: " + Entrance3.sumEntrances());
}
 
Example 2
Project: dubbocloud   File: MultiThreadTest.java   View Source Code Vote up 5 votes
public void testDubboMultiThreadInvoke() throws Exception
{
    Exporter<?> rpcExporter = protocol.export(proxy.getInvoker(new DemoServiceImpl(), DemoService.class, URL.valueOf("dubbo://127.0.0.1:20259/TestService")));
	
	final AtomicInteger counter = new AtomicInteger();
	final DemoService service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:20259/TestService")));
	assertEquals(service.getSize(new String[]{"123", "456", "789"}), 3);

	final StringBuffer sb = new StringBuffer();
	for(int i=0;i<1024*64+32;i++)
		sb.append('A');
	assertEquals(sb.toString(), service.echo(sb.toString()));

	ExecutorService exec = Executors.newFixedThreadPool(10);
	for(int i=0;i<10;i++)
	{
		final int fi = i;
		exec.execute(new Runnable(){
			public void run()
			{
				for(int i=0;i<30;i++)
				{
					System.out.println(fi+":"+counter.getAndIncrement());
					assertEquals(service.echo(sb.toString()), sb.toString());
				}
			}
		});
	}
	exec.shutdown();
	exec.awaitTermination(10, TimeUnit.SECONDS);
	rpcExporter.unexport();
}
 
Example 3
Project: JAddOn   File: ArrayListProcess.java   View Source Code Vote up 5 votes
public final void process(final ArrayList<Object> arraylist, final int threadPoolSize) {
    final ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
    if(arraylist.size() >= threadPoolSize && arraylist.size() >= MINARRAYLISTSIZE) {
        final int steps = arraylist.size() / threadPoolSize;
        for(int i = 0; i < threadPoolSize; i++) {
            final int u = i;
            Runnable run = new Runnable() {
                
                @Override
                public void run() {
                    final int extra = ((u == threadPoolSize - 1) ? (steps * threadPoolSize) - arraylist.size() : 0);
                    for(int z = 0; z < steps + extra; z++) {
                        final int pos = (u * steps + z);
                        arraylist.set(pos, process(arraylist.get(pos)));
                    }
                }
                
            };
            executor.execute(run);
        }
        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.DAYS);
        } catch (Exception ex) {
        }
    } else {
        for(int i = 0; i < arraylist.size(); i++) {
            arraylist.set(i, process(arraylist.get(i)));
        }
    }
}
 
Example 4
Project: fdt   File: FDT.java   View Source Code Vote up 5 votes
private void waitForTask() throws Exception {
    if (!DirectByteBufferPool.initInstance(config.getByteBufferSize(), Config.getMaxTakePollIter())) {
        // this is really wrong ... It cannot be already initialized
        throw new FDTProcolException("The buffer pool cannot be already initialized");
    }

    ExecutorService executor = null;
    ServerSocketChannel ssc = null;
    ServerSocket ss = null;
    Selector sel = null;
    try {
        executor = Utils.getStandardExecService("[ Acceptable ServersThreadPool ] ",
                2,
                10,
                new ArrayBlockingQueue<Runnable>(65500),
                Thread.NORM_PRIORITY - 2);
        ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        ss = ssc.socket();
        ss.bind(new InetSocketAddress(config.getPort()));
        sel = Selector.open();
        ssc.register(sel, SelectionKey.OP_ACCEPT);
        System.out.println("READY");
        Utils.waitAndWork(executor, ss, sel, config);
    } finally {
        logger.log(Level.INFO, "[FDT] [ waitForTask ] main loop FINISHED!");
        // close all the stuff
        Utils.closeIgnoringExceptions(ssc);
        Utils.closeIgnoringExceptions(sel);
        Utils.closeIgnoringExceptions(ss);
        if (executor != null) {
            executor.shutdown();
        }
    }
}
 
Example 5
Project: burstcoin   File: ThreadPool.java   View Source Code Vote up 5 votes
public static void shutdownExecutor(ExecutorService executor) {
    executor.shutdown();
    try {
        executor.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    if (! executor.isTerminated()) {
        Logger.logShutdownMessage("some threads didn't terminate, forcing shutdown");
        executor.shutdownNow();
    }
}
 
Example 6
Project: webBee   File: Executor.java   View Source Code Vote up 5 votes
public static void main(String[] args){
            //ExecutorService 的生命周期包括三种状态:运行、关闭、终止。创建后便进入运行状态,当调用了 shutdown()方法时
            // ,便进入关闭状态,此时意味着 ExecutorService 不再接受新的任务,但它还在执行已经提交了的任务
          ExecutorService executorService = Executors.newCachedThreadPool();
//          ExecutorService executorService = Executors.newFixedThreadPool(5);
            //创建一个单线程化的Executor。
//          ExecutorService executorService = Executors.newSingleThreadExecutor();
            for (int i = 0; i < 5; i++){
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName() + "线程被调用了。");
                    }
                });
                System.out.println("************* a" + i + " *************");
            }
            executorService.shutdown();
        }
 
Example 7
Project: automat   File: BaseService.java   View Source Code Vote up 5 votes
/** 根据Id查询(cls返回类型Class) */
public <K> List<K> getList(List<Long> ids, Class<K> cls) {
	List<K> list = InstanceUtil.newArrayList();
	if (ids != null) {
		for (int i = 0; i < ids.size(); i++) {
			list.add(null);
		}
		ExecutorService executorService = Executors.newFixedThreadPool(10);
		for (int i = 0; i < ids.size(); i++) {
			final int index = i;
			executorService.execute(new Runnable() {
				public void run() {
					T t = queryById(ids.get(index));
					K k = InstanceUtil.to(t, cls);
					list.set(index, k);
				}
			});
		}
		executorService.shutdown();
		try {
			executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
		} catch (InterruptedException e) {
			logger.error("awaitTermination", "", e);
		}
	}
	return list;
}
 
Example 8
Project: minikube-build-tools-for-java   File: CommandExecutor.java   View Source Code Vote up 5 votes
/**
 * Runs the command.
 *
 * @param command the list of command line tokens
 * @return the output of the command as a list of lines
 * @throws GradleException if the command exited with non-zero exit code
 */
public List<String> run(List<String> command) throws IOException, InterruptedException {
  if (logger != null) {
    logger.debug("Running command : " + String.join(" ", command));
  }

  ExecutorService executor = executorServiceFactory.createExecutorService();

  // Builds the command to execute.
  ProcessBuilder processBuilder = processBuilderFactory.createProcessBuilder();
  processBuilder.command(command);
  processBuilder.redirectErrorStream(true);
  if (environment != null) {
    processBuilder.environment().putAll(environment);
  }
  final Process process = processBuilder.start();

  // Runs the command and streams the output.
  List<String> output = new ArrayList<>();
  executor.execute(outputConsumerRunnable(process, output));
  int exitCode = process.waitFor();

  // Shuts down the executor.
  executor.shutdown();

  try {
    executor.awaitTermination(TIMEOUT_SECONDS, TimeUnit.SECONDS);
  } catch (InterruptedException ex) {
    if (logger != null) {
      logger.debug("Task Executor interrupted waiting for output consumer thread");
    }
  }

  // Stops the build if the command fails to do something, we may want to make this configurable.
  if (exitCode != 0) {
    throw new GradleException("command exited with non-zero exit code : " + exitCode);
  }

  return output;
}
 
Example 9
Project: r8   File: ApplicationReader.java   View Source Code Vote up 5 votes
public DexApplication read() throws IOException, ExecutionException {
  ExecutorService executor = Executors.newSingleThreadExecutor();
  try {
    return read(executor);
  } finally {
    executor.shutdown();
  }
}
 
Example 10
Project: think-in-java   File: SimplePriorities.java   View Source Code Vote up 5 votes
public static void main(String[] args)
{
	ExecutorService exec = Executors.newCachedThreadPool();
	for (int i = 0; i < 5; i++)
	{
		exec.execute(new SimplePriorities(Thread.MIN_PRIORITY));
	}
	exec.execute(new SimplePriorities(Thread.MAX_PRIORITY));
	exec.shutdown();
}
 
Example 11
Project: elastic-db-tools-for-java   File: MultiShardQueryE2ETests.java   View Source Code Vote up 5 votes
/**
 * Regression test for VSTS Bug# 3936154 - Execute a command that will result in a failure in a loop - Without the fix (disabling the command
 * behavior)s, the test will not respond and timeout.
 */
@Test
@Category(value = ExcludeFromGatedCheckin.class)
public final void testFailedCommandWithConnectionCloseCmdBehavior() {
    ExecutorService exec = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    try {
        for (int i = 0; i < 100; i++) {
            int index = i;
            exec.submit(() -> {
                try (MultiShardStatement stmt = shardConnection.createCommand()) {
                    stmt.setCommandText("select * from table_does_not_exist");

                    try (MultiShardResultSet sdr = stmt.executeQuery()) {
                        while (sdr.next()) {
                        }
                    }
                }
                catch (Exception ex) {
                    System.out.printf("Encountered exception: %1$s in iteration: %2$s \r\n", ex.toString(), index);
                }
                finally {
                    System.out.printf("Completed execution of iteration: %1$s" + "\r\n", index);
                }
            });
        }
    }
    finally {
        exec.shutdown();
    }
}
 
Example 12
Project: openjdk-jdk10   File: OidTableInit.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Throwable {
    final String[] algorithmNames = {
        "PBKDF2WITHHMACSHA1",
        "PBEWITHMD5ANDDES",
        "DSA",
        "SHA384WITHRSA",
        "RSA",
        "SHA1WITHDSA",
        "SHA512WITHRSA",
        "MD2WITHRSA",
        "PBEWITHSHA1ANDDESEDE",
        "SHA1WITHRSA",
        "DIFFIEHELLMAN",
        "MD5WITHRSA",
        "PBEWITHSHA1ANDRC2_40",
        "SHA256WITHRSA",
    };

    final int THREADS = 2;
    final ExecutorService pool = Executors.newFixedThreadPool(THREADS);
    final CountDownLatch startingGate = new CountDownLatch(THREADS);
    final Runnable r = new Runnable() { public void run() {
        startingGate.countDown();
        do {} while (startingGate.getCount() > 0);
        try {
            for (String algorithmName : algorithmNames)
                AlgorithmId.get(algorithmName);
        } catch (Throwable fail) {
            throw new AssertionError(fail);
        }
    }};
    final ArrayList<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < THREADS; i++)
        futures.add(pool.submit(r));
    pool.shutdown();
    for (Future<?> future : futures) future.get();
}
 
Example 13
Project: JAVA-   File: BaseService.java   View Source Code Vote up 5 votes
/** 根据Id查询(cls返回类型Class) */
public <K> Page<K> getPage(final Page<Long> ids, final Class<K> cls) {
	if (ids != null) {
		Page<K> page = new Page<K>(ids.getCurrent(), ids.getSize());
		page.setTotal(ids.getTotal());
		final List<K> records = InstanceUtil.newArrayList();
		for (int i = 0; i < ids.getRecords().size(); i++) {
			records.add(null);
		}
		int thread = Math.min(maxThread, Math.max(1, records.size() / 2));
		ExecutorService executorService = Executors.newFixedThreadPool(thread);
		for (int i = 0; i < ids.getRecords().size(); i++) {
			final int index = i;
			executorService.execute(new Runnable() {
				public void run() {
					T t = queryById(ids.getRecords().get(index));
					K k = InstanceUtil.to(t, cls);
					records.set(index, k);
				}
			});
		}
		executorService.shutdown();
		try {
			executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
		} catch (InterruptedException e) {
			logger.error("awaitTermination", "", e);
		}
		page.setRecords(records);
		return page;
	}
	return new Page<K>();
}
 
Example 14
Project: smaph   File: TuneModelLibSvm.java   View Source Code Vote up 5 votes
@Override
public void run() {
	ExecutorService execServ = Executors.newFixedThreadPool(THREADS_NUM);
	List<Future<ModelConfigurationResult>> futures = new Vector<>();

	/*for (int cI = 0; cI<steps; cI++)
		for (int gammaI = 0; gammaI<steps ; gammaI++){
			double c = TuneModelLibSvm.computeExpParameter(cMax, cMin, kappaC, cI, steps);
			double gamma = TuneModelLibSvm.computeExpParameter(gammaMax, gammaMin, kappaGamma, gammaI, steps);
			futures.add(execServ.submit(pt.cloneWithGammaC(gamma, c)));
		}*/
	for (int cI = 0; cI < steps; cI++) {
		double c = TuneModelLibSvm.computeExpParameter(cMax, cMin, kappaC, cI, steps);
		futures.add(execServ.submit(pt.cloneWithGammaC(origGamma, c)));
	}
	for (int gammaI = 0; gammaI < steps; gammaI++) {
		double gamma = TuneModelLibSvm.computeExpParameter(gammaMax, gammaMin, kappaGamma, gammaI, steps);
		futures.add(execServ.submit(pt.cloneWithGammaC(gamma, origC)));
	}
	futures.add(execServ.submit(pt.cloneWithGammaC(origGamma, origC)));
	for (Future<ModelConfigurationResult> future : futures)
		try {
			ModelConfigurationResult res = future.get();
			scoreBoard.add(res);
		} catch (InterruptedException | ExecutionException | Error e) {
			throw new RuntimeException(e);
		}
	execServ.shutdown();
}
 
Example 15
Project: dataflow   File: PriorityTaskQueueTest.java   View Source Code Vote up 5 votes
@Test
public void testMovingBackToHigherPriority() {
    final int parallelTasks = 10;
    PriorityTaskQueue pq = new PriorityTaskQueue(parallelTasks, () -> null, s -> {
    });

    AtomicBoolean test = new AtomicBoolean(false);
    AtomicInteger errors = new AtomicInteger();
    AtomicInteger doneCounter = new AtomicInteger();
    pq.addTask(10, q1 -> {
        q1.addTask(9, q2 -> {
            q2.addTask(7, q3 -> {
                if (!test.compareAndSet(false, true)) {
                    errors.incrementAndGet();
                }
                doneCounter.incrementAndGet();
            });
            q2.addTask(8, q3 -> {
                if (!test.compareAndSet(true, false)) {
                    errors.incrementAndGet();
                }
                doneCounter.incrementAndGet();
            });
            doneCounter.incrementAndGet();
        });
        doneCounter.incrementAndGet();
    });

    ExecutorService executorService = Executors.newCachedThreadPool();
    Queue<Exception> exceptions = new LinkedList<>();
    assertThat(
        pq.executeTasksAndAwaitDone(executorService, exceptions::offer, 1, TimeUnit.SECONDS),
        is(true));
    assertThat(exceptions.size(), is(0));
    assertThat(errors.get(), is(0));
    assertThat(doneCounter.get(), is(4));
    executorService.shutdown();
}
 
Example 16
Project: PEF   File: MultiThreadedPCAPNGPseudonymizer.java   View Source Code Vote up 4 votes
@Override
public File pseudonymize(final File inFile, final File outFile) throws IOException {
    try (final InputStreamByteStream input = new InputStreamByteStream(new FileInputStream(inFile));
         final OutputStream output = new BufferedOutputStream(new FileOutputStream(outFile), BUFFER_SIZE)) {

        long offset = 0;
        Encoding encoding = null;

        final ExecutorService pool = Executors.newFixedThreadPool(_framePseudonimyzers.size());
        final List<Future<byte[]>> futures = new ArrayList<>();

        while (true) {
            ParseResult result = Util.parse(input, offset, PCAPNG.SECTION_HEADER_CHO);

            if (result.succeeded) {
                encoding = getSectionEncoding(result);
            }
            else {
                result = Util.parse(input, offset, PCAPNG.BLOCK, encoding);
            }

            if (!result.succeeded) {
                break;
            }

            futures.add(pool.submit(new BlockHandlerTask(result, _framePseudonimyzers)));

            offset = result.environment.offset;
        }

        pool.shutdown();
        for (final Future<byte[]> future : futures) {
            output.write(future.get());
        }
        pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
    }
    catch (final InterruptedException | ExecutionException e) {
        // TODO PEF-61 add logging
        throw new IllegalStateException(e);
    }

    return outFile;
}
 
Example 17
Project: googles-monorepo-demo   File: AtomicLongMapTest.java   View Source Code Vote up 4 votes
@GwtIncompatible // threads
public void testModify_basher() throws InterruptedException {
  int nTasks = 3000;
  int nThreads = 100;
  final int getsPerTask = 1000;
  final int deltaRange = 10000;
  final String key = "key";

  final AtomicLong sum = new AtomicLong();
  final AtomicLongMap<String> map = AtomicLongMap.create();

  ExecutorService threadPool = Executors.newFixedThreadPool(nThreads);
  for (int i = 0; i < nTasks; i++) {
    threadPool.submit(new Runnable() {
      @Override public void run() {
        int threadSum = 0;
        for (int j = 0; j < getsPerTask; j++) {
          long delta = random.nextInt(deltaRange);
          int behavior = random.nextInt(10);
          switch (behavior) {
            case 0:
              map.incrementAndGet(key);
              threadSum++;
              break;
            case 1:
              map.decrementAndGet(key);
              threadSum--;
              break;
            case 2:
              map.addAndGet(key, delta);
              threadSum += delta;
              break;
            case 3:
              map.getAndIncrement(key);
              threadSum++;
              break;
            case 4:
              map.getAndDecrement(key);
              threadSum--;
              break;
            case 5:
              map.getAndAdd(key, delta);
              threadSum += delta;
              break;
            case 6:
              long oldValue = map.put(key, delta);
              threadSum += delta - oldValue;
              break;
            case 7:
              oldValue = map.get(key);
              if (map.replace(key, oldValue, delta)) {
                threadSum += delta - oldValue;
              }
              break;
            case 8:
              oldValue = map.remove(key);
              threadSum -= oldValue;
              break;
            case 9:
              oldValue = map.get(key);
              if (map.remove(key, oldValue)) {
                threadSum -= oldValue;
              }
              break;
            default:
              throw new AssertionError();
          }
        }
        sum.addAndGet(threadSum);
      }
    });
  }

  threadPool.shutdown();
  assertTrue(threadPool.awaitTermination(300, TimeUnit.SECONDS));

  assertEquals(sum.get(), map.get(key));
}
 
Example 18
Project: ts-benchmark   File: StressAppend.java   View Source Code Vote up 4 votes
public static void startStressAppend(){
		
		//1,生成dn个 设备(每个设备500个传感器)的7min的数据
		//dn=threads*k k>0;
		
		
//		int sn=500;
//		initSensorCodes(sn);
//		initSensorFunction();
		int seq=1;
		long start=System.currentTimeMillis();
		while(seq*THREAD_NUMBER<10000){
			DBBase dbBase= Constants.getDBBase();
//			dbBase.deletePoints(new Date());//删除已有数据
			long start1=System.currentTimeMillis();
			//生成数据
//			Map<Integer,List<List<TsPoint>>> dataMap= generate7MinData(seq,THREAD_NUMBER);
			final Map<Integer,TsPoint[][]> dataMap= generate7MinData(seq,THREAD_NUMBER);//用数组存储效率虽然占用内存多一些,但是运行效率高些,空间换时间
			long end1=System.currentTimeMillis();
			System.out.println("device number="+seq*THREAD_NUMBER+",generate data costTime:["+(end1-start1)+" ms]");
			int startTime=0;
//			int sumThreadPoints=0;
			long avgPointsRatio=0;
			long avgTimeout=0;
			while(startTime<420){
				SUM_THREAD_POINTS=0;
				IMPORT_COST_TIME=0;
				TsPoint[][] tsPoints = dataMap.get(startTime);
				//开辟线程写入数据
				ExecutorService pool = Executors.newFixedThreadPool(THREAD_NUMBER);
				long startTimeLoad=System.currentTimeMillis();
				for(int threadIndex=0;threadIndex<THREAD_NUMBER;threadIndex++){
					final List<TsPoint> points = new ArrayList<TsPoint>(Arrays.asList(tsPoints[threadIndex]));
//					SUM_THREAD_POINTS+=getPointSize(points);
					pool.execute(new Runnable() {
						@Override
						public void run() {
							Status status = dbBase.insertMulti(points);
							if(status.isOK()){
								synchronized (StressAppend.class) {
									long costTime = status.getCostTime();
									IMPORT_COST_TIME+=costTime;
									SUM_THREAD_POINTS+=getPointSize(points);
								}
							}else{
								System.out.println("status failed");
							}
						}
					});
				}
				startTime+=7;
				pool.shutdown();
				try {
					pool.awaitTermination(15, TimeUnit.MINUTES);
					long endTimeLoad=System.currentTimeMillis();
					long avgCostTime=(long) ((endTimeLoad-startTimeLoad)*Math.pow(1000, 2));
					int pointsRatio=(int)(SUM_THREAD_POINTS/(avgCostTime/Math.pow(1000.0, 3)));
					int timeout=(int) (IMPORT_COST_TIME/(double)SUM_THREAD_POINTS/Math.pow(1000, 1));//每个点的延迟时间,单位是us
//					System.out.println("order["+startTime/7+"/"+420/7+"]import["+SUM_THREAD_POINTS+"]points,cost ["+TimeUnit.NANOSECONDS.toMillis(avgCostTime) +" ms],import speed["+pointsRatio+" points/s],timeout["
//							+timeout
//							+" us/point]");
					avgPointsRatio=(avgPointsRatio+pointsRatio)/2;
					avgTimeout=(avgTimeout+timeout)/2;
					Thread.sleep(300L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println("device number="+seq*THREAD_NUMBER+",avg load speed="+avgPointsRatio+" points/s,avg timeout="+avgTimeout+"");
			if(seq<=20){
				seq*=2;//3
			}else{
				seq+=20;//3
			}
		}
		long end=System.currentTimeMillis();
		System.out.println("costTime:"+(end-start));
	}
 
Example 19
Project: hadoop-oss   File: TestRPC.java   View Source Code Vote up 4 votes
/**
 *  Test RPC backoff.
 */
@Test (timeout=30000)
public void testClientBackOff() throws Exception {
  Server server;
  final TestRpcService proxy;

  boolean succeeded = false;
  final int numClients = 2;
  final List<Future<Void>> res = new ArrayList<Future<Void>>();
  final ExecutorService executorService =
      Executors.newFixedThreadPool(numClients);
  conf.setInt(CommonConfigurationKeys.IPC_CLIENT_CONNECT_MAX_RETRIES_KEY, 0);
  conf.setBoolean(CommonConfigurationKeys.IPC_CALLQUEUE_NAMESPACE +
      ".0." + CommonConfigurationKeys.IPC_BACKOFF_ENABLE, true);
  RPC.Builder builder = newServerBuilder(conf)
      .setQueueSizePerHandler(1).setNumHandlers(1).setVerbose(true);
  server = setupTestServer(builder);

  @SuppressWarnings("unchecked")
  CallQueueManager<Call> spy = spy((CallQueueManager<Call>) Whitebox
      .getInternalState(server, "callQueue"));
  Whitebox.setInternalState(server, "callQueue", spy);

  Exception lastException = null;
  proxy = getClient(addr, conf);
  try {
    // start a sleep RPC call to consume the only handler thread.
    // Start another sleep RPC call to make callQueue full.
    // Start another sleep RPC call to make reader thread block on CallQueue.
    for (int i = 0; i < numClients; i++) {
      res.add(executorService.submit(
          new Callable<Void>() {
            @Override
            public Void call() throws ServiceException, InterruptedException {
              proxy.sleep(null, newSleepRequest(100000));
              return null;
            }
          }));
      verify(spy, timeout(500).times(i + 1)).offer(Mockito.<Call>anyObject());
    }
    try {
      proxy.sleep(null, newSleepRequest(100));
    } catch (ServiceException e) {
      RemoteException re = (RemoteException) e.getCause();
      IOException unwrapExeption = re.unwrapRemoteException();
      if (unwrapExeption instanceof RetriableException) {
        succeeded = true;
      } else {
        lastException = unwrapExeption;
      }
    }
  } finally {
    executorService.shutdown();
    stop(server, proxy);
  }
  if (lastException != null) {
    LOG.error("Last received non-RetriableException:", lastException);
  }
  assertTrue("RetriableException not received", succeeded);
}
 
Example 20
Project: ditb   File: TestHBaseFsck.java   View Source Code Vote up 4 votes
/**
 * This test makes sure that with enough retries both parallel instances
 * of hbck will be completed successfully.
 *
 * @throws Exception
 */
@Test (timeout=180000)
public void testParallelWithRetriesHbck() throws Exception {
  final ExecutorService service;
  final Future<HBaseFsck> hbck1,hbck2;

  // With the ExponentialBackoffPolicyWithLimit (starting with 200 milliseconds sleep time, and
  // max sleep time of 5 seconds), we can retry around 15 times within 80 seconds before bail out.
  //
  // Note: the reason to use 80 seconds is that in HADOOP-2.6 and later, the create file would
  // retry up to HdfsConstants.LEASE_SOFTLIMIT_PERIOD (60 seconds).  See HBASE-13574 for more
  // details.
  final int timeoutInSeconds = 80;
  final int sleepIntervalInMilliseconds = 200;
  final int maxSleepTimeInMilliseconds = 6000;
  final int maxRetryAttempts = 15;

  class RunHbck implements Callable<HBaseFsck>{

    @Override
    public HBaseFsck call() throws Exception {
      // Increase retry attempts to make sure the non-active hbck doesn't get starved
      Configuration c = new Configuration(conf);
      c.setInt("hbase.hbck.lockfile.maxwaittime", timeoutInSeconds);
      c.setInt("hbase.hbck.lockfile.attempt.sleep.interval", sleepIntervalInMilliseconds);
      c.setInt("hbase.hbck.lockfile.attempt.maxsleeptime", maxSleepTimeInMilliseconds);
      c.setInt("hbase.hbck.lockfile.attempts", maxRetryAttempts);
      return doFsck(c, false);
    }
  }

  service = Executors.newFixedThreadPool(2);
  hbck1 = service.submit(new RunHbck());
  hbck2 = service.submit(new RunHbck());
  service.shutdown();
  //wait for some time, for both hbck calls finish
  service.awaitTermination(timeoutInSeconds * 2, TimeUnit.SECONDS);
  HBaseFsck h1 = hbck1.get();
  HBaseFsck h2 = hbck2.get();
  // Both should be successful
  assertNotNull(h1);
  assertNotNull(h2);
  assert(h1.getRetCode() >= 0);
  assert(h2.getRetCode() >= 0);

}