1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.client;
22
23 import org.apache.hadoop.hbase.DoNotRetryIOException;
24 import org.apache.hadoop.hbase.HRegionInfo;
25 import org.apache.hadoop.hbase.NotServingRegionException;
26 import org.apache.hadoop.hbase.RemoteExceptionHandler;
27 import org.apache.hadoop.ipc.RemoteException;
28 import org.mortbay.log.Log;
29
30 import java.io.IOException;
31
32
33
34
35
36
37 public class ScannerCallable extends ServerCallable<Result[]> {
38 private long scannerId = -1L;
39 private boolean instantiated = false;
40 private boolean closed = false;
41 private Scan scan;
42 private int caching = 1;
43
44
45
46
47
48
49 public ScannerCallable (HConnection connection, byte [] tableName, Scan scan) {
50 super(connection, tableName, scan.getStartRow());
51 this.scan = scan;
52 }
53
54
55
56
57
58 @Override
59 public void instantiateServer(boolean reload) throws IOException {
60 if (!instantiated || reload) {
61 super.instantiateServer(reload);
62 instantiated = true;
63 }
64 }
65
66
67
68
69 public Result [] call() throws IOException {
70 if (scannerId != -1L && closed) {
71 close();
72 } else if (scannerId == -1L && !closed) {
73 this.scannerId = openScanner();
74 } else {
75 Result [] rrs = null;
76 try {
77 rrs = server.next(scannerId, caching);
78 } catch (IOException e) {
79 IOException ioe = null;
80 if (e instanceof RemoteException) {
81 ioe = RemoteExceptionHandler.decodeRemoteException((RemoteException)e);
82 }
83 if (ioe == null) throw new IOException(e);
84 if (ioe instanceof NotServingRegionException) {
85
86
87
88 throw new DoNotRetryIOException("Reset scanner", ioe);
89 } else {
90
91 throw ioe;
92 }
93 }
94 return rrs;
95 }
96 return null;
97 }
98
99 private void close() {
100 if (this.scannerId == -1L) {
101 return;
102 }
103 try {
104 this.server.close(this.scannerId);
105 } catch (IOException e) {
106 Log.warn("Ignore, probably already closed", e);
107 }
108 this.scannerId = -1L;
109 }
110
111 protected long openScanner() throws IOException {
112 return this.server.openScanner(this.location.getRegionInfo().getRegionName(),
113 this.scan);
114 }
115
116 protected Scan getScan() {
117 return scan;
118 }
119
120
121
122
123 public void setClose() {
124 this.closed = true;
125 }
126
127
128
129
130 public HRegionInfo getHRegionInfo() {
131 if (!instantiated) {
132 return null;
133 }
134 return location.getRegionInfo();
135 }
136
137
138
139
140
141 public int getCaching() {
142 return caching;
143 }
144
145
146
147
148
149 public void setCaching(int caching) {
150 this.caching = caching;
151 }
152 }