public List<DatasetVersionRecord> getAllValidVersionsOutOfRetentionCount(short accountId, short containerId,
      String accountName, String containerName, String datasetName) throws SQLException, AccountServiceException {
    long startTimeMs = System.currentTimeMillis();
    Integer retentionCount;
    Dataset.VersionSchema versionSchema;
    String retentionPolicy;
    List<DatasetVersionRecord> datasetVersionRecordList;
    try {
      //if dataset is deleted, we should not be able to get any dataset version.
      Dataset dataset = getDatasetHelper(accountId, containerId, accountName, containerName, datasetName, true);
      retentionCount = dataset.getRetentionCount();
      versionSchema = dataset.getVersionSchema();
      retentionPolicy = dataset.getRetentionPolicy();
      datasetVersionRecordList = new ArrayList<>();
      if (retentionCount == null) {
        dataAccessor.onSuccess(Read, System.currentTimeMillis() - startTimeMs);
        return datasetVersionRecordList;
      }
      if (DEFAULT_RETENTION_POLICY.equals(retentionPolicy)) {
        datasetVersionRecordList =
            getDatasetVersionOutOfRetentionByDefault(accountId, containerId, datasetName, retentionCount,
                versionSchema);
        dataAccessor.onSuccess(Read, System.currentTimeMillis() - startTimeMs);
        return datasetVersionRecordList;
      } else {
        List<DatasetVersionRecord> allValidDatasetVersionsOrderedByVersion =
            listValidDatasetVersionsByListStatement(accountId, containerId, datasetName, versionSchema);
        DatasetRetentionPolicy customizedRetentionPolicy = Utils.getObj(retentionPolicy);
        datasetVersionRecordList =
            customizedRetentionPolicy.getDatasetVersionOutOfRetention(allValidDatasetVersionsOrderedByVersion,
                retentionCount, versionSchema);
        dataAccessor.onSuccess(Read, System.currentTimeMillis() - startTimeMs);
        return datasetVersionRecordList;
      }
    } catch (SQLException | AccountServiceException e) {
      dataAccessor.onException(e, Read);
      throw e;
    } catch (ReflectiveOperationException e) {
      dataAccessor.onException(e, Read);
      metrics.constructRetentionPolicyFailureCount.inc();
      throw new IllegalStateException("Unable to construct the dataset retention policy");
    }
  }
Get all versions from a dataset which has not expired and out of retentionCount by checking the last modified time.
@param accountId the id for the parent account.
@param containerId the id of the container.
@param accountName the name for the parent account.
@param containerName the name for the container.
@param datasetName the name of the dataset.
@return a list of {@link DatasetVersionRecord}
ambry-account/src/main/java/com/github/ambry/account/mysql/DatasetDao.java