Search in sources :

Example 11 with HaDescriptor

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

the class RMHaDispatchTest method testConnectivityFailure.

@Test
public void testConnectivityFailure() throws Exception {
    String serviceName = "RESOURCEMANAGER";
    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<>();
    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<ServletOutputStream>() {

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

                @Override
                public void write(int b) throws IOException {
                    throw new IOException("unreachable-host");
                }

                @Override
                public void setWriteListener(WriteListener arg0) {
                }

                @Override
                public boolean isReady() {
                    return false;
                }
            };
        }
    }).once();
    EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse);
    Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName));
    RMHaDispatch dispatch = new RMHaDispatch();
    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) ServletOutputStream(javax.servlet.ServletOutputStream) 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) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) BasicHttpParams(org.apache.http.params.BasicHttpParams) WriteListener(javax.servlet.WriteListener) DefaultHaProvider(org.apache.knox.gateway.ha.provider.impl.DefaultHaProvider) HaProvider(org.apache.knox.gateway.ha.provider.HaProvider) Test(org.junit.Test)

Example 12 with HaDescriptor

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

the class RMHaDispatchTest method testConnectivityFailover.

@Test
public void testConnectivityFailover() throws Exception {
    String serviceName = "RESOURCEMANAGER";
    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://passive-host");
    URI uri2 = new URI("http://other-host");
    URI uri3 = new URI("http://active-host");
    ArrayList<String> urlList = new ArrayList<>();
    urlList.add(uri1.toString());
    urlList.add(uri2.toString());
    urlList.add(uri3.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();
    BasicHttpResponse inboundResponse = EasyMock.createNiceMock(BasicHttpResponse.class);
    EasyMock.expect(inboundResponse.getStatusLine()).andReturn(getStatusLine()).anyTimes();
    EasyMock.expect(inboundResponse.getEntity()).andReturn(getResponseEntity()).anyTimes();
    EasyMock.expect(inboundResponse.getFirstHeader(LOCATION)).andReturn(getFirstHeader(uri3.toString())).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<ServletOutputStream>() {

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

                @Override
                public void write(int b) throws IOException {
                    throw new IOException("unreachable-host");
                }

                @Override
                public void setWriteListener(WriteListener arg0) {
                }

                @Override
                public boolean isReady() {
                    return false;
                }
            };
        }
    }).once();
    Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName));
    EasyMock.replay(filterConfig, servletContext, inboundResponse, outboundRequest, inboundRequest, outboundResponse);
    RMHaDispatch dispatch = new RMHaDispatch();
    HttpClientBuilder builder = HttpClientBuilder.create();
    CloseableHttpClient client = builder.build();
    dispatch.setHttpClient(client);
    dispatch.setHaProvider(provider);
    dispatch.init();
    long startTime = System.currentTimeMillis();
    try {
        dispatch.setInboundResponse(inboundResponse);
        dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
    } catch (IOException e) {
    // this is expected after the failover limit is reached
    }
    Assert.assertEquals(uri3.toString(), dispatch.getUriFromInbound(inboundRequest, inboundResponse, null).toString());
    long elapsedTime = System.currentTimeMillis() - startTime;
    Assert.assertEquals(uri3.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) ServletOutputStream(javax.servlet.ServletOutputStream) 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) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) BasicHttpParams(org.apache.http.params.BasicHttpParams) WriteListener(javax.servlet.WriteListener) DefaultHaProvider(org.apache.knox.gateway.ha.provider.impl.DefaultHaProvider) HaProvider(org.apache.knox.gateway.ha.provider.HaProvider) Test(org.junit.Test)

Example 13 with HaDescriptor

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

the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_MultipleMixed.

