Skip to content

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. - #3195

Merged
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev
Mar 28, 2022
Merged

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.#3195
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev

Conversation

@guohao-rosicky

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Optimized the putBlock method of BlockDataStreamOutput

  1. PutBlock does not use RaftRpc, but through the stream close() method
// putBlock metadata
ByteBuffer buf =
ContainerCommandRequestMessage.toMessage(putBlockRequestProto, null)
.getContent().asReadOnlyByteBuffer();
out.writeAsync(buf, StandardWriteOption.CLOSE); 

2.Mark the stream data boundary by sending an empty stream packet

// Mark the stream data boundary
out.writeAsync(ByteBuffer.allocateDirect(0).asReadOnlyBuffer()); 
  1. Flush is also in the form of async RPC, because putBlock needs to fetch bcsId (Raft log index)

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-6137

How was this patch tested?

Use the existing UT

@guohao-rosickyguohao-rosicky changed the title Guohao hdds 6137 devHDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputMar 15, 2022

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guohao-rosicky Improve this. LGTM over all. TestBlockDataStreamOutput has some error, can you confirm?

@szetszwoszetszwoMar 16, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky, Writing an empty buffer may not work as expected. In the streaming model, data is continuous but not block-by-block. Both sender and receiver must consider that the data is a stream of bytes.

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

Let me think about how to make it work.

@guohao-rosickyguohao-rosickyMar 16, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment @szetszwo . I wrote a piece of pseudo-code, Ideas about the modification of org.apache.ratis.netty.server.DataStreamManagement#writeTo.

image

What do you think of this idea? There is no need to send this empty buffer rpc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , this is a standard problem in streaming. The standard solution is to write the length with the data. Let's don't change the API for it.

@guohao-rosickyguohao-rosickyMar 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @szetszwo
I have a question, about

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

I noticed that in HDFS, marking the end of stream is also represented by an empty package.
In Ratis Stream, sticky packets have already been dealt with.

org.apache.ratis.netty.NettyDataStreamUtils#encodeDataStreamRequestHeader
https://github.com/apache/ratis/blob/master/ratis-netty/src/main/java/org/apache/ratis/netty/NettyDataStreamUtils.java#L90

@captainzmc tested the code from some time ago and did not find this problem

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

Thanks @szetszwo, Patch has been applied

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , please fix the checkstyle warnings and test the change manually. I have not tested it. Thanks.

@captainzmc

Copy link
Copy Markdown
Member

Thanks @guohao-rosicky@szetszwo update this. I used this PR test and found that the file failed to write. In DataNode IndexOutOfBoundsException will appear.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks. Let me test the serialization/deserialization code.

@szetszwo

Copy link
Copy Markdown
Contributor

Found the bug: ByteBu.nioBuffers(index, length) somehow is not working. Let me fix it.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky, Fixed the bug; see https://issues.apache.org/jira/secure/attachment/13041255/3195_TestStreamDataChannel.patch

Thanks @szetszwo for the update. Using new patch I found IndexOutOfBoundsException still appear.
I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks a lot for testing it!

I added one more test for the serialization and buffers. It worked fine. See https://issues.apache.org/jira/secure/attachment/13041275/3195_testBuffers.patch

I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.

It seems there are some synchronization problems. Let me check.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , could you post the test code?

@captainzmc

Copy link
Copy Markdown
Member

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Actually there a problem with my previous description. I noticed that the when I use 30 threads writing, we’ll get same error.

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@szetszwo Here is my datanode log. https://issues.apache.org/jira/secure/attachment/13041276/ozone-root-datanode-9-29-173-57.log
In the test, I wrote 30 files concurrently. Each file is 128MB and the chunk size is 1MB

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

@szetszwo

Copy link
Copy Markdown
Contributor

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Thanks for sharing the test code. It looks good.

@captainzmc

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

Thanks @szetszwo for update this. I had just test this patch, we still get same error. Just as the same as this.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an abstract class instead static methods. Then, KeyDataStreamOutput can extends it. If we need to add SmallFileDataStreamOutput, it can extends it too.

package org.apache.hadoop.ozone.client.io;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.scm.storage.ByteBufferStreamOutput;
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
import org.apache.ratis.protocol.exceptions.RaftRetryFailureException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractDataStreamOutput implements ByteBufferStreamOutput {
private final Map<Class<? extends Throwable>, RetryPolicy> retryPolicyMap;
private int retryCount;
private boolean isException;
protected AbstractDataStreamOutput() {
this.retryPolicyMap = HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL));
}
@VisibleForTesting
public int getRetryCount() {
return retryCount;
}
void resetRetryCount() {
retryCount = 0;
}
boolean isException() {
return isException;
}
/**
* Checks if the provided exception signifies retry failure in ratis client.
* In case of retry failure, ratis client throws RaftRetryFailureException
* and all succeeding operations are failed with AlreadyClosedException.
*/
boolean checkForRetryFailure(Throwable t) {
return t instanceof RaftRetryFailureException
|| t instanceof AlreadyClosedException;
}
// Every container specific exception from datatnode will be seen as
// StorageContainerException
boolean checkIfContainerToExclude(Throwable t) {
return t instanceof StorageContainerException;
}
private void setExceptionAndThrow(IOException ioe) throws IOException {
isException = true;
throw ioe;
}
void handleRetry(IOException exception) throws IOException {
RetryPolicy retryPolicy = retryPolicyMap
.get(HddsClientUtils.checkForException(exception).getClass());
if (retryPolicy == null) {
retryPolicy = retryPolicyMap.get(Exception.class);
}
handleRetry(exception, retryPolicy);
}
private void handleRetry(IOException exception, RetryPolicy retryPolicy)
throws IOException {
RetryPolicy.RetryAction action = null;
try {
action = retryPolicy.shouldRetry(exception, retryCount, 0, true);
} catch (Exception e) {
setExceptionAndThrow(new IOException(e));
}
if (action.action == RetryPolicy.RetryAction.RetryDecision.FAIL) {
String msg = "";
if (action.reason != null) {
msg = "Retry request failed. " + action.reason;
//LOG.error(msg, exception);
}
setExceptionAndThrow(new IOException(msg, exception));
}
// Throw the exception if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
//LOG.warn("Interrupted while trying for retry");
setExceptionAndThrow(exception);
}
Preconditions.checkArgument(
action.action == RetryPolicy.RetryAction.RetryDecision.RETRY);
if (action.delayMillis > 0) {
try {
Thread.sleep(action.delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
IOException ioe = (IOException) new InterruptedIOException(
"Interrupted: action=" + action + ", retry policy=" + retryPolicy)
.initCause(e);
setExceptionAndThrow(ioe);
}
}
retryCount++;
}
}

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Ok, I'll change it

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Has been modified. @szetszwo Please take a look.Thanks.

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , please review it yourself -- go through the change and remove the unused code (e.g. ConsumerWithIOException). Thanks.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , thanks for the update. Some comments inlined.

});
}

