2626import java .util .Set ;
2727import java .util .SortedMap ;
2828import java .util .SortedSet ;
29+ import java .util .UUID ;
2930import java .util .concurrent .CompletableFuture ;
3031import java .util .function .Consumer ;
3132import java .util .function .Function ;
@@ -108,7 +109,7 @@ public class TcpClientCache<K, V> implements ClientCache<K, V> {
108109 private final int cacheId ;
109110
110111 /** Channel. */
111- private final ReliableChannelImpl ch ;
112+ private final ReliableChannelEx ch ;
112113
113114 /** Cache name. */
114115 private final String name ;
@@ -144,11 +145,13 @@ public class TcpClientCache<K, V> implements ClientCache<K, V> {
144145 /** Constructor. */
145146 TcpClientCache (String name , ReliableChannelImpl ch , ClientBinaryMarshaller marsh , TcpClientTransactions transactions ,
146147 ClientCacheEntryListenersRegistry lsnrsRegistry , IgniteLogger log ) {
147- this (name , ch , marsh , transactions , lsnrsRegistry , false , null , log );
148+ this (name , new ReliableChannelWrapper (ch , name ), marsh , transactions , lsnrsRegistry , false , null , log );
149+
150+ ch .registerCacheIfCustomAffinity (name );
148151 }
149152
150153 /** Constructor. */
151- TcpClientCache (String name , ReliableChannelImpl ch , ClientBinaryMarshaller marsh ,
154+ private TcpClientCache (String name , ReliableChannelEx ch , ClientBinaryMarshaller marsh ,
152155 TcpClientTransactions transactions , ClientCacheEntryListenersRegistry lsnrsRegistry , boolean keepBinary ,
153156 ExpiryPolicy expiryPlc , IgniteLogger log ) {
154157 this .name = name ;
@@ -165,8 +168,6 @@ public class TcpClientCache<K, V> implements ClientCache<K, V> {
165168
166169 jCacheAdapter = new ClientJCacheAdapter <>(this );
167170
168- this .ch .registerCacheIfCustomAffinity (this .name );
169-
170171 this .log = log ;
171172 }
172173
@@ -1428,6 +1429,9 @@ private <T> T txAwareService(
14281429 throw new ClientException ("Transaction context has been lost due to connection errors. " +
14291430 "Cache operations are prohibited until current transaction closed." , e );
14301431 }
1432+ catch (Exception e ) {
1433+ throw convertException (e , name );
1434+ }
14311435 }
14321436 else if (affKey != null )
14331437 return ch .affinityService (cacheId , affKey , op , payloadWriter , payloadReader );
@@ -1459,7 +1463,7 @@ private <T> IgniteClientFuture<T> txAwareServiceAsync(
14591463 "Cache operations are prohibited until current transaction closed." , err ));
14601464 }
14611465 else if (err != null )
1462- fut .completeExceptionally (err );
1466+ fut .completeExceptionally (convertException (( Exception ) err , name ) );
14631467 else
14641468 fut .complete (res );
14651469 });
@@ -1540,8 +1544,9 @@ private void writeCacheInfo(PayloadOutputChannel payloadCh, TcpClientTransaction
15401544 ProtocolContext protocolCtx = payloadCh .clientChannel ().protocolCtx ();
15411545
15421546 if (!protocolCtx .isFeatureSupported (EXPIRY_POLICY )) {
1543- throw new ClientProtocolError (String .format ("Expire policies are not supported by the server " +
1544- "version %s, required version %s" , protocolCtx .version (), EXPIRY_POLICY .verIntroduced ()));
1547+ throw new ClientFeatureNotSupportedByServerException (String .format (
1548+ "Expire policies are not supported by the server version %s, required version %s" ,
1549+ protocolCtx .version (), EXPIRY_POLICY .verIntroduced ()));
15451550 }
15461551
15471552 flags |= WITH_EXPIRY_POLICY_FLAG_MASK ;
@@ -1754,4 +1759,136 @@ private boolean canBlockTx(boolean isGetOp, TransactionConcurrency concurrency,
17541759
17551760 return true ;
17561761 }
1762+
1763+ /** */
1764+ private static ClientException convertException (Exception e , String cacheName ) {
1765+ String msg = "Failed to perform cache operation [cacheName=" + cacheName + "]: " + e .getMessage ();
1766+
1767+ // Exception can be ClientProtocolError - it's an internal exception, can't be thrown to user.
1768+ if (!(e instanceof ClientException ))
1769+ return new ClientException (msg , e );
1770+ else if (X .hasCause (e , ClientServerError .class )) // Wrap server errors.
1771+ return new ClientException (msg , e );
1772+ else // Don't wrap authentication, authorization, connection errors.
1773+ return (ClientException )e ;
1774+ }
1775+
1776+ /** */
1777+ private static class ReliableChannelWrapper implements ReliableChannelEx {
1778+ /** */
1779+ private final ReliableChannelImpl delegate ;
1780+
1781+ /** */
1782+ private final String cacheName ;
1783+
1784+ /** */
1785+ public ReliableChannelWrapper (ReliableChannelImpl delegate , String cacheName ) {
1786+ this .delegate = delegate ;
1787+ this .cacheName = cacheName ;
1788+ }
1789+
1790+ /** {@inheritDoc} */
1791+ @ Override public <T > T service (
1792+ ClientOperation op ,
1793+ Consumer <PayloadOutputChannel > payloadWriter ,
1794+ Function <PayloadInputChannel , T > payloadReader
1795+ ) throws ClientException , ClientError {
1796+ try {
1797+ return delegate .service (op , payloadWriter , payloadReader );
1798+ }
1799+ catch (ClientException e ) {
1800+ throw convertException (e , cacheName );
1801+ }
1802+ }
1803+
1804+ /** {@inheritDoc} */
1805+ @ Override public <T > T service (
1806+ ClientOperation op ,
1807+ Consumer <PayloadOutputChannel > payloadWriter ,
1808+ Function <PayloadInputChannel , T > payloadReader ,
1809+ List <UUID > targetNodes
1810+ ) throws ClientException , ClientError {
1811+ try {
1812+ return delegate .service (op , payloadWriter , payloadReader , targetNodes );
1813+ }
1814+ catch (ClientException e ) {
1815+ throw convertException (e , cacheName );
1816+ }
1817+ }
1818+
1819+ /** {@inheritDoc} */
1820+ @ Override public <T > IgniteClientFuture <T > serviceAsync (
1821+ ClientOperation op ,
1822+ Consumer <PayloadOutputChannel > payloadWriter ,
1823+ Function <PayloadInputChannel , T > payloadReader
1824+ ) throws ClientException , ClientError {
1825+ CompletableFuture <T > fut = new CompletableFuture <>();
1826+
1827+ delegate .serviceAsync (op , payloadWriter , payloadReader ).whenComplete ((res , err ) -> {
1828+ if (err != null )
1829+ fut .completeExceptionally (convertException ((Exception )err , cacheName ));
1830+ else
1831+ fut .complete (res );
1832+ });
1833+
1834+ return new IgniteClientFutureImpl <>(fut );
1835+ }
1836+
1837+ /** {@inheritDoc} */
1838+ @ Override public <T > T affinityService (
1839+ int cacheId ,
1840+ Object key ,
1841+ ClientOperation op ,
1842+ Consumer <PayloadOutputChannel > payloadWriter ,
1843+ Function <PayloadInputChannel , T > payloadReader
1844+ ) throws ClientException , ClientError {
1845+ try {
1846+ return delegate .affinityService (cacheId , key , op , payloadWriter , payloadReader );
1847+ }
1848+ catch (ClientException e ) {
1849+ throw convertException (e , cacheName );
1850+ }
1851+ }
1852+
1853+ /** {@inheritDoc} */
1854+ @ Override public <T > T affinityService (
1855+ int cacheId ,
1856+ int part ,
1857+ ClientOperation op ,
1858+ Consumer <PayloadOutputChannel > payloadWriter ,
1859+ Function <PayloadInputChannel , T > payloadReader
1860+ ) throws ClientException , ClientError {
1861+ try {
1862+ return delegate .affinityService (cacheId , part , op , payloadWriter , payloadReader );
1863+ }
1864+ catch (ClientException e ) {
1865+ throw convertException (e , cacheName );
1866+ }
1867+ }
1868+
1869+ /** {@inheritDoc} */
1870+ @ Override public <T > IgniteClientFuture <T > affinityServiceAsync (
1871+ int cacheId ,
1872+ Object key ,
1873+ ClientOperation op ,
1874+ Consumer <PayloadOutputChannel > payloadWriter ,
1875+ Function <PayloadInputChannel , T > payloadReader
1876+ ) throws ClientException , ClientError {
1877+ CompletableFuture <T > fut = new CompletableFuture <>();
1878+
1879+ delegate .affinityServiceAsync (cacheId , key , op , payloadWriter , payloadReader ).whenComplete ((res , err ) -> {
1880+ if (err != null )
1881+ fut .completeExceptionally (convertException ((Exception )err , cacheName ));
1882+ else
1883+ fut .complete (res );
1884+ });
1885+
1886+ return new IgniteClientFutureImpl <>(fut );
1887+ }
1888+
1889+ /** {@inheritDoc} */
1890+ @ Override public void close () {
1891+ delegate .close ();
1892+ }
1893+ }
17571894}
0 commit comments