org.jgroups.Address Java Examples

The following examples show how to use org.jgroups.Address. 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: ELECTION.java    From jgroups-raft with Apache License 2.0 6 votes vote down vote up
protected void handleVoteRequest(Address sender, int term, int last_log_term, int last_log_index) {
    if(log.isTraceEnabled())
        log.trace("%s: received VoteRequest from %s: term=%d, my term=%d, last_log_term=%d, last_log_index=%d",
                  local_addr, sender, term, raft.currentTerm(), last_log_term, last_log_index);
    boolean send_vote_rsp=false;
    synchronized(this) {
        if(voteFor(sender)) {
            if(sameOrNewer(last_log_term, last_log_index))
                send_vote_rsp=true;
            else {
                log.trace("%s: dropped VoteRequest from %s as my log is more up-to-date", local_addr, sender);
            }
        }
        else
            log.trace("%s: already voted for %s in term %d; skipping vote", local_addr, sender, term);
    }
    if(send_vote_rsp)
        sendVoteResponse(sender, term); // raft.current_term);
}
 
Example #2
Source File: NO_DUPES.java    From jgroups-raft with Apache License 2.0 6 votes vote down vote up
/**
 * @return True if the message should be passed up, false if it should be discarded
 */
protected boolean handleGmsHeader(GMS.GmsHeader hdr, Address sender) {
    switch(hdr.getType()) {
        case GMS.GmsHeader.JOIN_REQ:
        case GMS.GmsHeader.JOIN_REQ_WITH_STATE_TRANSFER:
            Address joiner=hdr.getMember();
            if(!(joiner instanceof ExtendedUUID)) {
                log.debug("joiner %s needs to have an ExtendedUUID but has a %s", sender, joiner.getClass().getSimpleName());
                break;
            }
            View v=view;
            if(contains(v, (ExtendedUUID)joiner)) {
                String msg=String.format("join of %s rejected as it would create a view with duplicate members (current view: %s)", joiner, v);
                log.warn(msg);
                sendJoinRejectedMessageTo(sender, msg);
                return false;
            }
            break;
        case GMS.GmsHeader.MERGE_REQ:
            // to be done later when we know how to handle merges in jgroups-raft
            break;
    }
    return true;
}
 
Example #3
Source File: AppendEntriesTest.java    From jgroups-raft with Apache License 2.0 6 votes vote down vote up
public void testRAFTPaperScenarioF() throws Exception {
    Address leader=Util.createRandomAddress("A");
    initB();
    RaftImpl impl=getImpl(b);
    Log log=impl.raft().log();
    byte[] buf=new byte[10];
    append(impl,  1, 0, new LogEntry(1, buf), leader, 1);
    append(impl,  2, 1, new LogEntry(1, buf), leader, 1);
    append(impl,  3, 1, new LogEntry(1, buf), leader, 1);
    append(impl,  4, 1, new LogEntry(2, buf), leader, 1);
    append(impl,  5, 2, new LogEntry(2, buf), leader, 1);
    append(impl,  6, 2, new LogEntry(2, buf), leader, 1);
    append(impl,  7, 2, new LogEntry(3, buf), leader, 1);
    append(impl,  8, 3, new LogEntry(3, buf), leader, 1);
    append(impl,  9, 3, new LogEntry(3, buf), leader, 1);
    append(impl, 10, 3, new LogEntry(3, buf), leader, 1);
    append(impl, 11, 3, new LogEntry(3, buf), leader, 11);

    AppendResult result=append(impl, 11, 6, new LogEntry(6, buf), leader, 11);
    assertFalse(result.isSuccess());
    assertEquals(result.getIndex(), 7);
    assertLogIndices(log, 11, 11, 3);
}
 
Example #4
Source File: AppendEntriesTest.java    From jgroups-raft with Apache License 2.0 6 votes vote down vote up
public void testRAFTPaperAppendOnLeader() throws Exception {
    Address leader=Util.createRandomAddress("A");
    initB();
    RaftImpl impl=getImpl(b);
    Log log=impl.raft().log();
    byte[] buf=new byte[10];
    append(impl,  1, 0, new LogEntry(1, buf), leader, 1);
    append(impl,  2, 1, new LogEntry(1, buf), leader, 1);
    append(impl,  3, 1, new LogEntry(1, buf), leader, 1);
    append(impl,  4, 1, new LogEntry(4, buf), leader, 1);
    append(impl,  5, 4, new LogEntry(4, buf), leader, 1);
    append(impl,  6, 4, new LogEntry(5, buf), leader, 1);
    append(impl,  7, 5, new LogEntry(5, buf), leader, 1);
    append(impl,  8, 5, new LogEntry(6, buf), leader, 1);
    append(impl,  9, 6, new LogEntry(6, buf), leader, 1);
    append(impl, 10, 6, new LogEntry(6, buf), leader, 10);
    AppendResult result=append(impl, 11, 6, new LogEntry(6, buf), leader, 1);
    assertTrue(result.isSuccess());
    assertEquals(result.getIndex(), 11);
    assertLogIndices(log, 11, 10, 6);
}
 
