org.apache.thrift.TBaseProcessor Java Examples

The following examples show how to use org.apache.thrift.TBaseProcessor. 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: SyncEchoTestServer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void verifyServerTraces(PluginTestVerifier verifier) throws Exception {
    final InetSocketAddress socketAddress = super.environment.getServerAddress();
    final String address = SocketAddressUtils.getAddressFirst(socketAddress);
    verifier.verifyTraceCount(2);
    Method process = TBaseProcessor.class.getDeclaredMethod("process", TProtocol.class, TProtocol.class);
    verifier.verifyDiscreteTrace(
            // RootSpan - Thrift Server Invocation
            // refer to TBaseProcessorProcessInterceptor.finalizeSpan(...)
            root("THRIFT_SERVER", // ServiceType,
                    "Thrift Server Invocation", // Method
                    "com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo", // rpc
                    HostAndPort.toHostAndPortString(address, socketAddress.getPort()), // endPoint
                    address), // remoteAddress
            // SpanEvent - TBaseProcessor.process
            event("THRIFT_SERVER_INTERNAL", process));
}
 
Example #2
Source File: HttpEchoTestServer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void verifyTraces(PluginTestVerifier verifier) throws Exception {
    // ignore Jetty traces
    verifier.ignoreServiceType("JETTY", "JETTY_METHOD");

    // RootSpan - Jetty Servlet Process (ignore)

    // SpanEvent - (Jetty) Server.handle (ignore)

    // SpanEvent - TBaseProcessor.process
    // refer to TBaseProcessorProcessInterceptor.finalizeSpanEvent(...)
    Method processMethod = TBaseProcessor.class.getDeclaredMethod("process", TProtocol.class, TProtocol.class);
    ExpectedAnnotation thriftUrl = Expectations.annotation(
            "thrift.url", "com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo");
    ExpectedTrace tBaseProcessorProcessTrace = event(
            "THRIFT_SERVER_INTERNAL",
            processMethod,
            thriftUrl);

    verifier.verifyDiscreteTrace(tBaseProcessorProcessTrace);
}
 
Example #3
Source File: TBaseProcessorProcessInterceptor.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private String getMethodUri(Object target) {
    String methodUri = ThriftConstants.UNKNOWN_METHOD_URI;
    InterceptorScopeInvocation currentTransaction = this.scope.getCurrentInvocation();
    Object attachment = currentTransaction.getAttachment();
    if (attachment instanceof ThriftClientCallContext && target instanceof TBaseProcessor) {
        ThriftClientCallContext clientCallContext = (ThriftClientCallContext)attachment;
        String methodName = clientCallContext.getMethodName();
        methodUri = ThriftUtils.getProcessorNameAsUri((TBaseProcessor<?>)target);
        StringBuilder sb = new StringBuilder(methodUri);
        if (!methodUri.endsWith("/")) {
            sb.append("/");
        }
        sb.append(methodName);
        methodUri = sb.toString();
    }
    return methodUri;
}
 
Example #4
Source File: DemoServer.java    From nettythrift with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	int port = 8083;

	TBaseProcessor<?> processor = new TCalculator.Processor<TCalculator.Iface>(new CalcIfaceImpl());

	ThriftServerDef serverDef = ThriftServerDef.newBuilder().listen(port)//
			.withProcessor(processor)//
			.using(Executors.newCachedThreadPool())//
			.clientIdleTimeout(TimeUnit.SECONDS.toMillis(15)).build();
	final ServerBootstrap server = new ServerBootstrap(serverDef);
	// 启动 Server
	server.start();
}
 
