Java Code Examples for com.google.common.hash.Funnels#stringFunnel()
The following examples show how to use
com.google.common.hash.Funnels#stringFunnel() .
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: ConsistantHashLoadBalancer.java From xio with Apache License 2.0 | 6 votes |
@Override public Node getNextNode(ImmutableList<Node> pool, Map<UUID, Node> okNodes, String sessionID) { List<String> idStrings = new ArrayList<>(); okNodes.keySet().stream().forEach(xs -> idStrings.add(xs.toString())); RendezvousHash rendezvousHash = new RendezvousHash( Funnels.stringFunnel(Charset.defaultCharset()), idStrings, idStrings.size()); for (Object nodeID : rendezvousHash.get(sessionID.getBytes())) { if (nodeID instanceof String) { UUID id = UUID.fromString((String) nodeID); Node nextNode = okNodes.get(id); if (okToPick(nextNode)) { return nextNode; } } else { return null; } } return null; }
Example 2
Source File: ServiceRateLimiter.java From xrpc with Apache License 2.0 | 5 votes |
private RendezvousHash<CharSequence> buildHasher( Map<String, RateLimiter> limiterMap, int poolSize, double rate) { List<String> tempPool = new ArrayList<>(); for (int i = 0; i < poolSize; i++) { String id = UUID.randomUUID().toString(); tempPool.add(id); limiterMap.put(id, RateLimiter.create(rate)); } return new RendezvousHash<>(Funnels.stringFunnel(XrpcConstants.DEFAULT_CHARSET), tempPool); }
Example 3
Source File: RendezvousHashTest.java From xrpc with Apache License 2.0 | 5 votes |
@Test public void get_shouldReturnExpectedNumberOfHashesOnAllRuns() throws Exception { List<String> hostList = new ArrayList<>(); Map<String, List<String>> hostToMatchingHashes = PlatformDependent.newConcurrentHashMap(); int totalHosts = 100; for (int i = 0; i < totalHosts; i++) { hostList.add(("Host" + i)); hostToMatchingHashes.put(("Host" + i), new ArrayList<>()); } RendezvousHash<CharSequence> rendezvousHash = new RendezvousHash<>(Funnels.stringFunnel(Charset.defaultCharset()), hostList); int totalGetsToRun = 100000; int hashesToMatch = 3; Random random = new Random(); for (int i = 0; i < totalGetsToRun; i++) { String randomNumberString = (Integer.toString(random.nextInt(MAX_RANDOM_NUMBER))); List<CharSequence> hosts = rendezvousHash.get(randomNumberString.getBytes(), hashesToMatch); hosts.forEach(host -> hostToMatchingHashes.get(host).add(randomNumberString)); } Double averageMatchingHashesPerHost = hostToMatchingHashes.values().stream().mapToInt(List::size).average().orElse(-1); int expectedAverage = hashesToMatch * totalGetsToRun / totalHosts; assertEquals(expectedAverage, averageMatchingHashesPerHost.intValue()); }
Example 4
Source File: RendezvousHashTest.java From xrpc with Apache License 2.0 | 5 votes |
@Test void simpleGet() { Map<String, String> map = new ImmutableMap.Builder<String, String>() .put("a", "1") .put("b", "2") .put("c", "3") .put("d", "4") .put("e", "5") .build(); RendezvousHash<CharSequence> hasher = new RendezvousHash<>(Funnels.stringFunnel(XrpcConstants.DEFAULT_CHARSET), map.keySet()); String k1 = "foo"; String k2 = "bar"; String k3 = "baz"; assertEquals(hasher.getOne(k1.getBytes()), hasher.getOne(k1.getBytes())); assertEquals(hasher.getOne(k2.getBytes()), hasher.getOne(k2.getBytes())); assertEquals(hasher.getOne(k3.getBytes()), hasher.getOne(k3.getBytes())); String k4 = "biz"; assertEquals(hasher.getOne(k4.getBytes()), hasher.getOne(k4.getBytes())); System.out.println(hasher.getOne(k1.getBytes())); System.out.println(hasher.getOne(k2.getBytes())); System.out.println(hasher.getOne(k3.getBytes())); System.out.println(hasher.getOne(k4.getBytes())); System.out.println(hasher.getOne(k1.getBytes())); System.out.println(hasher.getOne(k2.getBytes())); System.out.println(hasher.getOne(k3.getBytes())); System.out.println(hasher.getOne(k4.getBytes())); assertNotEquals(hasher.getOne(k1.getBytes()), hasher.getOne(k4.getBytes())); }
Example 5
Source File: PersistentProxyHandler.java From xio with Apache License 2.0 | 5 votes |
private RendezvousHash<CharSequence> buildHasher( Map<String, ClientConfig> configMap, int configSize) { List<String> randomIdPool = new ArrayList<>(); val clientConfigs = config.clientConfigs(); // map each client config to a randomly-generated ID for (int i = 0; i < configSize; i++) { String id = UUID.randomUUID().toString(); randomIdPool.add(id); configMap.put(id, clientConfigs.get(i)); } return new RendezvousHash<>(Funnels.stringFunnel(Constants.DEFAULT_CHARSET), randomIdPool); }
Example 6
Source File: ServiceRateLimiter.java From xio with Apache License 2.0 | 5 votes |
private RendezvousHash<CharSequence> buildHasher( Map<CharSequence, RateLimiter> limiterMap, int poolSize, double rate) { List<String> tempPool = new ArrayList<>(); for (int i = 0; i < poolSize; i++) { String id = UUID.randomUUID().toString(); tempPool.add(id); limiterMap.put(id, RateLimiter.create(rate)); } return new RendezvousHash<>(Funnels.stringFunnel(Constants.DEFAULT_CHARSET), tempPool); }
Example 7
Source File: RendezvousHashUnitTest.java From xio with Apache License 2.0 | 5 votes |
@Test public void get() throws Exception { List<String> nodeList = new ArrayList<>(); Map<String, List<String>> mm = PlatformDependent.newConcurrentHashMap(); for (int i = 0; i < 100; i++) { nodeList.add(("Host" + i)); mm.put(("Host" + i), new ArrayList<>()); } RendezvousHash<CharSequence> rendezvousHash = new RendezvousHash<>(Funnels.stringFunnel(Charset.defaultCharset()), nodeList); Random r = new Random(); for (int i = 0; i < 100000; i++) { String thing = (Integer.toString(r.nextInt(123456789))); List<CharSequence> hosts = rendezvousHash.get(thing.getBytes(), 3); hosts.forEach( xs -> { mm.get(xs).add(thing); }); } List<Integer> xx = new ArrayList<>(); mm.keySet() .forEach( xs -> { xx.add(mm.get(xs).size()); }); Double xd = xx.stream().mapToInt(x -> x).average().orElse(-1); assertEquals(3000, xd.intValue()); }
Example 8
Source File: RendezvousHashUnitTest.java From xio with Apache License 2.0 | 5 votes |
@Test public void simpleGet() { Map<String, String> map = new ImmutableMap.Builder<String, String>() .put("a", "1") .put("b", "2") .put("c", "3") .put("d", "4") .put("e", "5") .build(); RendezvousHash<CharSequence> hasher = new RendezvousHash<>(Funnels.stringFunnel(Constants.DEFAULT_CHARSET), map.keySet()); String k1 = "foo"; String k2 = "bar"; String k3 = "baz"; assertEquals(hasher.getOne(k1.getBytes()), hasher.getOne(k1.getBytes())); assertEquals(hasher.getOne(k2.getBytes()), hasher.getOne(k2.getBytes())); assertEquals(hasher.getOne(k3.getBytes()), hasher.getOne(k3.getBytes())); String k4 = "biz"; assertEquals(hasher.getOne(k4.getBytes()), hasher.getOne(k4.getBytes())); System.out.println(hasher.getOne(k1.getBytes())); System.out.println(hasher.getOne(k2.getBytes())); System.out.println(hasher.getOne(k3.getBytes())); System.out.println(hasher.getOne(k4.getBytes())); System.out.println(hasher.getOne(k1.getBytes())); System.out.println(hasher.getOne(k2.getBytes())); System.out.println(hasher.getOne(k3.getBytes())); System.out.println(hasher.getOne(k4.getBytes())); assertNotEquals(hasher.getOne(k1.getBytes()), hasher.getOne(k4.getBytes())); }
Example 9
Source File: StableBloomFilterTest.java From streaminer with Apache License 2.0 | 4 votes |
@Before public void setUp() { sbf = new StableBloomFilter<CharSequence>(50, 10, 5, Funnels.stringFunnel()); }