Example usage for org.apache.hadoop.ipc RPC waitForProxy

List of usage examples for org.apache.hadoop.ipc RPC waitForProxy

Introduction

In this page you can find the example usage for org.apache.hadoop.ipc RPC waitForProxy.

Prototype

public static <T> T waitForProxy(Class<T> protocol, long clientVersion, InetSocketAddress addr,
        Configuration conf) throws IOException 

Source Link

Document

Get a proxy connection to a remote server

Usage

From source file:com.chinamobile.bcbsp.bspcontroller.BSPController.java

License:Apache License

/**
 * A WorkerManager registers with its status to BSPController when startup,
 * which will update WorkerManagers cache.
 * @param status/*from   w  ww. j ava2 s .  co m*/
 *        to be updated in cache.
 * @return true if registering successfully; false if fail.
 */
@Override
public boolean register(WorkerManagerStatus status) throws IOException {
    if (null == status) {
        LOG.error("No worker server status.");
        throw new NullPointerException("No worker server status.");
    }
    Throwable e = null;
    try {
        WorkerManagerProtocol wc = (WorkerManagerProtocol) RPC.waitForProxy(WorkerManagerProtocol.class,
                BSPRPCProtocolVersion.versionID, resolveWorkerAddress(status.getRpcServer()), this.conf);
        if (null == wc) {
            LOG.warn("Fail to create Worker client at host " + status.getWorkerManagerName());
            return false;
        }
        if (grayWorkerManagers.containsKey(status)) {
            grayWorkerManagers.remove(status);
            whiteWorkerManagers.put(status, wc);
        } else if (!whiteWorkerManagers.containsKey(status)) {
            LOG.info(status.getWorkerManagerName() + " is registered to the cluster "
                    + "and the maxClusterStaffs changes from " + this.maxClusterStaffs + " to "
                    + (this.maxClusterStaffs + status.getMaxStaffsCount()));
            maxClusterStaffs += status.getMaxStaffsCount();
            whiteWorkerManagers.put(status, wc);
        }
    } catch (UnsupportedOperationException u) {
        e = u;
    } catch (ClassCastException c) {
        e = c;
    } catch (NullPointerException n) {
        e = n;
    } catch (IllegalArgumentException i) {
        e = i;
    } catch (Exception ex) {
        e = ex;
    }
    if (e != null) {
        LOG.error("Fail to register WorkerManager " + status.getWorkerManagerName(), e);
        return false;
    }
    return true;
}

From source file:com.chinamobile.bcbsp.comm.CommunicatorNew.java

License:Apache License

/** Initialize the RPC protocol handle. */
private void initRPCHandler() throws IOException {
    Iterator<Integer> keyItr = this.partitionToWorkerManagerNameAndPort.keySet().iterator();
    int partitionId = 0;
    while (keyItr.hasNext()) {
        partitionId = keyItr.next();// w w  w.  ja  v a  2 s .  c  o m
        String addrPort = getDstWorkerManagerNameAndPort(partitionId);
        String[] tmp = addrPort.split(":");
        String hostname = tmp[0];
        int portNum = Integer.parseInt(tmp[1]);
        for (int i = 0; i < MetaDataOfMessage.HASH_NUMBER; i++) {
            RPCCommunicationProtocol rPCHandler = (RPCCommunicationProtocol) RPC.waitForProxy(
                    RPCCommunicationProtocol.class, RPCCommunicationProtocol.protocolVersion,
                    new InetSocketAddress(hostname, portNum), new Configuration());
            this.rpcCommHandlers.put(PartitionRule.getDestPartitionBucketId(partitionId, i), rPCHandler);
        }
    }
}

From source file:com.chinamobile.bcbsp.comm.RPCSingleSendSlave.java

License:Apache License

