Github user pnowojski commented on a diff in the pull request:

    https://github.com/apache/flink/pull/4840#discussion_r146574784
  
    --- Diff: 
flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/legacy/metrics/MetricStore.java
 ---
    @@ -38,29 +43,136 @@
     import static 
org.apache.flink.runtime.metrics.dump.QueryScopeInfo.INFO_CATEGORY_OPERATOR;
     import static 
org.apache.flink.runtime.metrics.dump.QueryScopeInfo.INFO_CATEGORY_TASK;
     import static 
org.apache.flink.runtime.metrics.dump.QueryScopeInfo.INFO_CATEGORY_TM;
    +import static org.apache.flink.util.Preconditions.checkNotNull;
     
     /**
      * Nested data-structure to store metrics.
    - *
    - * <p>This structure is not thread-safe.
      */
    +@ThreadSafe
     public class MetricStore {
        private static final Logger LOG = 
LoggerFactory.getLogger(MetricStore.class);
     
    -   final JobManagerMetricStore jobManager = new JobManagerMetricStore();
    -   final Map<String, TaskManagerMetricStore> taskManagers = new 
HashMap<>();
    -   final Map<String, JobMetricStore> jobs = new HashMap<>();
    +   private final ComponentMetricStore jobManager = new 
ComponentMetricStore();
    +   private final Map<String, TaskManagerMetricStore> taskManagers = new 
ConcurrentHashMap<>();
    +   private final Map<String, JobMetricStore> jobs = new 
ConcurrentHashMap<>();
    +
    +   /**
    +    * Remove not active task managers.
    +    *
    +    * @param activeTaskManagers to retain.
    +    */
    +   public synchronized void retainTaskManagers(List<String> 
activeTaskManagers) {
    --- End diff --
    
    1. performance here is not that big a deal, but code correctness is. With 
`synchronized` it is just easier to implement any changes here in a thread safe 
manner. Without it, any new developer coming here will have to understand much 
more assumptions about this code, like whether consistency matters here or nor? 
whether order of the operations/access to the fields is important or nor? etc...
    
    2. even with `synchronized` we still need either concurrent hash maps, 
because we return and make them visible to the outside world by getters. So we 
either need concurrent hash maps or return copies of them.


---

Reply via email to