Example #5
Source File: UniTimeClusterDiscovery.java    From unitime with Apache License 2.0 6 votes vote down vote up
protected void remove(Address addr) {
  	if (!ClusterDiscoveryDAO.isConfigured()) return;
  	org.hibernate.Session hibSession = ClusterDiscoveryDAO.getInstance().createNewSession();
String own_address = addressAsString(addr);
Transaction tx = null;
      try {
      	tx = hibSession.beginTransaction();
      	ClusterDiscovery cluster = ClusterDiscoveryDAO.getInstance().get(new ClusterDiscovery(own_address, cluster_name), hibSession);
      	if (cluster != null)
      		hibSession.delete(cluster);
      	hibSession.flush();
          if (tx != null) tx.commit();
      } catch (Exception e) {
	if (tx != null) tx.rollback();
	log.info("Failed to delete data for cluster " + cluster_name + ": " + e.getMessage());
} finally {
	hibSession.close();
}
  }
 
Example #6
Source File: UniTimeClusterDiscovery.java    From unitime with Apache License 2.0 6 votes vote down vote up
protected void handleView(View new_view, View old_view, boolean coord_changed) {
    if(is_coord) {
        if(clear_table_on_view_change)
            clearTable();
        else if(old_view != null && new_view != null) {
            Address[][] diff=View.diff(old_view, new_view);
            Address[] left_mbrs=diff[1];
            for(Address left_mbr : left_mbrs)
                if(left_mbr != null && !new_view.containsMember(left_mbr))
                    remove(left_mbr);
        }
    }
    if(coord_changed || clear_table_on_view_change)
        writeOwnInformation(); // write immediately
    if(info_writer_max_writes_after_view > 0)
        startInfoWriter(); // and / or write in the background
}
 
Example #7
Source File: AppendEntriesTest.java    From jgroups-raft with Apache License 2.0 6 votes vote down vote up
public void testRAFTPaperScenarioD() throws Exception {
    Address leader=Util.createRandomAddress("A");
    initB();
    RaftImpl impl=getImpl(b);
    Log log=impl.raft().log();
    byte[] buf=new byte[10];
    append(impl,  1, 0, new LogEntry(1, buf), leader, 1);
    append(impl,  2, 1, new LogEntry(1, buf), leader, 1);
    append(impl,  3, 1, new LogEntry(1, buf), leader, 1);
    append(impl,  4, 1, new LogEntry(4, buf), leader, 1);
    append(impl,  5, 4, new LogEntry(4, buf), leader, 1);
    append(impl,  6, 4, new LogEntry(5, buf), leader, 1);
    append(impl,  7, 5, new LogEntry(5, buf), leader, 1);
    append(impl,  8, 5, new LogEntry(6, buf), leader, 1);
    append(impl,  9, 6, new LogEntry(6, buf), leader, 1);
    append(impl, 10, 6, new LogEntry(6, buf), leader, 1);
    append(impl, 11, 6, new LogEntry(7, buf), leader, 1);
    append(impl, 12, 7, new LogEntry(7, buf), leader, 12);

    AppendResult result=append(impl, buf, leader, 10, 6, 8, 12);
    assertTrue(result.isSuccess());
    assertEquals(result.getIndex(), 11);
    assertLogIndices(log, 11, 11, 8);
}
 
Example #8
Source File: SolverServerService.java    From unitime with Apache License 2.0 5 votes vote down vote up
public SolverServer getServer(String host) {
	if ("local".equals(host) || host == null)
		return iServer;
	if (iChannel != null)
		for (Address address: iChannel.getView().getMembers()) {
			if (host.equals(address.toString()))
				return iServer.crateServerProxy(address);
		}
	return null;
}
 
