Search in sources :

Example 6 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class FailbackRegistry method retry.

// Retry the failed actions
protected void retry() {
    if (!failedRegistered.isEmpty()) {
        Set<URL> failed = new HashSet<URL>(failedRegistered);
        if (failed.size() > 0) {
            if (logger.isInfoEnabled()) {
                logger.info("Retry register " + failed);
            }
            try {
                for (URL url : failed) {
                    try {
                        doRegister(url);
                        failedRegistered.remove(url);
                    } catch (Throwable t) {
                        // Ignore all the exceptions and wait for the next retry
                        logger.warn("Failed to retry register " + failed + ", waiting for again, cause: " + t.getMessage(), t);
                    }
                }
            } catch (Throwable t) {
                // Ignore all the exceptions and wait for the next retry
                logger.warn("Failed to retry register " + failed + ", waiting for again, cause: " + t.getMessage(), t);
            }
        }
    }
    if (!failedUnregistered.isEmpty()) {
        Set<URL> failed = new HashSet<URL>(failedUnregistered);
        if (!failed.isEmpty()) {
            if (logger.isInfoEnabled()) {
                logger.info("Retry unregister " + failed);
            }
            try {
                for (URL url : failed) {
                    try {
                        doUnregister(url);
                        failedUnregistered.remove(url);
                    } catch (Throwable t) {
                        // Ignore all the exceptions and wait for the next retry
                        logger.warn("Failed to retry unregister  " + failed + ", waiting for again, cause: " + t.getMessage(), t);
                    }
                }
            } catch (Throwable t) {
                // Ignore all the exceptions and wait for the next retry
                logger.warn("Failed to retry unregister  " + failed + ", waiting for again, cause: " + t.getMessage(), t);
            }
        }
    }
    if (!failedSubscribed.isEmpty()) {
        Map<URL, Set<NotifyListener>> failed = new HashMap<URL, Set<NotifyListener>>(failedSubscribed);
        for (Map.Entry<URL, Set<NotifyListener>> entry : new HashMap<URL, Set<NotifyListener>>(failed).entrySet()) {
            if (entry.getValue() == null || entry.getValue().size() == 0) {
                failed.remove(entry.getKey());
            }
        }
        if (failed.size() > 0) {
            if (logger.isInfoEnabled()) {
                logger.info("Retry subscribe " + failed);
            }
            try {
                for (Map.Entry<URL, Set<NotifyListener>> entry : failed.entrySet()) {
                    URL url = entry.getKey();
                    Set<NotifyListener> listeners = entry.getValue();
                    for (NotifyListener listener : listeners) {
                        try {
                            doSubscribe(url, listener);
                            listeners.remove(listener);
                        } catch (Throwable t) {
                            // Ignore all the exceptions and wait for the next retry
                            logger.warn("Failed to retry subscribe " + failed + ", waiting for again, cause: " + t.getMessage(), t);
                        }
                    }
                }
            } catch (Throwable t) {
                // Ignore all the exceptions and wait for the next retry
                logger.warn("Failed to retry subscribe " + failed + ", waiting for again, cause: " + t.getMessage(), t);
            }
        }
    }
    if (!failedUnsubscribed.isEmpty()) {
        Map<URL, Set<NotifyListener>> failed = new HashMap<URL, Set<NotifyListener>>(failedUnsubscribed);
        for (Map.Entry<URL, Set<NotifyListener>> entry : new HashMap<URL, Set<NotifyListener>>(failed).entrySet()) {
            if (entry.getValue() == null || entry.getValue().isEmpty()) {
                failed.remove(entry.getKey());
            }
        }
        if (failed.size() > 0) {
            if (logger.isInfoEnabled()) {
                logger.info("Retry unsubscribe " + failed);
            }
            try {
                for (Map.Entry<URL, Set<NotifyListener>> entry : failed.entrySet()) {
                    URL url = entry.getKey();
                    Set<NotifyListener> listeners = entry.getValue();
                    for (NotifyListener listener : listeners) {
                        try {
                            doUnsubscribe(url, listener);
                            listeners.remove(listener);
                        } catch (Throwable t) {
                            // Ignore all the exceptions and wait for the next retry
                            logger.warn("Failed to retry unsubscribe " + failed + ", waiting for again, cause: " + t.getMessage(), t);
                        }
                    }
                }
            } catch (Throwable t) {
                // Ignore all the exceptions and wait for the next retry
                logger.warn("Failed to retry unsubscribe " + failed + ", waiting for again, cause: " + t.getMessage(), t);
            }
        }
    }
    if (!failedNotified.isEmpty()) {
        Map<URL, Map<NotifyListener, List<URL>>> failed = new HashMap<URL, Map<NotifyListener, List<URL>>>(failedNotified);
        for (Map.Entry<URL, Map<NotifyListener, List<URL>>> entry : new HashMap<URL, Map<NotifyListener, List<URL>>>(failed).entrySet()) {
            if (entry.getValue() == null || entry.getValue().size() == 0) {
                failed.remove(entry.getKey());
            }
        }
        if (failed.size() > 0) {
            if (logger.isInfoEnabled()) {
                logger.info("Retry notify " + failed);
            }
            try {
                for (Map<NotifyListener, List<URL>> values : failed.values()) {
                    for (Map.Entry<NotifyListener, List<URL>> entry : values.entrySet()) {
                        try {
                            NotifyListener listener = entry.getKey();
                            List<URL> urls = entry.getValue();
                            listener.notify(urls);
                            values.remove(listener);
                        } catch (Throwable t) {
                            // Ignore all the exceptions and wait for the next retry
                            logger.warn("Failed to retry notify " + failed + ", waiting for again, cause: " + t.getMessage(), t);
                        }
                    }
                }
            } catch (Throwable t) {
                // Ignore all the exceptions and wait for the next retry
                logger.warn("Failed to retry notify " + failed + ", waiting for again, cause: " + t.getMessage(), t);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) URL(com.alibaba.dubbo.common.URL) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) HashSet(java.util.HashSet) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 7 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class FailbackRegistryTest method testDoRetry.

/**
 * Test method for
 * {@link com.alibaba.dubbo.registry.support.FailbackRegistry#retry()}.
 *
 * @throws Exception
 */
@Test
public void testDoRetry() throws Exception {
    final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
    // All of them are called 3 times. Successful attempts to reduce the failure of 1. subscribe register will not be done again
    final CountDownLatch latch = new CountDownLatch(3);
    NotifyListener listner = new NotifyListener() {

        public void notify(List<URL> urls) {
            notified.set(Boolean.TRUE);
        }
    };
    registry = new MockRegistry(registryUrl, latch);
    registry.setBad(true);
    registry.register(serviceUrl);
    registry.unregister(serviceUrl);
    registry.subscribe(serviceUrl.setProtocol(Constants.CONSUMER_PROTOCOL).addParameters(CollectionUtils.toStringMap("check", "false")), listner);
    registry.unsubscribe(serviceUrl.setProtocol(Constants.CONSUMER_PROTOCOL).addParameters(CollectionUtils.toStringMap("check", "false")), listner);
    // Failure can not be called to listener.
    assertEquals(false, notified.get());
    assertEquals(3, latch.getCount());
    registry.setBad(false);
    for (int i = 0; i < trytimes; i++) {
        System.out.println("failback registry retry ,times:" + i);
        // System.out.println(latch.getCount());
        if (latch.getCount() == 0)
            break;
        Thread.sleep(sleeptime);
    }
    // Thread.sleep(100000);//for debug
    assertEquals(0, latch.getCount());
    // The failedsubcribe corresponding key will be cleared when unsubscribing
    assertEquals(false, notified.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) NotifyListener(com.alibaba.dubbo.registry.NotifyListener) Test(org.junit.Test)

Example 8 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class FailbackRegistryTest method testDoRetry_register.

@Test
public void testDoRetry_register() throws Exception {
    final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
    // All of them are called 4 times. A successful attempt to lose 1. subscribe will not be done
    final CountDownLatch latch = new CountDownLatch(1);
    NotifyListener listner = new NotifyListener() {

        public void notify(List<URL> urls) {
            notified.set(Boolean.TRUE);
        }
    };
    registry = new MockRegistry(registryUrl, latch);
    registry.setBad(true);
    registry.subscribe(serviceUrl.setProtocol(Constants.CONSUMER_PROTOCOL).addParameters(CollectionUtils.toStringMap("check", "false")), listner);
    // Failure can not be called to listener.
    assertEquals(false, notified.get());
    assertEquals(1, latch.getCount());
    registry.setBad(false);
    for (int i = 0; i < trytimes; i++) {
        System.out.println("failback registry retry ,times:" + i);
        // System.out.println(latch.getCount());
        if (latch.getCount() == 0)
            break;
        Thread.sleep(sleeptime);
    }
    // Thread.sleep(100000);
    assertEquals(0, latch.getCount());
    // The failedsubcribe corresponding key will be cleared when unsubscribing
    assertEquals(true, notified.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) NotifyListener(com.alibaba.dubbo.registry.NotifyListener) Test(org.junit.Test)

Example 9 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class AbstractRegistryService method doNotify.

private void doNotify(String service, List<URL> urls) {
    notified.put(service, urls);
    List<NotifyListener> listeners = notifyListeners.get(service);
    if (listeners != null) {
        for (NotifyListener listener : listeners) {
            try {
                notify(service, urls, listener);
            } catch (Throwable t) {
                logger.error("Failed to notify registry event, service: " + service + ", urls: " + urls + ", cause: " + t.getMessage(), t);
            }
        }
    }
}
Also used : NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 10 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class SimpleRegistryService method disconnect.

public void disconnect() {
    String client = RpcContext.getContext().getRemoteAddressString();
    if (logger.isInfoEnabled()) {
        logger.info("Disconnected " + client);
    }
    ConcurrentMap<String, URL> urls = remoteRegistered.get(client);
    if (urls != null && urls.size() > 0) {
        for (Map.Entry<String, URL> entry : urls.entrySet()) {
            super.unregister(entry.getKey(), entry.getValue());
        }
    }
    Map<String, NotifyListener> listeners = remoteListeners.get(client);
    if (listeners != null && listeners.size() > 0) {
        for (Map.Entry<String, NotifyListener> entry : listeners.entrySet()) {
            String service = entry.getKey();
            super.unsubscribe(service, new URL("subscribe", RpcContext.getContext().getRemoteHost(), RpcContext.getContext().getRemotePort(), com.alibaba.dubbo.registry.RegistryService.class.getName(), getSubscribed(service)), entry.getValue());
        }
    }
}
Also used : Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Aggregations

NotifyListener (com.alibaba.dubbo.registry.NotifyListener)30 URL (com.alibaba.dubbo.common.URL)25 Set (java.util.Set)16 ConcurrentHashSet (com.alibaba.dubbo.common.utils.ConcurrentHashSet)15 Map (java.util.Map)15 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)15 ConcurrentMap (java.util.concurrent.ConcurrentMap)13 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Test (org.junit.Test)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 RegistryProtocol (com.alibaba.dubbo.registry.integration.RegistryProtocol)2 ChildListener (com.alibaba.dubbo.remoting.zookeeper.ChildListener)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1