use of org.voltcore.agreement.AgreementSite in project voltdb by VoltDB.
the class HostMessenger method start.
/**
* Start the host messenger and connect to the leader, or become the leader
* if necessary.
*
* @param request The requested action to send to other nodes when joining
* the mesh. This is opaque to the HostMessenger, it can be any
* string. HostMessenger will encode this in the request to join mesh to the
* live hosts. The live hosts can use this request string to make further
* decision on whether or not to accept the request.
*/
public void start() throws Exception {
/*
* SJ uses this barrier if this node becomes the leader to know when ZooKeeper
* has been finished bootstrapping.
*/
CountDownLatch zkInitBarrier = new CountDownLatch(1);
/*
* If start returns true then this node is the leader, it bound to the coordinator address
* It needs to bootstrap its agreement site so that other nodes can join
*/
if (m_joiner.start(zkInitBarrier)) {
m_network.start();
/*
* m_localHostId is 0 of course.
*/
long agreementHSId = getHSIdForLocalSite(AGREEMENT_SITE_ID);
/*
* A set containing just the leader (this node)
*/
HashSet<Long> agreementSites = new HashSet<Long>();
agreementSites.add(agreementHSId);
/*
* A basic site mailbox for the agreement site
*/
SiteMailbox sm = new SiteMailbox(this, agreementHSId);
createMailbox(agreementHSId, sm);
/*
* Construct the site with just this node
*/
m_agreementSite = new AgreementSite(agreementHSId, agreementSites, 0, sm, new InetSocketAddress(m_config.zkInterface.split(":")[0], Integer.parseInt(m_config.zkInterface.split(":")[1])), m_config.backwardsTimeForgivenessWindow, m_failedHostsCallback);
m_agreementSite.start();
m_agreementSite.waitForRecovery();
m_zk = org.voltcore.zk.ZKUtil.getClient(m_config.zkInterface, 60 * 1000, VERBOTEN_THREADS);
if (m_zk == null) {
throw new Exception("Timed out trying to connect local ZooKeeper instance");
}
CoreZK.createHierarchy(m_zk);
/*
* This creates the ephemeral sequential node with host id 0 which
* this node already used for itself. Just recording that fact.
*/
final int selectedHostId = selectNewHostId(m_config.coordinatorIp.toString());
if (selectedHostId != 0) {
org.voltdb.VoltDB.crashLocalVoltDB("Selected host id for coordinator was not 0, " + selectedHostId, false, null);
}
/*
* seed the leader host criteria ad leader is always host id 0
*/
m_acceptor.accrue(selectedHostId, m_acceptor.decorate(new JSONObject(), Optional.empty()));
// Store the components of the instance ID in ZK
JSONObject instance_id = new JSONObject();
instance_id.put("coord", ByteBuffer.wrap(m_config.coordinatorIp.getAddress().getAddress()).getInt());
instance_id.put("timestamp", System.currentTimeMillis());
m_hostLog.debug("Cluster will have instance ID:\n" + instance_id.toString(4));
byte[] payload = instance_id.toString(4).getBytes("UTF-8");
m_zk.create(CoreZK.instance_id, payload, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
/*
* Store all the hosts and host ids here so that waitForGroupJoin
* knows the size of the mesh. This part only registers this host
*/
final HostInfo hostInfo = new HostInfo(m_config.coordinatorIp.toString(), m_config.group, m_config.localSitesCount);
m_zk.create(CoreZK.hosts_host + selectedHostId, hostInfo.toBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
zkInitBarrier.countDown();
}
use of org.voltcore.agreement.AgreementSite in project voltdb by VoltDB.
the class HostMessenger method notifyOfHosts.
/*
* SJ invokes this method after a node finishes connecting to the entire cluster.
* This method constructs all the hosts and puts them in the map
*/
@Override
public void notifyOfHosts(int yourHostId, int[] hosts, SocketChannel[] sockets, InetSocketAddress[] listeningAddresses, Map<Integer, JSONObject> jos) throws Exception {
m_localHostId = yourHostId;
long agreementHSId = getHSIdForLocalSite(AGREEMENT_SITE_ID);
/*
* Construct the set of agreement sites based on all the hosts that are connected
*/
HashSet<Long> agreementSites = new HashSet<Long>();
agreementSites.add(agreementHSId);
//network must be running for register to work
m_network.start();
for (int ii = 0; ii < hosts.length; ii++) {
m_networkLog.info(yourHostId + " notified of host " + hosts[ii]);
agreementSites.add(CoreUtils.getHSIdFromHostAndSite(hosts[ii], AGREEMENT_SITE_ID));
prepSocketChannel(sockets[ii]);
ForeignHost fhost = null;
try {
fhost = new ForeignHost(this, hosts[ii], sockets[ii], m_config.deadHostTimeout, listeningAddresses[ii], new PicoNetwork(sockets[ii]));
putForeignHost(hosts[ii], fhost);
} catch (java.io.IOException e) {
org.voltdb.VoltDB.crashLocalVoltDB("Failed to instantiate foreign host", true, e);
}
}
m_acceptor.accrue(jos);
/*
* Create the local agreement site. It knows that it is recovering because the number of
* prexisting sites is > 0
*/
SiteMailbox sm = new SiteMailbox(this, agreementHSId);
createMailbox(agreementHSId, sm);
m_agreementSite = new AgreementSite(agreementHSId, agreementSites, yourHostId, sm, new InetSocketAddress(m_config.zkInterface.split(":")[0], Integer.parseInt(m_config.zkInterface.split(":")[1])), m_config.backwardsTimeForgivenessWindow, m_failedHostsCallback);
/*
* Now that the agreement site mailbox has been created it is safe
* to enable read
*/
for (ForeignHost fh : m_foreignHosts.values()) {
fh.enableRead(VERBOTEN_THREADS);
}
m_agreementSite.start();
/*
* Do the usual thing of waiting for the agreement site
* to join the cluster and creating the client
*/
VERBOTEN_THREADS.addAll(m_network.getThreadIds());
VERBOTEN_THREADS.addAll(m_agreementSite.getThreadIds());
m_agreementSite.waitForRecovery();
m_zk = org.voltcore.zk.ZKUtil.getClient(m_config.zkInterface, 60 * 1000, VERBOTEN_THREADS);
if (m_zk == null) {
throw new Exception("Timed out trying to connect local ZooKeeper instance");
}
/*
* Publish the address of this node to ZK as seen by the leader
* Also allows waitForGroupJoin to know the number of nodes in the cluster
*/
HostInfo hostInfo;
if (m_config.internalInterface.isEmpty()) {
hostInfo = new HostInfo(new InetSocketAddress(m_joiner.m_reportedInternalInterface, m_config.internalPort).toString(), m_config.group, m_config.localSitesCount);
} else {
hostInfo = new HostInfo(new InetSocketAddress(m_config.internalInterface, m_config.internalPort).toString(), m_config.group, m_config.localSitesCount);
}
m_zk.create(CoreZK.hosts_host + getHostId(), hostInfo.toBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
Aggregations