Example usage for org.apache.thrift TException TException

List of usage examples for org.apache.thrift TException TException

Introduction

In this page you can find the example usage for org.apache.thrift TException TException.

Prototype

public TException(Throwable cause) 

Source Link

Usage

From source file:SimonSaysHandler.java

License:Open Source License

protected void checkRegistered() throws TException {
    if (clientKey == null)
        throw new TException("client did not register");
}

From source file:SimonSaysHandler.java

License:Open Source License

public boolean chooseColor(Color colorChosen) throws TException {
    checkRegistered();//  w  ww  . ja  v  a  2s . c  o m

    if (!inTurn)
        throw new TException("chooseColor without startTurn");

    // are we still expecting a colour?
    if (inTurn && coloursReturned < turnColours.size()) {
        // did client give correct colour in sequence?
        if (colorChosen == turnColours.get(coloursReturned)) {
            ++coloursReturned;
            System.out.println("  client said correct colour: " + colorChosen);
            // is this turn finished?
            if (coloursReturned == turnColours.size()) {
                ++turns;
                System.out.println("turn complete");
            }
            return true;
        } else {
            System.out.println("  client said wrong colour: " + colorChosen);
        }
    }
    resetTurn();
    return false;
}

From source file:backtype.storm.drpc.DRPCInvocationsClient.java

License:Apache License

public void result(String id, String result) throws TException, AuthorizationException {
    DistributedRPCInvocations.Client c = client.get();
    try {/*  w  w  w  .j  av  a  2  s  .com*/
        if (c == null) {
            throw new TException("Client is not connected...");
        }
        c.result(id, result);
    } catch (TException e) {
        client.compareAndSet(c, null);
        throw e;
    }
}

From source file:backtype.storm.drpc.DRPCInvocationsClient.java

License:Apache License

public DRPCRequest fetchRequest(String func) throws TException, AuthorizationException {
    DistributedRPCInvocations.Client c = client.get();
    try {/*from  w  ww.j av a2  s.c o  m*/
        if (c == null) {
            throw new TException("Client is not connected...");
        }
        return c.fetchRequest(func);
    } catch (TException e) {
        client.compareAndSet(c, null);
        throw e;
    }
}

From source file:backtype.storm.drpc.DRPCInvocationsClient.java

License:Apache License

public void failRequest(String id) throws TException, AuthorizationException {
    DistributedRPCInvocations.Client c = client.get();
    try {//from  w  ww. j av  a  2  s. c  om
        if (c == null) {
            throw new TException("Client is not connected...");
        }
        c.failRequest(id);
    } catch (TException e) {
        client.compareAndSet(c, null);
        throw e;
    }
}

From source file:co.cask.tephra.distributed.AbstractClientProvider.java

License:Apache License

protected TransactionServiceThriftClient newClient(int timeout) throws TException {
    initialize();/*from w ww .j a v  a 2s  .  c o m*/
    String address;
    int port;

    if (endpointStrategy == null) {
        // if there is no discovery service, try to read host and port directly
        // from the configuration
        LOG.info("Reading address and port from configuration.");
        address = configuration.get(TxConstants.Service.CFG_DATA_TX_BIND_ADDRESS,
                TxConstants.Service.DEFAULT_DATA_TX_BIND_ADDRESS);
        port = configuration.getInt(TxConstants.Service.CFG_DATA_TX_BIND_PORT,
                TxConstants.Service.DEFAULT_DATA_TX_BIND_PORT);
        LOG.info("Service assumed at " + address + ":" + port);
    } else {
        Discoverable endpoint = endpointStrategy.pick();
        if (endpoint == null) {
            LOG.error("Unable to discover tx service.");
            throw new TException("Unable to discover tx service.");
        }
        address = endpoint.getSocketAddress().getHostName();
        port = endpoint.getSocketAddress().getPort();
        LOG.info("Service discovered at " + address + ":" + port);
    }

    // now we have an address and port, try to connect a client
    if (timeout < 0) {
        timeout = configuration.getInt(TxConstants.Service.CFG_DATA_TX_CLIENT_TIMEOUT,
                TxConstants.Service.DEFAULT_DATA_TX_CLIENT_TIMEOUT_MS);
    }
    LOG.info("Attempting to connect to tx service at " + address + ":" + port + " with timeout " + timeout
            + " ms.");
    // thrift transport layer
    TTransport transport = new TFramedTransport(new TSocket(address, port, timeout));
    try {
        transport.open();
    } catch (TTransportException e) {
        LOG.error("Unable to connect to tx service: " + e.getMessage());
        throw e;
    }
    // and create a thrift client
    TransactionServiceThriftClient newClient = new TransactionServiceThriftClient(transport);

    LOG.info("Connected to tx service at " + address + ":" + port);
    return newClient;
}

