Azure Databricks는 Apache Spark 작업과 동일한 방식으로 클러스터 및 작업 구성을 처리하여 Ray 클러스터를 시작하는 프로세스를 간소화합니다. 이는 Ray 클러스터가 실제로 관리되는 Apache Spark 클러스터 위에서 시작되기 때문입니다.
Azure Databricks에서 Ray 실행
from ray.util.spark import setup_ray_cluster
import ray
# If the cluster has four workers with 8 CPUs each as an example
setup_ray_cluster(num_worker_nodes=4, num_cpus_per_worker=8)
# Pass any custom configuration to ray.init
ray.init(ignore_reinit_error=True)
이 방법은 몇 개의 노드에서 수백 개의 노드까지 모든 클러스터 규모에서 작동합니다. Azure Databricks의 광선 클러스터는 자동 크기 조정도 지원합니다.
Ray 클러스터를 만든 후 Azure Databricks Notebook에서 모든 Ray 애플리케이션 코드를 실행할 수 있습니다.
중요합니다
Databricks는 애플리케이션에 필요한 라이브러리를 %pip install <your-library-dependency>로 설치하여 Ray 클러스터와 애플리케이션에서 사용할 수 있도록 할 것을 권장합니다. Ray init 함수 호출에 종속성을 지정하면 Apache Spark 작업자 노드에 액세스할 수 없는 위치에 종속성이 설치되어 버전 비호환성 및 가져오기 오류가 발생합니다.
예를 들어 다음과 같이 Azure Databricks Notebook에서 간단한 Ray 애플리케이션을 실행할 수 있습니다.
import ray
import random
import time
from fractions import Fraction
ray.init()
@ray.remote
def pi4_sample(sample_count):
"""pi4_sample runs sample_count experiments, and returns the
fraction of time it was inside the circle.
"""
in_count = 0
for i in range(sample_count):
x = random.random()
y = random.random()
if x*x + y*y <= 1:
in_count += 1
return Fraction(in_count, sample_count)
SAMPLE_COUNT = 1000 * 1000
start = time.time()
future = pi4_sample.remote(sample_count=SAMPLE_COUNT)
pi4 = ray.get(future)
end = time.time()
dur = end - start
print(f'Running {SAMPLE_COUNT} tests took {dur} seconds')
pi = pi4 * 4
print(float(pi))
Ray 클러스터 종료
광선 클러스터는 다음과 같은 상황에서 자동으로 종료됩니다.
- Azure Databricks 클러스터에서 대화형 Notebook을 분리합니다.
- Azure Databricks 작업이 완료되었습니다.
- Azure Databricks 클러스터가 다시 시작되거나 종료됩니다.
- 지정된 유휴 시간에 대한 활동이 없습니다.
Azure Databricks에서 실행되는 Ray 클러스터를 종료하려면 API를 호출할 ray.utils.spark.shutdown_ray_cluster 수 있습니다.
from ray.utils.spark import shutdown_ray_cluster
import ray
shutdown_ray_cluster()
ray.shutdown()