Exemplo n.º 1
0
  public void addBootstrapToken(Token token, InetAddress endpoint) {
    assert token != null;
    assert endpoint != null;

    lock.writeLock().lock();
    try {
      InetAddress oldEndPoint = null;

      oldEndPoint = bootstrapTokens.get(token);
      if (oldEndPoint != null && !oldEndPoint.equals(endpoint))
        throw new RuntimeException(
            "Bootstrap Token collision between "
                + oldEndPoint
                + " and "
                + endpoint
                + " (token "
                + token);

      oldEndPoint = tokenToEndPointMap.get(token);
      if (oldEndPoint != null && !oldEndPoint.equals(endpoint))
        throw new RuntimeException(
            "Bootstrap Token collision between "
                + oldEndPoint
                + " and "
                + endpoint
                + " (token "
                + token);

      bootstrapTokens.inverse().remove(endpoint);
      bootstrapTokens.put(token, endpoint);
    } finally {
      lock.writeLock().unlock();
    }
  }
 @Override
 protected Map<String, Integer> makePopulatedMap() {
   BiMap<String, Integer> bimap = HashBiMap.create();
   bimap.put("foo", 1);
   bimap.put("bar", 2);
   return Maps.unmodifiableBiMap(bimap);
 }
Exemplo n.º 3
0
  /**
   * Store an end-point to host ID mapping. Each ID must be unique, and cannot be changed after the
   * fact.
   *
   * @param hostId
   * @param endpoint
   */
  public void updateHostId(UUID hostId, InetAddress endpoint) {
    assert hostId != null;
    assert endpoint != null;

    lock.writeLock().lock();
    try {
      InetAddress storedEp = endpointToHostIdMap.inverse().get(hostId);
      if (storedEp != null) {
        if (!storedEp.equals(endpoint) && (FailureDetector.instance.isAlive(storedEp))) {
          throw new RuntimeException(
              String.format(
                  "Host ID collision between active endpoint %s and %s (id=%s)",
                  storedEp, endpoint, hostId));
        }
      }

      UUID storedId = endpointToHostIdMap.get(endpoint);
      if ((storedId != null) && (!storedId.equals(hostId)))
        logger.warn("Changing {}'s host ID from {} to {}", endpoint, storedId, hostId);

      endpointToHostIdMap.forcePut(endpoint, hostId);
    } finally {
      lock.writeLock().unlock();
    }
  }
Exemplo n.º 4
0
 @SuppressWarnings("unchecked")
 @Override
 public BiMap<Country, Currency> create(Object... entries) {
   BiMap<Country, Currency> result = EnumBiMap.create(Country.class, Currency.class);
   for (Object object : entries) {
     Entry<Country, Currency> entry = (Entry<Country, Currency>) object;
     result.put(entry.getKey(), entry.getValue());
   }
   return result;
 }
Exemplo n.º 5
0
 @Override
 protected BiMap<String, String> create(Entry<String, String>[] entries) {
   BiMap<String, String> bimap =
       MapConstraints.constrainedBiMap(HashBiMap.<String, String>create(), TEST_CONSTRAINT);
   for (Entry<String, String> entry : entries) {
     checkArgument(!bimap.containsKey(entry.getKey()));
     bimap.put(entry.getKey(), entry.getValue());
   }
   return bimap;
 }
Exemplo n.º 6
0
 public void testPutWithAllowedKeyForbiddenValue() {
   BiMap<String, String> map =
       MapConstraints.constrainedBiMap(HashBiMap.<String, String>create(), TEST_CONSTRAINT);
   try {
     map.put("allowed", TEST_VALUE);
     fail("Expected IllegalArgumentException");
   } catch (IllegalArgumentException expected) {
     // success
   }
 }
Exemplo n.º 7
0
  public InetAddress getFirstEndpoint() {
    assert tokenToEndPointMap.size() > 0;

    lock.readLock().lock();
    try {
      return tokenToEndPointMap.get(sortedTokens.get(0));
    } finally {
      lock.readLock().unlock();
    }
  }
