@SuppressWarnings("unchecked")
  @Test
  public void testRpcException() {
    Logger logger = EasyMock.createMock(Logger.class);
    RpcContext.getContext().setRemoteAddress("127.0.0.1", 1234);
    RpcException exception = new RpcException("TestRpcException");
    logger.error(
        EasyMock.eq(
            "Got unchecked and undeclared exception which called by 127.0.0.1. service: "
                + DemoService.class.getName()
                + ", method: sayHello, exception: "
                + RpcException.class.getName()
                + ": TestRpcException"),
        EasyMock.eq(exception));
    ExceptionFilter exceptionFilter = new ExceptionFilter(logger);
    RpcInvocation invocation =
        new RpcInvocation("sayHello", new Class<?>[] {String.class}, new Object[] {"world"});
    Invoker<DemoService> invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class);
    EasyMock.expect(invoker.invoke(EasyMock.eq(invocation))).andThrow(exception);

    EasyMock.replay(logger, invoker);

    try {
      exceptionFilter.invoke(invoker, invocation);
    } catch (RpcException e) {
      assertEquals("TestRpcException", e.getMessage());
    }
    EasyMock.verify(logger, invoker);
    RpcContext.removeContext();
  }
 protected void recover() throws Exception {
   // register
   Set<URL> recoverRegistered = new HashSet<URL>(getRegistered());
   if (!recoverRegistered.isEmpty()) {
     if (logger.isInfoEnabled()) {
       logger.info("Recover register url " + recoverRegistered);
     }
     for (URL url : recoverRegistered) {
       register(url);
     }
   }
   // subscribe
   Map<URL, Set<NotifyListener>> recoverSubscribed =
       new HashMap<URL, Set<NotifyListener>>(getSubscribed());
   if (!recoverSubscribed.isEmpty()) {
     if (logger.isInfoEnabled()) {
       logger.info("Recover subscribe url " + recoverSubscribed.keySet());
     }
     for (Map.Entry<URL, Set<NotifyListener>> entry : recoverSubscribed.entrySet()) {
       URL url = entry.getKey();
       for (NotifyListener listener : entry.getValue()) {
         subscribe(url, listener);
       }
     }
   }
 }