private CompletableFuture<ContainerCommandResponseProto> runCommandAsync(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCommandAsync is not used. Let's add it later.

Comment on lines +194 to +197
LOG.error(
"Get stream data channel error Malformed request " +
"containerID: {} msg: {}",
container.getContainerData().getContainerID(), msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove LOG.error(..) since it already throws an exception.

ContainerCommandRequestProto request, KeyValueContainer kvContainer,
DispatcherContext dispatcherContext) {
if (!request.hasWriteChunk()) {
BlockID blockID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final.

@@ -97,12 +85,8 @@ enum StreamAction {
*/
@VisibleForTesting
public KeyDataStreamOutput() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere. Let's remove it.

Comment on lines -161 to -162
this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
config.getMaxRetryCount(), config.getRetryInterval());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the retryPolicyMap to the super constructor.

Comment on lines +116 to +119
super(HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is passed incorrectly. Below is the original map.

- this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
- config.getMaxRetryCount(), config.getRetryInterval());

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @guohao-rosicky. The change looks good.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo
szetszwo merged commit 9546d4c into apache:HDDS-4454Mar 28, 2022
@szetszwoszetszwo changed the title HDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputHDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.Mar 28, 2022
szetszwo pushed a commit to szetszwo/ozone that referenced this pull request May 6, 2022
captainzmc pushed a commit to captainzmc/hadoop-ozone that referenced this pull request Jul 4, 2022
szetszwo pushed a commit that referenced this pull request Oct 25, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
szetszwo pushed a commit that referenced this pull request Nov 7, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
(cherry picked from commit 99b414d)
szetszwo pushed a commit that referenced this pull request Dec 1, 2022
szetszwo pushed a commit that referenced this pull request Dec 16, 2022
nishitpatira pushed a commit to nishitpatira/ozone that referenced this pull request Dec 16, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@guohao-rosicky@szetszwo@captainzmc
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. by guohao-rosicky · Pull Request #3195 · apache/ozone · GitHub
Skip to content

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. - #3195

Merged
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev
Mar 28, 2022
Merged

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.#3195
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev

Conversation

@guohao-rosicky

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Optimized the putBlock method of BlockDataStreamOutput

  1. PutBlock does not use RaftRpc, but through the stream close() method
// putBlock metadata
ByteBuffer buf =
ContainerCommandRequestMessage.toMessage(putBlockRequestProto, null)
.getContent().asReadOnlyByteBuffer();
out.writeAsync(buf, StandardWriteOption.CLOSE); 

2.Mark the stream data boundary by sending an empty stream packet

// Mark the stream data boundary
out.writeAsync(ByteBuffer.allocateDirect(0).asReadOnlyBuffer()); 
  1. Flush is also in the form of async RPC, because putBlock needs to fetch bcsId (Raft log index)

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-6137

How was this patch tested?

Use the existing UT

@guohao-rosickyguohao-rosicky changed the title Guohao hdds 6137 devHDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputMar 15, 2022

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guohao-rosicky Improve this. LGTM over all. TestBlockDataStreamOutput has some error, can you confirm?

@szetszwoszetszwoMar 16, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky, Writing an empty buffer may not work as expected. In the streaming model, data is continuous but not block-by-block. Both sender and receiver must consider that the data is a stream of bytes.

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

Let me think about how to make it work.

@guohao-rosickyguohao-rosickyMar 16, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment @szetszwo . I wrote a piece of pseudo-code, Ideas about the modification of org.apache.ratis.netty.server.DataStreamManagement#writeTo.

image

What do you think of this idea? There is no need to send this empty buffer rpc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , this is a standard problem in streaming. The standard solution is to write the length with the data. Let's don't change the API for it.

@guohao-rosickyguohao-rosickyMar 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @szetszwo
I have a question, about

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

I noticed that in HDFS, marking the end of stream is also represented by an empty package.
In Ratis Stream, sticky packets have already been dealt with.

org.apache.ratis.netty.NettyDataStreamUtils#encodeDataStreamRequestHeader
https://github.com/apache/ratis/blob/master/ratis-netty/src/main/java/org/apache/ratis/netty/NettyDataStreamUtils.java#L90

@captainzmc tested the code from some time ago and did not find this problem

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

Thanks @szetszwo, Patch has been applied

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , please fix the checkstyle warnings and test the change manually. I have not tested it. Thanks.

@captainzmc

Copy link
Copy Markdown
Member

Thanks @guohao-rosicky@szetszwo update this. I used this PR test and found that the file failed to write. In DataNode IndexOutOfBoundsException will appear.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks. Let me test the serialization/deserialization code.

@szetszwo

Copy link
Copy Markdown
Contributor

Found the bug: ByteBu.nioBuffers(index, length) somehow is not working. Let me fix it.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky, Fixed the bug; see https://issues.apache.org/jira/secure/attachment/13041255/3195_TestStreamDataChannel.patch

Thanks @szetszwo for the update. Using new patch I found IndexOutOfBoundsException still appear.
I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks a lot for testing it!

I added one more test for the serialization and buffers. It worked fine. See https://issues.apache.org/jira/secure/attachment/13041275/3195_testBuffers.patch

I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.

It seems there are some synchronization problems. Let me check.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , could you post the test code?

@captainzmc

Copy link
Copy Markdown
Member

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Actually there a problem with my previous description. I noticed that the when I use 30 threads writing, we’ll get same error.

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@szetszwo Here is my datanode log. https://issues.apache.org/jira/secure/attachment/13041276/ozone-root-datanode-9-29-173-57.log
In the test, I wrote 30 files concurrently. Each file is 128MB and the chunk size is 1MB

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

@szetszwo

Copy link
Copy Markdown
Contributor

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Thanks for sharing the test code. It looks good.

@captainzmc

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

Thanks @szetszwo for update this. I had just test this patch, we still get same error. Just as the same as this.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an abstract class instead static methods. Then, KeyDataStreamOutput can extends it. If we need to add SmallFileDataStreamOutput, it can extends it too.

package org.apache.hadoop.ozone.client.io;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.scm.storage.ByteBufferStreamOutput;
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
import org.apache.ratis.protocol.exceptions.RaftRetryFailureException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractDataStreamOutput implements ByteBufferStreamOutput {
private final Map<Class<? extends Throwable>, RetryPolicy> retryPolicyMap;
private int retryCount;
private boolean isException;
protected AbstractDataStreamOutput() {
this.retryPolicyMap = HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL));
}
@VisibleForTesting
public int getRetryCount() {
return retryCount;
}
void resetRetryCount() {
retryCount = 0;
}
boolean isException() {
return isException;
}
/**
* Checks if the provided exception signifies retry failure in ratis client.
* In case of retry failure, ratis client throws RaftRetryFailureException
* and all succeeding operations are failed with AlreadyClosedException.
*/
boolean checkForRetryFailure(Throwable t) {
return t instanceof RaftRetryFailureException
|| t instanceof AlreadyClosedException;
}
// Every container specific exception from datatnode will be seen as
// StorageContainerException
boolean checkIfContainerToExclude(Throwable t) {
return t instanceof StorageContainerException;
}
private void setExceptionAndThrow(IOException ioe) throws IOException {
isException = true;
throw ioe;
}
void handleRetry(IOException exception) throws IOException {
RetryPolicy retryPolicy = retryPolicyMap
.get(HddsClientUtils.checkForException(exception).getClass());
if (retryPolicy == null) {
retryPolicy = retryPolicyMap.get(Exception.class);
}
handleRetry(exception, retryPolicy);
}
private void handleRetry(IOException exception, RetryPolicy retryPolicy)
throws IOException {
RetryPolicy.RetryAction action = null;
try {
action = retryPolicy.shouldRetry(exception, retryCount, 0, true);
} catch (Exception e) {
setExceptionAndThrow(new IOException(e));
}
if (action.action == RetryPolicy.RetryAction.RetryDecision.FAIL) {
String msg = "";
if (action.reason != null) {
msg = "Retry request failed. " + action.reason;
//LOG.error(msg, exception);
}
setExceptionAndThrow(new IOException(msg, exception));
}
// Throw the exception if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
//LOG.warn("Interrupted while trying for retry");
setExceptionAndThrow(exception);
}
Preconditions.checkArgument(
action.action == RetryPolicy.RetryAction.RetryDecision.RETRY);
if (action.delayMillis > 0) {
try {
Thread.sleep(action.delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
IOException ioe = (IOException) new InterruptedIOException(
"Interrupted: action=" + action + ", retry policy=" + retryPolicy)
.initCause(e);
setExceptionAndThrow(ioe);
}
}
retryCount++;
}
}

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Ok, I'll change it

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Has been modified. @szetszwo Please take a look.Thanks.

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , please review it yourself -- go through the change and remove the unused code (e.g. ConsumerWithIOException). Thanks.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , thanks for the update. Some comments inlined.

});
}

