@Override
  @Transactional
  public void updateRepositoryState(String reportedVersion) throws AmbariException {

    ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findById(
        desiredStateEntityId);

    List<ServiceComponentVersionEntity> componentVersions = serviceComponentDesiredStateDAO.findVersions(
        getClusterId(), getServiceName(), getName());

    // per component, this list should be small, so iterating here isn't a big deal
    Map<String, ServiceComponentVersionEntity> map = new HashMap<>(Maps.uniqueIndex(componentVersions,
        new Function<ServiceComponentVersionEntity, String>() {
          @Override
          public String apply(ServiceComponentVersionEntity input) {
            return input.getRepositoryVersion().getVersion();
          }
      }));

    if (LOG.isDebugEnabled()) {
      LOG.debug("Existing versions for {}/{}/{}: {}",
          getClusterName(), getServiceName(), getName(), map.keySet());
    }

    ServiceComponentVersionEntity componentVersion = map.get(reportedVersion);

    if (null == componentVersion) {
      RepositoryVersionEntity repoVersion = repoVersionDAO.findByStackAndVersion(
          getDesiredStackId(), reportedVersion);

      if (null != repoVersion) {
        componentVersion = new ServiceComponentVersionEntity();
        componentVersion.setRepositoryVersion(repoVersion);
        componentVersion.setState(RepositoryVersionState.INSTALLED);
        componentVersion.setUserName("auto-reported");

        // since we've never seen this version before, mark the component as CURRENT
        serviceComponentDesiredStateEntity.setRepositoryState(RepositoryVersionState.CURRENT);
        serviceComponentDesiredStateEntity.addVersion(componentVersion);

        serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.merge(serviceComponentDesiredStateEntity);

        map.put(reportedVersion, componentVersion);

      } else {
        LOG.warn("There is no repository available for stack {}, version {}",
            getDesiredStackId(), reportedVersion);
      }
    }

    if (MapUtils.isNotEmpty(map)) {
      String desiredVersion = serviceComponentDesiredStateEntity.getDesiredVersion();
      RepositoryVersionEntity desiredRepositoryVersion = service.getDesiredRepositoryVersion();

      List<HostComponentStateEntity> hostComponents = hostComponentDAO.findByServiceAndComponentAndNotVersion(
          serviceComponentDesiredStateEntity.getServiceName(), serviceComponentDesiredStateEntity.getComponentName(), reportedVersion);

      LOG.debug("{}/{} reportedVersion={}, desiredVersion={}, non-matching desired count={}, repo_state={}",
          serviceComponentDesiredStateEntity.getServiceName(), serviceComponentDesiredStateEntity.getComponentName(), reportedVersion,
          desiredVersion, hostComponents.size(), serviceComponentDesiredStateEntity.getRepositoryState());

      // !!! if we are unknown, that means it's never been set.  Try to determine it.
      if (StackVersionListener.UNKNOWN_VERSION.equals(desiredVersion)) {
        if (CollectionUtils.isEmpty(hostComponents)) {
          // all host components are the same version as reported
          serviceComponentDesiredStateEntity.setDesiredRepositoryVersion(desiredRepositoryVersion);
          serviceComponentDesiredStateEntity.setRepositoryState(RepositoryVersionState.CURRENT);
        } else {
          // desired is UNKNOWN and there's a mix of versions in the host components
          serviceComponentDesiredStateEntity.setRepositoryState(RepositoryVersionState.OUT_OF_SYNC);
        }
      } else {
        if (!reportedVersion.equals(desiredVersion)) {
          serviceComponentDesiredStateEntity.setRepositoryState(RepositoryVersionState.OUT_OF_SYNC);
        } else if (CollectionUtils.isEmpty(hostComponents)) {
          serviceComponentDesiredStateEntity.setRepositoryState(RepositoryVersionState.CURRENT);
        }
      }

      serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.merge(serviceComponentDesiredStateEntity);
    }
  }
Follows this version logic:
<table border="1">
  <tr>
    <th>DB hostcomponent1</th>
    <th>DB hostcomponentN</th>
    <th>DB desired</th>
    <th>New desired</th>
    <th>Repo State</th>
  </tr>
  <tr>
    <td>v1</td>
    <td>v1</td>
    <td>UNKNOWN</td>
    <td>v1</td>
    <td>CURRENT</td>
  </tr>
  <tr>
    <td>v1</td>
    <td>v2</td>
    <td>UNKNOWN</td>
    <td>UNKNOWN</td>
    <td>OUT_OF_SYNC</td>
  </tr>
  <tr>
    <td>v1</td>
    <td>v2</td>
    <td>v2</td>
    <td>v2 (no change)</td>
    <td>OUT_OF_SYNC</td>
  </tr>
  <tr>
    <td>v2</td>
    <td>v2</td>
    <td>v1</td>
    <td>v1 (no change)</td>
    <td>OUT_OF_SYNC</td>
  </tr>
  <tr>
    <td>v2</td>
    <td>v2</td>
    <td>v2</td>
    <td>v2 (no change)</td>
    <td>CURRENT</td>
  </tr>
</table>
ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java