org.apache.thrift.ProcessFunction Java Examples

The following examples show how to use org.apache.thrift.ProcessFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocatorServiceImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public final boolean process(final TProtocol in, final TProtocol out)
    throws TException {
  final TMessage msg = in.readMessageBegin();
  final ProcessFunction<LocatorServiceImpl, ?> fn = this.fnMap
      .get(msg.name);
  if (fn != null) {
    fn.process(msg.seqid, in, out, this.inst);
    // terminate connection on receiving closeConnection
    // direct class comparison should be the fastest way
    return fn.getClass() != LocatorService.Processor.closeConnection.class;
  }
  else {
    TProtocolUtil.skip(in, TType.STRUCT);
    in.readMessageEnd();
    TApplicationException x = new TApplicationException(
        TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"
            + msg.name + "'");
    out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION,
        msg.seqid));
    x.write(out);
    out.writeMessageEnd();
    out.getTransport().flush();
    return true;
  }
}
 
Example #2
Source File: ThriftServiceMetadata.java    From armeria with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private void registerFunction(Class<?> iface, String name,
                              Object func, @Nullable Object implementation) {
    if (functions.containsKey(name)) {
        logger.warn("duplicate Thrift method name: " + name);
        return;
    }

    try {
        final ThriftFunction f;
        if (func instanceof ProcessFunction) {
            f = new ThriftFunction(iface, (ProcessFunction) func, implementation);
        } else {
            f = new ThriftFunction(iface, (AsyncProcessFunction) func, implementation);
        }
        functions.put(name, f);
    } catch (Exception e) {
        throw new IllegalArgumentException("failed to retrieve function metadata: " +
                                           iface.getName() + '.' + name + "()", e);
    }
}
 
Example #3
Source File: LocatorServiceImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@Override
public final boolean process(final TProtocol in, final TProtocol out)
    throws TException {
  final TMessage msg = in.readMessageBegin();
  final ProcessFunction<LocatorServiceImpl, ?> fn = this.fnMap
      .get(msg.name);
  if (fn != null) {
    fn.process(msg.seqid, in, out, this.inst);
    // terminate connection on receiving closeConnection
    // direct class comparison should be the fastest way
    return fn.getClass() != LocatorService.Processor.closeConnection.class;
  }
  else {
    TProtocolUtil.skip(in, TType.STRUCT);
    in.readMessageEnd();
    TApplicationException x = new TApplicationException(
        TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"
            + msg.name + "'");
    out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION,
        msg.seqid));
    x.write(out);
    out.writeMessageEnd();
    out.getTransport().flush();
    return true;
  }
}
 
Example #4
Source File: SessionDriver.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs the map from function name -> handler.
 * @param calls The call queue.
 * @return The map.
 */
private static Map<String, ProcessFunction> getProcessMap(
  LinkedBlockingQueue<TBase> calls) {
  Map<String, ProcessFunction> processMap =
    new HashMap<String, ProcessFunction>();
  processMap.put("grantResource", new grantResourceHandler(calls));
  processMap.put("revokeResource", new revokeResourceHandler(calls));
  processMap.put("processDeadNode", new processDeadNodeHandler(calls));
  return processMap;
}
 
Example #5
Source File: ProcessFunctionProcessInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    // process(int seqid, TProtocol iprot, TProtocol oprot, I iface)
    if (args.length != 4) {
        return;
    }
    String methodName = ThriftConstants.UNKNOWN_METHOD_NAME;
    if (target instanceof ProcessFunction) {
        ProcessFunction<?, ?> processFunction = (ProcessFunction<?, ?>)target;
        methodName = processFunction.getMethodName();
    }
    ThriftClientCallContext clientCallContext = new ThriftClientCallContext(methodName);
    InterceptorScopeInvocation currentTransaction = this.scope.getCurrentInvocation();
    currentTransaction.setAttachment(clientCallContext);
    // Set server marker - server handlers may create a client to call another Thrift server.
    // When this happens, TProtocol interceptors for clients are triggered since technically they're still within THRIFT_SERVER_SCOPE.
    // We set the marker inside server's input protocol to safeguard against such cases.
    Object iprot = args[1];
    // With the addition of TProtocolDecorator, iprot may actually be a wrapper around the actual input protocol
    Object rootInputProtocol = getRootInputProtocol(iprot);
    if (validateInputProtocol(rootInputProtocol)) {
        ((ServerMarkerFlagFieldAccessor)rootInputProtocol)._$PINPOINT$_setServerMarkerFlag(true);
    }
}
 
