jacek-lewandowski commented on code in PR #2458: URL: https://github.com/apache/cassandra/pull/2458#discussion_r1254197140
########## src/java/org/apache/cassandra/locator/AbstractCloudMetadataServiceSnitch.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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; + +abstract class AbstractCloudMetadataServiceSnitch extends AbstractNetworkTopologySnitch +{ + static final Logger logger = LoggerFactory.getLogger(AbstractCloudMetadataServiceSnitch.class); + + static final String DEFAULT_DC = "UNKNOWN-DC"; + static final String DEFAULT_RACK = "UNKNOWN-RACK"; + + protected final AbstractCloudMetadataServiceConnector connector; + protected final SnitchProperties snitchProperties; + + protected String localRack; Review Comment: So my preference is that those two fields are private final in the subclasses and in this class we only have abstract methods like `getLocalRack()` and `getLocalDC()` which are called from `getRack(endpoint)` and `getDatacenter(endpoint)` ########## src/java/org/apache/cassandra/locator/AbstractCloudMetadataServiceSnitch.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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; + +abstract class AbstractCloudMetadataServiceSnitch extends AbstractNetworkTopologySnitch +{ + static final Logger logger = LoggerFactory.getLogger(AbstractCloudMetadataServiceSnitch.class); + + static final String DEFAULT_DC = "UNKNOWN-DC"; + static final String DEFAULT_RACK = "UNKNOWN-RACK"; + + protected final AbstractCloudMetadataServiceConnector connector; + protected final SnitchProperties snitchProperties; + + protected String localRack; + protected String localDc; + + private Map<InetAddressAndPort, Map<String, String>> savedEndpoints; + + public AbstractCloudMetadataServiceSnitch(AbstractCloudMetadataServiceConnector connector, SnitchProperties snitchProperties) + { + this.connector = connector; + this.snitchProperties = snitchProperties; + } + + @Override + public String getRack(InetAddressAndPort endpoint) + { + if (endpoint.equals(FBUtilities.getBroadcastAddressAndPort())) + return localRack; + EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint); + if (state == null || state.getApplicationState(ApplicationState.RACK) == null) + { + if (savedEndpoints == null) + savedEndpoints = SystemKeyspace.loadDcRackInfo(); + if (savedEndpoints.containsKey(endpoint)) + return savedEndpoints.get(endpoint).get("rack"); + return DEFAULT_RACK; + } + return state.getApplicationState(ApplicationState.RACK).value; + + } + + @Override + public String getDatacenter(InetAddressAndPort endpoint) + { + if (endpoint.equals(FBUtilities.getBroadcastAddressAndPort())) + return localDc; + EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint); + if (state == null || state.getApplicationState(ApplicationState.DC) == null) + { + if (savedEndpoints == null) + savedEndpoints = SystemKeyspace.loadDcRackInfo(); + if (savedEndpoints.containsKey(endpoint)) + return savedEndpoints.get(endpoint).get("data_center"); + return DEFAULT_DC; + } + return state.getApplicationState(ApplicationState.DC).value; + } + + protected void parseResponse(String response) Review Comment: I think that this method does not belong to this abstract implementation. There should be no details about the response interpretation. If this method is used in many places, perhaps it could go as static to something like `SnitchUtils`. ########## src/java/org/apache/cassandra/locator/DefaultCloudMetadataServiceSnitch.java: ########## @@ -0,0 +1,63 @@ +/* + * 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 com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; + +import org.apache.cassandra.locator.AbstractCloudMetadataServiceConnector.DefaultCloudMetadataServiceConnector; + +import static org.apache.cassandra.locator.AbstractCloudMetadataServiceConnector.METADATA_URL_PROPERTY; + +/** + * Default cloud-based metadata service snitch which parses datacenter and rack of a node by + * executing GET query for URL configured in cassandra-rackdc.properties under + * key {@link AbstractCloudMetadataServiceConnector#METADATA_URL_PROPERTY}. It expects HTTP response code 200. + * It is not passing any headers to GET request. + * <p> + * The response will be parsed like following - if the response body is, for example "us-central1-a", then + * the datacenter will be "us-central1" and rack will be "a". There is value of "dc_suffix" key in + * cassandra-rackdc.properties appended to datacenter. + */ +public class DefaultCloudMetadataServiceSnitch extends AbstractCloudMetadataServiceSnitch +{ + public DefaultCloudMetadataServiceSnitch() throws IOException + { + this(new SnitchProperties()); + } + + public DefaultCloudMetadataServiceSnitch(SnitchProperties properties) throws IOException Review Comment: maybe let it accept the default url as argument so that the inheritors can set it directly? ########## src/java/org/apache/cassandra/locator/DefaultCloudMetadataServiceSnitch.java: ########## @@ -0,0 +1,63 @@ +/* + * 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 com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; + +import org.apache.cassandra.locator.AbstractCloudMetadataServiceConnector.DefaultCloudMetadataServiceConnector; + +import static org.apache.cassandra.locator.AbstractCloudMetadataServiceConnector.METADATA_URL_PROPERTY; + +/** + * Default cloud-based metadata service snitch which parses datacenter and rack of a node by + * executing GET query for URL configured in cassandra-rackdc.properties under + * key {@link AbstractCloudMetadataServiceConnector#METADATA_URL_PROPERTY}. It expects HTTP response code 200. + * It is not passing any headers to GET request. + * <p> + * The response will be parsed like following - if the response body is, for example "us-central1-a", then + * the datacenter will be "us-central1" and rack will be "a". There is value of "dc_suffix" key in + * cassandra-rackdc.properties appended to datacenter. + */ +public class DefaultCloudMetadataServiceSnitch extends AbstractCloudMetadataServiceSnitch +{ Review Comment: would you rename it into something like `GenericCloudMetadataServiceSnitch`? ########## src/java/org/apache/cassandra/locator/AbstractCloudMetadataServiceConnector.java: ########## @@ -25,22 +25,41 @@ import java.net.URL; import java.util.Map; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import static java.nio.charset.StandardCharsets.UTF_8; abstract class AbstractCloudMetadataServiceConnector { + static final String METADATA_URL_PROPERTY = "metadata_url"; + + public static class DefaultCloudMetadataServiceConnector extends AbstractCloudMetadataServiceConnector + { + protected DefaultCloudMetadataServiceConnector(String metadataServiceUrl) + { + super(metadataServiceUrl); + } + } + protected final String metadataServiceUrl; + @VisibleForTesting // for mockito + protected String getMetadataServiceUrl() + { + return metadataServiceUrl; + } + protected AbstractCloudMetadataServiceConnector(String metadataServiceUrl) { + Preconditions.checkNotNull(metadataServiceUrl, METADATA_URL_PROPERTY + " for a connector can not be null"); this.metadataServiceUrl = metadataServiceUrl; } public String apiCall(String query) throws IOException { - return apiCall(metadataServiceUrl, query, 200); + return apiCall(getMetadataServiceUrl(), query, 200); Review Comment: why? ########## src/java/org/apache/cassandra/locator/DefaultCloudMetadataServiceSnitch.java: ########## @@ -0,0 +1,63 @@ +/* + * 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 com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; + +import org.apache.cassandra.locator.AbstractCloudMetadataServiceConnector.DefaultCloudMetadataServiceConnector; + +import static org.apache.cassandra.locator.AbstractCloudMetadataServiceConnector.METADATA_URL_PROPERTY; + +/** + * Default cloud-based metadata service snitch which parses datacenter and rack of a node by + * executing GET query for URL configured in cassandra-rackdc.properties under + * key {@link AbstractCloudMetadataServiceConnector#METADATA_URL_PROPERTY}. It expects HTTP response code 200. + * It is not passing any headers to GET request. + * <p> + * The response will be parsed like following - if the response body is, for example "us-central1-a", then + * the datacenter will be "us-central1" and rack will be "a". There is value of "dc_suffix" key in + * cassandra-rackdc.properties appended to datacenter. + */ +public class DefaultCloudMetadataServiceSnitch extends AbstractCloudMetadataServiceSnitch Review Comment: does this class need to be public? ########## src/java/org/apache/cassandra/locator/Ec2MetadataServiceConnector.java: ########## @@ -45,7 +45,7 @@ abstract class Ec2MetadataServiceConnector extends AbstractCloudMetadataServiceC Ec2MetadataServiceConnector(SnitchProperties properties) { - super(properties.get(EC2_METADATA_URL_PROPERTY, DEFAULT_EC2_METADATA_URL)); + super(properties.get(EC2_METADATA_URL_PROPERTY, properties.get(METADATA_URL_PROPERTY, DEFAULT_EC2_METADATA_URL))); Review Comment: why do we need this? ########## src/java/org/apache/cassandra/locator/AbstractCloudMetadataServiceConnector.java: ########## @@ -25,22 +25,41 @@ import java.net.URL; import java.util.Map; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import static java.nio.charset.StandardCharsets.UTF_8; abstract class AbstractCloudMetadataServiceConnector { + static final String METADATA_URL_PROPERTY = "metadata_url"; Review Comment: CassandraRelevantProperties? ########## src/java/org/apache/cassandra/locator/AbstractCloudMetadataServiceConnector.java: ########## @@ -25,22 +25,41 @@ import java.net.URL; import java.util.Map; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import static java.nio.charset.StandardCharsets.UTF_8; abstract class AbstractCloudMetadataServiceConnector { + static final String METADATA_URL_PROPERTY = "metadata_url"; + + public static class DefaultCloudMetadataServiceConnector extends AbstractCloudMetadataServiceConnector + { + protected DefaultCloudMetadataServiceConnector(String metadataServiceUrl) + { + super(metadataServiceUrl); + } + } + protected final String metadataServiceUrl; + @VisibleForTesting // for mockito + protected String getMetadataServiceUrl() + { + return metadataServiceUrl; + } + protected AbstractCloudMetadataServiceConnector(String metadataServiceUrl) { + Preconditions.checkNotNull(metadataServiceUrl, METADATA_URL_PROPERTY + " for a connector can not be null"); Review Comment: Since you do validation, maybe also validate if it is a valid URL? ########## src/java/org/apache/cassandra/locator/AbstractCloudMetadataServiceConnector.java: ########## @@ -61,6 +80,7 @@ public String apiCall(String url, conn = (HttpURLConnection) new URL(url + query).openConnection(); extraHeaders.forEach(conn::setRequestProperty); conn.setRequestMethod(method); + conn.setConnectTimeout(30000); Review Comment: Should we take the timeout from the properties with 30s as default? -- 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]

