/** Return all of the neighbors with whom we share the provided range. */
  static Set<InetAddress> getNeighbors(String table, Range<Token> toRepair) {
    StorageService ss = StorageService.instance;
    Map<Range<Token>, List<InetAddress>> replicaSets = ss.getRangeToAddressMap(table);
    Range<Token> rangeSuperSet = null;
    for (Range<Token> range : ss.getLocalRanges(table)) {
      if (range.contains(toRepair)) {
        rangeSuperSet = range;
        break;
      } else if (range.intersects(toRepair)) {
        throw new IllegalArgumentException(
            "Requested range intersects a local range but is not fully contained in one; this would lead to imprecise repair");
      }
    }
    if (rangeSuperSet == null || !replicaSets.containsKey(toRepair)) return Collections.emptySet();

    Set<InetAddress> neighbors = new HashSet<InetAddress>(replicaSets.get(rangeSuperSet));
    neighbors.remove(FBUtilities.getBroadcastAddress());
    // Excluding all node with version <= 0.7 since they don't know how to
    // create a correct merkle tree (they build it over the full range)
    Iterator<InetAddress> iter = neighbors.iterator();
    while (iter.hasNext()) {
      InetAddress endpoint = iter.next();
      if (Gossiper.instance.getVersion(endpoint) <= MessagingService.VERSION_07) {
        logger.info(
            "Excluding "
                + endpoint
                + " from repair because it is on version 0.7 or sooner. You should consider updating this node before running repair again.");
        iter.remove();
      }
    }
    return neighbors;
  }
  @Test
  public void putFromMultipleThreads() throws InterruptedException {
    final HazelcastInstance h = Hazelcast.newHazelcastInstance(null);
    final AtomicInteger counter = new AtomicInteger(0);
    class Putter implements Runnable {
      volatile Boolean run = true;

      public void run() {
        HazelcastClient hClient = TestUtility.newHazelcastClient(h);
        while (run) {
          Map<String, String> clientMap = hClient.getMap("putFromMultipleThreads");
          clientMap.put(String.valueOf(counter.incrementAndGet()), String.valueOf(counter.get()));
        }
      }
    };
    List<Putter> list = new ArrayList<Putter>();
    for (int i = 0; i < 10; i++) {
      Putter p = new Putter();
      list.add(p);
      new Thread(p).start();
    }
    Thread.sleep(5000);
    for (Iterator<Putter> it = list.iterator(); it.hasNext(); ) {
      Putter p = it.next();
      p.run = false;
    }
    Thread.sleep(100);
    assertEquals(counter.get(), h.getMap("putFromMultipleThreads").size());
  }
 /** iterator() returns an iterator containing the elements of the list */
 public void testIterator() {
   List full = populatedArray(SIZE);
   Iterator i = full.iterator();
   int j;
   for (j = 0; i.hasNext(); j++) assertEquals(j, ((Integer) i.next()).intValue());
   assertEquals(SIZE, j);
 }
Exemplo n.º 4
0
  public List<TravelQuote> getRankedTravelQuotes(
      TravelInfo travelInfo,
      Set<TravelCompany> companies,
      Comparator<TravelQuote> ranking,
      long time,
      TimeUnit unit)
      throws InterruptedException {
    List<QuoteTask> tasks = new ArrayList<QuoteTask>();
    for (TravelCompany company : companies) tasks.add(new QuoteTask(company, travelInfo));

    List<Future<TravelQuote>> futures = exec.invokeAll(tasks, time, unit);

    List<TravelQuote> quotes = new ArrayList<TravelQuote>(tasks.size());
    Iterator<QuoteTask> taskIter = tasks.iterator();
    for (Future<TravelQuote> f : futures) {
      QuoteTask task = taskIter.next();
      try {
        quotes.add(f.get());
      } catch (ExecutionException e) {
        quotes.add(task.getFailureQuote(e.getCause()));
      } catch (CancellationException e) {
        quotes.add(task.getTimeoutQuote(e));
      }
    }

    Collections.sort(quotes, ranking);
    return quotes;
  }
  /**
   * Waits for renting partitions.
   *
   * @return {@code True} if mapping was changed.
   * @throws IgniteCheckedException If failed.
   */
  private boolean waitForRent() throws IgniteCheckedException {
    boolean changed = false;

    // Synchronously wait for all renting partitions to complete.
    for (Iterator<GridDhtLocalPartition> it = locParts.values().iterator(); it.hasNext(); ) {
      GridDhtLocalPartition p = it.next();

      GridDhtPartitionState state = p.state();

      if (state == RENTING || state == EVICTED) {
        if (log.isDebugEnabled()) log.debug("Waiting for renting partition: " + p);

        // Wait for partition to empty out.
        p.rent(true).get();

        if (log.isDebugEnabled()) log.debug("Finished waiting for renting partition: " + p);

        // Remove evicted partition.
        it.remove();

        changed = true;
      }
    }

    return changed;
  }