/** Run method of Thread. */
public void run() {
    while (true) {
        while (this.idle) {
            if (this.completed) {
                return;
            }// w w  w.j  a  v a  2 s . c  om
            if (this.noMoreMessagesFlag) {
                this.superStepCounter++;
                this.noMoreMessagesFlag = false;
            }
            try {
                Thread.sleep(this.sleepTime);
            } catch (InterruptedException e) {
                LOG.error("[RPCSingleSendSlave] to " + this.hostNameAndPort + " has been interrupted for ", e);
                return;
            }
        }
        if (this.hostNameAndPort == null) {
            LOG.error("Destination hostname is null.");
            return;
        }
        if (this.messageQueue == null) {
            LOG.error("Message queue for ProducerTool is null.");
            return;
        }
        this.messageCount = 0;
        this.connectTime = 0;
        this.sendTime = 0;
        while (true) {
            if (this.reconnectCount == RPCSingleSendSlave.RECONNECTTHRESHOLD) {
                break;
            }
            try {
                if (this.newHostNameAndPort) { // Should create new connection
                    if (senderProtocol != null) {
                        LOG.info("Sender RPC protocol is not null.");
                    } else {
                        LOG.info("Sender RPC protocol is null and it will instanced now.");
                    }
                    long start = System.currentTimeMillis();
                    String[] tmp = this.hostNameAndPort.split(":");
                    String hostname = tmp[0];
                    int portNum = Integer.parseInt(tmp[1]);
                    this.senderProtocol = (RPCCommunicationProtocol) RPC.waitForProxy(
                            RPCCommunicationProtocol.class, RPCCommunicationProtocol.protocolVersion,
                            new InetSocketAddress(hostname, portNum), new Configuration());
                    this.connectTime += (System.currentTimeMillis() - start);
                    this.newHostNameAndPort = false;
                }
                // Start sending messages
                this.sendPacked();
                this.idle = true;
                break;
            } catch (Exception e) {
                this.reconnectCount++;
                if (this.reconnectCount == 1) {
                    LOG.error("[SingRPCSendSlave] to " + this.hostNameAndPort + " caught: ", e);
                }
                LOG.info("[SingRPCSendSlave] to " + this.hostNameAndPort + " is reconnecting for "
                        + this.reconnectCount + "th time.");
                LOG.info("---------------- Memory Info ------------------");
                MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
                MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
                long used = memoryUsage.getUsed();
                long committed = memoryUsage.getCommitted();
                LOG.info("[JVM Memory used] = " + used / MB_SIZE + "MB");
                LOG.info("[JVM Memory committed] = " + committed / MB_SIZE + "MB");
                LOG.info("-----------------------------------------------");
                try {
                    Thread.sleep(this.sleepTime);
                } catch (InterruptedException e1) {
                    LOG.error("[SingRPCSendSlave] caught: ", e1);
                }
            }
        }
        LOG.info("[SingRPCSendSlave] to " + this.hostNameAndPort + " has sent " + this.messageCount
                + " messages totally! (with " + this.messageQueue.size() + " messages lost!)");
        this.sender.addConnectTime(this.connectTime);
        /* Clock */
        this.sender.addSendTime(this.sendTime);
        if (this.reconnectCount == RPCSingleSendSlave.RECONNECTTHRESHOLD) {
            LOG.info("[ProducerTool] to " + this.hostNameAndPort + " has reconnected for " + this.reconnectCount
                    + " times but failed!");
            this.isFailed = true;
            break;
        }
    }
}

From source file:com.chinamobile.bcbsp.workermanager.WorkerManager.java

License:Apache License

/**
 * Initialize workerManager./*from w w w  . j av  a  2s . co m*/
 */
