Skip to content

Commit

Permalink
Fixing CC runtime artifacts retrieval issue and GW timeout issue duri…
Browse files Browse the repository at this point in the history
…ng API subscription retrieval (wso2#12514)

* Fixing Error when getting Choreo Connect Runtime Artifacts

* Fixing gateway timeout issue in API Subscription retrieval with high #of APIs (wso2#6737)
  • Loading branch information
HiranyaKavishani authored and PasanT9 committed Oct 9, 2024
1 parent d1ca39e commit 440da63
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15964,7 +15964,6 @@ public List<APIProductResource> getProductMappingsForAPI(API api) throws APIMana
APIProductResource productMapping = new APIProductResource();
productMapping.setProductIdentifier(apiProductIdentifier);
productMapping.setUriTemplate(uriTemplate);

productMappings.add(productMapping);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ public RuntimeArtifactDto generateGatewayArtifact(List<APIRuntimeArtifactDto> ap
// adding env_properties.json
Map<String, Map<String, Environment>> environmentSpecificAPIProperties =
getEnvironmentSpecificAPIProperties(apiRuntimeArtifactDtoList);
String environmentSpecificAPIPropertyFile = Paths.get(tempDirectory.getAbsolutePath(),
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE).toString();
CommonUtil.writeDtoToFile(environmentSpecificAPIPropertyFile, ExportFormat.JSON,
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE,
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_KEY_NAME,
environmentSpecificAPIProperties);

if (environmentSpecificAPIProperties != null) {
String environmentSpecificAPIPropertyFile = Paths.get(tempDirectory.getAbsolutePath(),
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE).toString();
CommonUtil.writeDtoToFile(environmentSpecificAPIPropertyFile, ExportFormat.JSON,
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE,
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_KEY_NAME,
environmentSpecificAPIProperties);
}
CommonUtil.archiveDirectory(tempDirectory.getAbsolutePath());
FileUtils.deleteQuietly(tempDirectory);
RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
Expand Down Expand Up @@ -150,7 +152,10 @@ private Map<String, Map<String, Environment>> getEnvironmentSpecificAPIPropertie
List<String> apiIds = apiRuntimeArtifactDtoList.stream()
.map(APIRuntimeArtifactDto::getApiId)
.collect(Collectors.toList());
return environmentSpecificAPIPropertyDao.getEnvironmentSpecificAPIPropertiesOfAPIs(apiIds);
if (!apiIds.isEmpty()) {
return environmentSpecificAPIPropertyDao.getEnvironmentSpecificAPIPropertiesOfAPIs(apiIds);
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


public class RegistryPersistenceUtil {
private static final Log log = LogFactory.getLog(RegistryPersistenceUtil.class);

//Stores <tenantId, isTenantRXTLoaded> to load RXTs only once
private static final ConcurrentHashMap<Integer, Boolean> tenantRxtLoaded = new ConcurrentHashMap<>();

/**
* When an input is having '-AT-',replace it with @ [This is required to persist API data between registry and database]
*
Expand Down Expand Up @@ -483,76 +487,84 @@ public static void loadTenantRegistry(int tenantId) throws RegistryException {
}

public static void loadloadTenantAPIRXT(String tenant, int tenantID) throws RegistryException, PersistenceException {

RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
UserRegistry registry = null;
try {

registry = registryService.getGovernanceSystemRegistry(tenantID);
} catch (RegistryException e) {
throw new PersistenceException("Error when create registry instance ", e);
}

String rxtDir = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources"
+ File.separator + "rxts";
File file = new File(rxtDir);
FilenameFilter filenameFilter = new FilenameFilter() {
@Override public boolean accept(File dir, String name) {
// if the file extension is .rxt return true, else false
return name.endsWith(".rxt");
// Synchronize on the tenant specific lock
synchronized (tenantID + "_loadTenantAPIRXT") {
// Check if RXTs have already been loaded for this tenant
if (Boolean.TRUE.equals(tenantRxtLoaded.get(tenantID))) {
return; // RXTs already loaded, exit method
}
};
String[] rxtFilePaths = file.list(filenameFilter);

if (rxtFilePaths == null) {
throw new PersistenceException("rxt files not found in directory " + rxtDir);
}
RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
UserRegistry registry = null;
try {

for (String rxtPath : rxtFilePaths) {
String resourcePath = GovernanceConstants.RXT_CONFIGS_PATH + RegistryConstants.PATH_SEPARATOR + rxtPath;
registry = registryService.getGovernanceSystemRegistry(tenantID);
} catch (RegistryException e) {
throw new PersistenceException("Error when create registry instance ", e);
}

//This is "registry" is a governance registry instance, therefore calculate the relative path to governance.
String govRelativePath = RegistryUtils.getRelativePathToOriginal(resourcePath,
RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(),
RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH));
try {
// calculate resource path
UserRealm tenantUserRealm = (UserRealm) ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID);
RegistryAuthorizationManager authorizationManager = new RegistryAuthorizationManager(tenantUserRealm);
resourcePath = authorizationManager.computePathOnMount(resourcePath);
String rxtDir = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources"
+ File.separator + "rxts";
File file = new File(rxtDir);
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// if the file extension is .rxt return true, else false
return name.endsWith(".rxt");
}
};
String[] rxtFilePaths = file.list(filenameFilter);

org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance()
.getRealmService().
getTenantUserRealm(tenantID)
.getAuthorizationManager();
if (rxtFilePaths == null) {
throw new PersistenceException("rxt files not found in directory " + rxtDir);
}

if (registry.resourceExists(govRelativePath)) {
// set anonymous user permission to RXTs
if (!authManager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET)) {
authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
for (String rxtPath : rxtFilePaths) {
String resourcePath = GovernanceConstants.RXT_CONFIGS_PATH + RegistryConstants.PATH_SEPARATOR + rxtPath;

//This is "registry" is a governance registry instance, therefore calculate the relative path to governance.
String govRelativePath = RegistryUtils.getRelativePathToOriginal(resourcePath,
RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(),
RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH));
try {
// calculate resource path
UserRealm tenantUserRealm = (UserRealm) ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID);
RegistryAuthorizationManager authorizationManager = new RegistryAuthorizationManager(tenantUserRealm);
resourcePath = authorizationManager.computePathOnMount(resourcePath);

org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance()
.getRealmService().
getTenantUserRealm(tenantID)
.getAuthorizationManager();

if (registry.resourceExists(govRelativePath)) {
// set anonymous user permission to RXTs
if (!authManager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET)) {
authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
}
continue;
}
continue;
}

String rxt = FileUtil.readFileToString(rxtDir + File.separator + rxtPath);
Resource resource = registry.newResource();
resource.setContent(rxt.getBytes(Charset.defaultCharset()));
resource.setMediaType(APIConstants.RXT_MEDIA_TYPE);
registry.put(govRelativePath, resource);
String rxt = FileUtil.readFileToString(rxtDir + File.separator + rxtPath);
Resource resource = registry.newResource();
resource.setContent(rxt.getBytes(Charset.defaultCharset()));
resource.setMediaType(APIConstants.RXT_MEDIA_TYPE);
registry.put(govRelativePath, resource);

authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);

} catch (UserStoreException e) {
throw new PersistenceException("Error while adding role permissions to API", e);
} catch (IOException e) {
String msg = "Failed to read rxt files";
throw new PersistenceException(msg, e);
} catch (RegistryException e) {
String msg = "Failed to add rxt to registry ";
throw new PersistenceException(msg, e);
} catch (UserStoreException e) {
throw new PersistenceException("Error while adding role permissions to API", e);
} catch (IOException e) {
String msg = "Failed to read rxt files";
throw new PersistenceException(msg, e);
} catch (RegistryException e) {
String msg = "Failed to add rxt to registry ";
throw new PersistenceException(msg, e);
}
}
// Mark RXTs as loaded for this tenant
tenantRxtLoaded.put(tenantID, Boolean.TRUE);
}

}

/**
Expand Down

0 comments on commit 440da63

Please sign in to comment.