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 */
018package org.apache.hadoop.hbase.master.procedure;
019
020import java.io.IOException;
021import java.util.Date;
022import java.util.LinkedHashMap;
023import java.util.List;
024import java.util.Map;
025import java.util.stream.Collectors;
026import org.apache.hadoop.hbase.procedure2.Procedure;
027import org.apache.hadoop.hbase.procedure2.ProcedureUtil;
028import org.apache.hadoop.hbase.protobuf.ProtobufMessageConverter;
029import org.apache.hadoop.hbase.util.JRubyFormat;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.apache.yetus.audience.InterfaceStability;
032
033import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
034
035import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos;
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().map((any) -> {
047        try {
048          return ProtobufMessageConverter.toJavaObject(any);
049        } catch (InvalidProtocolBufferException e) {
050          return e.toString();
051        }
052      }).collect(Collectors.toList());
053      return parameters;
054    } catch (IOException e) {
055      return e.toString();
056    }
057  }
058
059  public static String describe(Procedure<?> proc) {
060    Map<String, Object> description = new LinkedHashMap<>();
061
062    description.put("ID", proc.getProcId());
063    description.put("PARENT_ID", proc.getParentProcId());
064    description.put("STATE", proc.getState());
065    description.put("OWNER", proc.getOwner());
066    description.put("TYPE", proc.getProcName());
067    description.put("START_TIME", new Date(proc.getSubmittedTime()));
068    description.put("LAST_UPDATE", new Date(proc.getLastUpdate()));
069
070    if (proc.isFailed()) {
071      description.put("ERRORS", MasterProcedureUtil.unwrapRemoteIOException(proc).getMessage());
072    }
073    description.put("PARAMETERS", parametersToObject(proc));
074
075    return JRubyFormat.print(description);
076  }
077
078  public static String describeParameters(Procedure<?> proc) {
079    Object object = parametersToObject(proc);
080    return JRubyFormat.print(object);
081  }
082}