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;
}
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");
}
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");
}
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");
}
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"));
}
Aggregations