Search in sources :

Example 1 with DRPCRequest

use of backtype.storm.generated.DRPCRequest in project storm by nathanmarz.

the class DRPCSpout method nextTuple.

@Override
public void nextTuple() {
    boolean gotRequest = false;
    if (_local_drpc_id == null) {
        for (int i = 0; i < _clients.size(); i++) {
            DRPCInvocationsClient client = _clients.get(i);
            try {
                DRPCRequest req = client.fetchRequest(_function);
                if (req.get_request_id().length() > 0) {
                    Map returnInfo = new HashMap();
                    returnInfo.put("id", req.get_request_id());
                    returnInfo.put("host", client.getHost());
                    returnInfo.put("port", client.getPort());
                    gotRequest = true;
                    _collector.emit(new Values(req.get_func_args(), JSONValue.toJSONString(returnInfo)), new DRPCMessageId(req.get_request_id(), i));
                    break;
                }
            } catch (TException e) {
                LOG.error("Failed to fetch DRPC result from DRPC server", e);
            }
        }
    } else {
        DistributedRPCInvocations.Iface drpc = (DistributedRPCInvocations.Iface) ServiceRegistry.getService(_local_drpc_id);
        if (drpc != null) {
            // can happen during shutdown of drpc while topology is still up
            try {
                DRPCRequest req = drpc.fetchRequest(_function);
                if (req.get_request_id().length() > 0) {
                    Map returnInfo = new HashMap();
                    returnInfo.put("id", req.get_request_id());
                    returnInfo.put("host", _local_drpc_id);
                    returnInfo.put("port", 0);
                    gotRequest = true;
                    _collector.emit(new Values(req.get_func_args(), JSONValue.toJSONString(returnInfo)), new DRPCMessageId(req.get_request_id(), 0));
                }
            } catch (TException e) {
                throw new RuntimeException(e);
            }
        }
    }
    if (!gotRequest) {
        Utils.sleep(1);
    }
}
Also used : TException(org.apache.thrift7.TException) HashMap(java.util.HashMap) Values(backtype.storm.tuple.Values) DistributedRPCInvocations(backtype.storm.generated.DistributedRPCInvocations) DRPCRequest(backtype.storm.generated.DRPCRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DRPCRequest

use of backtype.storm.generated.DRPCRequest in project jstorm by alibaba.

the class DRPCSpout method nextTuple.

@Override
public void nextTuple() {
    boolean gotRequest = false;
    if (_local_drpc_id == null) {
        int size = 0;
        synchronized (_clients) {
            // This will only ever grow, so no need to worry about falling off the end
            size = _clients.size();
        }
        for (int i = 0; i < size; i++) {
            DRPCInvocationsClient client;
            synchronized (_clients) {
                client = _clients.get(i);
            }
            if (!client.isConnected()) {
                continue;
            }
            try {
                DRPCRequest req = client.fetchRequest(_function);
                if (req.get_request_id().length() > 0) {
                    Map returnInfo = new HashMap();
                    returnInfo.put("id", req.get_request_id());
                    returnInfo.put("host", client.getHost());
                    returnInfo.put("port", client.getPort());
                    gotRequest = true;
                    _collector.emit(new Values(req.get_func_args(), JSONValue.toJSONString(returnInfo)), new DRPCMessageId(req.get_request_id(), i));
                    break;
                }
            } catch (AuthorizationException aze) {
                reconnect(client);
                LOG.error("Not authorized to fetch DRPC result from DRPC server", aze);
            } catch (TException e) {
                reconnect(client);
                LOG.error("Failed to fetch DRPC result from DRPC server", e);
            } catch (Exception e) {
                LOG.error("Failed to fetch DRPC result from DRPC server", e);
            }
        }
        JStormServerUtils.checkFutures(_futures);
    } else {
        DistributedRPCInvocations.Iface drpc = (DistributedRPCInvocations.Iface) ServiceRegistry.getService(_local_drpc_id);
        if (drpc != null) {
            // can happen during shutdown of drpc while topology is still up
            try {
                DRPCRequest req = drpc.fetchRequest(_function);
                if (req.get_request_id().length() > 0) {
                    Map returnInfo = new HashMap();
                    returnInfo.put("id", req.get_request_id());
                    returnInfo.put("host", _local_drpc_id);
                    returnInfo.put("port", 0);
                    gotRequest = true;
                    _collector.emit(new Values(req.get_func_args(), JSONValue.toJSONString(returnInfo)), new DRPCMessageId(req.get_request_id(), 0));
                }
            } catch (TException e) {
                throw new RuntimeException(e);
            }
        }
    }
    if (!gotRequest) {
        Utils.sleep(1);
    }
}
Also used : TException(org.apache.thrift.TException) AuthorizationException(backtype.storm.generated.AuthorizationException) Values(backtype.storm.tuple.Values) DistributedRPCInvocations(backtype.storm.generated.DistributedRPCInvocations) TException(org.apache.thrift.TException) AuthorizationException(backtype.storm.generated.AuthorizationException) DRPCRequest(backtype.storm.generated.DRPCRequest)

Example 3 with DRPCRequest

use of backtype.storm.generated.DRPCRequest in project jstorm by alibaba.

the class Drpc method fetchRequest.

@Override
public DRPCRequest fetchRequest(String functionName) throws TException {
    ConcurrentLinkedQueue<DRPCRequest> queue = acquireQueue(functionName);
    DRPCRequest req = queue.poll();
    if (req != null) {
        LOG.info("Fetched request for " + functionName + " at " + (System.currentTimeMillis()));
        return req;
    } else {
        return new DRPCRequest("", "");
    }
}
Also used : DRPCRequest(backtype.storm.generated.DRPCRequest)

Example 4 with DRPCRequest

use of backtype.storm.generated.DRPCRequest in project jstorm by alibaba.

the class Drpc method execute.

@Override
public String execute(String function, String args) throws TException {
    LOG.info("Received DRPC request for " + function + " " + args + " at " + (System.currentTimeMillis()));
    int idInc = this.ctr.incrementAndGet();
    int maxvalue = 1000000000;
    int newId = idInc % maxvalue;
    if (idInc != newId) {
        this.ctr.compareAndSet(idInc, newId);
    }
    String strId = String.valueOf(newId);
    Semaphore sem = new Semaphore(0);
    DRPCRequest req = new DRPCRequest(args, strId);
    this.idToStart.put(strId, TimeUtils.current_time_secs());
    this.idToSem.put(strId, sem);
    this.idToFunction.put(strId, function);
    this.idToRequest.put(strId, req);
    ConcurrentLinkedQueue<DRPCRequest> queue = acquireQueue(function);
    queue.add(req);
    LOG.info("Waiting for DRPC request for " + function + " " + args + " at " + (System.currentTimeMillis()));
    try {
        sem.acquire();
    } catch (InterruptedException e) {
        LOG.error("acquire fail ", e);
    }
    LOG.info("Acquired for DRPC request for " + function + " " + args + " at " + (System.currentTimeMillis()));
    Object result = this.idToResult.get(strId);
    if (!this.idToResult.containsKey(strId)) {
        // this request has timed out
        result = new DRPCExecutionException("Request timed out");
    }
    LOG.info("Returning for DRPC request for " + function + " " + args + " at " + (System.currentTimeMillis()));
    this.cleanup(strId);
    if (result instanceof DRPCExecutionException) {
        throw (DRPCExecutionException) result;
    }
    return String.valueOf(result);
}
Also used : DRPCRequest(backtype.storm.generated.DRPCRequest) DRPCExecutionException(backtype.storm.generated.DRPCExecutionException) Semaphore(java.util.concurrent.Semaphore)

Example 5 with DRPCRequest

use of backtype.storm.generated.DRPCRequest in project jstorm by alibaba.

the class ClearThread method run.

@Override
public void run() {
    for (Entry<String, Integer> e : drpcService.getIdToStart().entrySet()) {
        if (TimeUtils.time_delta(e.getValue()) > REQUEST_TIMEOUT_SECS) {
            String id = e.getKey();
            LOG.warn("DRPC request timed out, id: {} start at {}", id, e.getValue());
            ConcurrentLinkedQueue<DRPCRequest> queue = drpcService.acquireQueue(drpcService.getIdToFunction().get(id));
            // remove timeout request
            queue.remove(drpcService.getIdToRequest().get(id));
            Semaphore s = drpcService.getIdToSem().get(id);
            if (s != null) {
                s.release();
            }
            drpcService.cleanup(id);
            LOG.info("Clear request " + id);
        }
    }
    JStormUtils.sleepMs(10);
}
Also used : DRPCRequest(backtype.storm.generated.DRPCRequest) Semaphore(java.util.concurrent.Semaphore)

Aggregations

DRPCRequest (backtype.storm.generated.DRPCRequest)5 DistributedRPCInvocations (backtype.storm.generated.DistributedRPCInvocations)2 Values (backtype.storm.tuple.Values)2 Semaphore (java.util.concurrent.Semaphore)2 AuthorizationException (backtype.storm.generated.AuthorizationException)1 DRPCExecutionException (backtype.storm.generated.DRPCExecutionException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TException (org.apache.thrift.TException)1 TException (org.apache.thrift7.TException)1