-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add byte array pooling to nio http transport #31349
Merged
Tim-Brooks
merged 11 commits into
elastic:master
from
Tim-Brooks:reference_counting_for_pages
Jun 15, 2018
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d823bba
Work on implementing reference counting for http reading
Tim-Brooks 4aa8dba
WIP
Tim-Brooks 9e51ba2
Hook up reference counting
Tim-Brooks 46d7982
Add tests
Tim-Brooks 72f45ee
change names
Tim-Brooks 8e42325
Merge remote-tracking branch 'upstream/master' into reference_countin…
Tim-Brooks da37b44
Chnages for review
Tim-Brooks c11429c
Fix test
Tim-Brooks 8fac55d
Merge remote-tracking branch 'upstream/master' into reference_countin…
Tim-Brooks 29014ad
Cleanup test
Tim-Brooks d81fe44
Fix test
Tim-Brooks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
75 changes: 75 additions & 0 deletions
75
plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/PagedByteBuf.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,75 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.http.nio; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.CompositeByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.buffer.UnpooledByteBufAllocator; | ||
import io.netty.buffer.UnpooledHeapByteBuf; | ||
import org.elasticsearch.nio.InboundChannelBuffer; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PagedByteBuf extends UnpooledHeapByteBuf { | ||
|
||
private final Runnable releasable; | ||
|
||
private PagedByteBuf(byte[] array, Runnable releasable) { | ||
super(UnpooledByteBufAllocator.DEFAULT, array, array.length); | ||
this.releasable = releasable; | ||
} | ||
|
||
static ByteBuf byteBufFromPages(InboundChannelBuffer.Page[] pages) { | ||
int componentCount = pages.length; | ||
if (componentCount == 0) { | ||
return Unpooled.EMPTY_BUFFER; | ||
} else if (componentCount == 1) { | ||
return byteBufFromPage(pages[0]); | ||
} else { | ||
int maxComponents = Math.max(16, componentCount); | ||
final List<ByteBuf> components = new ArrayList<>(componentCount); | ||
for (InboundChannelBuffer.Page page : pages) { | ||
components.add(byteBufFromPage(page)); | ||
} | ||
return new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, maxComponents, components); | ||
} | ||
} | ||
|
||
private static ByteBuf byteBufFromPage(InboundChannelBuffer.Page page) { | ||
ByteBuffer buffer = page.getByteBuffer(); | ||
assert !buffer.isDirect() && buffer.hasArray() : "Must be a heap buffer with an array"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use |
||
int offset = buffer.arrayOffset() + buffer.position(); | ||
PagedByteBuf newByteBuf = new PagedByteBuf(buffer.array(), page::close); | ||
return newByteBuf.slice(offset, buffer.remaining()); | ||
} | ||
|
||
|
||
@Override | ||
protected void deallocate() { | ||
try { | ||
super.deallocate(); | ||
} finally { | ||
releasable.run(); | ||
} | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a little concerned about this. What if the referenceCount is already 0? I think this should implement
AbstractRefCounted
? I guess that is not available in this lib? I would love to move that class tocore
and replace theAlreadyClosedException
withIllegalStateException
and use it here?!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do wonder why we need to refCount this. I think it would be good to have some explanation why this is is needed. I also assume if we miss returning a page we will fail tests because not all byte blocks are released right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to integrate with netty. Netty can hold onto bytes internally and we don't know when it is down with them without overriding its buffer.
Yeah normal mock page recycler stuff
I will do this but it will cause the introduction of more layers of objects. Can
Page
be reference counted? Yes. But when a return aPage
, it needs to be a differentPage
object because theByteBuffer
needs to be different. So internally we will need to create like aReleaser
object that is reference counted that is passed around to the different pages.