i3wangyi commented on a change in pull request #417: Move partition health 
check method into dataAccessor layer
URL: https://github.com/apache/helix/pull/417#discussion_r314912447
 
 

 ##########
 File path: 
helix-rest/src/main/java/org/apache/helix/rest/common/HelixDataAccessorWrapper.java
 ##########
 @@ -1,24 +1,158 @@
 package org.apache.helix.rest.common;
 
-import org.apache.helix.HelixProperty;
-import org.apache.helix.PropertyKey;
-import org.apache.helix.manager.zk.ZKHelixDataAccessor;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.stream.Collectors;
+
+import org.apache.helix.HelixProperty;
+import org.apache.helix.PropertyKey;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.manager.zk.ZKHelixDataAccessor;
+import org.apache.helix.model.ExternalView;
+import org.apache.helix.model.RESTConfig;
+import org.apache.helix.rest.client.CustomRestClient;
+import org.apache.helix.rest.client.CustomRestClientFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This is a wrapper for {@link ZKHelixDataAccessor} that caches the result of 
the batch reads it
  * performs.
  * Note that the usage of this object is valid for one REST request.
  */
 public class HelixDataAccessorWrapper extends ZKHelixDataAccessor {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HelixDataAccessorWrapper.class);
+  private static final ExecutorService POOL = Executors.newCachedThreadPool();
+
+  private static final String PARTITION_HEALTH_KEY = "PARTITION_HEALTH";
+  private static final String IS_HEALTHY_KEY = "IS_HEALTHY";
+  private static final String EXPIRY_KEY = "EXPIRE";
+
+  private final CustomRestClient _restClient;
   private final Map<PropertyKey, HelixProperty> _propertyCache = new 
HashMap<>();
   private final Map<PropertyKey, List<String>> _batchNameCache = new 
HashMap<>();
 
   public HelixDataAccessorWrapper(ZKHelixDataAccessor dataAccessor) {
     super(dataAccessor);
+    _restClient = CustomRestClientFactory.get();
+  }
+
+  HelixDataAccessorWrapper(ZKHelixDataAccessor dataAccessor, CustomRestClient 
client) {
+    super(dataAccessor);
+    _restClient = client;
+  }
+
+  public List<ExternalView> getExternalViews() {
+    return getChildNames(keyBuilder().externalViews()).stream()
+        .map(externalView -> (ExternalView) 
getProperty(keyBuilder().externalView(externalView)))
+        .collect(Collectors.toList());
+  }
+
+  public Map<String, Map<String, Boolean>> 
getPartitionHealthOfInstance(RESTConfig restConfig,
+      Map<String, String> customPayLoads) {
+    // Only checks the instances are online with valid reports
+    List<String> liveInstances = getChildNames(keyBuilder().liveInstances());
+    // Make a parallel batch call for getting all healthreports from ZK.
+    List<HelixProperty> zkHealthReports = getProperty(liveInstances.stream()
+        .map(instance -> keyBuilder().healthReport(instance, 
PARTITION_HEALTH_KEY))
+        .collect(Collectors.toList()), false);
+    Map<String, Future<Map<String, Boolean>>> parallelTasks = new HashMap<>();
+    for (int i = 0; i < liveInstances.size(); i++) {
+      String liveInstance = liveInstances.get(i);
+      Optional<ZNRecord> maybeHealthRecord =
+          
Optional.ofNullable(zkHealthReports.get(i)).map(HelixProperty::getRecord);
+      parallelTasks.put(liveInstance, POOL.submit(() -> {
+        if (maybeHealthRecord.isPresent()) {
+          return getPartitionsHealth(liveInstance, maybeHealthRecord.get(), 
restConfig,
+              customPayLoads);
+        } else {
 
 Review comment:
   It's different than the previous implementation. The private method 
`getPartitionsHealth` only submits queries when it's expired. The else case 
deals with when we don't have any record for the instance, it needs to submit a 
query of empty partitions list.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org
For additional commands, e-mail: reviews-h...@helix.apache.org

Reply via email to