You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2019/12/19 12:41:22 UTC

[GitHub] [hadoop-ozone] adoroszlai opened a new pull request #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer

adoroszlai opened a new pull request #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer
URL: https://github.com/apache/hadoop-ozone/pull/378
 
 
   ## What changes were proposed in this pull request?
   
   Introduce a new `ChunkBuffer` implementation that wraps an existing list of `ByteBuffer` instances.  
   
   For now the only usage is in `WriteChunk` (and `PutSmallFile`) where it wraps the chunk data buffer list.
   
   https://issues.apache.org/jira/browse/HDDS-2721
   
   ## How was this patch tested?
   
   Added unit test.
   
   https://github.com/adoroszlai/hadoop-ozone/runs/356252796

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: ozone-issues-help@hadoop.apache.org


[GitHub] [hadoop-ozone] adoroszlai commented on issue #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer

Posted by GitBox <gi...@apache.org>.
adoroszlai commented on issue #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer
URL: https://github.com/apache/hadoop-ozone/pull/378#issuecomment-571542211
 
 
   Thanks @bshashikant and @lokeshj1703 for reviewing it.
   
   > Should we replace ChunkBufferImplWithByteBuffer with ChunkBufferImplWithByteBufferList by including implementation for singleton list?
   
   I'd rather keep `ChunkBufferImplWithByteBuffer` to avoid any potential runtime penalties.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: ozone-issues-help@hadoop.apache.org


[GitHub] [hadoop-ozone] adoroszlai merged pull request #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer

Posted by GitBox <gi...@apache.org>.
adoroszlai merged pull request #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer
URL: https://github.com/apache/hadoop-ozone/pull/378
 
 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: ozone-issues-help@hadoop.apache.org


[GitHub] [hadoop-ozone] lokeshj1703 commented on a change in pull request #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer

Posted by GitBox <gi...@apache.org>.
lokeshj1703 commented on a change in pull request #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer
URL: https://github.com/apache/hadoop-ozone/pull/378#discussion_r363275358
 
 

 ##########
 File path: hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/ChunkBufferImplWithByteBufferList.java
 ##########
 @@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.ozone.common;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