Example #9
Source File: ExaminationSolverContainerRemote.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public ExamSolverProxy createProxy(Address address, String user) {
	SolverInvocationHandler handler = new SolverInvocationHandler(address, user);
	return (ExamSolverProxy)Proxy.newProxyInstance(
			ExamSolverProxy.class.getClassLoader(),
			new Class[] {ExamSolverProxy.class, RemoteSolver.class, },
			handler);
}
 
Example #10
Source File: AppendEntriesTest.java    From jgroups-raft with Apache License 2.0 5 votes vote down vote up
public void testRAFTPaperScenarioB() throws Exception {
    Address leader=Util.createRandomAddress("A");
    initB();
    RaftImpl impl=getImpl(b);
    Log log=impl.raft().log();
    byte[] buf=new byte[10];
    append(impl,  1, 0, new LogEntry(1, buf), leader, 1);
    append(impl, 2, 1, new LogEntry(1, buf), leader, 1);
    append(impl, 3, 1, new LogEntry(1, buf), leader, 1);
    append(impl, 4, 1, new LogEntry(4, buf), leader, 4);
    AppendResult result=append(impl, 11, 6, new LogEntry(6, buf), leader, 4);
    assertFalse(result.isSuccess());
    assertEquals(result.getIndex(), 4);
    assertLogIndices(log, 4, 4, 4);
}
 
Example #11
Source File: SolverServerImplementation.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public RoomAvailabilityInterface getRoomAvailability() {
	if (isLocal())
		return super.getRoomAvailability();

	Address local = getLocalAddress();
	if (local != null)
		return (RoomAvailabilityInterface)Proxy.newProxyInstance(
				SolverServerImplementation.class.getClassLoader(),
				new Class[] {RoomAvailabilityInterface.class},
				new RoomAvailabilityInvocationHandler(local, iRemoteRoomAvailability));

	return null;
}
 
Example #12
Source File: SolverServerImplementation.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public QueueProcessor getQueueProcessor() {
	Address local = getLocalAddress();
	if (local != null)
		return (QueueProcessor)Proxy.newProxyInstance(
				SolverServerImplementation.class.getClassLoader(),
				new Class[] {QueueProcessor.class},
				new QueueProcessorInvocationHandler(local, iRemoteQueueProcessor));
	return super.getQueueProcessor();
}
 
Example #13
Source File: OnlineStudentSchedulingContainerRemote.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public OnlineSectioningServer createProxy(Collection<Address> addresses, String user) {
	ReplicatedServerInvocationHandler handler = new ReplicatedServerInvocationHandler(addresses, user);
	OnlineSectioningServer px = (OnlineSectioningServer)Proxy.newProxyInstance(
			SolverProxy.class.getClassLoader(),
			new Class[] {OnlineSectioningServer.class, RemoteSolver.class, },
			handler);
   	return px;
}
 
Example #14
Source File: UniTimeClusterDiscovery.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected boolean contains(Address addr) {
  	if (!ClusterDiscoveryDAO.isConfigured()) return false;
  	org.hibernate.Session hibSession = ClusterDiscoveryDAO.getInstance().createNewSession();
String own_address = addressAsString(addr);
      try {
      	ClusterDiscovery cluster = ClusterDiscoveryDAO.getInstance().get(new ClusterDiscovery(own_address, cluster_name), hibSession);
      	return cluster != null;
      } catch (Exception e) {
	log.info("Failed to read data for cluster " + cluster_name + ": " + e.getMessage());
	return false;
} finally {
	hibSession.close();
}
  }
 
Example #15
Source File: UniTimeClusterDiscovery.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void discoveryRequestReceived(Address sender, String logical_name, PhysicalAddress physical_addr) {
    super.discoveryRequestReceived(sender, logical_name, physical_addr);
    if(physical_addr != null) {
        if(!initial_hosts.contains(physical_addr))
            dynamic_hosts.addIfAbsent(physical_addr);
    }
}
 
Example #16
Source File: RemoteRoomAvailability.java    From unitime with Apache License 2.0 5 votes vote down vote up
public Object dispatch(Address address, Method method, Object[] args) throws Exception {
	try {
		return iDispatcher.callRemoteMethod(address, "invoke",  new Object[] { method.getName(), method.getParameterTypes(), args }, new Class[] { String.class, Class[].class, Object[].class }, SolverServerImplementation.sFirstResponse);
	} catch (Exception e) {
		sLog.error("Excution of room availability method " + method + " failed: " + e.getMessage(), e);
		return null;
	}
}
 
