Skip to content

Commit

Permalink
Correct the approach
Browse files Browse the repository at this point in the history
Related: #6056

Motivation:

Users might want to use the metadata from the Eureka `InstanceInfo` but currently, there's no way to retrieve it.

Modifications:

- Remove helper class because users cannot access this class to retrieve `InstanceInfo`.
- Move `com.linecorp.armeria.internal.common.eureka.InstanceInfo` to `com.linecorp.armeria.common.eureka.InstanceInfo`.
- Move the methods to the `InstanceInfo` class.
- Add a test to verify that users can retrieve the `InstanceInfo`.

Result:

- Closes #6056
- Now users can retrieve it.
  • Loading branch information
Ivan-Montes committed Jan 14, 2025
1 parent 67e0c88 commit b99a0b0
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.eureka.Application;
import com.linecorp.armeria.internal.common.eureka.Applications;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo.InstanceStatus;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo.PortWrapper;
import com.linecorp.armeria.common.eureka.InstanceInfo;
import com.linecorp.armeria.common.eureka.InstanceInfo.InstanceStatus;
import com.linecorp.armeria.common.eureka.InstanceInfo.PortWrapper;
import com.linecorp.armeria.server.eureka.EurekaUpdatingListener;

import io.netty.channel.EventLoop;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.ScheduledFuture;

/**
Expand Down Expand Up @@ -400,24 +399,6 @@ public List<Endpoint> apply(byte[] content) {
}
}

private static final class EurekaInstanceInfoUtil {

private static final AttributeKey<InstanceInfo> INSTANCE_INFO = AttributeKey.valueOf(
EurekaInstanceInfoUtil.class, "INSTANCE_INFO");

@Nullable
static InstanceInfo get(Endpoint endpoint) {
requireNonNull(endpoint, "endpoint");
return endpoint.attr(INSTANCE_INFO);
}

static Endpoint with(Endpoint endpoint, InstanceInfo instanceInfo) {
requireNonNull(endpoint, "endpoint");
requireNonNull(instanceInfo, "instanceInfo");
return endpoint.withAttr(INSTANCE_INFO, instanceInfo);
}
}

private static Endpoint endpoint(InstanceInfo instanceInfo, boolean secureVip) {
final String hostname = instanceInfo.getHostName();
final PortWrapper portWrapper = instanceInfo.getPort();
Expand All @@ -434,7 +415,7 @@ private static Endpoint endpoint(InstanceInfo instanceInfo, boolean secureVip) {
if (ipAddr != null && hostname != ipAddr) {
endpoint = endpoint.withIpAddr(ipAddr);
}
return EurekaInstanceInfoUtil.with(endpoint, instanceInfo);
return InstanceInfo.with(endpoint, instanceInfo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.internal.common.eureka;
package com.linecorp.armeria.common.eureka;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;
Expand All @@ -32,7 +32,12 @@
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;

import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.eureka.DataCenterInfo;
import com.linecorp.armeria.internal.common.eureka.LeaseInfo;

import io.netty.util.AttributeKey;

/**
* An instance information.
Expand All @@ -43,6 +48,33 @@ public final class InstanceInfo {

private static final Logger logger = LoggerFactory.getLogger(InstanceInfo.class);

private static final AttributeKey<InstanceInfo> INSTANCE_INFO = AttributeKey.valueOf(
InstanceInfo.class, "INSTANCE_INFO");

/**
* Returns the {@link InstanceInfo} associated with the given {@link Endpoint}.
*
* @param endpoint The {@link Endpoint} whose {@link InstanceInfo} is to be retrieved.
* @return The {@link InstanceInfo} associated with the specified {@link Endpoint}.
*/
@Nullable
public static InstanceInfo get(Endpoint endpoint) {
requireNonNull(endpoint, "endpoint");
return endpoint.attr(INSTANCE_INFO);
}

/**
* Sets the Eureka {@link InstanceInfo} parameter to the {@link Endpoint} as an attribute.
*
* @param endpoint The {@link Endpoint} to which the {@link InstanceInfo} will be set as an attribute.
* @return The same {@link Endpoint} passed as a parameter.
*/
public static Endpoint with(Endpoint endpoint, InstanceInfo instanceInfo) {
requireNonNull(endpoint, "endpoint");
requireNonNull(instanceInfo, "instanceInfo");
return endpoint.withAttr(INSTANCE_INFO, instanceInfo);
}

