forked from opensearch-project/opensearch-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a JsonEmitter leak and an over-release.
I've made the JsonEmitter implement AutoCloseable so that the underlying stream can be closed, which in turn will release the compositeByteBuf that it uses to buffer contents. The NettyJsonBodySerializeHandler calls fireChannelRead(), then immediately called release on the same buffer that it just sent downstream on the channel. That latter release() has no been removed. I've added a new unit test to help to test the NettyJsonBodySerializeHandler and to make sure that the memory accesses are solid. I've also updated the JsonEmitter test to not leak so that we're closer to a zero-leaks target. Signed-off-by: Greg Schohn <greg.schohn@gmail.com>
- Loading branch information
1 parent
6de4dd4
commit c54e360
Showing
11 changed files
with
121 additions
and
59 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
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
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
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
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
58 changes: 58 additions & 0 deletions
58
...org/opensearch/migrations/replay/datahandlers/http/NettyJsonBodySerializeHandlerTest.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,58 @@ | ||
package org.opensearch.migrations.replay.datahandlers.http; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.embedded.EmbeddedChannel; | ||
import io.netty.handler.codec.http.HttpContent; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.migrations.replay.GenerateRandomNestedJsonObject; | ||
import org.opensearch.migrations.replay.ReplayUtils; | ||
import org.opensearch.migrations.replay.datahandlers.PayloadAccessFaultingMap; | ||
import org.opensearch.migrations.testutils.WrapWithNettyLeakDetection; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@Slf4j | ||
public class NettyJsonBodySerializeHandlerTest { | ||
@Test | ||
@WrapWithNettyLeakDetection | ||
public void testJsonSerializerHandler() throws Exception { | ||
var randomJsonGenerator = new GenerateRandomNestedJsonObject(); | ||
var randomJson = randomJsonGenerator.makeRandomJsonObject(new Random(2), 2, 1); | ||
var headers = new StrictCaseInsensitiveHttpHeadersMap(); | ||
headers.put(IHttpMessage.CONTENT_TYPE, List.of(IHttpMessage.APPLICATION_JSON)); | ||
var fullHttpMessageWithJsonBody = new HttpJsonMessageWithFaultingPayload(headers); | ||
fullHttpMessageWithJsonBody.setPayloadFaultMap(new PayloadAccessFaultingMap(headers)); | ||
fullHttpMessageWithJsonBody.payload().put(PayloadAccessFaultingMap.INLINED_JSON_BODY_DOCUMENT_KEY, randomJson); | ||
|
||
var channel = new EmbeddedChannel(new NettyJsonBodySerializeHandler()); | ||
channel.writeInbound(fullHttpMessageWithJsonBody); | ||
|
||
var handlerAccumulatedStream = | ||
ReplayUtils.byteBufsToInputStream(getByteBufStreamFromChannel(channel)); | ||
|
||
String originalTreeStr = new ObjectMapper().writeValueAsString(randomJson); | ||
var reconstitutedTreeStr = new String(handlerAccumulatedStream.readAllBytes(),StandardCharsets.UTF_8); | ||
Assertions.assertEquals(originalTreeStr, reconstitutedTreeStr); | ||
|
||
getByteBufStreamFromChannel(channel).forEach(bb->bb.release()); | ||
} | ||
|
||
private static Stream<ByteBuf> getByteBufStreamFromChannel(EmbeddedChannel channel) { | ||
return channel.inboundMessages().stream() | ||
.filter(x -> x instanceof HttpContent) | ||
.map(x -> { | ||
var rval = ((HttpContent) x).content(); | ||
log.info("refCnt=" + rval.refCnt() + " for " + x); | ||
return rval; | ||
}); | ||
} | ||
} |
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