Java Code Examples for java.util.concurrent.BlockingQueue#offer()
The following examples show how to use
java.util.concurrent.BlockingQueue#offer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RWQueueRpcExecutor.java From hbase with Apache License 2.0 | 6 votes |
@Override public boolean dispatch(final CallRunner callTask) throws InterruptedException { RpcCall call = callTask.getRpcCall(); int queueIndex; if (isWriteRequest(call.getHeader(), call.getParam())) { queueIndex = writeBalancer.getNextQueue(); } else if (numScanQueues > 0 && isScanRequest(call.getHeader(), call.getParam())) { queueIndex = numWriteQueues + numReadQueues + scanBalancer.getNextQueue(); } else { queueIndex = numWriteQueues + readBalancer.getNextQueue(); } BlockingQueue<CallRunner> queue = queues.get(queueIndex); if (queue.size() >= currentQueueLimit) { return false; } return queue.offer(callTask); }
Example 2
Source File: PushPullBlockingQueueTest.java From disruptor with Apache License 2.0 | 6 votes |
@Test public void testClear() { final int cap = 100; final BlockingQueue<Integer> dbq = new PushPullBlockingQueue<Integer>(cap); for(int i=0; i<cap; i++) { dbq.offer(Integer.valueOf(i)); } Set<Integer> si = new HashSet(cap); for(int i=0; i<cap/10; i++) { si.add(Integer.valueOf(i)); } Assert.assertTrue(dbq.containsAll(si)); dbq.clear(); Assert.assertFalse(dbq.containsAll(si)); Assert.assertEquals(0, dbq.size()); Assert.assertTrue(dbq.isEmpty()); Assert.assertNull(dbq.poll()); }
Example 3
Source File: DisruptorBlockingQueueTest.java From disruptor with Apache License 2.0 | 6 votes |
@Test public void testRemoveObj() { final int cap = 100; final BlockingQueue<Integer> dbq = new DisruptorBlockingQueue<Integer>(cap); for(int i=0; i<cap; i++) { dbq.offer(Integer.valueOf(i)); } for(int i=0; i<cap; i+=2) { dbq.remove(Integer.valueOf(i)); } Assert.assertEquals(dbq.size(), cap/2); for(int i=1; i<cap; i+=2) { Assert.assertEquals(Integer.valueOf(i), dbq.poll()); } }
Example 4
Source File: DisruptorBlockingQueueTest.java From disruptor with Apache License 2.0 | 6 votes |
@Test public void testClear() { final int cap = 100; final BlockingQueue<Integer> dbq = new DisruptorBlockingQueue<Integer>(cap); for(int i=0; i<cap; i++) { dbq.offer(Integer.valueOf(i)); } Set<Integer> si = new HashSet(cap); for(int i=0; i<cap/10; i++) { si.add(Integer.valueOf(i)); } Assert.assertTrue(dbq.containsAll(si)); dbq.clear(); Assert.assertFalse(dbq.containsAll(si)); Assert.assertEquals(0, dbq.size()); Assert.assertTrue(dbq.isEmpty()); Assert.assertNull(dbq.poll()); }
Example 5
Source File: InterruptibleCharSequenceTest.java From webarchive-commons with Apache License 2.0 | 6 votes |
public Thread tryMatchInThread(final CharSequence input, final String regex, final BlockingQueue<Object> atFinish) { Thread t = new Thread() { public void run() { boolean result; try { result = tryMatch(input,regex); } catch (Exception e) { atFinish.offer(e); return; } atFinish.offer(result); } }; t.start(); return t; }
Example 6
Source File: MPMCBlockingQueueTest.java From disruptor with Apache License 2.0 | 6 votes |
@Test public void testPeek() { final int cap = 10; BlockingQueue<Integer> dbq = new MPMCBlockingQueue<>(cap); try { Assert.assertNull(dbq.peek()); } catch(NoSuchElementException nsex) { Assert.fail(); } for(int i=0; i<cap; i++) { dbq.offer(Integer.valueOf(i)); Assert.assertEquals(Integer.valueOf(0), dbq.peek()); } for(int i=0; i<cap; i++) { Assert.assertEquals(Integer.valueOf(i), dbq.peek()); dbq.poll(); // count up values checking peeks } }
Example 7
Source File: FunctionExecutionPooledExecutor.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
public FunctionExecutionPooledExecutor(BlockingQueue<Runnable> q, int maxPoolSize, PoolStatHelper stats, ThreadFactory tf, int msTimeout, final boolean forFnExec) { this(initQ(q), maxPoolSize, stats, tf, msTimeout, initREH(q,forFnExec)); final int retryFor = Integer.getInteger("gemfire.RETRY_INTERVAL", 5000).intValue(); if (!(q instanceof SynchronousQueue)) { this.bufferQueue = q; // create a thread that takes from bufferQueue and puts into result final BlockingQueue<Runnable> takeQueue = q; final BlockingQueue<Runnable> putQueue = getQueue(); Runnable r = new Runnable() { public void run() { try { for (;;) { SystemFailure.checkFailure(); Runnable task = takeQueue.take(); if(forFnExec) { if(!putQueue.offer(task,retryFor , TimeUnit.MILLISECONDS)){ submit(task); } }else { putQueue.put(task); } } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); // this thread is being shutdown so just return; return; } } }; this.bufferConsumer = tf.newThread(r); this.bufferConsumer.start(); } }
Example 8
Source File: ReduceStreamingOperation.java From twister2 with Apache License 2.0 | 5 votes |
@Override public boolean receive(int target, Object object) { TaskMessage msg = new TaskMessage<>(object, inEdge, target); BlockingQueue<IMessage> messages = outMessages.get(target); if (messages != null) { if (messages.offer(msg)) { return true; } } return true; }
Example 9
Source File: FairCallQueue.java From big-c with Apache License 2.0 | 5 votes |
@Override public boolean offer(E e) { int priorityLevel = scheduler.getPriorityLevel(e); BlockingQueue<E> q = this.queues.get(priorityLevel); boolean ret = q.offer(e); signalNotEmpty(); return ret; }
Example 10
Source File: FairCallQueue.java From hadoop with Apache License 2.0 | 5 votes |
/** * Put and offer follow the same pattern: * 1. Get a priorityLevel from the scheduler * 2. Get the nth sub-queue matching this priorityLevel * 3. delegate the call to this sub-queue. * * But differ in how they handle overflow: * - Put will move on to the next queue until it lands on the last queue * - Offer does not attempt other queues on overflow */ @Override public void put(E e) throws InterruptedException { int priorityLevel = scheduler.getPriorityLevel(e); final int numLevels = this.queues.size(); while (true) { BlockingQueue<E> q = this.queues.get(priorityLevel); boolean res = q.offer(e); if (!res) { // Update stats this.overflowedCalls.get(priorityLevel).getAndIncrement(); // If we failed to insert, try again on the next level priorityLevel++; if (priorityLevel == numLevels) { // That was the last one, we will block on put in the last queue // Delete this line to drop the call this.queues.get(priorityLevel-1).put(e); break; } } else { break; } } signalNotEmpty(); }
Example 11
Source File: MPMCBlockingQueueTest.java From disruptor with Apache License 2.0 | 5 votes |
@Test public void textIntMaxValue() { // the blocking queue depends on sequence numbers that are integers // be sure the blocking queue operates normally over // a range spanning integer values if(ALLOW_LONG_RUN) { final int cap = 3; final BlockingQueue<Integer> dbq = new MPMCBlockingQueue<>(cap); long nIter = 0; for(int i=0; i<Integer.MAX_VALUE; i++) { for(int a=0; a<cap; a++) { Assert.assertEquals(dbq.size(), a); dbq.offer(Integer.valueOf(a)); nIter++; } for(int a=0; a<cap; a++) { Assert.assertEquals(dbq.size(), cap-a); Assert.assertEquals("At i="+i, dbq.poll(),Integer.valueOf(a)); } if(nIter % Integer.MAX_VALUE == 0) System.out.println(nIter+"times MAX_VALUE"); } } else { System.out.println("max value test not executed"); } }
Example 12
Source File: KeyedReduceStreamingOperation.java From twister2 with Apache License 2.0 | 5 votes |
@Override public boolean receive(int target, Object object) { TaskMessage msg = new TaskMessage<>(object, inEdge, target); BlockingQueue<IMessage> messages = outMessages.get(target); if (messages != null) { if (messages.offer(msg)) { return true; } } return true; }
Example 13
Source File: Broker.java From flink with Apache License 2.0 | 5 votes |
/** Blocking retrieval and removal of the object to share. */ public V get(String key) { try { BlockingQueue<V> queue = retrieveSharedQueue(key); V objToShare = queue.take(); if (!queue.offer(objToShare)) { throw new RuntimeException("Error: Concurrent modification of the broker slot for key '" + key + "'."); } return objToShare; } catch (InterruptedException e) { throw new RuntimeException(e); } }
Example 14
Source File: PushPullBlockingQueueTest.java From disruptor with Apache License 2.0 | 5 votes |
@Test public void testPushPullBlockingQueueTestC1() { final int cap = 10; BlockingQueue<Integer> dbq = new PushPullBlockingQueue<Integer>(cap); while(dbq.offer(Integer.valueOf(0))); Assert.assertEquals(16, dbq.size()); }
Example 15
Source File: IgnoredReturnValue.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
@ExpectWarning("RV") public static void main(String args[]) throws Exception { String str = " ttesting "; str.trim(); str.toLowerCase(); str.toUpperCase(); str.replace(" ", ""); str.replace(' ', '.'); str.substring(0, 10); str.equals("testing"); Semaphore s = new Semaphore(17, true); s.tryAcquire(); s.tryAcquire(12, TimeUnit.MILLISECONDS); BlockingQueue<Object> q = new LinkedBlockingQueue<Object>(); q.offer(new Object()); q.offer(new Object(), 12, TimeUnit.MILLISECONDS); q.poll(12, TimeUnit.MILLISECONDS); q.poll(); Lock l = new ReentrantLock(); Condition c = l.newCondition(); l.lock(); try { c.awaitNanos(12); c.awaitUntil(new Date()); c.await(12, TimeUnit.NANOSECONDS); } finally { l.unlock(); } q.poll(); }
Example 16
Source File: DirectStreamingOperation.java From twister2 with Apache License 2.0 | 5 votes |
@Override public boolean receive(int target, Object object) { BlockingQueue<IMessage> messages = outMessages.get(target); TaskMessage msg = new TaskMessage<>(object, inEdge, target); return messages.offer(msg); }
Example 17
Source File: SlotManagerTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
/** * Tests that pending request is removed if task executor reports a slot with its allocation id. */ @Test public void testSlotRequestRemovedIfTMReportAllocation() throws Exception { try (final SlotManager slotManager = createSlotManager(ResourceManagerId.generate(), new TestingResourceActionsBuilder().build())) { final JobID jobID = new JobID(); final SlotRequest slotRequest1 = new SlotRequest(jobID, new AllocationID(), ResourceProfile.UNKNOWN, "foobar"); slotManager.registerSlotRequest(slotRequest1); final BlockingQueue<Tuple5<SlotID, JobID, AllocationID, String, ResourceManagerId>> requestSlotQueue = new ArrayBlockingQueue<>(1); final BlockingQueue<CompletableFuture<Acknowledge>> responseQueue = new ArrayBlockingQueue<>(1); final TestingTaskExecutorGateway testingTaskExecutorGateway = new TestingTaskExecutorGatewayBuilder() .setRequestSlotFunction(slotIDJobIDAllocationIDStringResourceManagerIdTuple5 -> { requestSlotQueue.offer(slotIDJobIDAllocationIDStringResourceManagerIdTuple5); try { return responseQueue.take(); } catch (InterruptedException ignored) { return FutureUtils.completedExceptionally(new FlinkException("Response queue was interrupted.")); } }) .createTestingTaskExecutorGateway(); final ResourceID taskExecutorResourceId = ResourceID.generate(); final TaskExecutorConnection taskExecutionConnection = new TaskExecutorConnection(taskExecutorResourceId, testingTaskExecutorGateway); final SlotReport slotReport = new SlotReport(new SlotStatus(new SlotID(taskExecutorResourceId, 0), ResourceProfile.UNKNOWN)); final CompletableFuture<Acknowledge> firstManualSlotRequestResponse = new CompletableFuture<>(); responseQueue.offer(firstManualSlotRequestResponse); slotManager.registerTaskManager(taskExecutionConnection, slotReport); final Tuple5<SlotID, JobID, AllocationID, String, ResourceManagerId> firstRequest = requestSlotQueue.take(); final CompletableFuture<Acknowledge> secondManualSlotRequestResponse = new CompletableFuture<>(); responseQueue.offer(secondManualSlotRequestResponse); final SlotRequest slotRequest2 = new SlotRequest(jobID, new AllocationID(), ResourceProfile.UNKNOWN, "foobar"); slotManager.registerSlotRequest(slotRequest2); // fail first request firstManualSlotRequestResponse.completeExceptionally(new TimeoutException("Test exception to fail first allocation")); final Tuple5<SlotID, JobID, AllocationID, String, ResourceManagerId> secondRequest = requestSlotQueue.take(); // fail second request secondManualSlotRequestResponse.completeExceptionally(new SlotOccupiedException("Test exception", slotRequest1.getAllocationId(), jobID)); assertThat(firstRequest.f2, equalTo(slotRequest1.getAllocationId())); assertThat(secondRequest.f2, equalTo(slotRequest2.getAllocationId())); assertThat(secondRequest.f0, equalTo(firstRequest.f0)); secondManualSlotRequestResponse.complete(Acknowledge.get()); final TaskManagerSlot slot = slotManager.getSlot(secondRequest.f0); assertThat(slot.getState(), equalTo(TaskManagerSlot.State.ALLOCATED)); assertThat(slot.getAllocationId(), equalTo(firstRequest.f2)); assertThat(slotManager.getNumberRegisteredSlots(), is(1)); } }
Example 18
Source File: TaskExecutorTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
/** * Tests that the {@link TaskExecutor} tries to reconnect if the initial slot report * fails. */ @Test public void testInitialSlotReportFailure() throws Exception { final TaskSlotTable taskSlotTable = new TaskSlotTable(Collections.singleton(ResourceProfile.UNKNOWN), timerService); final TaskManagerLocation taskManagerLocation = new LocalTaskManagerLocation(); final TaskManagerServices taskManagerServices = new TaskManagerServicesBuilder() .setTaskSlotTable(taskSlotTable) .setTaskManagerLocation(taskManagerLocation) .build(); final TaskExecutor taskExecutor = createTaskExecutor(taskManagerServices); taskExecutor.start(); try { final TestingResourceManagerGateway testingResourceManagerGateway = new TestingResourceManagerGateway(); final BlockingQueue<CompletableFuture<Acknowledge>> responseQueue = new ArrayBlockingQueue<>(2); testingResourceManagerGateway.setSendSlotReportFunction( resourceIDInstanceIDSlotReportTuple3 -> { try { return responseQueue.take(); } catch (InterruptedException e) { return FutureUtils.completedExceptionally(e); } }); final CompletableFuture<RegistrationResponse> registrationResponse = CompletableFuture.completedFuture( new TaskExecutorRegistrationSuccess( new InstanceID(), testingResourceManagerGateway.getOwnResourceId(), new ClusterInformation("foobar", 1234))); final CountDownLatch numberRegistrations = new CountDownLatch(2); testingResourceManagerGateway.setRegisterTaskExecutorFunction(new Function<Tuple4<String, ResourceID, Integer, HardwareDescription>, CompletableFuture<RegistrationResponse>>() { @Override public CompletableFuture<RegistrationResponse> apply(Tuple4<String, ResourceID, Integer, HardwareDescription> stringResourceIDIntegerHardwareDescriptionTuple4) { numberRegistrations.countDown(); return registrationResponse; } }); responseQueue.offer(FutureUtils.completedExceptionally(new FlinkException("Test exception"))); responseQueue.offer(CompletableFuture.completedFuture(Acknowledge.get())); rpc.registerGateway(testingResourceManagerGateway.getAddress(), testingResourceManagerGateway); resourceManagerLeaderRetriever.notifyListener(testingResourceManagerGateway.getAddress(), testingResourceManagerGateway.getFencingToken().toUUID()); //wait for the second registration attempt numberRegistrations.await(); } finally { RpcUtils.terminateRpcEndpoint(taskExecutor, timeout); } }
Example 19
Source File: Timeline.java From codeu_project_2017 with Apache License 2.0 | 4 votes |
private static <T> void forceAdd(BlockingQueue<T> queue, T value) { while (!queue.offer(value)) { LOG.warning("Failed to add to queue, trying again..."); } }
Example 20
Source File: TaskExecutorTest.java From flink with Apache License 2.0 | 4 votes |
/** * Tests that the {@link TaskExecutor} tries to reconnect if the initial slot report * fails. */ @Test public void testInitialSlotReportFailure() throws Exception { final TaskSlotTable<Task> taskSlotTable = TaskSlotUtils.createTaskSlotTable(1); final UnresolvedTaskManagerLocation unresolvedTaskManagerLocation = new LocalUnresolvedTaskManagerLocation(); final TaskManagerServices taskManagerServices = new TaskManagerServicesBuilder() .setTaskSlotTable(taskSlotTable) .setUnresolvedTaskManagerLocation(unresolvedTaskManagerLocation) .build(); final TaskExecutor taskExecutor = createTaskExecutor(taskManagerServices); taskExecutor.start(); try { final TestingResourceManagerGateway testingResourceManagerGateway = new TestingResourceManagerGateway(); final BlockingQueue<CompletableFuture<Acknowledge>> responseQueue = new ArrayBlockingQueue<>(2); testingResourceManagerGateway.setSendSlotReportFunction( resourceIDInstanceIDSlotReportTuple3 -> { try { return responseQueue.take(); } catch (InterruptedException e) { return FutureUtils.completedExceptionally(e); } }); final CompletableFuture<RegistrationResponse> registrationResponse = CompletableFuture.completedFuture( new TaskExecutorRegistrationSuccess( new InstanceID(), testingResourceManagerGateway.getOwnResourceId(), new ClusterInformation("foobar", 1234))); final CountDownLatch numberRegistrations = new CountDownLatch(2); testingResourceManagerGateway.setRegisterTaskExecutorFunction(taskExecutorRegistration -> { numberRegistrations.countDown(); return registrationResponse; }); responseQueue.offer(FutureUtils.completedExceptionally(new FlinkException("Test exception"))); responseQueue.offer(CompletableFuture.completedFuture(Acknowledge.get())); rpc.registerGateway(testingResourceManagerGateway.getAddress(), testingResourceManagerGateway); resourceManagerLeaderRetriever.notifyListener(testingResourceManagerGateway.getAddress(), testingResourceManagerGateway.getFencingToken().toUUID()); //wait for the second registration attempt numberRegistrations.await(); } finally { RpcUtils.terminateRpcEndpoint(taskExecutor, timeout); } }