private CompletableFuture<ContainerCommandResponseProto> runCommandAsync(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCommandAsync is not used. Let's add it later.

Comment on lines +194 to +197
LOG.error(
"Get stream data channel error Malformed request " +
"containerID: {} msg: {}",
container.getContainerData().getContainerID(), msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove LOG.error(..) since it already throws an exception.

ContainerCommandRequestProto request, KeyValueContainer kvContainer,
DispatcherContext dispatcherContext) {
if (!request.hasWriteChunk()) {
BlockID blockID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final.

@@ -97,12 +85,8 @@ enum StreamAction {
*/
@VisibleForTesting
public KeyDataStreamOutput() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere. Let's remove it.

Comment on lines -161 to -162
this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
config.getMaxRetryCount(), config.getRetryInterval());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the retryPolicyMap to the super constructor.

Comment on lines +116 to +119
super(HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is passed incorrectly. Below is the original map.

- this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
- config.getMaxRetryCount(), config.getRetryInterval());

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @guohao-rosicky. The change looks good.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo
szetszwo merged commit 9546d4c into apache:HDDS-4454Mar 28, 2022
@szetszwoszetszwo changed the title HDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputHDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.Mar 28, 2022
szetszwo pushed a commit to szetszwo/ozone that referenced this pull request May 6, 2022
captainzmc pushed a commit to captainzmc/hadoop-ozone that referenced this pull request Jul 4, 2022
szetszwo pushed a commit that referenced this pull request Oct 25, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
szetszwo pushed a commit that referenced this pull request Nov 7, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
(cherry picked from commit 99b414d)
szetszwo pushed a commit that referenced this pull request Dec 1, 2022
szetszwo pushed a commit that referenced this pull request Dec 16, 2022
nishitpatira pushed a commit to nishitpatira/ozone that referenced this pull request Dec 16, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@guohao-rosicky@szetszwo@captainzmc
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. by guohao-rosicky · Pull Request #3195 · apache/ozone · GitHub
Skip to content

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. - #3195

Merged
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev
Mar 28, 2022
Merged

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.#3195
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev

Conversation

@guohao-rosicky

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Optimized the putBlock method of BlockDataStreamOutput

  1. PutBlock does not use RaftRpc, but through the stream close() method
// putBlock metadata
ByteBuffer buf =
ContainerCommandRequestMessage.toMessage(putBlockRequestProto, null)
.getContent().asReadOnlyByteBuffer();
out.writeAsync(buf, StandardWriteOption.CLOSE); 

2.Mark the stream data boundary by sending an empty stream packet

// Mark the stream data boundary
out.writeAsync(ByteBuffer.allocateDirect(0).asReadOnlyBuffer()); 
  1. Flush is also in the form of async RPC, because putBlock needs to fetch bcsId (Raft log index)

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-6137

How was this patch tested?

Use the existing UT

@guohao-rosickyguohao-rosicky changed the title Guohao hdds 6137 devHDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputMar 15, 2022

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guohao-rosicky Improve this. LGTM over all. TestBlockDataStreamOutput has some error, can you confirm?

@szetszwoszetszwoMar 16, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky, Writing an empty buffer may not work as expected. In the streaming model, data is continuous but not block-by-block. Both sender and receiver must consider that the data is a stream of bytes.

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

Let me think about how to make it work.

@guohao-rosickyguohao-rosickyMar 16, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment @szetszwo . I wrote a piece of pseudo-code, Ideas about the modification of org.apache.ratis.netty.server.DataStreamManagement#writeTo.

image

What do you think of this idea? There is no need to send this empty buffer rpc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , this is a standard problem in streaming. The standard solution is to write the length with the data. Let's don't change the API for it.

@guohao-rosickyguohao-rosickyMar 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @szetszwo
I have a question, about

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

I noticed that in HDFS, marking the end of stream is also represented by an empty package.
In Ratis Stream, sticky packets have already been dealt with.

org.apache.ratis.netty.NettyDataStreamUtils#encodeDataStreamRequestHeader
https://github.com/apache/ratis/blob/master/ratis-netty/src/main/java/org/apache/ratis/netty/NettyDataStreamUtils.java#L90

@captainzmc tested the code from some time ago and did not find this problem

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

Thanks @szetszwo, Patch has been applied

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , please fix the checkstyle warnings and test the change manually. I have not tested it. Thanks.

@captainzmc

Copy link
Copy Markdown
Member

Thanks @guohao-rosicky@szetszwo update this. I used this PR test and found that the file failed to write. In DataNode IndexOutOfBoundsException will appear.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks. Let me test the serialization/deserialization code.

@szetszwo

Copy link
Copy Markdown
Contributor

Found the bug: ByteBu.nioBuffers(index, length) somehow is not working. Let me fix it.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky, Fixed the bug; see https://issues.apache.org/jira/secure/attachment/13041255/3195_TestStreamDataChannel.patch

Thanks @szetszwo for the update. Using new patch I found IndexOutOfBoundsException still appear.
I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks a lot for testing it!

I added one more test for the serialization and buffers. It worked fine. See https://issues.apache.org/jira/secure/attachment/13041275/3195_testBuffers.patch

I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.

It seems there are some synchronization problems. Let me check.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , could you post the test code?

@captainzmc

Copy link
Copy Markdown
Member

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Actually there a problem with my previous description. I noticed that the when I use 30 threads writing, we’ll get same error.

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@szetszwo Here is my datanode log. https://issues.apache.org/jira/secure/attachment/13041276/ozone-root-datanode-9-29-173-57.log
In the test, I wrote 30 files concurrently. Each file is 128MB and the chunk size is 1MB

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

@szetszwo

Copy link
Copy Markdown
Contributor

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Thanks for sharing the test code. It looks good.

@captainzmc

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

Thanks @szetszwo for update this. I had just test this patch, we still get same error. Just as the same as this.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an abstract class instead static methods. Then, KeyDataStreamOutput can extends it. If we need to add SmallFileDataStreamOutput, it can extends it too.

package org.apache.hadoop.ozone.client.io;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.scm.storage.ByteBufferStreamOutput;
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
import org.apache.ratis.protocol.exceptions.RaftRetryFailureException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractDataStreamOutput implements ByteBufferStreamOutput {
private final Map<Class<? extends Throwable>, RetryPolicy> retryPolicyMap;
private int retryCount;
private boolean isException;
protected AbstractDataStreamOutput() {
this.retryPolicyMap = HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL));
}
@VisibleForTesting
public int getRetryCount() {
return retryCount;
}
void resetRetryCount() {
retryCount = 0;
}
boolean isException() {
return isException;
}
/**
* Checks if the provided exception signifies retry failure in ratis client.
* In case of retry failure, ratis client throws RaftRetryFailureException
* and all succeeding operations are failed with AlreadyClosedException.
*/
boolean checkForRetryFailure(Throwable t) {
return t instanceof RaftRetryFailureException
|| t instanceof AlreadyClosedException;
}
// Every container specific exception from datatnode will be seen as
// StorageContainerException
boolean checkIfContainerToExclude(Throwable t) {
return t instanceof StorageContainerException;
}
private void setExceptionAndThrow(IOException ioe) throws IOException {
isException = true;
throw ioe;
}
void handleRetry(IOException exception) throws IOException {
RetryPolicy retryPolicy = retryPolicyMap
.get(HddsClientUtils.checkForException(exception).getClass());
if (retryPolicy == null) {
retryPolicy = retryPolicyMap.get(Exception.class);
}
handleRetry(exception, retryPolicy);
}
private void handleRetry(IOException exception, RetryPolicy retryPolicy)
throws IOException {
RetryPolicy.RetryAction action = null;
try {
action = retryPolicy.shouldRetry(exception, retryCount, 0, true);
} catch (Exception e) {
setExceptionAndThrow(new IOException(e));
}
if (action.action == RetryPolicy.RetryAction.RetryDecision.FAIL) {
String msg = "";
if (action.reason != null) {
msg = "Retry request failed. " + action.reason;
//LOG.error(msg, exception);
}
setExceptionAndThrow(new IOException(msg, exception));
}
// Throw the exception if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
//LOG.warn("Interrupted while trying for retry");
setExceptionAndThrow(exception);
}
Preconditions.checkArgument(
action.action == RetryPolicy.RetryAction.RetryDecision.RETRY);
if (action.delayMillis > 0) {
try {
Thread.sleep(action.delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
IOException ioe = (IOException) new InterruptedIOException(
"Interrupted: action=" + action + ", retry policy=" + retryPolicy)
.initCause(e);
setExceptionAndThrow(ioe);
}
}
retryCount++;
}
}

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Ok, I'll change it

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Has been modified. @szetszwo Please take a look.Thanks.

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , please review it yourself -- go through the change and remove the unused code (e.g. ConsumerWithIOException). Thanks.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , thanks for the update. Some comments inlined.

});
}

