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