Exemple #3
0
 public void execute(Context context) {
   Set<String> applications = new HashSet<String>();
   Set<String> services = new HashSet<String>();
   List<Provider> pList = new ArrayList<Provider>();
   try {
     pList = providerService.findAll();
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   for (Provider p : pList) {
     applications.add(p.getApplication());
     services.add(p.getService());
   }
   List<Consumer> cList = new ArrayList<Consumer>();
   try {
     cList = consumerService.findAll();
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   for (Consumer c : cList) {
     applications.add(c.getApplication());
     services.add(c.getService());
   }
   context.put("rootContextPath", new RootContextPath(request.getContextPath()));
   context.put("services", services.size());
   context.put("providers", pList.size());
   context.put("consumers", cList.size());
   context.put("applications", applications.size());
 }
 public void destroy() {
   super.destroy();
   try {
     expireFuture.cancel(true);
   } catch (Throwable t) {
     logger.warn(t.getMessage(), t);
   }
   try {
     for (RedisRegistryNotifier notifier : notifiers.values()) {
       notifier.shutdown();
     }
   } catch (Throwable t) {
     logger.warn(t.getMessage(), t);
   }
   for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
     JedisPool jedisPool = entry.getValue();
     try {
       jedisPool.destroy();
     } catch (Throwable t) {
       logger.warn(
           "Failed to destroy the redis registry client. registry: "
               + entry.getKey()
               + ", cause: "
               + t.getMessage(),
           t);
     }
   }
 }
Exemple #5
0
  public static void info(Logger logger, Throwable e) {
    if (logger == null) {
      return;
    }

    if (logger.isInfoEnabled()) {
      logger.info(e);
    }
  }
 public void unregister(URL url) {
   if (url == null) {
     throw new IllegalArgumentException("unregister url == null");
   }
   if (logger.isInfoEnabled()) {
     logger.info("Unregister: " + url);
   }
   registered.remove(url);
 }
Exemple #7
0
  public static void debug(Logger logger, String msg, Throwable e) {
    if (logger == null) {
      return;
    }

    if (logger.isDebugEnabled()) {
      logger.debug(msg, e);
    }
  }
Exemple #8
0
  public static void info(Logger logger, String msg) {
    if (logger == null) {
      return;
    }

    if (logger.isInfoEnabled()) {
      logger.info(msg);
    }
  }
Exemple #9
0
  public static void trace(Logger logger, Throwable throwable) {
    if (logger == null) {
      return;
    }

    if (logger.isTraceEnabled()) {
      logger.trace(throwable);
    }
  }
Exemple #10
0
  public static void trace(Logger logger, String msg, Throwable e) {
    if (logger == null) {
      return;
    }

    if (logger.isTraceEnabled()) {
      logger.trace(msg, e);
    }
  }
Exemple #11
0
  public static void error(Logger logger, String msg, Throwable e) {
    if (logger == null) {
      return;
    }

    if (logger.isErrorEnabled()) {
      logger.error(msg, e);
    }
  }
Exemple #12
0
  public static void warn(Logger logger, Throwable e) {
    if (logger == null) {
      return;
    }

    if (logger.isWarnEnabled()) {
      logger.warn(e);
    }
  }
Exemple #13
0
  public static void warn(Logger logger, String msg) {
    if (logger == null) {
      return;
    }

    if (logger.isWarnEnabled()) {
      logger.warn(msg);
    }
  }
Exemple #14
0
  protected Object decode(Channel channel, ChannelBuffer buffer, int readable, byte[] header)
      throws IOException {
    // check magic number.
    if (readable > 0 && header[0] != MAGIC_HIGH || readable > 1 && header[1] != MAGIC_LOW) {
      int length = header.length;
      if (header.length < readable) {
        header = Bytes.copyOf(header, readable);
        buffer.readBytes(header, length, readable - length);
      }
      for (int i = 1; i < header.length - 1; i++) {
        if (header[i] == MAGIC_HIGH && header[i + 1] == MAGIC_LOW) {
          buffer.readerIndex(buffer.readerIndex() - header.length + i);
          header = Bytes.copyOf(header, i);
          break;
        }
      }
      return super.decode(channel, buffer, readable, header);
    }
    // check length.
    if (readable < HEADER_LENGTH) {
      return DecodeResult.NEED_MORE_INPUT;
    }

    // get data length.
    int len = Bytes.bytes2int(header, 12);
    checkPayload(channel, len);

    int tt = len + HEADER_LENGTH;
    if (readable < tt) {
      return DecodeResult.NEED_MORE_INPUT;
    }

    // limit input stream.
    ChannelBufferInputStream is = new ChannelBufferInputStream(buffer, len);

    try {
      return decodeBody(channel, is, header);
    } finally {
      if (is.available() > 0) {
        try {
          if (logger.isWarnEnabled()) {
            logger.warn("Skip input stream " + is.available());
          }
          StreamUtils.skipUnusedStream(is);
        } catch (IOException e) {
          logger.warn(e.getMessage(), e);
        }
      }
    }
  }
Exemple #15
0
 public void unsubscribe(URL url, NotifyListener listener) {
   if (url == null) {
     throw new IllegalArgumentException("unsubscribe url == null");
   }
   if (listener == null) {
     throw new IllegalArgumentException("unsubscribe listener == null");
   }
   if (logger.isInfoEnabled()) {
     logger.info("Unsubscribe: " + url);
   }
   Set<NotifyListener> listeners = subscribed.get(url);
   if (listeners != null) {
     listeners.remove(listener);
   }
 }
Exemple #16
0
  public void encode(Channel channel, ChannelBuffer buffer, Object msg) throws IOException {
    if (msg instanceof Request) {
      encodeRequest(channel, buffer, (Request) msg);
    } else if (msg instanceof Response) {
      encodeResponse(channel, buffer, (Response) msg);
    } else {
      super.encode(channel, buffer, msg);
    }

    // TODO modified by lishen
    //        System.out.println(">>>>>>>>>>>>>>>>>>>>>> the resulting byte size of encoding is " +
    // buffer.readableBytes());
    if (logger.isTraceEnabled()) {
      logger.trace("the resulting byte size of encoding is " + buffer.readableBytes());
    }
  }
 private void deferExpired() {
   for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
     JedisPool jedisPool = entry.getValue();
     try {
       Jedis jedis = jedisPool.getResource();
       try {
         RedisRegistryUtil.publishToJedis(jedis, getRegistered(), root, expirePeriod);
         if (admin) {
           clean(jedis);
         }
         if (!replicate) {
           break; //  如果服务器端已同步数据,只需写入单台机器
         }
       } finally {
         jedisPool.returnResource(jedis);
       }
     } catch (Throwable t) {
       logger.warn(
           "Failed to write provider heartbeat to redis registry. registry: "
               + entry.getKey()
               + ", cause: "
               + t.getMessage(),
           t);
     }
   }
 }
