Skip to content

Commit

Permalink
initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
NilanshBansal committed May 9, 2024
1 parent 60dd0f9 commit 9b144b6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;

@Getter
@Setter
@ToString
@NoArgsConstructor
@Document
@FieldNameConstants
public class Tenant extends BaseDomain {
public class Tenant extends BaseDomain implements Serializable {

@Unique String slug;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

@Data
@EqualsAndHashCode(callSuper = true)
public class TenantConfiguration extends TenantConfigurationCE {}
public class TenantConfiguration extends TenantConfigurationCE implements Serializable {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.appsmith.server.repositories.ce;

import com.appsmith.server.domains.Tenant;
import com.appsmith.server.domains.User;
import reactor.core.publisher.Mono;

Expand All @@ -18,4 +19,8 @@ public interface CacheableRepositoryHelperCE {
Mono<String> getDefaultTenantId();

Mono<String> getInstanceAdminPermissionGroupId();

Mono<Tenant> fetchCachedTenant(String tenantId);

Mono<Void> evictCachedTenant(String tenantId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.appsmith.server.domains.Config;
import com.appsmith.server.domains.PermissionGroup;
import com.appsmith.server.domains.Tenant;
import com.appsmith.server.domains.TenantConfiguration;
import com.appsmith.server.domains.User;
import com.appsmith.server.domains.Workspace;
import com.appsmith.server.exceptions.AppsmithError;
Expand Down Expand Up @@ -166,4 +167,29 @@ public Mono<String> getInstanceAdminPermissionGroupId() {
.doOnSuccess(permissionGroupId ->
inMemoryCacheableRepositoryHelper.setInstanceAdminPermissionGroupId(permissionGroupId));
}

@Cache(cacheName = "defaultTenant", key = "{#tenantId}")
@Override
public Mono<Tenant> fetchCachedTenant(String tenantId) {
// Get the default tenant object from the DB and then populate the relevant user permissions in that
// We are doing this differently because `findBySlug` is a Mongo JPA query and not a custom Appsmith query
BridgeQuery<Tenant> defaultTenantCriteria = Bridge.equal(Tenant.Fields.slug, FieldName.DEFAULT);
Query query = new Query();
query.addCriteria(defaultTenantCriteria);

return mongoOperations.findOne(query, Tenant.class).map(tenant -> {
if (tenant.getTenantConfiguration() == null) {
tenant.setTenantConfiguration(new TenantConfiguration());
}
return tenant;
});
// .flatMap(tenant ->
// setUserPermissionsInObject(tenant).switchIfEmpty(Mono.just(tenant)));
}

@CacheEvict(cacheName = "defaultTenant", key = "{#tenantId}")
@Override
public Mono<Void> evictCachedTenant(String tenantId) {
return Mono.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.appsmith.server.services;

import com.appsmith.server.helpers.FeatureFlagMigrationHelper;
import com.appsmith.server.repositories.CacheableRepositoryHelper;
import com.appsmith.server.repositories.TenantRepository;
import com.appsmith.server.services.ce.TenantServiceCEImpl;
import com.appsmith.server.solutions.EnvManager;
Expand All @@ -19,7 +20,15 @@ public TenantServiceImpl(
AnalyticsService analyticsService,
ConfigService configService,
@Lazy EnvManager envManager,
FeatureFlagMigrationHelper featureFlagMigrationHelper) {
super(validator, repository, analyticsService, configService, envManager, featureFlagMigrationHelper);
FeatureFlagMigrationHelper featureFlagMigrationHelper,
CacheableRepositoryHelper cacheableRepositoryHelper) {
super(
validator,
repository,
analyticsService,
configService,
envManager,
featureFlagMigrationHelper,
cacheableRepositoryHelper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.helpers.CollectionUtils;
import com.appsmith.server.helpers.FeatureFlagMigrationHelper;
import com.appsmith.server.repositories.CacheableRepositoryHelper;
import com.appsmith.server.repositories.TenantRepository;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.BaseService;
Expand Down Expand Up @@ -39,17 +40,21 @@ public class TenantServiceCEImpl extends BaseService<TenantRepository, Tenant, S

private final FeatureFlagMigrationHelper featureFlagMigrationHelper;

private CacheableRepositoryHelper cacheableRepositoryHelper;

public TenantServiceCEImpl(
Validator validator,
TenantRepository repository,
AnalyticsService analyticsService,
ConfigService configService,
@Lazy EnvManager envManager,
FeatureFlagMigrationHelper featureFlagMigrationHelper) {
FeatureFlagMigrationHelper featureFlagMigrationHelper,
CacheableRepositoryHelper cacheableRepositoryHelper) {
super(validator, repository, analyticsService);
this.configService = configService;
this.envManager = envManager;
this.featureFlagMigrationHelper = featureFlagMigrationHelper;
this.cacheableRepositoryHelper = cacheableRepositoryHelper;
}

@Override
Expand All @@ -69,8 +74,9 @@ public Mono<String> getDefaultTenantId() {

@Override
public Mono<Tenant> updateTenantConfiguration(String tenantId, TenantConfiguration tenantConfiguration) {
return repository
.findById(tenantId, MANAGE_TENANT)
Mono<Void> evictTenantCache = cacheableRepositoryHelper.evictCachedTenant(tenantId);
return evictTenantCache
.then(repository.findById(tenantId, MANAGE_TENANT))
.switchIfEmpty(Mono.error(
new AppsmithException(AppsmithError.ACL_NO_RESOURCE_FOUND, FieldName.TENANT, tenantId)))
.flatMap(tenant -> {
Expand All @@ -89,11 +95,13 @@ public Mono<Tenant> updateTenantConfiguration(String tenantId, TenantConfigurati
return Mono.empty();
});
}
return envMono.then(Mono.zip(Mono.just(oldtenantConfiguration), Mono.just(tenant)));

return envMono.then(
Mono.zip(evictTenantCache, Mono.just(oldtenantConfiguration), Mono.just(tenant)));
})
.flatMap(tuple2 -> {
Tenant tenant = tuple2.getT2();
TenantConfiguration oldConfig = tuple2.getT1();
.flatMap(tuple3 -> {
Tenant tenant = tuple3.getT3();
TenantConfiguration oldConfig = tuple3.getT2();
AppsmithBeanUtils.copyNestedNonNullProperties(tenantConfiguration, oldConfig);
tenant.setTenantConfiguration(oldConfig);
return repository.updateById(tenantId, tenant, MANAGE_TENANT);
Expand Down Expand Up @@ -151,17 +159,8 @@ public Mono<Tenant> getTenantConfiguration() {

@Override
public Mono<Tenant> getDefaultTenant() {
// Get the default tenant object from the DB and then populate the relevant user permissions in that
// We are doing this differently because `findBySlug` is a Mongo JPA query and not a custom Appsmith query
return repository
.findBySlug(FieldName.DEFAULT)
.map(tenant -> {
if (tenant.getTenantConfiguration() == null) {
tenant.setTenantConfiguration(new TenantConfiguration());
}
return tenant;
})
.flatMap(tenant -> repository.setUserPermissionsInObject(tenant).switchIfEmpty(Mono.just(tenant)));
// Fetching Tenant from cache
return getDefaultTenantId().flatMap(tenantId -> cacheableRepositoryHelper.fetchCachedTenant(tenantId));
}

@Override
Expand Down

0 comments on commit 9b144b6

Please sign in to comment.