You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2023/06/20 02:04:43 UTC
[brooklyn-server] 07/10: use weak map to access more tasks in memory
This is an automated email from the ASF dual-hosted git repository.
heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit da5e8360e58371334635dd9329793c135fea0392
Author: Alex Heneveld <al...@cloudsoft.io>
AuthorDate: Sun Jun 18 01:45:07 2023 +0100
use weak map to access more tasks in memory
sometimes tasks are brooklyn GC'd, ie removed from the map,
but still referenced elsewhere, giving a lot more "task GC'd" messages than necessary.
by using weak refs, if there are still child references, a direct request will find the task
even if brooklyn has determined it can be GC'd
---
.../util/core/task/BasicExecutionManager.java | 23 +++++++++++-----------
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java b/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
index 005ba07336..e2cde9a050 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionManager.java
@@ -21,16 +21,9 @@ package org.apache.brooklyn.util.core.task;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Iterables;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
+
+import java.lang.ref.WeakReference;
+import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
@@ -50,6 +43,8 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
+
+import com.google.common.collect.MapMaker;
import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.internal.BrooklynLoggingCategories;
import org.apache.brooklyn.api.mgmt.ExecutionContext;
@@ -270,7 +265,8 @@ public class BasicExecutionManager implements ExecutionManager {
//NB CopyOnWriteArraySet is a perf bottleneck, and the simple map makes it easier to remove when a tag is empty
private Map<Object, Set<Task<?>>> tasksByTag = new HashMap<Object, Set<Task<?>>>();
- private ConcurrentMap<String, Task<?>> tasksById = new ConcurrentHashMap<String, Task<?>>();
+ private Map<String, Task<?>> tasksById = new ConcurrentHashMap<String, Task<?>>();
+ private Map<String, Task<?>> tasksByIdWeak = Collections.synchronizedMap(new MapMaker().weakValues().makeMap());
private ConcurrentMap<Object, TaskScheduler> schedulerByTag = new ConcurrentHashMap<Object, TaskScheduler>();
@@ -593,7 +589,9 @@ public class BasicExecutionManager implements ExecutionManager {
@Override
public Task<?> getTask(String id) {
- return tasksById.get(id);
+ Task<?> result = tasksById.get(id);
+ if (result==null) result = tasksByIdWeak.get(id);
+ return result;
}
/**
@@ -1123,6 +1121,7 @@ public class BasicExecutionManager implements ExecutionManager {
}
tasksById.put(task.getId(), task);
+ tasksByIdWeak.put(task.getId(), task);
totalTaskCount.incrementAndGet();
}