Search in sources :

Example 11 with NodeNetwork

use of bftsmart.reconfiguration.views.NodeNetwork in project bftsmart by blockchain-jd-com.

the class ServiceReplica method receiveReadonlyMessage.

/**
 * This message delivers a readonly message, i.e., a message that was not
 * ordered to the replica and gather the reply to forward to the client
 *
 * @param message the request received from the delivery thread
 */
public final void receiveReadonlyMessage(TOMMessage message, MessageContext msgCtx) {
    byte[] response = null;
    // applications to log it or keep any proof.
    if (executor instanceof FIFOExecutable) {
        response = ((FIFOExecutable) executor).executeUnorderedFIFO(message.getContent(), msgCtx, message.getSender(), message.getOperationId());
    } else {
        if (message.getViewID() == serverViewController.getCurrentViewId()) {
            response = executor.executeUnordered(message.getContent(), msgCtx);
        } else if (message.getViewID() < serverViewController.getCurrentViewId()) {
            View view = serverViewController.getCurrentView();
            List<NodeNetwork> addressesTemp = new ArrayList<>();
            for (int i = 0; i < view.getProcesses().length; i++) {
                int cpuId = view.getProcesses()[i];
                NodeNetwork inetSocketAddress = view.getAddress(cpuId);
                if (inetSocketAddress.getHost().equals("0.0.0.0")) {
                    // proc docker env
                    String host = serverViewController.getStaticConf().getOuterHostConfig().getHost(cpuId);
                    NodeNetwork tempSocketAddress = new NodeNetwork(host, inetSocketAddress.getConsensusPort(), inetSocketAddress.getMonitorPort(), inetSocketAddress.isConsensusSecure(), inetSocketAddress.isMonitorSecure());
                    LOGGER.info("I am proc {}, tempSocketAddress.getAddress().getHostAddress() = {}", serverViewController.getStaticConf().getProcessId(), host);
                    addressesTemp.add(tempSocketAddress);
                } else {
                    LOGGER.info("I am proc {}, tempSocketAddress.getAddress().getHostAddress() = {}", serverViewController.getStaticConf().getProcessId(), inetSocketAddress);
                    addressesTemp.add(inetSocketAddress);
                }
            }
            View replyView = new View(view.getId(), view.getProcesses(), view.getF(), addressesTemp.toArray(new NodeNetwork[addressesTemp.size()]));
            response = TOMUtil.getBytes(replyView);
        }
    }
    if (message.getReqType() == TOMMessageType.UNORDERED_HASHED_REQUEST && message.getReplyServer() != this.id) {
        try {
            response = TOMUtil.computeHash(response);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    // Generate the messages to send back to the clients
    message.reply = new TOMMessage(id, message.getSession(), message.getSequence(), message.getOperationId(), response, null, serverViewController.getCurrentViewId(), message.getReqType());
    if (serverViewController.getStaticConf().getNumRepliers() > 0) {
        repMan.send(message);
    } else {
        cs.send(new int[] { message.getSender() }, message.reply);
    }
}
Also used : TOMMessage(bftsmart.tom.core.messages.TOMMessage) FIFOExecutable(bftsmart.tom.server.FIFOExecutable) ArrayList(java.util.ArrayList) List(java.util.List) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) View(bftsmart.reconfiguration.views.View) NodeNetwork(bftsmart.reconfiguration.views.NodeNetwork)

Example 12 with NodeNetwork

use of bftsmart.reconfiguration.views.NodeNetwork in project bftsmart by blockchain-jd-com.

the class ViewSyncTimer method updateView.

/**
 * 根据远端的信息更新本地视图
 *
 * @param remoteId
 * @param remoteView
 */
public void updateView(final int remoteId, final View remoteView) {
    /**
     * 处理逻辑:
     * 1、根据remoteId对应的地址,更新其对应网络配置;
     * 2、若本地视图中除自身及remoteId外的网络配置为空,而view中不为空则使用其结果
     */
    lock.lock();
    try {
        LOGGER.info("I am {}, start update view from [{}] !!!", processId, remoteId);
        View localView = tomLayer.controller.getCurrentView();
        Map<Integer, NodeNetwork> localViewAddresses = localView.getAddresses();
        // 获取远端的地址列表
        Map<Integer, NodeNetwork> remoteViewAddresses = remoteView.getAddresses();
        if (localView.getId() == remoteView.getId()) {
            // 对视图addresses的副本进行http端口更新
            for (Map.Entry<Integer, NodeNetwork> entry : remoteViewAddresses.entrySet()) {
                int nodeId = entry.getKey();
                NodeNetwork nodeNetwork = entry.getValue();
                if (checkNodeNetwork(nodeNetwork)) {
                    if (nodeId == remoteId) {
                        LOGGER.info("Receive remote[{}]'s view message, node[{}]'s network = [{}] !", remoteId, nodeId, nodeNetwork);
                        // 是远端节点的配置信息,则更新本地
                        localViewAddresses.put(nodeId, nodeNetwork);
                    } else if (nodeId != processId) {
                        // 非本地节点,则需要进行判断
                        NodeNetwork localNodeNetwork = localViewAddresses.get(nodeId);
                        if (localNodeNetwork == null) {
                            LOGGER.info("Receive remote[{}]'s view message, update node[{}]'s network = [{}] because local is NULL !", remoteId, nodeId, nodeNetwork);
                            // 若本地不存在该配置,则更新
                            localViewAddresses.put(nodeId, nodeNetwork);
                        } else {
                            // 判断本地配置是否合法
                            if (!checkNodeNetwork(localNodeNetwork)) {
                                LOGGER.info("Receive remote[{}]'s view message, update node[{}]'s network = [{}] because local is illegal !", remoteId, nodeId, nodeNetwork);
                                // 本地不合法,表示本地配置信息不合法,可以更新
                                localViewAddresses.put(nodeId, nodeNetwork);
                            }
                        }
                    }
                } else {
                    LOGGER.warn("Receive remote[{}]'s view message, node[{}]'s network = [{}] !", remoteId, nodeId, nodeNetwork);
                }
            }
            List<NodeNetwork> addressesTemp = new ArrayList<>();
            for (NodeNetwork nodeNetwork : localViewAddresses.values()) {
                addressesTemp.add(nodeNetwork);
            }
            View newView = new View(localView.getId(), localView.getProcesses(), localView.getF(), addressesTemp.toArray(new NodeNetwork[addressesTemp.size()]));
            // 通过reconfigureTo这个唯一的方法进行视图更新
            this.tomLayer.controller.reconfigureTo(newView);
        }
    } finally {
        lock.unlock();
    }
}
Also used : ArrayList(java.util.ArrayList) View(bftsmart.reconfiguration.views.View) Map(java.util.Map) NodeNetwork(bftsmart.reconfiguration.views.NodeNetwork)

Example 13 with NodeNetwork

use of bftsmart.reconfiguration.views.NodeNetwork in project jdchain-core by blockchain-jd-com.

the class BftsmartNodeServer method createConfig.

protected void createConfig() {
    setting = ((BftsmartServerSettings) serverSettings).getConsensusSettings();
    List<HostsConfig.Config> configList = new ArrayList<>();
    NodeSettings[] nodeSettingsArray = setting.getNodes();
    for (NodeSettings nodeSettings : nodeSettingsArray) {
        BftsmartNodeSettings node = (BftsmartNodeSettings) nodeSettings;
        if (node.getId() > MAX_SERVER_ID) {
            // 节点 ID 不允许超过最大值;
            throw new IllegalArgumentException(String.format("The id of node[%s | %s | %s] is large than MAX_SERVER_ID[%s]!", node.getId(), node.getAddress(), node.getNetworkAddress(), MAX_SERVER_ID));
        }
        LOGGER.info("createConfig node id = {}, port = {}", node.getId(), node.getNetworkAddress().getPort());
        configList.add(new HostsConfig.Config(node.getId(), node.getNetworkAddress().getHost(), node.getNetworkAddress().getPort(), -1, node.getNetworkAddress().isSecure(), false));
        consensusAddresses.put(node.getId(), new NodeNetwork(node.getNetworkAddress().getHost(), node.getNetworkAddress().getPort(), -1, node.getNetworkAddress().isSecure(), false));
    }
    // create HostsConfig instance based on consensus realm nodes
    hostsConfig = new HostsConfig(configList.toArray(new HostsConfig.Config[configList.size()]));
    systemConfig = PropertiesUtils.createProperties(setting.getSystemConfigs());
    return;
}
Also used : NodeSettings(com.jd.blockchain.consensus.NodeSettings) BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) HostsConfig(bftsmart.reconfiguration.util.HostsConfig) ArrayList(java.util.ArrayList) HostsConfig(bftsmart.reconfiguration.util.HostsConfig) NodeNetwork(bftsmart.reconfiguration.views.NodeNetwork)

