fix: custom spi metadata loaded bug (#973)

pull/1016/head 1.11.1-2021.0.6
Shanyou Yu (Sean Yu) 2 years ago committed by GitHub
parent b2298dce83
commit c979e2e881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,4 @@
# Change Log # Change Log
--- ---
- [fix: custom spi metadata loaded bug.](https://github.com/Tencent/spring-cloud-tencent/pull/973)

@ -88,7 +88,7 @@
<properties> <properties>
<!-- Project revision --> <!-- Project revision -->
<revision>1.12.0-2021.0.6-SNAPSHOT</revision> <revision>1.11.1-2021.0.6</revision>
<!-- Spring Framework --> <!-- Spring Framework -->
<spring.framework.version>5.3.25</spring.framework.version> <spring.framework.version>5.3.25</spring.framework.version>

@ -184,26 +184,24 @@ public class StaticMetadataManager {
@SuppressWarnings("DuplicatedCode") @SuppressWarnings("DuplicatedCode")
private void parseCustomMetadata(List<InstanceMetadataProvider> instanceMetadataProviders) { private void parseCustomMetadata(List<InstanceMetadataProvider> instanceMetadataProviders) {
if (CollectionUtils.isEmpty(instanceMetadataProviders)) { // init customSPIMetadata
customSPIMetadata = Collections.emptyMap(); customSPIMetadata = new HashMap<>();
customSPITransitiveMetadata = Collections.emptyMap(); customSPITransitiveMetadata = new HashMap<>();
customSPIDisposableMetadata = Collections.emptyMap(); customSPIDisposableMetadata = new HashMap<>();
return; if (!CollectionUtils.isEmpty(instanceMetadataProviders)) {
instanceMetadataProviders.forEach(this::parseCustomMetadata);
} }
customSPIMetadata = Collections.unmodifiableMap(customSPIMetadata);
instanceMetadataProviders.forEach(this::parseCustomMetadata); customSPITransitiveMetadata = Collections.unmodifiableMap(customSPITransitiveMetadata);
customSPIDisposableMetadata = Collections.unmodifiableMap(customSPIDisposableMetadata);
} }
@SuppressWarnings("DuplicatedCode") @SuppressWarnings("DuplicatedCode")
private void parseCustomMetadata(InstanceMetadataProvider instanceMetadataProvider) { private void parseCustomMetadata(InstanceMetadataProvider instanceMetadataProvider) {
// resolve all metadata // resolve all metadata
Map<String, String> allMetadata = instanceMetadataProvider.getMetadata(); Map<String, String> allMetadata = instanceMetadataProvider.getMetadata();
if (allMetadata == null) { if (!CollectionUtils.isEmpty(allMetadata)) {
customSPIMetadata = Collections.emptyMap(); customSPIMetadata.putAll(allMetadata);
}
else {
customSPIMetadata = Collections.unmodifiableMap(allMetadata);
} }
// resolve transitive metadata // resolve transitive metadata
@ -216,7 +214,7 @@ public class StaticMetadataManager {
} }
} }
} }
customSPITransitiveMetadata = Collections.unmodifiableMap(transitiveMetadata); customSPITransitiveMetadata.putAll(transitiveMetadata);
Set<String> disposableKeys = instanceMetadataProvider.getDisposableMetadataKeys(); Set<String> disposableKeys = instanceMetadataProvider.getDisposableMetadataKeys();
Map<String, String> disposableMetadata = new HashMap<>(); Map<String, String> disposableMetadata = new HashMap<>();
@ -227,7 +225,7 @@ public class StaticMetadataManager {
} }
} }
} }
customSPIDisposableMetadata = Collections.unmodifiableMap(disposableMetadata); customSPIDisposableMetadata.putAll(disposableMetadata);
} }
private void merge() { private void merge() {

@ -18,6 +18,7 @@
package com.tencent.cloud.common.metadata; package com.tencent.cloud.common.metadata;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -26,15 +27,27 @@ import java.util.Set;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.spi.InstanceMetadataProvider; import com.tencent.cloud.common.spi.InstanceMetadataProvider;
import com.tencent.cloud.common.spi.impl.DefaultInstanceMetadataProvider;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub; import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAME;
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
@ -53,6 +66,26 @@ public class StaticMetadataManagerTest {
@Mock @Mock
private MetadataLocalProperties metadataLocalProperties; private MetadataLocalProperties metadataLocalProperties;
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
@BeforeAll
static void beforeAll() {
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
.thenReturn("unit-test");
}
@AfterAll
static void afterAll() {
mockedApplicationContextAwareUtils.close();
}
@BeforeEach
void setUp() {
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
}
@Test @Test
public void testParseConfigMetadata() { public void testParseConfigMetadata() {
Map<String, String> content = new HashMap<>(); Map<String, String> content = new HashMap<>();
@ -63,6 +96,7 @@ public class StaticMetadataManagerTest {
when(metadataLocalProperties.getContent()).thenReturn(content); when(metadataLocalProperties.getContent()).thenReturn(content);
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1")); when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
when(metadataLocalProperties.getDisposable()).thenReturn(Collections.singletonList("k1"));
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null); StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null);
@ -75,6 +109,10 @@ public class StaticMetadataManagerTest {
assertThat(transitiveMetadata.size()).isEqualTo(1); assertThat(transitiveMetadata.size()).isEqualTo(1);
assertThat(transitiveMetadata.get("k1")).isEqualTo("v1"); assertThat(transitiveMetadata.get("k1")).isEqualTo("v1");
Map<String, String> disposableMetadata = metadataManager.getConfigDisposableMetadata();
assertThat(disposableMetadata.size()).isEqualTo(1);
assertThat(disposableMetadata.get("k1")).isEqualTo("v1");
assertThat(metadataManager.getZone()).isEqualTo("zone1"); assertThat(metadataManager.getZone()).isEqualTo("zone1");
assertThat(metadataManager.getRegion()).isEqualTo("region1"); assertThat(metadataManager.getRegion()).isEqualTo("region1");
@ -93,17 +131,25 @@ public class StaticMetadataManagerTest {
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1")); when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties,
Collections.singletonList(new MockedMetadataProvider())); Arrays.asList(new MockedMetadataProvider(), new DefaultInstanceMetadataProvider(null)));
Map<String, String> metadata = metadataManager.getAllCustomMetadata(); Map<String, String> metadata = metadataManager.getAllCustomMetadata();
assertThat(metadata.size()).isEqualTo(3); assertThat(metadata.size()).isEqualTo(5);
assertThat(metadata.get("k1")).isEqualTo("v1"); assertThat(metadata.get("k1")).isEqualTo("v1");
assertThat(metadata.get("k2")).isEqualTo("v22"); assertThat(metadata.get("k2")).isEqualTo("v22");
assertThat(metadata.get("k3")).isEqualTo("v33"); assertThat(metadata.get("k3")).isEqualTo("v33");
assertThat(metadata.get(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE)).isEqualTo(NAMESPACE_TEST);
assertThat(metadata.get(DEFAULT_METADATA_SOURCE_SERVICE_NAME)).isEqualTo(SERVICE_PROVIDER);
Map<String, String> transitiveMetadata = metadataManager.getCustomSPITransitiveMetadata(); Map<String, String> transitiveMetadata = metadataManager.getCustomSPITransitiveMetadata();
assertThat(transitiveMetadata.size()).isEqualTo(1); assertThat(transitiveMetadata.size()).isEqualTo(1);
assertThat(metadata.get("k2")).isEqualTo("v22"); assertThat(transitiveMetadata.get("k2")).isEqualTo("v22");
Map<String, String> disposableMetadata = metadataManager.getCustomSPIDisposableMetadata();
assertThat(disposableMetadata.size()).isEqualTo(3);
assertThat(disposableMetadata.get("k3")).isEqualTo("v33");
assertThat(disposableMetadata.get(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE)).isEqualTo(NAMESPACE_TEST);
assertThat(disposableMetadata.get(DEFAULT_METADATA_SOURCE_SERVICE_NAME)).isEqualTo(SERVICE_PROVIDER);
assertThat(metadataManager.getZone()).isEqualTo("zone2"); assertThat(metadataManager.getZone()).isEqualTo("zone2");
assertThat(metadataManager.getRegion()).isEqualTo("region1"); assertThat(metadataManager.getRegion()).isEqualTo("region1");
@ -126,29 +172,38 @@ public class StaticMetadataManagerTest {
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1")); when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties,
Collections.singletonList(new MockedMetadataProvider())); Arrays.asList(new MockedMetadataProvider(), new DefaultInstanceMetadataProvider(null)));
Map<String, String> metadata = metadataManager.getMergedStaticMetadata(); Map<String, String> metadata = metadataManager.getMergedStaticMetadata();
assertThat(metadata.size()).isEqualTo(6); assertThat(metadata.size()).isEqualTo(8);
assertThat(metadata.get("k1")).isEqualTo("v1"); assertThat(metadata.get("k1")).isEqualTo("v1");
assertThat(metadata.get("k2")).isEqualTo("v22"); assertThat(metadata.get("k2")).isEqualTo("v22");
assertThat(metadata.get("k3")).isEqualTo("v33"); assertThat(metadata.get("k3")).isEqualTo("v33");
assertThat(metadata.get(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE)).isEqualTo(NAMESPACE_TEST);
assertThat(metadata.get(DEFAULT_METADATA_SOURCE_SERVICE_NAME)).isEqualTo(SERVICE_PROVIDER);
Map<String, String> transitiveMetadata = metadataManager.getMergedStaticTransitiveMetadata(); Map<String, String> transitiveMetadata = metadataManager.getMergedStaticTransitiveMetadata();
assertThat(transitiveMetadata.size()).isEqualTo(2); assertThat(transitiveMetadata.size()).isEqualTo(2);
assertThat(metadata.get("k1")).isEqualTo("v1"); assertThat(transitiveMetadata.get("k1")).isEqualTo("v1");
assertThat(metadata.get("k2")).isEqualTo("v22"); assertThat(transitiveMetadata.get("k2")).isEqualTo("v22");
Map<String, String> disposableMetadata = metadataManager.getMergedStaticDisposableMetadata();
assertThat(disposableMetadata.size()).isEqualTo(3);
assertThat(disposableMetadata.get("k3")).isEqualTo("v33");
assertThat(disposableMetadata.get(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE)).isEqualTo(NAMESPACE_TEST);
assertThat(disposableMetadata.get(DEFAULT_METADATA_SOURCE_SERVICE_NAME)).isEqualTo(SERVICE_PROVIDER);
assertThat(metadataManager.getAllEnvMetadata()).isEmpty(); assertThat(metadataManager.getAllEnvMetadata()).isEmpty();
assertThat(metadataManager.getEnvTransitiveMetadata()).isEmpty(); assertThat(metadataManager.getEnvTransitiveMetadata()).isEmpty();
assertThat(metadataManager.getZone()).isEqualTo("zone2"); assertThat(metadataManager.getZone()).isEqualTo("zone2");
assertThat(metadataManager.getRegion()).isEqualTo("region1"); assertThat(metadataManager.getRegion()).isEqualTo("region1");
assertThat(metadataManager.getCampus()).isEqualTo("campus2");
Map<String, String> locationInfo = metadataManager.getLocationMetadata(); Map<String, String> locationInfo = metadataManager.getLocationMetadata();
assertThat(locationInfo.get("zone")).isEqualTo("zone2"); assertThat(locationInfo.get("zone")).isEqualTo("zone2");
assertThat(locationInfo.get("region")).isEqualTo("region1"); assertThat(locationInfo.get("region")).isEqualTo("region1");
assertThat(locationInfo.get("campus")).isEqualTo("campus1"); assertThat(locationInfo.get("campus")).isEqualTo("campus2");
} }
@Test @Test
@ -195,6 +250,13 @@ public class StaticMetadataManagerTest {
return transitiveKeys; return transitiveKeys;
} }
@Override
public Set<String> getDisposableMetadataKeys() {
Set<String> transitiveKeys = new HashSet<>();
transitiveKeys.add("k3");
return transitiveKeys;
}
@Override @Override
public String getRegion() { public String getRegion() {
return "region1"; return "region1";
@ -207,7 +269,7 @@ public class StaticMetadataManagerTest {
@Override @Override
public String getCampus() { public String getCampus() {
return null; return "campus2";
} }
} }
} }

@ -70,7 +70,7 @@
</developers> </developers>
<properties> <properties>
<revision>1.12.0-2021.0.6-SNAPSHOT</revision> <revision>1.11.1-2021.0.6</revision>
<!-- Dependencies --> <!-- Dependencies -->
<polaris.version>1.12.0</polaris.version> <polaris.version>1.12.0</polaris.version>

Loading…
Cancel
Save