public static Constructor<?> getClientConstructor(Class<?> svcInterface) { String client = svcInterface.getName().indexOf("Async") > 0 ? ASYNC_CLIENT_NAME : CLIENT_NAME; Class<?>[] args = svcInterface.getName().indexOf("Async") > 0 ? new Class[]{TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class} : new Class[]{TProtocol.class}; Class<?> clientClass = getThriftServiceInnerClassOrNull(svcInterface.getEnclosingClass(), client, false); if (clientClass == null) { throw new ThriftRuntimeException("the client class is null"); } Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(clientClass, args); if (constructor == null) { throw new ThriftRuntimeException("the clientClass constructor is null"); } return constructor; }
public ApacheThriftMethodInvoker( ListeningExecutorService executorService, ListeningScheduledExecutorService delayService, TTransportFactory transportFactory, TProtocolFactory protocolFactory, Duration connectTimeout, Duration requestTimeout, Optional<HostAndPort> socksProxy, Optional<SSLContext> sslContext) { this.executorService = requireNonNull(executorService, "executorService is null"); this.delayService = requireNonNull(delayService, "delayService is null"); this.transportFactory = requireNonNull(transportFactory, "transportFactory is null"); this.protocolFactory = requireNonNull(protocolFactory, "protocolFactory is null"); this.connectTimeoutMillis = Ints.saturatedCast(requireNonNull(connectTimeout, "connectTimeout is null").toMillis()); this.requestTimeoutMillis = Ints.saturatedCast(requireNonNull(requestTimeout, "requestTimeout is null").toMillis()); this.socksProxy = requireNonNull(socksProxy, "socksProxy is null"); this.sslContext = requireNonNull(sslContext, "sslContext is null"); }
public ThriftTestingSource(String handlerName, int port, String protocol) throws Exception { TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(new InetSocketAddress("0.0.0.0", port)); ThriftSourceProtocol.Iface handler = getHandler(handlerName); TProtocolFactory transportProtocolFactory = null; if (protocol != null && protocol == ThriftRpcClient.BINARY_PROTOCOL) { transportProtocolFactory = new TBinaryProtocol.Factory(); } else { transportProtocolFactory = new TCompactProtocol.Factory(); } server = new THsHaServer(new THsHaServer.Args(serverTransport).processor( new ThriftSourceProtocol.Processor(handler)).protocolFactory( transportProtocolFactory)); Executors.newSingleThreadExecutor().submit(new Runnable() { @Override public void run() { server.serve(); } }); }
private TProtocolFactory getProtocolFactory() { if (protocol.equals(BINARY_PROTOCOL)) { logger.info("Using TBinaryProtocol"); return new TBinaryProtocol.Factory(); } else { logger.info("Using TCompactProtocol"); return new TCompactProtocol.Factory(); } }
private static TServer getTThreadPoolServer(TProtocolFactory protocolFactory, TProcessor processor, TTransportFactory transportFactory, int workerThreads, InetSocketAddress inetSocketAddress, int backlog, int clientTimeout) throws TTransportException { TServerTransport serverTransport = new TServerSocket( new TServerSocket.ServerSocketTransportArgs(). bindAddr(inetSocketAddress).backlog(backlog). clientTimeout(clientTimeout)); log.info("starting HBase ThreadPool Thrift server on " + inetSocketAddress.toString()); TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport); serverArgs.processor(processor); serverArgs.transportFactory(transportFactory); serverArgs.protocolFactory(protocolFactory); if (workerThreads > 0) { serverArgs.maxWorkerThreads(workerThreads); } return new TThreadPoolServer(serverArgs); }
@Override @SuppressWarnings("unchecked") public <X extends TAsyncClient> X getClient(final Class<X> clazz) { return (X) super.clients.computeIfAbsent(ClassNameUtils.getOuterClassName(clazz), (className) -> { TProtocolFactory protocolFactory = (TProtocolFactory) tTransport -> { TProtocol protocol = new TBinaryProtocol(tTransport); return new TMultiplexedProtocol(protocol, className); }; try { return clazz.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class) .newInstance(protocolFactory, this.clientManager, this.transport); } catch (Throwable e) { if (e instanceof UnresolvedAddressException) { this.isOpen = false; } return null; } }); }
public void start(CountDownLatch latch, int port) { try { TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(port); //异步IO,需要使用TFramedTransport,它将分块缓存读取。 TTransportFactory transportFactory = new TFramedTransport.Factory(); //使用高密度二进制协议 TProtocolFactory proFactory = new TBinaryProtocol.Factory(); //发布多个服务 TMultiplexedProcessor processor = new TMultiplexedProcessor(); processor.registerProcessor(ClassNameUtils.getClassName(Hello.class), new Hello.Processor<>(new HelloServer())); TServer server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args(serverTransport) .transportFactory(transportFactory) .protocolFactory(proFactory) .processor(processor) ); System.out.println("Starting the hello server..."); latch.countDown(); server.serve(); } catch (Exception e) { e.printStackTrace(); } }
@Override protected void messageReceived(ChannelHandlerContext ctx, ThriftMessage message) throws Exception { ByteBuf buffer = message.getContent(); logger.debug("msg.content:: {}", buffer); try { TNettyTransport transport = new TNettyTransport(ctx.channel(), buffer); TProtocolFactory protocolFactory = message.getProtocolFactory(); TProtocol protocol = protocolFactory.getProtocol(transport); serverDef.nettyProcessor.process(ctx, protocol, protocol, new DefaultWriterListener(message, transport, ctx, serverDef)); } catch (Throwable ex) { int refCount = buffer.refCnt(); if (refCount > 0) { buffer.release(refCount); } throw ex; } }
public void run() { ThriftTestHandler handler = new ThriftTestHandler(System.out); ThriftTest.Processor<ThriftTestHandler> processor = new ThriftTest.Processor<>(handler); TProtocolFactory factory = getProtocolFactory(); serverTransport = getServerTransport(); server = startServer(processor, factory); final CountDownLatch latch = new CountDownLatch(1); serverThread = new Thread(() -> { latch.countDown(); server.serve(); }); serverThread.start(); try { latch.await(100, TimeUnit.MILLISECONDS); } catch (InterruptedException ignored) { // continue } }
/** * Returns the {@link TProtocolFactory} for the specified {@link SerializationFormat}. * * @throws IllegalArgumentException if the specified {@link SerializationFormat} is not for Thrift */ public static TProtocolFactory get(SerializationFormat serializationFormat) { requireNonNull(serializationFormat, "serializationFormat"); if (serializationFormat == ThriftSerializationFormats.BINARY) { return BINARY; } if (serializationFormat == ThriftSerializationFormats.COMPACT) { return COMPACT; } if (serializationFormat == ThriftSerializationFormats.JSON) { return JSON; } if (serializationFormat == ThriftSerializationFormats.TEXT) { return TEXT; } throw new IllegalArgumentException("non-Thrift serializationFormat: " + serializationFormat); }
/** * Returns the {@link SerializationFormat} for the specified {@link TProtocolFactory}. * * @throws IllegalArgumentException if the specified {@link TProtocolFactory} is not known by this class */ public static SerializationFormat toSerializationFormat(TProtocolFactory protoFactory) { requireNonNull(protoFactory, "protoFactory"); if (protoFactory instanceof TBinaryProtocol.Factory) { return ThriftSerializationFormats.BINARY; } else if (protoFactory instanceof TCompactProtocol.Factory) { return ThriftSerializationFormats.COMPACT; } else if (protoFactory instanceof TJSONProtocol.Factory) { return ThriftSerializationFormats.JSON; } else if (protoFactory instanceof TTextProtocol.Factory) { return ThriftSerializationFormats.TEXT; } else { throw new IllegalArgumentException( "unsupported TProtocolFactory: " + protoFactory.getClass().getName()); } }
private static TServer getTHsHaServer(TProtocolFactory protocolFactory, TProcessor processor, TTransportFactory transportFactory, int workerThreads, int maxCallQueueSize, InetSocketAddress inetSocketAddress, ThriftMetrics metrics) throws TTransportException { TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(inetSocketAddress); log.info("starting HBase HsHA Thrift server on " + inetSocketAddress.toString()); THsHaServer.Args serverArgs = new THsHaServer.Args(serverTransport); if (workerThreads > 0) { // Could support the min & max threads, avoiding to preserve existing functionality. serverArgs.minWorkerThreads(workerThreads).maxWorkerThreads(workerThreads); } ExecutorService executorService = createExecutor( workerThreads, maxCallQueueSize, metrics); serverArgs.executorService(executorService); serverArgs.processor(processor); serverArgs.transportFactory(transportFactory); serverArgs.protocolFactory(protocolFactory); return new THsHaServer(serverArgs); }
private static TServer getTThreadedSelectorServer(TProtocolFactory protocolFactory, TProcessor processor, TTransportFactory transportFactory, int workerThreads, int selectorThreads, int maxCallQueueSize, InetSocketAddress inetSocketAddress, ThriftMetrics metrics) throws TTransportException { TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(inetSocketAddress); log.info("starting HBase ThreadedSelector Thrift server on " + inetSocketAddress.toString()); TThreadedSelectorServer.Args serverArgs = new TThreadedSelectorServer.Args(serverTransport); if (workerThreads > 0) { serverArgs.workerThreads(workerThreads); } if (selectorThreads > 0) { serverArgs.selectorThreads(selectorThreads); } ExecutorService executorService = createExecutor( workerThreads, maxCallQueueSize, metrics); serverArgs.executorService(executorService); serverArgs.processor(processor); serverArgs.transportFactory(transportFactory); serverArgs.protocolFactory(protocolFactory); return new TThreadedSelectorServer(serverArgs); }
private TServer getServer(int workerThreads, int selectorThreads, int maxCallQueueSize, int readTimeout, int backlog, boolean nonblocking, boolean hsha, boolean selector, ThriftMetrics metrics, TProtocolFactory protocolFactory, TProcessor processor, TTransportFactory transportFactory, InetSocketAddress inetSocketAddress) throws TTransportException { TServer server; if (nonblocking) { server = getTNonBlockingServer(protocolFactory, processor, transportFactory, inetSocketAddress); } else if (hsha) { server = getTHsHaServer(protocolFactory, processor, transportFactory, workerThreads, maxCallQueueSize, inetSocketAddress, metrics); } else if (selector) { server = getTThreadedSelectorServer(protocolFactory, processor, transportFactory, workerThreads, selectorThreads, maxCallQueueSize, inetSocketAddress, metrics); } else { server = getTThreadPoolServer(protocolFactory, processor, transportFactory, workerThreads, inetSocketAddress, backlog, readTimeout, metrics); } return server; }
public static TServer createThreadedSelectorServer(TProcessorFactory processorFactory, int port, int clientTimeoutMillisecs, int maxFrameSize, long maxReadBufferSize) throws TTransportException { int numThreads = Math.max(2, Runtime.getRuntime().availableProcessors()); int selectorThreads = Math.max(2, Runtime.getRuntime().availableProcessors() / 2); TNonblockingServerTransport transport = new TNonblockingServerSocket(port, clientTimeoutMillisecs); TProtocolFactory protocolFactory = new TBinaryProtocol.Factory(); TTransportFactory transportFactory = new TFramedTransport.Factory(maxFrameSize); TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(transport) .processorFactory(processorFactory).protocolFactory(protocolFactory) .transportFactory(transportFactory).workerThreads(numThreads) .acceptPolicy(AcceptPolicy.FAIR_ACCEPT).acceptQueueSizePerThread(10000) .selectorThreads(selectorThreads); args.maxReadBufferBytes = maxReadBufferSize; TThreadedSelectorServer server = new TThreadedSelectorServer(args); return server; }
private <T extends TBase<?,?>> Path createFile(T... tObjs) throws IOException, InterruptedException, TException { final Path fileToCreate = new Path("target/test/TestThriftToParquetFileWriter/"+tObjs[0].getClass()+".parquet"); LOG.info("File created: {}", fileToCreate.toString()); Configuration conf = new Configuration(); final FileSystem fs = fileToCreate.getFileSystem(conf); if (fs.exists(fileToCreate)) { fs.delete(fileToCreate, true); } TProtocolFactory protocolFactory = new TCompactProtocol.Factory(); TaskAttemptID taskId = new TaskAttemptID("local", 0, true, 0, 0); ThriftToParquetFileWriter w = new ThriftToParquetFileWriter(fileToCreate, ContextUtil.newTaskAttemptContext(conf, taskId), protocolFactory, (Class<? extends TBase<?, ?>>) tObjs[0].getClass()); for(T tObj:tObjs) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(baos)); tObj.write(protocol); w.write(new BytesWritable(baos.toByteArray())); } w.close(); return fileToCreate; }
public ThriftMessageParser(SecorConfig config) throws InstantiationException, IllegalAccessException, ClassNotFoundException { super(config); TProtocolFactory protocolFactory = null; String protocolName = mConfig.getThriftProtocolClass(); if (StringUtils.isNotEmpty(protocolName)) { String factoryClassName = protocolName.concat("$Factory"); protocolFactory = ((Class<? extends TProtocolFactory>) Class.forName(factoryClassName)).newInstance(); } else protocolFactory = new TBinaryProtocol.Factory(); mDeserializer = new TDeserializer(protocolFactory); mThriftPath = new ThriftPath(mConfig.getMessageTimestampName(),(short) mConfig.getMessageTimestampId()); mTimestampType = mConfig.getMessageTimestampType(); }
@Test public void testThrftSerialization() throws TException { HMSPathsDumper serDe = genHMSPathsDumper(); long t1 = System.currentTimeMillis(); TPathsDump pathsDump = serDe.createPathsDump(); TProtocolFactory protoFactory = useCompact ? new TCompactProtocol.Factory( ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT, ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT) : new TBinaryProtocol.Factory(true, true, ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT, ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT); byte[] ser = new TSerializer(protoFactory).serialize(pathsDump); long serTime = System.currentTimeMillis() - t1; System.out.println("Serialization Time: " + serTime + ", " + ser.length); t1 = System.currentTimeMillis(); TPathsDump tPathsDump = new TPathsDump(); new TDeserializer(protoFactory).deserialize(tPathsDump, ser); HMSPaths fromDump = serDe.initializeFromDump(tPathsDump); System.out.println("Deserialization Time: " + (System.currentTimeMillis() - t1)); Assert.assertEquals(new HashSet<String>(Arrays.asList("db9.tbl999")), fromDump.findAuthzObject(new String[]{"user", "hive", "warehouse", "db9", "tbl999"}, false)); Assert.assertEquals(new HashSet<String>(Arrays.asList("db9.tbl999")), fromDump.findAuthzObject(new String[]{"user", "hive", "warehouse", "db9", "tbl999", "part99"}, false)); }
public static TProtocolFactory getProtocolFactory(ThriftProtocol protocol) { // TODO(ruibm): Check whether the Factories are thread safe so we can static initialize // them just once. switch (protocol) { case JSON: return new TJSONProtocol.Factory(); case COMPACT: return new TCompactProtocol.Factory(); case BINARY: return new TBinaryProtocol.Factory(); default: throw new IllegalArgumentException( String.format("Unknown ThriftProtocol [%s].", protocol.toString())); } }
@Override public void startServer(final TProcessor processor, final TProtocolFactory protoFactory) throws Exception { serverThread = new Thread() { public void run() { try { // Transport TServerSocket socket = new TServerSocket(PORT); TTransportFactory factory = new TSaslServerTransport.Factory( WRAPPED_MECHANISM, SERVICE, HOST, WRAPPED_PROPS, new TestSaslCallbackHandler(PASSWORD)); server = new TSimpleServer(new Args(socket).processor(processor).transportFactory(factory).protocolFactory(protoFactory)); // Run it LOGGER.debug("Starting the server on port {}", PORT); server.serve(); } catch (Exception e) { e.printStackTrace(); fail(); } } }; serverThread.start(); Thread.sleep(1000); }
@Override public void startServer(final TProcessor processor, final TProtocolFactory protoFactory) throws Exception { serverThread = new Thread() { public void run() { try { TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(PORT); server = new TSimpleServer(new Args(serverTransport).processor(processor)); server.serve(); } catch (TTransportException e) { e.printStackTrace(); assert false; } } }; serverThread.start(); Thread.sleep(1000); }
@Override public void startServer(final TProcessor processor, final TProtocolFactory protoFactory) throws Exception { serverThread = new Thread() { public void run() { try { // Transport TNonblockingServerSocket tServerSocket = new TNonblockingServerSocket(PORT); server = getServer(processor, tServerSocket, protoFactory); // Run it System.out.println("Starting the server on port " + PORT + "..."); server.serve(); } catch (Exception e) { e.printStackTrace(); fail(); } } }; serverThread.start(); Thread.sleep(1000); }
private static int testProcessor(TProcessor processor, List<ToIntFunction<HostAndPort>> clients) throws Exception { try (TServerSocket serverTransport = new TServerSocket(0)) { TProtocolFactory protocolFactory = new Factory(); TTransportFactory transportFactory = new TFramedTransport.Factory(); TServer server = new TSimpleServer(new Args(serverTransport) .protocolFactory(protocolFactory) .transportFactory(transportFactory) .processor(processor)); Thread serverThread = new Thread(server::serve); try { serverThread.start(); int localPort = serverTransport.getServerSocket().getLocalPort(); HostAndPort address = HostAndPort.fromParts("localhost", localPort); int sum = 0; for (ToIntFunction<HostAndPort> client : clients) { sum += client.applyAsInt(address); } return sum; } finally { server.stop(); serverThread.interrupt(); } } }
private static int testProcessor(TProcessor processor, List<ToIntFunction<HostAndPort>> clients) throws Exception { try (TServerSocket serverTransport = new TServerSocket(0)) { TProtocolFactory protocolFactory = new TBinaryProtocol.Factory(); TTransportFactory transportFactory = new TFramedTransport.Factory(); TServer server = new TSimpleServer(new Args(serverTransport) .protocolFactory(protocolFactory) .transportFactory(transportFactory) .processor(processor)); Thread serverThread = new Thread(server::serve); try { serverThread.start(); int localPort = serverTransport.getServerSocket().getLocalPort(); HostAndPort address = HostAndPort.fromParts("localhost", localPort); int sum = 0; for (ToIntFunction<HostAndPort> client : clients) { sum += client.applyAsInt(address); } return sum; } finally { server.stop(); serverThread.interrupt(); } } }
@Bean Servlet thrift(ThriftCodecManager thriftCodecManager, TProtocolFactory protocolFactory, TCalculatorService calculatorService) { ThriftServiceProcessor processor = new ThriftServiceProcessor(thriftCodecManager, Arrays.<ThriftEventHandler>asList(), calculatorService); return new TServlet( NiftyProcessorAdapters.processorToTProcessor(processor), protocolFactory, protocolFactory ); }
public ThriftTestingSource(String handlerName, int port, String protocol, String keystore, String keystorePassword, String keyManagerType, String keystoreType) throws Exception { TSSLTransportFactory.TSSLTransportParameters params = new TSSLTransportFactory.TSSLTransportParameters(); params.setKeyStore(keystore, keystorePassword, keyManagerType, keystoreType); TServerSocket serverTransport = TSSLTransportFactory.getServerSocket( port, 10000, InetAddress.getByName("0.0.0.0"), params); ThriftSourceProtocol.Iface handler = getHandler(handlerName); Class serverClass = Class.forName("org.apache.thrift" + ".server.TThreadPoolServer"); Class argsClass = Class.forName("org.apache.thrift.server" + ".TThreadPoolServer$Args"); TServer.AbstractServerArgs args = (TServer.AbstractServerArgs) argsClass .getConstructor(TServerTransport.class) .newInstance(serverTransport); Method m = argsClass.getDeclaredMethod("maxWorkerThreads", int.class); m.invoke(args, Integer.MAX_VALUE); TProtocolFactory transportProtocolFactory = null; if (protocol != null && protocol == ThriftRpcClient.BINARY_PROTOCOL) { transportProtocolFactory = new TBinaryProtocol.Factory(); } else { transportProtocolFactory = new TCompactProtocol.Factory(); } args.protocolFactory(transportProtocolFactory); args.inputTransportFactory(new TFastFramedTransport.Factory()); args.outputTransportFactory(new TFastFramedTransport.Factory()); args.processor(new ThriftSourceProtocol.Processor<ThriftSourceProtocol.Iface>(handler)); server = (TServer) serverClass.getConstructor(argsClass).newInstance(args); Executors.newSingleThreadExecutor().submit(new Runnable() { @Override public void run() { server.serve(); } }); }
private M3Reporter(Builder builder) { try { // Builder verifies non-null, non-empty socketAddresses SocketAddress[] socketAddresses = builder.socketAddresses; TProtocolFactory protocolFactory = new TCompactProtocol.Factory(); if (socketAddresses.length > 1) { transport = new TMultiUdpClient(socketAddresses); } else { transport = new TUdpClient(socketAddresses[0]); } transport.open(); client = new M3.Client(protocolFactory.getProtocol(transport)); calcProtocol = new TCompactProtocol.Factory().getProtocol(new TCalcTransport()); calc = (TCalcTransport) calcProtocol.getTransport(); freeBytes = calculateFreeBytes(builder.maxPacketSizeBytes, builder.metricTagSet); maxProcessorWaitUntilFlushMillis = builder.maxProcessorWaitUntilFlushMillis; bucketIdTagName = builder.histogramBucketIdName; bucketTagName = builder.histogramBucketName; bucketValFmt = String.format("%%.%df", builder.histogramBucketTagPrecision); metricQueue = new LinkedBlockingQueue<>(builder.maxQueueSize); executor = builder.executor; addAndRunProcessor(builder.metricTagSet); } catch (TTransportException | SocketException e) { throw new RuntimeException("Exception creating M3Reporter", e); } }
/** * 如果没有设置序列化协议,使用JSON协议 * * @return */ @Bean @ConditionalOnMissingBean(TProtocolFactory.class) public TProtocolFactory tProtocolFactory() { logger.info("init default TProtocol use TJSONProtocol"); return new TJSONProtocol.Factory(); }
private static TProtocolFactory getTProtocolFactory(boolean isCompact) { if (isCompact) { log.debug("Using compact protocol"); return new TCompactProtocol.Factory(); } else { log.debug("Using binary protocol"); return new TBinaryProtocol.Factory(); } }
private static TServer getTNonBlockingServer(TProtocolFactory protocolFactory, TProcessor processor, TTransportFactory transportFactory, InetSocketAddress inetSocketAddress) throws TTransportException { TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(inetSocketAddress); log.info("starting HBase Nonblocking Thrift server on " + inetSocketAddress.toString()); TNonblockingServer.Args serverArgs = new TNonblockingServer.Args(serverTransport); serverArgs.processor(processor); serverArgs.transportFactory(transportFactory); serverArgs.protocolFactory(protocolFactory); return new TNonblockingServer(serverArgs); }
private static TServer getTHsHaServer(TProtocolFactory protocolFactory, TProcessor processor, TTransportFactory transportFactory, int workerThreads, InetSocketAddress inetSocketAddress, ThriftMetrics metrics) throws TTransportException { TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(inetSocketAddress); log.info("starting HBase HsHA Thrift server on " + inetSocketAddress.toString()); THsHaServer.Args serverArgs = new THsHaServer.Args(serverTransport); if (workerThreads > 0) { // Could support the min & max threads, avoiding to preserve existing functionality. serverArgs.minWorkerThreads(workerThreads).maxWorkerThreads(workerThreads); } ExecutorService executorService = createExecutor( workerThreads, metrics); serverArgs.executorService(executorService); serverArgs.processor(processor); serverArgs.transportFactory(transportFactory); serverArgs.protocolFactory(protocolFactory); return new THsHaServer(serverArgs); }
public ThriftHttpServlet(TProcessor processor, TProtocolFactory protocolFactory, UserGroupInformation realUser, Configuration conf, ThriftServerRunner.HBaseHandler hbaseHandler, boolean securityEnabled, boolean doAsEnabled) { super(processor, protocolFactory); this.realUser = realUser; this.conf = conf; this.hbaseHandler = hbaseHandler; this.securityEnabled = securityEnabled; this.doAsEnabled = doAsEnabled; }
/** * Start extension by communicating with osquery core and starting thrift * server * * @param name * name of extension * @param version * version of extension * @param sdkVersion * version of the osquery SDK used to build this extension * @param minSdkVersion * minimum version of the osquery SDK that you can use * @throws IOException * @throws ExtensionException */ public void startExtension(String name, String version, String sdkVersion, String minSdkVersion) throws IOException, ExtensionException { ExtensionManager.Client client = new ClientManager(EXTENSION_SOCKET).getClient(); InternalExtensionInfo info = new InternalExtensionInfo(name, version, sdkVersion, minSdkVersion); try { ExtensionStatus status = client.registerExtension(info, registry); if (status.getCode() == 0) { this.uuid = status.uuid; Processor<PluginManager> processor = new Processor<PluginManager>(this); String serverSocketPath = EXTENSION_SOCKET + "." + String.valueOf(uuid); File socketFile = new File(serverSocketPath); if (socketFile.exists()) { socketFile.delete(); } AFUNIXServerSocket socket = AFUNIXServerSocket.bindOn(new AFUNIXSocketAddress(socketFile)); socketFile.setExecutable(true, false); socketFile.setWritable(true, false); socketFile.setReadable(true, false); TServerSocket transport = new TServerSocket(socket); TTransportFactory transportFactory = new TTransportFactory(); TProtocolFactory protocolFactory = new TBinaryProtocol.Factory(); TServer server = new TSimpleServer(new Args(transport).processor(processor) .transportFactory(transportFactory).protocolFactory(protocolFactory)); // Run it System.out.println("Starting the server..."); server.serve(); } else { throw new ExtensionException(1, status.getMessage(), uuid); } } catch (TException e) { throw new ExtensionException(1, "Could not connect to socket", uuid); } }
public void init() { try { TMultiplexedProcessor processor = new TMultiplexedProcessor(); for (ServiceArgs service : serverArgs.getServices()) { String className = service.getService(); if (className.endsWith("$Processor")) { className = className.substring(0, className.indexOf("$Processor")); } processor.registerProcessor(className, service.getProcessor()); } if (serverArgs.getNettyServerArgs() != null) { this.server = new TNettyServer(serverArgs.getNettyServerArgs().ip(serverArgs.getHost()).port(serverArgs.getPort())); } else { TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(new InetSocketAddress(serverArgs.getHost(), serverArgs.getPort())); //异步IO,需要使用TFramedTransport,它将分块缓存读取。 TTransportFactory transportFactory = new TFramedTransport.Factory(); //使用高密度二进制协议 TProtocolFactory proFactory = new TBinaryProtocol.Factory(); // Use this for a multithreaded key this.server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args(serverTransport) .transportFactory(transportFactory) .protocolFactory(proFactory) .processor(processor) ); } log.info("Starting the Thrift key..."); this.server.setServerEventHandler(new TrpcRegistryEventHandler(serverArgs)); this.server.serve(); if (this.serverArgs.getNettyServerArgs() != null) { ((TNettyServer) this.server).waitForClose(); } } catch (Exception e) { log.error("publish thrift key error", e); } }
public ThriftHandler(TProcessorFactory processorFactory, TProtocolFactory inProtocolFactory, TProtocolFactory outProtocolFactory, Executor executor) { this.processorFactory = processorFactory; this.inProtocolFactory = inProtocolFactory; this.outProtocolFactory = outProtocolFactory; this.userExecutor = executor; }
public TProtocolFactory getProtocolFactory(short head) { // SimpleJson的前两个字符为:[" ,而TJSONProtocol的第二个字符为一个数字 TProtocolFactory fac = protocolFactoryMap.get(head); if (logger.isDebugEnabled()) { logger.debug("head:{}, getProtocolFactory:{}", head, fac); } return fac; }