Exemplo n.º 8
0
  public void removeEndpoint(InetAddress endpoint) {
    assert endpoint != null;

    lock.writeLock().lock();
    try {
      bootstrapTokens.inverse().remove(endpoint);
      tokenToEndPointMap.inverse().remove(endpoint);
      leavingEndPoints.remove(endpoint);
      sortedTokens = sortTokens();
    } finally {
      lock.writeLock().unlock();
    }
  }
 @Override
 protected void assertMoreInvariants(Map<String, Integer> map) {
   BiMap<String, Integer> bimap = (BiMap<String, Integer>) map;
   BiMap<Integer, String> inverse = bimap.inverse();
   assertEquals(bimap.size(), inverse.size());
   for (Entry<String, Integer> entry : bimap.entrySet()) {
     assertEquals(entry.getKey(), inverse.get(entry.getValue()));
   }
   for (Entry<Integer, String> entry : inverse.entrySet()) {
     assertEquals(entry.getKey(), bimap.get(entry.getValue()));
   }
 }
Exemplo n.º 10
0
 public Token getSuccessor(Token token) {
   List tokens = sortedTokens();
   int index = Collections.binarySearch(tokens, token);
   assert index >= 0
       : token + " not found in " + StringUtils.join(tokenToEndPointMap.keySet(), ", ");
   return (Token) ((index == (tokens.size() - 1)) ? tokens.get(0) : tokens.get(index + 1));
 }
Exemplo n.º 11
0
  public void updateNormalToken(Token token, InetAddress endpoint) {
    assert token != null;
    assert endpoint != null;

    lock.writeLock().lock();
    try {
      bootstrapTokens.inverse().remove(endpoint);
      tokenToEndPointMap.inverse().remove(endpoint);
      if (!endpoint.equals(tokenToEndPointMap.put(token, endpoint))) {
        sortedTokens = sortTokens();
      }
      leavingEndPoints.remove(endpoint);
    } finally {
      lock.writeLock().unlock();
    }
  }
Exemplo n.º 12
0
 public InetAddress getEndPoint(Token token) {
   lock.readLock().lock();
   try {
     return tokenToEndPointMap.get(token);
   } finally {
     lock.readLock().unlock();
   }
 }
Exemplo n.º 13
0
 /** Return the end-point for a unique host ID */
 public InetAddress getEndpointForHostId(UUID hostId) {
   lock.readLock().lock();
   try {
     return endpointToHostIdMap.inverse().get(hostId);
   } finally {
     lock.readLock().unlock();
   }
 }
Exemplo n.º 14
0
 /** Return the unique host ID for an end-point. */
 public UUID getHostId(InetAddress endpoint) {
   lock.readLock().lock();
   try {
     return endpointToHostIdMap.get(endpoint);
   } finally {
     lock.readLock().unlock();
   }
 }
Exemplo n.º 15
0
 public Set<InetAddress> getAllEndpoints() {
   lock.readLock().lock();
   try {
     return ImmutableSet.copyOf(endpointToHostIdMap.keySet());
   } finally {
     lock.readLock().unlock();
   }
 }
Exemplo n.º 16
0
  public String toString() {
    StringBuilder sb = new StringBuilder();
    lock.readLock().lock();
    try {
      Set<InetAddress> eps = tokenToEndPointMap.inverse().keySet();

      if (!eps.isEmpty()) {
        sb.append("Normal Tokens:");
        sb.append(System.getProperty("line.separator"));
        for (InetAddress ep : eps) {
          sb.append(ep);
          sb.append(":");
          sb.append(tokenToEndPointMap.inverse().get(ep));
          sb.append(System.getProperty("line.separator"));
        }
      }

      if (!bootstrapTokens.isEmpty()) {
        sb.append("Bootstrapping Tokens:");
        sb.append(System.getProperty("line.separator"));
        for (Map.Entry<Token, InetAddress> entry : bootstrapTokens.entrySet()) {
          sb.append(entry.getValue() + ":" + entry.getKey());
          sb.append(System.getProperty("line.separator"));
        }
      }

      if (!leavingEndPoints.isEmpty()) {
        sb.append("Leaving EndPoints:");
        sb.append(System.getProperty("line.separator"));
        for (InetAddress ep : leavingEndPoints) {
          sb.append(ep);
          sb.append(System.getProperty("line.separator"));
        }
      }

      if (!pendingRanges.isEmpty()) {
        sb.append("Pending Ranges:");
        sb.append(System.getProperty("line.separator"));
        sb.append(printPendingRanges());
      }
    } finally {
      lock.readLock().unlock();
    }

    return sb.toString();
  }
Exemplo n.º 17
0
  public UUID convertOldCfId(Integer oldCfId) throws UnknownColumnFamilyException {
    UUID cfId = oldCfIdMap.get(oldCfId);

    if (cfId == null)
      throw new UnknownColumnFamilyException(
          "ColumnFamily identified by old " + oldCfId + " was not found.", null);

    return cfId;
  }