Exemple #18
0
  private void saveProperties(URL url) {
    if (file == null) {
      return;
    }

    try {
      StringBuilder buf = new StringBuilder();
      Map<String, List<URL>> categoryNotified = notified.get(url);
      if (categoryNotified != null) {
        for (List<URL> us : categoryNotified.values()) {
          for (URL u : us) {
            if (buf.length() > 0) {
              buf.append(URL_SEPARATOR);
            }
            buf.append(u.toFullString());
          }
        }
      }
      properties.setProperty(url.getServiceKey(), buf.toString());
      long version = lastCacheChanged.incrementAndGet();
      if (syncSaveFile) {
        doSaveProperties(version);
      } else {
        registryCacheExecutor.execute(new SaveProperties(version));
      }
    } catch (Throwable t) {
      logger.warn(t.getMessage(), t);
    }
  }
Exemple #19
0
 private void returnResource(ShardedJedis shardedJedis) {
   try {
     shardedJedis.close();
   } catch (Exception e) {
     logger.error("returnResource error.", e);
   }
 }
 public void doSubscribe(final URL url, final NotifyListener listener) {
   String service = RedisRegistryUtil.toServicePath(url, root);
   RedisRegistryNotifier notifier = notifiers.get(service);
   if (notifier == null) {
     RedisRegistryNotifier newNotifier = new RedisRegistryNotifier(service, this);
     notifiers.putIfAbsent(service, newNotifier);
     notifier = notifiers.get(service);
     if (notifier == newNotifier) {
       notifier.start();
     }
   }
   boolean success = false;
   RpcException exception = null;
   for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
     JedisPool jedisPool = entry.getValue();
     try {
       Jedis jedis = jedisPool.getResource();
       try {
         if (service.endsWith(Constants.ANY_VALUE)) {
           admin = true;
           Set<String> keys = jedis.keys(service);
           if (keys != null && keys.size() > 0) {
             Map<String, Set<String>> serviceKeys = RedisRegistryUtil.getServiceKeys(keys, root);
             for (Set<String> sk : serviceKeys.values()) {
               doNotify(jedis, sk, url, Arrays.asList(listener));
             }
           }
         } else {
           doNotify(
               jedis,
               jedis.keys(service + Constants.PATH_SEPARATOR + Constants.ANY_VALUE),
               url,
               Arrays.asList(listener));
         }
         success = true;
         break; // 只需读一个服务器的数据
       } finally {
         jedisPool.returnResource(jedis);
       }
     } catch (Throwable t) { // 尝试下一个服务器
       exception =
           new RpcException(
               "Failed to subscribe service from redis registry. registry: "
                   + entry.getKey()
                   + ", service: "
                   + url
                   + ", cause: "
                   + t.getMessage(),
               t);
     }
   }
   if (exception != null) {
     if (success) {
       logger.warn(exception.getMessage(), exception);
     } else {
       throw exception;
     }
   }
 }
Exemple #21
0
 public void subscribe(URL url, NotifyListener listener) {
   if (url == null) {
     throw new IllegalArgumentException("subscribe url == null");
   }
   if (listener == null) {
     throw new IllegalArgumentException("subscribe listener == null");
   }
   if (logger.isInfoEnabled()) {
     logger.info("Subscribe: " + url);
   }
   Set<NotifyListener> listeners = subscribed.get(url);
   if (listeners == null) {
     subscribed.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
     listeners = subscribed.get(url);
   }
   listeners.add(listener);
 }
 public void close() {
   try {
     if (executor instanceof ExecutorService) {
       ((ExecutorService) executor).shutdown();
     }
   } catch (Throwable t) {
     logger.warn("fail to destroy thread pool of server: " + t.getMessage(), t);
   }
 }
Exemple #23
0
 protected void notify(URL url, NotifyListener listener, List<URL> urls) {
   if (url == null) {
     throw new IllegalArgumentException("notify url == null");
   }
   if (listener == null) {
     throw new IllegalArgumentException("notify listener == null");
   }
   if ((urls == null || urls.size() == 0)
       && !Constants.ANY_VALUE.equals(url.getServiceInterface())) {
     logger.warn("Ignore empty notify urls for subscribe url " + url);
     return;
   }
   if (logger.isInfoEnabled()) {
     logger.info("Notify urls for subscribe url " + url + ", urls: " + urls);
   }
   Map<String, List<URL>> result = new HashMap<String, List<URL>>();
   for (URL u : urls) {
     if (UrlUtils.isMatch(url, u)) {
       String category = u.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
       List<URL> categoryList = result.get(category);
       if (categoryList == null) {
         categoryList = new ArrayList<URL>();
         result.put(category, categoryList);
       }
       categoryList.add(u);
     }
   }
   if (result.size() == 0) {
     return;
   }
   Map<String, List<URL>> categoryNotified = notified.get(url);
   if (categoryNotified == null) {
     notified.putIfAbsent(url, new ConcurrentHashMap<String, List<URL>>());
     categoryNotified = notified.get(url);
   }
   for (Map.Entry<String, List<URL>> entry : result.entrySet()) {
     String category = entry.getKey();
     List<URL> categoryList = entry.getValue();
     categoryNotified.put(category, categoryList);
     saveProperties(url);
     listener.notify(categoryList);
   }
 }
 @Override
 protected void doClose() throws Throwable {
   try {
     if (acceptor != null) {
       acceptor.unbind(getBindAddress());
     }
   } catch (Throwable e) {
     logger.warn(e.getMessage(), e);
   }
 }
