Search in sources :

Example 16 with InstanceStatus

use of com.netflix.appinfo.InstanceInfo.InstanceStatus in project eureka by Netflix.

the class AbstractInstanceRegistry method deleteStatusOverride.

/**
 * Removes status override for a give instance.
 *
 * @param appName the application name of the instance.
 * @param id the unique identifier of the instance.
 * @param newStatus the new {@link InstanceStatus}.
 * @param lastDirtyTimestamp last timestamp when this instance information was updated.
 * @param isReplication true if this is a replication event from other nodes, false
 *                      otherwise.
 * @return true if the status was successfully updated, false otherwise.
 */
@Override
public boolean deleteStatusOverride(String appName, String id, InstanceStatus newStatus, String lastDirtyTimestamp, boolean isReplication) {
    read.lock();
    try {
        STATUS_OVERRIDE_DELETE.increment(isReplication);
        Map<String, Lease<InstanceInfo>> gMap = registry.get(appName);
        Lease<InstanceInfo> lease = null;
        if (gMap != null) {
            lease = gMap.get(id);
        }
        if (lease == null) {
            return false;
        } else {
            lease.renew();
            InstanceInfo info = lease.getHolder();
            // This log statement is provided as a safeguard, in case this invariant is violated.
            if (info == null) {
                logger.error("Found Lease without a holder for instance id {}", id);
            }
            InstanceStatus currentOverride = overriddenInstanceStatusMap.remove(id);
            if (currentOverride != null && info != null) {
                info.setOverriddenStatus(InstanceStatus.UNKNOWN);
                info.setStatusWithoutDirty(newStatus);
                long replicaDirtyTimestamp = 0;
                if (lastDirtyTimestamp != null) {
                    replicaDirtyTimestamp = Long.parseLong(lastDirtyTimestamp);
                }
                // it to the replica's.
                if (replicaDirtyTimestamp > info.getLastDirtyTimestamp()) {
                    info.setLastDirtyTimestamp(replicaDirtyTimestamp);
                }
                info.setActionType(ActionType.MODIFIED);
                recentlyChangedQueue.add(new RecentlyChangedItem(lease));
                info.setLastUpdatedTimestamp();
                invalidateCache(appName, info.getVIPAddress(), info.getSecureVipAddress());
            }
            return true;
        }
    } finally {
        read.unlock();
    }
}
Also used : Lease(com.netflix.eureka.lease.Lease) InstanceStatus(com.netflix.appinfo.InstanceInfo.InstanceStatus) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 17 with InstanceStatus

use of com.netflix.appinfo.InstanceInfo.InstanceStatus in project eureka by Netflix.

the class EurekaConfigBasedInstanceInfoProvider method get.

@Override
public synchronized InstanceInfo get() {
    if (instanceInfo == null) {
        // Build the lease information to be passed to the server based on config
        LeaseInfo.Builder leaseInfoBuilder = LeaseInfo.Builder.newBuilder().setRenewalIntervalInSecs(config.getLeaseRenewalIntervalInSeconds()).setDurationInSecs(config.getLeaseExpirationDurationInSeconds());
        if (vipAddressResolver == null) {
            vipAddressResolver = new Archaius1VipAddressResolver();
        }
        // Builder the instance information to be registered with eureka server
        InstanceInfo.Builder builder = InstanceInfo.Builder.newBuilder(vipAddressResolver);
        // set the appropriate id for the InstanceInfo, falling back to datacenter Id if applicable, else hostname
        String instanceId = config.getInstanceId();
        if (instanceId == null || instanceId.isEmpty()) {
            DataCenterInfo dataCenterInfo = config.getDataCenterInfo();
            if (dataCenterInfo instanceof UniqueIdentifier) {
                instanceId = ((UniqueIdentifier) dataCenterInfo).getId();
            } else {
                instanceId = config.getHostName(false);
            }
        }
        String defaultAddress;
        if (config instanceof RefreshableInstanceConfig) {
            // Refresh AWS data center info, and return up to date address
            defaultAddress = ((RefreshableInstanceConfig) config).resolveDefaultAddress(false);
        } else {
            defaultAddress = config.getHostName(false);
        }
        // fail safe
        if (defaultAddress == null || defaultAddress.isEmpty()) {
            defaultAddress = config.getIpAddress();
        }
        builder.setNamespace(config.getNamespace()).setInstanceId(instanceId).setAppName(config.getAppname()).setAppGroupName(config.getAppGroupName()).setDataCenterInfo(config.getDataCenterInfo()).setIPAddr(config.getIpAddress()).setHostName(defaultAddress).setPort(config.getNonSecurePort()).enablePort(PortType.UNSECURE, config.isNonSecurePortEnabled()).setSecurePort(config.getSecurePort()).enablePort(PortType.SECURE, config.getSecurePortEnabled()).setVIPAddress(config.getVirtualHostName()).setSecureVIPAddress(config.getSecureVirtualHostName()).setHomePageUrl(config.getHomePageUrlPath(), config.getHomePageUrl()).setStatusPageUrl(config.getStatusPageUrlPath(), config.getStatusPageUrl()).setASGName(config.getASGName()).setHealthCheckUrls(config.getHealthCheckUrlPath(), config.getHealthCheckUrl(), config.getSecureHealthCheckUrl());
        // Start off with the STARTING state to avoid traffic
        if (!config.isInstanceEnabledOnit()) {
            InstanceStatus initialStatus = InstanceStatus.STARTING;
            LOG.info("Setting initial instance status as: {}", initialStatus);
            builder.setStatus(initialStatus);
        } else {
            LOG.info("Setting initial instance status as: {}. This may be too early for the instance to advertise " + "itself as available. You would instead want to control this via a healthcheck handler.", InstanceStatus.UP);
        }
        // Add any user-specific metadata information
        for (Map.Entry<String, String> mapEntry : config.getMetadataMap().entrySet()) {
            String key = mapEntry.getKey();
            String value = mapEntry.getValue();
            // only add the metadata if the value is present
            if (value != null && !value.isEmpty()) {
                builder.add(key, value);
            }
        }
        instanceInfo = builder.build();
        instanceInfo.setLeaseInfo(leaseInfoBuilder.build());
    }
    return instanceInfo;
}
Also used : UniqueIdentifier(com.netflix.appinfo.UniqueIdentifier) InstanceStatus(com.netflix.appinfo.InstanceInfo.InstanceStatus) DataCenterInfo(com.netflix.appinfo.DataCenterInfo) RefreshableInstanceConfig(com.netflix.appinfo.RefreshableInstanceConfig) LeaseInfo(com.netflix.appinfo.LeaseInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) Map(java.util.Map)