@SuppressWarnings("static-access")
public synchronized void initialize() throws IOException {
    if (this.conf.get(Constants.BC_BSP_WORKERMANAGER_RPC_HOST) != null) {
        this.workerManagerName = conf.get(Constants.BC_BSP_WORKERMANAGER_RPC_HOST);
    }
    if (this.workerManagerName == null) {
        this.workerManagerName = DNS.getDefaultHost(conf.get("bsp.dns.interface", "default"),
                conf.get("bsp.dns.nameserver", "default"));
    }
    // check local disk
    checkLocalDirs(conf.getStrings(Constants.BC_BSP_LOCAL_DIRECTORY));
    deleteLocalFiles("workerManager");
    this.workerFaultList = new ArrayList<Fault>();
    this.reportStaffStatusList = new ArrayList<StaffStatus>();
    this.runningStaffs = new ConcurrentHashMap<StaffAttemptID, StaffInProgress>();
    this.finishedStaffs = new ConcurrentHashMap<StaffAttemptID, StaffInProgress>();
    this.runningJobs = new ConcurrentHashMap<BSPJobID, RunningJob>();
    this.finishedJobs = new ConcurrentHashMap<BSPJobID, RunningJob>();
    this.runningJobtoWorkerAgent = new ConcurrentHashMap<BSPJobID, WorkerAgentForJob>();
    this.reprotStaffsMap = new ConcurrentHashMap<StaffAttemptID, StaffInProgress>();
    this.conf.set(Constants.BC_BSP_WORKERAGENT_HOST, this.workerManagerName);
    this.conf.set(Constants.BC_BSP_WORKERMANAGER_RPC_HOST, this.workerManagerName);
    this.maxStaffsCount = conf.getInt(Constants.BC_BSP_WORKERMANAGER_MAXSTAFFS, 1);
    WorkerManager.HEART_BEAT_INTERVAL = conf.getInt(Constants.HEART_BEAT_INTERVAL, 1000);
    LOG.info("The max number of staffs is : " + this.maxStaffsCount);
    int rpcPort = -1;
    String rpcAddr = null;
    if (!this.initialized) {
        rpcAddr = conf.get(Constants.BC_BSP_WORKERMANAGER_RPC_HOST,
                Constants.DEFAULT_BC_BSP_WORKERMANAGER_RPC_HOST);
        rpcPort = conf.getInt(Constants.BC_BSP_WORKERMANAGER_RPC_PORT, 5000);
        if (-1 == rpcPort || null == rpcAddr) {
            throw new IllegalArgumentException("Error rpc address " + rpcAddr + " port" + rpcPort);
        }
        this.workerServer = RPC.getServer(this, rpcAddr, rpcPort, conf);
        this.workerServer.start();
        this.rpcServer = rpcAddr + ":" + rpcPort;
        LOG.info("Worker rpc server --> " + rpcServer);
    }
    String address = conf.get(Constants.BC_BSP_WORKERMANAGER_REPORT_ADDRESS);
    InetSocketAddress socAddr = NetUtils.createSocketAddr(address);
    String bindAddress = socAddr.getHostName();
    int tmpPort = socAddr.getPort();
    // RPC initialization
    this.staffReportServer = RPC.getServer(this, bindAddress, tmpPort, 10, false, this.conf);
    this.staffReportServer.start();
    // http server
    InetAddress addr = InetAddress.getLocalHost();
    String ipSlave = addr.getHostAddress().toString();
    winfoPort = conf.getInt("bcbsp.http.winfoserver.port", 40027);
    winfoServer = new HttpServer("bcbsp", ipSlave, winfoPort, true, conf);
    winfoServer.setAttribute("WorkerManager", this);
    LOG.info("prot: 40027");
    LOG.info("ljn test : controllerClient before start ");
    winfoServer.start();
    LOG.info("server has started");
    LOG.info("ljn test : controllerClient before register ");
    // get the assigned address
    this.staffReportAddress = staffReportServer.getListenerAddress();
    LOG.info("WorkerManager up at: " + this.staffReportAddress);
    DistributedCache.purgeCache(this.conf);
    LOG.info("ljn test : DistributedCache ");
    LOG.info("ljn test : bspControllerAddr " + bspControllerAddr);
    LOG.info("ljn test : BSPRPCProtocolVersion.versionID " + BSPRPCProtocolVersion.versionID);
    // establish the communication link to bsp master
    try {
        this.controllerClient = (ControllerProtocol) RPC.waitForProxy(ControllerProtocol.class,
                BSPRPCProtocolVersion.versionID, bspControllerAddr, conf);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    LOG.info("ljn test : controllerClient controllerClient start ");
    // establish the communication link to standby bsp master
    if ("ha".equals(conf.get(Constants.BC_BSP_HA_FLAG, ""))) {
        this.standbyControllerClient = (ControllerProtocol) RPC.waitForProxy(ControllerProtocol.class,
                BSPRPCProtocolVersion.versionID, this.standbyControllerAddr, conf);
    }
    LOG.info("bspControllerAddr = " + bspControllerAddr + " standbyControllerAddr = " + standbyControllerAddr);
    LOG.info("ljn test : controllerClient before register ");
    // enroll in bsp controller
    if (-1 == rpcPort || null == rpcAddr) {
        throw new IllegalArgumentException("Error rpc address " + rpcAddr + " port" + rpcPort);
    }
    LOG.info("ljn test : controllerClient before lsManager.start ");
    this.lsManager.start();
    LOG.info("ljn test : controllerClient before lsManager.start  over");
    workerMangerStatus = new WorkerManagerStatus(workerManagerName, cloneAndResetRunningStaffStatuses(),
            maxStaffsCount, currentStaffsCount, finishedStaffsCount, failures, this.rpcServer, workerFaultList);
    this.workerMangerStatus.setHost(bindAddress);
    this.workerMangerStatus.setHttpPort(this.staffReportAddress.toString());
    this.workerMangerStatus.setLocalIp(ipSlave);
    LOG.info("ljn test : controllerClient before register ");
    if (!this.controllerClient.register(workerMangerStatus)) {
        LOG.error("There is a problem in establishing communication" + " link with BSPController");
        throw new IOException("There is a problem in establishing" + " communication link with BSPController.");
    } else {
        LOG.info("have registed to bsp master");
    }
    if ("ha".equals(conf.get(Constants.BC_BSP_HA_FLAG, ""))) {
        if (!this.standbyControllerClient.register(workerMangerStatus)) {
            LOG.error("There is a problem in establishing communication" + " link with BSPController");
            throw new IOException(
                    "There is a problem in establishing" + " communication link with BSPController.");
        } else {
            LOG.info("have registed to standby bsp master");
        }
    }
    this.running = true;
    this.initialized = true;
    LOG.info("ljn test : controllerClient after register ");
}

From source file:common.DataNode.java

License:Apache License

/**
 * Create the DataNode given a configuration and an array of dataDirs.
 * 'dataDirs' is where the blocks are stored.
 *///from w w  w  . j av  a  2s.  c o  m
DataNode(final Configuration conf, final AbstractList<File> dataDirs) throws IOException {
    this(conf, dataDirs, (DatanodeProtocol) RPC.waitForProxy(DatanodeProtocol.class, DatanodeProtocol.versionID,
            NameNode.getAddress(conf), conf));
}

From source file:org.goldenorb.jet.OrbPartitionMember.java

License:Apache License

/**
 * //from   w ww. j  a  v a 2s.  c  o  m
 * @param  OrbConfiguration orbConf
 */
public void initProxy(OrbConfiguration orbConf) throws IOException {
    if (client == null) {
        InetSocketAddress addr = new InetSocketAddress(hostname, port);
        client = (OrbPartitionCommunicationProtocol) RPC.waitForProxy(OrbPartitionCommunicationProtocol.class,
                OrbPartitionCommunicationProtocol.versionID, addr, orbConf);
    }
}

From source file:org.goldenorb.OrbPartitionManager.java

License:Apache License

/**
 * /*w  w  w  .  j  a  v  a2  s.co m*/
 */
public void stop(String jobNumber) {
    Configuration rpcConf = new Configuration();
    if (processesByJob.containsKey(jobNumber)) {
        for (M p : processesByJob.get(jobNumber)) {
            int rpcPort = conf.getOrbPartitionManagementBaseport() + p.getProcessNum() + 100;
            InetSocketAddress addr = new InetSocketAddress(ipAddress, rpcPort);
            try {
                partitionClient = (OrbPartitionManagerProtocol) RPC.waitForProxy(
                        OrbPartitionManagerProtocol.class, OrbPartitionManagerProtocol.versionID, addr,
                        rpcConf);

                int partitionStatus = partitionClient.stop();
                if (partitionStatus > 0) {
                    // wait some time before trying to stop it again
                    wait(5000);
                    if (partitionClient.isRunning()) {
                        p.kill();
                    }
                } else if (partitionStatus < 0) {
                    p.kill();
                }

            } catch (IOException e) {
                logger.error(e.getMessage());
            } catch (InterruptedException e) {
                logger.error(e.getMessage());
            }
        }
    }
}

From source file:org.goldenorb.types.message.SampleBooleanMessageTest.java

License:Apache License

/**
 * //from  w  w w.  jav  a  2s.  c  o  m
 */
@SuppressWarnings("unchecked")
@Before
public void startServer() throws IOException {
    server = new RPCServer<BooleanMessage, BooleanWritable>(SERVER_PORT);
    server.start();
    Configuration conf = new Configuration();
    InetSocketAddress addr = new InetSocketAddress("localhost", SERVER_PORT);
    if (client == null)
        client = (RPCProtocol<BooleanMessage, BooleanWritable>) RPC.waitForProxy(RPCProtocol.class,
                RPCProtocol.versionID, addr, conf);
}

From source file:org.goldenorb.types.message.SampleDoubleMessageTest.java

License:Apache License

/**
 * //from ww  w.  j  av a2s.co m
 */
@SuppressWarnings("unchecked")
@Before
public void startServer() throws IOException {
    server = new RPCServer<DoubleMessage, DoubleWritable>(SERVER_PORT);
    server.start();
    Configuration conf = new Configuration();
    InetSocketAddress addr = new InetSocketAddress("localhost", SERVER_PORT);
    if (client == null)
        client = (RPCProtocol<DoubleMessage, DoubleWritable>) RPC.waitForProxy(RPCProtocol.class,
                RPCProtocol.versionID, addr, conf);
}

From source file:org.goldenorb.types.message.SampleFloatMessageTest.java

License:Apache License

/**
 * /*from   www. j  a v  a2s. co m*/
 */
@SuppressWarnings("unchecked")
@Before
public void startServer() throws IOException {
    server = new RPCServer<FloatMessage, FloatWritable>(SERVER_PORT);
    server.start();
    Configuration conf = new Configuration();
    InetSocketAddress addr = new InetSocketAddress("localhost", SERVER_PORT);
    if (client == null)
        client = (RPCProtocol<FloatMessage, FloatWritable>) RPC.waitForProxy(RPCProtocol.class,
                RPCProtocol.versionID, addr, conf);
}