private CompletableFuture<ContainerCommandResponseProto> runCommandAsync(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCommandAsync is not used. Let's add it later.

Comment on lines +194 to +197
LOG.error(
"Get stream data channel error Malformed request " +
"containerID: {} msg: {}",
container.getContainerData().getContainerID(), msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove LOG.error(..) since it already throws an exception.

ContainerCommandRequestProto request, KeyValueContainer kvContainer,
DispatcherContext dispatcherContext) {
if (!request.hasWriteChunk()) {
BlockID blockID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final.

@@ -97,12 +85,8 @@ enum StreamAction {
*/
@VisibleForTesting
public KeyDataStreamOutput() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere. Let's remove it.

Comment on lines -161 to -162
this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
config.getMaxRetryCount(), config.getRetryInterval());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the retryPolicyMap to the super constructor.

Comment on lines +116 to +119
super(HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is passed incorrectly. Below is the original map.

- this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
- config.getMaxRetryCount(), config.getRetryInterval());

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @guohao-rosicky. The change looks good.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo
szetszwo merged commit 9546d4c into apache:HDDS-4454Mar 28, 2022
@szetszwoszetszwo changed the title HDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputHDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.Mar 28, 2022
szetszwo pushed a commit to szetszwo/ozone that referenced this pull request May 6, 2022
captainzmc pushed a commit to captainzmc/hadoop-ozone that referenced this pull request Jul 4, 2022
szetszwo pushed a commit that referenced this pull request Oct 25, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
szetszwo pushed a commit that referenced this pull request Nov 7, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
(cherry picked from commit 99b414d)
szetszwo pushed a commit that referenced this pull request Dec 1, 2022
szetszwo pushed a commit that referenced this pull request Dec 16, 2022
nishitpatira pushed a commit to nishitpatira/ozone that referenced this pull request Dec 16, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@guohao-rosicky@szetszwo@captainzmc
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. by guohao-rosicky · Pull Request #3195 · apache/ozone · GitHub
Skip to content

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. - #3195

Merged
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev
Mar 28, 2022
Merged

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.#3195
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev

Conversation

@guohao-rosicky

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Optimized the putBlock method of BlockDataStreamOutput

  1. PutBlock does not use RaftRpc, but through the stream close() method
// putBlock metadata
ByteBuffer buf =
ContainerCommandRequestMessage.toMessage(putBlockRequestProto, null)
.getContent().asReadOnlyByteBuffer();
out.writeAsync(buf, StandardWriteOption.CLOSE); 

2.Mark the stream data boundary by sending an empty stream packet

// Mark the stream data boundary
out.writeAsync(ByteBuffer.allocateDirect(0).asReadOnlyBuffer()); 
  1. Flush is also in the form of async RPC, because putBlock needs to fetch bcsId (Raft log index)

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-6137

How was this patch tested?

Use the existing UT

@guohao-rosickyguohao-rosicky changed the title Guohao hdds 6137 devHDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputMar 15, 2022

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guohao-rosicky Improve this. LGTM over all. TestBlockDataStreamOutput has some error, can you confirm?

@szetszwoszetszwoMar 16, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky, Writing an empty buffer may not work as expected. In the streaming model, data is continuous but not block-by-block. Both sender and receiver must consider that the data is a stream of bytes.

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

Let me think about how to make it work.

@guohao-rosickyguohao-rosickyMar 16, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment @szetszwo . I wrote a piece of pseudo-code, Ideas about the modification of org.apache.ratis.netty.server.DataStreamManagement#writeTo.

image

What do you think of this idea? There is no need to send this empty buffer rpc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , this is a standard problem in streaming. The standard solution is to write the length with the data. Let's don't change the API for it.

@guohao-rosickyguohao-rosickyMar 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @szetszwo
I have a question, about

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

I noticed that in HDFS, marking the end of stream is also represented by an empty package.
In Ratis Stream, sticky packets have already been dealt with.

org.apache.ratis.netty.NettyDataStreamUtils#encodeDataStreamRequestHeader
https://github.com/apache/ratis/blob/master/ratis-netty/src/main/java/org/apache/ratis/netty/NettyDataStreamUtils.java#L90

@captainzmc tested the code from some time ago and did not find this problem

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

Thanks @szetszwo, Patch has been applied

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , please fix the checkstyle warnings and test the change manually. I have not tested it. Thanks.

@captainzmc

Copy link
Copy Markdown
Member

Thanks @guohao-rosicky@szetszwo update this. I used this PR test and found that the file failed to write. In DataNode IndexOutOfBoundsException will appear.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks. Let me test the serialization/deserialization code.

@szetszwo

Copy link
Copy Markdown
Contributor

Found the bug: ByteBu.nioBuffers(index, length) somehow is not working. Let me fix it.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky, Fixed the bug; see https://issues.apache.org/jira/secure/attachment/13041255/3195_TestStreamDataChannel.patch

Thanks @szetszwo for the update. Using new patch I found IndexOutOfBoundsException still appear.
I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks a lot for testing it!

I added one more test for the serialization and buffers. It worked fine. See https://issues.apache.org/jira/secure/attachment/13041275/3195_testBuffers.patch

I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.

It seems there are some synchronization problems. Let me check.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , could you post the test code?

@captainzmc

Copy link
Copy Markdown
Member

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Actually there a problem with my previous description. I noticed that the when I use 30 threads writing, we’ll get same error.

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@szetszwo Here is my datanode log. https://issues.apache.org/jira/secure/attachment/13041276/ozone-root-datanode-9-29-173-57.log
In the test, I wrote 30 files concurrently. Each file is 128MB and the chunk size is 1MB

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

@szetszwo

Copy link
Copy Markdown
Contributor

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Thanks for sharing the test code. It looks good.

@captainzmc

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

Thanks @szetszwo for update this. I had just test this patch, we still get same error. Just as the same as this.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an abstract class instead static methods. Then, KeyDataStreamOutput can extends it. If we need to add SmallFileDataStreamOutput, it can extends it too.

package org.apache.hadoop.ozone.client.io;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.scm.storage.ByteBufferStreamOutput;
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
import org.apache.ratis.protocol.exceptions.RaftRetryFailureException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractDataStreamOutput implements ByteBufferStreamOutput {
private final Map<Class<? extends Throwable>, RetryPolicy> retryPolicyMap;
private int retryCount;
private boolean isException;
protected AbstractDataStreamOutput() {
this.retryPolicyMap = HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL));
}
@VisibleForTesting
public int getRetryCount() {
return retryCount;
}
void resetRetryCount() {
retryCount = 0;
}
boolean isException() {
return isException;
}
/**
* Checks if the provided exception signifies retry failure in ratis client.
* In case of retry failure, ratis client throws RaftRetryFailureException
* and all succeeding operations are failed with AlreadyClosedException.
*/
boolean checkForRetryFailure(Throwable t) {
return t instanceof RaftRetryFailureException
|| t instanceof AlreadyClosedException;
}
// Every container specific exception from datatnode will be seen as
// StorageContainerException
boolean checkIfContainerToExclude(Throwable t) {
return t instanceof StorageContainerException;
}
private void setExceptionAndThrow(IOException ioe) throws IOException {
isException = true;
throw ioe;
}
void handleRetry(IOException exception) throws IOException {
RetryPolicy retryPolicy = retryPolicyMap
.get(HddsClientUtils.checkForException(exception).getClass());
if (retryPolicy == null) {
retryPolicy = retryPolicyMap.get(Exception.class);
}
handleRetry(exception, retryPolicy);
}
private void handleRetry(IOException exception, RetryPolicy retryPolicy)
throws IOException {
RetryPolicy.RetryAction action = null;
try {
action = retryPolicy.shouldRetry(exception, retryCount, 0, true);
} catch (Exception e) {
setExceptionAndThrow(new IOException(e));
}
if (action.action == RetryPolicy.RetryAction.RetryDecision.FAIL) {
String msg = "";
if (action.reason != null) {
msg = "Retry request failed. " + action.reason;
//LOG.error(msg, exception);
}
setExceptionAndThrow(new IOException(msg, exception));
}
// Throw the exception if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
//LOG.warn("Interrupted while trying for retry");
setExceptionAndThrow(exception);
}
Preconditions.checkArgument(
action.action == RetryPolicy.RetryAction.RetryDecision.RETRY);
if (action.delayMillis > 0) {
try {
Thread.sleep(action.delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
IOException ioe = (IOException) new InterruptedIOException(
"Interrupted: action=" + action + ", retry policy=" + retryPolicy)
.initCause(e);
setExceptionAndThrow(ioe);
}
}
retryCount++;
}
}

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Ok, I'll change it

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Has been modified. @szetszwo Please take a look.Thanks.

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , please review it yourself -- go through the change and remove the unused code (e.g. ConsumerWithIOException). Thanks.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , thanks for the update. Some comments inlined.

});
}

