sv2000 commented on a change in pull request #3124: URL: https://github.com/apache/incubator-gobblin/pull/3124#discussion_r504078343
########## File path: gobblin-runtime/src/main/java/org/apache/gobblin/runtime/util/HelixLeaderUtils.java ########## @@ -0,0 +1,96 @@ +/* + * 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. + */ + +package org.apache.gobblin.runtime.util; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.apache.helix.HelixManager; +import org.apache.helix.PropertyKey; +import org.apache.helix.model.LiveInstance; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import com.linkedin.data.DataMap; +import com.linkedin.restli.common.HttpStatus; +import com.linkedin.restli.server.RestLiServiceException; +import com.typesafe.config.Config; + +import lombok.extern.slf4j.Slf4j; + +import org.apache.gobblin.service.ServiceConfigKeys; +import org.apache.gobblin.util.ConfigUtils; + + +/** + * Utils for storing/parsing helix URL in the helix instance name + */ +@Slf4j +public class HelixLeaderUtils { + public static String HELIX_INSTANCE_NAME_SEPARATOR = "@"; + + /** + */ + private static String getUrlFromHelixInstanceName(String helixInstanceName) { + if (!helixInstanceName.contains(HELIX_INSTANCE_NAME_SEPARATOR)) { + return null; + } else { + return helixInstanceName.substring(helixInstanceName.indexOf(HELIX_INSTANCE_NAME_SEPARATOR) + 1); + } + } + + private static String getLeaderUrl(HelixManager helixManager) { + PropertyKey key = helixManager.getHelixDataAccessor().keyBuilder().controllerLeader(); + LiveInstance leader = helixManager.getHelixDataAccessor().getProperty(key); + return getUrlFromHelixInstanceName(leader.getInstanceName()); + } + + /** + * If this host is not the leader, throw a {@link RestLiServiceException}, and include the URL of the leader host in + * the message and in the errorDetails under the key {@link ServiceConfigKeys#LEADER_URL}. + */ + public static void throwErrorIfNotLeader(Optional<HelixManager> helixManager) { + if (helixManager.isPresent() && !helixManager.get().isLeader()) { + String leaderUrl = getLeaderUrl(helixManager.get()); + if (leaderUrl == null) { + return; Review comment: Should we throw an error here? ########## File path: gobblin-restli/gobblin-flow-config-service/gobblin-flow-config-service-client/src/main/java/org/apache/gobblin/service/FlowClientUtils.java ########## @@ -0,0 +1,66 @@ +/* + * 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. + */ + +package org.apache.gobblin.service; + +import java.net.URI; +import java.net.URISyntaxException; + +import com.linkedin.r2.RemoteInvocationException; +import com.linkedin.r2.message.RequestContext; +import com.linkedin.restli.client.Request; +import com.linkedin.restli.client.Response; +import com.linkedin.restli.client.RestClient; +import com.linkedin.restli.client.RestLiResponseException; + + +/** + * Utils to be used by clients + */ +public class FlowClientUtils { + /** + * Send a restli {@link Request} to the server through a {@link RestClient}, but if the request is rejected due to not + * being sent to a leader node, get the leader node from the errorDetails and retry the request with that node by setting + * the D2-Hint-TargetService attribute. + * @param restClient rest client to use to send the request + * @param request request to send + * @param primaryResource resource part of the request URL (e.g. flowconfigsV2, which can be taken from + * {@link FlowconfigsV2RequestBuilders#getPrimaryResource()} + * @return {@link Response} returned from the request + * @throws RemoteInvocationException + */ + public static Response<?> sendRequestWithRetry(RestClient restClient, Request<?> request, String primaryResource) throws RemoteInvocationException { Review comment: Can we add unit tests? We should be able to add unit tests in GobblinServiceHATest. ---------------------------------------------------------------- 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: [email protected]
