Search in sources :

Example 6 with HaProvider

use of org.apache.knox.gateway.ha.provider.HaProvider in project knox by apache.

the class DefaultHaProviderTest method testAddingService.

@Test
public void testAddingService() {
    HaDescriptor descriptor = new DefaultHaDescriptor();
    HaProvider provider = new DefaultHaProvider(descriptor);
    ArrayList<String> urls = new ArrayList<String>();
    urls.add("http://host1");
    urls.add("http://host2");
    provider.addHaService("foo", urls);
    assertNull(provider.getActiveURL("bar"));
    String url = provider.getActiveURL("foo");
    assertNotNull(url);
    assertThat(url, isIn(urls));
}
Also used : ArrayList(java.util.ArrayList) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) HaProvider(org.apache.knox.gateway.ha.provider.HaProvider) Test(org.junit.Test)

Example 7 with HaProvider

use of org.apache.knox.gateway.ha.provider.HaProvider in project knox by apache.

the class WebHdfsHaDispatchTest method testConnectivityFailover.

@Test
public void testConnectivityFailover() throws Exception {
    String serviceName = "WEBHDFS";
    HaDescriptor descriptor = HaDescriptorFactory.createDescriptor();
    descriptor.addServiceConfig(HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000", null, null));
    HaProvider provider = new DefaultHaProvider(descriptor);
    URI uri1 = new URI("http://unreachable-host");
    URI uri2 = new URI("http://reachable-host");
    ArrayList<String> urlList = new ArrayList<String>();
    urlList.add(uri1.toString());
    urlList.add(uri2.toString());
    provider.addHaService(serviceName, urlList);
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)).andReturn(provider).anyTimes();
    BasicHttpParams params = new BasicHttpParams();
    HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpRequestBase.class);
    EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes();
    EasyMock.expect(outboundRequest.getURI()).andReturn(uri1).anyTimes();
    EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes();
    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once();
    EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0)).once();
    EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1)).once();
    HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<SynchronousServletOutputStreamAdapter>() {

        @Override
        public SynchronousServletOutputStreamAdapter answer() throws Throwable {
            return new SynchronousServletOutputStreamAdapter() {

                @Override
                public void write(int b) throws IOException {
                    throw new IOException("unreachable-host");
                }
            };
        }
    }).once();
    EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse);
    Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName));
    WebHdfsHaDispatch dispatch = new WebHdfsHaDispatch();
    HttpClientBuilder builder = HttpClientBuilder.create();
    CloseableHttpClient client = builder.build();
    dispatch.setHttpClient(client);
    dispatch.setHaProvider(provider);
    dispatch.init();
    long startTime = System.currentTimeMillis();
    try {
        dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
    } catch (IOException e) {
    // this is expected after the failover limit is reached
    }
    long elapsedTime = System.currentTimeMillis() - startTime;
    Assert.assertEquals(uri2.toString(), provider.getActiveURL(serviceName));
    // test to make sure the sleep took place
    Assert.assertTrue(elapsedTime > 1000);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) DefaultHaProvider(org.apache.knox.gateway.ha.provider.impl.DefaultHaProvider) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URI(java.net.URI) HttpServletRequest(javax.servlet.http.HttpServletRequest) IAnswer(org.easymock.IAnswer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) SynchronousServletOutputStreamAdapter(org.apache.knox.gateway.servlet.SynchronousServletOutputStreamAdapter) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHaProvider(org.apache.knox.gateway.ha.provider.impl.DefaultHaProvider) HaProvider(org.apache.knox.gateway.ha.provider.HaProvider) Test(org.junit.Test)

Example 8 with HaProvider

use of org.apache.knox.gateway.ha.provider.HaProvider in project knox by apache.

the class ServiceHostFunctionProcessorTest method setUp.

