package com.hubspot.singularity.data;

import com.google.inject.Inject;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApiCache<K, V> {
  private static final Logger LOG = LoggerFactory.getLogger(ApiCache.class);

  public final boolean isEnabled;
  private final AtomicReference<Map<K, V>> zkValues;
  private final Supplier<Map<K, V>> supplyMap;

  @Inject
  public ApiCache(
    boolean isEnabled,
    int cacheTtl,
    Supplier<Map<K, V>> supplyMap,
    ScheduledExecutorService executor
  ) {
    this.isEnabled = isEnabled;
    this.supplyMap = supplyMap;
    this.zkValues = new AtomicReference<>(new HashMap<>());

    if (this.isEnabled) {
      executor.scheduleAtFixedRate(this::reloadZkValues, 0, cacheTtl, TimeUnit.SECONDS);
    }
  }

  private void reloadZkValues() {
    LOG.debug("Reloading values for map from ZooKeeper");
    Map<K, V> newZkValues = supplyMap.get();
    LOG.debug("Loaded new ZooKeeper values: {}", newZkValues);
    zkValues.set(newZkValues);
  }

  public V get(K key) {
    return this.zkValues.get().get(key);
  }

  public Map<K, V> getAll() {
    Map<K, V> allValues = this.zkValues.get();
    LOG.debug("Get all returned {}", allValues);
    return allValues;
  }

  public Map<K, V> getAll(Collection<K> keys) {
    Map<K, V> allValues = this.zkValues.get();
    Map<K, V> filteredValues = keys
      .stream()
      .filter(allValues::containsKey)
      .collect(Collectors.toMap(Function.identity(), allValues::get));
    LOG.debug("Get all from collection returned {}", filteredValues);

    return filteredValues;
  }

  public boolean isEnabled() {
    return isEnabled;
  }
}

@@ -1187,16 +1187,28 @@
 eper%22);%0A
+    try %7B%0A  
     Map%3C
@@ -1236,32 +1236,34 @@
 upplyMap.get();%0A
+  
     LOG.debug(%22L
@@ -1313,16 +1313,18 @@
 s);%0A    
+ 
+ 
 zkValues
@@ -1342,16 +1342,115 @@
 alues);%0A
+    %7D catch (Exception e) %7B%0A      LOG.warn(%22Reloading ApiCache failed: %7B%7D%22, e.getMessage());%0A    %7D%0A
   %7D%0A%0A  p

Add try/catch