use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class PassingTimeStrategyTest method should_do_nothing_when_start_not_as_first_node_of_a_process.
@Test
void should_do_nothing_when_start_not_as_first_node_of_a_process() {
Invocation invocation = new Invocation();
invocation.setContext(ImmutableMap.of());
invocation.setLocalContext(ImmutableMap.of(CHAIN_START_TIME, 10L));
Throwable throwable = catchThrowable(() -> strategy.start(invocation));
assertThat(throwable).isNull();
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestInvocationStageTrace method consumer.
@Test
public void consumer() {
invocation = new Invocation(referenceConfig, operationMeta, invocationRuntimeType, args);
stageTrace = new InvocationStageTrace(invocation);
stageTrace.start(1);
nanoTime = 2;
stageTrace.startHandlersRequest();
nanoTime = 3;
stageTrace.startClientFiltersRequest();
nanoTime = 4;
stageTrace.startGetConnection();
stageTrace.startSend();
nanoTime = 5;
stageTrace.finishGetConnection();
stageTrace.finishWriteToBuffer(6);
nanoTime = 7;
stageTrace.finishReceiveResponse();
nanoTime = 8;
stageTrace.startClientFiltersResponse();
nanoTime = 9;
stageTrace.finishClientFiltersResponse();
nanoTime = 10;
stageTrace.finishHandlersResponse();
nanoTime = 11;
stageTrace.finish();
Assert.assertEquals(1, stageTrace.getStart());
Assert.assertEquals(2, stageTrace.getStartHandlersRequest());
Assert.assertEquals(3, stageTrace.getStartClientFiltersRequest());
Assert.assertEquals(4, stageTrace.getStartSend());
Assert.assertEquals(5, stageTrace.getFinishGetConnection());
Assert.assertEquals(6, stageTrace.getFinishWriteToBuffer());
Assert.assertEquals(7, stageTrace.getFinishReceiveResponse());
Assert.assertEquals(8, stageTrace.getStartClientFiltersResponse());
Assert.assertEquals(9, stageTrace.getFinishClientFiltersResponse());
Assert.assertEquals(10, stageTrace.getFinishHandlersResponse());
Assert.assertEquals(11, stageTrace.getFinish());
Assert.assertEquals(1f, stageTrace.calcInvocationPrepareTime(), 0.1f);
Assert.assertEquals(1f, stageTrace.calcHandlersRequestTime(), 0.1f);
Assert.assertEquals(1f, stageTrace.calcClientFiltersRequestTime(), 0.1f);
Assert.assertEquals(2f, stageTrace.calcSendRequestTime(), 0.1f);
Assert.assertEquals(1f, stageTrace.calcGetConnectionTime(), 0.1f);
Assert.assertEquals(1f, stageTrace.calcWriteToBufferTime(), 0.1f);
Assert.assertEquals(1f, stageTrace.calcReceiveResponseTime(), 0.1f);
Assert.assertEquals(1f, stageTrace.calcWakeConsumer(), 0.1f);
Assert.assertEquals(1f, stageTrace.calcClientFiltersResponseTime(), 0.1f);
Assert.assertEquals(1f, stageTrace.calcHandlersResponseTime(), 0.1f);
Assert.assertEquals(10f, stageTrace.calcTotalTime(), 0.1f);
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestSyncResponseExecutor method testSyncResponseExecutor.
@Test
public void testSyncResponseExecutor() {
SyncResponseExecutor executor = new SyncResponseExecutor();
Runnable cmd = Mockito.mock(Runnable.class);
Response response = Mockito.mock(Response.class);
Invocation invocation = Mockito.mock(Invocation.class);
executor.execute(cmd);
executor.setResponse(response);
try {
Response responseValue = executor.waitResponse(invocation);
Assert.assertNotNull(responseValue);
} catch (Exception e) {
Assert.assertNotNull(e);
}
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestLoadBalanceHandler2 method testZoneAwareAndIsolationFilterWorks.
@Test
public void testZoneAwareAndIsolationFilterWorks() throws Exception {
ReferenceConfig referenceConfig = Mockito.mock(ReferenceConfig.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
InvocationRuntimeType invocationRuntimeType = Mockito.mock(InvocationRuntimeType.class);
SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
MicroserviceMeta microserviceMeta = Mockito.mock(MicroserviceMeta.class);
when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
when(schemaMeta.getMicroserviceName()).thenReturn("testMicroserviceName");
when(microserviceMeta.getAppId()).thenReturn("testApp");
when(referenceConfig.getVersionRule()).thenReturn("0.0.0+");
when(referenceConfig.getTransport()).thenReturn("rest");
Invocation invocation = new Invocation(referenceConfig, operationMeta, invocationRuntimeType, new HashMap<>());
InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
TransportManager transportManager = Mockito.mock(TransportManager.class);
Transport transport = Mockito.mock(Transport.class);
ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
// set up data
MicroserviceInstance myself = new MicroserviceInstance();
DataCenterInfo info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone");
myself.setDataCenterInfo(info);
MicroserviceInstance allmatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone");
List<String> allMatchEndpoint = new ArrayList<>();
allMatchEndpoint.add("rest://localhost:9090");
allmatchInstance.setEndpoints(allMatchEndpoint);
allmatchInstance.setDataCenterInfo(info);
allmatchInstance.setInstanceId("allmatchInstance");
MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone2");
List<String> regionMatchEndpoint = new ArrayList<>();
regionMatchEndpoint.add("rest://localhost:9091");
regionMatchInstance.setEndpoints(regionMatchEndpoint);
regionMatchInstance.setDataCenterInfo(info);
regionMatchInstance.setInstanceId("regionMatchInstance");
MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region2");
info.setAvailableZone("test-zone2");
List<String> noMatchEndpoint = new ArrayList<>();
noMatchEndpoint.add("rest://localhost:9092");
noneMatchInstance.setEndpoints(noMatchEndpoint);
noneMatchInstance.setDataCenterInfo(info);
noneMatchInstance.setInstanceId("noneMatchInstance");
Map<String, MicroserviceInstance> data = new HashMap<>();
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
scbEngine.setTransportManager(transportManager);
LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
mockUpInstanceCacheManager(instanceCacheManager);
when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
when(transportManager.findTransport("rest")).thenReturn(transport);
LoadbalanceHandler handler = null;
LoadBalancer loadBalancer = null;
ServiceCombServer server = null;
handler = new LoadbalanceHandler();
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals(null, server);
data.put("noneMatchInstance", noneMatchInstance);
parent.cacheVersion(1);
handler = new LoadbalanceHandler();
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9092", server.getEndpoint().getEndpoint());
data.put("regionMatchInstance", regionMatchInstance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
data.put("allmatchInstance", allmatchInstance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
// if errorThresholdPercentage is 0,that means errorThresholdPercentage is not active.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "0");
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
// if errorThresholdPercentage greater than 0, it will activate.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "20");
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "10");
ServiceCombServer server2 = server;
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server2);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
mockDelayMillis(20);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestLoadBalanceHandler2 method testStatusFilterUsingMockedInvocationWorks.
@Test
public void testStatusFilterUsingMockedInvocationWorks() throws Exception {
ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.status.enabled", "false");
Invocation invocation = new NonSwaggerInvocation("testApp", "testMicroserviceName", "0.0.0+", (inv, aysnc) -> {
aysnc.success("OK");
});
InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
TransportManager transportManager = Mockito.mock(TransportManager.class);
Transport transport = Mockito.mock(Transport.class);
ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
// set up data
MicroserviceInstance myself = new MicroserviceInstance();
DataCenterInfo info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone");
myself.setDataCenterInfo(info);
MicroserviceInstance allmatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone");
List<String> allMatchEndpoint = new ArrayList<>();
allMatchEndpoint.add("rest://localhost:7090");
allmatchInstance.setEndpoints(allMatchEndpoint);
allmatchInstance.setDataCenterInfo(info);
allmatchInstance.setInstanceId("allmatchInstance");
allmatchInstance.setStatus(MicroserviceInstanceStatus.TESTING);
MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone2");
List<String> regionMatchEndpoint = new ArrayList<>();
regionMatchEndpoint.add("rest://localhost:7091");
regionMatchInstance.setEndpoints(regionMatchEndpoint);
regionMatchInstance.setDataCenterInfo(info);
regionMatchInstance.setInstanceId("regionMatchInstance");
MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region2");
info.setAvailableZone("test-zone2");
List<String> noMatchEndpoint = new ArrayList<>();
noMatchEndpoint.add("rest://localhost:7092");
noneMatchInstance.setEndpoints(noMatchEndpoint);
noneMatchInstance.setDataCenterInfo(info);
noneMatchInstance.setInstanceId("noneMatchInstance");
Map<String, MicroserviceInstance> data = new HashMap<>();
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
scbEngine.setTransportManager(transportManager);
LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
mockUpInstanceCacheManager(instanceCacheManager);
when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
when(transportManager.findTransport("rest")).thenReturn(transport);
LoadbalanceHandler handler = null;
LoadBalancer loadBalancer = null;
ServiceCombServer server = null;
DiscoveryTree discoveryTree = new DiscoveryTree();
discoveryTree.addFilter(new IsolationDiscoveryFilter());
discoveryTree.addFilter(new ZoneAwareDiscoveryFilter());
discoveryTree.addFilter(new ServerDiscoveryFilter());
discoveryTree.sort();
handler = new LoadbalanceHandler(discoveryTree);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals(null, server);
data.put("noneMatchInstance", noneMatchInstance);
parent.cacheVersion(1);
handler = new LoadbalanceHandler();
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:7092", server.getEndpoint().getEndpoint());
handler.handle(invocation, (response) -> {
Assert.assertEquals("OK", response.getResult());
});
data.put("regionMatchInstance", regionMatchInstance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
handler.handle(invocation, (response) -> {
Assert.assertEquals("OK", response.getResult());
});
data.put("allmatchInstance", allmatchInstance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
// if errorThresholdPercentage is 0,that means errorThresholdPercentage is not active.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "0");
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
// if errorThresholdPercentage greater than 0, it will activate.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "20");
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "10");
ServiceCombServer server2 = server;
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server2);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
mockDelayMillis(20);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
handler.handle(invocation, (response) -> {
Assert.assertEquals("OK", response.getResult());
});
}
Aggregations