-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[Refactor] Use local opensearch.common.SetOnce instead of lucene's utility class #5947
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
32e33ea
[Refactor] Use local opensearch.common.SetOnce instead of lucene's ut…
nknize 521d6a7
update changelog
nknize d5b43bf
fix unintended changelog additions
nknize 6431124
change SetOnce from opensearch.internal to opensearch.api
nknize 18dcf82
add lucene.util.SetOnce to forbiddenApis
nknize 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ java.nio.channels.SocketChannel#connect(java.net.SocketAddress) | |
java.lang.Boolean#getBoolean(java.lang.String) | ||
|
||
org.apache.lucene.util.IOUtils @ use @org.opensearch.core.internal.io instead | ||
org.apache.lucene.util.SetOnce @ use @org.opensearch.common.SetOnce instead | ||
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. 👍 |
||
|
||
@defaultMessage use executors from org.opensearch.common.util.concurrent.OpenSearchExecutors instead which will properly bubble up Errors | ||
java.util.concurrent.AbstractExecutorService#<init>() | ||
|
104 changes: 104 additions & 0 deletions
104
libs/core/src/main/java/org/opensearch/common/SetOnce.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,104 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
package org.opensearch.common; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* A convenient class which offers a semi-immutable object wrapper implementation which allows one | ||
* to set the value of an object exactly once, and retrieve it many times. If {@link #set(Object)} | ||
* is called more than once, {@link AlreadySetException} is thrown and the operation will fail. | ||
* | ||
* This is borrowed from lucene's experimental API. It is not reused to eliminate the dependency | ||
* on lucene core for such a simple (standalone) utility class that may change beyond OpenSearch needs. | ||
* | ||
* @opensearch.api | ||
*/ | ||
public final class SetOnce<T> implements Cloneable { | ||
|
||
/** Thrown when {@link SetOnce#set(Object)} is called more than once. */ | ||
public static final class AlreadySetException extends IllegalStateException { | ||
public AlreadySetException() { | ||
super("The object cannot be set twice!"); | ||
} | ||
} | ||
|
||
/** Holding object and marking that it was already set */ | ||
private static final class Wrapper<T> { | ||
private T object; | ||
|
||
private Wrapper(T object) { | ||
this.object = object; | ||
} | ||
} | ||
|
||
private final AtomicReference<Wrapper<T>> set; | ||
|
||
/** | ||
* A default constructor which does not set the internal object, and allows setting it by calling | ||
* {@link #set(Object)}. | ||
*/ | ||
public SetOnce() { | ||
set = new AtomicReference<>(); | ||
} | ||
|
||
/** | ||
* Creates a new instance with the internal object set to the given object. Note that any calls to | ||
* {@link #set(Object)} afterwards will result in {@link AlreadySetException} | ||
* | ||
* @throws AlreadySetException if called more than once | ||
* @see #set(Object) | ||
*/ | ||
public SetOnce(T obj) { | ||
set = new AtomicReference<>(new Wrapper<>(obj)); | ||
} | ||
|
||
/** Sets the given object. If the object has already been set, an exception is thrown. */ | ||
public final void set(T obj) { | ||
if (!trySet(obj)) { | ||
throw new AlreadySetException(); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the given object if none was set before. | ||
* | ||
* @return true if object was set successfully, false otherwise | ||
*/ | ||
public final boolean trySet(T obj) { | ||
return set.compareAndSet(null, new Wrapper<>(obj)); | ||
} | ||
|
||
/** Returns the object set by {@link #set(Object)}. */ | ||
public final T get() { | ||
Wrapper<T> wrapper = set.get(); | ||
return wrapper == null ? null : wrapper.object; | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
libs/core/src/test/java/org/opensearch/common/SetOnceTests.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,120 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.common; | ||
|
||
import org.opensearch.common.SetOnce.AlreadySetException; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.Random; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
|
||
public class SetOnceTests extends OpenSearchTestCase { | ||
private static final class SetOnceThread extends Thread { | ||
SetOnce<Integer> set; | ||
boolean success = false; | ||
final Random RAND; | ||
|
||
public SetOnceThread(Random random) { | ||
RAND = new Random(random.nextLong()); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
sleep(RAND.nextInt(10)); // sleep for a short time | ||
set.set(Integer.valueOf(getName().substring(2))); | ||
success = true; | ||
} catch (@SuppressWarnings("unused") InterruptedException e) { | ||
// ignore | ||
} catch (@SuppressWarnings("unused") RuntimeException e) { | ||
// TODO: change exception type | ||
// expected. | ||
success = false; | ||
} | ||
} | ||
} | ||
|
||
public void testEmptyCtor() { | ||
SetOnce<Integer> set = new SetOnce<>(); | ||
assertNull(set.get()); | ||
} | ||
|
||
public void testSettingCtor() { | ||
SetOnce<Integer> set = new SetOnce<>(5); | ||
assertEquals(5, set.get().intValue()); | ||
|
||
AlreadySetException alreadySetException = expectThrows(AlreadySetException.class, () -> set.set(7)); | ||
assertThat(alreadySetException.getMessage(), containsString("The object cannot be set twice!")); | ||
} | ||
|
||
public void testSetOnce() { | ||
SetOnce<Integer> set = new SetOnce<>(); | ||
set.set(5); | ||
assertEquals(5, set.get().intValue()); | ||
|
||
AlreadySetException alreadySetException = expectThrows(AlreadySetException.class, () -> set.set(7)); | ||
assertThat(alreadySetException.getMessage(), containsString("The object cannot be set twice!")); | ||
} | ||
|
||
public void testTrySet() { | ||
SetOnce<Integer> set = new SetOnce<>(); | ||
assertTrue(set.trySet(5)); | ||
assertEquals(5, set.get().intValue()); | ||
assertFalse(set.trySet(7)); | ||
assertEquals(5, set.get().intValue()); | ||
} | ||
|
||
public void testSetMultiThreaded() throws Exception { | ||
final SetOnce<Integer> set = new SetOnce<>(); | ||
SetOnceThread[] threads = new SetOnceThread[10]; | ||
for (int i = 0; i < threads.length; i++) { | ||
threads[i] = new SetOnceThread(random()); | ||
threads[i].setName("t-" + (i + 1)); | ||
threads[i].set = set; | ||
} | ||
|
||
for (Thread t : threads) { | ||
t.start(); | ||
} | ||
|
||
for (Thread t : threads) { | ||
t.join(); | ||
} | ||
|
||
for (SetOnceThread t : threads) { | ||
if (t.success) { | ||
int expectedVal = Integer.parseInt(t.getName().substring(2)); | ||
assertEquals("thread " + t.getName(), expectedVal, t.set.get().intValue()); | ||
} | ||
} | ||
} | ||
} |
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
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
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.
What's the compatibility concern with backporting this to 2.x? It looks to me like it would be fine since the existing Lucene class remains available for anything that is using it. (If it is possible to backport this change, then the changelog entry should go in the
[Unreleased 2.x]
section).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 think there are no compatibility concerns for backport: the dependency graph is not changing so the
SetOnce
is still available at the same place as before (lucene-core
).