use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class RemoteRegionRegistry method fetchAndStoreDelta.
private boolean fetchAndStoreDelta() throws Throwable {
long currDeltaGeneration = deltaGeneration.get();
Applications delta = fetchRemoteRegistry(true);
if (delta == null) {
logger.error("The delta is null for some reason. Not storing this information");
} else if (deltaGeneration.compareAndSet(currDeltaGeneration, currDeltaGeneration + 1)) {
this.applicationsDelta.set(delta);
} else {
// set the delta to null so we don't use it
delta = null;
logger.warn("Not updating delta as another thread is updating it already");
}
if (delta == null) {
logger.warn("The server does not allow the delta revision to be applied because it is not " + "safe. Hence got the full registry.");
return storeFullRegistry();
} else {
updateDelta(delta);
String reconcileHashCode = getApplications().getReconcileHashCode();
// There is a diff in number of instances for some reason
if ((!reconcileHashCode.equals(delta.getAppsHashCode()))) {
return reconcileAndLogDifference(delta, reconcileHashCode);
}
}
return delta != null;
}
use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class RemoteRegionRegistry method storeFullRegistry.
/**
* Gets the full registry information from the eureka server and stores it
* locally.
*
* @return the full registry information.
*/
public boolean storeFullRegistry() {
long currentUpdateGeneration = fullRegistryGeneration.get();
Applications apps = fetchRemoteRegistry(false);
if (apps == null) {
logger.error("The application is null for some reason. Not storing this information");
} else if (fullRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
applications.set(apps);
logger.info("Successfully updated registry with the latest content");
return true;
} else {
logger.warn("Not updating applications as another thread is updating it already");
}
return false;
}
use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class RemoteRegionRegistry method fetchRemoteRegistry.
/**
* Fetch registry information from the remote region.
* @param delta - true, if the fetch needs to get deltas, false otherwise
* @return - response which has information about the data.
*/
private Applications fetchRemoteRegistry(boolean delta) {
logger.info("Getting instance registry info from the eureka server : {} , delta : {}", this.remoteRegionURL, delta);
if (shouldUseExperimentalTransport()) {
try {
EurekaHttpResponse<Applications> httpResponse = delta ? eurekaHttpClient.getDelta() : eurekaHttpClient.getApplications();
int httpStatus = httpResponse.getStatusCode();
if (httpStatus >= 200 && httpStatus < 300) {
logger.debug("Got the data successfully : {}", httpStatus);
return httpResponse.getEntity();
}
logger.warn("Cannot get the data from {} : {}", this.remoteRegionURL, httpStatus);
} catch (Throwable t) {
logger.error("Can't get a response from " + this.remoteRegionURL, t);
}
} else {
ClientResponse response = null;
try {
String urlPath = delta ? "apps/delta" : "apps/";
response = discoveryApacheClient.resource(this.remoteRegionURL + urlPath).accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
int httpStatus = response.getStatus();
if (httpStatus >= 200 && httpStatus < 300) {
logger.debug("Got the data successfully : {}", httpStatus);
return response.getEntity(Applications.class);
}
logger.warn("Cannot get the data from {} : {}", this.remoteRegionURL, httpStatus);
} catch (Throwable t) {
logger.error("Can't get a response from " + this.remoteRegionURL, t);
} finally {
closeResponse(response);
}
}
return null;
}
use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class ResponseCacheImpl method getApplicationsForVip.
private static Applications getApplicationsForVip(Key key, AbstractInstanceRegistry registry) {
Object[] args = { key.getEntityType(), key.getName(), key.getVersion(), key.getType() };
logger.debug("Retrieving applications from registry for key : {} {} {} {}", args);
Applications toReturn = new Applications();
Applications applications = registry.getApplications();
for (Application application : applications.getRegisteredApplications()) {
Application appToAdd = null;
for (InstanceInfo instanceInfo : application.getInstances()) {
String vipAddress;
if (Key.EntityType.VIP.equals(key.getEntityType())) {
vipAddress = instanceInfo.getVIPAddress();
} else if (Key.EntityType.SVIP.equals(key.getEntityType())) {
vipAddress = instanceInfo.getSecureVipAddress();
} else {
// should not happen, but just in case.
continue;
}
if (null != vipAddress) {
String[] vipAddresses = vipAddress.split(",");
Arrays.sort(vipAddresses);
if (Arrays.binarySearch(vipAddresses, key.getName()) >= 0) {
if (null == appToAdd) {
appToAdd = new Application(application.getName());
toReturn.addApplication(appToAdd);
}
appToAdd.addInstance(instanceInfo);
}
}
}
}
toReturn.setAppsHashCode(toReturn.getReconcileHashCode());
args = new Object[] { key.getEntityType(), key.getName(), key.getVersion(), key.getType(), toReturn.getReconcileHashCode() };
logger.debug("Retrieved applications from registry for key : {} {} {} {}, reconcile hashcode: {}", args);
return toReturn;
}
use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class AbstractInstanceRegistry method getApplicationDeltasFromMultipleRegions.
/**
* Gets the application delta also including instances from the passed remote regions, with the instances from the
* local region. <br/>
*
* The remote regions from where the instances will be chosen can further be restricted if this application does not
* appear in the whitelist specified for the region as returned by
* {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)} for a region. In case, there is no whitelist
* defined for a region, this method will also look for a global whitelist by passing <code>null</code> to the
* method {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)} <br/>
*
* @param remoteRegions The remote regions for which the instances are to be queried. The instances may be limited
* by a whitelist as explained above. If <code>null</code> all remote regions are included.
* If empty list then no remote region is included.
*
* @return The delta with instances from the passed remote regions as well as local region. The instances
* from remote regions can be further be restricted as explained above. <code>null</code> if the application does
* not exist locally or in remote regions.
*/
public Applications getApplicationDeltasFromMultipleRegions(String[] remoteRegions) {
if (null == remoteRegions) {
// null means all remote regions.
remoteRegions = allKnownRemoteRegions;
}
boolean includeRemoteRegion = remoteRegions.length != 0;
if (includeRemoteRegion) {
GET_ALL_WITH_REMOTE_REGIONS_CACHE_MISS_DELTA.increment();
} else {
GET_ALL_CACHE_MISS_DELTA.increment();
}
Applications apps = new Applications();
apps.setVersion(responseCache.getVersionDeltaWithRegions().get());
Map<String, Application> applicationInstancesMap = new HashMap<String, Application>();
try {
write.lock();
Iterator<RecentlyChangedItem> iter = this.recentlyChangedQueue.iterator();
logger.debug("The number of elements in the delta queue is :" + this.recentlyChangedQueue.size());
while (iter.hasNext()) {
Lease<InstanceInfo> lease = iter.next().getLeaseInfo();
InstanceInfo instanceInfo = lease.getHolder();
Object[] args = { instanceInfo.getId(), instanceInfo.getStatus().name(), instanceInfo.getActionType().name() };
logger.debug("The instance id %s is found with status %s and actiontype %s", args);
Application app = applicationInstancesMap.get(instanceInfo.getAppName());
if (app == null) {
app = new Application(instanceInfo.getAppName());
applicationInstancesMap.put(instanceInfo.getAppName(), app);
apps.addApplication(app);
}
app.addInstance(decorateInstanceInfo(lease));
}
if (includeRemoteRegion) {
for (String remoteRegion : remoteRegions) {
RemoteRegionRegistry remoteRegistry = regionNameVSRemoteRegistry.get(remoteRegion);
if (null != remoteRegistry) {
Applications remoteAppsDelta = remoteRegistry.getApplicationDeltas();
if (null != remoteAppsDelta) {
for (Application application : remoteAppsDelta.getRegisteredApplications()) {
if (shouldFetchFromRemoteRegistry(application.getName(), remoteRegion)) {
Application appInstanceTillNow = apps.getRegisteredApplications(application.getName());
if (appInstanceTillNow == null) {
appInstanceTillNow = new Application(application.getName());
apps.addApplication(appInstanceTillNow);
}
for (InstanceInfo instanceInfo : application.getInstances()) {
appInstanceTillNow.addInstance(instanceInfo);
}
}
}
}
}
}
}
Applications allApps = getApplicationsFromMultipleRegions(remoteRegions);
apps.setAppsHashCode(allApps.getReconcileHashCode());
return apps;
} finally {
write.unlock();
}
}
Aggregations