Skip to content

Commit

Permalink
adapt for situations when no appid is provided in client side
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Aug 1, 2016
1 parent 0a5424e commit 28929a3
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import com.ctrip.framework.apollo.core.MetaDomainConsts;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.enums.EnvUtils;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.foundation.Foundation;
import com.dianping.cat.Cat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Named;
import org.unidal.net.Networks;

import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -41,12 +42,15 @@ public ConfigUtil() {
/**
* Get the app id for the current application.
*
* @return the app id
* @throws IllegalStateException if app id is not set
* @return the app id or ConfigConsts.NO_APPID_PLACEHOLDER if app id is not available
*/
public String getAppId() {
String appId = Foundation.app().getAppId();
Preconditions.checkState(appId != null, "app.id is not set");
if (Strings.isNullOrEmpty(appId)) {
appId = ConfigConsts.NO_APPID_PLACEHOLDER;
logger.error("app.id is not set, apollo will only load public namespace configurations!");
Cat.logError(new ApolloConfigException("app.id is not set"));
}
return appId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,20 @@ public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String
@RequestParam(value = "ip", required = false) String clientIp,
HttpServletResponse response) throws IOException {
String originalNamespace = namespace;

//strip out .properties suffix
namespace = namespaceUtil.filterNamespaceName(namespace);

List<Release> releases = Lists.newLinkedList();

Release currentAppRelease = loadConfig(appId, clusterName, namespace, dataCenter);
String appClusterNameLoaded = clusterName;
if (!ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
Release currentAppRelease = loadConfig(appId, clusterName, namespace, dataCenter);

if (currentAppRelease != null) {
releases.add(currentAppRelease);
//we have cluster search process, so the cluster name might be overridden
appClusterNameLoaded = currentAppRelease.getClusterName();
if (currentAppRelease != null) {
releases.add(currentAppRelease);
//we have cluster search process, so the cluster name might be overridden
appClusterNameLoaded = currentAppRelease.getClusterName();
}
}

//if namespace does not belong to this appId, should check if there is a public configuration
Expand Down Expand Up @@ -116,6 +117,11 @@ private boolean namespaceBelongsToAppId(String appId, String namespaceName) {
return true;
}

//if no appId is present, then no other namespace belongs to it
if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return false;
}

AppNamespace appNamespace = appNamespaceService.findOne(appId, namespaceName);

return appNamespace != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Set<String> assembleAllWatchKeys(String appId, String clusterName, String

/**
* Assemble watch keys for the given appId, cluster, namespaces, dataCenter combination
*
* @return a multimap with namespace as the key and watch keys as the value
*/
public Multimap<String, String> assembleAllWatchKeys(String appId, String clusterName,
Expand Down Expand Up @@ -91,7 +92,9 @@ private String assembleKey(String appId, String cluster, String namespace) {

private Set<String> assembleWatchKeys(String appId, String clusterName, String namespace,
String dataCenter) {

if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return Collections.emptySet();
}
Set<String> watchedKeys = Sets.newHashSet();

//watch specified cluster config change
Expand Down Expand Up @@ -124,6 +127,9 @@ private Multimap<String, String> assembleWatchKeys(String appId, String clusterN
}

private Set<String> namespacesBelongToAppId(String appId, Set<String> namespaces) {
if (ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
return Collections.emptySet();
}
List<AppNamespace> appNamespaces =
appNamespaceService.findByAppIdAndNamespaces(appId, namespaces);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -283,8 +284,8 @@ public void testQueryConfigWithPubicNamespaceAndNoAppOverride() throws Exception
when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey);

ApolloConfig result = configController
.queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter,
someClientSideReleaseKey, someClientIp, someResponse);
.queryConfig(someAppId, someClusterName, somePublicNamespaceName, someDataCenter,
someClientSideReleaseKey, someClientIp, someResponse);

assertEquals(someServerSideReleaseKey, result.getReleaseKey());
assertEquals(someAppId, result.getAppId());
Expand Down Expand Up @@ -431,6 +432,50 @@ public void testTransformConfigurationToMapFailed() throws Exception {
configController.mergeReleaseConfigurations(Lists.newArrayList(someRelease));
}

@Test
public void testQueryConfigForNoAppIdPlaceHolder() throws Exception {
String someClientSideReleaseKey = "1";
HttpServletResponse someResponse = mock(HttpServletResponse.class);
String appId = ConfigConsts.NO_APPID_PLACEHOLDER;

ApolloConfig result = configController.queryConfig(appId, someClusterName,
defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
someClientIp, someResponse);

verify(releaseService, never()).findLatestActiveRelease(appId, someClusterName, defaultNamespaceName);
verify(appNamespaceService, never()).findPublicNamespaceByName(defaultNamespaceName);
assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
}

@Test
public void testQueryConfigForNoAppIdPlaceHolderWithPublicNamespace() throws Exception {
String someClientSideReleaseKey = "1";
String someServerSideReleaseKey = "2";
HttpServletResponse someResponse = mock(HttpServletResponse.class);
String somePublicAppId = "somePublicAppId";
AppNamespace somePublicAppNamespace =
assemblePublicAppNamespace(somePublicAppId, somePublicNamespaceName);
String appId = ConfigConsts.NO_APPID_PLACEHOLDER;

when(appNamespaceService.findPublicNamespaceByName(somePublicNamespaceName))
.thenReturn(somePublicAppNamespace);
when(releaseService.findLatestActiveRelease(somePublicAppId, someDataCenter, somePublicNamespaceName))
.thenReturn(somePublicRelease);
when(somePublicRelease.getReleaseKey()).thenReturn(someServerSideReleaseKey);

ApolloConfig result = configController.queryConfig(appId, someClusterName,
somePublicNamespaceName, someDataCenter, someClientSideReleaseKey,
someClientIp, someResponse);

verify(releaseService, never()).findLatestActiveRelease(appId, someClusterName, somePublicNamespaceName);
assertEquals(someServerSideReleaseKey, result.getReleaseKey());
assertEquals(appId, result.getAppId());
assertEquals(someClusterName, result.getCluster());
assertEquals(somePublicNamespaceName, result.getNamespaceName());
assertEquals("foo", result.getConfigurations().get("apollo.public.bar"));
}

private AppNamespace assemblePublicAppNamespace(String appId, String namespace) {
return assembleAppNamespace(appId, namespace, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,37 @@ public void testQueryPrivateConfigFileWithPublicNamespaceExists() throws Excepti
assertEquals("v1-file", result.getConfigurations().get("k1"));
assertEquals(null, result.getConfigurations().get("k2"));
}

@Test
public void testQueryConfigForNoAppIdPlaceHolderWithPrivateNamespace() throws Exception {

HttpStatusCodeException httpException = null;
try {
ResponseEntity<ApolloConfig> response = restTemplate
.getForEntity("{baseurl}/configs/{appId}/{clusterName}/{namespace}", ApolloConfig.class,
getHostUrl(), ConfigConsts.NO_APPID_PLACEHOLDER, someCluster, ConfigConsts.NAMESPACE_APPLICATION);
} catch (HttpStatusCodeException ex) {
httpException = ex;
}

assertEquals(HttpStatus.NOT_FOUND, httpException.getStatusCode());
}

@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryPublicConfigForNoAppIdPlaceHolder() throws Exception {
ResponseEntity<ApolloConfig> response = restTemplate
.getForEntity("{baseurl}/configs/{appId}/{clusterName}/{namespace}?dataCenter={dateCenter}",
ApolloConfig.class,
getHostUrl(), ConfigConsts.NO_APPID_PLACEHOLDER, someCluster, somePublicNamespace, someDC);
ApolloConfig result = response.getBody();

assertEquals("TEST-RELEASE-KEY4", result.getReleaseKey());
assertEquals(ConfigConsts.NO_APPID_PLACEHOLDER, result.getAppId());
assertEquals(someCluster, result.getCluster());
assertEquals(somePublicNamespace, result.getNamespaceName());
assertEquals("someDC-v1", result.getConfigurations().get("k1"));
assertEquals("someDC-v2", result.getConfigurations().get("k2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public void setUp() throws Exception {
when(somePublicAppNamespace.getName()).thenReturn(somePublicNamespace);
when(appNamespaceService.findPublicNamespacesByNames(Sets.newHashSet(somePublicNamespace)))
.thenReturn(Lists.newArrayList(somePublicAppNamespace));
when(appNamespaceService.findPublicNamespacesByNames(Sets.newHashSet(someNamespace, somePublicNamespace)))
.thenReturn(Lists.newArrayList(somePublicAppNamespace));

ReflectionTestUtils.setField(watchKeysUtil, "appNamespaceService", appNamespaceService);
}
Expand Down Expand Up @@ -141,6 +143,28 @@ public void testAssembleAllWatchKeysWithPrivateAndPublicNamespaces() throws Exce
assertWatchKeys(somePublicAppId, clusters, somePublicNamespace, watchKeysMap.get(somePublicNamespace));
}

@Test
public void testAssembleWatchKeysForNoAppIdPlaceHolder() throws Exception {
Multimap<String, String> watchKeysMap =
watchKeysUtil.assembleAllWatchKeys(ConfigConsts.NO_APPID_PLACEHOLDER, someCluster,
Sets.newHashSet(someNamespace, anotherNamespace), someDC);

assertTrue(watchKeysMap.isEmpty());
}

@Test
public void testAssembleWatchKeysForNoAppIdPlaceHolderAndPublicNamespace() throws Exception {
Multimap<String, String> watchKeysMap =
watchKeysUtil.assembleAllWatchKeys(ConfigConsts.NO_APPID_PLACEHOLDER, someCluster,
Sets.newHashSet(someNamespace, somePublicNamespace), someDC);

Set<String> clusters = Sets.newHashSet(defaultCluster, someCluster, someDC);

assertEquals(clusters.size(), watchKeysMap.size());

assertWatchKeys(somePublicAppId, clusters, somePublicNamespace, watchKeysMap.get(somePublicNamespace));
}

private void assertWatchKeys(String appId, Set<String> clusters, String namespaceName,
Collection<String> watchedKeys) {
for (String cluster : clusters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public interface ConfigConsts {
String CLUSTER_NAMESPACE_SEPARATOR = "+";
String APOLLO_CLUSTER_KEY = "apollo.cluster";
String CONFIG_FILE_CONTENT_KEY = "content";
String NO_APPID_PLACEHOLDER = "ApolloNoAppIdPlaceHolder";
}

0 comments on commit 28929a3

Please sign in to comment.