Example #1
0
def _convert_java_value(kv, include_non_primitives=True):
    try:
        pobj = javaobj.loads(kv.serialized_value)
        if pyutils.is_str_instance(pobj):
            return pobj

        if pobj.is_primitive():
            return pobj.value

        if include_non_primitives:
            # java objects that are not strings return value and encoded value
            # Hexadecimal byte array for Serialized objects that
            return {
                'value':
                json.dumps(pobj,
                           default=lambda custom_field: custom_field.__dict__,
                           sort_keys=True,
                           indent=2),
                'raw':
                utils.hex_escape(kv.serialized_value)
            }

        return None
    except Exception:
        Log.exception("Failed to parse data as java object")
        if include_non_primitives:
            return _raw_value(kv)
        else:
            return None
Example #2
0
def _convert_java_value(kv, include_non_primitives=True):
    try:
        pobj = javaobj.loads(kv.serialized_value)
        if isinstance(pobj, (str, int, float, bool)):
            return pobj

        if hasattr(pobj, 'value'):
            return pobj.value

        if include_non_primitives:
            # java objects that are not strings return value and encoded value
            # Hexadecimal byte array for Serialized objects that
            return {
                'value':
                json.dumps(pobj,
                           default=lambda custom_field: custom_field.__dict__,
                           sort_keys=True,
                           indent=2),
                'raw':
                kv.serialized_value.hex()
            }

        return None
    except Exception:
        Log.exception("Failed to parse data as java object")
        if include_non_primitives:
            return _raw_value(kv)
        return None
Example #3
0
def _convert_java_value(kv, include_non_primitives=True):
  try:
    pobj = javaobj.loads(kv.serialized_value)
    if pyutils.is_str_instance(pobj):
      return pobj

    if pobj.is_primitive():
      return pobj.value

    if include_non_primitives:
      # java objects that are not strings return value and encoded value
      # Hexadecimal byte array for Serialized objects that
      return {
          'value' : json.dumps(pobj,
                               default=lambda custom_field: custom_field.__dict__,
                               sort_keys=True,
                               indent=2),
          'raw' : utils.hex_escape(kv.serialized_value)}

    return None
  except Exception:
    Log.exception("Failed to parse data as java object")
    if include_non_primitives:
      return _raw_value(kv)
    else:
      return None
Example #4
0
def launch_topologies(cl_args, topology_file, tmp_dir):
    '''
  Launch topologies
  :param cl_args:
  :param topology_file:
  :param tmp_dir:
  :return:
  '''
    # the submitter would have written the .defn file to the tmp_dir
    defn_files = glob.glob(tmp_dir + '/*.defn')

    if len(defn_files) == 0:
        raise Exception("No topologies found")

    try:
        for defn_file in defn_files:

            # load the topology definition from the file
            topology_defn = topology_pb2.Topology()
            try:
                handle = open(defn_file, "rb")
                topology_defn.ParseFromString(handle.read())
                handle.close()

            except:
                raise Exception(
                    "Could not open and parse topology defn file %s" %
                    defn_file)

            # launch the topology
            try:
                Log.info("Launching topology \'%s\'" % topology_defn.name)
                launch_a_topology(cl_args, tmp_dir, topology_file, defn_file)
                Log.info("Topology \'%s\' launched successfully" %
                         topology_defn.name)

            except Exception as ex:
                Log.exception('Failed to launch topology \'%s\' because %s' %
                              (topology_defn.name, str(ex)))
                raise

    except:
        raise
Example #5
0
def launch_topologies(cl_args, topology_file, tmp_dir):
  '''
  Launch topologies
  :param cl_args:
  :param topology_file:
  :param tmp_dir:
  :return:
  '''
  # the submitter would have written the .defn file to the tmp_dir
  defn_files = glob.glob(tmp_dir + '/*.defn')

  if len(defn_files) == 0:
    raise Exception("No topologies found")

  try:
    for defn_file in defn_files:

      # load the topology definition from the file
      topology_defn = topology_pb2.Topology()
      try:
        handle = open(defn_file, "rb")
        topology_defn.ParseFromString(handle.read())
        handle.close()

      except:
        raise Exception("Could not open and parse topology defn file %s" % defn_file)

      # launch the topology
      try:
        Log.info("Launching topology \'%s\'" % topology_defn.name)
        launch_a_topology(cl_args, tmp_dir, topology_file, defn_file)
        Log.info("Topology \'%s\' launched successfully" % topology_defn.name)

      except Exception as ex:
        Log.exception('Failed to launch topology \'%s\' because %s' % (topology_defn.name, str(ex)))
        raise

  except:
    raise