Example 14 with NodeNetwork

use of bftsmart.reconfiguration.views.NodeNetwork in project jdchain-core by blockchain-jd-com.

the class BftsmartNodeServer method getOuterTopology.

private BftsmartTopology getOuterTopology() {
    View currView = this.replica.getReplicaContext().getCurrentView();
    int id = currView.getId();
    int f = currView.getF();
    int[] processes = currView.getProcesses();
    NodeNetwork[] addresses = new NodeNetwork[processes.length];
    for (int i = 0; i < processes.length; i++) {
        int pid = processes[i];
        if (serverId == pid) {
            addresses[i] = new NodeNetwork(getTomConfig().getHost(pid), getTomConfig().getPort(pid), getTomConfig().getMonitorPort(pid), getTomConfig().isSecure(pid), getTomConfig().isMonitorSecure(pid));
        } else {
            addresses[i] = currView.getAddress(pid);
        }
    }
    View returnView = new View(id, processes, f, addresses);
    this.outerTopology = new BftsmartTopology(returnView);
    return outerTopology;
}
Also used : BftsmartTopology(com.jd.blockchain.consensus.bftsmart.BftsmartTopology) View(bftsmart.reconfiguration.views.View) NodeNetwork(bftsmart.reconfiguration.views.NodeNetwork)

Example 15 with NodeNetwork

use of bftsmart.reconfiguration.views.NodeNetwork in project jdchain-core by blockchain-jd-com.

the class BftsmartNodeServer method appExecuteUnordered.

@Override
public byte[] appExecuteUnordered(byte[] bytes, MessageContext messageContext) {
    if (Arrays.equals(MonitorService.LOAD_MONITOR, bytes)) {
        try {
            // 获取加载管理端口的信息
            View currView = this.replica.getReplicaContext().getCurrentView();
            Map<Integer, NodeNetwork> addresses = currView.getAddresses();
            TreeMap<Integer, NodeNetwork> tree = new TreeMap<>();
            for (Map.Entry<Integer, NodeNetwork> entry : addresses.entrySet()) {
                tree.put(entry.getKey(), entry.getValue());
            }
            Collection<NodeNetwork> nodeNetworks = tree.values();
            NodeNetworkAddresses nodeNetworkAddresses = nodeNetworkAddresses(new ArrayList<>(nodeNetworks));
            return BinaryProtocol.encode(nodeNetworkAddresses, NodeNetworkAddresses.class);
        } catch (Exception e) {
            // 返回启动异常的错误
            throw new IllegalStateException(String.format("BftSmartNodeServer[%s] is still not started !", serverId));
        }
    }
    return messageHandle.processUnordered(bytes).get();
}
Also used : NodeNetworkAddresses(com.jd.blockchain.consensus.NodeNetworkAddresses) TreeMap(java.util.TreeMap) View(bftsmart.reconfiguration.views.View) BlockRollbackException(com.jd.blockchain.ledger.BlockRollbackException) LRUMap(org.apache.commons.collections4.map.LRUMap) Map(java.util.Map) TreeMap(java.util.TreeMap) NodeNetwork(bftsmart.reconfiguration.views.NodeNetwork)

Aggregations

NodeNetwork (bftsmart.reconfiguration.views.NodeNetwork)15 View (bftsmart.reconfiguration.views.View)10 ArrayList (java.util.ArrayList)6 HostsConfig (bftsmart.reconfiguration.util.HostsConfig)4 TOMConfiguration (bftsmart.reconfiguration.util.TOMConfiguration)3 MemoryBasedViewStorage (bftsmart.reconfiguration.views.MemoryBasedViewStorage)2 TOMMessage (bftsmart.tom.core.messages.TOMMessage)2 FIFOExecutable (bftsmart.tom.server.FIFOExecutable)2 BftsmartNodeSettings (com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)2 BftsmartTopology (com.jd.blockchain.consensus.bftsmart.BftsmartTopology)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Map (java.util.Map)2 Properties (java.util.Properties)2 StringTokenizer (java.util.StringTokenizer)2 PreComputeBatchExecutable (bftsmart.consensus.app.PreComputeBatchExecutable)1 ConsensusMessage (bftsmart.consensus.messages.ConsensusMessage)1 ApplicationState (bftsmart.statemanagement.ApplicationState)1 AsynchServiceProxy (bftsmart.tom.AsynchServiceProxy)1 ServiceProxy (bftsmart.tom.ServiceProxy)1 DeliveryThread (bftsmart.tom.core.DeliveryThread)1