forked from apache/bookkeeper
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reference counting (retain/release) in PerChannelBookieClient (a…
…pache#4293) ### Motivation This addresses the remaining gaps of apache#4289 in handling ByteBuf retain/release. This PR will also address the concern about NioBuffer lifecycle brought up in the review of the original PR review: apache#791 (comment) . This PR fixes several problems: * ByteString buffer lifecycle in client, follows ByteBufList lifecycle * ByteBufList lifecycle, moved to write promise * Calling of write promises in AuthHandler which buffers messages while authentication is in progress. It was ignoring the promises. ### Changes - add 2 callback parameters to writeAndFlush: cleanupActionFailedBeforeWrite and cleanupActionAfterWrite - use these callback actions for proper cleanup - extract a utility class ByteStringUtil for wrapping ByteBufList or ByteBuf as concatenated zero copy ByteString - properly handle releasing of ByteBufList in the write promise - properly handle calling promises that are buffered while authentication is in progress
- Loading branch information
Showing
6 changed files
with
223 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ByteStringUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.bookkeeper.proto; | ||
|
||
import com.google.protobuf.ByteString; | ||
import com.google.protobuf.UnsafeByteOperations; | ||
import io.netty.buffer.ByteBuf; | ||
import java.nio.ByteBuffer; | ||
import org.apache.bookkeeper.util.ByteBufList; | ||
|
||
public class ByteStringUtil { | ||
|
||
/** | ||
* Wrap the internal buffers of a ByteBufList into a single ByteString. | ||
* The lifecycle of the wrapped ByteString is tied to the ByteBufList. | ||
* | ||
* @param bufList ByteBufList to wrap | ||
* @return ByteString wrapping the internal buffers of the ByteBufList | ||
*/ | ||
public static ByteString byteBufListToByteString(ByteBufList bufList) { | ||
ByteString aggregated = null; | ||
for (int i = 0; i < bufList.size(); i++) { | ||
ByteBuf buffer = bufList.getBuffer(i); | ||
if (buffer.readableBytes() > 0) { | ||
aggregated = byteBufToByteString(aggregated, buffer); | ||
} | ||
} | ||
return aggregated != null ? aggregated : ByteString.EMPTY; | ||
} | ||
|
||
/** | ||
* Wrap the internal buffers of a ByteBuf into a single ByteString. | ||
* The lifecycle of the wrapped ByteString is tied to the ByteBuf. | ||
* | ||
* @param byteBuf ByteBuf to wrap | ||
* @return ByteString wrapping the internal buffers of the ByteBuf | ||
*/ | ||
public static ByteString byteBufToByteString(ByteBuf byteBuf) { | ||
return byteBufToByteString(null, byteBuf); | ||
} | ||
|
||
// internal method to aggregate a ByteBuf into a single aggregated ByteString | ||
private static ByteString byteBufToByteString(ByteString aggregated, ByteBuf byteBuf) { | ||
if (byteBuf.readableBytes() == 0) { | ||
return ByteString.EMPTY; | ||
} | ||
if (byteBuf.nioBufferCount() > 1) { | ||
for (ByteBuffer nioBuffer : byteBuf.nioBuffers()) { | ||
ByteString piece = UnsafeByteOperations.unsafeWrap(nioBuffer); | ||
aggregated = (aggregated == null) ? piece : aggregated.concat(piece); | ||
} | ||
} else { | ||
ByteString piece; | ||
if (byteBuf.hasArray()) { | ||
piece = UnsafeByteOperations.unsafeWrap(byteBuf.array(), byteBuf.arrayOffset() + byteBuf.readerIndex(), | ||
byteBuf.readableBytes()); | ||
} else { | ||
piece = UnsafeByteOperations.unsafeWrap(byteBuf.nioBuffer()); | ||
} | ||
aggregated = (aggregated == null) ? piece : aggregated.concat(piece); | ||
} | ||
return aggregated; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.