Exemple #25
0
 public void destroy() {
   if (logger.isInfoEnabled()) {
     logger.info("Destroy registry:" + getUrl());
   }
   Set<URL> destroyRegistered = new HashSet<URL>(getRegistered());
   if (!destroyRegistered.isEmpty()) {
     for (URL url : new HashSet<URL>(getRegistered())) {
       if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
         try {
           unregister(url);
           if (logger.isInfoEnabled()) {
             logger.info("Destroy unregister url " + url);
           }
         } catch (Throwable t) {
           logger.warn(
               "Failed to unregister url "
                   + url
                   + " to registry "
                   + getUrl()
                   + " on destroy, cause: "
                   + t.getMessage(),
               t);
         }
       }
     }
   }
   Map<URL, Set<NotifyListener>> destroySubscribed =
       new HashMap<URL, Set<NotifyListener>>(getSubscribed());
   if (!destroySubscribed.isEmpty()) {
     for (Map.Entry<URL, Set<NotifyListener>> entry : destroySubscribed.entrySet()) {
       URL url = entry.getKey();
       for (NotifyListener listener : entry.getValue()) {
         try {
           unsubscribe(url, listener);
           if (logger.isInfoEnabled()) {
             logger.info("Destroy unsubscribe url " + url);
           }
         } catch (Throwable t) {
           logger.warn(
               "Failed to unsubscribe url "
                   + url
                   + " to registry "
                   + getUrl()
                   + " on destroy, cause: "
                   + t.getMessage(),
               t);
         }
       }
     }
   }
 }
Exemple #26
0
 public String get(String key) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     return shardedJedis.get(key);
   } catch (Exception ex) {
     logger.error("get error.", ex);
     return null;
   } finally {
     returnResource(shardedJedis);
   }
 }
Exemple #27
0
 /**
  * 获得排序打分
  *
  * @param key
  * @return
  */
 public Double getScore(String key, String member) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     return shardedJedis.zscore(key, member);
   } catch (Exception ex) {
     logger.error("getSoredSet error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return null;
 }
Exemple #28
0
 /**
  * 获取List
  *
  * @param key
  * @return
  */
 public List<String> getList(String key) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     return shardedJedis.lrange(key, 0, -1);
   } catch (Exception ex) {
     logger.error("getList error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return null;
 }
Exemple #29
0
 public long decr(String key) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = masterShardedJedisPool.getResource();
     return shardedJedis.decr(key);
   } catch (Exception ex) {
     logger.error("incr error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return 0;
 }
  private void doNotify(
      Jedis jedis, Collection<String> keys, URL url, Collection<NotifyListener> listeners) {
    if (keys == null || keys.size() == 0 || listeners == null || listeners.size() == 0) {
      return;
    }
    long now = System.currentTimeMillis();
    List<URL> result = new ArrayList<URL>();
    List<String> categories =
        Arrays.asList(url.getParameter(Constants.CATEGORY_KEY, new String[0]));
    String consumerService = url.getServiceInterface();
    for (String key : keys) {
      if (!Constants.ANY_VALUE.equals(consumerService)) {
        String prvoiderService = RedisRegistryUtil.toServiceName(key, root);
        if (!prvoiderService.equals(consumerService)) {
          continue;
        }
      }
      String category = RedisRegistryUtil.toCategoryName(key);
      if (!categories.contains(Constants.ANY_VALUE) && !categories.contains(category)) {
        continue;
      }

      Map<String, String> values = jedis.hgetAll(key);
      List<URL> urls = RedisRegistryUtil.getUrlsForDoNotify(url, now, values);

      if (urls.isEmpty()) {
        urls.add(RedisRegistryUtil.setUrlProperties(url, key, category, root));
      }
      result.addAll(urls);
      if (logger.isWarnEnabled()) {
        logger.warn("redis notify: " + key + " = " + urls);
      }
    }
    if (result == null || result.size() == 0) {
      return;
    }
    for (NotifyListener listener : listeners) {
      notify(url, listener, result);
    }
  }