Example 18 with InstanceStatus

use of com.netflix.appinfo.InstanceInfo.InstanceStatus in project eureka by Netflix.

the class ApplicationInfoManager method setInstanceStatus.

/**
 * Set the status of this instance. Application can use this to indicate
 * whether it is ready to receive traffic. Setting the status here also notifies all registered listeners
 * of a status change event.
 *
 * @param status Status of the instance
 */
public synchronized void setInstanceStatus(InstanceStatus status) {
    InstanceStatus next = instanceStatusMapper.map(status);
    if (next == null) {
        return;
    }
    InstanceStatus prev = instanceInfo.setStatus(next);
    if (prev != null) {
        for (StatusChangeListener listener : listeners.values()) {
            try {
                listener.notify(new StatusChangeEvent(prev, next));
            } catch (Exception e) {
                logger.warn("failed to notify listener: {}", listener.getId(), e);
            }
        }
    }
}
Also used : StatusChangeEvent(com.netflix.discovery.StatusChangeEvent) InstanceStatus(com.netflix.appinfo.InstanceInfo.InstanceStatus)

Example 19 with InstanceStatus

use of com.netflix.appinfo.InstanceInfo.InstanceStatus in project eureka by Netflix.

the class InstanceResource method deleteStatusUpdate.

/**
 * Removes status override for an instance, set with
 * {@link #statusUpdate(String, String, String)}.
 *
 * @param isReplication
 *            a header parameter containing information whether this is
 *            replicated from other nodes.
 * @param lastDirtyTimestamp
 *            last timestamp when this instance information was updated.
 * @return response indicating whether the operation was a success or
 *         failure.
 */
@DELETE
@Path("status")
public Response deleteStatusUpdate(@HeaderParam(PeerEurekaNode.HEADER_REPLICATION) String isReplication, @QueryParam("value") String newStatusValue, @QueryParam("lastDirtyTimestamp") String lastDirtyTimestamp) {
    try {
        if (registry.getInstanceByAppAndId(app.getName(), id) == null) {
            logger.warn("Instance not found: {}/{}", app.getName(), id);
            return Response.status(Status.NOT_FOUND).build();
        }
        InstanceStatus newStatus = newStatusValue == null ? InstanceStatus.UNKNOWN : InstanceStatus.valueOf(newStatusValue);
        boolean isSuccess = registry.deleteStatusOverride(app.getName(), id, newStatus, lastDirtyTimestamp, "true".equals(isReplication));
        if (isSuccess) {
            logger.info("Status override removed: {} - {}", app.getName(), id);
            return Response.ok().build();
        } else {
            logger.warn("Unable to remove status override: {} - {}", app.getName(), id);
            return Response.serverError().build();
        }
    } catch (Throwable e) {
        logger.error("Error removing instance's {} status override", id);
        return Response.serverError().build();
    }
}
Also used : InstanceStatus(com.netflix.appinfo.InstanceInfo.InstanceStatus) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Aggregations

InstanceStatus (com.netflix.appinfo.InstanceInfo.InstanceStatus)19 InstanceInfo (com.netflix.appinfo.InstanceInfo)10 Test (org.junit.Test)5 Lease (com.netflix.eureka.lease.Lease)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)1 LeaseInfo (com.netflix.appinfo.LeaseInfo)1 RefreshableInstanceConfig (com.netflix.appinfo.RefreshableInstanceConfig)1 UniqueIdentifier (com.netflix.appinfo.UniqueIdentifier)1 StatusChangeEvent (com.netflix.discovery.StatusChangeEvent)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1