Skip to content

Commit

Permalink
fix(hosts) : cache non existing hosts (#27366) (#29390)
Browse files Browse the repository at this point in the history
### Proposed Changes
* Non existing host added to host cache to improve performance

### Checklist
- [x] Tests

---------

Co-authored-by: erickgonzalez <[email protected]>
  • Loading branch information
dsolistorres and erickgonzalez authored Aug 22, 2024
1 parent fa74e31 commit 0b665e2
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void remove(final String key){
Logger.debug(this, "Cache not able to be removed", e);
}

final Host host = CacheLocator.getHostCache().get(key);
final Host host = CacheLocator.getHostCache().getById(key);
if(host != null){
CacheLocator.getHostCache().remove(host);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,56 @@ public Host findDefaultHost(User user, boolean respectFrontendRoles) throws DotS
@Override
@CloseDBIfOpened
public Host resolveHostName(String serverName, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
Host host = hostCache.getHostByAlias(serverName);
User systemUser = APILocator.systemUser();

if(host == null){

Host host;
final Host cachedHostByAlias = hostCache.getHostByAlias(serverName);
if (UtilMethods.isSet(() -> cachedHostByAlias.getIdentifier())) {
if (HostCache.CACHE_404_HOST.equals(cachedHostByAlias.getIdentifier())) {
return null;
}
host = cachedHostByAlias;
} else {
final User systemUser = APILocator.systemUser();
try {
final Optional<Host> optional = resolveHostNameWithoutDefault(serverName, systemUser, respectFrontendRoles);
host = optional.isPresent() ? optional.get() : findDefaultHost(systemUser, respectFrontendRoles);
} catch (Exception e) {
host = findDefaultHost(systemUser, respectFrontendRoles);
}

if(host != null){
if (host != null) {
hostCache.addHostAlias(serverName, host);
} else {
hostCache.addHostAlias(serverName, HostCache.cache404Contentlet);
}
}

checkSitePermission(user, respectFrontendRoles, host);
if (host != null) {
checkSitePermission(user, respectFrontendRoles, host);
}
return host;
}

@Override
@CloseDBIfOpened
public Optional<Host> resolveHostNameWithoutDefault(String serverName, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {

Host host = hostCache.getHostByAlias(serverName);
User systemUser = APILocator.systemUser();

if(host == null){
Host host;
final Host cachedHostByAlias = hostCache.getHostByAlias(serverName);
if (UtilMethods.isSet(() -> cachedHostByAlias.getIdentifier())) {
if (HostCache.CACHE_404_HOST.equals(cachedHostByAlias.getIdentifier())) {
return Optional.empty();
}
host = cachedHostByAlias;
} else {
User systemUser = APILocator.systemUser();
host = findByNameNotDefault(serverName, systemUser, respectFrontendRoles);

if(host == null){
host = findByAlias(serverName, systemUser, respectFrontendRoles);
}

if(host != null){
if (host == null) {
hostCache.addHostAlias(serverName, HostCache.cache404Contentlet);
} else {
hostCache.addHostAlias(serverName, host);
}
}
Expand Down Expand Up @@ -297,7 +311,14 @@ public Host find(final String id, final User user,
return findSystemHost();
}

Host site = hostCache.get(id);
Host site = null;
Host cachedSiteById = hostCache.getById(id);
if (UtilMethods.isSet(() -> cachedSiteById.getIdentifier())) {
if (HostCache.CACHE_404_HOST.equals(cachedSiteById.getIdentifier())) {
return null;
}
site = cachedSiteById;
}

if (site == null) {
site = DBSearch(id,user,respectFrontendRoles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@
public abstract class HostCache implements Cachable{
protected static String PRIMARY_GROUP = "HostCache";
protected static String ALIAS_GROUP = "HostAliasCache";

protected static String NOT_FOUND_BY_ID_GROUP = "Host404ByIdCache";
protected static String NOT_FOUND_BY_NAME_GROUP = "Host404ByNameCache";

public static final String CACHE_404_HOST = "CACHE_404_HOST";

protected static final Host cache404Contentlet = new Host() {
@Override
public String getIdentifier() {
return CACHE_404_HOST;
}
};

abstract protected Host add(Host host);

abstract protected Host get(String key);
abstract protected Host getById(String id);

abstract protected Host getByName(String name);

abstract public void clearCache();

Expand All @@ -26,5 +39,10 @@ public abstract class HostCache implements Cachable{
abstract protected Host getHostByAlias(String alias);

abstract protected void addHostAlias(String alias, Host host);

abstract protected void add404HostById(String id);

abstract protected void add404HostByName(String name);

abstract protected void clearAliasCache() ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class HostCacheImpl extends HostCache {


// region's name for the cache
private String[] groupNames = {PRIMARY_GROUP, ALIAS_GROUP};
private final String[] groupNames = {
PRIMARY_GROUP, ALIAS_GROUP, NOT_FOUND_BY_ID_GROUP, NOT_FOUND_BY_NAME_GROUP
};

public HostCacheImpl() {
cache = CacheLocator.getCacheAdministrator();
Expand All @@ -37,6 +39,9 @@ protected Host add(Host host) {
String key = host.getIdentifier();
String key2 =host.getHostname();

cache.remove(key, NOT_FOUND_BY_ID_GROUP);
cache.remove(key2, NOT_FOUND_BY_NAME_GROUP);

// Add the key to the cache
cache.put(key, host,PRIMARY_GROUP);
cache.put(key2, host,PRIMARY_GROUP);
Expand Down Expand Up @@ -72,7 +77,10 @@ protected Host getHostByAlias(String key) {
Host host = null;
try{
String hostId = (String) cache.get(key,ALIAS_GROUP);
host = get(hostId);
if (CACHE_404_HOST.equals(hostId)) {
return cache404Contentlet;
}
host = get(hostId, NOT_FOUND_BY_ID_GROUP);
if(host == null){
cache.remove(key, ALIAS_GROUP);
}
Expand All @@ -83,24 +91,55 @@ protected Host getHostByAlias(String key) {
return host;
}

protected Host get(String key) {
private Host get(String key, String notFoundCacheGroup) {
Host host = null;
try{
host = (Host) cache.get(key,PRIMARY_GROUP);
if (UtilMethods.isSet(notFoundCacheGroup)) {
host = (Host) cache.get(key, notFoundCacheGroup);
}
if (host != null && CACHE_404_HOST.equals(host.getIdentifier())) {
return cache404Contentlet;
} else {
host = (Host) cache.get(key, PRIMARY_GROUP);
}
}catch (DotCacheException e) {
Logger.debug(this, "Cache Entry not found", e);
}

return host;
}

/* (non-Javadoc)
/**
* Get a host by id
* @param id the id of the host
* @return the host or 404 if the host is in the not found cache,
* null if the host is not found in the cache
*/
@Override
protected Host getById(final String id) {
return get(id, NOT_FOUND_BY_ID_GROUP);
}

/**
* Get a host by name
* @param name the name of the host
* @return the host or 404 if the host is in the not found cache,
* null if the host is not found in the cache
*/
@Override
protected Host getByName(final String name) {
return get(name, NOT_FOUND_BY_NAME_GROUP);
}

/* (non-Javadoc)
* @see com.dotmarketing.business.PermissionCache#clearCache()
*/
public void clearCache() {
// clear the cache
cache.flushGroup(PRIMARY_GROUP);
cache.flushGroup(ALIAS_GROUP);
cache.flushGroup(NOT_FOUND_BY_ID_GROUP);
cache.flushGroup(NOT_FOUND_BY_NAME_GROUP);
}

/* (non-Javadoc)
Expand All @@ -114,21 +153,19 @@ protected void remove(Host host){
String _defaultHost =PRIMARY_GROUP +DEFAULT_HOST;
cache.remove(_defaultHost,PRIMARY_GROUP);

//remove aliases from host in cache
Host h = get(host.getIdentifier());


String key = host.getIdentifier();
String key2 = host.getHostname();

try{
cache.remove(key,PRIMARY_GROUP);
cache.remove(key,NOT_FOUND_BY_ID_GROUP);
}catch (Exception e) {
Logger.debug(this, "Cache not able to be removed", e);
}

try{
cache.remove(key2,PRIMARY_GROUP);
cache.remove(key2, NOT_FOUND_BY_NAME_GROUP);
}catch (Exception e) {
Logger.debug(this, "Cache not able to be removed", e);
}
Expand All @@ -147,16 +184,37 @@ public String getPrimaryGroup() {


protected Host getDefaultHost(){
return get(DEFAULT_HOST);
return get(DEFAULT_HOST, null);
}

protected void addHostAlias(String alias, Host host){
if(alias != null && host != null && UtilMethods.isSet(host.getIdentifier())){
cache.put(alias, host.getIdentifier(),ALIAS_GROUP);
}
}



/**
* Add the host id to the 404 (not found) cache
* @param id the id of the host
*/
@Override
protected void add404HostById(String id) {
if (id != null) {
cache.put(id, cache404Contentlet, NOT_FOUND_BY_ID_GROUP);
}
}

/**
* Add the host name to the 404 (not found) cache
* @param name the name of the host
*/
@Override
protected void add404HostByName(String name) {
if (name != null) {
cache.put(name, cache404Contentlet, NOT_FOUND_BY_NAME_GROUP);
}
}

protected void clearAliasCache() {
// clear the alias cache
cache.flushGroup(ALIAS_GROUP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,14 @@ protected ContentletAPI getContentletAPI() {

@Override
public Host bySiteName(final String siteName) {
Host site = siteCache.get(siteName);
if (null == site || !UtilMethods.isSet(site.getIdentifier())) {
Host site;
final Host cachedSiteByName = siteCache.getByName(siteName);;
if (UtilMethods.isSet(() -> cachedSiteByName.getIdentifier())) {
if (HostCache.CACHE_404_HOST.equals(cachedSiteByName.getIdentifier())) {
return null;
}
site = cachedSiteByName;
} else {
final DotConnect dc = new DotConnect();
final StringBuilder sqlQuery = new StringBuilder().append(SELECT_SITE_INODE)
.append(WHERE);
Expand All @@ -200,6 +206,7 @@ public Host bySiteName(final String siteName) {
try {
final List<Map<String, String>> dbResults = dc.loadResults();
if (dbResults.isEmpty()) {
siteCache.add404HostByName(siteName);
return null;
}
final String siteInode = dbResults.get(0).get("inode");
Expand Down Expand Up @@ -227,8 +234,14 @@ public Host bySiteName(final String siteName) {

@Override
public Host byAlias(String alias) {
Host site = this.siteCache.getHostByAlias(alias);
if (null == site) {
Host site = null;
Host cachedSiteByAlias = this.siteCache.getHostByAlias(alias);
if (UtilMethods.isSet(() -> cachedSiteByAlias.getIdentifier())) {
if (HostCache.CACHE_404_HOST.equals(cachedSiteByAlias.getIdentifier())) {
return null;
}
site = cachedSiteByAlias;
} else {
final DotConnect dc = new DotConnect();
final StringBuilder sqlQuery = new StringBuilder().append(SELECT_SITE_INODE_AND_ALIASES)
.append(WHERE)
Expand All @@ -248,6 +261,7 @@ public Host byAlias(String alias) {
try {
final List<Map<String, String>> dbResults = dc.loadResults();
if (dbResults.isEmpty()) {
siteCache.addHostAlias(alias, HostCache.cache404Contentlet);
return null;
}
if (dbResults.size() == 1) {
Expand Down Expand Up @@ -335,7 +349,7 @@ public List<Host> findAll(final int limit, final int offset, final String orderB

@Override
public Host findSystemHost(final User user, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
Host systemHost = this.siteCache.get(Host.SYSTEM_HOST);
Host systemHost = this.siteCache.getById(Host.SYSTEM_HOST);
if (null != systemHost) {
return systemHost;
}
Expand Down Expand Up @@ -409,6 +423,10 @@ public Host DBSearch(final String id, final boolean respectFrontendRoles) throws
this.siteCache.add(site);
}
}
if (null == site && !Host.SYSTEM_HOST.equals(id)) {
this.siteCache.add404HostById(id);
Logger.warn(HostAPIImpl.class, String.format("Site with id '%s' not found", id));
}
return site;
}

Expand Down
Loading

0 comments on commit 0b665e2

Please sign in to comment.