Exemplo n.º 6
0
  /**
   * Converts the form field values in the <tt>ffValuesIter</tt> into a caps string.
   *
   * @param ffValuesIter the {@link Iterator} containing the form field values.
   * @param capsBldr a <tt>StringBuilder</tt> to which the caps string representing the form field
   *     values is to be appended
   */
  private static void formFieldValuesToCaps(Iterator<String> ffValuesIter, StringBuilder capsBldr) {
    SortedSet<String> fvs = new TreeSet<String>();

    while (ffValuesIter.hasNext()) fvs.add(ffValuesIter.next());

    for (String fv : fvs) capsBldr.append(fv).append('<');
  }
Exemplo n.º 7
0
  /** Selects on sockets and informs their Communicator when there is something to do. */
  public void communicate(int timeout) {

    try {
      selector.select(timeout);
    } catch (IOException e) {
      // Not really sure why/when this happens yet
      return;
    }

    Iterator<SelectionKey> keys = selector.selectedKeys().iterator();

    while (keys.hasNext()) {
      SelectionKey key = keys.next();
      keys.remove();
      if (!key.isValid()) continue; // WHY
      Communicator communicator = (Communicator) key.attachment();

      if (key.isReadable()) communicator.onReadable();
      if (key.isWritable()) communicator.onWritable();
      if (key.isAcceptable()) communicator.onAcceptable();
    }

    // Go through the queue and handle each communicator
    while (!queue.isEmpty()) {
      Communicator c = queue.poll();
      c.onMemo();
    }
  }
Exemplo n.º 8
0
  /**
   * Remove records telling what entity caps node a contact has.
   *
   * @param contact the contact
   */
  public void removeContactCapsNode(Contact contact) {
    Caps caps = null;
    String lastRemovedJid = null;

    Iterator<String> iter = userCaps.keySet().iterator();
    while (iter.hasNext()) {
      String jid = iter.next();

      if (StringUtils.parseBareAddress(jid).equals(contact.getAddress())) {
        caps = userCaps.get(jid);
        lastRemovedJid = jid;
        iter.remove();
      }
    }

    // fire only for the last one, at the end the event out
    // of the protocol will be one and for the contact
    if (caps != null) {
      UserCapsNodeListener[] listeners;
      synchronized (userCapsNodeListeners) {
        listeners = userCapsNodeListeners.toArray(NO_USER_CAPS_NODE_LISTENERS);
      }
      if (listeners.length != 0) {
        String nodeVer = caps.getNodeVer();

        for (UserCapsNodeListener listener : listeners)
          listener.userCapsNodeRemoved(lastRemovedJid, nodeVer, false);
      }
    }
  }
Exemplo n.º 9
0
 protected void handleIterator(String[] args) {
   Iterator it = null;
   String iteratorStr = args[0];
   if (iteratorStr.startsWith("s.")) {
     it = getSet().iterator();
   } else if (iteratorStr.startsWith("m.")) {
     it = getMap().keySet().iterator();
   } else if (iteratorStr.startsWith("q.")) {
     it = getQueue().iterator();
   } else if (iteratorStr.startsWith("l.")) {
     it = getList().iterator();
   }
   if (it != null) {
     boolean remove = false;
     if (args.length > 1) {
       String removeStr = args[1];
       remove = removeStr.equals("remove");
     }
     int count = 1;
     while (it.hasNext()) {
       print(count++ + " " + it.next());
       if (remove) {
         it.remove();
         print(" removed");
       }
       println("");
     }
   }
 }
