1
- /**
1
+ /*
2
2
* Licensed to the Apache Software Foundation (ASF) under one
3
3
* or more contributor license agreements. See the NOTICE file
4
4
* distributed with this work for additional information
20
20
import static org .apache .hadoop .hbase .TableName .META_TABLE_NAME ;
21
21
import static org .apache .hadoop .hbase .trace .HBaseSemanticAttributes .REGION_NAMES_KEY ;
22
22
import static org .apache .hadoop .hbase .trace .HBaseSemanticAttributes .SERVER_NAME_KEY ;
23
- import static org .apache .hadoop .hbase .trace .TraceUtil .createSpan ;
24
- import static org .apache .hadoop .hbase .trace .TraceUtil .createTableSpan ;
25
23
import static org .apache .hadoop .hbase .util .FutureUtils .addListener ;
26
24
27
25
import io .opentelemetry .api .trace .Span ;
28
26
import io .opentelemetry .api .trace .StatusCode ;
29
27
import io .opentelemetry .context .Scope ;
30
- import java .util .ArrayList ;
31
28
import java .util .Arrays ;
29
+ import java .util .Collections ;
32
30
import java .util .List ;
31
+ import java .util .Objects ;
32
+ import java .util .Optional ;
33
33
import java .util .concurrent .CompletableFuture ;
34
34
import java .util .concurrent .TimeUnit ;
35
35
import java .util .function .Function ;
36
36
import java .util .function .Supplier ;
37
+ import java .util .stream .Collectors ;
37
38
import org .apache .hadoop .hbase .HRegionLocation ;
38
39
import org .apache .hadoop .hbase .RegionLocations ;
39
40
import org .apache .hadoop .hbase .ServerName ;
40
41
import org .apache .hadoop .hbase .TableName ;
42
+ import org .apache .hadoop .hbase .client .trace .ConnectionSpanBuilder ;
43
+ import org .apache .hadoop .hbase .client .trace .TableSpanBuilder ;
41
44
import org .apache .hadoop .hbase .exceptions .TimeoutIOException ;
42
45
import org .apache .hadoop .hbase .trace .TraceUtil ;
43
46
import org .apache .hadoop .hbase .util .Bytes ;
@@ -95,9 +98,12 @@ private boolean isMeta(TableName tableName) {
95
98
return TableName .isMetaTableName (tableName );
96
99
}
97
100
98
- private <T > CompletableFuture <T > tracedLocationFuture (Supplier <CompletableFuture <T >> action ,
99
- Function <T , List <String >> getRegionNames , TableName tableName , String methodName ) {
100
- Span span = createTableSpan ("AsyncRegionLocator." + methodName , tableName );
101
+ private <T > CompletableFuture <T > tracedLocationFuture (
102
+ Supplier <CompletableFuture <T >> action ,
103
+ Function <T , List <String >> getRegionNames ,
104
+ Supplier <Span > spanSupplier
105
+ ) {
106
+ final Span span = spanSupplier .get ();
101
107
try (Scope scope = span .makeCurrent ()) {
102
108
CompletableFuture <T > future = action .get ();
103
109
FutureUtils .addListener (future , (resp , error ) -> {
@@ -116,18 +122,30 @@ private <T> CompletableFuture<T> tracedLocationFuture(Supplier<CompletableFuture
116
122
}
117
123
}
118
124
119
- private List <String > getRegionName (RegionLocations locs ) {
120
- List <String > names = new ArrayList <>();
121
- for (HRegionLocation loc : locs .getRegionLocations ()) {
122
- if (loc != null ) {
123
- names .add (loc .getRegion ().getRegionNameAsString ());
124
- }
125
+ static List <String > getRegionNames (RegionLocations locs ) {
126
+ if (locs == null || locs .getRegionLocations () == null ) {
127
+ return Collections .emptyList ();
125
128
}
126
- return names ;
129
+ return Arrays .stream (locs .getRegionLocations ())
130
+ .filter (Objects ::nonNull )
131
+ .map (HRegionLocation ::getRegion )
132
+ .map (RegionInfo ::getRegionNameAsString )
133
+ .collect (Collectors .toList ());
134
+ }
135
+
136
+ static List <String > getRegionNames (HRegionLocation location ) {
137
+ return Optional .ofNullable (location )
138
+ .map (HRegionLocation ::getRegion )
139
+ .map (RegionInfo ::getRegionNameAsString )
140
+ .map (Collections ::singletonList )
141
+ .orElseGet (Collections ::emptyList );
127
142
}
128
143
129
144
CompletableFuture <RegionLocations > getRegionLocations (TableName tableName , byte [] row ,
130
145
RegionLocateType type , boolean reload , long timeoutNs ) {
146
+ final Supplier <Span > supplier = new TableSpanBuilder (conn )
147
+ .setName ("AsyncRegionLocator.getRegionLocations" )
148
+ .setTableName (tableName );
131
149
return tracedLocationFuture (() -> {
132
150
CompletableFuture <RegionLocations > future = isMeta (tableName ) ?
133
151
metaRegionLocator .getRegionLocations (RegionReplicaUtil .DEFAULT_REPLICA_ID , reload ) :
@@ -137,11 +155,14 @@ CompletableFuture<RegionLocations> getRegionLocations(TableName tableName, byte[
137
155
() -> "Timeout(" + TimeUnit .NANOSECONDS .toMillis (timeoutNs ) +
138
156
"ms) waiting for region locations for " + tableName + ", row='" +
139
157
Bytes .toStringBinary (row ) + "'" );
140
- }, this :: getRegionName , tableName , "getRegionLocations" );
158
+ }, AsyncRegionLocator :: getRegionNames , supplier );
141
159
}
142
160
143
161
CompletableFuture <HRegionLocation > getRegionLocation (TableName tableName , byte [] row ,
144
162
int replicaId , RegionLocateType type , boolean reload , long timeoutNs ) {
163
+ final Supplier <Span > supplier = new TableSpanBuilder (conn )
164
+ .setName ("AsyncRegionLocator.getRegionLocation" )
165
+ .setTableName (tableName );
145
166
return tracedLocationFuture (() -> {
146
167
// meta region can not be split right now so we always call the same method.
147
168
// Change it later if the meta table can have more than one regions.
@@ -172,8 +193,7 @@ CompletableFuture<HRegionLocation> getRegionLocation(TableName tableName, byte[]
172
193
() -> "Timeout(" + TimeUnit .NANOSECONDS .toMillis (timeoutNs ) +
173
194
"ms) waiting for region location for " + tableName + ", row='" +
174
195
Bytes .toStringBinary (row ) + "', replicaId=" + replicaId );
175
- }, loc -> Arrays .asList (loc .getRegion ().getRegionNameAsString ()), tableName ,
176
- "getRegionLocation" );
196
+ }, AsyncRegionLocator ::getRegionNames , supplier );
177
197
}
178
198
179
199
CompletableFuture <HRegionLocation > getRegionLocation (TableName tableName , byte [] row ,
@@ -201,31 +221,38 @@ void updateCachedLocationOnError(HRegionLocation loc, Throwable exception) {
201
221
}
202
222
203
223
void clearCache (TableName tableName ) {
224
+ Supplier <Span > supplier = new TableSpanBuilder (conn )
225
+ .setName ("AsyncRegionLocator.clearCache" )
226
+ .setTableName (tableName );
204
227
TraceUtil .trace (() -> {
205
228
LOG .debug ("Clear meta cache for {}" , tableName );
206
229
if (tableName .equals (META_TABLE_NAME )) {
207
230
metaRegionLocator .clearCache ();
208
231
} else {
209
232
nonMetaRegionLocator .clearCache (tableName );
210
233
}
211
- }, () -> createTableSpan ( "AsyncRegionLocator.clearCache" , tableName ) );
234
+ }, supplier );
212
235
}
213
236
214
237
void clearCache (ServerName serverName ) {
238
+ Supplier <Span > supplier = new ConnectionSpanBuilder (conn )
239
+ .setName ("AsyncRegionLocator.clearCache" )
240
+ .addAttribute (SERVER_NAME_KEY , serverName .getServerName ());
215
241
TraceUtil .trace (() -> {
216
242
LOG .debug ("Clear meta cache for {}" , serverName );
217
243
metaRegionLocator .clearCache (serverName );
218
244
nonMetaRegionLocator .clearCache (serverName );
219
245
conn .getConnectionMetrics ().ifPresent (MetricsConnection ::incrMetaCacheNumClearServer );
220
- }, () -> createSpan ("AsyncRegionLocator.clearCache" ).setAttribute (SERVER_NAME_KEY ,
221
- serverName .getServerName ()));
246
+ }, supplier );
222
247
}
223
248
224
249
void clearCache () {
250
+ Supplier <Span > supplier = new ConnectionSpanBuilder (conn )
251
+ .setName ("AsyncRegionLocator.clearCache" );
225
252
TraceUtil .trace (() -> {
226
253
metaRegionLocator .clearCache ();
227
254
nonMetaRegionLocator .clearCache ();
228
- }, "AsyncRegionLocator.clearCache" );
255
+ }, supplier );
229
256
}
230
257
231
258
AsyncNonMetaRegionLocator getNonMetaRegionLocator () {
0 commit comments