@Test
public void testServiceLevelParamOverrides_MultipleMixed() throws Exception {
    // Define some provider params
    Map<String, String> providerParams = new HashMap<>();
    // Specify a subset of the possible HaProvider-level params for TestRoleOne
    providerParams.put("TestRoleOne", "enabled=true;maxRetryAttempts=1;retrySleep=10;maxFailoverAttempts=2;failoverSleep=20");
    // Specify all the possible params at the HaProvider level for TestRoleTwo
    providerParams.put("TestRoleTwo", "enabled=false;" + "maxRetryAttempts=3;" + "retrySleep=30;" + "maxFailoverAttempts=4;" + "failoverSleep=40;" + "zookeeperNamespace=testRoleTwo;" + "zookeeperEnsemble=http://host1:2181,http://host2:2181");
    Provider testHaProvider = createHaProvider(providerParams);
    // Define the topology content (e.g., services)
    Collection<Service> topologyServices = new HashSet<>();
    // A service with no param overrides
    Service testRoleOneService = EasyMock.createNiceMock(Service.class);
    EasyMock.expect(testRoleOneService.getRole()).andReturn("TestRoleOne").anyTimes();
    EasyMock.expect(testRoleOneService.getName()).andReturn("TestRoleOneService").anyTimes();
    EasyMock.expect(testRoleOneService.getParams()).andReturn(Collections.emptyMap()).anyTimes();
    EasyMock.replay(testRoleOneService);
    topologyServices.add(testRoleOneService);
    // Override all the possible params in the TestRoleTwo service level
    Map<String, String> testRoleTwoParams = new HashMap<>();
    testRoleTwoParams.put("enabled", "true");
    testRoleTwoParams.put("maxRetryAttempts", "6");
    testRoleTwoParams.put("retrySleep", "60");
    testRoleTwoParams.put("maxFailoverAttempts", "8");
    testRoleTwoParams.put("failoverSleep", "80");
    testRoleTwoParams.put("zookeeperNamespace", "testRoleTwoOverride");
    testRoleTwoParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
    Service testRoleTwoService = EasyMock.createNiceMock(Service.class);
    EasyMock.expect(testRoleTwoService.getRole()).andReturn("TestRoleTwo").anyTimes();
    EasyMock.expect(testRoleTwoService.getName()).andReturn("TestRoleTwoService").anyTimes();
    EasyMock.expect(testRoleTwoService.getParams()).andReturn(testRoleTwoParams).anyTimes();
    EasyMock.replay(testRoleTwoService);
    topologyServices.add(testRoleTwoService);
    Topology topology = EasyMock.createNiceMock(Topology.class);
    EasyMock.expect(topology.getServices()).andReturn(topologyServices).anyTimes();
    EasyMock.replay(topology);
    WebArchive war = EasyMock.createNiceMock(WebArchive.class);
    EasyMock.replay(war);
    DeploymentContext context = new DescriptorCaptureDeploymentContext(topology, war);
    // Invoke the contributor
    HaProviderDeploymentContributor haPDC = new HaProviderDeploymentContributor();
    haPDC.contributeProvider(context, testHaProvider);
    HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
    assertNotNull(descriptor);
    assertEquals(2, descriptor.getServiceConfigs().size());
    // Validate the service with no-overrides, checking that the provider-level defaults are applied
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 20, 2, 10, 1, null, null);
    // Validate the service with all-overrides, checking that the service-level defaults are applied
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleTwo"), true, 80, 8, 60, 6, "testRoleTwoOverride", "http://host3:2181,http://host4:2181");
}
Also used : HashMap(java.util.HashMap) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Provider(org.apache.knox.gateway.topology.Provider) DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with HaDescriptor

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

the class DefaultHaDispatchTest method testConnectivityFailover.

@Test
public void testConnectivityFailover() throws Exception {
    String serviceName = "OOZIE";
    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));
    DefaultHaDispatch dispatch = new DefaultHaDispatch();
    HttpClientBuilder builder = HttpClientBuilder.create();
    CloseableHttpClient client = builder.build();
    dispatch.setHttpClient(client);
    dispatch.setHaProvider(provider);
    dispatch.setServiceRole(serviceName);
    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 15 with HaDescriptor

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

the class DefaultHaProviderTest method testActiveUrl.

@Test
public void testActiveUrl() {
    HaDescriptor descriptor = new DefaultHaDescriptor();
    HaProvider provider = new DefaultHaProvider(descriptor);
    ArrayList<String> urls = new ArrayList<String>();
    String url1 = "http://host1";
    urls.add(url1);
    String url2 = "http://host2";
    urls.add(url2);
    String url3 = "http://host3";
    urls.add(url3);
    String serviceName = "foo";
    provider.addHaService(serviceName, urls);
    assertEquals(url1, provider.getActiveURL(serviceName));
    provider.markFailedURL(serviceName, url1);
    assertEquals(url2, provider.getActiveURL(serviceName));
    provider.markFailedURL(serviceName, url2);
    assertEquals(url3, provider.getActiveURL(serviceName));
    provider.markFailedURL(serviceName, url3);
    assertEquals(url1, provider.getActiveURL(serviceName));
    provider.setActiveURL(serviceName, url3);
    assertEquals(url3, provider.getActiveURL(serviceName));
    provider.setActiveURL(serviceName, url2);
    assertEquals(url2, provider.getActiveURL(serviceName));
}
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)

Aggregations

HaDescriptor (org.apache.knox.gateway.ha.provider.HaDescriptor)16 Test (org.junit.Test)14 HaProvider (org.apache.knox.gateway.ha.provider.HaProvider)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Service (org.apache.knox.gateway.topology.Service)5 Topology (org.apache.knox.gateway.topology.Topology)5 URI (java.net.URI)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)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 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)4