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.ExtendedCellScanner; 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 ExtendedCellScanner 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, 067 final ExtendedCellScanner cells, final Message responseDefaultType, int timeout, int priority, 068 Map<String, byte[]> attributes, RpcCallback<Call> callback, 069 MetricsConnection.CallStats callStats) { 070 this.param = param; 071 this.md = md; 072 this.cells = cells; 073 this.callStats = callStats; 074 this.callStats.setStartTime(EnvironmentEdgeManager.currentTime()); 075 this.responseDefaultType = responseDefaultType; 076 this.id = id; 077 this.timeout = timeout; 078 this.priority = priority; 079 this.attributes = attributes; 080 this.callback = callback; 081 this.span = Span.current(); 082 } 083 084 /** 085 * Builds a simplified {@link #toString()} that includes just the id and method name. 086 */ 087 public String toShortString() { 088 // Call[id=32153218,methodName=Get] 089 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("id", id) 090 .append("methodName", md != null ? md.getName() : "").toString(); 091 } 092 093 @Override 094 public String toString() { 095 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(toShortString()) 096 .append("param", param != null ? ProtobufUtil.getShortTextFormat(param) : "").toString(); 097 } 098 099 /** 100 * called from timeoutTask, prevent self cancel 101 */ 102 public void setTimeout(IOException error) { 103 synchronized (this) { 104 if (done) { 105 return; 106 } 107 this.done = true; 108 this.error = error; 109 } 110 callback.run(this); 111 } 112 113 private void callComplete() { 114 if (timeoutTask != null) { 115 timeoutTask.cancel(); 116 } 117 callback.run(this); 118 } 119 120 /** 121 * Set the exception when there is an error. Notify the caller the call is done. 122 * @param error exception thrown by the call; either local or remote 123 */ 124 public void setException(IOException error) { 125 synchronized (this) { 126 if (done) { 127 return; 128 } 129 this.done = true; 130 this.error = error; 131 } 132 callComplete(); 133 } 134 135 /** 136 * Set the return value when there is no error. Notify the caller the call is done. 137 * @param response return value of the call. 138 * @param cells Can be null 139 */ 140 public void setResponse(Message response, final ExtendedCellScanner cells) { 141 synchronized (this) { 142 if (done) { 143 return; 144 } 145 this.done = true; 146 this.response = response; 147 this.cells = cells; 148 } 149 callComplete(); 150 } 151 152 public synchronized boolean isDone() { 153 return done; 154 } 155 156 public long getStartTime() { 157 return this.callStats.getStartTime(); 158 } 159 160 public boolean isConnectionRegistryCall() { 161 return md.getService().equals(ConnectionRegistryService.getDescriptor()); 162 } 163}