HostVersionOutOfSyncListener { @Subscribe @Transactional public void onHostEvent(HostsAddedEvent event) { if (LOG.isDebugEnabled()) { LOG.debug(event.toString()); } try { Cluster cluster = clusters.get().getClusterById(event.getClusterId()); Collection<ClusterVersionEntity> allClusterVersions = cluster.getAllClusterVersions(); for (ClusterVersionEntity clusterVersion : allClusterVersions) { if (clusterVersion.getState() != RepositoryVersionState.CURRENT) { RepositoryVersionEntity repositoryVersion = clusterVersion.getRepositoryVersion(); for (String hostName : event.getHostNames()) { HostEntity hostEntity = hostDAO.get().findByName(hostName); HostVersionEntity missingHostVersion = new HostVersionEntity(hostEntity, repositoryVersion, RepositoryVersionState.OUT_OF_SYNC); LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})", missingHostVersion.getHostName(), missingHostVersion.getState(), missingHostVersion.getRepositoryVersion().getVersion(), missingHostVersion.getRepositoryVersion().getId()); hostVersionDAO.get().create(missingHostVersion); } cluster.recalculateClusterVersionState(repositoryVersion); } } } catch (AmbariException e) { LOG.error("Can not update hosts about out of sync", e); } } @Inject  HostVersionOutOfSyncListener(AmbariEventPublisher ambariEventPublisher, LockFactory lockFactory); @Subscribe @Transactional void onServiceComponentEvent(ServiceComponentInstalledEvent event); @Subscribe @Transactional void onServiceComponentHostEvent(ServiceComponentUninstalledEvent event); @Subscribe @Transactional void onServiceEvent(ServiceInstalledEvent event); @Subscribe @Transactional void onHostEvent(HostsAddedEvent event); @Subscribe @Transactional void onHostEvent(HostsRemovedEvent event);  }
@Test public void testOnHostEvent() throws AmbariException { Host h1 = clusters.getHost("h1"); h1.setState(HostState.HEALTHY); StackId stackId = new StackId(this.stackId); RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,"2.2.0-1000"); RepositoryVersionEntity repositoryVersionEntity2 = helper.getOrCreateRepositoryVersion(stackId,"2.2.0-2000"); c1.createClusterVersion(stackId, "2.2.0-1000", "admin", RepositoryVersionState.INSTALLING); c1.setCurrentStackVersion(stackId); c1.recalculateAllClusterVersionStates(); assertRepoVersionState(stackId.getStackId(), "2.2.0-1000", RepositoryVersionState.INSTALLING); assertRepoVersionState(stackId.getStackId(), "2.2.0-2086", RepositoryVersionState.CURRENT); helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLED); helper.createHostVersion("h1", repositoryVersionEntity2, RepositoryVersionState.INSTALLED); c1.recalculateAllClusterVersionStates(); assertRepoVersionState(stackId.getStackId(), "2.2.0-1000", RepositoryVersionState.INSTALLED); assertRepoVersionState(stackId.getStackId(), "2.2.0-2000", RepositoryVersionState.INSTALLED); assertRepoVersionState(stackId.getStackId(), "2.2.0-2086", RepositoryVersionState.CURRENT); addHost("h2"); clusters.mapHostToCluster("h2", "c1"); List<HostVersionEntity> h2Versions = hostVersionDAO.findByHost("h2"); for (HostVersionEntity hostVersionEntity : h2Versions) { if (hostVersionEntity.getRepositoryVersion().toString().equals("2.2.0-2086")) { assertEquals(hostVersionEntity.getState(), RepositoryVersionState.CURRENT); } else { assertEquals(hostVersionEntity.getState(), RepositoryVersionState.OUT_OF_SYNC); } } }