You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "Cyrill (via GitHub)" <gi...@apache.org> on 2023/09/26 13:41:35 UTC

[GitHub] [ozone] Cyrill opened a new pull request, #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Cyrill opened a new pull request, #5364:
URL: https://github.com/apache/ozone/pull/5364

   ## What changes were proposed in this pull request?
   
   Cache byte buffers for checksum calculation to reduce GC pressure.
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/HDDS-7228
   
   ## How was this patch tested?
   
   Performance tests.
   


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1739933963

   So with either the cached buffers or the reflection fix, the GC is much better. I'd expect the write performance to be better with the reflect fix, as it avoids copying all the data from one buffer to another - did you notice any impact there?


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] jojochuang commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "jojochuang (via GitHub)" <gi...@apache.org>.
jojochuang commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1736347597

   It's a really silly hack though... https://github.com/apache/ozone/pull/3759


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1737116258

   @Cyrill Could you test with the change in #3759 and see how performance looks?
   
   Also, are you seeing this allocation problem on the DN and have you turned on DN side checksum validation (chunk.data.validation.check)?


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] Cyrill commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "Cyrill (via GitHub)" <gi...@apache.org>.
Cyrill commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1739843334

   So I tested with the `chunk.data.validation.check` property and without it and I clearly see the effect.
   
   This is the test run **without** the DN checksum check
   <img width="1255" alt="noprop" src="https://github.com/apache/ozone/assets/56274/6abc3077-bc48-41d2-9e91-dd72562bc34d">
   
   This is the test run **with** the DN checksum check
   <img width="1268" alt="with_prop" src="https://github.com/apache/ozone/assets/56274/14217317-4bd9-47a4-bd2d-36cc99937654">
   
   Total allocated byte[] arrays memory is twice as large as when the property is turned off.


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1735950667

   
   So we start with a byteString in the protobuf. Then we convert that into a ByteBuffer, back to a byteString and back to a ByteBuffer and then in this PR a final copy from ByteBuffer to ByteBuffer!
   
   Were you seeing excessive memory usage on the DN side when writing chunks and with chunk.data.validation.check=true (the default is false, which is frankly crazy and will result in dataloss some day).
   
   
   Ok - so it seems that `ByteString.asReadOnlyByteBufferList()` simply wraps the internal ByteBuffer inside the ByteString.
   
   Then `data.toByteString(byteBufferToByteString)` uses this function:
   
   ```
       byteBufferToByteString =
           ByteStringConversion.createByteBufferConversion(isUnsafeByteBufferConversionEnabled);
   ```
   
   `isUnsafeByteBufferConversionEnabled` defaults to true, so this again creates a ByteString which wraps the ByteBuffer and doesn't copy. So it seems like all these copies are not copies at all.


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] Cyrill commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "Cyrill (via GitHub)" <gi...@apache.org>.
Cyrill commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1740427853

   The observed behaviour is not significantly different with writes since the number of concurrent write operations is lower than that in reads.
   But still GC is hitting more frequently without the fix:
   <img width="1650" alt="no_fix" src="https://github.com/apache/ozone/assets/56274/9192b1f5-0109-44c1-aac7-b80c229dc8a5">
   
   Now with the reflection fix:
   <img width="1654" alt="with_fix" src="https://github.com/apache/ozone/assets/56274/11e25001-2c5e-4fca-8236-762a1a8d3c67">
   
   


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] Cyrill commented on a diff in pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "Cyrill (via GitHub)" <gi...@apache.org>.
Cyrill commented on code in PR #5364:
URL: https://github.com/apache/ozone/pull/5364#discussion_r1337439706


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/CRC32ByteBufferImpl.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  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 org.xerial.snappy.pool.BufferPool;
+import org.xerial.snappy.pool.CachingBufferPool;
+
+import java.nio.ByteBuffer;
+import java.util.function.Consumer;
+import java.util.zip.CRC32;
+
+public class CRC32ByteBufferImpl implements ChecksumByteBuffer {
+
+  public static final BufferPool POOL = CachingBufferPool.getInstance();
+  private final CRC32 checksum;
+
+  public CRC32ByteBufferImpl(CRC32 checksum) {
+    this.checksum = checksum;
+  }
+
+  @Override
+  public void update(ByteBuffer buffer) {
+    if (buffer.hasArray()) {
+      checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset(),
+          buffer.remaining());
+    } else {

Review Comment:
   Unfortunately not. The code here and in `ChecksumByteBufferImpl` is merely a copy of `checksum.update()` and the latter is prone to the same issue - `buffer.hasArray()` returns `false` when the buffer is read only (which is our 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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] Cyrill commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "Cyrill (via GitHub)" <gi...@apache.org>.
Cyrill commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1739852382

   @sodonnel ^^ added test results


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] HDDS-7228. Avoid excessive memory consumpiton in checksum calculation [ozone]

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1742866815

   @Cyrill I've kicked off the build on https://github.com/apache/ozone/pull/3759 - @jojochuang Any reason not to commit your change? I see you mentioned there was still some overhead on NVME, but it makes things a lot better than they are currently, so I think its worth committing and we can see what else can be done in the future.


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] Cyrill commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "Cyrill (via GitHub)" <gi...@apache.org>.
Cyrill commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1739850521

   Also I checked the reflection fix
   <img width="1646" alt="reflection_fix_" src="https://github.com/apache/ozone/assets/56274/68374abc-32c2-4e91-ac33-335c8a53b604">
   The results are similar the cached buffer fix, though they look more stable.
   
   Here is what we had with the cached buffers:
   ![after](https://github.com/apache/ozone/assets/56274/643482dd-bd7b-4880-9fcd-95c4f7277ad5)
   


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] HDDS-7228. Avoid excessive memory consumpiton in checksum calculation [ozone]

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1757213594

   #3759 is now merged, so I think we can close this one?


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] HDDS-7228. Avoid excessive memory consumpiton in checksum calculation [ozone]

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1745134010

   There are some test failures in https://github.com/apache/ozone/pull/3759 - see my last couple of comments on that PR.


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on a diff in pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on code in PR #5364:
URL: https://github.com/apache/ozone/pull/5364#discussion_r1337393185


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/CRC32ByteBufferImpl.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  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 org.xerial.snappy.pool.BufferPool;
+import org.xerial.snappy.pool.CachingBufferPool;
+
+import java.nio.ByteBuffer;
+import java.util.function.Consumer;
+import java.util.zip.CRC32;
+
+public class CRC32ByteBufferImpl implements ChecksumByteBuffer {
+
+  public static final BufferPool POOL = CachingBufferPool.getInstance();
+  private final CRC32 checksum;
+
+  public CRC32ByteBufferImpl(CRC32 checksum) {
+    this.checksum = checksum;
+  }
+
+  @Override
+  public void update(ByteBuffer buffer) {
+    if (buffer.hasArray()) {
+      checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset(),
+          buffer.remaining());
+    } else {

Review Comment:
   As `checksum.update()` accepts a buffer, why do you need to copy the incoming buffer contents into a new (cached) buffer and then pass that new buffer to the `checksum.update()`. Could we not pass `buffer` directly to `checksum.update`?



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] HDDS-7228. Avoid excessive memory consumpiton in checksum calculation [ozone]

Posted by "Cyrill (via GitHub)" <gi...@apache.org>.
Cyrill commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1742629058

   @sodonnel 
   As a summary of all test that we ran with and without the fix:
   - stable +3% total throughput
   - DN CPU usage during **writes** decreased by 20-30 %
   - S3G CPU usage during **reads** decreased by 30 %
   - Fewer newgen cleanups
   - almost got rid of Full GC.
   
   Bottom line, the performance looks way better with the fix. 
   We can go with the reflection one since it is way simpler than the cached (this) one. 
   What would be the next steps to get this change in master?


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1736236356

   I benchmarked something similar to the above with some code I added in #1910 but has since been removed.
   
   The byte by byte approach is very slow. In my test it was at about 70 ops per second.
   
   Using the original code, assuming hasArray() returns true, gives about 2000 ops per second.
   
   Then I changed my code to have a second byteBuffer. Copied the data into it and then ran the test and it dropped to about 1850 ops per second. This was with an Indirect buffer.
   
   Finally I tried again with a "Direct" buffer `private static ByteBuffer data3 = DirectByteBufferAllocator.INSTANCE.allocate(1024 * 1024);` - this was actually slower at 1400 ops per second. I have no idea why the Direct is slower than indirect, and this was run on my laptop. Apple M1 Java 11, so different OS / Java may vary.
   
   @jojochuang Suggested there may be some reflection hacks to get access to the underlying byte array inside the ByteString, which could allow us to avoid copying the data at all.


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1735960548

   I think it would be worth benchmarking this code:
   
   ```
         while(buffer.hasRemaining()) {
           checksum.update(buffer.get());
         }
   ```
   
   vs
   
   ```
   withCachedBuffer(buffer.capacity(), cachedBuffer -> {
           cachedBuffer.put(buffer);
           cachedBuffer.flip();
           checksum.update(cachedBuffer);
         });
   ```
   
   As if the former may be is as fast as the latter because:
   
   1. The checksum has to be updated byte by byte anyway.
   2. It avoids copying the source bytes into a new buffer (cached) and then applying each byte in turn to the checksum.
   
   If both are the same speed, the former avoid any new allocations.
   


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1736354885

   @jojochuang You mentioned you still noticed some overhead on a NVME cluster - do you recall what that was? I guess the checksum calculation itself would still have overhead, but this "hack" you mentioned would avoid the memory allocation and an extra copy, so it makes the checksum calculation as fast as the algorithm allows. 
   
   I guess the write rate onto NVME may be so high the checksum overhead is significant. In my benchmarks, I was getting about 2000 ops per section on a single thread calculating 1M bytes per checksum on 4MB of data, which is about 8000MB/s per thread.
   
   The hack you suggested may not be perfect, but its a lot better than what we have currently with the extra copy!


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] HDDS-7228. Avoid excessive memory consumpiton in checksum calculation [ozone]

