Skip to content

Commit

Permalink
KNOX-2533 - Qualifying service params for discovery improvements (apa…
Browse files Browse the repository at this point in the history
  • Loading branch information
pzampino authored Jan 26, 2021
1 parent 110f415 commit 85d8236
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.knox.gateway.topology.discovery.cm;

import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.topology.discovery.ServiceDiscovery;
import org.apache.knox.gateway.topology.discovery.cm.collector.ServiceURLCollectors;

Expand All @@ -30,6 +31,9 @@
*/
public class ClouderaManagerCluster implements ServiceDiscovery.Cluster {

private static final ClouderaManagerServiceDiscoveryMessages log =
MessagesFactory.get(ClouderaManagerServiceDiscoveryMessages.class);

private String name;

private Map<String, List<ServiceModel>> serviceModels = new HashMap<>();
Expand Down Expand Up @@ -60,8 +64,17 @@ public List<String> getServiceURLs(String serviceName, Map<String, String> servi
boolean isMatchingModel = true;
if (serviceParams != null) {
for (Map.Entry<String, String> serviceParam : serviceParams.entrySet()) {
if (!serviceParam.getValue().equals(model.getQualifyingServiceParam(serviceParam.getKey()))) {
isMatchingModel = false;
String serviceParamKey = serviceParam.getKey();
// If it's a qualifying service param, then perform the qualification check
if (serviceParamKey.startsWith(ServiceModel.QUALIFYING_SERVICE_PARAM_PREFIX)) {
if (!serviceParam.getValue().equals(model.getQualifyingServiceParam(serviceParamKey))) {
isMatchingModel = false;
log.qualifyingServiceParamMismatch(serviceName,
serviceParamKey,
serviceParam.getValue(),
model.getQualifyingServiceParam(serviceParamKey));
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ void failedToInstantiateJAASConfigurationFileImplementation(String implementatio
text = "No password configured for Cloudera Manager service discovery.")
void aliasServicePasswordNotFound();

@Message(level = MessageLevel.INFO,
text = "The value of the qualifying parameter {1} for service {0} does not match: Expected={2}, Actual={3}")
void qualifyingServiceParamMismatch(String serviceName, String paramName, String expectedValue, String actualValue);

@Message(level = MessageLevel.ERROR,
text = "Unable to access the ClouderaManager Configuration Change Monitor: {0}")
void errorAccessingConfigurationChangeMonitor(@StackTrace(level = MessageLevel.DEBUG) Exception e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.knox.gateway.topology.discovery.cm;

import org.apache.knox.gateway.topology.simple.SimpleDescriptor;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -29,6 +31,8 @@ public class ServiceModel {

public enum Type {API, UI}

public static final String QUALIFYING_SERVICE_PARAM_PREFIX = SimpleDescriptor.DISCOVERY_PARAM_PREFIX;

private static final String NULL_VALUE = "null";

private final Type type;
Expand Down Expand Up @@ -66,7 +70,9 @@ public ServiceModel(final Type type,
}

public void addQualifyingServiceParam(final String name, final String value) {
qualifyingServiceParams.put(name, (value != null ? value : NULL_VALUE));
// Fix the name if it doesn't include the prefix
String paramName = name.startsWith(QUALIFYING_SERVICE_PARAM_PREFIX) ? name : QUALIFYING_SERVICE_PARAM_PREFIX + name;
qualifyingServiceParams.put(paramName, (value != null ? value : NULL_VALUE));
}

public void addServiceProperty(final String name, final String value) {
Expand All @@ -88,7 +94,8 @@ public Map<String, String> getQualifyingServiceParams() {
* @return The value of the metadata property associated with the model, which can be used to qualify service discovery.
*/
public String getQualifyingServiceParam(final String name) {
return qualifyingServiceParams.get(name);
String paramName = name.startsWith(QUALIFYING_SERVICE_PARAM_PREFIX) ? name : QUALIFYING_SERVICE_PARAM_PREFIX + name;
return qualifyingServiceParams.get(paramName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class NameNodeServiceModelGenerator extends AbstractServiceModelGenerator
public static final String SERVICE_TYPE = "HDFS";
public static final String ROLE_TYPE = "NAMENODE";

static final String DISCOVERY_NAMESERVICE = "discovery-nameservice";
static final String DISCOVERY_NAMESERVICE = "nameservice";

static final String AUTOFAILOVER_ENABLED = "autofailover_enabled";
static final String NN_NAMESERVICE = "dfs_federation_namenode_nameservice";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class SolrServiceModelGenerator extends AbstractServiceModelGenerator {
public static final String SERVICE_TYPE = "SOLR";
public static final String ROLE_TYPE = "SOLR_SERVER";

static final String DISCOVERY_SERVICE_NAME = "discovery-service-name";
static final String DISCOVERY_SERVICE_DISPLAY_NAME = "discovery-service-display-name";
public static final String DISCOVERY_SERVICE_NAME = "service-name";
public static final String DISCOVERY_SERVICE_DISPLAY_NAME = "service-display-name";

static final String USE_SSL = "solr_use_ssl";
static final String HTTP_PORT = "solr_http_port";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with this
* * work for additional information regarding copyright ownership. The ASF
* * licenses this file to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations under
* * the License.
*
*/
package org.apache.knox.gateway.topology.discovery.cm;

import org.apache.knox.gateway.topology.discovery.cm.model.solr.SolrServiceModelGenerator;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.apache.knox.gateway.topology.discovery.cm.ServiceModel.QUALIFYING_SERVICE_PARAM_PREFIX;
import static org.apache.knox.gateway.topology.discovery.cm.model.solr.SolrServiceModelGenerator.DISCOVERY_SERVICE_DISPLAY_NAME;
import static org.apache.knox.gateway.topology.discovery.cm.model.solr.SolrServiceModelGenerator.DISCOVERY_SERVICE_NAME;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ClouderaManagerClusterTest {

/**
* This is the default case, with no service parameters declared for the SOLR service.
*/
@Test
public void testGetUnqualifiedMultipleSOLRServiceURLs_NoServiceParams() {
doTestGetSOLRServiceURLs(Arrays.asList("SOLR-1", "SOLR-2"),
Collections.emptyMap(),
Arrays.asList("SOLR-1", "SOLR-2"));
}

/**
* Ensure that declared service parameters, which are NOT qualifying service parameters, do not affect the default
* discovery.
*/
@Test
public void testGetUnqualifiedMultipleSOLRServiceURLs_NoDiscoveryServiceParams() {
doTestGetSOLRServiceURLs(Arrays.asList("SOLR-3", "SOLR-7"),
Collections.singletonMap("test-param", "test-value"),
Arrays.asList("SOLR-3", "SOLR-7"));
}

/**
* Ensure that an incorrectly-specified SOLR service display name qualifying parameter results in no matching
* services, and consequently with no service URLs.
*/
@Test
public void testGetIncorrectlyQualifiedSOLRServiceURLs() {
doTestGetSOLRServiceURLs(Arrays.asList("SOLR-3", "SOLR-7"),
Collections.singletonMap(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_DISPLAY_NAME, "SOLR-5Display"),
Collections.emptyList());
}

/**
* Ensure that a correctly-specified SOLR service display name qualifying parameter is honored.
*/
@Test
public void testGetDisplayNameQualifiedSOLRServiceURLs() {
doTestGetSOLRServiceURLs(Arrays.asList("SOLR-3", "SOLR-7"),
Collections.singletonMap(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_DISPLAY_NAME, "SOLR-7Display"),
Collections.singletonList("SOLR-7"));
}

/**
* Ensure that a correctly-specified SOLR service name qualifying parameter is honored.
*/
@Test
public void testGetServiceNameQualifiedSOLRServiceURLs() {
doTestGetSOLRServiceURLs(Arrays.asList("SOLR-5", "SOLR-6"),
Collections.singletonMap(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_NAME, "SOLR-5"),
Collections.singletonList("SOLR-5"));
}

/**
* Ensure that a correctly-specified SOLR service name and display name qualifying parameters are honored.
*/
@Test
public void testGetServiceAndDisplayNameQualifiedSOLRServiceURLs() {
Map<String, String> serviceParams = new HashMap<>();
serviceParams.put(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_NAME, "SOLR-6");
serviceParams.put(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_DISPLAY_NAME, "SOLR-6Display");
doTestGetSOLRServiceURLs(Arrays.asList("SOLR-5", "SOLR-6"),
serviceParams,
Collections.singletonList("SOLR-6"));
}

/**
* Ensure that an incorrectly-specified service name qualifying parameter, even with a correctly-specified SOLR
* display name qualifying parameter, results in no matching services.
*/
@Test
public void testGetServiceAndDisplayNameQualifiedSOLRServiceURLs_IncorrectServiceName() {
Map<String, String> serviceParams = new HashMap<>();
serviceParams.put(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_NAME, "SOLR-3");
serviceParams.put(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_DISPLAY_NAME, "SOLR-6Display");
doTestGetSOLRServiceURLs(Arrays.asList("SOLR-5", "SOLR-6"),
serviceParams,
Collections.emptyList());
}

/**
* Ensure that an incorrectly-specified service display name qualifying parameter, even with a correctly-specified
* SOLR service name qualifying parameter, results in no matching services.
*/
@Test
public void testGetServiceAndDisplayNameQualifiedSOLRServiceURLs_IncorrectDisplayName() {
Map<String, String> serviceParams = new HashMap<>();
serviceParams.put(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_NAME, "SOLR-6");
serviceParams.put(QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_DISPLAY_NAME, "SOLR-8Display");
doTestGetSOLRServiceURLs(Arrays.asList("SOLR-5", "SOLR-6"),
serviceParams,
Collections.emptyList());
}

/**
* Test the SOLR service URL discovery based on the specified parameters.
*
* @param serviceNames The name(s) of the SOLR service(s) in the cluster.
* @param serviceParams Any service parameter declarations (i.e., from the supposed descriptor)
* @param serviceNamesToExpectURLs The service(s) for which URLs are expected.
*/
private void doTestGetSOLRServiceURLs(final List<String> serviceNames,
final Map<String, String> serviceParams,
final List<String> serviceNamesToExpectURLs) {
final Set<ServiceModel> testSolrServiceModels = new HashSet<>();
final List<String> expectedURLs = new ArrayList<>();

for (String serviceName : serviceNames) {
ServiceModel model = createSOLRServiceModel("http://" + serviceName + "-host:1234/solr");
model.addQualifyingServiceParam(DISCOVERY_SERVICE_NAME, serviceName);
model.addQualifyingServiceParam(DISCOVERY_SERVICE_DISPLAY_NAME, serviceName + "Display");
testSolrServiceModels.add(model);
if (serviceNamesToExpectURLs.contains(serviceName)) {
expectedURLs.add(model.getServiceUrl());
}
}

final ClouderaManagerCluster cluster = new ClouderaManagerCluster("test");
cluster.addServiceModels(testSolrServiceModels);

List<String> solrURLs = cluster.getServiceURLs("SOLR", serviceParams);
assertEquals("Unexpected URL count.", expectedURLs.size(), solrURLs.size());
for (String expectedURL : expectedURLs) {
assertTrue("Missing expected URL: " + expectedURL, solrURLs.contains(expectedURL));
}
}

private static ServiceModel createSOLRServiceModel(final String url) {
return new ServiceModel(ServiceModel.Type.API,
SolrServiceModelGenerator.SERVICE,
SolrServiceModelGenerator.SERVICE_TYPE,
SolrServiceModelGenerator.ROLE_TYPE,
url);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ public void testServiceModelMetadataWithNameService() {
validateServiceModel(generated, serviceConfig, roleConfig);

// Validate model metadata properties
final String qualifyingProperty =
ServiceModel.QUALIFYING_SERVICE_PARAM_PREFIX + NameNodeServiceModelGenerator.DISCOVERY_NAMESERVICE;
Map<String, String> modelProps = generated.getQualifyingServiceParams();
assertEquals("Expected one service model properties", 1, modelProps.size());
assertEquals("Expected " + NameNodeServiceModelGenerator.DISCOVERY_NAMESERVICE + " model property.",
assertEquals("Expected " + qualifyingProperty + " model property.",
roleConfig.get(NameNodeServiceModelGenerator.NN_NAMESERVICE),
modelProps.get(NameNodeServiceModelGenerator.DISCOVERY_NAMESERVICE));
modelProps.get(qualifyingProperty));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ public void testServiceModelMetadataWithNameService() {
validateServiceModel(generated, serviceConfig, roleConfig, false);

// Validate model metadata properties
final String qualifyingProperty =
ServiceModel.QUALIFYING_SERVICE_PARAM_PREFIX + WebHdfsServiceModelGenerator.DISCOVERY_NAMESERVICE;
Map<String, String> modelProps = generated.getQualifyingServiceParams();
assertEquals("Expected one service model properties", 1, modelProps.size());
assertEquals("Expected " + NameNodeServiceModelGenerator.DISCOVERY_NAMESERVICE + " model property.",
assertEquals("Expected " + qualifyingProperty + " model property.",
roleConfig.get(WebHdfsServiceModelGenerator.NN_NAMESERVICE),
modelProps.get(WebHdfsServiceModelGenerator.DISCOVERY_NAMESERVICE));
modelProps.get(qualifyingProperty));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import java.util.HashMap;
import java.util.Map;

import static org.apache.knox.gateway.topology.discovery.cm.ServiceModel.QUALIFYING_SERVICE_PARAM_PREFIX;
import static org.apache.knox.gateway.topology.discovery.cm.model.solr.SolrServiceModelGenerator.DISCOVERY_SERVICE_DISPLAY_NAME;
import static org.apache.knox.gateway.topology.discovery.cm.model.solr.SolrServiceModelGenerator.DISCOVERY_SERVICE_NAME;
import static org.junit.Assert.assertEquals;

public class SolrServiceModelGeneratorTest extends AbstractServiceModelGeneratorTest {
Expand All @@ -40,14 +43,16 @@ public void testServiceModelMetadata() {
validateServiceModel(generated, serviceConfig, roleConfig);

// Validate model metadata properties
final String serviceNameQualifier = QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_NAME;
final String displayNameQualifier = QUALIFYING_SERVICE_PARAM_PREFIX + DISCOVERY_SERVICE_DISPLAY_NAME;
Map<String, String> modelProps = generated.getQualifyingServiceParams();
assertEquals("Expected two service model properties", 2, modelProps.size());
assertEquals("Expected " + SolrServiceModelGenerator.DISCOVERY_SERVICE_NAME + " model property.",
getServiceType() + "-1",
modelProps.get(SolrServiceModelGenerator.DISCOVERY_SERVICE_NAME));
assertEquals("Expected " + SolrServiceModelGenerator.DISCOVERY_SERVICE_DISPLAY_NAME + " model property.",
"null",
modelProps.get(SolrServiceModelGenerator.DISCOVERY_SERVICE_DISPLAY_NAME));
assertEquals("Expected " + serviceNameQualifier + " model property.",
getServiceType() + "-1",
modelProps.get(serviceNameQualifier));
assertEquals("Expected " + displayNameQualifier + " model property.",
"null",
modelProps.get(displayNameQualifier));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

public interface SimpleDescriptor {

String DISCOVERY_PARAM_PREFIX = "discovery-";

String getName();

String getDiscoveryType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ public class SimpleDescriptorHandler {

private static final SimpleDescriptorMessages log = MessagesFactory.get(SimpleDescriptorMessages.class);

private static final String DISCOVERY_PARAM_PREFIX = "discovery-";

private static Map<String, ServiceDiscovery> discoveryInstances = new HashMap<>();
private static final Map<String, ServiceDiscovery> discoveryInstances = new HashMap<>();

private static final Set<String> ALLOWED_SERVICES_WITHOUT_URLS_AND_PARAMS = Collections.unmodifiableSet(Stream.of("KNOX", "KNOX-METADATA", "KNOXSSOUT", "KNOX-SESSION").collect(Collectors.toSet()));

Expand Down Expand Up @@ -162,7 +160,7 @@ public static Map<String, File> handle(GatewayConfig config, SimpleDescriptor de
boolean hasNonDiscoveryParams = false;
// Determine if there are any params which are not discovery-only
for (String paramName : descriptorServiceParams.keySet()) {
if (!paramName.startsWith(DISCOVERY_PARAM_PREFIX)) {
if (!paramName.startsWith(SimpleDescriptor.DISCOVERY_PARAM_PREFIX)) {
hasNonDiscoveryParams = true;
break;
}
Expand Down Expand Up @@ -556,7 +554,7 @@ private static Map<String, File> generateTopology(final SimpleDescriptor desc,
Map<String, String> svcParams = serviceParams.get(serviceName);
if (svcParams != null) {
for (Entry<String, String> svcParam : svcParams.entrySet()) {
if (!(svcParam.getKey().toLowerCase(Locale.ROOT)).startsWith(DISCOVERY_PARAM_PREFIX)) {
if (!(svcParam.getKey().toLowerCase(Locale.ROOT)).startsWith(SimpleDescriptor.DISCOVERY_PARAM_PREFIX)) {
sw.write(" <param>\n");
sw.write(" <name>" + svcParam.getKey() + "</name>\n");
sw.write(" <value>" + svcParam.getValue() + "</value>\n");
Expand Down

0 comments on commit 85d8236

Please sign in to comment.