private CompletableFuture<ContainerCommandResponseProto> runCommandAsync(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCommandAsync is not used. Let's add it later.

Comment on lines +194 to +197
LOG.error(
"Get stream data channel error Malformed request " +
"containerID: {} msg: {}",
container.getContainerData().getContainerID(), msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove LOG.error(..) since it already throws an exception.

ContainerCommandRequestProto request, KeyValueContainer kvContainer,
DispatcherContext dispatcherContext) {
if (!request.hasWriteChunk()) {
BlockID blockID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final.

@@ -97,12 +85,8 @@ enum StreamAction {
*/
@VisibleForTesting
public KeyDataStreamOutput() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere. Let's remove it.

Comment on lines -161 to -162
this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
config.getMaxRetryCount(), config.getRetryInterval());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the retryPolicyMap to the super constructor.

Comment on lines +116 to +119
super(HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is passed incorrectly. Below is the original map.

- this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
- config.getMaxRetryCount(), config.getRetryInterval());

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @guohao-rosicky. The change looks good.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo
szetszwo merged commit 9546d4c into apache:HDDS-4454Mar 28, 2022
@szetszwoszetszwo changed the title HDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputHDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.Mar 28, 2022
szetszwo pushed a commit to szetszwo/ozone that referenced this pull request May 6, 2022
captainzmc pushed a commit to captainzmc/hadoop-ozone that referenced this pull request Jul 4, 2022
szetszwo pushed a commit that referenced this pull request Oct 25, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
szetszwo pushed a commit that referenced this pull request Nov 7, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
(cherry picked from commit 99b414d)
szetszwo pushed a commit that referenced this pull request Dec 1, 2022
szetszwo pushed a commit that referenced this pull request Dec 16, 2022
nishitpatira pushed a commit to nishitpatira/ozone that referenced this pull request Dec 16, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@guohao-rosicky@szetszwo@captainzmc
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. by guohao-rosicky · Pull Request #3195 · apache/ozone · GitHub
Skip to content

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. - #3195

Merged
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev
Mar 28, 2022
Merged

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.#3195
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev

Conversation

@guohao-rosicky

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Optimized the putBlock method of BlockDataStreamOutput

  1. PutBlock does not use RaftRpc, but through the stream close() method
// putBlock metadata
ByteBuffer buf =
ContainerCommandRequestMessage.toMessage(putBlockRequestProto, null)
.getContent().asReadOnlyByteBuffer();
out.writeAsync(buf, StandardWriteOption.CLOSE); 

2.Mark the stream data boundary by sending an empty stream packet

// Mark the stream data boundary
out.writeAsync(ByteBuffer.allocateDirect(0).asReadOnlyBuffer()); 
  1. Flush is also in the form of async RPC, because putBlock needs to fetch bcsId (Raft log index)

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-6137

How was this patch tested?

Use the existing UT

@guohao-rosickyguohao-rosicky changed the title Guohao hdds 6137 devHDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputMar 15, 2022

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guohao-rosicky Improve this. LGTM over all. TestBlockDataStreamOutput has some error, can you confirm?

@szetszwoszetszwoMar 16, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky, Writing an empty buffer may not work as expected. In the streaming model, data is continuous but not block-by-block. Both sender and receiver must consider that the data is a stream of bytes.

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

Let me think about how to make it work.

@guohao-rosickyguohao-rosickyMar 16, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment @szetszwo . I wrote a piece of pseudo-code, Ideas about the modification of org.apache.ratis.netty.server.DataStreamManagement#writeTo.

image

What do you think of this idea? There is no need to send this empty buffer rpc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , this is a standard problem in streaming. The standard solution is to write the length with the data. Let's don't change the API for it.

@guohao-rosickyguohao-rosickyMar 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @szetszwo
I have a question, about

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

I noticed that in HDFS, marking the end of stream is also represented by an empty package.
In Ratis Stream, sticky packets have already been dealt with.

org.apache.ratis.netty.NettyDataStreamUtils#encodeDataStreamRequestHeader
https://github.com/apache/ratis/blob/master/ratis-netty/src/main/java/org/apache/ratis/netty/NettyDataStreamUtils.java#L90

@captainzmc tested the code from some time ago and did not find this problem

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

Thanks @szetszwo, Patch has been applied

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , please fix the checkstyle warnings and test the change manually. I have not tested it. Thanks.

@captainzmc

Copy link
Copy Markdown
Member

Thanks @guohao-rosicky@szetszwo update this. I used this PR test and found that the file failed to write. In DataNode IndexOutOfBoundsException will appear.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks. Let me test the serialization/deserialization code.

@szetszwo

Copy link
Copy Markdown
Contributor

Found the bug: ByteBu.nioBuffers(index, length) somehow is not working. Let me fix it.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky, Fixed the bug; see https://issues.apache.org/jira/secure/attachment/13041255/3195_TestStreamDataChannel.patch

Thanks @szetszwo for the update. Using new patch I found IndexOutOfBoundsException still appear.
I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks a lot for testing it!

I added one more test for the serialization and buffers. It worked fine. See https://issues.apache.org/jira/secure/attachment/13041275/3195_testBuffers.patch

I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.

It seems there are some synchronization problems. Let me check.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , could you post the test code?

@captainzmc

Copy link
Copy Markdown
Member

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Actually there a problem with my previous description. I noticed that the when I use 30 threads writing, we’ll get same error.

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@szetszwo Here is my datanode log. https://issues.apache.org/jira/secure/attachment/13041276/ozone-root-datanode-9-29-173-57.log
In the test, I wrote 30 files concurrently. Each file is 128MB and the chunk size is 1MB

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

@szetszwo

Copy link
Copy Markdown
Contributor

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Thanks for sharing the test code. It looks good.

@captainzmc

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

Thanks @szetszwo for update this. I had just test this patch, we still get same error. Just as the same as this.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an abstract class instead static methods. Then, KeyDataStreamOutput can extends it. If we need to add SmallFileDataStreamOutput, it can extends it too.

package org.apache.hadoop.ozone.client.io;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.scm.storage.ByteBufferStreamOutput;
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
import org.apache.ratis.protocol.exceptions.RaftRetryFailureException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractDataStreamOutput implements ByteBufferStreamOutput {
private final Map<Class<? extends Throwable>, RetryPolicy> retryPolicyMap;
private int retryCount;
private boolean isException;
protected AbstractDataStreamOutput() {
this.retryPolicyMap = HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL));
}
@VisibleForTesting
public int getRetryCount() {
return retryCount;
}
void resetRetryCount() {
retryCount = 0;
}
boolean isException() {
return isException;
}
/**
* Checks if the provided exception signifies retry failure in ratis client.
* In case of retry failure, ratis client throws RaftRetryFailureException
* and all succeeding operations are failed with AlreadyClosedException.
*/
boolean checkForRetryFailure(Throwable t) {
return t instanceof RaftRetryFailureException
|| t instanceof AlreadyClosedException;
}
// Every container specific exception from datatnode will be seen as
// StorageContainerException
boolean checkIfContainerToExclude(Throwable t) {
return t instanceof StorageContainerException;
}
private void setExceptionAndThrow(IOException ioe) throws IOException {
isException = true;
throw ioe;
}
void handleRetry(IOException exception) throws IOException {
RetryPolicy retryPolicy = retryPolicyMap
.get(HddsClientUtils.checkForException(exception).getClass());
if (retryPolicy == null) {
retryPolicy = retryPolicyMap.get(Exception.class);
}
handleRetry(exception, retryPolicy);
}
private void handleRetry(IOException exception, RetryPolicy retryPolicy)
throws IOException {
RetryPolicy.RetryAction action = null;
try {
action = retryPolicy.shouldRetry(exception, retryCount, 0, true);
} catch (Exception e) {
setExceptionAndThrow(new IOException(e));
}
if (action.action == RetryPolicy.RetryAction.RetryDecision.FAIL) {
String msg = "";
if (action.reason != null) {
msg = "Retry request failed. " + action.reason;
//LOG.error(msg, exception);
}
setExceptionAndThrow(new IOException(msg, exception));
}
// Throw the exception if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
//LOG.warn("Interrupted while trying for retry");
setExceptionAndThrow(exception);
}
Preconditions.checkArgument(
action.action == RetryPolicy.RetryAction.RetryDecision.RETRY);
if (action.delayMillis > 0) {
try {
Thread.sleep(action.delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
IOException ioe = (IOException) new InterruptedIOException(
"Interrupted: action=" + action + ", retry policy=" + retryPolicy)
.initCause(e);
setExceptionAndThrow(ioe);
}
}
retryCount++;
}
}

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Ok, I'll change it

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Has been modified. @szetszwo Please take a look.Thanks.

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , please review it yourself -- go through the change and remove the unused code (e.g. ConsumerWithIOException). Thanks.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , thanks for the update. Some comments inlined.

});
}