Posted by "Cyrill (via GitHub)" <gi...@apache.org>.
Cyrill closed pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation
URL: https://github.com/apache/ozone/pull/5364


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1735849785

   I recall an earlier conversation we had on Slack, where you pointed out that the reason the current code does not work well, is because it does this:
   
   ```
     public void update(ByteBuffer buffer) {
       if (buffer.hasArray()) {
         checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset(),
             buffer.remaining());
       } else {
         byte[] b = new byte[buffer.remaining()];
         buffer.get(b);
         checksum.update(b, 0, b.length);
       }
     }
   ```
   
   `buffer.hasArray()` always returns false if the buffer is readOnly, and it our case, the buffer is created by a call to `writeChunk.getData().asReadOnlyByteBufferList()` which is `ByteString.asReadOnlyByteBufferList()`.
   
   Looking inside `KeyValueHandler.java` where the chunk writes happen on the Datanode and where the checksum validation happens, the code looks like:
   
   ```
         WriteChunkRequestProto writeChunk = request.getWriteChunk();
         BlockID blockID = BlockID.getFromProtobuf(writeChunk.getBlockID());
         ContainerProtos.ChunkInfo chunkInfoProto = writeChunk.getChunkData();
         ChunkInfo chunkInfo = ChunkInfo.getFromProtoBuf(chunkInfoProto);
         Preconditions.checkNotNull(chunkInfo);
   
         ChunkBuffer data = null;
         if (dispatcherContext == null) {
           dispatcherContext = new DispatcherContext.Builder().build();
         }
         WriteChunkStage stage = dispatcherContext.getStage();
         if (stage == WriteChunkStage.WRITE_DATA ||
             stage == WriteChunkStage.COMBINED) {
           data =
               ChunkBuffer.wrap(writeChunk.getData().asReadOnlyByteBufferList());
           validateChunkChecksumData(data, chunkInfo);
   ```
   
   We can see that pull the data from the proto as a byteString and then create the asReadOnlyByteBufferList, which basically copies the proto into a ByteBuffer.
   
   Then inside `validateChunkChecksumData`, the code looks like:
   
   ```
     private void validateChunkChecksumData(ChunkBuffer data, ChunkInfo info)
         throws StorageContainerException {
       if (validateChunkChecksumData) {
         try {
           Checksum.verifyChecksum(data.toByteString(byteBufferToByteString),
               info.getChecksumData(), 0);
         } catch (OzoneChecksumException ex) {
           throw ChunkUtils.wrapInStorageContainerException(ex);
         }
       }
     }
   ```
   
   And then:
   
   ```
     public static boolean verifyChecksum(ByteString byteString,
         ChecksumData checksumData, int startIndex) throws OzoneChecksumException {
       final ByteBuffer buffer = byteString.asReadOnlyByteBuffer();
       return verifyChecksum(buffer, checksumData, startIndex);
   }
   ```
   
   So we start with a byteString in the protobuf. Then we convert that into a ByteBuffer, back to a byteString and back to a ByteBuffer and then in this PR a final copy from ByteBuffer to ByteBuffer!
   
   Were you seeing excessive memory usage on the DN side when writing chunks and with chunk.data.validation.check=true (the default is false, which is frankly crazy and will result in dataloss some day).


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] sodonnel commented on a diff in pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "sodonnel (via GitHub)" <gi...@apache.org>.
sodonnel commented on code in PR #5364:
URL: https://github.com/apache/ozone/pull/5364#discussion_r1337393185


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/CRC32ByteBufferImpl.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  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 org.xerial.snappy.pool.BufferPool;
+import org.xerial.snappy.pool.CachingBufferPool;
+
+import java.nio.ByteBuffer;
+import java.util.function.Consumer;
+import java.util.zip.CRC32;
+
+public class CRC32ByteBufferImpl implements ChecksumByteBuffer {
+
+  public static final BufferPool POOL = CachingBufferPool.getInstance();
+  private final CRC32 checksum;
+
+  public CRC32ByteBufferImpl(CRC32 checksum) {
+    this.checksum = checksum;
+  }
+
+  @Override
+  public void update(ByteBuffer buffer) {
+    if (buffer.hasArray()) {
+      checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset(),
+          buffer.remaining());
+    } else {

Review Comment:
   As `checksum.update()` accepts a buffer, why do you need to copy the incoming buffer contents into a new (cached) buffer and then pass that new buffer to the `checksum.update()`. Could we not pass `buffer` directly to `checksum.update`?
   
   I think it would be worth testing the performance difference between this approach, which is copying the incoming buffer into a new buf



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [ozone] adoroszlai commented on pull request #5364: HDDS-7228. Avoid excessive memory consumpiton in checksum calculation

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on PR #5364:
URL: https://github.com/apache/ozone/pull/5364#issuecomment-1735763901

   Thanks @Cyrill for the patch.  Please fix this checkstyle issue:
   
   ```
   hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/CRC32ByteBufferImpl.java
    27: Missing a Javadoc comment.
   ```
   
   https://github.com/Cyrill/ozone/actions/runs/6313525138/job/17141782688#step:7:11


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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