对于较大的文件传输,数据传输速率在15GB后变慢

时间:2017-04-10 05:37:57

标签: java networking tcp jvm netty

发送大于15GB的文件时,我遇到数据传输速率问题。我有3台服务器和一台客户端。从客户端向服务器发送文件时,我将文件拆分为块(每个块通常为256MB),并在2台服务器上复制每个块。复制发生在管道方法中。发送块时,每个块被切成较小的数据包(每个数据包通常为128 KB),发送到服务器,并在服务器端合并以存储在硬盘驱动器中。这里一切都很好。我测试了系统5GB到50GB的文件,增量为5GB。所有文件的平均写入速度约为600MB /秒。见下图。我在这里与HDFS进行比较。

enter image description here

从服务器读取相同文件时会出现问题。文件分布在多个服务器上。例如,我可以从server1读取block1,从server2读取block2,依此类推。直观地说,读取必须比写入更快,因为客户端并行读取3个服务器。阅读小于15GB {5GB,10GB,15GG}的文件时,性能约为1.1GB /秒。读取大于20GB {20GB,25GB,......,50GB}的文件时会出现问题。随着文件大小的增加,性能会下降。

Performance graph for 50GB file when reading

上图显示了读取50GB文件的基准测试。每个黑点显示单独的块读取时间。如您所见,性能在第60-70块后开始下降。有趣的是,这种情况发生在所有大于15GB的文件中,在同一个地方(约65个区块)左右放慢速度。随着文件大小的增加,缓慢的部分占据主导地位,性能也越来越差。我觉得16GB左右有一些障碍。我看到的唯一提示可能有帮助,即3台服务器随机并行发送块直到65左右。所以阻止'转移是重叠的。之后,一台服务器以循环顺序一次发送。我可以从日志输出中看到这一点。这里仍然存在一些重叠,但没有65个区块之前那么多。

我正在为这个项目使用java 1.8,以及netty 4.1.8。作为tcp服务器。 操作系统是CentOS 7。 每台服务器有两个CPU(Intel(R)Xeon(R)CPU E5-2650 v3 @ 2.30GHz)= 40个核心 64GB RAM 10 GBit以太网。

我花了很多时间,找不到问题的根本原因。 问题可能来自Java VM,Netty,OS,OS TCP默认值或其他原因。

服务器端BlockSenderManager