From source file:com.abiquo.aimstub.mock.AimServerMock.java

License:Open Source License

private Exception checkFailure(final String method) {
    List<Object[]> list = fails.get(method);
    if (list == null) {
        list = addToCache(method);//www . j a va  2s  . c o  m

        fails.put(method, list);
    }

    for (Object[] o : list) {
        Integer ratio = (Integer) o[1];
        String msg = (String) o[3];

        if (ratio > 0 && ratio >= random.nextInt(101)) {
            LOG.info("Error raised method " + method + " " + o[0] + " " + o[1] + " " + o[2] + " " + o[3]);
            String excName = (String) o[2];
            if ("VLanException".equals(excName)) {
                return new VLanException(msg);
            } else if ("RimpException".equals(excName)) {
                return new RimpException(msg);
            } else {
                return new TException(msg);
            }
        }
    }
    return null;
}

From source file:com.alibaba.jstorm.cluster.Common.java

License:Apache License

public static boolean confValidate(Map userConf, Map clusterConf) throws TException {
    Set<String> validateConfig = new HashSet<>();
    validateConfig.add(Config.STORM_LOCAL_DIR);

    if (userConf != null && clusterConf != null) {
        for (String config : validateConfig) {
            Object userObject = userConf.get(config);
            Object clusterObject = clusterConf.get(config);
            if (userObject != null && clusterObject != null && !userObject.equals(clusterObject)) {
                throw new TException("invalid userConf at " + config);
            }//w  w  w  .  ja  v a 2s. c om
        }
    }
    return true;
}

From source file:com.alibaba.jstorm.daemon.nimbus.ServiceHandler.java

License:Apache License

/**
 * Submit one Topology// w w w .j ava  2 s.co m
 *
 * @param topologyName        String: topology name
 * @param uploadedJarLocation String: already uploaded jar path
 * @param jsonConf            String: jsonConf serialize all toplogy configuration to
 *                            Json
 * @param topology            StormTopology: topology Object
 */
