-
Notifications
You must be signed in to change notification settings - Fork 327
Make ES query recording allocation free #283
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
Merged
felixbarny
merged 4 commits into
elastic:master
from
felixbarny:es-statement-allocation-free
Nov 4, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
84f8194
Make ES query recording allocation free
felixbarny 59eacfe
Incorporate feedback
felixbarny abace29
Merge remote-tracking branch 'origin/master' into es-statement-alloca…
felixbarny 5c2d8aa
Move ESRestClientInstrumentationHelper to core to avoid duplication
felixbarny 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -20,16 +20,37 @@ | |
|
|
||
| package co.elastic.apm.impl.transaction; | ||
|
|
||
| import co.elastic.apm.objectpool.Allocator; | ||
| import co.elastic.apm.objectpool.ObjectPool; | ||
| import co.elastic.apm.objectpool.Recyclable; | ||
| import co.elastic.apm.objectpool.impl.QueueBasedObjectPool; | ||
| import co.elastic.apm.objectpool.impl.Resetter; | ||
| import co.elastic.apm.report.serialize.DslJsonSerializer; | ||
| import org.jctools.queues.atomic.MpmcAtomicArrayQueue; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.nio.CharBuffer; | ||
|
|
||
|
|
||
| /** | ||
| * An object containing contextual data for database spans | ||
| */ | ||
| public class Db implements Recyclable { | ||
|
|
||
| private final ObjectPool<CharBuffer> charBufferPool = QueueBasedObjectPool.of(new MpmcAtomicArrayQueue<CharBuffer>(128), false, | ||
| new Allocator<CharBuffer>() { | ||
| @Override | ||
| public CharBuffer createInstance() { | ||
| return CharBuffer.allocate(DslJsonSerializer.MAX_LONG_STRING_VALUE_LENGTH); | ||
| } | ||
| }, | ||
| new Resetter<CharBuffer>() { | ||
| @Override | ||
| public void recycle(CharBuffer object) { | ||
| object.clear(); | ||
| } | ||
| }); | ||
|
|
||
| /** | ||
| * Database instance name | ||
| */ | ||
|
|
@@ -40,6 +61,10 @@ public class Db implements Recyclable { | |
| */ | ||
| @Nullable | ||
| private String statement; | ||
|
|
||
| @Nullable | ||
| private CharBuffer statementBuffer; | ||
|
|
||
| /** | ||
| * Database type. For any SQL database, "sql". For others, the lower-case database category, e.g. "cassandra", "hbase", or "redis" | ||
| */ | ||
|
|
@@ -83,6 +108,37 @@ public Db withStatement(@Nullable String statement) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Gets a pooled {@link CharBuffer} to record the DB statement and associates it with this instance. | ||
| * <p> | ||
| * Note: you may not hold a reference to the returned {@link CharBuffer} as it will be reused. | ||
| * </p> | ||
| * <p> | ||
| * Note: This method is not thread safe | ||
| * </p> | ||
| * | ||
| * @return a {@link CharBuffer} to record the DB statement | ||
| */ | ||
| public CharBuffer withStatementBuffer() { | ||
|
Contributor
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. Add documentation that user of this method is not allowed to hold a reference to the buffer |
||
| if (this.statementBuffer == null) { | ||
| this.statementBuffer = charBufferPool.createInstance(); | ||
| } | ||
| return this.statementBuffer; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the associated pooled {@link CharBuffer} to record the DB statement. | ||
| * <p> | ||
| * Note: returns {@code null} unless {@link #withStatementBuffer()} has previously been called | ||
| * </p> | ||
| * | ||
| * @return a {@link CharBuffer} to record the DB statement, or {@code null} | ||
| */ | ||
| @Nullable | ||
| public CharBuffer getStatementBuffer() { | ||
|
Contributor
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. same |
||
| return statementBuffer; | ||
| } | ||
|
|
||
| /** | ||
| * Database type. For any SQL database, "sql". For others, the lower-case database category, e.g. "cassandra", "hbase", or "redis" | ||
| */ | ||
|
|
@@ -121,13 +177,18 @@ public void resetState() { | |
| statement = null; | ||
| type = null; | ||
| user = null; | ||
| if (statementBuffer != null) { | ||
| charBufferPool.recycle(statementBuffer); | ||
| } | ||
| statementBuffer = null; | ||
| } | ||
|
|
||
| public boolean hasContent() { | ||
| return instance != null || | ||
| statement != null || | ||
| type != null || | ||
| user != null; | ||
| user != null || | ||
| statementBuffer != null; | ||
| } | ||
|
|
||
| public void copyFrom(Db other) { | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
41 changes: 41 additions & 0 deletions
41
apm-agent-core/src/main/java/co/elastic/apm/objectpool/impl/Resetter.java
This file contains hidden or 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,41 @@ | ||
| /*- | ||
| * #%L | ||
| * Elastic APM Java agent | ||
| * %% | ||
| * Copyright (C) 2018 Elastic and contributors | ||
| * %% | ||
| * Licensed 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. | ||
| * #L% | ||
| */ | ||
| package co.elastic.apm.objectpool.impl; | ||
|
|
||
| import co.elastic.apm.objectpool.Recyclable; | ||
|
|
||
| public interface Resetter<T> { | ||
|
|
||
| void recycle(T object); | ||
|
|
||
| class ForRecyclable<T extends Recyclable> implements Resetter<T> { | ||
| private static ForRecyclable INSTANCE = new ForRecyclable(); | ||
|
|
||
| public static <T extends Recyclable> Resetter<T> get() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public void recycle(Recyclable object) { | ||
| object.resetState(); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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.
stay consistent with the other pool initialization, not through usage of
newThere 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.
You mean I should add a static initializer for creating a
QueueBasedObjectPoolwith anAllocatorandResetter? Or do you mean I should not use an anonymous inner class forAllocator?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.
created
QueueBasedObjectPool.ofstatic initializerThere 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 was referring to the ObjectPool- I saw you changed all others to use a static initializer, so I wondered why not this one as well.
Anyway, not very important