コード例 #1
0
    def invalidated(self,
                    targets,
                    invalidate_dependents=False,
                    partition_size_hint=sys.maxint,
                    silent=False,
                    locally_changed_targets=None,
                    fingerprint_strategy=None,
                    topological_order=False):
        """Checks targets for invalidation, first checking the artifact cache.

    Subclasses call this to figure out what to work on.

    :param targets:               The targets to check for changes.
    :param invalidate_dependents: If True then any targets depending on changed targets are invalidated.
    :param partition_size_hint:   Each VersionedTargetSet in the yielded list will represent targets
                                  containing roughly this number of source files, if possible. Set to
                                  sys.maxint for a single VersionedTargetSet. Set to 0 for one
                                  VersionedTargetSet per target. It is up to the caller to do the right
                                  thing with whatever partitioning it asks for.
    :param locally_changed_targets: Targets that we've edited locally. If specified, and there aren't too
                                  many of them, we keep these in separate partitions from other targets,
                                  as these are more likely to have build errors, and so to be rebuilt over
                                  and over, and partitioning them separately is a performance win.
    :param fingerprint_strategy:   A FingerprintStrategy instance, which can do per task, finer grained
                                  fingerprinting of a given Target.

    If no exceptions are thrown by work in the block, the build cache is updated for the targets.
    Note: the artifact cache is not updated. That must be done manually.

    :returns: Yields an InvalidationCheck object reflecting the (partitioned) targets.
    :rtype: InvalidationCheck
    """

        # TODO(benjy): Compute locally_changed_targets here instead of passing it in? We currently pass
        # it in because JvmCompile already has the source->target mapping for other reasons, and also
        # to selectively enable this feature.
        fingerprint_strategy = fingerprint_strategy or TaskIdentityFingerprintStrategy(
            self)
        cache_manager = self.create_cache_manager(
            invalidate_dependents, fingerprint_strategy=fingerprint_strategy)
        # We separate locally-modified targets from others by coloring them differently.
        # This can be a performance win, because these targets are more likely to be iterated
        # over, and this preserves "chunk stability" for them.
        colors = {}

        # But we only do so if there aren't too many, or this optimization will backfire.
        locally_changed_target_limit = 10

        if locally_changed_targets and len(
                locally_changed_targets) < locally_changed_target_limit:
            for t in targets:
                if t in locally_changed_targets:
                    colors[t] = 'locally_changed'
                else:
                    colors[t] = 'not_locally_changed'
        invalidation_check = cache_manager.check(
            targets,
            partition_size_hint,
            colors,
            topological_order=topological_order)

        if invalidation_check.invalid_vts and self.artifact_cache_reads_enabled(
        ):
            with self.context.new_workunit('cache'):
                cached_vts, uncached_vts = \
                  self.check_artifact_cache(self.check_artifact_cache_for(invalidation_check))
            if cached_vts:
                cached_targets = [vt.target for vt in cached_vts]
                for t in cached_targets:
                    self.context.run_tracker.artifact_cache_stats.add_hit(
                        'default', t)
                if not silent:
                    self._report_targets('Using cached artifacts for ',
                                         cached_targets, '.')
            if uncached_vts:
                uncached_targets = [vt.target for vt in uncached_vts]
                for t in uncached_targets:
                    self.context.run_tracker.artifact_cache_stats.add_miss(
                        'default', t)
                if not silent:
                    self._report_targets('No cached artifacts for ',
                                         uncached_targets, '.')
            # Now that we've checked the cache, re-partition whatever is still invalid.
            invalidation_check = \
              InvalidationCheck(invalidation_check.all_vts, uncached_vts, partition_size_hint, colors)

        self._maybe_create_results_dirs(invalidation_check.all_vts)

        if not silent:
            targets = []
            num_invalid_partitions = len(
                invalidation_check.invalid_vts_partitioned)
            for vt in invalidation_check.invalid_vts_partitioned:
                targets.extend(vt.targets)

            if len(targets):
                msg_elements = [
                    'Invalidated ',
                    items_to_report_element(
                        [t.address.reference() for t in targets], 'target')
                ]
                if num_invalid_partitions > 1:
                    msg_elements.append(' in {} target partitions'.format(
                        num_invalid_partitions))
                msg_elements.append('.')
                self.context.log.info(*msg_elements)

        invalidation_report = self.context.invalidation_report
        if invalidation_report:
            for vts in invalidation_check.all_vts:
                invalidation_report.add_vts(cache_manager,
                                            vts.targets,
                                            vts.cache_key,
                                            vts.valid,
                                            phase='pre-check')

        # Yield the result, and then mark the targets as up to date.
        yield invalidation_check

        if invalidation_report:
            for vts in invalidation_check.all_vts:
                invalidation_report.add_vts(cache_manager,
                                            vts.targets,
                                            vts.cache_key,
                                            vts.valid,
                                            phase='post-check')
        for vt in invalidation_check.invalid_vts:
            vt.update()  # In case the caller doesn't update.

        write_to_cache = (self.cache_target_dirs
                          and self.artifact_cache_writes_enabled()
                          and invalidation_check.invalid_vts)
        if write_to_cache:
            pairs = []
            for vt in invalidation_check.invalid_vts:
                if self._should_cache(vt):
                    pairs.append((vt, [vt.results_dir]))
            self.update_artifact_cache(pairs)
