19
19
package org .apache .pulsar .broker .admin .v2 ;
20
20
21
21
import static org .apache .pulsar .common .util .Codec .decode ;
22
+ import static org .apache .pulsar .common .util .FutureUtil .unwrapCompletionException ;
22
23
import com .fasterxml .jackson .core .JsonProcessingException ;
23
24
import io .swagger .annotations .Api ;
24
25
import io .swagger .annotations .ApiOperation ;
25
26
import io .swagger .annotations .ApiParam ;
26
27
import io .swagger .annotations .ApiResponse ;
27
28
import io .swagger .annotations .ApiResponses ;
29
+ import java .io .IOException ;
28
30
import java .util .List ;
29
31
import java .util .Map ;
30
32
import java .util .Optional ;
31
33
import java .util .Set ;
34
+ import java .util .concurrent .CompletionException ;
35
+ import javax .servlet .http .HttpServletRequest ;
32
36
import javax .ws .rs .DELETE ;
33
37
import javax .ws .rs .DefaultValue ;
34
38
import javax .ws .rs .Encoded ;
42
46
import javax .ws .rs .WebApplicationException ;
43
47
import javax .ws .rs .container .AsyncResponse ;
44
48
import javax .ws .rs .container .Suspended ;
49
+ import javax .ws .rs .core .Context ;
45
50
import javax .ws .rs .core .MediaType ;
46
51
import javax .ws .rs .core .Response ;
52
+ import javax .ws .rs .core .StreamingOutput ;
47
53
import org .apache .bookkeeper .mledger .Position ;
48
54
import org .apache .bookkeeper .mledger .impl .PositionImpl ;
49
55
import org .apache .pulsar .broker .admin .impl .PersistentTopicsBase ;
83
89
import org .apache .pulsar .common .policies .data .stats .PartitionedTopicStatsImpl ;
84
90
import org .apache .pulsar .common .util .Codec ;
85
91
import org .apache .pulsar .common .util .FutureUtil ;
92
+ import org .apache .pulsar .common .util .ObjectMapperFactory ;
86
93
import org .apache .pulsar .metadata .api .MetadataStoreException ;
87
94
import org .slf4j .Logger ;
88
95
import org .slf4j .LoggerFactory ;
@@ -1120,7 +1127,7 @@ public void deleteTopic(
1120
1127
internalDeleteTopicAsync (authoritative , force )
1121
1128
.thenAccept (__ -> asyncResponse .resume (Response .noContent ().build ()))
1122
1129
.exceptionally (ex -> {
1123
- Throwable t = FutureUtil . unwrapCompletionException (ex );
1130
+ Throwable t = unwrapCompletionException (ex );
1124
1131
if (!force && (t instanceof BrokerServiceException .TopicBusyException )) {
1125
1132
ex = new RestException (Response .Status .PRECONDITION_FAILED ,
1126
1133
t .getMessage ());
@@ -1215,6 +1222,8 @@ public void getStats(
1215
1222
});
1216
1223
}
1217
1224
1225
+ @ Context HttpServletRequest servletRequest ;
1226
+
1218
1227
@ GET
1219
1228
@ Path ("{tenant}/{namespace}/{topic}/internalStats" )
1220
1229
@ ApiOperation (value = "Get the internal stats for the topic." , response = PersistentTopicInternalStats .class )
@@ -1226,8 +1235,7 @@ public void getStats(
1226
1235
@ ApiResponse (code = 412 , message = "Topic name is not valid" ),
1227
1236
@ ApiResponse (code = 500 , message = "Internal server error" ),
1228
1237
@ ApiResponse (code = 503 , message = "Failed to validate global cluster configuration" ) })
1229
- public void getInternalStats (
1230
- @ Suspended final AsyncResponse asyncResponse ,
1238
+ public StreamingOutput getInternalStats (
1231
1239
@ ApiParam (value = "Specify the tenant" , required = true )
1232
1240
@ PathParam ("tenant" ) String tenant ,
1233
1241
@ ApiParam (value = "Specify the namespace" , required = true )
@@ -1238,15 +1246,22 @@ public void getInternalStats(
1238
1246
@ QueryParam ("authoritative" ) @ DefaultValue ("false" ) boolean authoritative ,
1239
1247
@ QueryParam ("metadata" ) @ DefaultValue ("false" ) boolean metadata ) {
1240
1248
validateTopicName (tenant , namespace , encodedTopic );
1241
- internalGetInternalStatsAsync (authoritative , metadata )
1242
- .thenAccept (asyncResponse ::resume )
1243
- .exceptionally (ex -> {
1244
- if (isNot307And404Exception (ex )) {
1245
- log .error ("[{}] Failed to get internal stats for topic {}" , clientAppId (), topicName , ex );
1246
- }
1247
- resumeAsyncResponseExceptionally (asyncResponse , ex );
1248
- return null ;
1249
- });
1249
+ return output -> {
1250
+ internalGetInternalStatsAsync (authoritative , metadata )
1251
+ .thenAccept (stats -> {
1252
+ try {
1253
+ ObjectMapperFactory .getMapper ().getObjectMapper ().writeValue (output , stats );
1254
+ } catch (IOException error ) {
1255
+ throw new CompletionException (error );
1256
+ }
1257
+ })
1258
+ .exceptionally (ex -> {
1259
+ if (isNot307And404Exception (ex )) {
1260
+ log .error ("[{}] Failed to get internal stats for topic {}" , clientAppId (), topicName , ex );
1261
+ }
1262
+ throw translateToWebApplicationException (ex );
1263
+ });
1264
+ };
1250
1265
}
1251
1266
1252
1267
@ GET
0 commit comments