Exemplo n.º 18
0
    @Override
    protected void assertMoreInvariants(Map<K, V> map) {

      BiMap<K, V> bimap = (BiMap<K, V>) map;

      for (Entry<K, V> entry : map.entrySet()) {
        assertEquals(entry.getKey() + "=" + entry.getValue(), entry.toString());
        assertEquals(entry.getKey(), bimap.inverse().get(entry.getValue()));
      }

      assertEquals("{" + joiner.join(map.entrySet()) + "}", map.toString());
      assertEquals("[" + joiner.join(map.entrySet()) + "]", map.entrySet().toString());
      assertEquals("[" + joiner.join(map.keySet()) + "]", map.keySet().toString());
      assertEquals("[" + joiner.join(map.values()) + "]", map.values().toString());

      assertEquals(Sets.newHashSet(map.entrySet()), map.entrySet());
      assertEquals(Sets.newHashSet(map.keySet()), map.keySet());
    }
Exemplo n.º 19
0
  public void removeBootstrapToken(Token token) {
    assert token != null;

    lock.writeLock().lock();
    try {
      bootstrapTokens.remove(token);
    } finally {
      lock.writeLock().unlock();
    }
  }
Exemplo n.º 20
0
  public boolean isMember(InetAddress endpoint) {
    assert endpoint != null;

    lock.readLock().lock();
    try {
      return tokenToEndPointMap.inverse().containsKey(endpoint);
    } finally {
      lock.readLock().unlock();
    }
  }
Exemplo n.º 21
0
  public Token getToken(InetAddress endpoint) {
    assert endpoint != null;
    assert isMember(endpoint); // don't want to return nulls

    lock.readLock().lock();
    try {
      return tokenToEndPointMap.inverse().get(endpoint);
    } finally {
      lock.readLock().unlock();
    }
  }
Exemplo n.º 22
0
 /** used by tests */
 public void clearUnsafe() {
   lock.writeLock().lock();
   try {
     bootstrapTokens.clear();
     tokenToEndpointMap.clear();
     topology.clear();
     leavingEndpoints.clear();
     pendingRanges.clear();
     endpointToHostIdMap.clear();
     invalidateCachedRings();
   } finally {
     lock.writeLock().unlock();
   }
 }
  @Test
  public void whenCreateBiMap_thenCreated() {
    final BiMap<String, Integer> words = HashBiMap.create();
    words.put("First", 1);
    words.put("Second", 2);
    words.put("Third", 3);

    assertEquals(2, words.get("Second").intValue());
    assertEquals("Third", words.inverse().get(3));
  }
Exemplo n.º 24
0
  public void removeEndpoint(InetAddress endpoint) {
    assert endpoint != null;

    lock.writeLock().lock();
    try {
      bootstrapTokens.removeValue(endpoint);
      tokenToEndpointMap.removeValue(endpoint);
      topology.removeEndpoint(endpoint);
      leavingEndpoints.remove(endpoint);
      endpointToHostIdMap.remove(endpoint);
      sortedTokens = sortTokens();
      invalidateCachedRings();
    } finally {
      lock.writeLock().unlock();
    }
  }
Exemplo n.º 25
0
 public Class<?> classOf(ONIX o) {
   return map.get(o);
 }
Exemplo n.º 26
0
 public ONIX onixTypeOf(Object o) {
   return map.get(o.getClass());
 }
Exemplo n.º 27
0
 public Integer convertNewCfId(UUID newCfId) {
   return oldCfIdMap.containsValue(newCfId) ? oldCfIdMap.inverse().get(newCfId) : null;
 }
Exemplo n.º 28
0
 /** @return the number of nodes bootstrapping into source's primary range */
 public int pendingRangeChanges(InetAddress source) {
   int n = 0;
   Range sourceRange = getPrimaryRangeFor(getToken(source));
   for (Token token : bootstrapTokens.keySet()) if (sourceRange.contains(token)) n++;
   return n;
 }
Exemplo n.º 29
0
 private List<Token> sortTokens() {
   List<Token> tokens = new ArrayList<Token>(tokenToEndPointMap.keySet());
   Collections.sort(tokens);
   return Collections.unmodifiableList(tokens);
 }
Exemplo n.º 30
0
 /** used by tests */
 public void clearUnsafe() {
   bootstrapTokens.clear();
   tokenToEndPointMap.clear();
   leavingEndPoints.clear();
   pendingRanges.clear();
 }