-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactored OxiaClientBuilder * Fixed perf client * Fixed pulsar adapters * Fixed import
- Loading branch information
Showing
21 changed files
with
262 additions
and
33 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
client-api/src/main/java/io/streamnative/oxia/client/api/OxiaClientBuilder.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,54 @@ | ||
/* | ||
* Copyright © 2022-2024 StreamNative Inc. | ||
* | ||
* 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. | ||
*/ | ||
package io.streamnative.oxia.client.api; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.streamnative.oxia.client.api.exceptions.OxiaException; | ||
import io.streamnative.oxia.client.internal.DefaultImplementation; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Supplier; | ||
|
||
public interface OxiaClientBuilder { | ||
|
||
static OxiaClientBuilder create(String serviceAddress) { | ||
return DefaultImplementation.getDefaultImplementation(serviceAddress); | ||
} | ||
|
||
SyncOxiaClient syncClient() throws OxiaException; | ||
|
||
CompletableFuture<AsyncOxiaClient> asyncClient(); | ||
|
||
OxiaClientBuilder requestTimeout(Duration requestTimeout); | ||
|
||
OxiaClientBuilder batchLinger(Duration batchLinger); | ||
|
||
OxiaClientBuilder maxRequestsPerBatch(int maxRequestsPerBatch); | ||
|
||
OxiaClientBuilder recordCacheCapacity(int recordCacheCapacity); | ||
|
||
OxiaClientBuilder namespace(String namespace); | ||
|
||
OxiaClientBuilder disableRecordCache(); | ||
|
||
OxiaClientBuilder sessionTimeout(Duration sessionTimeout); | ||
|
||
OxiaClientBuilder clientIdentifier(String clientIdentifier); | ||
|
||
OxiaClientBuilder clientIdentifier(Supplier<String> clientIdentifier); | ||
|
||
OxiaClientBuilder openTelemetry(OpenTelemetry openTelemetry); | ||
} |
53 changes: 53 additions & 0 deletions
53
client-api/src/main/java/io/streamnative/oxia/client/internal/DefaultImplementation.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,53 @@ | ||
/* | ||
* Copyright © 2022-2024 StreamNative Inc. | ||
* | ||
* 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. | ||
*/ | ||
package io.streamnative.oxia.client.internal; | ||
|
||
import io.streamnative.oxia.client.api.OxiaClientBuilder; | ||
import java.lang.reflect.Constructor; | ||
|
||
/** | ||
* This class loads the implementation for {@link OxiaClientBuilderImpl} and allows you to decouple | ||
* the API from the actual implementation. <b>This class is internal to the Oxia API implementation, | ||
* and it is not part of the public API it is not meant to be used by client applications.</b> | ||
*/ | ||
public class DefaultImplementation { | ||
private static final Constructor<?> CONSTRUCTOR; | ||
|
||
private static final String IMPL_CLASS_NAME = "io.streamnative.oxia.client.OxiaClientBuilderImpl"; | ||
|
||
static { | ||
Constructor<?> impl; | ||
try { | ||
impl = ReflectionUtils.newClassInstance(IMPL_CLASS_NAME).getConstructor(String.class); | ||
} catch (Throwable error) { | ||
throw new RuntimeException("Cannot load Oxia Client Implementation: " + error, error); | ||
} | ||
CONSTRUCTOR = impl; | ||
} | ||
|
||
/** | ||
* Access the actual implementation of the Oxia Client API. | ||
* | ||
* @return the loaded implementation. | ||
*/ | ||
public static OxiaClientBuilder getDefaultImplementation(String serviceAddress) { | ||
try { | ||
return (OxiaClientBuilder) CONSTRUCTOR.newInstance(serviceAddress); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
client-api/src/main/java/io/streamnative/oxia/client/internal/ReflectionUtils.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,82 @@ | ||
/* | ||
* Copyright © 2022-2024 StreamNative Inc. | ||
* | ||
* 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. | ||
*/ | ||
package io.streamnative.oxia.client.internal; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
class ReflectionUtils { | ||
interface SupplierWithException<T> { | ||
T get() throws Exception; | ||
} | ||
|
||
static <T> T catchExceptions(SupplierWithException<T> s) { | ||
try { | ||
return s.get(); | ||
} catch (Throwable t) { | ||
if (t instanceof InvocationTargetException) { | ||
// exception is thrown during invocation | ||
Throwable cause = t.getCause(); | ||
if (cause instanceof RuntimeException) { | ||
throw (RuntimeException) cause; | ||
} else { | ||
throw new RuntimeException(cause); | ||
} | ||
} | ||
throw new RuntimeException(t); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static <T> Class<T> newClassInstance(String className) { | ||
try { | ||
try { | ||
// when the API is loaded in the same classloader as the impl | ||
return (Class<T>) | ||
Class.forName(className, true, DefaultImplementation.class.getClassLoader()); | ||
} catch (Exception e) { | ||
// when the API is loaded in a separate classloader as the impl | ||
// the classloader that loaded the impl needs to be a child classloader of the classloader | ||
// that loaded the API | ||
return (Class<T>) | ||
Class.forName(className, true, Thread.currentThread().getContextClassLoader()); | ||
} | ||
} catch (ClassNotFoundException | NoClassDefFoundError e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
static <T> Constructor<T> getConstructor(String className, Class<?>... argTypes) { | ||
try { | ||
Class<T> clazz = newClassInstance(className); | ||
return clazz.getConstructor(argTypes); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
static <T> Method getStaticMethod(String className, String method, Class<?>... argTypes) { | ||
try { | ||
Class<T> clazz = newClassInstance(className); | ||
return clazz.getMethod(method, argTypes); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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.