コード例 #2
0
ファイル: task.py プロジェクト: rkstap/pants
  def invalidated(self,
                  targets,
                  invalidate_dependents=False,
                  silent=False,
                  fingerprint_strategy=None,
                  topological_order=False):
    """Checks targets for invalidation, first checking the artifact cache.

    Subclasses call this to figure out what to work on.

    :API: public

    :param targets: The targets to check for changes.
    :param invalidate_dependents: If True then any targets depending on changed targets are
                                  invalidated.
    :param silent: If true, suppress logging information about target invalidation.
    :param fingerprint_strategy: A FingerprintStrategy instance, which can do per task,
                                finer grained fingerprinting of a given Target.
    :param topological_order: Whether to invalidate in dependency order.

    If no exceptions are thrown by work in the block, the build cache is updated for the targets.
    Note: the artifact cache is not updated. That must be done manually.

    :returns: Yields an InvalidationCheck object reflecting the targets.
    :rtype: InvalidationCheck
    """
    invalidation_check = self._do_invalidation_check(fingerprint_strategy,
                                                     invalidate_dependents,
                                                     targets,
                                                     topological_order)

    self._maybe_create_results_dirs(invalidation_check.all_vts)

    if invalidation_check.invalid_vts and self.artifact_cache_reads_enabled():
      with self.context.new_workunit('cache'):
        cached_vts, uncached_vts, uncached_causes = \
          self.check_artifact_cache(self.check_artifact_cache_for(invalidation_check))
      if cached_vts:
        cached_targets = [vt.target for vt in cached_vts]
        self.context.run_tracker.artifact_cache_stats.add_hits(self._task_name, cached_targets)
        if not silent:
          self._report_targets('Using cached artifacts for ', cached_targets, '.')
      if uncached_vts:
        uncached_targets = [vt.target for vt in uncached_vts]
        self.context.run_tracker.artifact_cache_stats.add_misses(self._task_name,
                                                                 uncached_targets,
                                                                 uncached_causes)
        if not silent:
          self._report_targets('No cached artifacts for ', uncached_targets, '.')
      # Now that we've checked the cache, re-partition whatever is still invalid.
      invalidation_check = InvalidationCheck(invalidation_check.all_vts, uncached_vts)

    if not silent:
      targets = []
      for vt in invalidation_check.invalid_vts:
        targets.extend(vt.targets)

      if len(targets):
        target_address_references = [t.address.reference() for t in targets]
        msg_elements = [
          'Invalidated ',
          items_to_report_element(target_address_references, 'target'),
          '.',
        ]
        self.context.log.info(*msg_elements)

    self._update_invalidation_report(invalidation_check, 'pre-check')

    # Cache has been checked to create the full list of invalid VTs.
    # Only copy previous_results for this subset of VTs.
    if self.incremental:
      for vts in invalidation_check.invalid_vts:
        vts.copy_previous_results()

    # This may seem odd: why would we need to invalidate a VersionedTargetSet that is already
    # invalid?  But the name force_invalidate() is slightly misleading in this context - what it
    # actually does is delete the key file created at the end of the last successful task run.
    # This is necessary to avoid the following scenario:
    #
    # 1) In state A: Task suceeds and writes some output.  Key is recorded by the invalidator.
    # 2) In state B: Task fails, but writes some output.  Key is not recorded.
    # 3) After reverting back to state A: The current key is the same as the one recorded at the
    #    end of step 1), so it looks like no work needs to be done, but actually the task
    #   must re-run, to overwrite the output written in step 2.
    #
    # Deleting the file ensures that if a task fails, there is no key for which we might think
    # we're in a valid state.
    for vts in invalidation_check.invalid_vts:
      vts.force_invalidate()

    # Yield the result, and then mark the targets as up to date.
    yield invalidation_check

    self._update_invalidation_report(invalidation_check, 'post-check')

    for vt in invalidation_check.invalid_vts:
      vt.update()

    # Background work to clean up previous builds.
    if self.context.options.for_global_scope().workdir_max_build_entries is not None:
      self._launch_background_workdir_cleanup(invalidation_check.all_vts)