Example #5
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 #6
Source File: ThriftServerDef.java    From nettythrift with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public ThriftServerDef(String name, int serverPort, int maxFrameSize, int maxConnections, int queuedResponseLimit,
		NettyProcessorFactory nettyProcessorFactory, ChannelHandler codecInstaller,
		@SuppressWarnings("rawtypes") TBaseProcessor processor, ExecutorService executor, long clientIdleTimeout,
		ProtocolFactorySelectorFactory protocolFactorySelectorFactory, HttpResourceHandler httpResourceHandler,
		boolean voidMethodDirectReturn, HttpHandlerFactory httpHandlerFactory, TrafficForecastFactory trafficForecastFac,
		LogicExecutionStatistics _logicExecutionStatistics) {
	super();
	this.name = name;
	this.serverPort = serverPort;
	this.maxFrameSize = maxFrameSize;
	this.maxConnections = maxConnections;
	this.queuedResponseLimit = queuedResponseLimit;
	this.processMap = processor.getProcessMapView();
	this.executor = executor;
	this.clientIdleTimeout = clientIdleTimeout;
	this.httpResourceHandler = httpResourceHandler;
	this.voidMethodDirectReturn = voidMethodDirectReturn;
	this.httpHandlerFactory = httpHandlerFactory == null ? new DefaultHttpHandlerFactory() : httpHandlerFactory;
	if (nettyProcessorFactory == null) {
		nettyProcessorFactory = new DefaultNettyProcessorFactory();
	}
	if (codecInstaller == null) {
		codecInstaller = new DefaultChannelInitializer<SocketChannel>(this);
	}
	this.nettyProcessor = nettyProcessorFactory.create(this);
	this.codecInstaller = codecInstaller;
	Object iface = null;
	try {
		Field f = TBaseProcessor.class.getDeclaredField("iface");
		f.setAccessible(true);
		iface = f.get(processor);
	} catch (Exception e) {
		e.printStackTrace();
	}
	Class<?> ifaceClass = null;
	{
		Class<?> clazz = iface.getClass();
		boolean find = false;
		while (!find && clazz != null) {
			Class<?>[] ifcs = clazz.getInterfaces();
			if (ifcs != null && ifcs.length > 0) {
				for (Class<?> c : ifcs) {
					if (c.getEnclosingClass() != null && c.getSimpleName().equals("Iface")) {
						ifaceClass = c;
						find = true;
						break;
					}
				}
			}
			clazz = clazz.getSuperclass();
		}
	}
	Map<String, Integer> inits = Collections.emptyMap();
	int[] voidMethodHashes = null;
	if (ifaceClass != null) {
		inits = new HashMap<>();
		Method[] ms = ifaceClass.getMethods();
		int len = 0;
		int[] hashes = new int[ms.length];
		for (Method m : ms) {
			if (m.getReturnType() == void.class) {
				hashes[len++] = m.getName().hashCode();
				inits.put(m.getName(), 128);
			} else {
				inits.put(m.getName(), 1024);
			}
		}
		if (len > 0) {
			if (len < ms.length) {
				voidMethodHashes = new int[len];
				System.arraycopy(hashes, 0, voidMethodHashes, 0, len);
			} else {
				voidMethodHashes = hashes;
			}
			Arrays.sort(voidMethodHashes);
		}
	}
	this.trafficForecast = trafficForecastFac != null ? trafficForecastFac.create(inits)
			: new DefaultTrafficForecastImpl(inits, Integer.parseInt(System.getProperty("trafficForecast.logmax", "100")));
	this.logicExecutionStatistics = _logicExecutionStatistics != null ? _logicExecutionStatistics
			: new DefaultLogicExecutionStatisticsImpl(Integer.parseInt(System.getProperty("ioexe.threshold", "5")),
					Integer.parseInt(System.getProperty("ioexe.logmax", "100")));
	this.voidMethodHashes = voidMethodHashes;
	this.iface = iface;
	protocolFactorySelector = protocolFactorySelectorFactory.createProtocolFactorySelector(ifaceClass);
}
 
Example #7
Source File: ThriftServerDefBuilderBase.java    From nettythrift with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public T withProcessor(@SuppressWarnings("rawtypes") TBaseProcessor processor) {
	this.processor = processor;
	return (T) this;
}
 
Example #8
Source File: ThriftUtils.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the name of the specified {@link org.apache.thrift.TBaseProcessor TBaseProcessor}
 * as uri to be used in Pinpoint.
 */
public static String getProcessorNameAsUri(TBaseProcessor<?> processor) {
    String actualProcessorName = processor.getClass().getName();
    return convertDotPathToUriPath(ThriftConstants.PROCESSOR_PATTERN.matcher(actualProcessorName).replaceAll("."));
}