Perfomance improvement for tenant state load
This commit is contained in:
parent
fbb3d85e08
commit
a88d624ffa
@ -72,6 +72,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
@ -421,21 +422,34 @@ public class DefaultTbApiUsageStateService extends TbApplicationEventListener<Pa
|
|||||||
private void initStatesFromDataBase() {
|
private void initStatesFromDataBase() {
|
||||||
try {
|
try {
|
||||||
log.info("Initializing tenant states.");
|
log.info("Initializing tenant states.");
|
||||||
|
updateLock.lock();
|
||||||
|
try {
|
||||||
|
ExecutorService tmpInitExecutor = Executors.newWorkStealingPool(20);
|
||||||
|
try {
|
||||||
PageDataIterable<Tenant> tenantIterator = new PageDataIterable<>(tenantService::findTenants, 1024);
|
PageDataIterable<Tenant> tenantIterator = new PageDataIterable<>(tenantService::findTenants, 1024);
|
||||||
|
List<Future<?>> futures = new ArrayList<>();
|
||||||
for (Tenant tenant : tenantIterator) {
|
for (Tenant tenant : tenantIterator) {
|
||||||
if (!myTenantStates.containsKey(tenant.getId()) && partitionService.resolve(ServiceType.TB_CORE, tenant.getId(), tenant.getId()).isMyPartition()) {
|
if (!myTenantStates.containsKey(tenant.getId()) && partitionService.resolve(ServiceType.TB_CORE, tenant.getId(), tenant.getId()).isMyPartition()) {
|
||||||
log.debug("[{}] Initializing tenant state.", tenant.getId());
|
log.debug("[{}] Initializing tenant state.", tenant.getId());
|
||||||
updateLock.lock();
|
futures.add(tmpInitExecutor.submit(() -> {
|
||||||
try {
|
try {
|
||||||
updateTenantState(getOrFetchState(tenant.getId()), tenantProfileCache.get(tenant.getTenantProfileId()));
|
updateTenantState(getOrFetchState(tenant.getId()), tenantProfileCache.get(tenant.getTenantProfileId()));
|
||||||
log.debug("[{}] Initialized tenant state.", tenant.getId());
|
log.debug("[{}] Initialized tenant state.", tenant.getId());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("[{}] Failed to initialize tenant API state", tenant.getId(), e);
|
log.warn("[{}] Failed to initialize tenant API state", tenant.getId(), e);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (Future<?> future : futures) {
|
||||||
|
future.get();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
tmpInitExecutor.shutdownNow();
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
updateLock.unlock();
|
updateLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
log.info("Initialized tenant states.");
|
log.info("Initialized tenant states.");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Unknown failure", e);
|
log.warn("Unknown failure", e);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user