Exemplo n.º 10
0
  @Test
  public void testCloseable() throws IOException {
    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
    jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort()));

    JedisCluster jc = null;
    try {
      jc = new JedisCluster(jedisClusterNode);
      jc.set("51", "foo");
    } finally {
      if (jc != null) {
        jc.close();
      }
    }

    Iterator<JedisPool> poolIterator = jc.getClusterNodes().values().iterator();
    while (poolIterator.hasNext()) {
      JedisPool pool = poolIterator.next();
      try {
        pool.getResource();
        fail("JedisCluster's internal pools should be already destroyed");
      } catch (JedisConnectionException e) {
        // ok to go...
      }
    }
  }
  /**
   * Notifies this {@code SimulcastReceiver} that a specific {@code SimulcastReceiver} has detected
   * the start of a new video frame in the RTP stream that it represents. Determines whether any of
   * {@link #simulcastLayers} other than {@code source} have been paused/stopped by the remote peer.
   * The determination is based on counting (video) frames.
   *
   * @param source the {@code SimulcastLayer} which is the source of the event i.e. which has
   *     detected the start of a new video frame in the RTP stream that it represents
   * @param pkt the {@code RawPacket} which was received by {@code source} and possibly influenced
   *     the decision that a new view frame was started in the RTP stream represented by {@code
   *     source}
   * @param layers the set of {@code SimulcastLayer}s managed by this {@code SimulcastReceiver}.
   *     Explicitly provided to the method in order to avoid invocations of {@link
   *     #getSimulcastLayers()} because the latter makes a copy at the time of this writing.
   */
  private void simulcastLayerFrameStarted(
      SimulcastLayer source, RawPacket pkt, SimulcastLayer[] layers) {
    // Allow the value of the constant TIMEOUT_ON_FRAME_COUNT to disable (at
    // compile time) the frame-based approach to the detection of layer
    // drops.
    if (TIMEOUT_ON_FRAME_COUNT <= 1) return;

    // Timeouts in layers caused by source may occur only based on the span
    // (of time or received frames) during which source has received
    // TIMEOUT_ON_FRAME_COUNT number of frames. The current method
    // invocation signals the receipt of 1 frame by source.
    int indexOfLastSourceOccurrenceInHistory = -1;
    int sourceFrameCount = 0;
    int ix = 0;

    for (Iterator<SimulcastLayer> it = simulcastLayerFrameHistory.iterator(); it.hasNext(); ++ix) {
      if (it.next() == source) {
        if (indexOfLastSourceOccurrenceInHistory != -1) {
          // Prune simulcastLayerFrameHistory so that it does not
          // become unnecessarily long.
          it.remove();
        } else if (++sourceFrameCount >= TIMEOUT_ON_FRAME_COUNT - 1) {
          // The span of TIMEOUT_ON_FRAME_COUNT number of frames
          // received by source only is to be examined for the
          // purposes of timeouts. The current method invocations
          // signals the receipt of 1 frame by source so
          // TIMEOUT_ON_FRAME_COUNT - 1 occurrences of source in
          // simulcastLayerFrameHistory is enough.
          indexOfLastSourceOccurrenceInHistory = ix;
        }
      }
    }

    if (indexOfLastSourceOccurrenceInHistory != -1) {
      // Presumably, if a SimulcastLayer is active, all SimulcastLayers
      // before it (according to SimulcastLayer's order) are active as
      // well. Consequently, timeouts may occur in SimulcastLayers which
      // are after source.
      boolean maybeTimeout = false;

      for (SimulcastLayer layer : layers) {
        if (maybeTimeout) {
          // There's no point in timing layer out if it's timed out
          // already.
          if (layer.isStreaming()) {
            maybeTimeout(source, pkt, layer, indexOfLastSourceOccurrenceInHistory);
          }
        } else if (layer == source) {
          maybeTimeout = true;
        }
      }
    }

    // As previously stated, the current method invocation signals the
    // receipt of 1 frame by source.
    simulcastLayerFrameHistory.add(0, source);
    // TODO Prune simulcastLayerFrameHistory by forgetting so that it does
    // not become too long.
  }
 /** iterator.remove removes element */
 public void testIteratorRemove() {
   List full = populatedArray(SIZE);
   Iterator it = full.iterator();
   Object first = full.get(0);
   it.next();
   it.remove();
   assertFalse(full.contains(first));
 }