コード例 #3
0
    def test_partition(self):
        # The default EmptyPayload chunking unit happens to be 1, so each of these Targets
        # has a chunking unit contribution of 1
        a = self.make_target(':a', dependencies=[])
        b = self.make_target(':b', dependencies=[a])
        c = self.make_target(':c', dependencies=[b])
        d = self.make_target(':d', dependencies=[c, a])
        e = self.make_target(':e', dependencies=[d])

        targets = [a, b, c, d, e]

        def print_partitions(partitions):
            strs = []
            for partition in partitions:
                strs.append('(%s)' %
                            ', '.join([t.id for t in partition.targets]))
            print('[%s]' % ' '.join(strs))

        # Verify basic data structure soundness.
        all_vts = self.cache_manager.wrap_targets(targets)
        invalid_vts = filter(lambda vt: not vt.valid, all_vts)
        self.assertEquals(5, len(invalid_vts))
        self.assertEquals(5, len(all_vts))
        vts_targets = [vt.targets[0] for vt in all_vts]
        self.assertEquals(set(targets), set(vts_targets))

        # Test a simple partition.
        ic = InvalidationCheck(all_vts, [], 3)
        partitioned = ic.all_vts_partitioned
        print_partitions(partitioned)

        # Several correct partitionings are possible, but in all cases 4 1-source targets will be
        # added to the first partition before it exceeds the limit of 3, and the final target will
        # be in a partition by itself.
        self.assertEquals(2, len(partitioned))
        self.assertEquals(4, len(partitioned[0].targets))
        self.assertEquals(1, len(partitioned[1].targets))

        # Test partition with colors.
        red = 'red'
        blue = 'blue'

        colors = {a: blue, b: red, c: red, d: red, e: blue}

        # As a reference, we partition without colors.
        ic = InvalidationCheck(all_vts, [], 2)
        partitioned = ic.all_vts_partitioned
        print_partitions(partitioned)

        self.assertEquals(2, len(partitioned))
        self.assertEquals(3, len(partitioned[0].targets))
        self.assertEquals(2, len(partitioned[1].targets))

        # Now apply color restrictions.
        ic = InvalidationCheck(all_vts, [], 2, target_colors=colors)
        partitioned = ic.all_vts_partitioned
        print_partitions(partitioned)

        self.assertEquals(3, len(partitioned))
        self.assertEquals(1, len(partitioned[0].targets))
        self.assertEquals(3, len(partitioned[1].targets))
        self.assertEquals(1, len(partitioned[2].targets))