+
+import java.io.IOException;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.nio.channels.GatheringByteChannel;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+
+/**
+ * {@link ChunkBuffer} implementation using a list of {@link ByteBuffer}s.
+ * Not thread-safe.
+ */
+public class ChunkBufferImplWithByteBufferList implements ChunkBuffer {
+
+  private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
+
+  /** Buffer list backing the ChunkBuffer. */
+  private final List<ByteBuffer> buffers;
+  private final int limit;
+
+  private int limitPrecedingCurrent;
+  private int currentIndex;
+
+  ChunkBufferImplWithByteBufferList(List<ByteBuffer> buffers) {
+    Preconditions.checkArgument(buffers != null, "buffer == null");
+
+    this.buffers = !buffers.isEmpty() ? ImmutableList.copyOf(buffers) :
+        ImmutableList.of(EMPTY_BUFFER);
+    this.limit = buffers.stream().mapToInt(ByteBuffer::limit).sum();
+
+    findCurrent();
+  }
+
+  private void findCurrent() {
+    boolean found = false;
+    for (int i = 0; i < buffers.size(); i++) {
+      final ByteBuffer buf = buffers.get(i);
+      final int pos = buf.position();
+      if (found) {
+        Preconditions.checkArgument(pos == 0,
+            "all buffers after current one should have position=0");
+      } else if (pos < buf.limit()) {
+        found = true;
+        currentIndex = i;
+      } else {
+        limitPrecedingCurrent += buf.limit();
+      }
+    }
+    if (!found) {
+      currentIndex = buffers.size() - 1;
+      limitPrecedingCurrent -= current().limit();
+    }
+  }
+
+  private ByteBuffer current() {
+    return buffers.get(currentIndex);
+  }
+
+  private void advanceCurrent() {
+    if (currentIndex < buffers.size() - 1) {
+      final ByteBuffer current = buffers.get(currentIndex);
+      if (!current.hasRemaining()) {
+        currentIndex++;
+        limitPrecedingCurrent += current.limit();
+      }
+    }
+  }
+
+  private void rewindCurrent() {
+    currentIndex = 0;
+    limitPrecedingCurrent = 0;
+  }
+
+  @Override
+  public int position() {
+    return limitPrecedingCurrent + current().position();
+  }
+
+  @Override
+  public int remaining() {
+    return limit - position();
+  }
+
+  @Override
+  public int limit() {
+    return limit;
+  }
+
+  @Override
+  public ChunkBuffer rewind() {
+    buffers.forEach(ByteBuffer::rewind);
+    rewindCurrent();
+    return this;
+  }
+
+  @Override
+  public ChunkBuffer clear() {
+    buffers.forEach(ByteBuffer::clear);
+    rewindCurrent();
+    return this;
+  }
+
+  @Override
+  public ChunkBuffer put(ByteBuffer that) {
+    final int thisRemaining = remaining();
+    int thatRemaining = that.remaining();
+    if (thatRemaining > thisRemaining) {
+      final BufferOverflowException boe = new BufferOverflowException();
+      boe.initCause(new IllegalArgumentException(
+          "Failed to put since that.remaining() = " + thatRemaining
+              + " > this.remaining() = " + thisRemaining));
+      throw boe;
+    }
+
+    while (thatRemaining > 0) {
+      final ByteBuffer b = current();
+      final int bytes = Math.min(b.remaining(), thatRemaining);
+      that.limit(that.position() + bytes);
+      b.put(that);
+      thatRemaining -= bytes;
+      advanceCurrent();
+    }
+
+    return this;
+  }
+
+  @Override
+  public ChunkBuffer duplicate(int newPosition, int newLimit) {
+    Preconditions.checkArgument(newPosition >= 0);
+    Preconditions.checkArgument(newPosition <= newLimit);
+    Preconditions.checkArgument(newLimit <= limit());
+
+    final List<ByteBuffer> duplicates = new ArrayList<>(buffers.size());
+    int min = 0;
+    for (final ByteBuffer buf : buffers) {
+      final int max = min + buf.limit();
+      final int pos = relativeToRange(newPosition, min, max);
+      final int lim = relativeToRange(newLimit, min, max);
+
+      final ByteBuffer duplicate = buf.duplicate();
+      duplicate.position(pos).limit(lim);
 
 Review comment:
   I think position and limit are set with buf.duplicate(). We might not need the computation of min, max and lim in that case.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: ozone-issues-help@hadoop.apache.org


[GitHub] [hadoop-ozone] adoroszlai commented on a change in pull request #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer

Posted by GitBox <gi...@apache.org>.
adoroszlai commented on a change in pull request #378: HDDS-2721. Allow wrapping list of ByteBuffers with ChunkBuffer
URL: https://github.com/apache/hadoop-ozone/pull/378#discussion_r363303844
 
 

 ##########
 File path: hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/ChunkBufferImplWithByteBufferList.java
 ##########
 @@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.ozone.common;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
+
+import java.io.IOException;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.nio.channels.GatheringByteChannel;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+
+/**
+ * {@link ChunkBuffer} implementation using a list of {@link ByteBuffer}s.
+ * Not thread-safe.
+ */
+public class ChunkBufferImplWithByteBufferList implements ChunkBuffer {
+
+  private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
+
+  /** Buffer list backing the ChunkBuffer. */
+  private final List<ByteBuffer> buffers;
+  private final int limit;
+
+  private int limitPrecedingCurrent;
+  private int currentIndex;
+
+  ChunkBufferImplWithByteBufferList(List<ByteBuffer> buffers) {
+    Preconditions.checkArgument(buffers != null, "buffer == null");
+
+    this.buffers = !buffers.isEmpty() ? ImmutableList.copyOf(buffers) :
+        ImmutableList.of(EMPTY_BUFFER);
+    this.limit = buffers.stream().mapToInt(ByteBuffer::limit).sum();
+
+    findCurrent();
+  }
+
+  private void findCurrent() {
+    boolean found = false;
+    for (int i = 0; i < buffers.size(); i++) {
+      final ByteBuffer buf = buffers.get(i);
+      final int pos = buf.position();
+      if (found) {
+        Preconditions.checkArgument(pos == 0,
+            "all buffers after current one should have position=0");
+      } else if (pos < buf.limit()) {
+        found = true;
+        currentIndex = i;
+      } else {
+        limitPrecedingCurrent += buf.limit();
+      }
+    }
+    if (!found) {
+      currentIndex = buffers.size() - 1;
+      limitPrecedingCurrent -= current().limit();
+    }
+  }
+
+  private ByteBuffer current() {
+    return buffers.get(currentIndex);
+  }
+
+  private void advanceCurrent() {
+    if (currentIndex < buffers.size() - 1) {
+      final ByteBuffer current = buffers.get(currentIndex);
+      if (!current.hasRemaining()) {
+        currentIndex++;
+        limitPrecedingCurrent += current.limit();
+      }
+    }
+  }
+
+  private void rewindCurrent() {
+    currentIndex = 0;
+    limitPrecedingCurrent = 0;
+  }
+
+  @Override
+  public int position() {
+    return limitPrecedingCurrent + current().position();
+  }
+
+  @Override
+  public int remaining() {
+    return limit - position();
+  }
+
+  @Override
+  public int limit() {
+    return limit;
+  }
+
+  @Override
+  public ChunkBuffer rewind() {
+    buffers.forEach(ByteBuffer::rewind);
+    rewindCurrent();
+    return this;
+  }
+
+  @Override
+  public ChunkBuffer clear() {
+    buffers.forEach(ByteBuffer::clear);
+    rewindCurrent();
+    return this;
+  }
+
+  @Override
+  public ChunkBuffer put(ByteBuffer that) {
+    final int thisRemaining = remaining();
+    int thatRemaining = that.remaining();
+    if (thatRemaining > thisRemaining) {
+      final BufferOverflowException boe = new BufferOverflowException();
+      boe.initCause(new IllegalArgumentException(
+          "Failed to put since that.remaining() = " + thatRemaining
+              + " > this.remaining() = " + thisRemaining));
+      throw boe;
+    }
+
+    while (thatRemaining > 0) {
+      final ByteBuffer b = current();
+      final int bytes = Math.min(b.remaining(), thatRemaining);
+      that.limit(that.position() + bytes);
+      b.put(that);
+      thatRemaining -= bytes;
+      advanceCurrent();
+    }
+
+    return this;
+  }
+
+  @Override
+  public ChunkBuffer duplicate(int newPosition, int newLimit) {
+    Preconditions.checkArgument(newPosition >= 0);
+    Preconditions.checkArgument(newPosition <= newLimit);
+    Preconditions.checkArgument(newLimit <= limit());
+
+    final List<ByteBuffer> duplicates = new ArrayList<>(buffers.size());
+    int min = 0;
+    for (final ByteBuffer buf : buffers) {
+      final int max = min + buf.limit();
+      final int pos = relativeToRange(newPosition, min, max);
+      final int lim = relativeToRange(newLimit, min, max);
+
+      final ByteBuffer duplicate = buf.duplicate();
+      duplicate.position(pos).limit(lim);
 
 Review comment:
   Calculation of min/max/lim is required because `ChunkBuffer#duplicate` combines operation of `duplicate` and `position`/`limit` (by accepting `newLimit` and `newPosition` parameters).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: ozone-issues-help@hadoop.apache.org