public static void testPickNext() { List<Integer> list = new ArrayList<>(10); for (int i = 0; i < 10; i++) list.add(i); Integer num = Util.pickNext(list, 5); System.out.println("number next to 5: " + num); assert num != null; assert num.equals(6); num = Util.pickNext(list, 9); System.out.println("number next to 9: " + num); assert num != null; assert num.equals(0); num = Util.pickNext(list, 11); assert num == null; }
protected void handleView(View view) { view_size = view.size(); Address tmp = Util.pickNext(view.getMembers(), local_addr); if (tmp != null && !tmp.equals(local_addr)) { next = tmp; if (log.isDebugEnabled()) log.debug("next=" + next); } }
public static void testPickNextN() { List<Integer> list = Arrays.asList(1, 2, 3, 4); List<Integer> result = Util.pickNext(list, 0, 1); assert result.isEmpty(); result = Util.pickNext(list, 1, 1); System.out.println("result = " + result); assert result.size() == 1; assert result.contains(2); result = Util.pickNext(list, 3, 2); System.out.println("result = " + result); assert result.size() == 2; assert result.contains(4) && result.contains(1); result = Util.pickNext(list, 4, 5); System.out.println("result = " + result); assert result.size() == 3; assert result.contains(1) && result.contains(2) && result.contains(3); }
public void handleView(View view) { Address oldCoord = coord; if (view.size() > 0) { coord = view.getMembers().iterator().next(); is_coord = coord.equals(local_addr); if (log.isDebugEnabled()) log.debug("local_addr=" + local_addr + ", coord=" + coord + ", is_coord=" + is_coord); } // If we got a new coordinator we have to send all the requests for // tasks and consumers again just incase they were missed when // coordinator went down // We are okay with duplicates since we don't add multiple times. // We also have a problem that if a task/consumer was picked up as the // consumer is changing we may have duplicates. But this is technically // okay in that an extra consumer will reject and an extra task will just // be ran and return nowhere, but at least we won't lose data. if (oldCoord != coord) { for (Long requests : _requestId.values()) { sendToCoordinator(Type.RUN_REQUEST, requests, local_addr); } for (Long requests : _consumerId.keySet()) { sendToCoordinator(Type.CONSUMER_READY, requests, local_addr); } } if (num_backups > 0) { if (is_coord) { List<Address> new_backups = Util.pickNext(view.getMembers(), local_addr, num_backups); List<Address> new_members = null; synchronized (backups) { if (!backups.equals(new_backups)) { new_members = new ArrayList<Address>(new_backups); new_members.removeAll(backups); backups.clear(); backups.addAll(new_backups); } } if (new_members != null && !new_members.isEmpty()) copyQueueTo(new_members); } // We keep what backups we have ourselves, so that when we become // the coordinator we don't update them again. Technically we can // send multiple requests but don't if to prevent more message being // sent. else { List<Address> possiblebackups = Util.pickNext(view.getMembers(), coord, num_backups); boolean foundMyself = false; List<Address> myBackups = new ArrayList<Address>(); for (Address backup : possiblebackups) { if (foundMyself) { myBackups.add(backup); } else if (backup.equals(local_addr)) { foundMyself = true; } } synchronized (backups) { backups.clear(); backups.addAll(myBackups); } } } // Need to run this last so the backups are updated super.handleView(view); }