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 java.io.IOException; 021import java.util.Optional; 022import org.apache.commons.lang3.builder.ToStringBuilder; 023import org.apache.commons.lang3.builder.ToStringStyle; 024import org.apache.hadoop.hbase.CellScanner; 025import org.apache.hadoop.hbase.client.MetricsConnection; 026import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 027import org.apache.htrace.core.Span; 028import org.apache.htrace.core.Tracer; 029import org.apache.yetus.audience.InterfaceAudience; 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; 034import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 035 036/** A call waiting for a value. */ 037@InterfaceAudience.Private 038class Call { 039 final int id; // call id 040 final Message param; // rpc request method param object 041 /** 042 * Optionally has cells when making call. Optionally has cells set on response. Used passing cells 043 * to the rpc and receiving the response. 044 */ 045 CellScanner cells; 046 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC", 047 justification = "Direct access is only allowed after done") 048 Message response; // value, null if error 049 // The return type. Used to create shell into which we deserialize the response if any. 050 Message responseDefaultType; 051 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC", 052 justification = "Direct access is only allowed after done") 053 IOException error; // exception, null if value 054 private boolean done; // true when call is done 055 final Descriptors.MethodDescriptor md; 056 final int timeout; // timeout in millisecond for this call; 0 means infinite. 057 final int priority; 058 final MetricsConnection.CallStats callStats; 059 private final RpcCallback<Call> callback; 060 final Span span; 061 Timeout timeoutTask; 062 063 protected Call(int id, final Descriptors.MethodDescriptor md, Message param, 064 final CellScanner cells, final Message responseDefaultType, int timeout, int priority, 065 RpcCallback<Call> callback, MetricsConnection.CallStats callStats) { 066 this.param = param; 067 this.md = md; 068 this.cells = cells; 069 this.callStats = callStats; 070 this.callStats.setStartTime(EnvironmentEdgeManager.currentTime()); 071 this.responseDefaultType = responseDefaultType; 072 this.id = id; 073 this.timeout = timeout; 074 this.priority = priority; 075 this.callback = callback; 076 this.span = Tracer.getCurrentSpan(); 077 } 078 079 /** 080 * Builds a simplified {@link #toString()} that includes just the id and method name. 081 */ 082 public String toShortString() { 083 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 084 .append("id", id) 085 .append("methodName", md.getName()) 086 .toString(); 087 } 088 089 @Override 090 public String toString() { 091 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 092 .appendSuper(toShortString()) 093 .append("param", Optional.ofNullable(param) 094 .map(ProtobufUtil::getShortTextFormat) 095 .orElse("")) 096 .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 CellScanner 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}