@Override
    public void run(){

        while(nodeManager.isRunning()){
            try
            {
                BlockRequest br = blockSenders.take();
                if(br != null){
                    executor.execute(new BlockSender( br, this));
                }

                if(wait.take())
                    System.out.println(br.getBlockId()+" Delivered");
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

服务器端的BlockSender:

    @Override
        public void run()
        {
            FileInputStream fis = null;

            try
            {
                java.io.File file = new java.io.File(path+"/" + blockRequest.getBlockId());

                fis = new FileInputStream(file);
                fSize = file.length();
                long rem = fSize;

                sendBlockInfo();
                int bufSize;
                if (fSize < (long) packetSize)
                    bufSize = (int) fSize;
                else
                    bufSize = packetSize;
                int read = 0, packetOrder = 1;

                byte[] data;
                if(bufSize <= rem)
                    data = new byte[bufSize];
                else
                    data = new byte[(int)rem];
                while ((read = (fis.read(data))) > 0)
                {
                    if (read < 1)
                        break;

                    BlockPacket bp = new BlockPacket();

                    bp.setRequestId(blockRequest.getRequestId());
                    bp.setBlockId(blockRequest.getBlockId());
                    bp.setData(data);
                    bp.setPacketSeqNo(packetOrder);
                    if(read < bufSize)
                    {
                        bp.setIsLastPacket(true);
                    }

                    executor.execute(new Sender(bp));

                    packetOrder++;
                    if(rem > bufSize)
                        rem = rem - bufSize;

                    if(bufSize <= rem)
                        data = new byte[bufSize];
                    else
                    {
                        data = new byte[(int)rem];
                    }
                }

                fis.close();
                executor.shutdown();
            }
            catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

public class Sender implements Runnable
    {
        private final BlockPacket bp;
        private final FileBlock fb;
        private DataClient dc;

        public Sender(BlockPacket bp)
        {
            this.bp = bp;
            this.fb = null;
            dc = getDataClient(requestClient);
        }

        public Sender(FileBlock fb)
        {
            this.bp = null;
            this.fb = fb;
            dc = getDataClient(requestClient);
        }

        @Override
        public void run()
        {

            if (dc != null)
            {
                if (bp != null)
                {
                    dc.send(bp);
                }
                else if (fb != null)
                {
                    dc.send(fb);
                }
            }

        }
    }

客户端的ReceivedPacketProcessor

public void processBlockPacket(BlockPacket bp)
    {
        ByteBuffer buf = ByteBuffer.wrap(bp.getData());
        try
        {
            inChannel.write(buf);  
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
        public void run()
        {
            try
            {
                aFile = new RandomAccessFile(path+"/"+fileName, "rw");
                inChannel = aFile.getChannel();
                //java.io.File f = new java.io.File(path+"/"+fileName);
                //fop = new FileOutputStream(f);
                String reqId = file.getFileID();
                currentBlockId = reqId + "_" + currentBlockSeq;
                while (true)
                {
                    BlockPacket bp = null;
                    if (numberOfBlocks > 0)
                    {
                        try
                        {
                            bp = this.blockingQueue.take();
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                        if (bp.getBlockId().equals(currentBlockId))
                        {
                            if (currentPacket == bp.getPacketSeqNo())
                            {

                                if(fileBlocks.containsKey(currentBlockId))
                                {
                                    processBlockPacket(bp);
                                    if(currentPacket < fileBlocks.get(currentBlockId).getNoOfPackets())
                                        currentPacket++;
                                    else
                                    {
                                        if (fileBlocks.get(currentBlockId).getPackets().size() < 1)
                                        {
                                            removeFileBlock(currentBlockId);
                                            currentBlockSeq++;
                                            currentBlockId = reqId + "_" + currentBlockSeq;
                                            currentPacket = 1;
                                            numberOfBlocks--;
                                        } 
                                    } 
                                }
                                else
                                {
                                    tempList.add(bp); 
                                }

                                for(int k =numberOfBlocks; k>0; k--)
                                {
                                    if(fileBlocks.containsKey(currentBlockId))
                                    {
                                        int pCount = fileBlocks.get(currentBlockId).getNoOfPackets();
                                        int i;
                                        for (i = currentPacket; i <= pCount; i++)
                                        {
                                            if (fileBlocks.get(currentBlockId).getPackets().containsKey(i))
                                            {
                                                processBlockPacket(fileBlocks.get(currentBlockId).getPackets().remove(i));
                                                currentPacket++;
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                        if(i <= pCount)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            if (fileBlocks.get(currentBlockId).getPackets().size()  < 1)
                                            {
                                                removeFileBlock(currentBlockId);
                                                currentBlockSeq++;
                                                currentBlockId = reqId + "_" + currentBlockSeq;
                                                currentPacket = 1;
                                                numberOfBlocks--;
                                            } 
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if(fileBlocks.containsKey(bp.getBlockId())){
                                fileBlocks.get(bp.getBlockId()).getPackets().put(bp.getPacketSeqNo(), bp);
                            }else{
                                tempList.add(bp);
                            }

                        }
                    }
                    else{
                        break;
                    }
                    for(int i=0; i<tempList.size(); i++){
                        if(fileBlocks.containsKey(tempList.get(i).getBlockId())){
                            BlockPacket temp = tempList.remove(i);
                            fileBlocks.get(temp.getBlockId()).getPackets().put(temp.getPacketSeqNo(), temp); 
                        }
                    }
                }
                System.out.println("CLOSING FILE....");
                this.isCompleted.put(true);
                inChannel.force(true);
                inChannel.close();
            }
            catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

启用-XX:+ PrintGCDetails,here is a sample log

感谢任何评论/帮助。

1 个答案:

答案 0 :(得分:0)

这是因为内存中的脏页面比例。由于传入数据速率高于本地IO刷新吞吐量,因此数据在内存中累积。达到最大允许脏页面比率后,接收器不接受更多数据。因此,在这种情况下,系统与本地IO绑定,而不是网络。因此,收益的减少发生在大约15GB。您可以在

中更改一些设置
  

/etc/sysctl.conf中

如:

vm.dirty_background_ratio = 2
vm.dirty_ratio = 80
vm.dirty_expire_centisecs = 3000
vm.dirty_writeback_centisecs = 500

This可能是一本有用的阅读材料。

系统性能仍受本地IO和最大允许脏页率的限制。您可以增加脏页面比率,只能推迟缩短返回时间。如果文件/数据较大,它将再次达到这一点。新结果: enter image description here