Split out getOrCreateTenant and do not synchronize createTenant
Split out so that we can avoid the need to synchronized on instance,
since we now just do one operation on the synchronized tenants map
@@ -219,7 +219,7 @@ public class TenantRepository {
if (tenants.containsKey(tenantName)) throw new IllegalArgumentException("Tenant '" + tenantName + "' already exists");
writeTenantPath(tenantName);
- return createTenant(tenantName, clock.instant());
+ return getOrCreateTenant(tenantName, clock.instant());
}
public void createAndWriteTenantMetaData(Tenant tenant) {
@@ -288,7 +288,7 @@ public class TenantRepository {
}
// Use when bootstrapping an existing tenant based on ZooKeeper data
- protected synchronized void bootstrapTenant(TenantName tenantName) {
+ protected void bootstrapTenant(TenantName tenantName) {
createTenant(tenantName, readCreatedTimeFromZooKeeper(tenantName));
}
@@ -300,14 +300,18 @@ public class TenantRepository {
return clock.instant();
}
- // Creates tenant and all its dependencies. This also includes loading active applications
- private Tenant createTenant(TenantName tenantName, Instant created) {
+ private Tenant getOrCreateTenant(TenantName tenantName, Instant created) {
if (tenants.containsKey(tenantName)) {
Tenant tenant = getTenant(tenantName);
createAndWriteTenantMetaData(tenant);
return tenant;
+ } else {
+ return createTenant(tenantName, created);
+ }
}
+ // Creates tenant and all its dependencies. This also includes loading active applications
+ private Tenant createTenant(TenantName tenantName, Instant created) {
TenantApplications applicationRepo =
new TenantApplications(tenantName,
curator,
@@ -497,7 +501,7 @@ public class TenantRepository {
case CHILD_ADDED:
TenantName t1 = getTenantNameFromEvent(event);
if ( ! tenants.containsKey(t1))
- zkWatcherExecutor.execute(t1, () -> bootstrapTenant(t1));
+ zkWatcherExecutor.execute(t1, () -> getOrCreateTenant(t1, readCreatedTimeFromZooKeeper(t1)));
break;
case CHILD_REMOVED:
TenantName t2 = getTenantNameFromEvent(event);
