Search in sources :

Example 91 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project coffeenet-starter by coffeenet.

the class IntegrationCoffeeNetAppServiceTest method getServiceInstance.

private EurekaServiceInstance getServiceInstance(String name, String roles) {
    InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName(name).setVIPAddress(name).build();
    if (roles != null) {
        instanceInfo.getMetadata().put("allowedAuthorities", roles);
    }
    EurekaServiceInstance eurekaServiceInstance = mock(EurekaServiceInstance.class);
    when(eurekaServiceInstance.getInstanceInfo()).thenReturn(instanceInfo);
    return eurekaServiceInstance;
}
Also used : EurekaServiceInstance(org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient.EurekaServiceInstance) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 92 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project riposte by Nike-Inc.

the class EurekaVipAddressRoundRobinService method getActiveInstanceInfoForVipAddressBlocking.

/**
     * Round-robins the instances returned by {@link #getActiveInstanceInfosForVipAddressBlocking(String)} to determine
     * the *next* active {@link InstanceInfo} that should be called for the given VIP name
     *
     * @return The *next* active {@link InstanceInfo} that should be called for the given VIP name (using a round-robin
     * strategy), or an empty Optional if no active instances were found.
     */
public Optional<InstanceInfo> getActiveInstanceInfoForVipAddressBlocking(String vipName) {
    List<InstanceInfo> instancesByVipAddress = getActiveInstanceInfosForVipAddressBlocking(vipName);
    if (instancesByVipAddress.isEmpty())
        return Optional.empty();
    // Found at least one "up" instance at this VIP. Grab the AtomicInteger associated with this VIP
    //      (map a new one if necessary).
    AtomicInteger roundRobinCounter = vipRoundRobinCounterMap.computeIfAbsent(vipName, vip -> new AtomicInteger(0));
    // Atomically get-and-increment the atomic int associated with this VIP, then mod it against the number of
    //      instances available. This effectively round robins the use of all the instances associated with the VIP.
    int instanceIndexToUse = roundRobinCounter.getAndIncrement() % instancesByVipAddress.size();
    if (instanceIndexToUse < 0) {
        // The counter went high enough to do an integer overflow. Fix the index so we don't blow up this call,
        //      and reset the counter to 0.
        instanceIndexToUse = Math.abs(instanceIndexToUse);
        roundRobinCounter.set(0);
    }
    return Optional.of(instancesByVipAddress.get(instanceIndexToUse));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 93 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project riposte by Nike-Inc.

the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfoForVipAddress_returns_future_that_returns_data_from_getActiveInstanceInfoForVipAddressBlocking.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getActiveInstanceInfoForVipAddress_returns_future_that_returns_data_from_getActiveInstanceInfoForVipAddressBlocking(boolean useExecutor) {
    // given
    InstanceInfo iiMock = mock(InstanceInfo.class);
    doReturn(Optional.of(iiMock)).when(serviceSpy).getActiveInstanceInfoForVipAddressBlocking(vip);
    Optional<Executor> executorOpt = useExecutor ? Optional.of(Executors.newSingleThreadExecutor()) : Optional.empty();
    // when
    CompletableFuture<Optional<InstanceInfo>> result = serviceSpy.getActiveInstanceInfoForVipAddress(vip, executorOpt);
    // then
    assertThat(result.join()).isEqualTo(Optional.of(iiMock));
}
Also used : Executor(java.util.concurrent.Executor) Optional(java.util.Optional) InstanceInfo(com.netflix.appinfo.InstanceInfo) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 94 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project riposte by Nike-Inc.

the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfoForVipAddressBlocking_returns_empty_if_no_instances_are_active.

@Test
public void getActiveInstanceInfoForVipAddressBlocking_returns_empty_if_no_instances_are_active() {
    // given
    InstanceInfo down = mock(InstanceInfo.class);
    doReturn(InstanceInfo.InstanceStatus.DOWN).when(down).getStatus();
    List<InstanceInfo> allInstancesForVip = Collections.singletonList(down);
    doReturn(allInstancesForVip).when(discoveryClientMock).getInstancesByVipAddress(vip, false);
    // when
    Optional<InstanceInfo> result = serviceSpy.getActiveInstanceInfoForVipAddressBlocking(vip);
    // then
    assertThat(result).isEmpty();
}
Also used : InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 95 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project riposte by Nike-Inc.

the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfosForVipAddress_uses_provided_executor_for_future_if_provided_one.

@Test
public void getActiveInstanceInfosForVipAddress_uses_provided_executor_for_future_if_provided_one() {
    // given
    InstanceInfo iiMock = mock(InstanceInfo.class);
    List<InstanceInfo> expectedInstances = Collections.singletonList(iiMock);
    doReturn(expectedInstances).when(serviceSpy).getActiveInstanceInfosForVipAddressBlocking(vip);
    Executor executorSpy = spy(new SpyableExecutor());
    // when
    CompletableFuture<List<InstanceInfo>> result = serviceSpy.getActiveInstanceInfosForVipAddress(vip, Optional.of(executorSpy));
    // then
    assertThat(result.join()).isEqualTo(expectedInstances);
    verify(executorSpy).execute(any(Runnable.class));
}
Also used : Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) List(java.util.List) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Aggregations

InstanceInfo (com.netflix.appinfo.InstanceInfo)192 Test (org.junit.Test)74 Application (com.netflix.discovery.shared.Application)35 Applications (com.netflix.discovery.shared.Applications)22 AmazonInfo (com.netflix.appinfo.AmazonInfo)21 ArrayList (java.util.ArrayList)19 InstanceStatus (com.netflix.appinfo.InstanceInfo.InstanceStatus)11 Before (org.junit.Before)11 DiscoveryClient (com.netflix.discovery.DiscoveryClient)10 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)10 Lease (com.netflix.eureka.lease.Lease)9 HashMap (java.util.HashMap)8 Response (javax.ws.rs.core.Response)8 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)7 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)7 EncoderWrapper (com.netflix.discovery.converters.wrappers.EncoderWrapper)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)6 EurekaEntityFunctions.mergeApplications (com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6