forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Use local opensearch.common.SetOnce instead of lucene's ut…
…ility class (opensearch-project#5947) This copies Apache Lucene's o.a.l.util.SetOnce to o.opensearch.common.SetOnce in the opensearch-core library and refactors all opensearch classes from the lucene implementation to the local opensearch implementation. This accomplishes three objectives: 1. remove the dependency on lucene-core library for classes that only import this one lucene utility class, 2. shield opensearch core dependency on this class from any upstream breaking changes, 3. further decouples other foundation classes from third party libraries so they can be refactored from the server module into opensearch support libraries. Signed-off-by: Nicholas Walter Knize <[email protected]>
- Loading branch information
Showing
75 changed files
with
299 additions
and
72 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
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.