private CompletableFuture<ContainerCommandResponseProto> runCommandAsync(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCommandAsync is not used. Let's add it later.

Comment on lines +194 to +197
LOG.error(
"Get stream data channel error Malformed request " +
"containerID: {} msg: {}",
container.getContainerData().getContainerID(), msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove LOG.error(..) since it already throws an exception.

ContainerCommandRequestProto request, KeyValueContainer kvContainer,
DispatcherContext dispatcherContext) {
if (!request.hasWriteChunk()) {
BlockID blockID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final.

@@ -97,12 +85,8 @@ enum StreamAction {
*/
@VisibleForTesting
public KeyDataStreamOutput() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere. Let's remove it.

Comment on lines -161 to -162
this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
config.getMaxRetryCount(), config.getRetryInterval());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the retryPolicyMap to the super constructor.

Comment on lines +116 to +119
super(HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is passed incorrectly. Below is the original map.

- this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
- config.getMaxRetryCount(), config.getRetryInterval());

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @guohao-rosicky. The change looks good.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo
szetszwo merged commit 9546d4c into apache:HDDS-4454Mar 28, 2022
@szetszwoszetszwo changed the title HDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputHDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.Mar 28, 2022
szetszwo pushed a commit to szetszwo/ozone that referenced this pull request May 6, 2022
captainzmc pushed a commit to captainzmc/hadoop-ozone that referenced this pull request Jul 4, 2022
szetszwo pushed a commit that referenced this pull request Oct 25, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
szetszwo pushed a commit that referenced this pull request Nov 7, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
(cherry picked from commit 99b414d)
szetszwo pushed a commit that referenced this pull request Dec 1, 2022
szetszwo pushed a commit that referenced this pull request Dec 16, 2022
nishitpatira pushed a commit to nishitpatira/ozone that referenced this pull request Dec 16, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@guohao-rosicky@szetszwo@captainzmc
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. by guohao-rosicky · Pull Request #3195 · apache/ozone · GitHub
Skip to content

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. - #3195

Merged
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev
Mar 28, 2022
Merged

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.#3195
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev

Conversation

@guohao-rosicky

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Optimized the putBlock method of BlockDataStreamOutput

  1. PutBlock does not use RaftRpc, but through the stream close() method
// putBlock metadata
ByteBuffer buf =
ContainerCommandRequestMessage.toMessage(putBlockRequestProto, null)
.getContent().asReadOnlyByteBuffer();
out.writeAsync(buf, StandardWriteOption.CLOSE); 

2.Mark the stream data boundary by sending an empty stream packet

// Mark the stream data boundary
out.writeAsync(ByteBuffer.allocateDirect(0).asReadOnlyBuffer()); 
  1. Flush is also in the form of async RPC, because putBlock needs to fetch bcsId (Raft log index)

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-6137

How was this patch tested?

Use the existing UT

@guohao-rosickyguohao-rosicky changed the title Guohao hdds 6137 devHDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputMar 15, 2022

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guohao-rosicky Improve this. LGTM over all. TestBlockDataStreamOutput has some error, can you confirm?

@szetszwoszetszwoMar 16, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky, Writing an empty buffer may not work as expected. In the streaming model, data is continuous but not block-by-block. Both sender and receiver must consider that the data is a stream of bytes.

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

Let me think about how to make it work.

@guohao-rosickyguohao-rosickyMar 16, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment @szetszwo . I wrote a piece of pseudo-code, Ideas about the modification of org.apache.ratis.netty.server.DataStreamManagement#writeTo.

image

What do you think of this idea? There is no need to send this empty buffer rpc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , this is a standard problem in streaming. The standard solution is to write the length with the data. Let's don't change the API for it.

@guohao-rosickyguohao-rosickyMar 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @szetszwo
I have a question, about

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

I noticed that in HDFS, marking the end of stream is also represented by an empty package.
In Ratis Stream, sticky packets have already been dealt with.

org.apache.ratis.netty.NettyDataStreamUtils#encodeDataStreamRequestHeader
https://github.com/apache/ratis/blob/master/ratis-netty/src/main/java/org/apache/ratis/netty/NettyDataStreamUtils.java#L90

@captainzmc tested the code from some time ago and did not find this problem

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

Thanks @szetszwo, Patch has been applied

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , please fix the checkstyle warnings and test the change manually. I have not tested it. Thanks.

@captainzmc

Copy link
Copy Markdown
Member

Thanks @guohao-rosicky@szetszwo update this. I used this PR test and found that the file failed to write. In DataNode IndexOutOfBoundsException will appear.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks. Let me test the serialization/deserialization code.

@szetszwo

Copy link
Copy Markdown
Contributor

Found the bug: ByteBu.nioBuffers(index, length) somehow is not working. Let me fix it.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky, Fixed the bug; see https://issues.apache.org/jira/secure/attachment/13041255/3195_TestStreamDataChannel.patch

Thanks @szetszwo for the update. Using new patch I found IndexOutOfBoundsException still appear.
I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks a lot for testing it!

I added one more test for the serialization and buffers. It worked fine. See https://issues.apache.org/jira/secure/attachment/13041275/3195_testBuffers.patch

I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.

It seems there are some synchronization problems. Let me check.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , could you post the test code?

@captainzmc

Copy link
Copy Markdown
Member

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Actually there a problem with my previous description. I noticed that the when I use 30 threads writing, we’ll get same error.

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@szetszwo Here is my datanode log. https://issues.apache.org/jira/secure/attachment/13041276/ozone-root-datanode-9-29-173-57.log
In the test, I wrote 30 files concurrently. Each file is 128MB and the chunk size is 1MB

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

@szetszwo

Copy link
Copy Markdown
Contributor

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Thanks for sharing the test code. It looks good.

@captainzmc

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

Thanks @szetszwo for update this. I had just test this patch, we still get same error. Just as the same as this.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an abstract class instead static methods. Then, KeyDataStreamOutput can extends it. If we need to add SmallFileDataStreamOutput, it can extends it too.

package org.apache.hadoop.ozone.client.io;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.scm.storage.ByteBufferStreamOutput;
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
import org.apache.ratis.protocol.exceptions.RaftRetryFailureException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractDataStreamOutput implements ByteBufferStreamOutput {
private final Map<Class<? extends Throwable>, RetryPolicy> retryPolicyMap;
private int retryCount;
private boolean isException;
protected AbstractDataStreamOutput() {
this.retryPolicyMap = HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL));
}
@VisibleForTesting
public int getRetryCount() {
return retryCount;
}
void resetRetryCount() {
retryCount = 0;
}
boolean isException() {
return isException;
}
/**
* Checks if the provided exception signifies retry failure in ratis client.
* In case of retry failure, ratis client throws RaftRetryFailureException
* and all succeeding operations are failed with AlreadyClosedException.
*/
boolean checkForRetryFailure(Throwable t) {
return t instanceof RaftRetryFailureException
|| t instanceof AlreadyClosedException;
}
// Every container specific exception from datatnode will be seen as
// StorageContainerException
boolean checkIfContainerToExclude(Throwable t) {
return t instanceof StorageContainerException;
}
private void setExceptionAndThrow(IOException ioe) throws IOException {
isException = true;
throw ioe;
}
void handleRetry(IOException exception) throws IOException {
RetryPolicy retryPolicy = retryPolicyMap
.get(HddsClientUtils.checkForException(exception).getClass());
if (retryPolicy == null) {
retryPolicy = retryPolicyMap.get(Exception.class);
}
handleRetry(exception, retryPolicy);
}
private void handleRetry(IOException exception, RetryPolicy retryPolicy)
throws IOException {
RetryPolicy.RetryAction action = null;
try {
action = retryPolicy.shouldRetry(exception, retryCount, 0, true);
} catch (Exception e) {
setExceptionAndThrow(new IOException(e));
}
if (action.action == RetryPolicy.RetryAction.RetryDecision.FAIL) {
String msg = "";
if (action.reason != null) {
msg = "Retry request failed. " + action.reason;
//LOG.error(msg, exception);
}
setExceptionAndThrow(new IOException(msg, exception));
}
// Throw the exception if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
//LOG.warn("Interrupted while trying for retry");
setExceptionAndThrow(exception);
}
Preconditions.checkArgument(
action.action == RetryPolicy.RetryAction.RetryDecision.RETRY);
if (action.delayMillis > 0) {
try {
Thread.sleep(action.delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
IOException ioe = (IOException) new InterruptedIOException(
"Interrupted: action=" + action + ", retry policy=" + retryPolicy)
.initCause(e);
setExceptionAndThrow(ioe);
}
}
retryCount++;
}
}

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Ok, I'll change it

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Has been modified. @szetszwo Please take a look.Thanks.

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , please review it yourself -- go through the change and remove the unused code (e.g. ConsumerWithIOException). Thanks.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , thanks for the update. Some comments inlined.

});
}

