/** * Close this Topic * * @return true if closed */ @Override public boolean close() { this.distributor.getSubscribers() .forEach(it -> it.close()); return true; }
public void close() { counter.completable = true; if (queue != null && counter.active.get() == 0) { if(counter.closing.compareAndSet(false,true)) { counter.closed = true; queue.addContinuation(new Continuation( () -> { throw new ClosedQueueException(); })); queue.close(); } } }
@Override public void onComplete() { counter.active.decrementAndGet(); counter.subscription.remove(subscription); if (queue != null && counter.active.get() == 0) { if (counter.completable) { if(counter.closing.compareAndSet(false,true)) { counter.closed = true; queue.addContinuation(new Continuation( () -> { final List current = new ArrayList(); while (queue.size() > 0) { try { current.add(queue.get()); }catch(ClosedQueueException e){ break; } } throw new ClosedQueueException( current); })); queue.close(); } } } }
@Override public Stream<T> unwrapStream() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); AtomicBoolean wip = new AtomicBoolean(false); Continuation cont = new Continuation(() -> { if (wip.compareAndSet(false, true)) { this.source.subscribeAll(queue::offer, i -> { queue.close(); }, () -> queue.close()); } return Continuation.empty(); }); queue.addContinuation(cont); return queue.stream(); } return StreamSupport.stream(new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).spliterator(), false); }
default <R> R foldParallel(Function<? super Stream<T>,? extends R> fn){ Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build().withTimeout(1); AtomicReference<Continuation> ref = new AtomicReference<>(null); Continuation cont = new Continuation(()->{ if(ref.get()==null && ref.compareAndSet(null,Continuation.empty())){ try { //use the first consuming thread to tell this Stream onto the Queue this.spliterator().forEachRemaining(queue::offer); }finally { queue.close(); } } return Continuation.empty(); }); ; queue.addContinuation(cont); return fn.apply(queue.jdkStream().parallel()); } default <R> R foldParallel(ForkJoinPool fj,Function<? super Stream<T>,? extends R> fn){
@Override public void handleContinuation() { continuation = ReactiveSeq.fromIterable(continuation) .<Optional<Continuation>> map(c -> { try { return Optional.of(c.proceed()); } catch (final Queue.ClosedQueueException e) { return Optional.empty(); } }) .filter(Optional::isPresent) .map(Optional::get) .toList(); if (continuation.size() == 0) { queue.close(); throw new Queue.ClosedQueueException(); } }
@Override public Iterator<T> iterator() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); AtomicBoolean wip = new AtomicBoolean(false); Subscription[] sub = {null}; Continuation cont = new Continuation(() -> { if (wip.compareAndSet(false, true)) { this.source.subscribeAll(queue::offer, i -> queue.close(), () -> queue.close()); } return Continuation.empty(); }); queue.addContinuation(cont); return queue.stream().iterator(); } return new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).iterator(); }
@Test public void publishToAndMerge(){ com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t= new Thread( ()-> { while(true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing!"); queue.close(); } }); t.start(); assertThat(Spouts.of(1,2,3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(), Matchers.equalTo(Arrays.asList(1,1,2,2,3,3))); } @Test
@Test public void publishTest() { for (int k = 0; k < ITERATIONS; k++) { Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { try { System.out.println("Sleeping!"); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Waking!"); System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); of(1, 2, 3).peek(i -> System.out.println("publishing " + i)) .publishTo(queue) .forEach(System.out::println); assertThat(queue.stream().collect(Collectors.toList()), equalTo(Arrays.asList(1, 2, 3))); t = null; System.gc(); } }
@Test public void publishToAndMerge(){ com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t= new Thread( ()-> { while(true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing!"); queue.close(); } }); t.start(); assertThat(ReactiveSeq.of(1,2,3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(),equalTo(Arrays.asList(1,1,2,2,3,3))); }
@Test public void mergeAdapterTest() { for (int k = 0; k < ITERATIONS; k++) { Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { queue.add(1); queue.add(2); queue.add(3); try { System.out.println("Sleeping!"); Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Waking!"); System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); assertThat(this.<Integer>of().peek(i -> System.out.println("publishing " + i)) .merge(queue).collect(Collectors.toList()), equalTo(Arrays.asList(1, 2, 3))); t = null; System.gc(); } }
@Test public void publishToAndMerge() { for (int k = 0; k < ITERATIONS; k++) { System.out.println("Publish toNested and zip iteration " + k); com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); AtomicBoolean complete = new AtomicBoolean(false); AtomicBoolean start = new AtomicBoolean(false); List<Integer> list = of(1, 2, 3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(); assertThat(list, hasItems(1, 2, 3)); assertThat(list.size(), equalTo(6)); System.gc(); } }
@Test public void mergeAdapterTest1() { for (int k = 0; k < ITERATIONS; k++) { System.out.println("Test iteration " + k); Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { queue.add(1); queue.add(2); queue.add(3); try { // System.out.println("Sleeping!"); Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } // System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); assertThat(this.<Integer>of(10).peek(i -> System.out.println("publishing " + i)) .merge(queue).collect(Collectors.toList()), hasItems(10, 1, 2, 3)); t = null; System.gc(); } }
/** * Close this Topic * * @return true if closed */ @Override public boolean close() { this.distributor.getSubscribers() .forEach(it -> it.close()); return true; }
public void close() { counter.completable = true; if (queue != null && counter.active.get() == 0) { if(counter.closing.compareAndSet(false,true)) { counter.closed = true; queue.addContinuation(new Continuation( () -> { throw new ClosedQueueException(); })); queue.close(); } } }
@Override default <K> void toQueue(final Map<K, Queue<U>> shards, final Function<? super U, ? extends K> sharder) { //in this case all the items have to be pushed to the shards, //we can't rely on the client pulling them all to getValue them in to the right shards final LazyReact service = getPopulator(); then(it -> shards.get(sharder.apply(it)) .offer(it), service.getExecutor()).runThread(() -> { shards.values() .forEach(it -> it.close()); returnPopulator(service); }); }
@Override default void addToQueue(final Queue queue) { thenSync(it -> queue.offer(it)).allOf(it -> queue.close()); }
@Override default Queue<U> toQueue(final Function<Queue, Queue> fn) { final Queue<U> queue = fn.apply(getQueueFactory().build()); final Continuation continuation = thenSync(queue::add).self(s -> { if (this.getPopulator().isPoolingActive()) s.peekSync(v -> { throw new CompletedException( v); }); }).runContinuation(() -> {queue.close();}); queue.addContinuation(continuation); return queue; }