Search in sources :

Example 1 with HaDescriptor

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

the class HaDescriptorManager method load.

public static HaDescriptor load(InputStream inputStream) throws IOException {
    HaDescriptor descriptor = HaDescriptorFactory.createDescriptor();
    try {
        Document document = XmlUtils.readXml(inputStream);
        NodeList nodeList = document.getElementsByTagName(SERVICE_ELEMENT);
        if (nodeList != null && nodeList.getLength() > 0) {
            for (int i = 0; i < nodeList.getLength(); i++) {
                Element element = (Element) nodeList.item(i);
                HaServiceConfig config = HaDescriptorFactory.createServiceConfig(element.getAttribute(SERVICE_NAME_ATTRIBUTE), element.getAttribute(ENABLED_ATTRIBUTE), element.getAttribute(MAX_FAILOVER_ATTEMPTS), element.getAttribute(FAILOVER_SLEEP), element.getAttribute(MAX_RETRY_ATTEMPTS), element.getAttribute(RETRY_SLEEP), element.getAttribute(ZOOKEEPER_ENSEMBLE), element.getAttribute(ZOOKEEPER_NAMESPACE));
                descriptor.addServiceConfig(config);
            }
        }
    } catch (ParserConfigurationException e) {
        LOG.failedToLoadHaDescriptor(e);
        throw new IOException(e);
    } catch (SAXException e) {
        LOG.failedToLoadHaDescriptor(e);
        throw new IOException(e);
    }
    return descriptor;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) Document(org.w3c.dom.Document) HaServiceConfig(org.apache.knox.gateway.ha.provider.HaServiceConfig) SAXException(org.xml.sax.SAXException)

Example 2 with HaDescriptor

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

the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_SubsetProviderParams.

/**
 * Verify a mixture of provider-level params and service-level params.
 */
@Test
public void testServiceLevelParamOverrides_SubsetProviderParams() throws Exception {
    // Define some provider params
    Map<String, String> providerParams = new HashMap<>();
    // Specify all the possible params at the HaProvider level for TestRoleTwo
    providerParams.put("TestRoleOne", "enabled=false;" + "maxRetryAttempts=5;" + "maxFailoverAttempts=4;" + "failoverSleep=40");
    Provider haProvider = createHaProvider(providerParams);
    // Define the topology content (e.g., services)
    Collection<Service> topologyServices = new HashSet<>();
    // Specify all the possible params in the TestRoleOne service level
    Map<String, String> testRoleOneParams = new HashMap<>();
    testRoleOneParams.put("enabled", "true");
    testRoleOneParams.put("retrySleep", "60");
    testRoleOneParams.put("zookeeperNamespace", "testRoleOneOverride");
    testRoleOneParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
    // A service with all the params overriden
    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(testRoleOneParams).anyTimes();
    EasyMock.replay(testRoleOneService);
    topologyServices.add(testRoleOneService);
    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, haProvider);
    HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
    assertNotNull(descriptor);
    assertEquals(1, descriptor.getServiceConfigs().size());
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 40, 4, 60, 5, "testRoleOneOverride", "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 3 with HaDescriptor

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

the class HaProviderDeploymentContributorTest method testProviderLevelParams.

/**
 * Basically, a backward-compatibility test to ensure that HaProvider service params specified ONLY at the provider
 * level still work.
 */
@Test
public void testProviderLevelParams() throws Exception {
    // Define some provider params
    Map<String, String> providerParams = new HashMap<>();
    // Specify all the possible params at the HaProvider level for TestRoleTwo
    providerParams.put("TestRoleOne", "enabled=false;" + "maxRetryAttempts=5;" + "retrySleep=50;" + "maxFailoverAttempts=4;" + "failoverSleep=40;" + "zookeeperNamespace=testRoleOne;" + "zookeeperEnsemble=http://host1:2181,http://host2:2181");
    Provider haProvider = 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);
    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, haProvider);
    HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
    assertNotNull(descriptor);
    assertEquals(1, descriptor.getServiceConfigs().size());
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), false, 40, 4, 50, 5, "testRoleOne", "http://host1:2181,http://host2: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 4 with HaDescriptor

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

the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_NoProviderParams.

/**
 * Simple test verifying that HaProvider service params specified ONLY at the service level works.
 */
@Test
public void testServiceLevelParamOverrides_NoProviderParams() throws Exception {
    // Define some provider params
    Map<String, String> providerParams = new HashMap<>();
    // Specify all the possible params at the HaProvider level for TestRoleTwo
    providerParams.put("TestRoleOne", "");
    Provider haProvider = createHaProvider(providerParams);
    // Define the topology content (e.g., services)
    Collection<Service> topologyServices = new HashSet<>();
    // Specify all the possible params in the TestRoleOne service level
    Map<String, String> testRoleOneParams = new HashMap<>();
    testRoleOneParams.put("enabled", "true");
    testRoleOneParams.put("maxRetryAttempts", "6");
    testRoleOneParams.put("retrySleep", "60");
    testRoleOneParams.put("maxFailoverAttempts", "8");
    testRoleOneParams.put("failoverSleep", "80");
    testRoleOneParams.put("zookeeperNamespace", "testRoleOneOverride");
    testRoleOneParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
    // A service with all the params overriden
    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(testRoleOneParams).anyTimes();
    EasyMock.replay(testRoleOneService);
    topologyServices.add(testRoleOneService);
    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, haProvider);
    HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
    assertNotNull(descriptor);
    assertEquals(1, descriptor.getServiceConfigs().size());
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 80, 8, 60, 6, "testRoleOneOverride", "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 5 with HaDescriptor

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

the class DefaultHaProviderTest method testDescriptor.

@Test
public void testDescriptor() {
    try {
        new DefaultHaProvider(null);
        fail("provider construction should have failed with null descriptor");
    } catch (IllegalArgumentException e) {
    }
    HaDescriptor descriptor = new DefaultHaDescriptor();
    HaProvider provider = new DefaultHaProvider(descriptor);
    assertNotNull(provider.getHaDescriptor());
    descriptor.addServiceConfig(new DefaultHaServiceConfig("foo"));
    assertTrue(provider.isHaEnabled("foo"));
}
Also used : 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