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.ipc;
019
020import io.opentelemetry.api.trace.Span;
021import java.io.IOException;
022import java.util.Map;
023import org.apache.commons.lang3.builder.ToStringBuilder;
024import org.apache.commons.lang3.builder.ToStringStyle;
025import org.apache.hadoop.hbase.CellScanner;
026import org.apache.hadoop.hbase.client.MetricsConnection;
027import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
028import org.apache.yetus.audience.InterfaceAudience;
029
030import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors;
031import org.apache.hbase.thirdparty.com.google.protobuf.Message;
032import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback;
033import org.apache.hbase.thirdparty.io.netty.util.Timeout;
034
035import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
036import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.ConnectionRegistryService;
037
038/** A call waiting for a value. */
039@InterfaceAudience.Private
040class Call {
041  final int id; // call id
042  final Message param; // rpc request method param object
043  /**
044   * Optionally has cells when making call. Optionally has cells set on response. Used passing cells
045   * to the rpc and receiving the response.
046   */
047  CellScanner cells;
048  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",
049      justification = "Direct access is only allowed after done")
050  Message response; // value, null if error
051  // The return type. Used to create shell into which we deserialize the response if any.
052  Message responseDefaultType;
053  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",
054      justification = "Direct access is only allowed after done")
055  IOException error; // exception, null if value
056  private boolean done; // true when call is done
057  final Descriptors.MethodDescriptor md;
058  final int timeout; // timeout in millisecond for this call; 0 means infinite.
059  final int priority;
060  final Map<String, byte[]> attributes;
061  final MetricsConnection.CallStats callStats;
062  private final RpcCallback<Call> callback;
063  final Span span;
064  Timeout timeoutTask;
065
066  Call(int id, final Descriptors.MethodDescriptor md, Message param, final CellScanner cells,
067    final Message responseDefaultType, int timeout, int priority, Map<String, byte[]> attributes,
068    RpcCallback<Call> callback, MetricsConnection.CallStats callStats) {
069    this.param = param;
070    this.md = md;
071    this.cells = cells;
072    this.callStats = callStats;
073    this.callStats.setStartTime(EnvironmentEdgeManager.currentTime());
074    this.responseDefaultType = responseDefaultType;
075    this.id = id;
076    this.timeout = timeout;
077    this.priority = priority;
078    this.attributes = attributes;
079    this.callback = callback;
080    this.span = Span.current();
081  }
082
083  /**
084   * Builds a simplified {@link #toString()} that includes just the id and method name.
085   */
086  public String toShortString() {
087    // Call[id=32153218,methodName=Get]
088    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("id", id)
089      .append("methodName", md != null ? md.getName() : "").toString();
090  }
091
092  @Override
093  public String toString() {
094    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(toShortString())
095      .append("param", param != null ? ProtobufUtil.getShortTextFormat(param) : "").toString();
096  }
097
098  /**
099   * called from timeoutTask, prevent self cancel
100   */
101  public void setTimeout(IOException error) {
102    synchronized (this) {
103      if (done) {
104        return;
105      }
106      this.done = true;
107      this.error = error;
108    }
109    callback.run(this);
110  }
111
112  private void callComplete() {
113    if (timeoutTask != null) {
114      timeoutTask.cancel();
115    }
116    callback.run(this);
117  }
118
119  /**
120   * Set the exception when there is an error. Notify the caller the call is done.
121   * @param error exception thrown by the call; either local or remote
122   */
123  public void setException(IOException error) {
124    synchronized (this) {
125      if (done) {
126        return;
127      }
128      this.done = true;
129      this.error = error;
130    }
131    callComplete();
132  }
133
134  /**
135   * Set the return value when there is no error. Notify the caller the call is done.
136   * @param response return value of the call.
137   * @param cells    Can be null
138   */
139  public void setResponse(Message response, final CellScanner cells) {
140    synchronized (this) {
141      if (done) {
142        return;
143      }
144      this.done = true;
145      this.response = response;
146      this.cells = cells;
147    }
148    callComplete();
149  }
150
151  public synchronized boolean isDone() {
152    return done;
153  }
154
155  public long getStartTime() {
156    return this.callStats.getStartTime();
157  }
158
159  public boolean isConnectionRegistryCall() {
160    return md.getService().equals(ConnectionRegistryService.getDescriptor());
161  }
162}