Examples of CyclicBarrier


Examples of java.util.concurrent.CyclicBarrier

    server.start();

    int numConcurrentRPC = 200;
    InetSocketAddress addr = NetUtils.getConnectAddress(server);
    final CyclicBarrier barrier = new CyclicBarrier(numConcurrentRPC);
    final CountDownLatch latch = new CountDownLatch(numConcurrentRPC);
    final AtomicBoolean leaderRunning = new AtomicBoolean(true);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>(null);
    Thread leaderThread = null;
   
    for (int i = 0; i < numConcurrentRPC; i++) {
      final int num = i;
      final TestProtocol proxy = (TestProtocol) RPC.getProxy(
      TestProtocol.class, TestProtocol.versionID, addr, conf);
      Thread rpcThread = new Thread(new Runnable() {
        @Override
        public void run() {
          try {
            barrier.await();
            while (num == 0 || leaderRunning.get()) {
              proxy.slowPing(false);
            }
           
            proxy.slowPing(false);
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

    public void parallel() throws Exception {
        JobScheduler instance = create(
                "parallel.default", "1",
                "parallel.para", "2");

        final CyclicBarrier barrier = new CyclicBarrier(3);
        List<Mock> jobs = new ArrayList<Mock>();
        jobs.add(new Mock("a0") {
            @Override
            protected void hook() throws InterruptedException, IOException {
                try {
                    barrier.await();
                } catch (BrokenBarrierException e) {
                    throw new IOException(e);
                }
            }
        }.resource("para"));
        jobs.add(new Mock("a2") {
            @Override
            protected void hook() throws InterruptedException, IOException {
                try {
                    barrier.await();
                } catch (BrokenBarrierException e) {
                    throw new IOException(e);
                }
            }
        }.resource("para"));
        jobs.add(new Mock("a3") {
            @Override
            protected void hook() throws InterruptedException, IOException {
                try {
                    barrier.await();
                } catch (BrokenBarrierException e) {
                    throw new IOException(e);
                }
            }
        }.resource("otherwise"));
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

    }

    protected void setUp() throws Exception {
        SimpleCollector c = new SimpleCollector();
        this.collector = new ConcurrentCollector(c);
        this.barrier = new CyclicBarrier(2);
    }
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

        super(testName);
    }

    protected void setUp() throws Exception {
        this.collector = new ThreadLocalCollector();
        this.barrier = new CyclicBarrier(2);
    }
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

                    String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
                    assert sessionCookie != null;

                    ExecutorService executor = Executors.newCachedThreadPool();
                    int clientsCount = 50;
                    CyclicBarrier barrier = new CyclicBarrier(clientsCount + 1);
                    int requestsCount = 100;
                    Worker[] workers = new Worker[clientsCount];
                    for (int i = 0; i < clientsCount; ++i)
                    {
                        workers[i] = new Worker(barrier, requestsCount, sessionCookie, urls);
                        workers[i].start();
                        executor.execute(workers[i]);
                    }
                    // Wait for all workers to be ready
                    barrier.await();
                    long start = System.nanoTime();

                    // Wait for all workers to be done
                    barrier.await();
                    long end = System.nanoTime();
                    long elapsed = TimeUnit.NANOSECONDS.toMillis(end - start);
                    System.out.println("elapsed ms: " + elapsed);

                    for (Worker worker : workers) worker.stop();
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

                    assert sessionCookie != null;
                    System.out.println("sessionCookie = " + sessionCookie);

                    ExecutorService executor = Executors.newCachedThreadPool();
                    int clientsCount = 50;
                    CyclicBarrier barrier = new CyclicBarrier(clientsCount + 1);
                    int requestsCount = 100;
                    Worker[] workers = new Worker[clientsCount];
                    for (int i = 0; i < clientsCount; ++i)
                    {
                        workers[i] = new Worker(barrier, requestsCount, sessionCookie, urls);
                        workers[i].start();
                        executor.execute(workers[i]);
                    }
                    // Wait for all workers to be ready
                    barrier.await();
                    long start = System.nanoTime();

                    // Wait for all workers to be done
                    barrier.await();
                    long end = System.nanoTime();
                    long elapsed = TimeUnit.NANOSECONDS.toMillis(end - start);
                    System.out.println("elapsed ms: " + elapsed);

                    for (Worker worker : workers) worker.stop();
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

     */
    @Override
    public synchronized void replay(final ReplayListener listener, final ReplayOption... options) {

        // Create a barrier that allows us to wait for synchronous replay
        final CyclicBarrier barrier = new CyclicBarrier(2);

        try {
            InputStream input = null;

            // First check if we have a .zip ...
            if (this.file.getAbsolutePath().endsWith(".xstream")) {
                input = new FileInputStream(this.file);
            }

            // ... and check if we have .xstream file
            if (this.file.getAbsolutePath().endsWith(".zip")) {
                this.loader = new ZIPLoader(this.file);
                input = this.loader.getSessionInputStream();
            }

            // FIXED: #26
            this.in = this.xstream.createObjectInputStream(new BufferedReader(new InputStreamReader(input, "UTF-8")));

        } catch (final FileNotFoundException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        }

        // Sanity check
        if (this.in == null) {
            this.logger.warning("Unable to load replay for file " + this.file);
            return;
        }

        // Options
        final AtomicBoolean gettingMetaInfo = new AtomicBoolean(false);
        final AtomicBoolean realtimeReplay = new AtomicBoolean(false);
        final AtomicBoolean loadImages = new AtomicBoolean(false);

        // Some variables
        final AtomicLong slowdownFactor = new AtomicLong(1);
        final AtomicLong currentEvenTime = new AtomicLong();
        final AtomicLong firstEventTime = new AtomicLong();
        final AtomicLong realtimeDuration = new AtomicLong();

        // Process options
        final OptionUtils<ReplayOption> ou = new OptionUtils<ReplayOption>(options);
        if (ou.contains(OptionGetMetaInfo.class)) gettingMetaInfo.set(true);
        if (ou.contains(OptionRealtime.class)) realtimeReplay.set(true);
        if (ou.contains(OptionLoadImages.class)) loadImages.set(true);
        if (ou.contains(OptionSlowMotion.class)) {
            realtimeReplay.set(true);
            slowdownFactor.set(ou.get(OptionSlowMotion.class).getFactor());
        }

        // Create the actual replay thread
        final Thread t = new Thread(new Runnable() {

            private boolean hasMore = true;

            @Override
            public void run() {
                try {
                    // Lock until we finished
                    SessionReplayImpl.this.finishedLock.lock();

                    // Synchronize with exit of the function
                    try {
                        barrier.await();
                    } catch (final InterruptedException e) {
                        e.printStackTrace();
                    } catch (final BrokenBarrierException e) {
                        e.printStackTrace();
                    }

                    AbstractSessionEvent previousEvent = null;

                    // As long as we have more events
                    while (this.hasMore) {
                        AbstractSessionEvent event = null;

                        try {
                            // Load the next event
                            event = (AbstractSessionEvent) loadFromStream(SessionReplayImpl.this.in);

                            // In case we have no previous event, save the first event time
                            if (previousEvent == null) {
                                firstEventTime.set(event.originalEventTime);
                                previousEvent = event;
                            }

                            // Store current event time
                            currentEvenTime.set(event.originalEventTime);

                            // Dont process if filtered
                            if (SessionReplayImpl.this.toFilter.contains(event.getClass())) {
                                continue;
                            }

                            // Check if we only get meta events ...
                            if (gettingMetaInfo.get()) {
                                if (event instanceof ScreenSizeEvent)
                                    SessionReplayImpl.this.screenSize = ((ScreenSizeEvent) event).screenSize;

                                if (event instanceof PropertyEvent) {
                                    SessionReplayImpl.this.propertyMap.put(((PropertyEvent) event).key, ((PropertyEvent) event).value);
                                }

                                continue;
                            }

                            // Can be switched off, to make replay as fast as possible.
                            if (realtimeReplay.get()) {
                                long delta = event.originalEventTime - previousEvent.originalEventTime;

                                // TODO: When does this happen?
                                if (delta < 0) {
                                    SessionReplayImpl.this.logger.fine("Event times are mixed up " + event.originalEventTime + " < " + previousEvent.originalEventTime);
                                    delta = 0;
                                }

                                // And now wait for the given time
                                try {
                                    Thread.sleep(delta * slowdownFactor.get());
                                } catch (final InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }

                            // Check what kind of event it is and if we have some special rules
                            if (event instanceof ImageEvent && loadImages.get()) {
                                final ImageEvent e = (ImageEvent) event;
                                final InputStream is = SessionReplayImpl.this.loader.getFile(e.associatedFilename);
                                final BufferedImage read = ImageIO.read(is);

                                event = new PseudoImageEvent(e, read);
                            }

                            // Now we are permitted to fire the event.
                            try {
                                listener.nextEvent(event);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            previousEvent = event;
                        } catch (EOFException e) {
                            e.printStackTrace();
                            this.hasMore = false;
                            if (gettingMetaInfo.get()) {
                                realtimeDuration.set(currentEvenTime.get() - firstEventTime.get());
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                } finally {
                    SessionReplayImpl.this.finishedLock.unlock();

                    try {
                        barrier.await();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

        t.setDaemon(true);
        t.start();

        // Synchronize with starting of thread.
        try {
            barrier.await();
        } catch (final InterruptedException e) {
            e.printStackTrace();
        } catch (final BrokenBarrierException e) {
            e.printStackTrace();
        }
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

     */
    @Override
    public void replay(final ReplayListener listener, final ReplayOption... options) {

        final List<AbstractSessionEvent> allEvents = this.record.getAllEvents();
        final CyclicBarrier barrier = new CyclicBarrier(2);

        final OptionUtils<ReplayOption> ou = new OptionUtils<ReplayOption>(options);
        if (ou.contains(OptionGetMetaInfo.class)) setGettingMetaInfo(true);
        if (ou.contains(OptionRealtime.class)) setRealtimeReplay(true);
        if (ou.contains(OptionSlowMotion.class)) {
            setRealtimeReplay(true);
            setSlowdown(ou.get(OptionSlowMotion.class).getFactor());
        }

        // Create replay thread
        final Thread t = new Thread(new Runnable() {

            @Override
            public void run() {

                AbstractSessionEvent previousEvent = null;

                while (allEvents.size() > 0) {
                    AbstractSessionEvent event = null;

                    event = allEvents.get(0);
                    allEvents.remove(0);

                    if (previousEvent == null) {
                        SessionReplayImpl.this.firstEventtime = event.originalEventTime;
                        previousEvent = event;
                    }
                    SessionReplayImpl.this.currentEventime = event.originalEventTime;

                    // Dont process if filtered
                    if (!SessionReplayImpl.this.toFilter.contains(event.getClass())) {
                        if (!isGettingMetaInfo()) {
                            listener.nextEvent(event);

                            // Can be switched off, to make replay as fast as possible.
                            if (SessionReplayImpl.this.realtimeReplay && !isGettingMetaInfo()) {
                                // Sleep till next event
                                long delta = event.originalEventTime - previousEvent.originalEventTime;
                                if (delta <= 0) {
                                    delta = 0;
                                }

                                try {
                                    Thread.sleep(delta * SessionReplayImpl.this.slowdownFactor);
                                } catch (final InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }

                            previousEvent = event;
                        } else {
                            if (event instanceof ScreenSizeEvent) {
                                SessionReplayImpl.this.screenSize = ((ScreenSizeEvent) event).screenSize;
                                //                                                                                System.out.println(event);
                            }
                            if (event instanceof PropertyEvent) {
                                SessionReplayImpl.this.propertyMap.put(((PropertyEvent) event).key, ((PropertyEvent) event).value);
                                //                                                                                System.out.println(event);
                            }
                        }
                    }
                }

                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        t.setDaemon(true);
        t.start();

        // Synchronize with starting of thread.
        try {
            barrier.await();
        } catch (final InterruptedException e) {
            e.printStackTrace();
        } catch (final BrokenBarrierException e) {
            e.printStackTrace();
        }
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

    Configuration conf = new Configuration();
    conf.set(MRJobConfig.MR_AM_STAGING_DIR, stagingDir);
    AsyncDispatcher dispatcher = new AsyncDispatcher();
    dispatcher.init(conf);
    dispatcher.start();
    CyclicBarrier syncBarrier = new CyclicBarrier(2);
    OutputCommitter committer = new TestingOutputCommitter(syncBarrier, false);
    CommitterEventHandler commitHandler =
        createCommitterEventHandler(dispatcher, committer);
    commitHandler.init(conf);
    commitHandler.start();

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

    // let the committer fail and verify the job fails
    syncBarrier.await();
    assertJobState(job, JobStateInternal.FAILED);
    dispatcher.stop();
    commitHandler.stop();
  }
View Full Code Here

Examples of java.util.concurrent.CyclicBarrier

    Configuration conf = new Configuration();
    conf.set(MRJobConfig.MR_AM_STAGING_DIR, stagingDir);
    AsyncDispatcher dispatcher = new AsyncDispatcher();
    dispatcher.init(conf);
    dispatcher.start();
    CyclicBarrier syncBarrier = new CyclicBarrier(2);
    OutputCommitter committer = new TestingOutputCommitter(syncBarrier, true);
    CommitterEventHandler commitHandler =
        createCommitterEventHandler(dispatcher, committer);
    commitHandler.init(conf);
    commitHandler.start();

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

    // let the committer complete and verify the job succeeds
    syncBarrier.await();
    assertJobState(job, JobStateInternal.SUCCEEDED);
   
    job.handle(new JobEvent(job.getID(),
        JobEventType.JOB_TASK_ATTEMPT_COMPLETED));
    assertJobState(job, JobStateInternal.SUCCEEDED);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.