Skip to content

Commit

Permalink
Fixing gateway timeout issue in API Subscription retrieval with high …
Browse files Browse the repository at this point in the history
…#of APIs (#6737)
  • Loading branch information
HiranyaKavishani committed Sep 19, 2024
1 parent 45b0ec9 commit f6c8463
Showing 1 changed file with 71 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,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 @@ -446,76 +450,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);

Check warning on line 465 in components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java#L464-L465

Added lines #L464 - L465 were not covered by tests
}

//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);

Check warning on line 481 in components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java#L481

Added line #L481 was not covered by tests
}

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);

Check warning on line 514 in components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java#L510-L514

Added lines #L510 - L514 were not covered by tests

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

Check warning on line 516 in components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java#L516

Added line #L516 was not covered by tests

} 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);
}

Check warning on line 526 in components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java#L518-L526

Added lines #L518 - L526 were not covered by tests
}
// Mark RXTs as loaded for this tenant
tenantRxtLoaded.put(tenantID, Boolean.TRUE);
}

}

/**
Expand Down

0 comments on commit f6c8463

Please sign in to comment.