use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse in project geode by apache.
the class GMSJoinLeave method findCoordinatorFromView.
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "WA_NOT_IN_LOOP")
boolean findCoordinatorFromView() {
ArrayList<FindCoordinatorResponse> result;
SearchState state = searchState;
NetView v = state.view;
List<InternalDistributedMember> recipients = new ArrayList<>(v.getMembers());
if (recipients.size() > MAX_DISCOVERY_NODES && MAX_DISCOVERY_NODES > 0) {
recipients = recipients.subList(0, MAX_DISCOVERY_NODES);
}
if (state.registrants != null) {
recipients.addAll(state.registrants);
}
recipients.remove(localAddress);
// FindCoordinatorRequest req = new FindCoordinatorRequest(localAddress, state.alreadyTried,
// state.viewId, services.getMessenger().getPublickey(
// localAddress), services.getMessenger().getRequestId());
// req.setRecipients(v.getMembers());
boolean testing = unitTesting.contains("findCoordinatorFromView");
synchronized (state.responses) {
if (!testing) {
state.responses.clear();
}
String dhalgo = services.getConfig().getDistributionConfig().getSecurityUDPDHAlgo();
if (!dhalgo.isEmpty()) {
// Usually this happens when locator re-joins the cluster and it has saved view.
for (InternalDistributedMember mbr : v.getMembers()) {
Set<InternalDistributedMember> r = new HashSet<>();
r.add(mbr);
FindCoordinatorRequest req = new FindCoordinatorRequest(localAddress, state.alreadyTried, state.viewId, services.getMessenger().getPublicKey(localAddress), services.getMessenger().getRequestId(), dhalgo);
req.setRecipients(r);
services.getMessenger().send(req, v);
}
} else {
FindCoordinatorRequest req = new FindCoordinatorRequest(localAddress, state.alreadyTried, state.viewId, services.getMessenger().getPublicKey(localAddress), services.getMessenger().getRequestId(), dhalgo);
req.setRecipients(v.getMembers());
services.getMessenger().send(req, v);
}
try {
if (!testing) {
state.responses.wait(DISCOVERY_TIMEOUT);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
result = new ArrayList<>(state.responses);
state.responses.clear();
}
InternalDistributedMember coord = null;
if (localAddress.getNetMember().preferredForCoordinator()) {
// it's possible that all other potential coordinators are gone
// and this new member must become the coordinator
coord = localAddress;
}
boolean coordIsNoob = true;
for (FindCoordinatorResponse resp : result) {
InternalDistributedMember mbr = resp.getCoordinator();
if (!state.alreadyTried.contains(mbr)) {
boolean mbrIsNoob = (mbr.getVmViewId() < 0);
if (mbrIsNoob) {
// member has not yet joined
if (coordIsNoob && (coord == null || coord.compareTo(mbr, false) > 0)) {
coord = mbr;
}
} else {
// member has already joined
if (coordIsNoob || mbr.getVmViewId() > coord.getVmViewId()) {
coord = mbr;
coordIsNoob = false;
}
}
}
}
state.possibleCoordinator = coord;
return coord != null;
}
use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse in project geode by apache.
the class GMSJoinLeave method findCoordinator.
/**
* This contacts the locators to find out who the current coordinator is. All locators are
* contacted. If they don't agree then we choose the oldest coordinator and return it.
*/
private boolean findCoordinator() {
SearchState state = searchState;
assert this.localAddress != null;
// the coordinator
if (!state.hasContactedAJoinedLocator && state.view != null) {
return findCoordinatorFromView();
}
String dhalgo = services.getConfig().getDistributionConfig().getSecurityUDPDHAlgo();
FindCoordinatorRequest request = new FindCoordinatorRequest(this.localAddress, state.alreadyTried, state.viewId, services.getMessenger().getPublicKey(localAddress), services.getMessenger().getRequestId(), dhalgo);
Set<InternalDistributedMember> possibleCoordinators = new HashSet<InternalDistributedMember>();
Set<InternalDistributedMember> coordinatorsWithView = new HashSet<InternalDistributedMember>();
long giveUpTime = System.currentTimeMillis() + ((long) services.getConfig().getLocatorWaitTime() * 1000L);
int connectTimeout = (int) services.getConfig().getMemberTimeout() * 2;
boolean anyResponses = false;
logger.debug("sending {} to {}", request, locators);
state.hasContactedAJoinedLocator = false;
state.locatorsContacted = 0;
do {
for (InetSocketAddress addr : locators) {
try {
Object o = tcpClientWrapper.sendCoordinatorFindRequest(addr, request, connectTimeout);
FindCoordinatorResponse response = (o instanceof FindCoordinatorResponse) ? (FindCoordinatorResponse) o : null;
if (response != null) {
if (response.getRejectionMessage() != null) {
throw new GemFireConfigException(response.getRejectionMessage());
}
setCoordinatorPublicKey(response);
state.locatorsContacted++;
if (!state.hasContactedAJoinedLocator && response.getSenderId() != null && response.getSenderId().getVmViewId() >= 0) {
logger.debug("Locator's address indicates it is part of a distributed system " + "so I will not become membership coordinator on this attempt to join");
state.hasContactedAJoinedLocator = true;
}
if (response.getCoordinator() != null) {
anyResponses = true;
NetView v = response.getView();
int viewId = v == null ? -1 : v.getViewId();
if (viewId > state.viewId) {
state.viewId = viewId;
state.view = v;
state.registrants.clear();
if (response.getRegistrants() != null) {
state.registrants.addAll(response.getRegistrants());
}
}
if (viewId > -1) {
coordinatorsWithView.add(response.getCoordinator());
}
possibleCoordinators.add(response.getCoordinator());
}
}
} catch (IOException | ClassNotFoundException problem) {
}
}
} while (!anyResponses && System.currentTimeMillis() < giveUpTime);
if (possibleCoordinators.isEmpty()) {
return false;
}
if (coordinatorsWithView.size() > 0) {
// lets check current coordinators in view only
possibleCoordinators = coordinatorsWithView;
}
Iterator<InternalDistributedMember> it = possibleCoordinators.iterator();
if (possibleCoordinators.size() == 1) {
state.possibleCoordinator = it.next();
} else {
InternalDistributedMember oldest = it.next();
while (it.hasNext()) {
InternalDistributedMember candidate = it.next();
if (oldest.compareTo(candidate) > 0) {
oldest = candidate;
}
}
state.possibleCoordinator = oldest;
}
InternalDistributedMember coord = null;
boolean coordIsNoob = true;
for (; it.hasNext(); ) {
InternalDistributedMember mbr = it.next();
if (!state.alreadyTried.contains(mbr)) {
boolean mbrIsNoob = (mbr.getVmViewId() < 0);
if (mbrIsNoob) {
// member has not yet joined
if (coordIsNoob && (coord == null || coord.compareTo(mbr) > 0)) {
coord = mbr;
}
} else {
// member has already joined
if (coordIsNoob || mbr.getVmViewId() > coord.getVmViewId()) {
coord = mbr;
coordIsNoob = false;
}
}
}
}
return true;
}
use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse in project geode by apache.
the class GMSJoinLeaveJUnitTest method testFindCoordinatorInView.
@Test
public void testFindCoordinatorInView() throws Exception {
initMocks();
int viewId = 1;
List<InternalDistributedMember> mbrs = new LinkedList<>();
mbrs.add(mockMembers[0]);
mbrs.add(mockMembers[1]);
mbrs.add(mockMembers[2]);
when(services.getMessenger()).thenReturn(messenger);
// prepare the view
NetView netView = new NetView(mockMembers[0], viewId, mbrs);
SearchState state = gmsJoinLeave.searchState;
state.view = netView;
state.viewId = netView.getViewId();
InternalDistributedMember coordinator = mockMembers[2];
coordinator.setVmViewId(viewId);
// already tried joining using members 0 and 1
Set<InternalDistributedMember> set = new HashSet<>();
mockMembers[0].setVmViewId(viewId - 1);
set.add(mockMembers[0]);
mockMembers[1].setVmViewId(viewId - 1);
set.add(mockMembers[1]);
state.alreadyTried = set;
state.hasContactedAJoinedLocator = true;
// simulate a response being received
InternalDistributedMember sender = mockMembers[2];
FindCoordinatorResponse resp = new FindCoordinatorResponse(coordinator, sender, null, 0);
gmsJoinLeave.processMessage(resp);
// tell GMSJoinLeave that a unit test is running so it won't clear the
// responses collection
gmsJoinLeave.unitTesting.add("findCoordinatorFromView");
// now for the test
boolean result = gmsJoinLeave.findCoordinatorFromView();
assertTrue("should have found coordinator " + mockMembers[2], result);
assertTrue("should have found " + coordinator + " but found " + state.possibleCoordinator, state.possibleCoordinator == coordinator);
}
use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse in project geode by apache.
the class GMSJoinLeaveJUnitTest method testCoordinatorFindRequestFailure.
@Test
public void testCoordinatorFindRequestFailure() throws Exception {
try {
initMocks(false);
HashSet<InternalDistributedMember> registrants = new HashSet<>();
registrants.add(mockMembers[0]);
FindCoordinatorResponse fcr = new FindCoordinatorResponse(mockMembers[0], mockMembers[0], false, null, registrants, false, true, null);
NetView view = createView();
JoinResponseMessage jrm = new JoinResponseMessage(mockMembers[0], view, 0);
gmsJoinLeave.setJoinResponseMessage(jrm);
TcpClientWrapper tcpClientWrapper = mock(TcpClientWrapper.class);
gmsJoinLeave.setTcpClientWrapper(tcpClientWrapper);
FindCoordinatorRequest fcreq = new FindCoordinatorRequest(gmsJoinLeaveMemberId, new HashSet<>(), -1, null, 0, "");
int connectTimeout = (int) services.getConfig().getMemberTimeout() * 2;
// passing wrong port here, so ot will fail
when(tcpClientWrapper.sendCoordinatorFindRequest(new InetSocketAddress("localhost", 12346), fcreq, connectTimeout)).thenReturn(fcr);
assertFalse("Should not be able to join ", gmsJoinLeave.join());
} finally {
}
}
use of org.apache.geode.distributed.internal.membership.gms.locator.FindCoordinatorResponse in project geode by apache.
the class JGroupsMessengerJUnitTest method testEncryptedFindCoordinatorResponse.
@Test
public void testEncryptedFindCoordinatorResponse() throws Exception {
InternalDistributedMember otherMbr = new InternalDistributedMember("localhost", 8888);
Properties p = new Properties();
p.put(ConfigurationProperties.SECURITY_UDP_DHALGO, "AES:128");
initMocks(false, p);
NetView v = createView(otherMbr);
GMSEncrypt otherMbrEncrptor = new GMSEncrypt(services);
otherMbrEncrptor.setPublicKey(messenger.getPublicKey(messenger.getMemberID()), messenger.getMemberID());
messenger.setPublicKey(otherMbrEncrptor.getPublicKeyBytes(), otherMbr);
messenger.initClusterKey();
FindCoordinatorResponse gfmsg = new FindCoordinatorResponse(messenger.getMemberID(), messenger.getMemberID(), messenger.getClusterSecretKey(), 1);
Set<InternalDistributedMember> recipients = new HashSet<>();
recipients.add(otherMbr);
gfmsg.setRecipients(recipients);
short version = Version.CURRENT_ORDINAL;
HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
messenger.writeEncryptedMessage(gfmsg, version, out);
byte[] requestBytes = out.toByteArray();
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(requestBytes));
messenger.addRequestId(1, messenger.getMemberID());
DistributionMessage distributionMessage = messenger.readEncryptedMessage(dis, version, otherMbrEncrptor);
assertEquals(gfmsg, distributionMessage);
}
Aggregations