jam2in / arcus-java-client

Arcus Java client
Apache License 2.0
0 stars 0 forks source link

fillWriteBuffer() 코드 검증. #27

Closed jhpark816 closed 8 years ago

jhpark816 commented 8 years ago

아래 코드에서 bytesToCopy는 아래 둘 중의 작은 값으로 설정해야 하지 않나요 ?

    public final void fillWriteBuffer(boolean shouldOptimize) {
        if(toWrite == 0 && readQ.remainingCapacity() > 0) {
            getWbuf().clear();
            Operation o=getCurrentWriteOp();
            while(o != null && toWrite < getWbuf().capacity()) {
                assert o.getState() == OperationState.WRITING;
                // This isn't the most optimal way to do this, but it hints
                // at a larger design problem that may need to be taken care
                // if in the bowels of the client.
                // In practice, readQ should be small, however.
                if(!readQ.contains(o)) {
                    readQ.add(o);
                }

                ByteBuffer obuf=o.getBuffer();
                assert obuf != null : "Didn't get a write buffer from " + o;
                int bytesToCopy=Math.min(getWbuf().remaining(),
                        obuf.remaining());
                byte b[]=new byte[bytesToCopy];
                obuf.get(b);
                getWbuf().put(b);
                getLogger().debug("After copying stuff from %s: %s",
                        o, getWbuf());
                if(!o.getBuffer().hasRemaining()) {
                    o.writeComplete();
                    transitionWriteItem();

                    preparePending();
                    if(shouldOptimize) {
                        optimize();
                    }

                    o=getCurrentWriteOp();
                }
                toWrite += bytesToCopy;
            }
            getWbuf().flip();
            assert toWrite <= getWbuf().capacity()
                : "toWrite exceeded capacity: " + this;
            assert toWrite == getWbuf().remaining()
                : "Expected " + toWrite + " remaining, got "
                + getWbuf().remaining();
        } else {
            getLogger().debug("Buffer is full, skipping");
        }
    }
whchoi83 commented 8 years ago

먼저 ByteBuffer 에 대한 설명이 필요할 것 같습니다. ByteBuffer 에서 필요한 속성 개념이 position, limit, capacity 가 있습니다. (mark 도 있지만 제외하겠습니다.)

아래 그림은 ByteBuffer 를 8로 생성했을 때 입니다. java-nio-channel-1

각 속성 값의 관계는 아래와 같습니다. 0 <= position <= limit <= capacity

ByteBuffer 에서 get 과 put 등을 호출하면 해당 position 에 작업을 처리하고 position 을 증가 시킵니다. remaining 은 limit - position 의 값을 return 해줍니다.


Operation 에 존재하는 cmd ByteBuffuer 에 있는 것을 Memcached Node 에 있는 write ByteBuffuer 로 옮기는 작업을 할 때 while 문을 이용하기 때문에 두 ByteBuffer 의 remaining 을 비교하는 것이 맞습니다.

jhpark816 commented 8 years ago

@whchoi83 내가 remaining에 대해 잘못 이해하고 있었군요..

cmd byte buffer에는 cmd string을 put하고 나서 flip()을 한 상태라, remaining이 cmd byte buffer에 기록된 string의 길이를 의미하게 되는 거고, write buffer의 remaing은 아직 put할 수 있는 크기를 나타내는 거네요... write buffer에 cmd를 모두 채운 후에는 flip()하여 remaining 만큼 보내는 거고요..

Thank You..