Skip to content
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

IGNITE-16117 Thin client: Convert async exceptions in sync APIs #525

Merged
merged 9 commits into from Dec 27, 2021
Expand Up @@ -21,6 +21,7 @@
import static org.apache.ignite.client.IgniteClientConfiguration.DFLT_RECONNECT_THROTTLING_PERIOD;
import static org.apache.ignite.client.IgniteClientConfiguration.DFLT_RECONNECT_THROTTLING_RETRIES;
import static org.apache.ignite.client.IgniteClientConfiguration.DFLT_RETRY_LIMIT;
import static org.apache.ignite.internal.client.ClientUtils.sync;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -171,7 +172,7 @@ public Builder reconnectThrottlingRetries(int reconnectThrottlingRetries) {
* @return Ignite client.
*/
public IgniteClient build() {
return buildAsync().join();
return sync(buildAsync());
}

/**
Expand Down
@@ -0,0 +1,62 @@
/*
* 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.
*/

package org.apache.ignite.internal.client;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.apache.ignite.client.IgniteClientException;
import org.apache.ignite.lang.IgniteException;

/**
* Client utilities.
*/
public class ClientUtils {
/**
* Waits for async operation completion.
*
* @param fut Future to wait to.
* @param <T> Future result type.
* @return Future result.
*/
public static <T> T sync(CompletableFuture<T> fut) {
try {
return fut.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restore interrupt flag.

throw convertException(e);
} catch (ExecutionException e) {
throw convertException(e.getCause());
}
}

/**
* Converts an internal exception to a public one.
*
* @param e Internal exception.
* @return Public exception.
*/
public static IgniteException convertException(Throwable e) {
if (e instanceof IgniteException) {
return (IgniteException) e;
}

//TODO: IGNITE-14500 Replace with public exception with an error code (or unwrap?).
return new IgniteClientException(e.getMessage(), e);
}
}
Expand Up @@ -17,6 +17,8 @@

package org.apache.ignite.internal.client.table;

import static org.apache.ignite.internal.client.ClientUtils.sync;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -57,7 +59,7 @@ public ClientKeyValueBinaryView(ClientTable tbl) {
/** {@inheritDoc} */
@Override
public Tuple get(@Nullable Transaction tx, @NotNull Tuple key) {
return getAsync(tx, key).join();
return sync(getAsync(tx, key));
}

/** {@inheritDoc} */
Expand All @@ -74,7 +76,7 @@ public Tuple get(@Nullable Transaction tx, @NotNull Tuple key) {
/** {@inheritDoc} */
@Override
public Map<Tuple, Tuple> getAll(@Nullable Transaction tx, @NotNull Collection<Tuple> keys) {
return getAllAsync(tx, keys).join();
return sync(getAllAsync(tx, keys));
}

/** {@inheritDoc} */
Expand All @@ -92,7 +94,7 @@ public Map<Tuple, Tuple> getAll(@Nullable Transaction tx, @NotNull Collection<Tu
/** {@inheritDoc} */
@Override
public boolean contains(@Nullable Transaction tx, @NotNull Tuple key) {
return containsAsync(tx, key).join();
return sync(containsAsync(tx, key));
}

/** {@inheritDoc} */
Expand All @@ -109,7 +111,7 @@ public CompletableFuture<Boolean> containsAsync(@Nullable Transaction tx, @NotNu
/** {@inheritDoc} */
@Override
public void put(@Nullable Transaction tx, @NotNull Tuple key, Tuple val) {
putAsync(tx, key, val).join();
sync(putAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand All @@ -128,7 +130,7 @@ public void put(@Nullable Transaction tx, @NotNull Tuple key, Tuple val) {
/** {@inheritDoc} */
@Override
public void putAll(@Nullable Transaction tx, @NotNull Map<Tuple, Tuple> pairs) {
putAllAsync(tx, pairs).join();
sync(putAllAsync(tx, pairs));
}

/** {@inheritDoc} */
Expand All @@ -145,7 +147,7 @@ public void putAll(@Nullable Transaction tx, @NotNull Map<Tuple, Tuple> pairs) {
/** {@inheritDoc} */
@Override
public Tuple getAndPut(@Nullable Transaction tx, @NotNull Tuple key, Tuple val) {
return getAndPutAsync(tx, key, val).join();
return sync(getAndPutAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand All @@ -162,7 +164,7 @@ public Tuple getAndPut(@Nullable Transaction tx, @NotNull Tuple key, Tuple val)
/** {@inheritDoc} */
@Override
public boolean putIfAbsent(@Nullable Transaction tx, @NotNull Tuple key, @NotNull Tuple val) {
return putIfAbsentAsync(tx, key, val).join();
return sync(putIfAbsentAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand All @@ -179,13 +181,13 @@ public boolean putIfAbsent(@Nullable Transaction tx, @NotNull Tuple key, @NotNul
/** {@inheritDoc} */
@Override
public boolean remove(@Nullable Transaction tx, @NotNull Tuple key) {
return removeAsync(tx, key).join();
return sync(removeAsync(tx, key));
}

/** {@inheritDoc} */
@Override
public boolean remove(@Nullable Transaction tx, @NotNull Tuple key, @NotNull Tuple val) {
return removeAsync(tx, key, val).join();
return sync(removeAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -214,7 +216,7 @@ public boolean remove(@Nullable Transaction tx, @NotNull Tuple key, @NotNull Tup
/** {@inheritDoc} */
@Override
public Collection<Tuple> removeAll(@Nullable Transaction tx, @NotNull Collection<Tuple> keys) {
return removeAllAsync(tx, keys).join();
return sync(removeAllAsync(tx, keys));
}

/** {@inheritDoc} */
Expand All @@ -232,7 +234,7 @@ public Collection<Tuple> removeAll(@Nullable Transaction tx, @NotNull Collection
/** {@inheritDoc} */
@Override
public Tuple getAndRemove(@Nullable Transaction tx, @NotNull Tuple key) {
return getAndRemoveAsync(tx, key).join();
return sync(getAndRemoveAsync(tx, key));
}

/** {@inheritDoc} */
Expand All @@ -249,13 +251,13 @@ public Tuple getAndRemove(@Nullable Transaction tx, @NotNull Tuple key) {
/** {@inheritDoc} */
@Override
public boolean replace(@Nullable Transaction tx, @NotNull Tuple key, Tuple val) {
return replaceAsync(tx, key, val).join();
return sync(replaceAsync(tx, key, val));
}

/** {@inheritDoc} */
@Override
public boolean replace(@Nullable Transaction tx, @NotNull Tuple key, Tuple oldVal, Tuple newVal) {
return replaceAsync(tx, key, oldVal, newVal).join();
return sync(replaceAsync(tx, key, oldVal, newVal));
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -286,7 +288,7 @@ public boolean replace(@Nullable Transaction tx, @NotNull Tuple key, Tuple oldVa
/** {@inheritDoc} */
@Override
public Tuple getAndReplace(@Nullable Transaction tx, @NotNull Tuple key, Tuple val) {
return getAndReplaceAsync(tx, key, val).join();
return sync(getAndReplaceAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand Down
Expand Up @@ -17,6 +17,7 @@

package org.apache.ignite.internal.client.table;

import static org.apache.ignite.internal.client.ClientUtils.sync;
import static org.apache.ignite.internal.client.table.ClientTable.writeTx;

import java.io.Serializable;
Expand Down Expand Up @@ -77,7 +78,7 @@ public ClientKeyValueView(ClientTable tbl, Mapper<K> keyMapper, Mapper<V> valMap
/** {@inheritDoc} */
@Override
public V get(@Nullable Transaction tx, @NotNull K key) {
return getAsync(tx, key).join();
return sync(getAsync(tx, key));
}

/** {@inheritDoc} */
Expand All @@ -94,7 +95,7 @@ public V get(@Nullable Transaction tx, @NotNull K key) {
/** {@inheritDoc} */
@Override
public Map<K, V> getAll(@Nullable Transaction tx, @NotNull Collection<K> keys) {
return getAllAsync(tx, keys).join();
return sync(getAllAsync(tx, keys));
}

/** {@inheritDoc} */
Expand All @@ -116,7 +117,7 @@ public Map<K, V> getAll(@Nullable Transaction tx, @NotNull Collection<K> keys) {
/** {@inheritDoc} */
@Override
public boolean contains(@Nullable Transaction tx, @NotNull K key) {
return containsAsync(tx, key).join();
return sync(containsAsync(tx, key));
}

/** {@inheritDoc} */
Expand All @@ -133,7 +134,7 @@ public CompletableFuture<Boolean> containsAsync(@Nullable Transaction tx, @NotNu
/** {@inheritDoc} */
@Override
public void put(@Nullable Transaction tx, @NotNull K key, V val) {
putAsync(tx, key, val).join();
sync(putAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand All @@ -150,7 +151,7 @@ public void put(@Nullable Transaction tx, @NotNull K key, V val) {
/** {@inheritDoc} */
@Override
public void putAll(@Nullable Transaction tx, @NotNull Map<K, V> pairs) {
putAllAsync(tx, pairs).join();
sync(putAllAsync(tx, pairs));
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -181,7 +182,7 @@ public void putAll(@Nullable Transaction tx, @NotNull Map<K, V> pairs) {
/** {@inheritDoc} */
@Override
public V getAndPut(@Nullable Transaction tx, @NotNull K key, V val) {
return getAndPutAsync(tx, key, val).join();
return sync(getAndPutAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand All @@ -198,7 +199,7 @@ public V getAndPut(@Nullable Transaction tx, @NotNull K key, V val) {
/** {@inheritDoc} */
@Override
public boolean putIfAbsent(@Nullable Transaction tx, @NotNull K key, @NotNull V val) {
return putIfAbsentAsync(tx, key, val).join();
return sync(putIfAbsentAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand All @@ -215,13 +216,13 @@ public boolean putIfAbsent(@Nullable Transaction tx, @NotNull K key, @NotNull V
/** {@inheritDoc} */
@Override
public boolean remove(@Nullable Transaction tx, @NotNull K key) {
return removeAsync(tx, key).join();
return sync(removeAsync(tx, key));
}

/** {@inheritDoc} */
@Override
public boolean remove(@Nullable Transaction tx, @NotNull K key, V val) {
return removeAsync(tx, key, val).join();
return sync(removeAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -249,7 +250,7 @@ public boolean remove(@Nullable Transaction tx, @NotNull K key, V val) {
/** {@inheritDoc} */
@Override
public Collection<K> removeAll(@Nullable Transaction tx, @NotNull Collection<K> keys) {
return removeAllAsync(tx, keys).join();
return sync(removeAllAsync(tx, keys));
}

/** {@inheritDoc} */
Expand All @@ -271,7 +272,7 @@ public Collection<K> removeAll(@Nullable Transaction tx, @NotNull Collection<K>
/** {@inheritDoc} */
@Override
public V getAndRemove(@Nullable Transaction tx, @NotNull K key) {
return getAndRemoveAsync(tx, key).join();
return sync(getAndRemoveAsync(tx, key));
}

/** {@inheritDoc} */
Expand All @@ -288,15 +289,15 @@ public V getAndRemove(@Nullable Transaction tx, @NotNull K key) {
/** {@inheritDoc} */
@Override
public boolean replace(@Nullable Transaction tx, @NotNull K key, V val) {
return replaceAsync(tx, key, val).join();
return sync(replaceAsync(tx, key, val));
}

/** {@inheritDoc} */
@Override
public boolean replace(@Nullable Transaction tx, @NotNull K key, V oldVal, V newVal) {
Objects.requireNonNull(key);

return replaceAsync(tx, key, oldVal, newVal).join();
return sync(replaceAsync(tx, key, oldVal, newVal));
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -330,7 +331,7 @@ public boolean replace(@Nullable Transaction tx, @NotNull K key, V oldVal, V new
/** {@inheritDoc} */
@Override
public V getAndReplace(@Nullable Transaction tx, @NotNull K key, V val) {
return getAndReplaceAsync(tx, key, val).join();
return sync(getAndReplaceAsync(tx, key, val));
}

/** {@inheritDoc} */
Expand Down