Exemplo n.º 13
0
 public Map<String, String> getFormatSpecifierDescriptions() {
   Map<String, String> m = new LinkedHashMap<String, String>();
   Iterator<String> keys = scoreBoardValues.keySet().iterator();
   while (keys.hasNext()) {
     String k = keys.next();
     m.put(k, scoreBoardValues.get(k).getDescription());
   }
   return m;
 }
Exemplo n.º 14
0
 // Check that the iterator iterates the correct number of times
 private void checkSizes(String msg, int sz, Iterator it, int expectedSize) {
   assertEquals(msg, expectedSize, sz);
   int result = 0;
   while (it.hasNext()) {
     result++;
     it.next();
   }
   assertEquals(msg, expectedSize, result);
 }
Exemplo n.º 15
0
 synchronized TestParameters getTest() {
   if (failed) {
     return null;
   }
   if (testIterator.hasNext()) {
     return (TestParameters) testIterator.next();
   }
   return null;
 }
Exemplo n.º 16
0
 protected void handleMapKeys() {
   Set set = getMap().keySet();
   Iterator it = set.iterator();
   int count = 0;
   while (it.hasNext()) {
     count++;
     println(it.next());
   }
   println("Total " + count);
 }
Exemplo n.º 17
0
 protected void handleMapValues() {
   Collection set = getMap().values();
   Iterator it = set.iterator();
   int count = 0;
   while (it.hasNext()) {
     count++;
     println(it.next());
   }
   println("Total " + count);
 }
 /** keySet is ordered */
 public void testDescendingKeySetOrder() {
   ConcurrentNavigableMap map = dmap5();
   Set s = map.keySet();
   Iterator i = s.iterator();
   Integer last = (Integer) i.next();
   assertEquals(last, m1);
   while (i.hasNext()) {
     Integer k = (Integer) i.next();
     assertTrue(last.compareTo(k) > 0);
     last = k;
   }
 }
Exemplo n.º 19
0
 /** Stop the executors and clean memory on registered CUObject */
 public static void stop() {
   synchronized (cudaEngines) {
     cuCtxSynchronizeAll();
     for (Iterator<CudaEngine> iterator = cudaEngines.values().iterator(); iterator.hasNext(); ) {
       iterator.next().shutdown();
       iterator.remove();
     }
     //		for (CudaEngine ce : cudaEngines.values()) {
     //			ce.shutdown();
     //		}
   }
 }
Exemplo n.º 20
0
 protected void handleMapEntries() {
   Set set = getMap().entrySet();
   Iterator it = set.iterator();
   int count = 0;
   long time = Clock.currentTimeMillis();
   while (it.hasNext()) {
     count++;
     Map.Entry entry = (Entry) it.next();
     println(entry.getKey() + " : " + entry.getValue());
   }
   println("Total " + count);
 }
Exemplo n.º 21
0
  public void deregisterAllVerbHandlers(EndPoint localEndPoint) {
    Iterator keys = verbHandlers_.keySet().iterator();
    String key = null;

    /*
     * endpoint specific verbhandlers can be distinguished because
     * their key's contain the name of the endpoint.
     */
    while (keys.hasNext()) {
      key = (String) keys.next();
      if (key.contains(localEndPoint.toString())) keys.remove();
    }
  }
Exemplo n.º 22
0
 /*public boolean deleteTick(TickActer E)
 {
 	//TockClient equiv=new TockClient(E, 0);
 	for(Iterator<Tick> e=ticks.iterator();e.hasNext();)
 	{
 		if(e.next().delTicker(E))
 			return true;
 	}
 	return false;
 }*/
 public boolean deleteArea(Tickable E) {
   TickArea almostTock = null;
   Iterator set = null;
   for (Iterator<TickArea> e = areaGroups(); e.hasNext(); ) {
     almostTock = e.next();
     if (almostTock.clientObject == E) {
       delArea(almostTock);
       almostTock.dead = true;
       if (Thread.currentThread() != almostTock) almostTock.shutdown();
       return true;
     }
   }
   return false;
 }
