View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.TreeMap;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  /**
31   * A container for Result objects, grouped by regionName.
32   */
33  @InterfaceAudience.Private
34  public class MultiResponse extends AbstractResponse {
35  
36    // map of regionName to map of Results by the original index for that Result
37    private Map<byte[], RegionResult> results = new TreeMap<>(Bytes.BYTES_COMPARATOR);
38  
39    /**
40     * The server can send us a failure for the region itself, instead of individual failure.
41     * It's a part of the protobuf definition.
42     */
43    private Map<byte[], Throwable> exceptions =
44        new TreeMap<byte[], Throwable>(Bytes.BYTES_COMPARATOR);
45  
46    public MultiResponse() {
47      super();
48    }
49  
50    /**
51     * @return Number of pairs in this container
52     */
53    public int size() {
54      int size = 0;
55      for (RegionResult result: results.values()) {
56        size += result.size();
57      }
58      return size;
59    }
60  
61    /**
62     * Add the pair to the container, grouped by the regionName
63     *
64     * @param regionName
65     * @param originalIndex the original index of the Action (request).
66     * @param resOrEx the result or error; will be empty for successful Put and Delete actions.
67     */
68    public void add(byte[] regionName, int originalIndex, Object resOrEx) {
69      getResult(regionName).addResult(originalIndex, resOrEx);
70    }
71  
72    public void addException(byte []regionName, Throwable ie){
73      exceptions.put(regionName, ie);
74    }
75  
76    /**
77     * @return the exception for the region, if any. Null otherwise.
78     */
79    public Throwable getException(byte []regionName){
80      return exceptions.get(regionName);
81    }
82  
83    public Map<byte[], Throwable> getExceptions() {
84      return exceptions;
85    }
86  
87    public void addStatistic(byte[] regionName, ClientProtos.RegionLoadStats stat) {
88      getResult(regionName).setStat(stat);
89    }
90  
91    private RegionResult getResult(byte[] region){
92      RegionResult rs = results.get(region);
93      if (rs == null) {
94        rs = new RegionResult();
95        results.put(region, rs);
96      }
97      return rs;
98    }
99  
100   public Map<byte[], RegionResult> getResults(){
101     return this.results;
102   }
103 
104   @Override
105   public ResponseType type() {
106     return ResponseType.MULTI;
107   }
108
109   static class RegionResult{
110     Map<Integer, Object> result = new HashMap<>();
111     ClientProtos.RegionLoadStats stat;
112
113     public void addResult(int index, Object result){
114       this.result.put(index, result);
115     }
116
117     public void setStat(ClientProtos.RegionLoadStats stat){
118       this.stat = stat;
119     }
120
121     public int size() {
122       return this.result.size();
123     }
124
125     public ClientProtos.RegionLoadStats getStat() {
126       return this.stat;
127     }
128   }
129 }