Search in sources :

Example 1 with AsyncToSyncInvoker

use of org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker in project dubbo by alibaba.

the class CallbackServiceCodec method referOrDestroyCallbackService.

/**
 * refer or destroy callback service on server side
 *
 * @param url
 */
@SuppressWarnings("unchecked")
private static Object referOrDestroyCallbackService(Channel channel, URL url, Class<?> clazz, Invocation inv, int instid, boolean isRefer) {
    Object proxy;
    String invokerCacheKey = getServerSideCallbackInvokerCacheKey(channel, clazz.getName(), instid);
    String proxyCacheKey = getServerSideCallbackServiceCacheKey(channel, clazz.getName(), instid);
    proxy = channel.getAttribute(proxyCacheKey);
    String countkey = getServerSideCountKey(channel, clazz.getName());
    if (isRefer) {
        if (proxy == null) {
            URL referurl = URL.valueOf("callback://" + url.getAddress() + "/" + clazz.getName() + "?" + INTERFACE_KEY + "=" + clazz.getName());
            referurl = referurl.addParametersIfAbsent(url.getParameters()).removeParameter(METHODS_KEY);
            if (!isInstancesOverLimit(channel, referurl, clazz.getName(), instid, true)) {
                ApplicationModel.getServiceRepository().registerService(clazz);
                @SuppressWarnings("rawtypes") Invoker<?> invoker = new ChannelWrappedInvoker(clazz, channel, referurl, String.valueOf(instid));
                proxy = PROXY_FACTORY.getProxy(new AsyncToSyncInvoker<>(invoker));
                channel.setAttribute(proxyCacheKey, proxy);
                channel.setAttribute(invokerCacheKey, invoker);
                increaseInstanceCount(channel, countkey);
                // convert error fail fast .
                // ignore concurrent problem.
                Set<Invoker<?>> callbackInvokers = (Set<Invoker<?>>) channel.getAttribute(CHANNEL_CALLBACK_KEY);
                if (callbackInvokers == null) {
                    callbackInvokers = new ConcurrentHashSet<>(1);
                    channel.setAttribute(CHANNEL_CALLBACK_KEY, callbackInvokers);
                }
                callbackInvokers.add(invoker);
                logger.info("method " + inv.getMethodName() + " include a callback service :" + invoker.getUrl() + ", a proxy :" + invoker + " has been created.");
            }
        }
    } else {
        if (proxy != null) {
            Invoker<?> invoker = (Invoker<?>) channel.getAttribute(invokerCacheKey);
            try {
                Set<Invoker<?>> callbackInvokers = (Set<Invoker<?>>) channel.getAttribute(CHANNEL_CALLBACK_KEY);
                if (callbackInvokers != null) {
                    callbackInvokers.remove(invoker);
                }
                invoker.destroy();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            // cancel refer, directly remove from the map
            channel.removeAttribute(proxyCacheKey);
            channel.removeAttribute(invokerCacheKey);
            decreaseInstanceCount(channel, countkey);
        }
    }
    return proxy;
}
Also used : Set(java.util.Set) ConcurrentHashSet(org.apache.dubbo.common.utils.ConcurrentHashSet) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) Invoker(org.apache.dubbo.rpc.Invoker) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) URL(org.apache.dubbo.common.URL) RemotingException(org.apache.dubbo.remoting.RemotingException) IOException(java.io.IOException)

Example 2 with AsyncToSyncInvoker

use of org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker in project dubbo by alibaba.

the class ReferenceCountExchangeClientTest method getInvokerClientList.

private List<ExchangeClient> getInvokerClientList(Invoker<?> invoker) {
    @SuppressWarnings("rawtypes") DubboInvoker dInvoker = (DubboInvoker) ((AsyncToSyncInvoker) invoker).getInvoker();
    try {
        Field clientField = DubboInvoker.class.getDeclaredField("clients");
        ReflectUtils.makeAccessible(clientField);
        ExchangeClient[] clients = (ExchangeClient[]) clientField.get(dInvoker);
        List<ExchangeClient> clientList = new ArrayList<ExchangeClient>(clients.length);
        for (ExchangeClient client : clients) {
            clientList.add(client);
        }
        // sorting makes it easy to compare between lists
        Collections.sort(clientList, Comparator.comparing(c -> Integer.valueOf(Objects.hashCode(c))));
        return clientList;
    } catch (Exception e) {
        e.printStackTrace();
        Assertions.fail(e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ExtensionLoader(org.apache.dubbo.common.extension.ExtensionLoader) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) SHARE_CONNECTIONS_KEY(org.apache.dubbo.rpc.protocol.dubbo.Constants.SHARE_CONNECTIONS_KEY) DubboAppender(org.apache.dubbo.common.utils.DubboAppender) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) Invoker(org.apache.dubbo.rpc.Invoker) NetUtils(org.apache.dubbo.common.utils.NetUtils) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) URL(org.apache.dubbo.common.URL) ProtocolUtils(org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) ReflectUtils(org.apache.dubbo.common.utils.ReflectUtils) Exporter(org.apache.dubbo.rpc.Exporter) CONNECTIONS_KEY(org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY) LogUtil(org.apache.dubbo.common.utils.LogUtil) Field(java.lang.reflect.Field) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) LAZY_REQUEST_WITH_WARNING_KEY(org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_REQUEST_WITH_WARNING_KEY) List(java.util.List) Assertions(org.junit.jupiter.api.Assertions) Comparator(java.util.Comparator) Collections(java.util.Collections) Field(java.lang.reflect.Field) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) ArrayList(java.util.ArrayList)

Example 3 with AsyncToSyncInvoker

use of org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker in project dubbo by alibaba.

the class DubboInvokerAvailableTest method test_Lazy_ChannelReadOnly.

@Test
public void test_Lazy_ChannelReadOnly() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf("dubbo://127.0.0.1:" + port + "/org.apache.dubbo.rpc.protocol.dubbo.IDemoService?lazy=true&connections=1&timeout=10000");
    ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url);
    AsyncToSyncInvoker<?> invoker = (AsyncToSyncInvoker) protocol.refer(IDemoService.class, url);
    Assertions.assertTrue(invoker.isAvailable());
    ExchangeClient exchangeClient = getClients((DubboInvoker<?>) invoker.getInvoker())[0];
    Assertions.assertFalse(exchangeClient.isClosed());
    try {
        exchangeClient.setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE);
        fail();
    } catch (IllegalStateException e) {
    }
    // invoke method --> init client
    IDemoService service = (IDemoService) proxy.getProxy(invoker);
    Assertions.assertEquals("ok", service.get());
    Assertions.assertTrue(invoker.isAvailable());
    exchangeClient.setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE);
    Assertions.assertFalse(invoker.isAvailable());
}
Also used : ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

URL (org.apache.dubbo.common.URL)3 AsyncToSyncInvoker (org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker)3 ExchangeClient (org.apache.dubbo.remoting.exchange.ExchangeClient)2 Invoker (org.apache.dubbo.rpc.Invoker)2 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 ExtensionLoader (org.apache.dubbo.common.extension.ExtensionLoader)1 ConcurrentHashSet (org.apache.dubbo.common.utils.ConcurrentHashSet)1 DubboAppender (org.apache.dubbo.common.utils.DubboAppender)1 LogUtil (org.apache.dubbo.common.utils.LogUtil)1 NetUtils (org.apache.dubbo.common.utils.NetUtils)1 ReflectUtils (org.apache.dubbo.common.utils.ReflectUtils)1 CONNECTIONS_KEY (org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY)1