Example #6
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean process(final TProtocol in, final TProtocol out)
    throws TException {
  final TMessage msg = in.readMessageBegin();
  final ProcessFunction<GFXDServiceImpl, ?> fn = this.fnMap.get(msg.name);
  if (fn != null) {
    fn.process(msg.seqid, in, out, this.inst);
    // terminate connection on receiving closeConnection
    // direct class comparison should be the fastest way
    // TODO: SW: also need to clean up connection artifacts in the case of
    // client connection failure (ConnectionListener does get a notification
    // but how to tie the socket/connectionNumber to the connectionID?)
    return fn.getClass() != GFXDService.Processor.closeConnection.class;
  }
  else {
    TProtocolUtil.skip(in, TType.STRUCT);
    in.readMessageEnd();
    TApplicationException x = new TApplicationException(
        TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"
            + msg.name + "'");
    out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION,
        msg.seqid));
    x.write(out);
    out.writeMessageEnd();
    out.getTransport().flush();
    return true;
  }
}
 
Example #7
Source File: ThriftServiceMetadata.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Map<String, ProcessFunction<?, ?>> getThriftProcessMap(@Nullable Object service,
                                                                      Class<?> iface) {
    final String name = iface.getName();
    if (!name.endsWith("$Iface")) {
        return null;
    }

    final String processorName = name.substring(0, name.length() - 5) + "Processor";
    try {
        final Class<?> processorClass = Class.forName(processorName, false, iface.getClassLoader());
        if (!TBaseProcessor.class.isAssignableFrom(processorClass)) {
            return null;
        }

        final Constructor<?> processorConstructor = processorClass.getConstructor(iface);

        @SuppressWarnings("rawtypes")
        final TBaseProcessor processor = (TBaseProcessor) processorConstructor.newInstance(service);

        @SuppressWarnings("unchecked")
        final Map<String, ProcessFunction<?, ?>> processMap =
                (Map<String, ProcessFunction<?, ?>>) processor.getProcessMapView();

        return processMap;
    } catch (Exception e) {
        logger.debug("Failed to retrieve the process map from: {}", iface, e);
        return null;
    }
}
 
Example #8
Source File: ThriftServiceMetadata.java    From armeria with Apache License 2.0 5 votes vote down vote up
private Set<Class<?>> init(@Nullable Object implementation, Iterable<Class<?>> candidateInterfaces) {

        // Build the map of method names and their corresponding process functions.
        // If a method is defined multiple times, we take the first definition
        final Set<Class<?>> interfaces = new HashSet<>();

        for (Class<?> iface : candidateInterfaces) {
            final Map<String, AsyncProcessFunction<?, ?, ?>> asyncProcessMap;
            asyncProcessMap = getThriftAsyncProcessMap(implementation, iface);
            if (asyncProcessMap != null) {
                asyncProcessMap.forEach(
                        (name, func) -> registerFunction(iface, name, func, implementation));
                interfaces.add(iface);
            }

            final Map<String, ProcessFunction<?, ?>> processMap;
            processMap = getThriftProcessMap(implementation, iface);
            if (processMap != null) {
                processMap.forEach(
                        (name, func) -> registerFunction(iface, name, func, implementation));
                interfaces.add(iface);
            }
        }

        if (functions.isEmpty()) {
            if (implementation != null) {
                throw new IllegalArgumentException('\'' + implementation.getClass().getName() +
                                                   "' is not a Thrift service implementation.");
            } else {
                throw new IllegalArgumentException("not a Thrift service interface: " + candidateInterfaces);
            }
        }

        return Collections.unmodifiableSet(interfaces);
    }
 
Example #9
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean process(final TProtocol in, final TProtocol out)
    throws TException {
  final TMessage msg = in.readMessageBegin();
  final ProcessFunction<GFXDServiceImpl, ?> fn = this.fnMap.get(msg.name);
  if (fn != null) {
    fn.process(msg.seqid, in, out, this.inst);
    // terminate connection on receiving closeConnection
    // direct class comparison should be the fastest way
    // TODO: SW: also need to clean up connection artifacts in the case of
    // client connection failure (ConnectionListener does get a notification
    // but how to tie the socket/connectionNumber to the connectionID?)
    return fn.getClass() != GFXDService.Processor.closeConnection.class;
  }
  else {
    TProtocolUtil.skip(in, TType.STRUCT);
    in.readMessageEnd();
    TApplicationException x = new TApplicationException(
        TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"
            + msg.name + "'");
    out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION,
        msg.seqid));
    x.write(out);
    out.writeMessageEnd();
    out.getTransport().flush();
    return true;
  }
}
 