@SuppressWarnings("unchecked")
@Override
public void submitTopologyWithOpts(String topologyName, String uploadedJarLocation, String jsonConf,
        StormTopology topology, SubmitOptions options)
        throws AlreadyAliveException, InvalidTopologyException, TopologyAssignException, TException {
    LOG.info("Receive " + topologyName + ", uploadedJarLocation:" + uploadedJarLocation);
    long start = System.nanoTime();

    //check topologyname is valid
    if (!Common.charValidate(topologyName)) {
        throw new InvalidTopologyException(topologyName + " is not a valid topology name");
    }

    try {
        checkTopologyActive(data, topologyName, false);
    } catch (AlreadyAliveException e) {
        LOG.info(topologyName + " already exists ");
        throw e;
    } catch (Throwable e) {
        LOG.info("Failed to check whether topology is alive or not", e);
        throw new TException(e);
    }

    String topologyId = null;
    synchronized (data) {
        // avoid to the same topologys wered submmitted at the same time
        Set<String> pendingTopologys = data.getPendingSubmitTopoloygs().keySet();
        for (String cachTopologyId : pendingTopologys) {
            if (cachTopologyId.contains(topologyName + "-"))
                throw new AlreadyAliveException(topologyName + "  were submitted");
        }
        int counter = data.getSubmittedCount().incrementAndGet();
        topologyId = Common.topologyNameToId(topologyName, counter);
        data.getPendingSubmitTopoloygs().put(topologyId, null);
    }
    try {

        Map<Object, Object> serializedConf = (Map<Object, Object>) JStormUtils.from_json(jsonConf);
        if (serializedConf == null) {
            LOG.warn("Failed to serialized Configuration");
            throw new InvalidTopologyException("Failed to serialize topology configuration");
        }

        serializedConf.put(Config.TOPOLOGY_ID, topologyId);
        serializedConf.put(Config.TOPOLOGY_NAME, topologyName);

        Map<Object, Object> stormConf;

        stormConf = NimbusUtils.normalizeConf(conf, serializedConf, topology);
        LOG.info("Normalized configuration:" + stormConf);

        Map<Object, Object> totalStormConf = new HashMap<Object, Object>(conf);
        totalStormConf.putAll(stormConf);

        StormTopology normalizedTopology = NimbusUtils.normalizeTopology(stormConf, topology, true);

        // this validates the structure of the topology
        Common.validate_basic(normalizedTopology, totalStormConf, topologyId);
        // don't need generate real topology, so skip Common.system_topology
        // Common.system_topology(totalStormConf, topology);

        StormClusterState stormClusterState = data.getStormClusterState();

        double metricsSampleRate = ConfigExtension.getMetricSampleRate(stormConf);
        // create /local-dir/nimbus/topologyId/xxxx files
        setupStormCode(conf, topologyId, uploadedJarLocation, stormConf, normalizedTopology);

        // generate TaskInfo for every bolt or spout in ZK
        // /ZK/tasks/topoologyId/xxx
        setupZkTaskInfo(conf, topologyId, stormClusterState);

        // make assignments for a topology
        LOG.info("Submit for " + topologyName + " with conf " + serializedConf);
        makeAssignment(topologyName, topologyId, options.get_initial_status());

        // when make assignment for a topology,so remove the topologyid form
        // pendingSubmitTopologys
        data.getPendingSubmitTopoloygs().remove(topologyId);

        // push start event after startup
        StartTopologyEvent startEvent = new StartTopologyEvent();
        startEvent.clusterName = this.data.getClusterName();
        startEvent.topologyId = topologyId;
        startEvent.timestamp = System.currentTimeMillis();
        startEvent.sampleRate = metricsSampleRate;
        this.data.getMetricRunnable().pushEvent(startEvent);

        notifyTopologyActionListener(topologyName, "submitTopology");

    } catch (FailedAssignTopologyException e) {
        StringBuilder sb = new StringBuilder();
        sb.append("Fail to sumbit topology, Root cause:");
        if (e.getMessage() == null) {
            sb.append("submit timeout");
        } else {
            sb.append(e.getMessage());
        }

        sb.append("\n\n");
        sb.append("topologyId:" + topologyId);
        sb.append(", uploadedJarLocation:" + uploadedJarLocation + "\n");
        LOG.error(sb.toString(), e);
        data.getPendingSubmitTopoloygs().remove(topologyId);
        throw new TopologyAssignException(sb.toString());
    } catch (InvalidParameterException e) {
        StringBuilder sb = new StringBuilder();
        sb.append("Fail to sumbit topology ");
        sb.append(e.getMessage());
        sb.append(", cause:" + e.getCause());
        sb.append("\n\n");
        sb.append("topologyId:" + topologyId);
        sb.append(", uploadedJarLocation:" + uploadedJarLocation + "\n");
        LOG.error(sb.toString(), e);
        data.getPendingSubmitTopoloygs().remove(topologyId);
        throw new InvalidParameterException(sb.toString());
    } catch (InvalidTopologyException e) {
        LOG.error("Topology is invalid. " + e.get_msg());
        data.getPendingSubmitTopoloygs().remove(topologyId);
        throw e;
    } catch (Throwable e) {
        StringBuilder sb = new StringBuilder();
        sb.append("Fail to sumbit topology ");
        sb.append(e.getMessage());
        sb.append(", cause:" + e.getCause());
        sb.append("\n\n");
        sb.append("topologyId:" + topologyId);
        sb.append(", uploadedJarLocation:" + uploadedJarLocation + "\n");
        LOG.error(sb.toString(), e);
        data.getPendingSubmitTopoloygs().remove(topologyId);
        throw new TopologyAssignException(sb.toString());
    } finally {
        double spend = (System.nanoTime() - start) / TimeUtils.NS_PER_US;
        SimpleJStormMetric.updateNimbusHistogram("submitTopologyWithOpts", spend);
        LOG.info("submitTopologyWithOpts {} costs {}ms", topologyName, spend);
    }

}

From source file:com.alibaba.jstorm.daemon.nimbus.ServiceHandler.java

License:Apache License

@Override
public void killTopologyWithOpts(String topologyName, KillOptions options)
        throws TException, NotAliveException {
    try {// w  ww.ja v a2 s .  c  om
        checkTopologyActive(data, topologyName, true);

        String topologyId = getTopologyId(topologyName);

        Integer wait_amt = null;
        if (options.is_set_wait_secs()) {
            wait_amt = options.get_wait_secs();
        }
        NimbusUtils.transitionName(data, topologyName, true, StatusType.kill, wait_amt);

        Remove event = new Remove();
        event.topologyId = topologyId;
        data.getMetricRunnable().pushEvent(event);
        notifyTopologyActionListener(topologyName, "killTopology");
    } catch (NotAliveException e) {
        String errMsg = "KillTopology Error, no this topology " + topologyName;
        LOG.error(errMsg, e);
        throw new NotAliveException(errMsg);
    } catch (Exception e) {
        String errMsg = "Failed to kill topology " + topologyName;
        LOG.error(errMsg, e);
        throw new TException(errMsg);
    }

}