yifan-c commented on code in PR #45: URL: https://github.com/apache/cassandra-sidecar/pull/45#discussion_r1192861006
########## client/src/main/java/org/apache/cassandra/sidecar/client/exception/UnexpectedStatusCodeException.java: ########## @@ -0,0 +1,53 @@ +/* + * 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.sidecar.client.exception; + +/** + * Represents an exception raised when an unexpected status code is encountered + */ +public class UnexpectedStatusCodeException extends RuntimeException +{ + /** + * Constructs a new {@link UnexpectedStatusCodeException} with the given {@code statusCode} and {@code body} + * + * @param statusCode the response status code + * @param body the body of the response + */ + public UnexpectedStatusCodeException(int statusCode, String body) Review Comment: Is it safe to assume the body is always `String`? ########## client/src/main/java/org/apache/cassandra/sidecar/client/selection/OrderedInstanceSelectionPolicy.java: ########## @@ -0,0 +1,54 @@ +/* + * 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.sidecar.client.selection; + +import java.util.Iterator; + +import org.apache.cassandra.sidecar.client.SidecarInstance; +import org.apache.cassandra.sidecar.client.SidecarInstancesProvider; +import org.jetbrains.annotations.NotNull; + +/** + * A selection policy for a multiple Cassandra Sidecar instances where the iterator returns in order + */ +public class OrderedInstanceSelectionPolicy implements InstanceSelectionPolicy +{ + protected final SidecarInstancesProvider instancesProvider; + + /** + * Constructs a new {@link OrderedInstanceSelectionPolicy} with the provided list of instances + * + * @param instancesProvider the class that provides the sidecar instances + */ + public OrderedInstanceSelectionPolicy(SidecarInstancesProvider instancesProvider) + { + this.instancesProvider = instancesProvider; + } + + /** + * Returns an iterator of {@link SidecarInstance instances}. + * + * @return an iterator of {@link SidecarInstance instances} + */ + @NotNull + public Iterator<SidecarInstance> iterator() + { + return instancesProvider.instances().iterator(); Review Comment: The order depends in the value returned from `instancesProvider.instances()`, which is implementation dependent. For example, the provider could shuffle the instances and return. In this example, the `OrderedInstanceSelectionPolicy` is no different from `RandomInstanceSelectionPolicy`. nit: It might want to sort the instances, so the order is guaranteed. ########## client/src/main/java/org/apache/cassandra/sidecar/client/retry/ExponentialBackoffRetryPolicy.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.sidecar.client.retry; + +/** + * A retry policy that will perform an exponential backoff. The backoff period increases for each retry attempt. + * + * <p>Example: For a configured {@code maxRetries} of {@code 10}, {@code retryDelayMillis} of {@code 200} + * milliseconds, and {@code maxRetryDelayMillis} of {@code 20,000} milliseconds, the sequence is as follows: + * + * <pre> + * request backoff + * + * 1 200 + * 2 400 + * 3 800 + * 4 1600 + * 5 3200 + * 6 6400 + * 7 12800 + * 8 20000 + * 9 20000 + * 10 20000 + * </pre> + */ +public class ExponentialBackoffRetryPolicy extends BasicRetryPolicy +{ + private final long maxRetryDelayMillis; + + /** + * Constructs an exponential backoff retry policy unlimited number of retries and no delay between retries. + */ + public ExponentialBackoffRetryPolicy() + { + super(); + this.maxRetryDelayMillis = 0; + } + + /** + * Constructs an exponential backoff retry policy with {@code maxRetries} number of retries, + * {@code retryDelayMillis} delay between retries, and {@code maxRetryDelayMillis} maximum delay for the + * exponential backoff. + * + * @param maxRetries the maximum number of retries + * @param retryDelayMillis the delay between retries in milliseconds + * @param maxRetryDelayMillis the maximum retry delay in milliseconds + */ + public ExponentialBackoffRetryPolicy(int maxRetries, long retryDelayMillis, long maxRetryDelayMillis) + { + super(maxRetries, retryDelayMillis); + this.maxRetryDelayMillis = maxRetryDelayMillis; + } + + /** + * Returns the number of milliseconds to wait before attempting the next request. This value is upper-bounded + * by {@code maxRetryDelayMillis} if configured. The delay increases exponentially based on the number of + * attempts already performed. + * + * @param attempts the number of attempts already performed for this request + * @return the number of milliseconds to wait before attempting the next request + */ + @Override + protected long retryDelayMillis(int attempts) + { + long exponentialValue = (long) Math.pow(2, attempts - 1); + // do not multiply times retryDelayMillis, if we've reached the Long.MAX_VALUE already to avoid overflows + long retryDelay = exponentialValue == Long.MAX_VALUE ? exponentialValue : exponentialValue * retryDelayMillis; Review Comment: `exponentialValue * retryDelayMillis` could overflow and result in a negative value. But it will never happen in practice, no human could possibly wait for this long time. ########## client/src/main/java/org/apache/cassandra/sidecar/client/request/Request.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.sidecar.client.request; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpMethod; +import org.apache.cassandra.sidecar.common.utils.HttpRange; + +/** + * An abstract class representing a request for Sidecar + */ +public abstract class Request +{ + protected final String requestURI; + + /** + * Constructs a Sidecar request with the given {@code requestURI}. Defaults to {@code ssl} enabled. + * + * @param requestURI the URI of the request + */ + protected Request(String requestURI) + { + this.requestURI = requestURI; + } + + /** + * @return a map of headers for the request + */ + public Map<String, String> headers() + { + HttpRange range = range(); + if (range == null) + { + return Collections.emptyMap(); + } + Map<String, String> headers = new HashMap<>(); + headers.put(HttpHeaderNames.RANGE.toString(), range.toString()); + return Collections.unmodifiableMap(headers); + } + + /** + * @return the range for the HTTP request + */ + protected HttpRange range() + { + return null; + } + + /** + * @return the HTTP method to be used for the request + */ + public HttpMethod method() + { + return HttpMethod.GET; Review Comment: nit: it would read more clear to not supply a default HttpMethod, and enforce the subclasses to define it. ########## client/src/main/java/org/apache/cassandra/sidecar/client/retry/BasicRetryPolicy.java: ########## @@ -0,0 +1,297 @@ +/* + * 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.sidecar.client.retry; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpStatusClass; +import org.apache.cassandra.sidecar.client.HttpResponse; +import org.apache.cassandra.sidecar.client.exception.ResourceNotFoundException; +import org.apache.cassandra.sidecar.client.request.Request; + +/** + * A basic {@link RetryPolicy} supporting standard status codes + */ +public class BasicRetryPolicy extends RetryPolicy +{ + protected static final String RETRY_AFTER = "Retry-After"; + // Use -1 to retry indefinitely Review Comment: nit: define a constants -- 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]

