Thrift Direct Memory OOM问题解决方法

FrameBuffer为AbstractNonblockingServer类的内部类,TThreadedSelectorServer继承了AbstractNonblockingServer

通过研究代码,发现FrameBuffer的read方法的代码中有如下片段,

          // pull out the frame size as an integer.
          int frameSize = buffer_.getInt(0);
          if (frameSize <= 0) {
            LOGGER.error("Read an invalid frame size of " + frameSize
                + ". Are you using TFramedTransport on the client side?");
            return false;
          }

          // if this frame will always be too large for this server, log the
          // error and close the connection.
          if (frameSize > MAX_READ_BUFFER_BYTES) {
            LOGGER.error("Read a frame size of " + frameSize
                + ", which is bigger than the maximum allowable buffer size for ALL connections.");
            return false;
          }

MAX_READ_BUFFER_BYTES这个值即为对读取的包的长度限制,为AbstractNonblockingServer类的属性,其值又取自内部类AbstractNonblockingServerArgs的maxReadBufferBytes属性,默认值为long型的最大值;

即只要修改maxReadBufferBytes的值就可以起到限制的作用,修改服务启动的代码如下:

TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(transport).processor(
                    processor).workerThreads(this.serverWorkerThreads);
            
            // The maximum amount of memory we will allocate to client IO buffers at a time.
            // set 1MB.
            args.maxReadBufferBytes = 1024 * 1024L;
            
            server = new TThreadedSelectorServer(args);

            server.setServerEventHandler(new DataIfaceServerEvent());
            LOG.info("DataIfaceServer start, port={}.", this.serverPort);
            server.serve();

args.maxReadBufferBytes = 1024 * 1024L;  --设置为1M

通过对我的thrift的服务进行抓包调研,我的方法调用请求数据包没有超过200字节的,所以1M的长度限制是足够了。

通过测试如上的修改没有问题,并且对服务继续发送http get请求不会导致直接内存增加。并且报出错误日志:

Read a frame size of XXX, which is bigger than the maximum allowable buffer size for ALL connections.

至此问题解决;

问题的分析过程:http://my.oschina.net/shipley/blog/422204






你可能感兴趣的:(oom,thrift)