Example #17
Source File: LogTest.java    From jgroups-raft with Apache License 2.0 5 votes vote down vote up
public void testFields(Log log) throws Exception {
    Address addr=Util.createRandomAddress("A");
    this.log=log;
    log.init(filename, null);
    log.currentTerm(22);
    int current_term=log.currentTerm();
    assertEquals(current_term, 22);

    log.votedFor(addr);
    Address voted_for=log.votedFor();
    assertEquals(addr, voted_for);

    log.close();
    log.init(filename, null);
    current_term=log.currentTerm();
    assertEquals(current_term, 22);
    voted_for=log.votedFor();
    assertEquals(addr, voted_for);

    log.close();
    log.delete();
    log.init(filename, null);
    current_term=log.currentTerm();
    assertEquals(current_term, 0);
    voted_for=log.votedFor();
    assertNull(voted_for);
}
 
Example #18
Source File: VoteTest.java    From jgroups-raft with Apache License 2.0 5 votes vote down vote up
protected static Address leader(long timeout, long interval, JChannel ... channels) {
    long target_time=System.currentTimeMillis() + timeout;
    while(System.currentTimeMillis() <= target_time) {
        for(JChannel ch : channels) {
            if(ch.isConnected() && raft(ch).leader() != null)
                return raft(ch).leader();
        }
        Util.sleep(interval);
    }
    return null;
}
 
Example #19
Source File: SolverServerImplementation.java    From unitime with Apache License 2.0 5 votes vote down vote up
public SolverContainer<OnlineSectioningServer> createOnlineStudentSchedulingContainerProxy(Address address) {
	ContainerInvocationHandler<RemoteSolverContainer<OnlineSectioningServer>> handler = new ContainerInvocationHandler<RemoteSolverContainer<OnlineSectioningServer>>(address, iOnlineStudentSchedulingContainer);
	SolverContainer<OnlineSectioningServer> px = (SolverContainer<OnlineSectioningServer>)Proxy.newProxyInstance(
			SolverServerImplementation.class.getClassLoader(),
			new Class[] {SolverContainer.class},
			handler
			);
	return px;
}
 
Example #20
Source File: SolverServerImplementation.java    From unitime with Apache License 2.0 5 votes vote down vote up
public SolverContainer<StudentSolverProxy> createStudentSolverContainerProxy(Address address) {
	ContainerInvocationHandler<RemoteSolverContainer<StudentSolverProxy>> handler = new ContainerInvocationHandler<RemoteSolverContainer<StudentSolverProxy>>(address, iStudentSolverContainer);
	SolverContainer<StudentSolverProxy> px = (SolverContainer<StudentSolverProxy>)Proxy.newProxyInstance(
			SolverServerImplementation.class.getClassLoader(),
			new Class[] {SolverContainer.class},
			handler
			);
	return px;
}
 
Example #21
Source File: OnlineStudentSchedulingContainerRemote.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public OnlineSectioningServer createProxy(Address address, String user) {
	ServerInvocationHandler handler = new ServerInvocationHandler(address, user);
	OnlineSectioningServer px = (OnlineSectioningServer)Proxy.newProxyInstance(
			SolverProxy.class.getClassLoader(),
			new Class[] {OnlineSectioningServer.class, RemoteSolver.class, },
			handler);
   	return px;
}
 
Example #22
Source File: DynamicMembershipTest.java    From jgroups-raft with Apache License 2.0 5 votes vote down vote up
protected JChannel channel(Address addr) {
    for(JChannel ch: Arrays.asList(channels)) {
        if(ch.getAddress() != null && ch.getAddress().equals(addr))
            return ch;
    }
    return null;
}
 
Example #23
Source File: SolverServerImplementation.java    From unitime with Apache License 2.0 5 votes vote down vote up
public SolverContainer<InstructorSchedulingProxy> createInstructorSchedulingContainerProxy(Address address) {
	ContainerInvocationHandler<RemoteSolverContainer<InstructorSchedulingProxy>> handler = new ContainerInvocationHandler<RemoteSolverContainer<InstructorSchedulingProxy>>(address, iInstructorSchedulingContainer);
	SolverContainer<InstructorSchedulingProxy> px = (SolverContainer<InstructorSchedulingProxy>)Proxy.newProxyInstance(
			SolverServerImplementation.class.getClassLoader(),
			new Class[] {SolverContainer.class},
			handler
			);
	return px;
}
 
