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 org.apache.hbase.thirdparty.io.netty.util.Timeout; 021 022import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors; 023import org.apache.hbase.thirdparty.com.google.protobuf.Message; 024import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback; 025import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 026 027import java.io.IOException; 028 029import org.apache.hadoop.hbase.CellScanner; 030import org.apache.yetus.audience.InterfaceAudience; 031import org.apache.hadoop.hbase.client.MetricsConnection; 032import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 033import org.apache.htrace.core.Span; 034import org.apache.htrace.core.Tracer; 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 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 @Override 080 public String toString() { 081 return "callId: " + this.id + " methodName: " + this.md.getName() + " param {" 082 + (this.param != null ? ProtobufUtil.getShortTextFormat(this.param) : "") + "}"; 083 } 084 085 /** 086 * called from timeoutTask, prevent self cancel 087 */ 088 public void setTimeout(IOException error) { 089 synchronized (this) { 090 if (done) { 091 return; 092 } 093 this.done = true; 094 this.error = error; 095 } 096 callback.run(this); 097 } 098 099 private void callComplete() { 100 if (timeoutTask != null) { 101 timeoutTask.cancel(); 102 } 103 callback.run(this); 104 } 105 106 /** 107 * Set the exception when there is an error. Notify the caller the call is done. 108 * @param error exception thrown by the call; either local or remote 109 */ 110 public void setException(IOException error) { 111 synchronized (this) { 112 if (done) { 113 return; 114 } 115 this.done = true; 116 this.error = error; 117 } 118 callComplete(); 119 } 120 121 /** 122 * Set the return value when there is no error. Notify the caller the call is done. 123 * @param response return value of the call. 124 * @param cells Can be null 125 */ 126 public void setResponse(Message response, final CellScanner cells) { 127 synchronized (this) { 128 if (done) { 129 return; 130 } 131 this.done = true; 132 this.response = response; 133 this.cells = cells; 134 } 135 callComplete(); 136 } 137 138 public synchronized boolean isDone() { 139 return done; 140 } 141 142 public long getStartTime() { 143 return this.callStats.getStartTime(); 144 } 145}