Search in sources :

Example 1 with NodeRecord

use of tezc.core.record.NodeRecord in project tezcRaft by tezc.

the class Cluster method handleIncomingConnEvent.

/**
 * Handle incoming connection callback
 * @param conn incoming connection
 * @param req  connectRequest message
 */
public void handleIncomingConnEvent(Connection conn, ConnectReq req) {
    if (req.isClient()) {
        if (role != Role.LEADER) {
            conn.sendCancelEvent();
            return;
        }
        PeerNode node = clients.get(req.getName());
        if (node != null) {
            node.inherit(conn);
            conn.sendAcceptConnEvent(node);
            node.sendConnectResp(clusterRecord, node.getId(), node.getSequence(), true);
        } else {
            node = new PeerNode(worker, this, conn, null, new NodeRecord(req.getName(), ""), PeerNode.Type.CLIENT);
            conn.sendAcceptConnEvent(node);
            clients.put(req.getName(), node);
            RegisterCommand cmd = new RegisterCommand(req.getName());
            cmd.encode();
            createEntry(new CommandRequest(own, new ClientReq(false, 0, sequence++, false, cmd.getRaw())));
            flush();
        }
    } else {
        for (PeerNode node : nodes) {
            if (req.getName().equals(node.getRemote().name)) {
                node.inherit(conn);
                conn.sendAcceptConnEvent(node);
                node.sendConnectResp(clusterRecord, node.getId(), node.getSequence(), true);
                if (!activeNodes.contains(node)) {
                    activeNodes.add(node);
                }
                return;
            }
        }
        conn.sendCancelEvent();
    }
}
Also used : NodeRecord(tezc.core.record.NodeRecord) PeerNode(tezc.core.node.PeerNode) RegisterCommand(tezc.core.cluster.state.command.RegisterCommand)

Example 2 with NodeRecord

use of tezc.core.record.NodeRecord in project tezcRaft by tezc.

the class Connection method getNextTransport.

/**
 * Get next transport, if local is in same group with the remote, we use
 * TCP, otherwise TLS connection
 *
 * @return Transport record
 */
private TransportRecord getNextTransport() {
    NodeRecord local = node.getLocal();
    NodeRecord remote = node.getRemote();
    if (local.group == remote.group) {
        int nextIndex = transportIndex++ % remote.transports.size();
        return remote.transports.get(nextIndex);
    } else {
        int nextIndex = transportIndex++ % remote.secureTransports.size();
        return remote.secureTransports.get(nextIndex);
    }
}
Also used : NodeRecord(tezc.core.record.NodeRecord)

Example 3 with NodeRecord

use of tezc.core.record.NodeRecord in project tezcRaft by tezc.

the class App method start.

public void start(List<State> states, String name) throws IOException {
    TlsConfig tlsConfig = new TlsConfig(null, null, null, null, null, null, null, null, null, null);
    Config config = new Config();
    config.setLogLevel("DEBUG");
    config.setTlsConfig(tlsConfig);
    Tezc tezc = new Tezc("./cluster1/" + name, this, config);
    RaftCluster cluster = tezc.openCluster("cluster1", states);
    if (!cluster.isStarted()) {
        NodeRecord nodeRecord1 = new NodeRecord("node0", "group0");
        nodeRecord1.addTransport(new TransportRecord("tcp", "127.0.0.1", 9090));
        /*NodeRecord nodeRecord2 = new NodeRecord("node1", "group0");
            nodeRecord2.addTransport(new TransportRecord("tcp", "127.0.0.1", 9091));

            NodeRecord nodeRecord3 = new NodeRecord("node2", "group1");
            nodeRecord3.addTransport(new TransportRecord("tcp", "127.0.0.1", 9092));*/
        cluster.setLocal(Integer.parseInt(name));
        cluster.addNode(nodeRecord1);
    // cluster.addNode(nodeRecord2);
    // cluster.addNode(nodeRecord3);
    }
    cluster.run();
}
Also used : NodeRecord(tezc.core.record.NodeRecord) TransportRecord(tezc.core.record.TransportRecord) Config(tezc.core.Config) TlsConfig(tezc.base.transport.TlsConfig) RaftCluster(tezc.core.cluster.RaftCluster) TlsConfig(tezc.base.transport.TlsConfig) Tezc(tezc.Tezc)

Example 4 with NodeRecord

use of tezc.core.record.NodeRecord in project tezcRaft by tezc.

the class Cluster method start.

/**
 * Start cluster
 *
 * @throws RaftException on a missing configuration
 */
public void start() {
    electionTimer = new ElectionTimer(this, true, new Random().nextInt(150) + 500, worker.timestamp() + 500);
    heartBeatTimer = new HeartBeatTimer(this, true, 2000, worker.timestamp() + 150);
    startLog();
    if (!isStarted() && nodeId == -1) {
        worker.logInfo("Cannot start cluster ", clusterRecord);
        throw new RaftException("Local record is not set :" + clusterRecord);
    }
    local = clusterRecord.getRecord(nodeId);
    for (TransportRecord record : local.transports) {
        worker.listenAt(record);
    }
    for (TransportRecord record : local.secureTransports) {
        worker.listenAt(record);
    }
    for (NodeRecord record : clusterRecord.nodes) {
        if (record.id != local.id) {
            nodes.add(new PeerNode(worker, this, null, local, record, PeerNode.Type.PEER));
        }
    }
    startElectionTimer();
    active = true;
}
Also used : TransportRecord(tezc.core.record.TransportRecord) NodeRecord(tezc.core.record.NodeRecord) RaftException(tezc.base.exception.RaftException) PeerNode(tezc.core.node.PeerNode)

Aggregations

NodeRecord (tezc.core.record.NodeRecord)4 PeerNode (tezc.core.node.PeerNode)2 TransportRecord (tezc.core.record.TransportRecord)2 Tezc (tezc.Tezc)1 RaftException (tezc.base.exception.RaftException)1 TlsConfig (tezc.base.transport.TlsConfig)1 Config (tezc.core.Config)1 RaftCluster (tezc.core.cluster.RaftCluster)1 RegisterCommand (tezc.core.cluster.state.command.RegisterCommand)1