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 ;
@@ -96,9 +99,12 @@ private boolean isMeta(TableName tableName) {
96
99
return TableName .isMetaTableName (tableName );
97
100
}
98
101
99
- private <T > CompletableFuture <T > tracedLocationFuture (Supplier <CompletableFuture <T >> action ,
100
- Function <T , List <String >> getRegionNames , TableName tableName , String methodName ) {
101
- Span span = createTableSpan ("AsyncRegionLocator." + methodName , tableName );
102
+ private <T > CompletableFuture <T > tracedLocationFuture (
103
+ Supplier <CompletableFuture <T >> action ,
104
+ Function <T , List <String >> getRegionNames ,
105
+ Supplier <Span > spanSupplier
106
+ ) {
107
+ final Span span = spanSupplier .get ();
102
108
try (Scope scope = span .makeCurrent ()) {
103
109
CompletableFuture <T > future = action .get ();
104
110
FutureUtils .addListener (future , (resp , error ) -> {
@@ -117,18 +123,29 @@ private <T> CompletableFuture<T> tracedLocationFuture(Supplier<CompletableFuture
117
123
}
118
124
}
119
125
120
- private List <String > getRegionName (RegionLocations locs ) {
121
- List <String > names = new ArrayList <>();
122
- for (HRegionLocation loc : locs .getRegionLocations ()) {
123
- if (loc != null ) {
124
- names .add (loc .getRegion ().getRegionNameAsString ());
125
- }
126
- }
127
- return names ;
126
+ private static List <String > getRegionNames (RegionLocations locs ) {
127
+ if (locs == null ) { return Collections .emptyList (); }
128
+ if (locs .getRegionLocations () == null ) { return Collections .emptyList (); }
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
+ private 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 );
128
142
}
129
143
130
144
CompletableFuture <RegionLocations > getRegionLocations (TableName tableName , byte [] row ,
131
145
RegionLocateType type , boolean reload , long timeoutNs ) {
146
+ final Supplier <Span > supplier = new TableSpanBuilder <>(conn )
147
+ .setName ("AsyncRegionLocator.getRegionLocations" )
148
+ .setTableName (tableName );
132
149
return tracedLocationFuture (() -> {
133
150
CompletableFuture <RegionLocations > future = isMeta (tableName ) ?
134
151
metaRegionLocator .getRegionLocations (RegionReplicaUtil .DEFAULT_REPLICA_ID , reload ) :
@@ -138,11 +155,14 @@ CompletableFuture<RegionLocations> getRegionLocations(TableName tableName, byte[
138
155
() -> "Timeout(" + TimeUnit .NANOSECONDS .toMillis (timeoutNs ) +
139
156
"ms) waiting for region locations for " + tableName + ", row='" +
140
157
Bytes .toStringBinary (row ) + "'" );
141
- }, this :: getRegionName , tableName , "getRegionLocations" );
158
+ }, AsyncRegionLocator :: getRegionNames , supplier );
142
159
}
143
160
144
161
CompletableFuture <HRegionLocation > getRegionLocation (TableName tableName , byte [] row ,
145
162
int replicaId , RegionLocateType type , boolean reload , long timeoutNs ) {
163
+ final Supplier <Span > supplier = new TableSpanBuilder <>(conn )
164
+ .setName ("AsyncRegionLocator.getRegionLocation" )
165
+ .setTableName (tableName );
146
166
return tracedLocationFuture (() -> {
147
167
// meta region can not be split right now so we always call the same method.
148
168
// Change it later if the meta table can have more than one regions.
@@ -173,8 +193,7 @@ CompletableFuture<HRegionLocation> getRegionLocation(TableName tableName, byte[]
173
193
() -> "Timeout(" + TimeUnit .NANOSECONDS .toMillis (timeoutNs ) +
174
194
"ms) waiting for region location for " + tableName + ", row='" +
175
195
Bytes .toStringBinary (row ) + "', replicaId=" + replicaId );
176
- }, loc -> Arrays .asList (loc .getRegion ().getRegionNameAsString ()), tableName ,
177
- "getRegionLocation" );
196
+ }, AsyncRegionLocator ::getRegionNames , supplier );
178
197
}
179
198
180
199
CompletableFuture <HRegionLocation > getRegionLocation (TableName tableName , byte [] row ,
@@ -202,31 +221,38 @@ void updateCachedLocationOnError(HRegionLocation loc, Throwable exception) {
202
221
}
203
222
204
223
void clearCache (TableName tableName ) {
224
+ Supplier <Span > supplier = new TableSpanBuilder <>(conn )
225
+ .setName ("AsyncRegionLocator.clearCache" )
226
+ .setTableName (tableName );
205
227
TraceUtil .trace (() -> {
206
228
LOG .debug ("Clear meta cache for {}" , tableName );
207
229
if (tableName .equals (META_TABLE_NAME )) {
208
230
metaRegionLocator .clearCache ();
209
231
} else {
210
232
nonMetaRegionLocator .clearCache (tableName );
211
233
}
212
- }, () -> createTableSpan ( "AsyncRegionLocator.clearCache" , tableName ) );
234
+ }, supplier );
213
235
}
214
236
215
237
void clearCache (ServerName serverName ) {
238
+ Supplier <Span > supplier = new ConnectionSpanBuilder <>(conn )
239
+ .setName ("AsyncRegionLocator.clearCache" )
240
+ .addAttribute (SERVER_NAME_KEY , serverName .getServerName ());
216
241
TraceUtil .trace (() -> {
217
242
LOG .debug ("Clear meta cache for {}" , serverName );
218
243
metaRegionLocator .clearCache (serverName );
219
244
nonMetaRegionLocator .clearCache (serverName );
220
245
conn .getConnectionMetrics ().ifPresent (MetricsConnection ::incrMetaCacheNumClearServer );
221
- }, () -> createSpan ("AsyncRegionLocator.clearCache" ).setAttribute (SERVER_NAME_KEY ,
222
- serverName .getServerName ()));
246
+ }, supplier );
223
247
}
224
248
225
249
void clearCache () {
250
+ Supplier <Span > supplier = new ConnectionSpanBuilder <>(conn )
251
+ .setName ("AsyncRegionLocator.clearCache" );
226
252
TraceUtil .trace (() -> {
227
253
metaRegionLocator .clearCache ();
228
254
nonMetaRegionLocator .clearCache ();
229
- }, "AsyncRegionLocator.clearCache" );
255
+ }, supplier );
230
256
}
231
257
232
258
AsyncNonMetaRegionLocator getNonMetaRegionLocator () {
0 commit comments