private CompletableFuture<ContainerCommandResponseProto> runCommandAsync(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCommandAsync is not used. Let's add it later.

Comment on lines +194 to +197
LOG.error(
"Get stream data channel error Malformed request " +
"containerID: {} msg: {}",
container.getContainerData().getContainerID(), msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove LOG.error(..) since it already throws an exception.

ContainerCommandRequestProto request, KeyValueContainer kvContainer,
DispatcherContext dispatcherContext) {
if (!request.hasWriteChunk()) {
BlockID blockID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final.

@@ -97,12 +85,8 @@ enum StreamAction {
*/
@VisibleForTesting
public KeyDataStreamOutput() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere. Let's remove it.

Comment on lines -161 to -162
this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
config.getMaxRetryCount(), config.getRetryInterval());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the retryPolicyMap to the super constructor.

Comment on lines +116 to +119
super(HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is passed incorrectly. Below is the original map.

- this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
- config.getMaxRetryCount(), config.getRetryInterval());

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @guohao-rosicky. The change looks good.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo
szetszwo merged commit 9546d4c into apache:HDDS-4454Mar 28, 2022
@szetszwoszetszwo changed the title HDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputHDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.Mar 28, 2022
szetszwo pushed a commit to szetszwo/ozone that referenced this pull request May 6, 2022
captainzmc pushed a commit to captainzmc/hadoop-ozone that referenced this pull request Jul 4, 2022
szetszwo pushed a commit that referenced this pull request Oct 25, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
szetszwo pushed a commit that referenced this pull request Nov 7, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
(cherry picked from commit 99b414d)
szetszwo pushed a commit that referenced this pull request Dec 1, 2022
szetszwo pushed a commit that referenced this pull request Dec 16, 2022
nishitpatira pushed a commit to nishitpatira/ozone that referenced this pull request Dec 16, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@guohao-rosicky@szetszwo@captainzmc
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. by guohao-rosicky · Pull Request #3195 · apache/ozone · GitHub
Skip to content

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. - #3195

Merged
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev
Mar 28, 2022
Merged

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.#3195
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev

Conversation

@guohao-rosicky

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Optimized the putBlock method of BlockDataStreamOutput

  1. PutBlock does not use RaftRpc, but through the stream close() method
// putBlock metadata
ByteBuffer buf =
ContainerCommandRequestMessage.toMessage(putBlockRequestProto, null)
.getContent().asReadOnlyByteBuffer();
out.writeAsync(buf, StandardWriteOption.CLOSE); 

2.Mark the stream data boundary by sending an empty stream packet

// Mark the stream data boundary
out.writeAsync(ByteBuffer.allocateDirect(0).asReadOnlyBuffer()); 
  1. Flush is also in the form of async RPC, because putBlock needs to fetch bcsId (Raft log index)

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-6137

How was this patch tested?

Use the existing UT

@guohao-rosickyguohao-rosicky changed the title Guohao hdds 6137 devHDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputMar 15, 2022

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guohao-rosicky Improve this. LGTM over all. TestBlockDataStreamOutput has some error, can you confirm?

@szetszwoszetszwoMar 16, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky, Writing an empty buffer may not work as expected. In the streaming model, data is continuous but not block-by-block. Both sender and receiver must consider that the data is a stream of bytes.

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

Let me think about how to make it work.

@guohao-rosickyguohao-rosickyMar 16, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment @szetszwo . I wrote a piece of pseudo-code, Ideas about the modification of org.apache.ratis.netty.server.DataStreamManagement#writeTo.

image

What do you think of this idea? There is no need to send this empty buffer rpc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , this is a standard problem in streaming. The standard solution is to write the length with the data. Let's don't change the API for it.

@guohao-rosickyguohao-rosickyMar 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @szetszwo
I have a question, about

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

I noticed that in HDFS, marking the end of stream is also represented by an empty package.
In Ratis Stream, sticky packets have already been dealt with.

org.apache.ratis.netty.NettyDataStreamUtils#encodeDataStreamRequestHeader
https://github.com/apache/ratis/blob/master/ratis-netty/src/main/java/org/apache/ratis/netty/NettyDataStreamUtils.java#L90

@captainzmc tested the code from some time ago and did not find this problem

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

Thanks @szetszwo, Patch has been applied

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , please fix the checkstyle warnings and test the change manually. I have not tested it. Thanks.

@captainzmc

Copy link
Copy Markdown
Member

Thanks @guohao-rosicky@szetszwo update this. I used this PR test and found that the file failed to write. In DataNode IndexOutOfBoundsException will appear.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks. Let me test the serialization/deserialization code.

@szetszwo

Copy link
Copy Markdown
Contributor

Found the bug: ByteBu.nioBuffers(index, length) somehow is not working. Let me fix it.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky, Fixed the bug; see https://issues.apache.org/jira/secure/attachment/13041255/3195_TestStreamDataChannel.patch

Thanks @szetszwo for the update. Using new patch I found IndexOutOfBoundsException still appear.
I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks a lot for testing it!

I added one more test for the serialization and buffers. It worked fine. See https://issues.apache.org/jira/secure/attachment/13041275/3195_testBuffers.patch

I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.

It seems there are some synchronization problems. Let me check.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , could you post the test code?

@captainzmc

Copy link
Copy Markdown
Member

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Actually there a problem with my previous description. I noticed that the when I use 30 threads writing, we’ll get same error.

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@szetszwo Here is my datanode log. https://issues.apache.org/jira/secure/attachment/13041276/ozone-root-datanode-9-29-173-57.log
In the test, I wrote 30 files concurrently. Each file is 128MB and the chunk size is 1MB

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

@szetszwo

Copy link
Copy Markdown
Contributor

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Thanks for sharing the test code. It looks good.

@captainzmc

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

Thanks @szetszwo for update this. I had just test this patch, we still get same error. Just as the same as this.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an abstract class instead static methods. Then, KeyDataStreamOutput can extends it. If we need to add SmallFileDataStreamOutput, it can extends it too.

package org.apache.hadoop.ozone.client.io;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.scm.storage.ByteBufferStreamOutput;
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
import org.apache.ratis.protocol.exceptions.RaftRetryFailureException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractDataStreamOutput implements ByteBufferStreamOutput {
private final Map<Class<? extends Throwable>, RetryPolicy> retryPolicyMap;
private int retryCount;
private boolean isException;
protected AbstractDataStreamOutput() {
this.retryPolicyMap = HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL));
}
@VisibleForTesting
public int getRetryCount() {
return retryCount;
}
void resetRetryCount() {
retryCount = 0;
}
boolean isException() {
return isException;
}
/**
* Checks if the provided exception signifies retry failure in ratis client.
* In case of retry failure, ratis client throws RaftRetryFailureException
* and all succeeding operations are failed with AlreadyClosedException.
*/
boolean checkForRetryFailure(Throwable t) {
return t instanceof RaftRetryFailureException
|| t instanceof AlreadyClosedException;
}
// Every container specific exception from datatnode will be seen as
// StorageContainerException
boolean checkIfContainerToExclude(Throwable t) {
return t instanceof StorageContainerException;
}
private void setExceptionAndThrow(IOException ioe) throws IOException {
isException = true;
throw ioe;
}
void handleRetry(IOException exception) throws IOException {
RetryPolicy retryPolicy = retryPolicyMap
.get(HddsClientUtils.checkForException(exception).getClass());
if (retryPolicy == null) {
retryPolicy = retryPolicyMap.get(Exception.class);
}
handleRetry(exception, retryPolicy);
}
private void handleRetry(IOException exception, RetryPolicy retryPolicy)
throws IOException {
RetryPolicy.RetryAction action = null;
try {
action = retryPolicy.shouldRetry(exception, retryCount, 0, true);
} catch (Exception e) {
setExceptionAndThrow(new IOException(e));
}
if (action.action == RetryPolicy.RetryAction.RetryDecision.FAIL) {
String msg = "";
if (action.reason != null) {
msg = "Retry request failed. " + action.reason;
//LOG.error(msg, exception);
}
setExceptionAndThrow(new IOException(msg, exception));
}
// Throw the exception if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
//LOG.warn("Interrupted while trying for retry");
setExceptionAndThrow(exception);
}
Preconditions.checkArgument(
action.action == RetryPolicy.RetryAction.RetryDecision.RETRY);
if (action.delayMillis > 0) {
try {
Thread.sleep(action.delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
IOException ioe = (IOException) new InterruptedIOException(
"Interrupted: action=" + action + ", retry policy=" + retryPolicy)
.initCause(e);
setExceptionAndThrow(ioe);
}
}
retryCount++;
}
}

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Ok, I'll change it

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Has been modified. @szetszwo Please take a look.Thanks.

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , please review it yourself -- go through the change and remove the unused code (e.g. ConsumerWithIOException). Thanks.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , thanks for the update. Some comments inlined.

});
}

