jacek-lewandowski commented on code in PR #2462: URL: https://github.com/apache/cassandra/pull/2462#discussion_r1253983448
########## src/java/org/apache/cassandra/locator/AzureSnitch.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.cassandra.locator; + +import java.io.IOException; +import java.util.Map; +import java.util.regex.Pattern; + +import com.google.common.collect.ImmutableMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.databind.JsonNode; +import org.apache.cassandra.db.SystemKeyspace; +import org.apache.cassandra.gms.ApplicationState; +import org.apache.cassandra.gms.EndpointState; +import org.apache.cassandra.gms.Gossiper; +import org.apache.cassandra.utils.FBUtilities; +import org.apache.cassandra.utils.JsonUtils; + +import static java.lang.String.format; + +public class AzureSnitch extends AbstractNetworkTopologySnitch +{ + private static final Logger logger = LoggerFactory.getLogger(AzureSnitch.class); + + private static final String DEFAULT_DC = "UNKNOWN-DC"; + private static final String DEFAULT_RACK = "UNKNOWN-RACK"; + + private Map<InetAddressAndPort, Map<String, String>> savedEndpoints; + + String rack; + String datacenter; + + public AzureSnitch() throws IOException + { + this(new SnitchProperties()); + } + + public AzureSnitch(SnitchProperties properties) throws IOException + { + this(new AzureCloudConnector(properties)); + } + + public AzureSnitch(AzureCloudConnector connector) throws IOException + { + String response = connector.getResponse(); + JsonNode jsonNode = JsonUtils.JSON_OBJECT_MAPPER.readTree(response); + + JsonNode location = jsonNode.get("location"); + JsonNode zone = jsonNode.get("zone"); + + if (location == null || location.isNull() || location.asText().isEmpty()) + datacenter = DEFAULT_DC; + else + datacenter = location.asText(); + + if (zone == null || zone.isNull() || zone.asText().isEmpty()) + rack = DEFAULT_RACK; + else + rack = "zone-" + zone.asText(); + + logger.info(format("%s using region: %s, zone: %s, properties: %s", AzureSnitch.class.getName(), datacenter, rack, connector)); + } + + static class AzureCloudConnector extends AbstractCloudMetadataServiceConnector + { + private static final Logger logger = LoggerFactory.getLogger(AzureSnitch.class); + private static final Pattern API_VERSION_PATTERN = Pattern.compile("\\d\\d\\d\\d-\\d\\d-\\d\\d"); + + static final String METADATA_SERVICE_PROPERTY_KEY = "metadata_url"; + static final String DEFAULT_METADATA_SERVICE_URL = "http://169.254.169.254"; + static final String METADATA_QUERY = "/metadata/instance/compute?api-version=%s&format=json"; + static final String VERSIONS_ENDPOINT = "/metadata/versions"; + static final String METADATA_HEADER = "Metadata"; + static final String API_VERSION_PROPERTY_KEY = "azure_api_version"; + + private final String apiVersion; + + protected AzureCloudConnector(SnitchProperties properties) throws IOException + { + super(properties.get(METADATA_SERVICE_PROPERTY_KEY, DEFAULT_METADATA_SERVICE_URL)); + apiVersion = getApiVersion(properties); + logger.debug("Resolved API version " + apiVersion); + } + + String getResponse() throws IOException + { + return apiCall(metadataServiceUrl, + String.format(METADATA_QUERY, apiVersion), + "GET", + ImmutableMap.of(METADATA_HEADER, "true"), + 200); + } + + String getApiVersion(SnitchProperties properties) throws IOException + { + String version = properties.get(API_VERSION_PROPERTY_KEY, null); + + if (version != null && API_VERSION_PATTERN.matcher(version).matches()) + return version; + + logger.debug(format("API version of '%s' does not follow pattern %s or is null. Going to " + + "parse the latest version from the service ...", + version, API_VERSION_PATTERN)); + + String response = apiCall(metadataServiceUrl, + VERSIONS_ENDPOINT, + "GET", + ImmutableMap.of("Metadata", "true"), + 200); + + JsonNode versions = JsonUtils.JSON_OBJECT_MAPPER.readTree(response); + JsonNode apiVersions = versions.get("apiVersions"); + + if (apiVersions == null) + throw new IllegalStateException("Unable to get field 'apiVersions' from the response!"); + + if (!(apiVersions.isArray())) + throw new IOException(format( + "Expected a list, but got a %s:%s", apiVersions.getClass().getSimpleName(), apiVersions)); + + String[] versionsArray = JsonUtils.JSON_OBJECT_MAPPER.convertValue(apiVersions, String[].class); + + if (versionsArray != null && versionsArray.length > 0) + return versionsArray[versionsArray.length - 1]; + else + throw new IllegalStateException("returned apiVersions array is empty!"); + } + + @Override + public String toString() + { + return format("%s{%s=%s,%s=%s}", + AzureCloudConnector.class.getName(), + METADATA_SERVICE_PROPERTY_KEY, + metadataServiceUrl, + API_VERSION_PROPERTY_KEY, + apiVersion); + } + } + + public String getRack(InetAddressAndPort endpoint) Review Comment: This method, and `getDatacenter` method, are identical in all snitch implementations. The only difference is that it returns a value of a field `rack` and, say in EC2, it returns value of a field `ec2zone`. Could you extract simple method like `getRackInternal() { return rack }`, define it abstract in `AbstractNetworkTopologySnitch` and move the implementation of `getRack(endpoint)` to `AbstractNetworkTopologySnitch`? You can do the same for `getDatacenter(endpoint)` method, and do that for all the snitch implementations. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