@Nullable
private final String instanceId;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020 LINE Corporation
*
* LINE Corporation 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:
*
* 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.
*/
/**
* <a href="https://github.com/Netflix/eureka/">Eureka</a>-related common classes.
*/
@NonNullByDefault
package com.linecorp.armeria.common.eureka;

import com.linecorp.armeria.common.annotation.NonNullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.fasterxml.jackson.annotation.JsonRootName;
import com.google.common.collect.ImmutableSet;

import com.linecorp.armeria.common.eureka.InstanceInfo;

/**
* An application.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import com.linecorp.armeria.common.QueryParamsBuilder;
import com.linecorp.armeria.common.RequestHeaders;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo.InstanceStatus;
import com.linecorp.armeria.common.eureka.InstanceInfo;
import com.linecorp.armeria.common.eureka.InstanceInfo.InstanceStatus;

/**
* A Eureka {@link WebClient} which communicates to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.SystemInfo;
import com.linecorp.armeria.internal.common.eureka.EurekaWebClient;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo.InstanceStatus;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo.PortWrapper;
import com.linecorp.armeria.common.eureka.InstanceInfo;
import com.linecorp.armeria.common.eureka.InstanceInfo.InstanceStatus;
import com.linecorp.armeria.common.eureka.InstanceInfo.PortWrapper;
import com.linecorp.armeria.server.Route;
import com.linecorp.armeria.server.RoutePathType;
import com.linecorp.armeria.server.Server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.eureka.DataCenterInfo;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo.InstanceStatus;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo.PortWrapper;
import com.linecorp.armeria.common.eureka.InstanceInfo;
import com.linecorp.armeria.common.eureka.InstanceInfo.InstanceStatus;
import com.linecorp.armeria.common.eureka.InstanceInfo.PortWrapper;
import com.linecorp.armeria.internal.common.eureka.LeaseInfo;

import io.netty.util.NetUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.auth.AuthToken;
import com.linecorp.armeria.internal.common.eureka.EurekaWebClient;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo.PortWrapper;
import com.linecorp.armeria.common.eureka.InstanceInfo.PortWrapper;

import jakarta.ws.rs.core.MediaType;

Expand Down Expand Up @@ -118,19 +118,19 @@ public EurekaHttpResponse<Void> register(InstanceInfo info) {
return convertVoidResponse(delegate.register(convertInstanceInfo(info)));
}

private static com.linecorp.armeria.internal.common.eureka.InstanceInfo convertInstanceInfo(
private static com.linecorp.armeria.common.eureka.InstanceInfo convertInstanceInfo(
InstanceInfo info) {
final PortWrapper port = new PortWrapper(info.isPortEnabled(PortType.UNSECURE), info.getPort());
final PortWrapper securePort = new PortWrapper(info.isPortEnabled(PortType.SECURE),
info.getSecurePort());

return new com.linecorp.armeria.internal.common.eureka.InstanceInfo(
return new com.linecorp.armeria.common.eureka.InstanceInfo(
info.getInstanceId(),
info.getAppName(), info.getAppGroupName(), info.getHostName(),
info.getIPAddr(),
info.getVIPAddress(), info.getSecureVipAddress(), port,
securePort,
com.linecorp.armeria.internal.common.eureka.InstanceInfo.InstanceStatus
com.linecorp.armeria.common.eureka.InstanceInfo.InstanceStatus
.toEnum(info.getStatus().name()), info.getHomePageUrl(),
info.getStatusPageUrl(),
info.getHealthCheckUrl(),
Expand Down Expand Up @@ -162,7 +162,7 @@ public EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id,
return convertResponse(delegate.sendHeartBeat(
appName, id, convertInstanceInfo(info),
overriddenStatus == null ?
null : com.linecorp.armeria.internal.common.eureka.InstanceInfo.InstanceStatus
null : com.linecorp.armeria.common.eureka.InstanceInfo.InstanceStatus
.toEnum(overriddenStatus.name())),
InstanceInfo.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.linecorp.armeria.common.eureka;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.common.eureka.InstanceInfo.InstanceStatus;
import com.linecorp.armeria.common.eureka.InstanceInfo.PortWrapper;
import com.linecorp.armeria.internal.common.eureka.DataCenterInfo;
import com.linecorp.armeria.internal.common.eureka.LeaseInfo;

class InstanceInfoTest {

@Test
void getShouldReturnAssociatedInstanceInfo() {

final String instanceId = "123";
final String appName = "myApp";
final String appGroupName = "myGroup";
final String hostName = "myHost";
final String ipAddr = "192.168.1.1";
final String vipAddress = "10.0.0.1";
final String secureVipAddress = "10.0.0.2";
final PortWrapper port = new PortWrapper(true,80);
final PortWrapper securePort = new PortWrapper(true,443);
final InstanceInfo. InstanceStatus status = InstanceStatus.UP;
final String homePageUrl = "https://example.com";
final String statusPageUrl = "https://status.example.com";
final String healthCheckUrl = "/health";
final String secureHealthCheckUrl = "/secure/health";
final DataCenterInfo dataCenterInfo = new DataCenterInfo("name",new HashMap<>());
final LeaseInfo leaseInfo = new LeaseInfo(30,90);
final Map<String, String> metadata = new HashMap<>();
final InstanceInfo instanceInfo = new InstanceInfo(
instanceId,
appName,
appGroupName,
hostName,
ipAddr,
vipAddress,
secureVipAddress,
port,
securePort,
status,
homePageUrl,
statusPageUrl,
healthCheckUrl,
secureHealthCheckUrl,
dataCenterInfo,
leaseInfo,
metadata
);
final Endpoint endpoint = Endpoint.parse("foo");

final Endpoint endpointWith = InstanceInfo.with(endpoint, instanceInfo);

final InstanceInfo instanceInfoRetrieved = InstanceInfo.get(endpointWith);
assertThat(instanceInfoRetrieved).isNotNull();
assertThat(instanceInfoRetrieved).isSameAs(instanceInfo);
assertThat(instanceInfoRetrieved.getInstanceId()).isEqualTo(instanceId);
assertThat(instanceInfoRetrieved.getAppName()).isEqualTo(appName);
assertThat(instanceInfoRetrieved.getAppGroupName()).isEqualTo(appGroupName);
assertThat(instanceInfoRetrieved.getHostName()).isEqualTo(hostName);
assertThat(instanceInfoRetrieved.getIpAddr()).isEqualTo(ipAddr);
assertThat(instanceInfoRetrieved.getVipAddress()).isEqualTo(vipAddress);
assertThat(instanceInfoRetrieved.getSecureVipAddress()).isEqualTo(secureVipAddress);
assertThat(instanceInfoRetrieved.getPort()).isEqualTo(port);
assertThat(instanceInfoRetrieved.getSecurePort()).isEqualTo(securePort);
assertThat(instanceInfoRetrieved.getStatus()).isEqualTo(status);
assertThat(instanceInfoRetrieved.getHomePageUrl()).isEqualTo(homePageUrl);
assertThat(instanceInfoRetrieved.getStatusPageUrl()).isEqualTo(statusPageUrl);
assertThat(instanceInfoRetrieved.getHealthCheckUrl()).isEqualTo(healthCheckUrl);
assertThat(instanceInfoRetrieved.getSecureHealthCheckUrl()).isEqualTo(secureHealthCheckUrl);
assertThat(instanceInfoRetrieved.getDataCenterInfo()).isEqualTo(dataCenterInfo);
assertThat(instanceInfoRetrieved.getLeaseInfo()).isEqualTo(leaseInfo);
assertThat(instanceInfoRetrieved.getMetadata()).isEqualTo(metadata);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import com.linecorp.armeria.common.RequestHeaders;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.util.SystemInfo;
import com.linecorp.armeria.internal.common.eureka.InstanceInfo;
import com.linecorp.armeria.common.eureka.InstanceInfo;
import com.linecorp.armeria.internal.testing.GenerateNativeImageTrace;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;
Expand Down

0 comments on commit b99a0b0

Please sign in to comment.