Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KNOX-2978 - Race condition between Service Discovery and Polling Config Analyzer #811

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ private ClouderaManagerCluster discoverCluster(DiscoveryApiClient client, String

log.discoveringCluster(clusterName);

repository.registerCluster(client.getConfig());

Set<ServiceModel> serviceModels = new HashSet<>();

List<ApiService> serviceList = getClusterServices(client.getConfig(), servicesResourceApi);
Expand Down Expand Up @@ -340,7 +338,7 @@ private boolean shouldSkipServiceDiscovery(List<ServiceModelGenerator> modelGene
private List<ApiService> getClusterServices(ServiceDiscoveryConfig serviceDiscoveryConfig, ServicesResourceApi servicesResourceApi) throws ApiException {
log.lookupClusterServicesFromRepository();
List<ApiService> services = repository.getServices(serviceDiscoveryConfig);
if (services == null || services.isEmpty()) {
if (services.isEmpty()) {
try {
log.lookupClusterServicesFromCM();
final ApiServiceList serviceList = servicesResourceApi.readServices(serviceDiscoveryConfig.getCluster(), VIEW_SUMMARY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,18 @@ void clear() {
repository.clear();
}

void registerCluster(ServiceDiscoveryConfig serviceDiscoveryConfig) {
repository.putIfAbsent(RepositoryKey.of(serviceDiscoveryConfig), Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(cacheEntryTTL)).build());
}

void addService(ServiceDiscoveryConfig serviceDiscoveryConfig, ApiService service) {
getClusterServices(serviceDiscoveryConfig).put(service, new ServiceDetails());
}

List<ApiService> getServices(ServiceDiscoveryConfig serviceDiscoveryConfig) {
final Cache<ApiService, ServiceDetails> clusterServices = getClusterServices(serviceDiscoveryConfig);
return clusterServices == null ? null
: clusterServices.asMap().entrySet().stream().filter(entry -> entry.getValue() != null).map(entry -> entry.getKey())
return clusterServices.asMap().entrySet().stream().filter(entry -> entry.getValue() != null).map(entry -> entry.getKey())
.collect(Collectors.toList());
}

private Cache<ApiService, ServiceDetails> getClusterServices(ServiceDiscoveryConfig serviceDiscoveryConfig) {
return repository.get(RepositoryKey.of(serviceDiscoveryConfig));
return repository.computeIfAbsent(RepositoryKey.of(serviceDiscoveryConfig), (k) -> Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(cacheEntryTTL)).build());
}

void addServiceConfig(ServiceDiscoveryConfig serviceDiscoveryConfig, ApiService service, ApiServiceConfig serviceConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ public static void init() {
@Before
public void setUp() {
repository.clear();
assertNull(repository.getServices(serviceDiscoveryConfig));
repository.setCacheEntryTTL(GatewayConfig.DEFAULT_CM_SERVICE_DISCOVERY_CACHE_ENTRY_TTL);
assertTrue(repository.getServices(serviceDiscoveryConfig).isEmpty());
}

@Test
public void testRegisterCluster() throws Exception {
assertNull(repository.getServices(serviceDiscoveryConfig));
repository.registerCluster(serviceDiscoveryConfig);
assertTrue(repository.getServices(serviceDiscoveryConfig).isEmpty());
final List<ApiService> services = repository.getServices(serviceDiscoveryConfig);
assertNotNull(services);
assertTrue(services.isEmpty());
Expand All @@ -72,7 +71,6 @@ public void testRegisterCluster() throws Exception {
@Test
public void testAddService() throws Exception {
final String serviceName = "HDFS-1";
repository.registerCluster(serviceDiscoveryConfig);
assertFalse(containsService(serviceName));
final ApiService service = EasyMock.createNiceMock(ApiService.class);
EasyMock.expect(service.getName()).andReturn(serviceName).anyTimes();
Expand All @@ -86,7 +84,6 @@ public void testAddServiceConfig() throws Exception {
final String serviceName = "HDFS-1";
final String serviceConfigName = "myServiceConfigName";
final String serviceConfigValue = "myServiceConfigValue";
repository.registerCluster(serviceDiscoveryConfig);
final ApiService service = EasyMock.createNiceMock(ApiService.class);
EasyMock.expect(service.getName()).andReturn(serviceName).anyTimes();
final ApiConfig serviceConfig = EasyMock.createNiceMock(ApiConfig.class);
Expand All @@ -105,7 +102,6 @@ public void testAddServiceConfig() throws Exception {
public void testAddRole() throws Exception {
final String serviceName = "HDFS-1";
final String roleName = "NAMENODE-1";
repository.registerCluster(serviceDiscoveryConfig);
final ApiService service = EasyMock.createNiceMock(ApiService.class);
EasyMock.expect(service.getName()).andReturn(serviceName).anyTimes();
final ApiRole role = EasyMock.createNiceMock(ApiRole.class);
Expand All @@ -121,7 +117,6 @@ public void testAddRole() throws Exception {

@Test
public void testAddNullRoles() throws Exception {
repository.registerCluster(serviceDiscoveryConfig);
final ApiService service = EasyMock.createNiceMock(ApiService.class);
EasyMock.expect(service.getName()).andReturn(ClouderaManagerServiceDiscovery.CORE_SETTINGS_TYPE).anyTimes();
EasyMock.replay(service);
Expand All @@ -132,7 +127,6 @@ public void testAddNullRoles() throws Exception {

@Test
public void testAddNullRoleItems() throws Exception {
repository.registerCluster(serviceDiscoveryConfig);
final ApiService service = EasyMock.createNiceMock(ApiService.class);
EasyMock.expect(service.getName()).andReturn(ClouderaManagerServiceDiscovery.CORE_SETTINGS_TYPE).anyTimes();
final ApiRoleList roles = EasyMock.createNiceMock(ApiRoleList.class);
Expand All @@ -150,7 +144,6 @@ public void testAddRoleConfig() throws Exception {
final String roleConfigName = "myRoleConfig";
final String roleConfigValue = "myRoleConfigValue";

repository.registerCluster(serviceDiscoveryConfig);
final ApiService service = EasyMock.createNiceMock(ApiService.class);
EasyMock.expect(service.getName()).andReturn(serviceName).anyTimes();
final ApiRole role = EasyMock.createNiceMock(ApiRole.class);
Expand All @@ -174,6 +167,7 @@ public void testAddRoleConfig() throws Exception {
public void testCacheAutoEviction() throws Exception {
final long entryTTL = 1;
repository.setCacheEntryTTL(entryTTL);
repository.clear();
testAddService();
TimeUnit.SECONDS.sleep(entryTTL + 1);
assertFalse(containsService("HDFS-1"));
Expand Down
Loading