Example #24
Source File: SolverServerImplementation.java    From unitime with Apache License 2.0 5 votes vote down vote up
public SolverContainer<ExamSolverProxy> createExamSolverContainerProxy(Address address) {
	ContainerInvocationHandler<RemoteSolverContainer<ExamSolverProxy>> handler = new ContainerInvocationHandler<RemoteSolverContainer<ExamSolverProxy>>(address, iExamSolverContainer);
	SolverContainer<ExamSolverProxy> px = (SolverContainer<ExamSolverProxy>)Proxy.newProxyInstance(
			SolverServerImplementation.class.getClassLoader(),
			new Class[] {SolverContainer.class},
			handler
			);
	return px;
}
 
Example #25
Source File: DummySolverServer.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public RoomAvailabilityInterface getRoomAvailability() {
	Address local = getLocalAddress();
	if (local == null) return null;
	
	return (RoomAvailabilityInterface)Proxy.newProxyInstance(
			SolverServerImplementation.class.getClassLoader(),
			new Class[] {RoomAvailabilityInterface.class},
			new RoomAvailabilityInvocationHandler(local));
}
 
Example #26
Source File: ELECTION.java    From jgroups-raft with Apache License 2.0 5 votes vote down vote up
protected synchronized void handleHeartbeat(int term, Address leader) {
    if(Objects.equals(local_addr, leader))
        return;
    heartbeatReceived(true);
    if(role != Role.Follower || raft.updateTermAndLeader(term, leader)) {
        changeRole(Role.Follower);
        voteFor(null);
    }
}
 
Example #27
Source File: SolverServerImplementation.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public Address getLocalAddress() {
	if (isLocal()) return getAddress();
	try {
		RspList<Boolean> ret = iDispatcher.callRemoteMethods(null, "isLocal", new Object[] {}, new Class[] {}, sAllResponses);
		for (Rsp<Boolean> local: ret) {
			if (Boolean.TRUE.equals(local.getValue()))
				return local.getSender();
		}
		return null;
	} catch (Exception e) {
		sLog.error("Failed to retrieve local address: " + e.getMessage(), e);
		return null;
	}
}
 
Example #28
Source File: DummySolverServer.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public T createProxy(Address address, String user) {
	SolverInvocationHandler handler = new SolverInvocationHandler(address, user);
	return (T)Proxy.newProxyInstance(
			iClazz.getClassLoader(),
			new Class[] {iClazz, RemoteSolver.class, },
			handler);
}
 
Example #29
Source File: LevelDBLog.java    From jgroups-raft with Apache License 2.0 5 votes vote down vote up
private void checkForConsistency() throws Exception {

        int loggedFirstAppended = fromByteArrayToInt(db.get(FIRSTAPPENDED));
        log.trace("FirstAppended in DB is: %d", loggedFirstAppended);

        int loggedLastAppended = fromByteArrayToInt(db.get(LASTAPPENDED));
        log.trace("LastAppended in DB is: %d", loggedLastAppended);

        int loggedCurrentTerm = fromByteArrayToInt(db.get(CURRENTTERM));
        log.trace("CurrentTerm in DB is: %d", loggedCurrentTerm);

        int loggedCommitIndex = fromByteArrayToInt(db.get(COMMITINDEX));
        log.trace("CommitIndex in DB is: %d", loggedCommitIndex);

        Address loggedVotedForAddress =Util.objectFromByteBuffer(db.get(VOTEDFOR));
        log.trace("VotedFor in DB is: %s", loggedVotedForAddress);

        assert (firstAppended == loggedFirstAppended);
        assert (lastAppended == loggedLastAppended);
        assert (currentTerm == loggedCurrentTerm);
        assert (commitIndex == loggedCommitIndex);
        if (votedFor != null) {
            assert (votedFor.equals(loggedVotedForAddress));
        }

        LogEntry lastAppendedEntry = getLogEntry(lastAppended);
        assert (lastAppendedEntry==null || lastAppendedEntry.term <= currentTerm);

    }
 
Example #30
Source File: CourseSolverContainerRemote.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public SolverProxy createProxy(Address address, String user) {
	SolverInvocationHandler handler = new SolverInvocationHandler(address, user);
	SolverProxy px = (SolverProxy)Proxy.newProxyInstance(
			SolverProxy.class.getClassLoader(),
			new Class[] {SolverProxy.class, RemoteSolver.class, },
			handler);
	handler.setRemoteSolverProxy(px);
   	return px;
}