Exemplo n.º 23
0
 public static ConnectionStatistics[] getPoolStatistics() {
   Set<ConnectionStatistics> stats = new HashSet<ConnectionStatistics>();
   Iterator<TcpConnectionManager> it = poolTable_.values().iterator();
   while (it.hasNext()) {
     TcpConnectionManager cp = it.next();
     ConnectionStatistics cs =
         new ConnectionStatistics(
             cp.getLocalEndPoint(),
             cp.getRemoteEndPoint(),
             cp.getPoolSize(),
             cp.getConnectionsInUse());
     stats.add(cs);
   }
   return stats.toArray(new ConnectionStatistics[0]);
 }
 /** entrySet contains all pairs */
 public void testDescendingEntrySet() {
   ConcurrentNavigableMap map = dmap5();
   Set s = map.entrySet();
   assertEquals(5, s.size());
   Iterator it = s.iterator();
   while (it.hasNext()) {
     Map.Entry e = (Map.Entry) it.next();
     assertTrue(
         (e.getKey().equals(m1) && e.getValue().equals("A"))
             || (e.getKey().equals(m2) && e.getValue().equals("B"))
             || (e.getKey().equals(m3) && e.getValue().equals("C"))
             || (e.getKey().equals(m4) && e.getValue().equals("D"))
             || (e.getKey().equals(m5) && e.getValue().equals("E")));
   }
 }
 /** entrySet contains all pairs */
 public void testEntrySet() {
   ConcurrentNavigableMap map = map5();
   Set s = map.entrySet();
   assertEquals(5, s.size());
   Iterator it = s.iterator();
   while (it.hasNext()) {
     Map.Entry e = (Map.Entry) it.next();
     assertTrue(
         (e.getKey().equals(one) && e.getValue().equals("A"))
             || (e.getKey().equals(two) && e.getValue().equals("B"))
             || (e.getKey().equals(three) && e.getValue().equals("C"))
             || (e.getKey().equals(four) && e.getValue().equals("D"))
             || (e.getKey().equals(five) && e.getValue().equals("E")));
   }
 }
Exemplo n.º 26
0
 /*public int numTickGroups()
 {
 	return ticks.size();
 }*/
 public String[][] threadInfo() {
   ArrayList<String[]> V = new ArrayList();
   for (Iterator<CMLibrary> e = CMLib.libraries(); e.hasNext(); ) {
     CMLibrary lib = e.next();
     SupportThread thread = lib.getSupportThread();
     if (thread != null) {
       String[] S = new String[3];
       S[0] = thread.getName();
       S[1] = CMLib.english().returnTime(thread.milliTotal, thread.tickTotal);
       S[2] = thread.status;
       V.add(S);
     }
   }
   return V.toArray(new String[V.size()][]);
 }
  /** {@inheritDoc} */
  @Override
  public void loadCache(GridBiInClosure<K, V> c, @Nullable Object... args) throws GridException {
    ExecutorService exec =
        new ThreadPoolExecutor(
            threadsCnt,
            threadsCnt,
            0L,
            MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(batchQueueSize),
            new BlockingRejectedExecutionHandler());

    Iterator<I> iter = inputIterator(args);

    Collection<I> buf = new ArrayList<>(batchSize);

    try {
      while (iter.hasNext()) {
        if (Thread.currentThread().isInterrupted()) {
          U.warn(log, "Working thread was interrupted while loading data.");

          break;
        }

        buf.add(iter.next());

        if (buf.size() == batchSize) {
          exec.submit(new Worker(c, buf, args));

          buf = new ArrayList<>(batchSize);
        }
      }

      if (!buf.isEmpty()) exec.submit(new Worker(c, buf, args));
    } catch (RejectedExecutionException ignored) {
      // Because of custom RejectedExecutionHandler.
      assert false : "RejectedExecutionException was thrown while it shouldn't.";
    } finally {
      exec.shutdown();

      try {
        exec.awaitTermination(Long.MAX_VALUE, MILLISECONDS);
      } catch (InterruptedException ignored) {
        U.warn(log, "Working thread was interrupted while waiting for put operations to complete.");

        Thread.currentThread().interrupt();
      }
    }
  }