Example #10
Source File: ThriftCallService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void invokeSynchronously(
        ServiceRequestContext ctx, Object impl,
        ThriftFunction func, TBase<?, ?> args, CompletableRpcResponse reply) {

    final ProcessFunction<Object, TBase<?, ?>> f = func.syncFunc();
    ctx.blockingTaskExecutor().execute(() -> {
        if (reply.isDone()) {
            // Closed already most likely due to timeout.
            return;
        }

        try {
            if (func.isOneWay()) {
                reply.complete(null);
                f.getResult(impl, args);
            } else {
                final TBase<?, ?> result = f.getResult(impl, args);
                reply.complete(func.getResult(result));
            }
        } catch (Throwable t) {
            if (func.isOneWay()) {
                reply.complete(null);
                logOneWayFunctionFailure(ctx, t);
            } else {
                reply.completeExceptionally(t);
            }
        }
    });
}
 
Example #11
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
ThriftFunction(Class<?> serviceType, ProcessFunction<?, ?> func) throws Exception {
    this(serviceType, func.getMethodName(), func, Type.SYNC,
         getArgFields(func), getResult(func), getDeclaredExceptions(func), null);
}
 
Example #12
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static Class<?>[] getDeclaredExceptions(ProcessFunction<?, ?> func) {
    return getDeclaredExceptions0(Type.SYNC, func.getClass(), func.getMethodName());
}
 
Example #13
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static TFieldIdEnum[] getArgFields(ProcessFunction<?, ?> func) {
    return getArgFields0(Type.SYNC, func.getClass(), func.getMethodName());
}
 
Example #14
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static TBase<?, ?> getArgs(ProcessFunction<?, ?> func) {
    return getArgs0(Type.SYNC, func.getClass(), func.getMethodName());
}
 
Example #15
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Nullable
private static TBase<?, ?> getResult(ProcessFunction<?, ?> func) {
    return getResult0(Type.SYNC, func.getClass(), func.getMethodName());
}
 
Example #16
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
ThriftFunction(Class<?> serviceType, ProcessFunction<?, ?> func,
               @Nullable Object implementation) throws Exception {
    this(serviceType, func.getMethodName(), func, Type.SYNC,
         getArgFields(func), getResult(func), getDeclaredExceptions(func), implementation);
}
 
Example #17
Source File: TMockProcessor.java    From thrift-mock with Apache License 2.0 4 votes vote down vote up
public TMockProcessor(Map<String, ProcessFunction> processFunctionMap) {
  super(new MockIface() {}, processFunctionMap);
}
 
Example #18
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public Processor(GFXDServiceImpl inst) {
  super(inst);
  this.inst = inst;
  this.fnMap = new HashMap<String, ProcessFunction<GFXDServiceImpl, ?>>(
      super.getProcessMapView());
}
 
Example #19
Source File: LocatorServiceImpl.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public Processor(LocatorServiceImpl inst) {
  super(inst);
  this.inst = inst;
  this.fnMap = new HashMap<String, ProcessFunction<LocatorServiceImpl, ?>>(
      super.getProcessMapView());
}
 
Example #20
Source File: ServiceProcessor.java    From ikasoa with MIT License 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private static Map<String, ProcessFunction<Service, ? extends TBase>> getProcessMap(
		Map<String, ProcessFunction<Service, ? extends TBase>> processMap) {
	processMap.put(FUNCTION_NAME, new GetProcessFunction());
	return processMap;
}
 
Example #21
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public Processor(GFXDServiceImpl inst) {
  super(inst);
  this.inst = inst;
  this.fnMap = new HashMap<String, ProcessFunction<GFXDServiceImpl, ?>>(
      super.getProcessMapView());
}
 
Example #22
Source File: LocatorServiceImpl.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public Processor(LocatorServiceImpl inst) {
  super(inst);
  this.inst = inst;
  this.fnMap = new HashMap<String, ProcessFunction<LocatorServiceImpl, ?>>(
      super.getProcessMapView());
}
 
Example #23
Source File: ServerConfig.java    From thrift-mock with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, ProcessFunction> getProcessMap() {
  return processMap;
}
 
Example #24
Source File: ThriftFunction.java    From armeria with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link ProcessFunction}.
 *
 * @throws ClassCastException if this function is asynchronous
 */
@SuppressWarnings("unchecked")
public ProcessFunction<Object, TBase<?, ?>> syncFunc() {
    return (ProcessFunction<Object, TBase<?, ?>>) func;
}