コード例 #4
0
    def invalidated(self,
                    targets,
                    invalidate_dependents=False,
                    silent=False,
                    fingerprint_strategy=None,
                    topological_order=False,
                    use_cache=True):
        """Checks targets for invalidation, first checking the artifact cache.

    Subclasses call this to figure out what to work on.

    :API: public

    :param targets:               The targets to check for changes.
    :param invalidate_dependents: If True then any targets depending on changed targets are invalidated.
    :param fingerprint_strategy:   A FingerprintStrategy instance, which can do per task, finer grained
                                  fingerprinting of a given Target.
    :param use_cache:             A boolean to indicate whether to read/write the cache within this
                                  invalidate call. In order for the cache to be used, both the task
                                  settings and this parameter must agree that they should be used.

    If no exceptions are thrown by work in the block, the build cache is updated for the targets.
    Note: the artifact cache is not updated. That must be done manually.

    :returns: Yields an InvalidationCheck object reflecting the targets.
    :rtype: InvalidationCheck
    """

        fingerprint_strategy = fingerprint_strategy or TaskIdentityFingerprintStrategy(
            self)
        cache_manager = self.create_cache_manager(
            invalidate_dependents, fingerprint_strategy=fingerprint_strategy)

        invalidation_check = cache_manager.check(
            targets, topological_order=topological_order)

        if invalidation_check.invalid_vts and use_cache and self.artifact_cache_reads_enabled(
        ):
            with self.context.new_workunit('cache'):
                cached_vts, uncached_vts, uncached_causes = \
                  self.check_artifact_cache(self.check_artifact_cache_for(invalidation_check))
            if cached_vts:
                cached_targets = [vt.target for vt in cached_vts]
                self.context.run_tracker.artifact_cache_stats.add_hits(
                    cache_manager.task_name, cached_targets)
                if not silent:
                    self._report_targets('Using cached artifacts for ',
                                         cached_targets, '.')
            if uncached_vts:
                uncached_targets = [vt.target for vt in uncached_vts]
                self.context.run_tracker.artifact_cache_stats.add_misses(
                    cache_manager.task_name, uncached_targets, uncached_causes)
                if not silent:
                    self._report_targets('No cached artifacts for ',
                                         uncached_targets, '.')
            # Now that we've checked the cache, re-partition whatever is still invalid.
            invalidation_check = \
              InvalidationCheck(invalidation_check.all_vts, uncached_vts)

        self._maybe_create_results_dirs(invalidation_check.all_vts)

        if not silent:
            targets = []
            for vt in invalidation_check.invalid_vts:
                targets.extend(vt.targets)

            if len(targets):
                msg_elements = [
                    'Invalidated ',
                    items_to_report_element(
                        [t.address.reference() for t in targets], 'target')
                ]
                msg_elements.append('.')
                self.context.log.info(*msg_elements)

        invalidation_report = self.context.invalidation_report
        if invalidation_report:
            for vts in invalidation_check.all_vts:
                invalidation_report.add_vts(cache_manager,
                                            vts.targets,
                                            vts.cache_key,
                                            vts.valid,
                                            phase='pre-check')

        # Yield the result, and then mark the targets as up to date.
        yield invalidation_check

        if invalidation_report:
            for vts in invalidation_check.all_vts:
                invalidation_report.add_vts(cache_manager,
                                            vts.targets,
                                            vts.cache_key,
                                            vts.valid,
                                            phase='post-check')
        for vt in invalidation_check.invalid_vts:
            vt.update()  # In case the caller doesn't update.

        # Background work to clean up previous builds.
        if self.context.options.for_global_scope(
        ).workdir_max_build_entries is not None:
            self._launch_background_workdir_cleanup(invalidation_check.all_vts)

        write_to_cache = (self.cache_target_dirs and use_cache
                          and self.artifact_cache_writes_enabled()
                          and invalidation_check.invalid_vts)
        if write_to_cache:
            pairs = []
            for vt in invalidation_check.invalid_vts:
                if self._should_cache(vt):
                    pairs.append((vt, [vt.current_results_dir]))
            self.update_artifact_cache(pairs)
