001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.master.procedure;
020
021import java.io.IOException;
022import java.util.Date;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.stream.Collectors;
027
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.yetus.audience.InterfaceStability;
030import org.apache.hadoop.hbase.procedure2.Procedure;
031import org.apache.hadoop.hbase.procedure2.ProcedureUtil;
032import org.apache.hadoop.hbase.protobuf.ProtobufMessageConverter;
033import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos;
035import org.apache.hadoop.hbase.util.JRubyFormat;
036
037@InterfaceAudience.Private
038@InterfaceStability.Evolving
039public class ProcedureDescriber {
040  private ProcedureDescriber() {
041  }
042
043  private static Object parametersToObject(Procedure<?> proc) {
044    try {
045      ProcedureProtos.Procedure protoProc = ProcedureUtil.convertToProtoProcedure(proc);
046      List<Object> parameters = protoProc.getStateMessageList().stream()
047        .map((any) -> {
048          try {
049            return ProtobufMessageConverter.toJavaObject(any);
050          } catch (InvalidProtocolBufferException e) {
051            return e.toString();
052          }
053        }).collect(Collectors.toList());
054      return parameters;
055    } catch (IOException e) {
056      return e.toString();
057    }
058  }
059
060  public static String describe(Procedure<?> proc) {
061    Map<String, Object> description = new LinkedHashMap<>();
062
063    description.put("ID", proc.getProcId());
064    description.put("PARENT_ID", proc.getParentProcId());
065    description.put("STATE", proc.getState());
066    description.put("OWNER", proc.getOwner());
067    description.put("TYPE", proc.getProcName());
068    description.put("START_TIME", new Date(proc.getSubmittedTime()));
069    description.put("LAST_UPDATE", new Date(proc.getLastUpdate()));
070
071    if (proc.isFailed()) {
072      description.put("ERRORS",
073          MasterProcedureUtil.unwrapRemoteIOException(proc).getMessage());
074    }
075    description.put("PARAMETERS", parametersToObject(proc));
076
077    return JRubyFormat.print(description);
078  }
079
080  public static String describeParameters(Procedure<?> proc) {
081    Object object = parametersToObject(proc);
082    return JRubyFormat.print(object);
083  }
084}