-
Notifications
You must be signed in to change notification settings - Fork 25k
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
[Refactoring] Ec2 and GCS plugins build client lazily #31250
Merged
albertzaharovits
merged 6 commits into
elastic:reload-secure-store-action
from
albertzaharovits:refactor-remove-double-dict
Jun 16, 2018
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f98d75f
Added Lazy
albertzaharovits c54ffa1
GCS removed double map (config + clients)
albertzaharovits 1184411
EC2 plugin lazy client ditching cached settings
albertzaharovits 7f76048
Merge branch 'reload-secure-store-action' into refactor-remove-double…
albertzaharovits e5a9934
polished LazyInitializable
albertzaharovits 86b3ebb
Renames
albertzaharovits 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
108 changes: 108 additions & 0 deletions
108
server/src/main/java/org/elasticsearch/common/util/LazyInitializable.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,108 @@ | ||
/* | ||
* 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.common.util; | ||
|
||
import org.elasticsearch.common.CheckedSupplier; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Encapsulates a {@link CheckedSupplier} which is lazily invoked once on the | ||
* first call to {@code #getOrCompute()}. The value which the | ||
* <code>supplier</code> returns is memorized and will be served until | ||
* {@code #reset()} is called. Each value returned by {@code #getOrCompute()}, | ||
* newly minted or cached, will be passed to the <code>primer</code> | ||
* {@link Consumer}. On {@code #reset()} the value will be passed to the | ||
* <code>finalizer</code> {@code Consumer} and the next {@code #getOrCompute()} | ||
* will regenerate the value. | ||
*/ | ||
public final class LazyInitializable<T, E extends Exception> { | ||
|
||
private final CheckedSupplier<T, E> supplier; | ||
private final Consumer<T> primer; | ||
private final Consumer<T> finalizer; | ||
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. how about |
||
private volatile T value; | ||
|
||
/** | ||
* Creates the simple LazyInitializable instance. | ||
* | ||
* @param supplier | ||
* The {@code CheckedSupplier} to generate values which will be | ||
* served on {@code #getOrCompute()} invocations. | ||
*/ | ||
public LazyInitializable(CheckedSupplier<T, E> supplier) { | ||
this(supplier, v -> {}, v -> {}); | ||
} | ||
|
||
/** | ||
* Creates the complete LazyInitializable instance. | ||
* | ||
* @param supplier | ||
* The {@code CheckedSupplier} to generate values which will be | ||
* served on {@code #getOrCompute()} invocations. | ||
* @param primer | ||
* A {@code Consumer} which is called on each value, newly forged or | ||
* stale, that is returned by {@code #getOrCompute()} | ||
* @param finalizer | ||
* A {@code Consumer} which is invoked on the value that will be | ||
* erased when calling {@code #reset()} | ||
*/ | ||
public LazyInitializable(CheckedSupplier<T, E> supplier, Consumer<T> primer, Consumer<T> finalizer) { | ||
this.supplier = supplier; | ||
this.primer = primer; | ||
this.finalizer = finalizer; | ||
} | ||
|
||
/** | ||
* Returns a value that was created by <code>supplier</code>. The value might | ||
* have been previously created, if not it will be created now, thread safe of | ||
* course. | ||
*/ | ||
public T getOrCompute() throws E { | ||
final T readOnce = value; // Read volatile just once... | ||
final T result = readOnce == null ? maybeCompute(supplier) : readOnce; | ||
primer.accept(result); | ||
return result; | ||
} | ||
|
||
/** | ||
* Clears the value, if it has been previously created by calling | ||
* {@code #getOrCompute()}. The <code>finalizer</code> will be called on this | ||
* value. The next call to {@code #getOrCompute()} will recreate the value. | ||
*/ | ||
public synchronized void reset() { | ||
if (value != null) { | ||
finalizer.accept(value); | ||
value = null; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new value thread safely. | ||
*/ | ||
private synchronized T maybeCompute(CheckedSupplier<T, E> supplier) throws E { | ||
if (value == null) { | ||
value = Objects.requireNonNull(supplier.get()); | ||
} | ||
return value; | ||
} | ||
|
||
} |
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.
how about
onGet
as a name instead of primer