Java Code Examples for org.apache.servicecomb.core.Invocation#getLocalContext()

The following examples show how to use org.apache.servicecomb.core.Invocation#getLocalContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadbalanceHandler.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private boolean defineEndpointAndHandle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
  Object endpoint = invocation.getLocalContext(SERVICECOMB_SERVER_ENDPOINT);
  if (endpoint == null) {
    return false;
  }
  if (endpoint instanceof String) {
    // compatible to old usage
    endpoint = parseEndpoint((String) endpoint);
  }

  invocation.setEndpoint((Endpoint) endpoint);
  invocation.next(resp -> {
    asyncResp.handle(resp);
  });
  return true;
}
 
Example 2
Source File: LoadBalancer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public ServiceCombServer chooseServer(Invocation invocation) {
  List<ServiceCombServer> servers = invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST);
  int serversCount = servers.size();
  for (ServerListFilterExt filterExt : filters) {
    if(!filterExt.enabled()) {
      continue;
    }
    servers = filterExt.getFilteredListOfServers(servers, invocation);
    if (servers.isEmpty() && serversCount > 0) {
      LOGGER.warn("There are not servers exist after filtered by {}.", filterExt.getClass());
      break;
    }
  }
  ServiceCombServer server = rule.choose(servers, invocation);
  if (null == server) {
    return null;
  }
  ServiceCombServerStats serverStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server);
  if (serverStats.isIsolated()) {
    LOGGER.info("The Service {}'s instance {} has been isolated for a while, give a single test opportunity.",
        invocation.getMicroserviceName(),
        server.getInstance().getInstanceId());
  }
  return server;
}
 
Example 3
Source File: RestProducerInvocationCreatorTest.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void should_save_requestEx_in_invocation_context() {
  mockGetServicePathManager();

  Invocation invocation = creator.create();

  Object request = invocation.getLocalContext(RestConst.REST_REQUEST);
  assertThat(request).isSameAs(requestEx);
}
 
Example 4
Source File: UrlPathWithQueryAccessItem.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public void appendClientFormattedItem(InvocationFinishEvent finishEvent, StringBuilder builder) {
  Invocation invocation = finishEvent.getInvocation();
  if (null == invocation || null == invocation.getLocalContext(REST_CLIENT_REQUEST_PATH)
      || StringUtils.isEmpty(invocation.getLocalContext(REST_CLIENT_REQUEST_PATH).toString())) {
    builder.append(EMPTY_RESULT);
    return;
  }
  builder.append(invocation.getLocalContext(REST_CLIENT_REQUEST_PATH).toString());
}
 
Example 5
Source File: TestLoadBalanceCreator.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadBalanceWithSessionSticknessRule(@Injectable Invocation invocation,
    @Injectable Transport transport) {
  SessionStickinessRule rule = new SessionStickinessRule();
  LoadBalancer lb = new LoadBalancer(rule, "service");

  List<ServiceCombServer> servers = new ArrayList<>();
  Endpoint host1 = new Endpoint(transport, "host1");
  MicroserviceInstance instance1 = new MicroserviceInstance();
  ServiceCombServer server = new ServiceCombServer(host1, instance1);
  instance1.setInstanceId("instance1");

  Endpoint host2 = new Endpoint(transport, "host2");
  MicroserviceInstance instance2 = new MicroserviceInstance();
  ServiceCombServer server2 = new ServiceCombServer(host2, instance2);
  instance2.setInstanceId("instance2");

  servers.add(server);
  servers.add(server2);

  lb.setFilters(new ArrayList<>());
  new Expectations() {
    {
      invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST);
      result = servers;
    }
  };

  Server s = lb.chooseServer(invocation);
  Assert.assertEquals(server, s);
  s = lb.chooseServer(invocation);
  Assert.assertEquals(server, s);

  long time = Deencapsulation.getField(rule, "lastAccessedTime");
  Deencapsulation.setField(rule, "lastAccessedTime", time - 1000 * 300);
  ArchaiusUtils.setProperty("cse.loadbalance.service.SessionStickinessRule.sessionTimeoutInSeconds", 9);
  s = lb.chooseServer(invocation);
  Assert.assertEquals(server2, s);

  ArchaiusUtils.setProperty("cse.loadbalance.service.SessionStickinessRule.successiveFailedTimes", 5);
  lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
  lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
  lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
  lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
  lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
  s = lb.chooseServer(invocation);
  Assert.assertEquals(server, s);
}