protected void initRemoteRegionRegistry() throws MalformedURLException { Map<String, String> remoteRegionUrlsWithName = serverConfig.getRemoteRegionUrlsWithName(); if (!remoteRegionUrlsWithName.isEmpty()) { allKnownRemoteRegions = new String[remoteRegionUrlsWithName.size()]; int remoteRegionArrayIndex = 0; for (Map.Entry<String, String> remoteRegionUrlWithName : remoteRegionUrlsWithName.entrySet()) { RemoteRegionRegistry remoteRegionRegistry = new RemoteRegionRegistry( serverConfig, clientConfig, serverCodecs, remoteRegionUrlWithName.getKey(), new URL(remoteRegionUrlWithName.getValue())); regionNameVSRemoteRegistry.put(remoteRegionUrlWithName.getKey(), remoteRegionRegistry); allKnownRemoteRegions[remoteRegionArrayIndex++] = remoteRegionUrlWithName.getKey(); } } logger.info("Finished initializing remote region registries. All known remote regions: {}", (Object) allKnownRemoteRegions); }
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(); logger.error("Can't get a response from {}", this.remoteRegionURL, t); } finally { closeResponse(response);
/** * Fetch the registry information from the remote region. * @return true, if the fetch was successful, false otherwise. */ private boolean fetchRegistry() { boolean success; Stopwatch tracer = fetchRegistryTimer.start(); try { // If the delta is disabled or if it is the first time, get all applications if (serverConfig.shouldDisableDeltaForRemoteRegions() || (getApplications() == null) || (getApplications().getRegisteredApplications().size() == 0)) { logger.info("Disable delta property : {}", serverConfig.shouldDisableDeltaForRemoteRegions()); logger.info("Application is null : {}", getApplications() == null); logger.info("Registered Applications size is zero : {}", getApplications().getRegisteredApplications().isEmpty()); success = storeFullRegistry(); } else { success = fetchAndStoreDelta(); } logTotalInstances(); } catch (Throwable e) { logger.error("Unable to fetch registry information from the remote registry {}", this.remoteRegionURL, e); return false; } finally { if (tracer != null) { tracer.stop(); } } return success; }
private boolean fetchAndStoreDelta() throws Throwable { long currGeneration = fetchRegistryGeneration.get(); Applications delta = fetchRemoteRegistry(true); 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 { String reconcileHashCode = ""; if (fetchRegistryUpdateLock.tryLock()) { try { updateDelta(delta); reconcileHashCode = getApplications().getReconcileHashCode(); } finally { fetchRegistryUpdateLock.unlock(); return reconcileAndLogDifference(delta, reconcileHashCode);
/** * Reconciles the delta information fetched to see if the hashcodes match. * * @param delta - the delta information fetched previously for reconciliation. * @param reconcileHashCode - the hashcode for comparison. * @return - response * @throws Throwable */ private boolean reconcileAndLogDifference(Applications delta, String reconcileHashCode) throws Throwable { logger.warn("The Reconcile hashcodes do not match, client : {}, server : {}. Getting the full registry", reconcileHashCode, delta.getAppsHashCode()); long currentGeneration = fetchRegistryGeneration.get(); Applications apps = this.fetchRemoteRegistry(false); if (apps == null) { logger.error("The application is null for some reason. Not storing this information"); return false; } if (fetchRegistryGeneration.compareAndSet(currentGeneration, currentGeneration + 1)) { applications.set(apps); applicationsDelta.set(apps); logger.warn("The Reconcile hashcodes after complete sync up, client : {}, server : {}.", getApplications().getReconcileHashCode(), delta.getAppsHashCode()); return true; }else { logger.warn("Not setting the applications map as another thread has advanced the update generation"); return true; // still return true } }
/** * Logs the total number of non-filtered instances stored locally. */ private void logTotalInstances() { int totInstances = 0; for (Application application : getApplications().getRegisteredApplications()) { totInstances += application.getInstancesAsIsFromEureka().size(); } logger.debug("The total number of all instances in the client now is {}", totInstances); }
/** * Get application information. * * @param appName The name of the application * @param includeRemoteRegion true, if we need to include applications from remote regions * as indicated by the region {@link URL} by this property * {@link EurekaServerConfig#getRemoteRegionUrls()}, false otherwise * @return the application */ @Override public Application getApplication(String appName, boolean includeRemoteRegion) { Application app = null; Map<String, Lease<InstanceInfo>> leaseMap = registry.get(appName); if (leaseMap != null && leaseMap.size() > 0) { for (Entry<String, Lease<InstanceInfo>> entry : leaseMap.entrySet()) { if (app == null) { app = new Application(appName); } app.addInstance(decorateInstanceInfo(entry.getValue())); } } else if (includeRemoteRegion) { for (RemoteRegionRegistry remoteRegistry : this.regionNameVSRemoteRegistry.values()) { Application application = remoteRegistry.getApplication(appName); if (application != null) { return application; } } } return app; }
Applications applications = remoteRegistry.getApplicationDeltas(); for (Application application : applications.getRegisteredApplications()) { Application appInLocalRegistry =
/** * Checks to see if the registry access is allowed or the server is in a * situation where it does not all getting registry information. The server * does not return registry information for a period specified in * {@link EurekaServerConfig#getWaitTimeInMsWhenSyncEmpty()}, if it cannot * get the registry information from the peer eureka nodes at start up. * * @return false - if the instances count from a replica transfer returned * zero and if the wait time has not elapsed, otherwise returns true */ @Override public boolean shouldAllowAccess(boolean remoteRegionRequired) { if (this.peerInstancesTransferEmptyOnStartup) { if (!(System.currentTimeMillis() > this.startupTime + serverConfig.getWaitTimeInMsWhenSyncEmpty())) { return false; } } if (remoteRegionRequired) { for (RemoteRegionRegistry remoteRegionRegistry : this.regionNameVSRemoteRegistry.values()) { if (!remoteRegionRegistry.isReadyForServingData()) { return false; } } } return true; }
/** * Gets the full registry information from the eureka server and stores it * locally. * * @return the full registry information. */ public boolean storeFullRegistry() { long currentGeneration = fetchRegistryGeneration.get(); Applications apps = fetchRemoteRegistry(false); if (apps == null) { logger.error("The application is null for some reason. Not storing this information"); } else if (fetchRegistryGeneration.compareAndSet(currentGeneration, currentGeneration + 1)) { applications.set(apps); applicationsDelta.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; }
if (fetchRegistry()) { this.readyForServingData = true; } else {
private boolean fetchAndStoreDelta() throws Throwable { long currGeneration = fetchRegistryGeneration.get(); Applications delta = fetchRemoteRegistry(true); 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 { String reconcileHashCode = ""; if (fetchRegistryUpdateLock.tryLock()) { try { updateDelta(delta); reconcileHashCode = getApplications().getReconcileHashCode(); } finally { fetchRegistryUpdateLock.unlock(); return reconcileAndLogDifference(delta, reconcileHashCode);
for (Application application : remoteRegistry.getApplications() .getRegisteredApplications()) { InstanceInfo instanceInfo = application.getByInstanceId(id);
/** * Reconciles the delta information fetched to see if the hashcodes match. * * @param delta - the delta information fetched previously for reconciliation. * @param reconcileHashCode - the hashcode for comparison. * @return - response * @throws Throwable */ private boolean reconcileAndLogDifference(Applications delta, String reconcileHashCode) throws Throwable { logger.warn("The Reconcile hashcodes do not match, client : {}, server : {}. Getting the full registry", reconcileHashCode, delta.getAppsHashCode()); long currentGeneration = fetchRegistryGeneration.get(); Applications apps = this.fetchRemoteRegistry(false); if (apps == null) { logger.error("The application is null for some reason. Not storing this information"); return false; } if (fetchRegistryGeneration.compareAndSet(currentGeneration, currentGeneration + 1)) { applications.set(apps); applicationsDelta.set(apps); logger.warn("The Reconcile hashcodes after complete sync up, client : {}, server : {}.", getApplications().getReconcileHashCode(), delta.getAppsHashCode()); return true; }else { logger.warn("Not setting the applications map as another thread has advanced the update generation"); return true; // still return true } }
/** * Gets the {@link InstanceInfo} information. * * @param appName the application name for which the information is requested. * @param id the unique identifier of the instance. * @param includeRemoteRegions true, if we need to include applications from remote regions * as indicated by the region {@link URL} by this property * {@link EurekaServerConfig#getRemoteRegionUrls()}, false otherwise * @return the information about the instance. */ @Override public InstanceInfo getInstanceByAppAndId(String appName, String id, boolean includeRemoteRegions) { Map<String, Lease<InstanceInfo>> leaseMap = registry.get(appName); Lease<InstanceInfo> lease = null; if (leaseMap != null) { lease = leaseMap.get(id); } if (lease != null && (!isLeaseExpirationEnabled() || !lease.isExpired())) { return decorateInstanceInfo(lease); } else if (includeRemoteRegions) { for (RemoteRegionRegistry remoteRegistry : this.regionNameVSRemoteRegistry.values()) { Application application = remoteRegistry.getApplication(appName); if (application != null) { return application.getByInstanceId(id); } } } return null; }
RemoteRegionRegistry remoteRegistry = regionNameVSRemoteRegistry.get(remoteRegion); if (null != remoteRegistry) { Applications remoteAppsDelta = remoteRegistry.getApplicationDeltas(); if (null != remoteAppsDelta) { for (Application application : remoteAppsDelta.getRegisteredApplications()) {
/** * Checks to see if the registry access is allowed or the server is in a * situation where it does not all getting registry information. The server * does not return registry information for a period specified in * {@link EurekaServerConfig#getWaitTimeInMsWhenSyncEmpty()}, if it cannot * get the registry information from the peer eureka nodes at start up. * * @return false - if the instances count from a replica transfer returned * zero and if the wait time has not elapsed, otherwise returns true */ @Override public boolean shouldAllowAccess(boolean remoteRegionRequired) { if (this.peerInstancesTransferEmptyOnStartup) { if (!(System.currentTimeMillis() > this.startupTime + serverConfig.getWaitTimeInMsWhenSyncEmpty())) { return false; } } if (remoteRegionRequired) { for (RemoteRegionRegistry remoteRegionRegistry : this.regionNameVSRemoteRegistry.values()) { if (!remoteRegionRegistry.isReadyForServingData()) { return false; } } } return true; }
/** * Gets the full registry information from the eureka server and stores it * locally. * * @return the full registry information. */ public boolean storeFullRegistry() { long currentGeneration = fetchRegistryGeneration.get(); Applications apps = fetchRemoteRegistry(false); if (apps == null) { logger.error("The application is null for some reason. Not storing this information"); } else if (fetchRegistryGeneration.compareAndSet(currentGeneration, currentGeneration + 1)) { applications.set(apps); applicationsDelta.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; }
if (fetchRegistry()) { this.readyForServingData = true; } else {
/** * Fetch the registry information from the remote region. * @return true, if the fetch was successful, false otherwise. */ private boolean fetchRegistry() { boolean success; Stopwatch tracer = fetchRegistryTimer.start(); try { // If the delta is disabled or if it is the first time, get all applications if (serverConfig.shouldDisableDeltaForRemoteRegions() || (getApplications() == null) || (getApplications().getRegisteredApplications().size() == 0)) { logger.info("Disable delta property : {}", serverConfig.shouldDisableDeltaForRemoteRegions()); logger.info("Application is null : {}", getApplications() == null); logger.info("Registered Applications size is zero : {}", getApplications().getRegisteredApplications().isEmpty()); success = storeFullRegistry(); } else { success = fetchAndStoreDelta(); } logTotalInstances(); } catch (Throwable e) { logger.error("Unable to fetch registry information from the remote registry {}", this.remoteRegionURL, e); return false; } finally { if (tracer != null) { tracer.stop(); } } return success; }