Exemplo n.º 28
0
  public void removeChannel(Channel channel) {
    lock.lock();
    try {
      if (channel.getDirection() == Direction.OUT) {
        BrokerHost registeredHost = null;
        for (Map.Entry<BrokerHost, List<Channel>> e : brokerHostToProducerChannelMap.entrySet()) {
          List<Channel> channels = e.getValue();
          Iterator<Channel> channelIterator = channels.iterator();
          while (channelIterator.hasNext()) {
            Channel c = channelIterator.next();
            if (c.equals(channel)) {
              registeredHost = e.getKey();
              channelIterator.remove();

              // if there are no more channels remove the producer
              if (channels.size() == 0) {
                Manageable producer = producers.remove(registeredHost);
                producer.stop();
              }
              break;
            }
          }
          if (registeredHost != null) {
            break;
          }
        }
      } else if (channel.getDirection() == Direction.IN) {
        BrokerHost registeredHost = null;
        for (Map.Entry<BrokerHost, List<Channel>> e : brokerHostToConsumerChannelMap.entrySet()) {
          List<Channel> channels = e.getValue();
          Iterator<Channel> channelIterator = channels.iterator();
          while (channelIterator.hasNext()) {
            Channel c = channelIterator.next();
            if (c.equals(channel)) {
              registeredHost = e.getKey();
              channelIterator.remove();

              // if there are no more channels remove the producer
              if (channels.size() == 0) {
                ConsumingWorker worker = consumingWorkers.remove(registeredHost);
                worker.stop();

                Manageable consumer = consumers.remove(registeredHost);
                consumer.stop();
              }
              break;
            }
          }
          if (registeredHost != null) {
            break;
          }
        }
      }
    } finally {
      lock.unlock();
    }
  }
 /** subMap returns map with keys in requested range */
 public void testSubMapContents() {
   ConcurrentNavigableMap map = map5();
   SortedMap sm = map.subMap(two, four);
   assertEquals(two, sm.firstKey());
   assertEquals(three, sm.lastKey());
   assertEquals(2, sm.size());
   assertFalse(sm.containsKey(one));
   assertTrue(sm.containsKey(two));
   assertTrue(sm.containsKey(three));
   assertFalse(sm.containsKey(four));
   assertFalse(sm.containsKey(five));
   Iterator i = sm.keySet().iterator();
   Object k;
   k = (Integer) (i.next());
   assertEquals(two, k);
   k = (Integer) (i.next());
   assertEquals(three, k);
   assertFalse(i.hasNext());
   Iterator j = sm.keySet().iterator();
   j.next();
   j.remove();
   assertFalse(map.containsKey(two));
   assertEquals(4, map.size());
   assertEquals(1, sm.size());
   assertEquals(three, sm.firstKey());
   assertEquals(three, sm.lastKey());
   assertEquals("C", sm.remove(three));
   assertTrue(sm.isEmpty());
   assertEquals(3, map.size());
 }
 /** subMap returns map with keys in requested range */
 public void testDescendingSubMapContents() {
   ConcurrentNavigableMap map = dmap5();
   SortedMap sm = map.subMap(m2, m4);
   assertEquals(m2, sm.firstKey());
   assertEquals(m3, sm.lastKey());
   assertEquals(2, sm.size());
   assertFalse(sm.containsKey(m1));
   assertTrue(sm.containsKey(m2));
   assertTrue(sm.containsKey(m3));
   assertFalse(sm.containsKey(m4));
   assertFalse(sm.containsKey(m5));
   Iterator i = sm.keySet().iterator();
   Object k;
   k = (Integer) (i.next());
   assertEquals(m2, k);
   k = (Integer) (i.next());
   assertEquals(m3, k);
   assertFalse(i.hasNext());
   Iterator j = sm.keySet().iterator();
   j.next();
   j.remove();
   assertFalse(map.containsKey(m2));
   assertEquals(4, map.size());
   assertEquals(1, sm.size());
   assertEquals(m3, sm.firstKey());
   assertEquals(m3, sm.lastKey());
   assertEquals("C", sm.remove(m3));
   assertTrue(sm.isEmpty());
   assertEquals(3, map.size());
 }