@Before
public void setUp() {
    reg = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(reg.lookupServiceURL("test-cluster", "test-service")).andReturn("test-scheme://test-host:777/test-path").anyTimes();
    svc = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(svc.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(reg).anyTimes();
    env = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(env.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(svc).anyTimes();
    EasyMock.expect(env.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    ctx = EasyMock.createNiceMock(UrlRewriteContext.class);
    desc = EasyMock.createNiceMock(ServiceHostFunctionDescriptor.class);
    HaProvider haProvider = EasyMock.createNiceMock(HaProvider.class);
    EasyMock.expect(env.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)).andReturn(haProvider).anyTimes();
    EasyMock.expect(haProvider.isHaEnabled(EasyMock.anyObject(String.class))).andReturn(Boolean.FALSE).anyTimes();
    EasyMock.replay(reg, svc, env, desc, ctx, haProvider);
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) ServiceHostFunctionDescriptor(org.apache.knox.gateway.svcregfunc.api.ServiceHostFunctionDescriptor) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) HaProvider(org.apache.knox.gateway.ha.provider.HaProvider) Before(org.junit.Before)

Example 9 with HaProvider

use of org.apache.knox.gateway.ha.provider.HaProvider in project knox by apache.

the class ServiceMappedHostFunctionProcessorTest method setUp.

@Before
public void setUp() {
    hm = EasyMock.createNiceMock(HostMapper.class);
    EasyMock.expect(hm.resolveInboundHostName("test-host")).andReturn("test-internal-host").anyTimes();
    hms = EasyMock.createNiceMock(HostMapperService.class);
    EasyMock.expect(hms.getHostMapper("test-cluster")).andReturn(hm).anyTimes();
    reg = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(reg.lookupServiceURL("test-cluster", "test-service")).andReturn("test-scheme://test-host:777/test-path").anyTimes();
    svc = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(svc.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(reg).anyTimes();
    EasyMock.expect(svc.getService(GatewayServices.HOST_MAPPING_SERVICE)).andReturn(hms).anyTimes();
    env = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(env.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(svc).anyTimes();
    EasyMock.expect(env.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    ctx = EasyMock.createNiceMock(UrlRewriteContext.class);
    EasyMock.expect(ctx.getDirection()).andReturn(UrlRewriter.Direction.IN).anyTimes();
    desc = EasyMock.createNiceMock(ServiceMappedHostFunctionDescriptor.class);
    HaProvider haProvider = EasyMock.createNiceMock(HaProvider.class);
    EasyMock.expect(env.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)).andReturn(haProvider).anyTimes();
    EasyMock.expect(haProvider.isHaEnabled(EasyMock.anyObject(String.class))).andReturn(Boolean.FALSE).anyTimes();
    EasyMock.replay(hm, hms, reg, svc, env, desc, ctx, haProvider);
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) HostMapper(org.apache.knox.gateway.services.hostmap.HostMapper) ServiceMappedHostFunctionDescriptor(org.apache.knox.gateway.svcregfunc.api.ServiceMappedHostFunctionDescriptor) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) HostMapperService(org.apache.knox.gateway.services.hostmap.HostMapperService) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) HaProvider(org.apache.knox.gateway.ha.provider.HaProvider) Before(org.junit.Before)

Example 10 with HaProvider

use of org.apache.knox.gateway.ha.provider.HaProvider in project knox by apache.

the class ServicePathFunctionProcessorTest method setUp.

@Before
public void setUp() {
    reg = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(reg.lookupServiceURL("test-cluster", "test-service")).andReturn("test-scheme://test-host:777/test-path").anyTimes();
    svc = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(svc.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(reg).anyTimes();
    env = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(env.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(svc).anyTimes();
    EasyMock.expect(env.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    ctx = EasyMock.createNiceMock(UrlRewriteContext.class);
    desc = EasyMock.createNiceMock(ServicePathFunctionDescriptor.class);
    HaProvider haProvider = EasyMock.createNiceMock(HaProvider.class);
    EasyMock.expect(env.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)).andReturn(haProvider).anyTimes();
    EasyMock.expect(haProvider.isHaEnabled(EasyMock.anyObject(String.class))).andReturn(Boolean.FALSE).anyTimes();
    EasyMock.replay(reg, svc, env, desc, ctx, haProvider);
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) ServicePathFunctionDescriptor(org.apache.knox.gateway.svcregfunc.api.ServicePathFunctionDescriptor) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) HaProvider(org.apache.knox.gateway.ha.provider.HaProvider) Before(org.junit.Before)

Aggregations

HaProvider (org.apache.knox.gateway.ha.provider.HaProvider)16 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)9 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)9 GatewayServices (org.apache.knox.gateway.services.GatewayServices)9 ServiceRegistry (org.apache.knox.gateway.services.registry.ServiceRegistry)9 Before (org.junit.Before)9 HaDescriptor (org.apache.knox.gateway.ha.provider.HaDescriptor)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)4 URI (java.net.URI)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 FilterConfig (javax.servlet.FilterConfig)4 ServletContext (javax.servlet.ServletContext)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)4 BasicHttpParams (org.apache.http.params.BasicHttpParams)4