コード例 #5
0
ファイル: task.py プロジェクト: JustinHendryx/pants
    def invalidated(self,
                    targets,
                    invalidate_dependents=False,
                    silent=False,
                    fingerprint_strategy=None,
                    topological_order=False):
        """Checks targets for invalidation, first checking the artifact cache.

    Subclasses call this to figure out what to work on.

    :API: public

    :param targets: The targets to check for changes.
    :param invalidate_dependents: If True then any targets depending on changed targets are
                                  invalidated.
    :param silent: If true, suppress logging information about target invalidation.
    :param fingerprint_strategy: A FingerprintStrategy instance, which can do per task,
                                finer grained fingerprinting of a given Target.
    :param topological_order: Whether to invalidate in dependency order.

    If no exceptions are thrown by work in the block, the build cache is updated for the targets.
    Note: the artifact cache is not updated. That must be done manually.

    :returns: Yields an InvalidationCheck object reflecting the targets.
    :rtype: InvalidationCheck
    """

        cache_key_generator = CacheKeyGenerator(
            self.context.options.for_global_scope().cache_key_gen_version,
            self.fingerprint)
        cache_manager = InvalidationCacheManager(
            self.workdir,
            cache_key_generator,
            self._build_invalidator_dir,
            invalidate_dependents,
            fingerprint_strategy=fingerprint_strategy,
            invalidation_report=self.context.invalidation_report,
            task_name=type(self).__name__,
            task_version=self.implementation_version_str(),
            artifact_write_callback=self.maybe_write_artifact)

        invalidation_check = cache_manager.check(
            targets, topological_order=topological_order)

        self._maybe_create_results_dirs(invalidation_check.all_vts)

        if invalidation_check.invalid_vts and self.artifact_cache_reads_enabled(
        ):
            with self.context.new_workunit('cache'):
                cached_vts, uncached_vts, uncached_causes = \
                  self.check_artifact_cache(self.check_artifact_cache_for(invalidation_check))
            if cached_vts:
                cached_targets = [vt.target for vt in cached_vts]
                self.context.run_tracker.artifact_cache_stats.add_hits(
                    cache_manager.task_name, cached_targets)
                if not silent:
                    self._report_targets('Using cached artifacts for ',
                                         cached_targets, '.')
            if uncached_vts:
                uncached_targets = [vt.target for vt in uncached_vts]
                self.context.run_tracker.artifact_cache_stats.add_misses(
                    cache_manager.task_name, uncached_targets, uncached_causes)
                if not silent:
                    self._report_targets('No cached artifacts for ',
                                         uncached_targets, '.')
            # Now that we've checked the cache, re-partition whatever is still invalid.
            invalidation_check = \
              InvalidationCheck(invalidation_check.all_vts, uncached_vts)

        if not silent:
            targets = []
            for vt in invalidation_check.invalid_vts:
                targets.extend(vt.targets)

            if len(targets):
                msg_elements = [
                    'Invalidated ',
                    items_to_report_element(
                        [t.address.reference() for t in targets], 'target'),
                    '.'
                ]
                self.context.log.info(*msg_elements)

        invalidation_report = self.context.invalidation_report
        if invalidation_report:
            for vts in invalidation_check.all_vts:
                invalidation_report.add_vts(cache_manager,
                                            vts.targets,
                                            vts.cache_key,
                                            vts.valid,
                                            phase='pre-check')

        # Cache has been checked to create the full list of invalid VTs.
        # Only copy previous_results for this subset of VTs.
        if self.incremental:
            for vts in invalidation_check.invalid_vts:
                vts.copy_previous_results()

        # Yield the result, and then mark the targets as up to date.
        yield invalidation_check

        if invalidation_report:
            for vts in invalidation_check.all_vts:
                invalidation_report.add_vts(cache_manager,
                                            vts.targets,
                                            vts.cache_key,
                                            vts.valid,
                                            phase='post-check')

        for vt in invalidation_check.invalid_vts:
            vt.update()

        # Background work to clean up previous builds.
        if self.context.options.for_global_scope(
        ).workdir_max_build_entries is not None:
            self._launch_background_workdir_cleanup(invalidation_check.all_vts)