private CompletableFuture<ContainerCommandResponseProto> runCommandAsync(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCommandAsync is not used. Let's add it later.

Comment on lines +194 to +197
LOG.error(
"Get stream data channel error Malformed request " +
"containerID: {} msg: {}",
container.getContainerData().getContainerID(), msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove LOG.error(..) since it already throws an exception.

ContainerCommandRequestProto request, KeyValueContainer kvContainer,
DispatcherContext dispatcherContext) {
if (!request.hasWriteChunk()) {
BlockID blockID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final.

@@ -97,12 +85,8 @@ enum StreamAction {
*/
@VisibleForTesting
public KeyDataStreamOutput() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere. Let's remove it.

Comment on lines -161 to -162
this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
config.getMaxRetryCount(), config.getRetryInterval());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the retryPolicyMap to the super constructor.

Comment on lines +116 to +119
super(HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is passed incorrectly. Below is the original map.

- this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
- config.getMaxRetryCount(), config.getRetryInterval());

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @guohao-rosicky. The change looks good.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo
szetszwo merged commit 9546d4c into apache:HDDS-4454Mar 28, 2022
@szetszwoszetszwo changed the title HDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputHDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.Mar 28, 2022
szetszwo pushed a commit to szetszwo/ozone that referenced this pull request May 6, 2022
captainzmc pushed a commit to captainzmc/hadoop-ozone that referenced this pull request Jul 4, 2022
szetszwo pushed a commit that referenced this pull request Oct 25, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
szetszwo pushed a commit that referenced this pull request Nov 7, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
(cherry picked from commit 99b414d)
szetszwo pushed a commit that referenced this pull request Dec 1, 2022
szetszwo pushed a commit that referenced this pull request Dec 16, 2022
nishitpatira pushed a commit to nishitpatira/ozone that referenced this pull request Dec 16, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@guohao-rosicky@szetszwo@captainzmc
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. by guohao-rosicky · Pull Request #3195 · apache/ozone · GitHub
Skip to content

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput. - #3195

Merged
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev
Mar 28, 2022
Merged

HDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.#3195
szetszwo merged 3 commits into
apache:HDDS-4454from
guohao-rosicky:guohao-HDDS-6137-dev

Conversation

@guohao-rosicky

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Optimized the putBlock method of BlockDataStreamOutput

  1. PutBlock does not use RaftRpc, but through the stream close() method
// putBlock metadata
ByteBuffer buf =
ContainerCommandRequestMessage.toMessage(putBlockRequestProto, null)
.getContent().asReadOnlyByteBuffer();
out.writeAsync(buf, StandardWriteOption.CLOSE); 

2.Mark the stream data boundary by sending an empty stream packet

// Mark the stream data boundary
out.writeAsync(ByteBuffer.allocateDirect(0).asReadOnlyBuffer()); 
  1. Flush is also in the form of async RPC, because putBlock needs to fetch bcsId (Raft log index)

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-6137

How was this patch tested?

Use the existing UT

@guohao-rosickyguohao-rosicky changed the title Guohao hdds 6137 devHDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputMar 15, 2022

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @guohao-rosicky Improve this. LGTM over all. TestBlockDataStreamOutput has some error, can you confirm?

@szetszwoszetszwoMar 16, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky, Writing an empty buffer may not work as expected. In the streaming model, data is continuous but not block-by-block. Both sender and receiver must consider that the data is a stream of bytes.

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

Let me think about how to make it work.

@guohao-rosickyguohao-rosickyMar 16, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment @szetszwo . I wrote a piece of pseudo-code, Ideas about the modification of org.apache.ratis.netty.server.DataStreamManagement#writeTo.

image

What do you think of this idea? There is no need to send this empty buffer rpc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , this is a standard problem in streaming. The standard solution is to write the length with the data. Let's don't change the API for it.

@guohao-rosickyguohao-rosickyMar 24, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @szetszwo
I have a question, about

For example, the sender may write 3 times with lengths 3, 4, 5. Since there could be buffers/encoder/decoder in between, the receiver may receive a single buffer with length 12.

I noticed that in HDFS, marking the end of stream is also represented by an empty package.
In Ratis Stream, sticky packets have already been dealt with.

org.apache.ratis.netty.NettyDataStreamUtils#encodeDataStreamRequestHeader
https://github.com/apache/ratis/blob/master/ratis-netty/src/main/java/org/apache/ratis/netty/NettyDataStreamUtils.java#L90

@captainzmc tested the code from some time ago and did not find this problem

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

@guohao-rosicky , the idea is to write the proto length at the end as shown below; see also https://issues.apache.org/jira/secure/attachment/13041230/3195_review.patch

 // readerIndex protoIndex lengthIndex readerIndex+readableBytes
// V V V V
// format: |--- data ---|--- proto ---|--- proto length (4 bytes) ---|

Thanks @szetszwo, Patch has been applied

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , please fix the checkstyle warnings and test the change manually. I have not tested it. Thanks.

@captainzmc

Copy link
Copy Markdown
Member

Thanks @guohao-rosicky@szetszwo update this. I used this PR test and found that the file failed to write. In DataNode IndexOutOfBoundsException will appear.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks. Let me test the serialization/deserialization code.

@szetszwo

Copy link
Copy Markdown
Contributor

Found the bug: ByteBu.nioBuffers(index, length) somehow is not working. Let me fix it.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky, Fixed the bug; see https://issues.apache.org/jira/secure/attachment/13041255/3195_TestStreamDataChannel.patch

Thanks @szetszwo for the update. Using new patch I found IndexOutOfBoundsException still appear.
I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.
image

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , thanks a lot for testing it!

I added one more test for the serialization and buffers. It worked fine. See https://issues.apache.org/jira/secure/attachment/13041275/3195_testBuffers.patch

I found that the ERROR only appeared when writing with high concurrency. I was successful with 30 concurrent writes, but I get the exception with 600 concurrent writes.

It seems there are some synchronization problems. Let me check.

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , could you post the test code?

@captainzmc

Copy link
Copy Markdown
Member

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Actually there a problem with my previous description. I noticed that the when I use 30 threads writing, we’ll get same error.

@captainzmc

captainzmc commented Mar 17, 2022

Copy link
Copy Markdown
Member

@szetszwo Here is my datanode log. https://issues.apache.org/jira/secure/attachment/13041276/ozone-root-datanode-9-29-173-57.log
In the test, I wrote 30 files concurrently. Each file is 128MB and the chunk size is 1MB

@szetszwo

Copy link
Copy Markdown
Contributor

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

@szetszwo

Copy link
Copy Markdown
Contributor

@szetszwo yes here is my test code. Is written according to the ratis Filestore test.

Thanks for sharing the test code. It looks good.

@captainzmc

Copy link
Copy Markdown
Member

@captainzmc , @guohao-rosicky , just found a bug in BlockDataStreamOutput. Could you test https://issues.apache.org/jira/secure/attachment/13041279/3195_bugfix.patch to see if it can fix the bug?

Thanks @szetszwo for update this. I had just test this patch, we still get same error. Just as the same as this.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an abstract class instead static methods. Then, KeyDataStreamOutput can extends it. If we need to add SmallFileDataStreamOutput, it can extends it too.

package org.apache.hadoop.ozone.client.io;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.scm.storage.ByteBufferStreamOutput;
import org.apache.hadoop.io.retry.RetryPolicies;
import org.apache.hadoop.io.retry.RetryPolicy;
import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
import org.apache.ratis.protocol.exceptions.RaftRetryFailureException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class AbstractDataStreamOutput implements ByteBufferStreamOutput {
private final Map<Class<? extends Throwable>, RetryPolicy> retryPolicyMap;
private int retryCount;
private boolean isException;
protected AbstractDataStreamOutput() {
this.retryPolicyMap = HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL));
}
@VisibleForTesting
public int getRetryCount() {
return retryCount;
}
void resetRetryCount() {
retryCount = 0;
}
boolean isException() {
return isException;
}
/**
* Checks if the provided exception signifies retry failure in ratis client.
* In case of retry failure, ratis client throws RaftRetryFailureException
* and all succeeding operations are failed with AlreadyClosedException.
*/
boolean checkForRetryFailure(Throwable t) {
return t instanceof RaftRetryFailureException
|| t instanceof AlreadyClosedException;
}
// Every container specific exception from datatnode will be seen as
// StorageContainerException
boolean checkIfContainerToExclude(Throwable t) {
return t instanceof StorageContainerException;
}
private void setExceptionAndThrow(IOException ioe) throws IOException {
isException = true;
throw ioe;
}
void handleRetry(IOException exception) throws IOException {
RetryPolicy retryPolicy = retryPolicyMap
.get(HddsClientUtils.checkForException(exception).getClass());
if (retryPolicy == null) {
retryPolicy = retryPolicyMap.get(Exception.class);
}
handleRetry(exception, retryPolicy);
}
private void handleRetry(IOException exception, RetryPolicy retryPolicy)
throws IOException {
RetryPolicy.RetryAction action = null;
try {
action = retryPolicy.shouldRetry(exception, retryCount, 0, true);
} catch (Exception e) {
setExceptionAndThrow(new IOException(e));
}
if (action.action == RetryPolicy.RetryAction.RetryDecision.FAIL) {
String msg = "";
if (action.reason != null) {
msg = "Retry request failed. " + action.reason;
//LOG.error(msg, exception);
}
setExceptionAndThrow(new IOException(msg, exception));
}
// Throw the exception if the thread is interrupted
if (Thread.currentThread().isInterrupted()) {
//LOG.warn("Interrupted while trying for retry");
setExceptionAndThrow(exception);
}
Preconditions.checkArgument(
action.action == RetryPolicy.RetryAction.RetryDecision.RETRY);
if (action.delayMillis > 0) {
try {
Thread.sleep(action.delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
IOException ioe = (IOException) new InterruptedIOException(
"Interrupted: action=" + action + ", retry policy=" + retryPolicy)
.initCause(e);
setExceptionAndThrow(ioe);
}
}
retryCount++;
}
}

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Ok, I'll change it

@guohao-rosicky

Copy link
Copy Markdown
ContributorAuthor

Has been modified. @szetszwo Please take a look.Thanks.

@szetszwo

Copy link
Copy Markdown
Contributor

@guohao-rosicky , please review it yourself -- go through the change and remove the unused code (e.g. ConsumerWithIOException). Thanks.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guohao-rosicky , thanks for the update. Some comments inlined.

});
}

private CompletableFuture<ContainerCommandResponseProto> runCommandAsync(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runCommandAsync is not used. Let's add it later.

Comment on lines +194 to +197
LOG.error(
"Get stream data channel error Malformed request " +
"containerID: {} msg: {}",
container.getContainerData().getContainerID(), msg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove LOG.error(..) since it already throws an exception.

ContainerCommandRequestProto request, KeyValueContainer kvContainer,
DispatcherContext dispatcherContext) {
if (!request.hasWriteChunk()) {
BlockID blockID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final.

@@ -97,12 +85,8 @@ enum StreamAction {
*/
@VisibleForTesting
public KeyDataStreamOutput() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not used anywhere. Let's remove it.

Comment on lines -161 to -162
this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
config.getMaxRetryCount(), config.getRetryInterval());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the retryPolicyMap to the super constructor.

Comment on lines +116 to +119
super(HddsClientUtils.getExceptionList()
.stream()
.collect(Collectors.toMap(Function.identity(),
e -> RetryPolicies.TRY_ONCE_THEN_FAIL)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map is passed incorrectly. Below is the original map.

- this.retryPolicyMap = HddsClientUtils.getRetryPolicyByException(
- config.getMaxRetryCount(), config.getRetryInterval());

@captainzmccaptainzmc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @guohao-rosicky. The change looks good.

@szetszwoszetszwo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo
szetszwo merged commit 9546d4c into apache:HDDS-4454Mar 28, 2022
@szetszwoszetszwo changed the title HDDS-6137. [Ozone-Streaming] Optimized the putBlock method of BlockDataStreamOutputHDDS-6137. [Ozone-Streaming] Refactor KeyDataStreamOutput.Mar 28, 2022
szetszwo pushed a commit to szetszwo/ozone that referenced this pull request May 6, 2022
captainzmc pushed a commit to captainzmc/hadoop-ozone that referenced this pull request Jul 4, 2022
szetszwo pushed a commit that referenced this pull request Oct 25, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
szetszwo pushed a commit that referenced this pull request Nov 7, 2022
(cherry picked from commit 88c4d59)
(cherry picked from commit 0a432aa0eb57bd25d63e656d0ec6e96a05689ac7)
(cherry picked from commit 99b414d)
szetszwo pushed a commit that referenced this pull request Dec 1, 2022
szetszwo pushed a commit that referenced this pull request Dec 16, 2022
nishitpatira pushed a commit to nishitpatira/ozone that referenced this pull request Dec 16, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@guohao-rosicky@szetszwo@captainzmc