Skip to content

Commit

Permalink
Add datasource caching
Browse files Browse the repository at this point in the history
Add datasource caching to avoid creating excessive connections to the databse.
Fixes wso2/api-manager/issues/781
  • Loading branch information
GDLMadushanka authored and HiranyaKavishani committed Feb 2, 2024
1 parent ee3726e commit 1232ce6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.user.core.tenant.Tenant;
import org.wso2.carbon.user.core.tenant.TenantManager;
import org.wso2.carbon.user.core.util.DatasourceDataHolder;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.ServerConstants;
Expand Down Expand Up @@ -480,6 +481,10 @@ public static void cleanupTenants(long tenantIdleTimeMillis) {
}
}
tenantConfigContexts.remove(tenantDomain);
// removing cached datasources of the domain
DatasourceDataHolder.removeDatasourcesOfTenant(getTenantId(tenantDomain));
} catch (Exception e) {
log.error("Error occurred while fetching the tenant details");
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.wso2.carbon.user.core.config.UserStoreConfigXMLProcessor;
import org.wso2.carbon.user.core.tenant.TenantCache;
import org.wso2.carbon.user.core.tenant.TenantIdKey;
import org.wso2.carbon.user.core.util.DatasourceDataHolder;
import org.wso2.carbon.user.core.util.UserCoreUtil;

import java.io.File;
import java.util.AbstractMap;
import java.util.regex.Pattern;

public class UserStoreDeploymentManager {
Expand Down Expand Up @@ -165,6 +167,10 @@ public void undeploy(String fileName) throws DeploymentException {

if (!isDisabled) {
userStoreManager.removeSecondaryUserStoreManager(domainName);
DatasourceDataHolder dataHolder = DatasourceDataHolder.getInstance();
AbstractMap.SimpleEntry<String, String> key
= new AbstractMap.SimpleEntry<>(String.valueOf(tenantId), domainName.toUpperCase());
dataHolder.removeDomainDataSources(key);
}
} catch (Exception ex) {
String errorMessage = "Error occurred at undeploying " + domainName + " from tenant:" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.wso2.carbon.user.core.profile.ProfileConfigurationManager;
import org.wso2.carbon.user.core.tenant.Tenant;
import org.wso2.carbon.user.core.util.DatabaseUtil;
import org.wso2.carbon.user.core.util.DatasourceDataHolder;
import org.wso2.carbon.user.core.util.JDBCRealmUtil;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.Secret;
Expand All @@ -71,6 +72,7 @@
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.SQLTimeoutException;
import java.sql.Timestamp;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -116,7 +118,7 @@ public class JDBCUserStoreManager extends AbstractUserStoreManager {
private static final String MYSQL = "mysql";
private static final String MARIADB = "mariadb";
private static final String POSTGRESQL = "postgresql";

public static final String PRIMARY_USER_STORE_DOMAIN = "PRIMARY";
private static final int MAX_ITEM_LIMIT_UNLIMITED = -1;

public JDBCUserStoreManager() {
Expand Down Expand Up @@ -293,9 +295,16 @@ public JDBCUserStoreManager(RealmConfiguration realmConfig, Map<String, Object>
}
this.claimManager = claimManager;
this.userRealm = realm;

boolean addDataStore = false;
// cache secondary user-store datasources.
String domain = getMyDomainName();
AbstractMap.SimpleEntry<String, String> key = new AbstractMap.SimpleEntry<>(String.valueOf(tenantId), domain);
jdbcds = DatasourceDataHolder.getInstance().getDataStoreForDomain(key);
try {
jdbcds = loadUserStoreSpacificDataSoruce();
if (jdbcds == null) {
addDataStore = !(PRIMARY_USER_STORE_DOMAIN.equals(domain) || tenantId == MultitenantConstants.SUPER_TENANT_ID);
jdbcds = loadUserStoreSpacificDataSoruce();
}

if (jdbcds == null) {
jdbcds = (DataSource) properties.get(UserCoreConstants.DATA_SOURCE);
Expand All @@ -305,6 +314,10 @@ public JDBCUserStoreManager(RealmConfiguration realmConfig, Map<String, Object>
properties.put(UserCoreConstants.DATA_SOURCE, jdbcds);
}

if (addDataStore) {
DatasourceDataHolder.getInstance().addDataSourceForDomain(key, jdbcds);
}

if (log.isDebugEnabled()) {
log.debug("The jdbcDataSource being used by JDBCUserStoreManager :: "
+ jdbcds.hashCode());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2022, WSO2 LLC (http://www.wso2.com).
*
* WSO2 LLC licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.user.core.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.sql.DataSource;
import java.util.AbstractMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

/**
* Data holder to maintain datasource (JDBC) references of each domain.
*/
public class DatasourceDataHolder {

private static final DatasourceDataHolder Instance = new DatasourceDataHolder();
private static Log log = LogFactory.getLog(DatasourceDataHolder.class);
//key = <tenantId,domain of the userstore)>
private static ConcurrentHashMap<AbstractMap.SimpleEntry<String, String>, DataSource> dataSourceHolder
= new ConcurrentHashMap<>();

public static DatasourceDataHolder getInstance() {
return Instance;
}

public void addDataSourceForDomain(AbstractMap.SimpleEntry<String, String> key, DataSource dataSource) {
dataSourceHolder.put(key, dataSource);
}

public void removeDomainDataSources(AbstractMap.SimpleEntry<String, String> domain) {
dataSourceHolder.remove(domain);
}

public DataSource getDataStoreForDomain(AbstractMap.SimpleEntry<String, String> domain) {
return dataSourceHolder.get(domain);
}

public static void removeDatasourcesOfTenant(int tenantId) {
Iterator<AbstractMap.SimpleEntry<String, String>> iterator = dataSourceHolder.keySet().iterator();
while (iterator.hasNext()) {
AbstractMap.SimpleEntry<String, String> entry = iterator.next();
if (entry.getKey().equals(String.valueOf(tenantId))) {
// Closing the connections
if (dataSourceHolder.get(entry) instanceof org.apache.tomcat.jdbc.pool.DataSourceProxy) {
((org.apache.tomcat.jdbc.pool.DataSourceProxy) dataSourceHolder.get(entry)).close(true);
}
dataSourceHolder.remove(entry);
}
}
}
}

0 comments on commit 1232ce6

Please sign in to comment.