From 4eb9dd0f01badf0460ac189f0441b5f842148c7e Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Fri, 3 Jul 2020 06:51:59 -0500 Subject: [PATCH] Move Request and Response to loadbalancer package. Fixes gh-772. (#773) * Move Request and Response to loadbalancer package. Fixes gh-772. * Fix checkstyle. * Inherit and reference repackaged classes. * Revert "Inherit and reference repackaged classes." This reverts commit 02808c5f188d0befa69839efadfc82a9e8887f9b. * Fix Status enums. * Make old classes extend the new ones. --- .../loadbalancer/CompletionContext.java | 70 +++++++++++++++++++ .../client/loadbalancer/DefaultRequest.java | 46 ++++++++++++ .../loadbalancer/DefaultRequestContext.java | 47 +++++++++++++ .../client/loadbalancer/DefaultResponse.java | 47 +++++++++++++ .../client/loadbalancer/EmptyResponse.java | 41 +++++++++++ .../cloud/client/loadbalancer/Request.java | 34 +++++++++ .../cloud/client/loadbalancer/Response.java | 40 +++++++++++ .../reactive/CompletionContext.java | 32 ++++----- .../loadbalancer/reactive/DefaultRequest.java | 19 ++--- .../reactive/DefaultRequestContext.java | 22 ++---- .../reactive/DefaultResponse.java | 21 ++---- .../loadbalancer/reactive/EmptyResponse.java | 17 ++--- .../client/loadbalancer/reactive/Request.java | 12 ++-- .../loadbalancer/reactive/Response.java | 9 ++- 14 files changed, 374 insertions(+), 83 deletions(-) create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/CompletionContext.java create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequest.java create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequestContext.java create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultResponse.java create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/EmptyResponse.java create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Request.java create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Response.java diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/CompletionContext.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/CompletionContext.java new file mode 100644 index 00000000..78495d78 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/CompletionContext.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.client.loadbalancer; + +import org.springframework.core.style.ToStringCreator; + +/** + * @author Spencer Gibb + */ +// TODO: add metrics +public class CompletionContext { + + private final Status status; + + private final Throwable throwable; + + public CompletionContext(Status status) { + this(status, null); + } + + public CompletionContext(Status status, Throwable throwable) { + this.status = status; + this.throwable = throwable; + } + + public Status status() { + return this.status; + } + + public Throwable getThrowable() { + return this.throwable; + } + + @Override + public String toString() { + ToStringCreator to = new ToStringCreator(this); + to.append("status", this.status); + to.append("throwable", this.throwable); + return to.toString(); + } + + /** + * Request status state. + */ + public enum Status { + + /** Request was handled successfully. */ + SUCCESS, + /** Request reached the server but failed due to timeout or internal error. */ + FAILED, + /** Request did not go off box and should not be counted for statistics. */ + DISCARD, + + } + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequest.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequest.java new file mode 100644 index 00000000..1d4ae931 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.client.loadbalancer; + +/** + * A default implementation of {@link Request}. + * + * @author Spencer Gibb + * @author Olga Maciaszek-Sharma + */ +public class DefaultRequest implements Request { + + private T context; + + public DefaultRequest() { + new DefaultRequestContext(); + } + + public DefaultRequest(T context) { + this.context = context; + } + + @Override + public T getContext() { + return context; + } + + public void setContext(T context) { + this.context = context; + } + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequestContext.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequestContext.java new file mode 100644 index 00000000..4e6bc5e0 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequestContext.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.client.loadbalancer; + +/** + * Contains information relevant to the request. + * + * @author Olga Maciaszek-Sharma + */ +public class DefaultRequestContext { + + /** + * A {@link String} value of hint that can be used to choose the correct service + * instance. + */ + private String hint = "default"; + + public DefaultRequestContext() { + } + + public DefaultRequestContext(String hint) { + this.hint = hint; + } + + public String getHint() { + return hint; + } + + public void setHint(String hint) { + this.hint = hint; + } + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultResponse.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultResponse.java new file mode 100644 index 00000000..387bc332 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultResponse.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.client.loadbalancer; + +import org.springframework.cloud.client.ServiceInstance; + +/** + * @author Spencer Gibb + */ +public class DefaultResponse implements Response { + + private final ServiceInstance serviceInstance; + + public DefaultResponse(ServiceInstance serviceInstance) { + this.serviceInstance = serviceInstance; + } + + @Override + public boolean hasServer() { + return this.serviceInstance != null; + } + + @Override + public ServiceInstance getServer() { + return this.serviceInstance; + } + + @Override + public void onComplete(CompletionContext completionContext) { + // TODO: implement + } + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/EmptyResponse.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/EmptyResponse.java new file mode 100644 index 00000000..f0c3ed4c --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/EmptyResponse.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.client.loadbalancer; + +import org.springframework.cloud.client.ServiceInstance; + +/** + * @author Spencer Gibb + */ +public class EmptyResponse implements Response { + + @Override + public boolean hasServer() { + return false; + } + + @Override + public ServiceInstance getServer() { + return null; + } + + @Override + public void onComplete(CompletionContext completionContext) { + // TODO: implement + } + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Request.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Request.java new file mode 100644 index 00000000..f0657956 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Request.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.client.loadbalancer; + +/** + * Marker interface for a request. + * + * @author Spencer Gibb + * @author Olga Maciaszek-Sharma + */ +public interface Request { + + // Avoid breaking backward compatibility + default C getContext() { + return null; + } + + // TODO: define contents + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Response.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Response.java new file mode 100644 index 00000000..19c71662 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Response.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.client.loadbalancer; + +/** + * Response created for each request. + * + * @param type of the server + * @author Spencer Gibb + */ +public interface Response { + + boolean hasServer(); + + T getServer(); + + /** + * Notification that the request completed. + * @deprecated onComplete callbacks for load-balanced calls are being + * moved to a separate interface + * @param completionContext - completion context + */ + @Deprecated + void onComplete(CompletionContext completionContext); + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/CompletionContext.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/CompletionContext.java index ac25c19b..e149e3a8 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/CompletionContext.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/CompletionContext.java @@ -16,46 +16,46 @@ package org.springframework.cloud.client.loadbalancer.reactive; -import org.springframework.core.style.ToStringCreator; - /** + * @deprecated in favour of + * {@link org.springframework.cloud.client.loadbalancer.CompletionContext} * @author Spencer Gibb */ // TODO: add metrics -public class CompletionContext { +@Deprecated +public class CompletionContext + extends org.springframework.cloud.client.loadbalancer.CompletionContext { private final Status status; - private final Throwable throwable; - public CompletionContext(Status status) { this(status, null); } public CompletionContext(Status status, Throwable throwable) { + super(resolveStatus(status), throwable); this.status = status; - this.throwable = throwable; } public Status getStatus() { return this.status; } - public Throwable getThrowable() { - return this.throwable; - } - - @Override - public String toString() { - ToStringCreator to = new ToStringCreator(this); - to.append("status", this.status); - to.append("throwable", this.throwable); - return to.toString(); + private static org.springframework.cloud.client.loadbalancer.CompletionContext.Status resolveStatus( + Status status) { + if (Status.SUCCESSS.equals(status)) { + return org.springframework.cloud.client.loadbalancer.CompletionContext.Status.SUCCESS; + } + return org.springframework.cloud.client.loadbalancer.CompletionContext.Status + .valueOf(status.name()); } /** * Request status state. + * @deprecated in favour of + * {@link org.springframework.cloud.client.loadbalancer.CompletionContext.Status} */ + @Deprecated public enum Status { /** Request was handled successfully. */ diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequest.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequest.java index 19573c62..2ef94a84 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequest.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequest.java @@ -19,28 +19,21 @@ package org.springframework.cloud.client.loadbalancer.reactive; /** * A default implementation of {@link Request}. * + * @deprecated in favour of * @author Spencer Gibb * @author Olga Maciaszek-Sharma */ -public class DefaultRequest implements Request { - - private T context; +@Deprecated +public class DefaultRequest + extends org.springframework.cloud.client.loadbalancer.DefaultRequest + implements Request { public DefaultRequest() { new DefaultRequestContext(); } public DefaultRequest(T context) { - this.context = context; - } - - @Override - public T getContext() { - return context; - } - - public void setContext(T context) { - this.context = context; + super(context); } } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequestContext.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequestContext.java index e71f3da0..96f8fc7e 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequestContext.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequestContext.java @@ -19,29 +19,19 @@ package org.springframework.cloud.client.loadbalancer.reactive; /** * Contains information relevant to the request. * + * @deprecated in favour of + * {@link org.springframework.cloud.client.loadbalancer.DefaultRequestContext} * @author Olga Maciaszek-Sharma */ -public class DefaultRequestContext { - - /** - * A {@link String} value of hint that can be used to choose the correct service - * instance. - */ - private String hint = "default"; +@Deprecated +public class DefaultRequestContext + extends org.springframework.cloud.client.loadbalancer.DefaultRequestContext { public DefaultRequestContext() { } public DefaultRequestContext(String hint) { - this.hint = hint; - } - - public String getHint() { - return hint; - } - - public void setHint(String hint) { - this.hint = hint; + super(hint); } } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultResponse.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultResponse.java index 6fa4ee98..a34f716a 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultResponse.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultResponse.java @@ -19,24 +19,17 @@ package org.springframework.cloud.client.loadbalancer.reactive; import org.springframework.cloud.client.ServiceInstance; /** + * @deprecated in favour of + * {@link org.springframework.cloud.client.loadbalancer.DefaultResponse} * @author Spencer Gibb */ -public class DefaultResponse implements Response { - - private final ServiceInstance serviceInstance; +@Deprecated +public class DefaultResponse + extends org.springframework.cloud.client.loadbalancer.DefaultResponse + implements Response { public DefaultResponse(ServiceInstance serviceInstance) { - this.serviceInstance = serviceInstance; - } - - @Override - public boolean hasServer() { - return this.serviceInstance != null; - } - - @Override - public ServiceInstance getServer() { - return this.serviceInstance; + super(serviceInstance); } @Override diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/EmptyResponse.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/EmptyResponse.java index c2e381a7..6ce7483d 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/EmptyResponse.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/EmptyResponse.java @@ -19,19 +19,14 @@ package org.springframework.cloud.client.loadbalancer.reactive; import org.springframework.cloud.client.ServiceInstance; /** + * @deprecated in favour of + * {@link org.springframework.cloud.client.loadbalancer.EmptyResponse} * @author Spencer Gibb */ -public class EmptyResponse implements Response { - - @Override - public boolean hasServer() { - return false; - } - - @Override - public ServiceInstance getServer() { - return null; - } +@Deprecated +public class EmptyResponse + extends org.springframework.cloud.client.loadbalancer.EmptyResponse + implements Response { @Override public void onComplete(CompletionContext completionContext) { diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Request.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Request.java index cf6e3580..8a487386 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Request.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Request.java @@ -19,16 +19,12 @@ package org.springframework.cloud.client.loadbalancer.reactive; /** * Marker interface for a request. * + * @deprecated in favour of {@link org.springframework.cloud.client.loadbalancer.Request} * @author Spencer Gibb * @author Olga Maciaszek-Sharma */ -public interface Request { - - // Avoid breaking backward compatibility - default C getContext() { - return null; - } - - // TODO: define contents +@Deprecated +public interface Request + extends org.springframework.cloud.client.loadbalancer.Request { } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Response.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Response.java index 6857c421..db1e538e 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Response.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Response.java @@ -19,14 +19,13 @@ package org.springframework.cloud.client.loadbalancer.reactive; /** * Response created for each request. * + * @deprecated in favour of {@link org.springframework.cloud.client.loadbalancer.Response} * @param type of the server * @author Spencer Gibb */ -public interface Response { - - boolean hasServer(); - - T getServer(); +@Deprecated +public interface Response + extends org.springframework.cloud.client.loadbalancer.Response { /** * Notification that the request completed.