client.py 8.53 KB
Newer Older
1 2 3
import rpyc
from sqlalchemy.orm.exc import NoResultFound

4 5 6 7 8
from common.state import AlchemySession
from common.state.application import Application, SparkNotebookApplication, SparkSubmitApplication, SparkApplication, PlainApplication
from common.state.container import Container
from common.state.execution import Execution, SparkSubmitExecution, PlainExecution
from common.state.user import User, PlainUser
9
from common.application_resources import SparkApplicationResources
10
from common.status import PlatformStatusReport, ApplicationStatusReport
11 12 13 14 15 16 17 18 19 20 21 22
from common.exceptions import UserIDDoesNotExist, ApplicationStillRunning
import common.object_storage as storage

REGISTRY = "10.0.0.2:5000"
MASTER_IMAGE = REGISTRY + "/zoe/spark-master-1.4.1:1.2"
WORKER_IMAGE = REGISTRY + "/zoe/spark-worker-1.4.1:1.2"
SHELL_IMAGE = REGISTRY + "/zoe/spark-shell-1.4.1:1.2"
SUBMIT_IMAGE = REGISTRY + "/zoe/spark-submit-1.4.1:1.2"
NOTEBOOK_IMAGE = REGISTRY + "/zoe/spark-notebook-1.4.1:1.2"


class ZoeClient:
23 24 25 26 27
    def __init__(self, rpyc_server=None, rpyc_port=4000):
        if rpyc_server is None:
            self.server_connection = rpyc.connect_by_service("ZoeSchedulerRPC")
        else:
            self.server_connection = rpyc.connect(rpyc_server, rpyc_port)
28 29 30
        self.server = self.server_connection.root
        self.state = AlchemySession()

31 32
    # Users
    def user_new(self, email: str) -> PlainUser:
33 34 35
        user = User(email=email)
        self.state.add(user)
        self.state.commit()
36
        return user.extract()
37

38
    def user_get(self, email: str) -> PlainUser:
39
        user = self.state.query(User).filter_by(email=email).one()
40
        return user.extract()
41

42 43 44 45 46
    def user_check(self, user_id: int) -> bool:
        user = self.state.query(User).filter_by(id=user_id).one()
        return user is not None

    # Platform
47 48 49
    def platform_status(self) -> PlatformStatusReport:
        return self.server.get_platform_status()

50 51
    # Applications
    def spark_application_new(self, user_id: int, worker_count: int, executor_memory: str, executor_cores: int, name: str) -> int:
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        try:
            self.state.query(User).filter_by(id=user_id).one()
        except NoResultFound:
            raise UserIDDoesNotExist(user_id)

        resources = SparkApplicationResources()
        resources.worker_count = worker_count
        resources.container_count = worker_count + 1
        resources.worker_resources["memory_limit"] = executor_memory
        resources.worker_resources["cores"] = executor_cores
        app = SparkApplication(master_image=MASTER_IMAGE,
                               worker_image=WORKER_IMAGE,
                               name=name,
                               required_resources=resources,
                               user_id=user_id)
        self.state.add(app)
        self.state.commit()
69
        return app.id
70

71
    def spark_notebook_application_new(self, user_id: int, worker_count: int, executor_memory: str, executor_cores: int, name: str) -> int:
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        try:
            self.state.query(User).filter_by(id=user_id).one()
        except NoResultFound:
            raise UserIDDoesNotExist(user_id)

        resources = SparkApplicationResources()
        resources.worker_count = worker_count
        resources.container_count = worker_count + 2
        resources.worker_resources["memory_limit"] = executor_memory
        resources.worker_resources["cores"] = executor_cores
        app = SparkNotebookApplication(master_image=MASTER_IMAGE,
                                       worker_image=WORKER_IMAGE,
                                       notebook_image=NOTEBOOK_IMAGE,
                                       name=name,
                                       required_resources=resources,
                                       user_id=user_id)
        self.state.add(app)
        self.state.commit()
90
        return app.id
91

