code: def test_locality_aware_leasing(ray_start_cluster):
      # This test ensures that a task will run where its task dependencies are
      # located. We run an initial non_local() task that is pinned to a
      # non-local node via a custom resource constraint, and then we run an
      # unpinned task f() that depends on the output of non_local(), ensuring
      # that f() runs on the same node as non_local().
      cluster = ray_start_cluster
  
      # Disable worker caching so worker leases are not reused, and disable
      # inlining of return objects so return objects are always put into Plasma.
      cluster.add_node(
          num_cpus=1,
          _system_config={
              "worker_lease_timeout_milliseconds": 0,
              "max_direct_call_object_size": 0,
              "scheduler_spread_threshold": 0.1,
          },
      )
      ray.init(address=cluster.address)
      # Use a custom resource for pinning tasks to a node.
      non_local_node = cluster.add_node(
          num_cpus=2, resources={"pin": 2}
      )
      cluster.wait_for_nodes()
  
      @ray.remote(num_cpus=1, resources={"pin": 1})
      class Actor:
          def ping(self):
              pass
  
      actor = Actor.remote()
      ray.get(actor.ping.remote())
  
      @ray.remote(resources={"pin": 1})
      def non_local():
          return ray._private.worker.global_worker.node.unique_id
  
      @ray.remote
      def f(x):
          return ray._private.worker.global_worker.node.unique_id
  
      # Test that task f() runs on the same node as non_local()
      # even though local node is lower critical resource utilization.
      assert (
          ray.get(f.remote(non_local.remote()))
          == non_local_node.unique_id
      )
  
imports: import collections
  import logging
  import platform
  import subprocess
  import sys
  import time
  import unittest
  import numpy
  import pytest
  import ray
  import ray.cluster_utils
  import ray.util.accelerators
  from ray._private.internal_api import memory_summary
  from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy
  from ray._private.test_utils import Semaphore, SignalActor, object_memory_usage, get_metric_check_condition, wait_for_condition, MetricSamplePattern
label_desc: Use image node_id 9 for calling a global function with example usage: test_locality_aware_leasing(ray_start_cluster) and returns: ray, ray