Search in sources :

Example 1 with GridClientMarshaller

use of org.apache.ignite.internal.client.marshaller.GridClientMarshaller in project ignite by apache.

the class GridClientConnectionManagerAdapter method connect.

/**
 * Create new connection to specified server.
 *
 * @param nodeId {@code UUID} of node for mapping with connection.
 *      {@code null} if no need of mapping.
 * @param addr Remote socket to connect.
 * @return Established connection.
 * @throws IOException If connection failed.
 * @throws GridClientException If protocol error happened.
 * @throws InterruptedException If thread was interrupted before connection was established.
 */
protected GridClientConnection connect(@Nullable UUID nodeId, InetSocketAddress addr) throws IOException, GridClientException, InterruptedException {
    endpointStripedLock.lock(addr);
    try {
        GridClientConnection old = conns.get(addr);
        if (old != null) {
            if (old.isClosed()) {
                conns.remove(addr, old);
                if (nodeId != null)
                    nodeConns.remove(nodeId, old);
            } else {
                if (nodeId != null)
                    nodeConns.put(nodeId, old);
                return old;
            }
        }
        SecurityCredentials cred = null;
        try {
            if (cfg.getSecurityCredentialsProvider() != null)
                cred = cfg.getSecurityCredentialsProvider().credentials();
        } catch (IgniteCheckedException e) {
            throw new GridClientException("Failed to obtain client credentials.", e);
        }
        GridClientConnection conn;
        if (cfg.getProtocol() == GridClientProtocol.TCP) {
            GridClientMarshaller marsh = cfg.getMarshaller();
            try {
                conn = new GridClientNioTcpConnection(srv, clientId, addr, sslCtx, pingExecutor, cfg.getConnectTimeout(), cfg.getPingInterval(), cfg.getPingTimeout(), cfg.isTcpNoDelay(), marsh, marshId, top, cred, cfg.getUserAttributes());
            } catch (GridClientException e) {
                if (marsh instanceof GridClientZipOptimizedMarshaller) {
                    log.warning("Failed to connect with GridClientZipOptimizedMarshaller," + " trying to fallback to default marshaller: " + e);
                    conn = new GridClientNioTcpConnection(srv, clientId, addr, sslCtx, pingExecutor, cfg.getConnectTimeout(), cfg.getPingInterval(), cfg.getPingTimeout(), cfg.isTcpNoDelay(), ((GridClientZipOptimizedMarshaller) marsh).defaultMarshaller(), marshId, top, cred, cfg.getUserAttributes());
                } else
                    throw e;
            }
        } else
            throw new GridServerUnreachableException("Failed to create client (protocol is not supported): " + cfg.getProtocol());
        old = conns.putIfAbsent(addr, conn);
        assert old == null;
        if (nodeId != null)
            nodeConns.put(nodeId, conn);
        return conn;
    } finally {
        endpointStripedLock.unlock(addr);
    }
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) SecurityCredentials(org.apache.ignite.plugin.security.SecurityCredentials) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) GridClientZipOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientZipOptimizedMarshaller)

Example 2 with GridClientMarshaller

use of org.apache.ignite.internal.client.marshaller.GridClientMarshaller in project ignite by apache.

the class GridTcpRestProtocol method onProcessorStart.

/**
 * {@inheritDoc}
 */
@Override
public void onProcessorStart() {
    super.onProcessorStart();
    Map<Byte, GridClientMarshaller> marshMap = new HashMap<>();
    ArrayList<PluginProvider> providers = new ArrayList<>(ctx.plugins().allProviders());
    GridClientOptimizedMarshaller optMarsh = new GridClientOptimizedMarshaller(providers);
    marshMap.put(GridClientOptimizedMarshaller.ID, optMarsh);
    marshMap.put(GridClientZipOptimizedMarshaller.ID, new GridClientZipOptimizedMarshaller(optMarsh, providers));
    try {
        IgnitePredicate<String> clsFilter = MarshallerUtils.classNameFilter(getClass().getClassLoader());
        marshMap.put(GridClientJdkMarshaller.ID, new GridClientJdkMarshaller(clsFilter));
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
    lsnr.marshallers(marshMap);
}
Also used : GridClientJdkMarshaller(org.apache.ignite.internal.client.marshaller.jdk.GridClientJdkMarshaller) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PluginProvider(org.apache.ignite.plugin.PluginProvider) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridClientOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller) IgniteException(org.apache.ignite.IgniteException) GridClientZipOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientZipOptimizedMarshaller)

Example 3 with GridClientMarshaller

use of org.apache.ignite.internal.client.marshaller.GridClientMarshaller in project ignite by apache.

the class GridTcpRestProtocol method onKernalStart.

/**
 * {@inheritDoc}
 */
@Override
public void onKernalStart() {
    super.onKernalStart();
    Map<Byte, GridClientMarshaller> marshMap = new HashMap<>();
    ArrayList<PluginProvider> providers = new ArrayList<>(ctx.plugins().allProviders());
    GridClientOptimizedMarshaller optMarsh = new GridClientOptimizedMarshaller(providers);
    marshMap.put(GridClientOptimizedMarshaller.ID, optMarsh);
    marshMap.put(GridClientZipOptimizedMarshaller.ID, new GridClientZipOptimizedMarshaller(optMarsh, providers));
    marshMap.put(GridClientJdkMarshaller.ID, new GridClientJdkMarshaller());
    lsnr.marshallers(marshMap);
}
Also used : GridClientJdkMarshaller(org.apache.ignite.internal.client.marshaller.jdk.GridClientJdkMarshaller) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) GridClientOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PluginProvider(org.apache.ignite.plugin.PluginProvider) GridClientZipOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientZipOptimizedMarshaller)

Example 4 with GridClientMarshaller

use of org.apache.ignite.internal.client.marshaller.GridClientMarshaller in project ignite by apache.

the class ClientMarshallerBenchmarkTest method testCacheRequestTime.

/**
 * @throws Exception If failed.
 */
@Test
public void testCacheRequestTime() throws Exception {
    GridClientCacheRequest req = new GridClientCacheRequest(CAS);
    req.clientId(UUID.randomUUID());
    req.cacheName("CacheName");
    req.requestId(1024);
    req.key("key");
    req.value(1L);
    req.value2(2L);
    Map<Object, Object> additional = new HashMap<>();
    for (int i = 0; i < 1000; i++) additional.put("key" + i, (long) i);
    req.values(additional);
    // Warm up.
    for (GridClientMarshaller marshaller : marshallers) {
        GridClientCacheRequest res = runMarshallUnmarshalLoop(req, 1, marshaller);
        assertEquals(req.operation(), res.operation());
        // requestId is not marshalled.
        assertEquals(0, res.requestId());
        // clientId is not marshalled.
        assertEquals(null, res.clientId());
        // destinationId is not marshalled.
        assertEquals(null, res.destinationId());
        assertEquals(req.cacheName(), res.cacheName());
        assertEquals(req.key(), res.key());
        assertEquals(req.value(), res.value());
        assertEquals(req.value2(), res.value2());
        for (Map.Entry<Object, Object> e : req.values().entrySet()) assertEquals(e.getValue(), res.values().get(e.getKey()));
    }
    // Now real test.
    for (GridClientMarshaller marshaller : marshallers) runMarshallUnmarshalLoop(req, 1000, marshaller);
}
Also used : GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) HashMap(java.util.HashMap) GridClientCacheRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientCacheRequest) HashMap(java.util.HashMap) Map(java.util.Map) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with GridClientMarshaller

use of org.apache.ignite.internal.client.marshaller.GridClientMarshaller in project ignite by apache.

the class GridTcpRouterNioListenerAdapter method onMessage.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("TypeMayBeWeakened")
@Override
public void onMessage(final GridNioSession ses, final GridClientMessage msg) {
    if (msg instanceof GridRouterRequest) {
        GridRouterRequest routerMsg = (GridRouterRequest) msg;
        final UUID clientId = routerMsg.clientId();
        final long reqId = routerMsg.requestId();
        try {
            client.forwardMessage(routerMsg, routerMsg.destinationId(), ses.<Byte>meta(MARSHALLER_ID.ordinal())).listen(new GridClientFutureListener() {

                @Override
                public void onDone(GridClientFuture fut) {
                    try {
                        GridRouterResponse res = (GridRouterResponse) fut.get();
                        // Restoring original request id, because it was overwritten by the client.
                        res.requestId(reqId);
                        ses.send(res);
                    } catch (GridClientException e) {
                        ses.send(makeFailureResponse(e, clientId, reqId));
                    }
                }
            });
        } catch (GridClientException e) {
            ses.send(makeFailureResponse(e, clientId, reqId));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            U.warn(log, "Message forwarding was interrupted (will ignore last message): " + e.getMessage());
        }
    } else if (msg instanceof GridClientHandshakeRequest) {
        GridClientHandshakeRequest hs = (GridClientHandshakeRequest) msg;
        short ver = hs.version();
        if (!SUPP_VERS.contains(ver)) {
            U.error(log, "Client protocol version is not supported [ses=" + ses + ", ver=" + ver + ", supported=" + SUPP_VERS + ']');
            ses.close();
        } else {
            byte marshId = hs.marshallerId();
            GridClientMarshaller marsh = marshMap.get(marshId);
            if (marsh == null) {
                U.error(log, "Client marshaller ID is invalid. Note that .NET and C++ clients " + "are supported only in enterprise edition [ses=" + ses + ", marshId=" + marshId + ']');
                ses.close();
            } else {
                ses.addMeta(MARSHALLER_ID.ordinal(), marshId);
                ses.addMeta(MARSHALLER.ordinal(), marsh);
                ses.send(GridClientHandshakeResponse.OK);
            }
        }
    } else if (msg instanceof GridClientPingPacket)
        ses.send(GridClientPingPacket.PING_MESSAGE);
    else
        throw new IllegalArgumentException("Unsupported input message: " + msg);
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientFuture(org.apache.ignite.internal.client.GridClientFuture) GridRouterResponse(org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) GridRouterRequest(org.apache.ignite.internal.processors.rest.client.message.GridRouterRequest) GridClientPingPacket(org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket) GridClientFutureListener(org.apache.ignite.internal.client.GridClientFutureListener) GridClientHandshakeRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest) UUID(java.util.UUID)

Aggregations

GridClientMarshaller (org.apache.ignite.internal.client.marshaller.GridClientMarshaller)8 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 HashMap (java.util.HashMap)3 GridClientZipOptimizedMarshaller (org.apache.ignite.internal.client.marshaller.optimized.GridClientZipOptimizedMarshaller)3 GridClientPingPacket (org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket)3 GridRouterResponse (org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse)3 ArrayList (java.util.ArrayList)2 GridClientException (org.apache.ignite.internal.client.GridClientException)2 GridClientJdkMarshaller (org.apache.ignite.internal.client.marshaller.jdk.GridClientJdkMarshaller)2 GridClientOptimizedMarshaller (org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller)2 GridClientHandshakeRequest (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest)2 GridClientMessage (org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)2 GridClientResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)2 PluginProvider (org.apache.ignite.plugin.PluginProvider)2 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1 UUID (java.util.UUID)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)1