You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/09/16 13:23:32 UTC

[GitHub] [flink] WeiZhong94 commented on a change in pull request #9655: [FLINK-14017][python] Support to start up Python worker in process mode.

WeiZhong94 commented on a change in pull request #9655: [FLINK-14017][python] Support to start up Python worker in process mode.
URL: https://github.com/apache/flink/pull/9655#discussion_r324671540
 
 

 ##########
 File path: flink-python/pyflink/fn_execution/boot.py
 ##########
 @@ -0,0 +1,352 @@
+#!/usr/bin/env python
+#################################################################################
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+# limitations under the License.
+################################################################################
+"""
+This script is a python implementation of the "boot.go" script in "beam-sdks-python-container"
+project of Apache Beam, see in:
+
+https://github.com/apache/beam/blob/release-2.14.0/sdks/python/container/boot.go
+
+Its original version is implemented by golang and involves unnecessary dependencies, so we
+re-implemented and modified it in python to support running in process mode and
+custom operators.
+
+It downloads and installs users' python artifacts, then launches the python SDK harness of
+Apache Beam.
+
+"""
+import argparse
+import hashlib
+import os
+from subprocess import call
+
+import grpc
+import logging
+import sys
+
+from apache_beam.portability.api.beam_provision_api_pb2_grpc import ProvisionServiceStub
+from apache_beam.portability.api.beam_provision_api_pb2 import GetProvisionInfoRequest
+from apache_beam.portability.api.beam_artifact_api_pb2_grpc import ArtifactRetrievalServiceStub
+from apache_beam.portability.api.beam_artifact_api_pb2 import (GetManifestRequest,
+                                                               GetArtifactRequest)
+from apache_beam.portability.api.endpoints_pb2 import ApiServiceDescriptor
+
+from google.protobuf import json_format, text_format
+
+parser = argparse.ArgumentParser()
+
+parser.add_argument("--id", default="", help="Local identifier (required).")
+parser.add_argument("--logging_endpoint", default="",
+                    help="Logging endpoint (required).")
+parser.add_argument("--artifact_endpoint", default="",
+                    help="Artifact endpoint (required).")
+parser.add_argument("--provision_endpoint", default="",
+                    help="Provision endpoint (required).")
+parser.add_argument("--control_endpoint", default="",
+                    help="Control endpoint (required).")
+parser.add_argument("--semi_persist_dir", default="/tmp",
+                    help="Local semi-persistent directory (optional).")
+
+args = parser.parse_args()
+
+worker_id = args.id
+logging_endpoint = args.logging_endpoint
+artifact_endpoint = args.artifact_endpoint
+provision_endpoint = args.provision_endpoint
+control_endpoint = args.control_endpoint
+semi_persist_dir = args.semi_persist_dir
+
+if worker_id == "":
+    logging.fatal("No id provided.")
+
+if logging_endpoint == "":
+    logging.fatal("No logging endpoint provided.")
+
+if artifact_endpoint == "":
+    logging.fatal("No artifact endpoint provided.")
+
+if provision_endpoint == "":
+    logging.fatal("No provision endpoint provided.")
+
+if control_endpoint == "":
+    logging.fatal("No control endpoint provided.")
+
+logging.info("Initializing python harness: %s" % " ".join(sys.argv))
+
+metadata = [("worker_id", worker_id)]
+
+# read job information from provision stub
+with grpc.insecure_channel(provision_endpoint) as channel:
+    client = ProvisionServiceStub(channel=channel)
+    info = client.GetProvisionInfo(GetProvisionInfoRequest(), metadata=metadata).info
+    options = json_format.MessageToJson(info.pipeline_options)
+
+staged_dir = os.path.join(semi_persist_dir, "staged")
+files = []
+
+# download files
+with grpc.insecure_channel(artifact_endpoint) as channel:
+    client = ArtifactRetrievalServiceStub(channel=channel)
+    # get file list via retrieval token
+    response = client.GetManifest(GetManifestRequest(retrieval_token=info.retrieval_token),
+                                  metadata=metadata)
+    artifacts = response.manifest.artifact
+    # download files and check hash values
+    for artifact in artifacts:
+        name = artifact.name
+        files.append(name)
+        permissions = artifact.permissions
+        sha256 = artifact.sha256
+        file_path = os.path.join(staged_dir, name)
+        if os.path.exists(file_path):
+            with open(file_path, "rb") as f:
+                sha256obj = hashlib.sha256()
+                sha256obj.update(f.read())
+                hash_value = sha256obj.hexdigest()
+            if hash_value == sha256:
+                logging.info("file exist and has same sha256 hash value, skip...")
+                continue
+            else:
+                os.remove(file_path)
+        if not os.path.exists(os.path.dirname(file_path)):
+            os.makedirs(os.path.dirname(file_path), 0o755)
+        stream = client.GetArtifact(
+            GetArtifactRequest(name=name, retrieval_token=info.retrieval_token), metadata=metadata)
+        with open(file_path, "wb") as f:
+            sha256obj = hashlib.sha256()
+            for artifact_chunk in stream:
+                sha256obj.update(artifact_chunk.data)
+                f.write(artifact_chunk.data)
+            hash_value = sha256obj.hexdigest()
+        if hash_value != sha256:
+            raise Exception("sha256 not consist!")
+        os.chmod(file_path, permissions)
+
+acceptable_whl_specs = [".whl"]
+
+
+def find_beam_sdk_whl():
+    """
+    Find apache beam sdk wheel package in downloaded files. If found, it will be installed.
+
+    :return: the file name of apache beam sdk wheel package.
+    """
+    for filename in files:
+        if filename.startswith("apache_beam"):
+            for acceptable_whl_spec in acceptable_whl_specs:
+                if filename.endswith(acceptable_whl_spec):
+                    logging.info("Found Apache Beam SDK wheel: %s" % filename)
+                    return filename
+    return ""
+
+
+def python_location():
+    """
+    Returns the python executable file location.
+    If the environment variable "python" exists, using its value.
+
+    :return: The python executable file location. Default value is "python".
+    """
+    if "python" in os.environ:
+        return os.environ["python"]
+    else:
+        return "python"
+
+
+def pip_command():
 
 Review comment:
   In some case it returns a command (e.g. python -m pip) instead of a pip location, maybe "pip_command" makes more sense here?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services