Example #6
0
  def extract_physical_plan(self, topology):
    """
    Returns the representation of physical plan that will
    be returned from Tracker.
    """
    physicalPlan = {
        "instances": {},
        "instance_groups": {},
        "stmgrs": {},
        "spouts": {},
        "bolts": {},
        "config": {},
    }

    if not topology.physical_plan:
      return physicalPlan

    spouts = topology.spouts()
    bolts = topology.bolts()
    stmgrs = None
    instances = None

    # Physical Plan
    stmgrs = list(topology.physical_plan.stmgrs)
    instances = list(topology.physical_plan.instances)

    # Configs
    if topology.physical_plan.topology.topology_config:
      for kvs in topology.physical_plan.topology.topology_config.kvs:
        if kvs.value:
          physicalPlan["config"][kvs.key] = kvs.value
        elif kvs.serialized_value:
          # currently assumes that serialized_value is Java serialization
          # when multi-language support is added later, ConfigValueType should be checked

          # Hexadecimal byte array for Serialized objects
          try:
            pobj = javaobj.loads(kvs.serialized_value)
            physicalPlan["config"][kvs.key] = {
                'value' : json.dumps(pobj,
                                     default=lambda custom_field: custom_field.__dict__,
                                     sort_keys=True,
                                     indent=2),
                'raw' : utils.hex_escape(kvs.serialized_value)}
          except Exception:
            Log.exception("Failed to parse data as java object")
            physicalPlan["config"][kvs.key] = {
                # The value should be a valid json object
                'value' : '{}',
                'raw' : utils.hex_escape(kvs.serialized_value)}
    for spout in spouts:
      spout_name = spout.comp.name
      physicalPlan["spouts"][spout_name] = []
    for bolt in bolts:
      bolt_name = bolt.comp.name
      physicalPlan["bolts"][bolt_name] = []

    for stmgr in stmgrs:
      host = stmgr.host_name
      cwd = stmgr.cwd
      shell_port = stmgr.shell_port if stmgr.HasField("shell_port") else None
      physicalPlan["stmgrs"][stmgr.id] = {
          "id": stmgr.id,
          "host": host,
          "port": stmgr.data_port,
          "shell_port": shell_port,
          "cwd": cwd,
          "pid": stmgr.pid,
          "joburl": utils.make_shell_job_url(host, shell_port, cwd),
          "logfiles": utils.make_shell_logfiles_url(host, shell_port, cwd),
          "instance_ids": []
      }

    instance_groups = collections.OrderedDict()
    for instance in instances:
      instance_id = instance.instance_id
      stmgrId = instance.stmgr_id
      name = instance.info.component_name
      stmgrInfo = physicalPlan["stmgrs"][stmgrId]
      host = stmgrInfo["host"]
      cwd = stmgrInfo["cwd"]
      shell_port = stmgrInfo["shell_port"]

      index = int(instance.info.component_index) + 1
      group_name = "container_%d" % index
      igroup = instance_groups.get(group_name, list())
      igroup.append(instance_id)
      instance_groups[group_name] = igroup

      physicalPlan["instances"][instance_id] = {
          "id": instance_id,
          "name": name,
          "stmgrId": stmgrId,
          "logfile": utils.make_shell_logfiles_url(host, shell_port, cwd, instance.instance_id),
      }
      physicalPlan["stmgrs"][stmgrId]["instance_ids"].append(instance_id)
      if name in physicalPlan["spouts"]:
        physicalPlan["spouts"][name].append(instance_id)
      else:
        physicalPlan["bolts"][name].append(instance_id)

    physicalPlan["instance_groups"] = instance_groups

    return physicalPlan
Example #7
0
  def extract_physical_plan(self, topology):
    """
    Returns the representation of physical plan that will
    be returned from Tracker.
    """
    physicalPlan = {
        "instances": {},
        "stmgrs": {},
        "spouts": {},
        "bolts": {},
        "config": {},
    }

    if not topology.physical_plan:
      return physicalPlan

    spouts = topology.spouts()
    bolts = topology.bolts()
    stmgrs = None
    instances = None

    # Physical Plan
    stmgrs = list(topology.physical_plan.stmgrs)
    instances = list(topology.physical_plan.instances)

    # Configs
    if topology.physical_plan.topology.topology_config:
      for kvs in topology.physical_plan.topology.topology_config.kvs:
        if kvs.value:
          physicalPlan["config"][kvs.key] = kvs.value
        elif kvs.serialized_value:
          # currently assumes that serialized_value is Java serialization
          # when multi-language support is added later, ConfigValueType should be checked

          # Hexadecimal byte array for Serialized objects
          try:
            pobj = javaobj.loads(kvs.serialized_value)
            physicalPlan["config"][kvs.key] = {
                'value' : json.dumps(pobj,
                                     default=lambda custom_field: custom_field.__dict__,
                                     sort_keys=True,
                                     indent=2),
                'raw' : utils.hex_escape(kvs.serialized_value)}
          except Exception:
            Log.exception("Failed to parse data as java object")
            physicalPlan["config"][kvs.key] = {
                # The value should be a valid json object
                'value' : '{}',
                'raw' : utils.hex_escape(kvs.serialized_value)}
    for spout in spouts:
      spout_name = spout.comp.name
      physicalPlan["spouts"][spout_name] = []
    for bolt in bolts:
      bolt_name = bolt.comp.name
      physicalPlan["bolts"][bolt_name] = []

    for stmgr in stmgrs:
      host = stmgr.host_name
      cwd = stmgr.cwd
      shell_port = stmgr.shell_port if stmgr.HasField("shell_port") else None
      physicalPlan["stmgrs"][stmgr.id] = {
          "id": stmgr.id,
          "host": host,
          "port": stmgr.data_port,
          "shell_port": shell_port,
          "cwd": cwd,
          "pid": stmgr.pid,
          "joburl": utils.make_shell_job_url(host, shell_port, cwd),
          "logfiles": utils.make_shell_logfiles_url(host, shell_port, cwd),
          "instance_ids": []
      }

    for instance in instances:
      instance_id = instance.instance_id
      stmgrId = instance.stmgr_id
      name = instance.info.component_name
      stmgrInfo = physicalPlan["stmgrs"][stmgrId]
      host = stmgrInfo["host"]
      cwd = stmgrInfo["cwd"]
      shell_port = stmgrInfo["shell_port"]

      physicalPlan["instances"][instance_id] = {
          "id": instance_id,
          "name": name,
          "stmgrId": stmgrId,
          "logfile": utils.make_shell_logfiles_url(host, shell_port, cwd, instance.instance_id),
      }
      physicalPlan["stmgrs"][stmgrId]["instance_ids"].append(instance_id)
      if name in physicalPlan["spouts"]:
        physicalPlan["spouts"][name].append(instance_id)
      else:
        physicalPlan["bolts"][name].append(instance_id)

    return physicalPlan