92
    def spark_submit_application_new(self, user_id: int, worker_count: int, executor_memory: str, executor_cores: int, name: str, file: str) -> int:
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        try:
            self.state.query(User).filter_by(id=user_id).one()
        except NoResultFound:
            raise UserIDDoesNotExist(user_id)

        resources = SparkApplicationResources()
        resources.worker_count = worker_count
        resources.container_count = worker_count + 2
        resources.worker_resources["memory_limit"] = executor_memory
        resources.worker_resources["cores"] = executor_cores
        app = SparkSubmitApplication(master_image=MASTER_IMAGE,
                                     worker_image=WORKER_IMAGE,
                                     submit_image=SUBMIT_IMAGE,
                                     name=name,
                                     required_resources=resources,
                                     user_id=user_id)
        self.state.add(app)
        self.state.flush()
        storage.application_data_upload(app, open(file, "rb").read())

        self.state.commit()
114
        return app.id
115

116
    def application_get(self, application_id) -> PlainApplication:
117
        try:
118 119
            ret = self.state.query(Application).filter_by(id=application_id).one()
            return ret.extract()
120 121 122
        except NoResultFound:
            return None

123 124 125 126 127
    def application_remove(self, application_id: int):
        try:
            application = self.state.query(Application).filter_by(id=application_id).one()
        except NoResultFound:
            return
128 129 130
        running = self.state.query(Execution).filter_by(application_id=application.id, time_finished=None).count()
        if running > 0:
            raise ApplicationStillRunning(application)
131 132 133 134 135

        storage.application_data_delete(application)
        for e in application.executions:
            self.execution_delete(e)

136 137 138
        self.state.delete(application)
        self.state.commit()

139 140 141 142 143
    def application_status(self, application_id: int) -> ApplicationStatusReport:
        try:
            application = self.state.query(Application).filter_by(id=application_id).one()
        except NoResultFound:
            return None
144 145
        return self.server.application_status(application.id)

146
    def application_list(self, user_id) -> [PlainApplication]:
147 148 149 150 151
        try:
            self.state.query(User).filter_by(id=user_id).one()
        except NoResultFound:
            raise UserIDDoesNotExist(user_id)

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        apps = self.state.query(Application).filter_by(user_id=user_id).all()
        return [x.extract() for x in apps]

    # Executions
    def execution_spark_new(self, application_id: int, name, commandline=None, spark_options=None) -> bool:
        try:
            application = self.state.query(Application).filter_by(id=application_id).one()
        except NoResultFound:
            return None

        if type(application) is SparkSubmitApplication:
            if commandline is None:
                raise ValueError("Spark submit application requires a commandline")
            execution = SparkSubmitExecution(name=name,
                                             application_id=application.id,
                                             status="submitted",
                                             commandline=commandline,
                                             spark_opts=spark_options)
        else:
            execution = Execution(name=name,
                                  application_id=application.id,
                                  status="submitted")
        self.state.add(execution)
        self.state.commit()
        return self.server.execution_schedule(execution.id)
177

178 179 180 181 182 183
    def execution_get(self, execution_id: int) -> PlainExecution:
        try:
            ret = self.state.query(Execution).filter_by(id=execution_id).one()
        except NoResultFound:
            return None
        return ret.extract()
184

185 186 187 188 189 190 191 192 193 194 195 196
    def execution_terminate(self, execution_id: int):
        try:
            self.state.query(Execution).filter_by(id=execution_id).one()
        except NoResultFound:
            pass
        self.server.terminate_execution(execution_id)

    def execution_delete(self, execution_id: int):
        try:
            execution = self.state.query(Execution).filter_by(id=execution_id).one()
        except NoResultFound:
            return
197

198 199 200 201 202
        if execution.status == "running":
            raise ApplicationStillRunning(execution.application)
        storage.logs_archive_delete(execution)
        self.state.delete(execution)

203
    # Logs
204
    def log_get(self, container_id: int) -> str:
205 206 207 208 209 210
        try:
            self.state.query(Container).filter_by(id=container_id).one()
        except NoResultFound:
            return None
        else:
            return self.server.log_get(container_id)