All Downloads are FREE. Search and download functionalities are using the official Maven repository.

alluxio.master.meta.MetaMasterClientServiceHandler Maven / Gradle / Ivy

There is a newer version: 313
Show newest version
/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.master.meta;

import alluxio.RpcUtils;
import alluxio.RuntimeConstants;
import alluxio.conf.PropertyKey;
import alluxio.conf.ServerConfiguration;
import alluxio.grpc.BackupPOptions;
import alluxio.grpc.BackupPResponse;
import alluxio.grpc.CheckpointPOptions;
import alluxio.grpc.CheckpointPResponse;
import alluxio.grpc.GetConfigReportPOptions;
import alluxio.grpc.GetConfigReportPResponse;
import alluxio.grpc.GetMasterInfoPOptions;
import alluxio.grpc.GetMasterInfoPResponse;
import alluxio.grpc.GetMetricsPOptions;
import alluxio.grpc.GetMetricsPResponse;
import alluxio.grpc.MasterInfo;
import alluxio.grpc.MasterInfoField;
import alluxio.grpc.MetaMasterClientServiceGrpc;
import alluxio.metrics.MetricsSystem;
import alluxio.wire.Address;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import io.grpc.stub.StreamObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * This class is a gRPC handler for meta master RPCs.
 */
public final class MetaMasterClientServiceHandler
    extends MetaMasterClientServiceGrpc.MetaMasterClientServiceImplBase {
  private static final Logger LOG = LoggerFactory.getLogger(MetaMasterClientServiceHandler.class);

  private final MetaMaster mMetaMaster;

  /**
   * @param metaMaster the Alluxio meta master
   */
  public MetaMasterClientServiceHandler(MetaMaster metaMaster) {
    mMetaMaster = metaMaster;
  }

  @Override
  public void backup(BackupPOptions options, StreamObserver responseObserver) {

    RpcUtils.call(LOG, (RpcUtils.RpcCallableThrowsIOException) () -> {
      return mMetaMaster.backup(options).toProto();
    }, "backup", "options=%s", responseObserver, options);
  }

  @Override
  public void getConfigReport(GetConfigReportPOptions options,
      StreamObserver responseObserver) {
    RpcUtils.call(LOG,
        (RpcUtils.RpcCallableThrowsIOException) () -> {

          return GetConfigReportPResponse.newBuilder()
              .setReport(mMetaMaster.getConfigCheckReport().toProto()).build();
        }, "getConfigReport", "options=%s", responseObserver, options);
  }

  @Override
  public void getMasterInfo(GetMasterInfoPOptions options,
      StreamObserver responseObserver) {
    RpcUtils.call(LOG, (RpcUtils.RpcCallableThrowsIOException) () -> {
      MasterInfo.Builder masterInfo = MasterInfo.newBuilder();
      for (MasterInfoField field : options.getFilterCount() > 0 ? options.getFilterList()
          : Arrays.asList(MasterInfoField.values())) {
        switch (field) {
          case LEADER_MASTER_ADDRESS:
            masterInfo.setLeaderMasterAddress(mMetaMaster.getRpcAddress().toString());
            break;
          case MASTER_ADDRESSES:
            masterInfo.addAllMasterAddresses(mMetaMaster.getMasterAddresses().stream()
                .map(Address::toProto).collect(Collectors.toList()));
            break;
          case RPC_PORT:
            masterInfo.setRpcPort(mMetaMaster.getRpcAddress().getPort());
            break;
          case SAFE_MODE:
            masterInfo.setSafeMode(mMetaMaster.isInSafeMode());
            break;
          case START_TIME_MS:
            masterInfo.setStartTimeMs(mMetaMaster.getStartTimeMs());
            break;
          case UP_TIME_MS:
            masterInfo.setUpTimeMs(mMetaMaster.getUptimeMs());
            break;
          case VERSION:
            masterInfo.setVersion(RuntimeConstants.VERSION);
            break;
          case WEB_PORT:
            masterInfo.setWebPort(mMetaMaster.getWebPort());
            break;
          case WORKER_ADDRESSES:
            masterInfo.addAllWorkerAddresses(mMetaMaster.getWorkerAddresses().stream()
                .map(Address::toProto).collect(Collectors.toList()));
            break;
          case ZOOKEEPER_ADDRESSES:
            if (ServerConfiguration.isSet(PropertyKey.ZOOKEEPER_ADDRESS)) {
              masterInfo.addAllZookeeperAddresses(
                  Arrays.asList(ServerConfiguration.get(PropertyKey.ZOOKEEPER_ADDRESS).split(",")));
            }
            break;
          default:
            LOG.warn("Unrecognized meta master info field: " + field);
        }
      }
      return GetMasterInfoPResponse.newBuilder().setMasterInfo(masterInfo).build();
    }, "getMasterInfo", "options=%s", responseObserver, options);
  }

  @Override
  public void getMetrics(GetMetricsPOptions options,
      StreamObserver responseObserver) {
    RpcUtils.call(LOG, (RpcUtils.RpcCallableThrowsIOException) () -> {

      MetricRegistry mr = MetricsSystem.METRIC_REGISTRY;
      Map metricsMap = new HashMap<>();

      for (Map.Entry entry : mr.getCounters().entrySet()) {
        metricsMap.put(MetricsSystem.stripInstanceAndHost(entry.getKey()), alluxio.grpc.MetricValue
            .newBuilder().setLongValue(entry.getValue().getCount()).build());
      }

      for (Map.Entry entry : mr.getGauges().entrySet()) {
        Object value = entry.getValue().getValue();
        if (value instanceof Integer) {
          metricsMap.put(entry.getKey(), alluxio.grpc.MetricValue.newBuilder()
              .setLongValue(Long.valueOf((Integer) value)).build());
        } else if (value instanceof Long) {
          metricsMap.put(entry.getKey(), alluxio.grpc.MetricValue.newBuilder()
              .setLongValue(Long.valueOf((Long) value)).build());
        } else if (value instanceof Double) {
          metricsMap.put(entry.getKey(),
              alluxio.grpc.MetricValue.newBuilder().setDoubleValue((Double) value).build());
        }
      }
      return GetMetricsPResponse.newBuilder().putAllMetrics(metricsMap).build();
    }, "getConfiguration", "options=%s", responseObserver, options);
  }

  @Override
  public void checkpoint(CheckpointPOptions options,
      StreamObserver responseObserver) {
    RpcUtils.call(LOG, (RpcUtils.RpcCallableThrowsIOException) () ->
        CheckpointPResponse.newBuilder().setMasterHostname(mMetaMaster.checkpoint()).build(),
        "checkpoint", "options=%s", responseObserver, options);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy