use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class FD_SOCK method getCacheFromCoordinator.
/**
* Determines coordinator C. If C is null and we are the first member, return. Else loop: send GET_CACHE message
* to coordinator and wait for GET_CACHE_RSP response. Loop until valid response has been received.
*/
protected void getCacheFromCoordinator() {
Address coord;
int attempts = num_tries;
get_cache_promise.reset();
while (attempts > 0 && isPingerThreadRunning()) {
if ((coord = determineCoordinator()) != null) {
if (// we are the first member --> empty cache
coord.equals(local_addr))
return;
// always sent to coord != self, so we don't need the DONT_LOOPBACK flag here:
Message msg = new EmptyMessage(coord).putHeader(this.id, new FdHeader(FdHeader.GET_CACHE));
down_prot.down(msg);
Map<Address, IpAddress> result = get_cache_promise.getResult(get_cache_timeout);
if (result != null) {
cache.addAll(result);
log.trace("%s: got cache from %s: cache is %s", local_addr, coord, cache);
return;
}
}
--attempts;
}
}
use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class FD_SOCK method unmarshal.
protected Map<Address, IpAddress> unmarshal(byte[] buffer, int offset, int length) {
if (buffer == null)
return null;
DataInput in = new ByteArrayDataInputStream(buffer, offset, length);
HashMap<Address, IpAddress> addrs = null;
try {
int size = in.readInt();
if (size > 0) {
addrs = new HashMap<>(size);
for (int i = 0; i < size; i++) {
Address key = Util.readAddress(in);
IpAddress val = Util.readStreamable(IpAddress::new, in);
addrs.put(key, val);
}
}
return addrs;
} catch (Exception ex) {
log.error("%s: failed reading addresses from message: %s", local_addr, ex);
return null;
}
}
use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class FD_SOCK method run.
/**
* Runs as long as there are 2 members and more. Determines the member to be monitored and fetches its
* server socket address (if n/a, sends a message to obtain it). The creates a client socket and listens on
* it until the connection breaks. If it breaks, emits a SUSPECT message. It the connection is closed regularly,
* nothing happens. In both cases, a new member to be monitored will be chosen and monitoring continues (unless
* there are fewer than 2 members).
*/
public void run() {
// 1. Broadcast my own addr:sock to all members so they can update their cache
if (!srv_sock_sent && srv_sock_addr != null) {
sendIHaveSockMessage(null, local_addr, srv_sock_addr);
srv_sock_sent = true;
}
// 2. Get the addr:pid cache from the coordinator (only if not already fetched)
if (!got_cache_from_coord) {
getCacheFromCoordinator();
got_cache_from_coord = true;
}
log.trace("%s: pinger_thread started", local_addr);
while (hasPingableMembers()) {
regular_sock_close = false;
// gets the neighbor to our right
ping_dest = determinePingDest();
if (ping_dest == null || !isPingerThreadRunning())
break;
log.debug("%s: pingable_mbrs=%s, ping_dest=%s", local_addr, printPingableMembers(), ping_dest);
IpAddress ping_addr = fetchPingAddress(ping_dest);
if (ping_addr == null) {
log.trace("%s: socket address for %s could not be fetched, retrying", local_addr, ping_dest);
Util.sleep(1000);
continue;
}
if (!setupPingSocket(ping_addr) && isPingerThreadRunning()) {
// log.debug("%s: failed connecting to to %s", local_addr, ping_dest);
broadcastSuspectMessage(ping_dest);
removeFromPingableMembers(ping_dest);
continue;
}
log.trace("%s: ping_dest=%s, ping_sock=%s, cache=%s", local_addr, ping_dest, ping_sock, cache);
// at this point ping_input must be non-null, otherwise setupPingSocket() would have thrown an exception
try {
if (ping_input != null) {
int c = ping_input.read();
switch(c) {
case NORMAL_TERMINATION:
log.debug("%s: %s closed socket gracefully", local_addr, ping_dest);
removeFromPingableMembers(ping_dest);
break;
case // -1 means EOF
ABNORMAL_TERMINATION:
handleSocketClose(null);
break;
default:
break;
}
}
} catch (IOException ex) {
// we got here when the peer closed the socket --> suspect peer and then continue
handleSocketClose(ex);
} catch (Throwable catch_all_the_rest) {
log.error("exception", catch_all_the_rest);
}
}
log.trace("%s: pinger thread terminated", local_addr);
}
use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class FD_SOCK2 method connectTo.
protected boolean connectTo(Address new_ping_dest, Membership mbrs) {
Address old_dest = ping_dest.dest();
IpAddress old_dest_physical = ping_dest.destPhysical();
List<IpAddress> dests = getPhysicalAddresses(new_ping_dest);
ping_dest.reset().dest(new_ping_dest);
log.debug("%s: trying to connect to %s", local_addr, new_ping_dest);
long start = System.currentTimeMillis();
for (IpAddress d : dests) {
if (connectTo(d, new_ping_dest)) {
long time = System.currentTimeMillis() - start;
ping_dest.dest(new_ping_dest).destPhysical(d);
log.debug("%s: connected successfully to %s (%s) in %d ms", local_addr, ping_dest.dest(), d, time);
// Close the connection to the previous ping_dest if it was *not* our neighbor to the left:
// 1. {A,B}: A - B, B - A // '-' denotes a bidirectional connection
// 2. {A,B,C}: A - B, B - C, C - A
// 3. {A,B,C,D}: A - B, B - C, C - D, D - A: C can now close its connection to A
Address left_mbr = mbrs.getPrevious(local_addr);
if (old_dest != null && !Objects.equals(old_dest, left_mbr)) {
// close connection to previous ping_dest
srv.closeConnection(old_dest_physical, false);
log.trace("%s: closed connection to previous ping-dest %s (%s)", local_addr, old_dest, old_dest_physical);
}
return true;
}
}
return false;
}
use of org.jgroups.stack.IpAddress in project JGroups by belaban.
the class FD_SOCK2 method start.
public void start() throws Exception {
super.start();
TP transport = getTransport();
timer = transport.getTimer();
if (timer == null)
throw new Exception("timer is null");
PhysicalAddress addr = transport.getPhysicalAddress();
int actual_port = ((IpAddress) addr).getPort();
int[] bind_ports = computeBindPorts(actual_port);
srv = createServer(bind_ports);
srv.receiver(this).clientBindPort(client_bind_port).usePeerConnections(true).addConnectionListener(this);
srv.start();
log.info("server listening on %s", bind_addr != null ? srv.getChannel().getLocalAddress() : "*." + getActualBindPort());
}
Aggregations