2222import org .apache .fluss .cluster .ServerType ;
2323import org .apache .fluss .config .Configuration ;
2424import org .apache .fluss .exception .DisconnectException ;
25+ import org .apache .fluss .metrics .Gauge ;
26+ import org .apache .fluss .metrics .Metric ;
27+ import org .apache .fluss .metrics .MetricType ;
28+ import org .apache .fluss .metrics .groups .AbstractMetricGroup ;
2529import org .apache .fluss .metrics .groups .MetricGroup ;
30+ import org .apache .fluss .metrics .registry .NOPMetricRegistry ;
2631import org .apache .fluss .metrics .util .NOPMetricsGroup ;
2732import org .apache .fluss .rpc .TestingGatewayService ;
33+ import org .apache .fluss .rpc .TestingTabletGatewayService ;
2834import org .apache .fluss .rpc .messages .GetTableSchemaRequest ;
35+ import org .apache .fluss .rpc .messages .LookupRequest ;
36+ import org .apache .fluss .rpc .messages .PbLookupReqForBucket ;
2937import org .apache .fluss .rpc .messages .PbTablePath ;
38+ import org .apache .fluss .rpc .metrics .ClientMetricGroup ;
3039import org .apache .fluss .rpc .metrics .TestingClientMetricGroup ;
3140import org .apache .fluss .rpc .netty .client .ServerConnection .ConnectionState ;
3241import org .apache .fluss .rpc .netty .server .NettyServer ;
4251import org .junit .jupiter .api .BeforeEach ;
4352import org .junit .jupiter .api .Test ;
4453
45- import java .util .Collections ;
54+ import java .util .Arrays ;
55+ import java .util .HashMap ;
56+ import java .util .Map ;
4657import java .util .concurrent .CompletableFuture ;
47-
58+ import java .util .concurrent .ExecutionException ;
59+
60+ import static org .apache .fluss .metrics .MetricNames .CLIENT_BYTES_IN_RATE_AVG ;
61+ import static org .apache .fluss .metrics .MetricNames .CLIENT_BYTES_IN_RATE_SUM ;
62+ import static org .apache .fluss .metrics .MetricNames .CLIENT_BYTES_OUT_RATE_AVG ;
63+ import static org .apache .fluss .metrics .MetricNames .CLIENT_BYTES_OUT_RATE_SUM ;
64+ import static org .apache .fluss .metrics .MetricNames .CLIENT_REQUESTS_IN_FLIGHT_AVG ;
65+ import static org .apache .fluss .metrics .MetricNames .CLIENT_REQUESTS_IN_FLIGHT_SUM ;
66+ import static org .apache .fluss .metrics .MetricNames .CLIENT_REQUESTS_RATE_AVG ;
67+ import static org .apache .fluss .metrics .MetricNames .CLIENT_REQUESTS_RATE_SUM ;
68+ import static org .apache .fluss .metrics .MetricNames .CLIENT_REQUEST_LATENCY_MS_AVG ;
69+ import static org .apache .fluss .metrics .MetricNames .CLIENT_REQUEST_LATENCY_MS_MAX ;
70+ import static org .apache .fluss .metrics .MetricNames .CLIENT_RESPONSES_RATE_AVG ;
71+ import static org .apache .fluss .metrics .MetricNames .CLIENT_RESPONSES_RATE_SUM ;
4872import static org .apache .fluss .rpc .netty .NettyUtils .getClientSocketChannelClass ;
4973import static org .apache .fluss .rpc .netty .NettyUtils .newEventLoopGroup ;
5074import static org .apache .fluss .utils .NetUtils .getAvailablePort ;
@@ -60,6 +84,7 @@ public class ServerConnectionTest {
6084 private Configuration conf ;
6185 private NettyServer nettyServer ;
6286 private ServerNode serverNode ;
87+ private ServerNode serverNode2 ;
6388 private TestingGatewayService service ;
6489
6590 @ BeforeEach
@@ -121,22 +146,100 @@ void testConnectionClose() {
121146 assertThat (future .isDone ()).isTrue ();
122147 }
123148
149+ @ Test
150+ void testConnectionMetrics () throws ExecutionException , InterruptedException {
151+ MockMetricRegistry metricRegistry = new MockMetricRegistry ();
152+ ClientMetricGroup client = new ClientMetricGroup (metricRegistry , "client" );
153+ ServerConnection connection =
154+ new ServerConnection (
155+ bootstrap ,
156+ serverNode ,
157+ client ,
158+ clientAuthenticator ,
159+ (con , ignore ) -> {},
160+ false );
161+ ServerConnection connection2 =
162+ new ServerConnection (
163+ bootstrap ,
164+ serverNode2 ,
165+ client ,
166+ clientAuthenticator ,
167+ (con , ignore ) -> {},
168+ false );
169+ LookupRequest request = new LookupRequest ().setTableId (1 );
170+ PbLookupReqForBucket pbLookupReqForBucket = request .addBucketsReq ();
171+ pbLookupReqForBucket .setBucketId (1 );
172+ assertThat (metricRegistry .registeredMetrics ).hasSize (12 );
173+
174+ connection .send (ApiKeys .LOOKUP , request ).get ();
175+ connection2 .send (ApiKeys .LOOKUP , request ).get ();
176+
177+ assertThat (metricRegistry .registeredMetrics ).hasSize (12 );
178+ assertThat (metricRegistry .registeredMetrics .keySet ())
179+ .containsExactlyInAnyOrder (
180+ CLIENT_REQUESTS_RATE_AVG ,
181+ CLIENT_REQUESTS_RATE_SUM ,
182+ CLIENT_RESPONSES_RATE_AVG ,
183+ CLIENT_RESPONSES_RATE_SUM ,
184+ CLIENT_BYTES_IN_RATE_AVG ,
185+ CLIENT_BYTES_IN_RATE_SUM ,
186+ CLIENT_BYTES_OUT_RATE_AVG ,
187+ CLIENT_BYTES_OUT_RATE_SUM ,
188+ CLIENT_REQUEST_LATENCY_MS_AVG ,
189+ CLIENT_REQUEST_LATENCY_MS_MAX ,
190+ CLIENT_REQUESTS_IN_FLIGHT_AVG ,
191+ CLIENT_REQUESTS_IN_FLIGHT_SUM );
192+ Metric metric = metricRegistry .registeredMetrics .get (CLIENT_REQUESTS_RATE_AVG );
193+ assertThat (metric .getMetricType ()).isEqualTo (MetricType .GAUGE );
194+ assertThat (((Gauge <?>) metric ).getValue ()).isEqualTo (1.0 );
195+ metric = metricRegistry .registeredMetrics .get (CLIENT_REQUESTS_RATE_SUM );
196+ assertThat (metric .getMetricType ()).isEqualTo (MetricType .GAUGE );
197+ assertThat (((Gauge <?>) metric ).getValue ()).isEqualTo (2L );
198+ connection .close ().get ();
199+ }
200+
124201 private void buildNettyServer (int serverId ) throws Exception {
125- try (NetUtils .Port availablePort = getAvailablePort ()) {
202+ try (NetUtils .Port availablePort = getAvailablePort ();
203+ NetUtils .Port availablePort2 = getAvailablePort ()) {
126204 serverNode =
127205 new ServerNode (
128- serverId , "localhost" , availablePort .getPort (), ServerType .COORDINATOR );
129- service = new TestingGatewayService ();
206+ serverId ,
207+ "localhost" ,
208+ availablePort .getPort (),
209+ ServerType .TABLET_SERVER );
210+ serverNode2 =
211+ new ServerNode (
212+ serverId ,
213+ "localhost" ,
214+ availablePort2 .getPort (),
215+ ServerType .TABLET_SERVER );
216+ service = new TestingTabletGatewayService ();
130217 MetricGroup metricGroup = NOPMetricsGroup .newInstance ();
131218 nettyServer =
132219 new NettyServer (
133220 conf ,
134- Collections .singleton (
135- new Endpoint (serverNode .host (), serverNode .port (), "INTERNAL" )),
221+ Arrays .asList (
222+ new Endpoint (serverNode .host (), serverNode .port (), "INTERNAL" ),
223+ new Endpoint (serverNode2 .host (), serverNode2 .port (), "CLIENT" )),
136224 service ,
137225 metricGroup ,
138226 RequestsMetrics .createCoordinatorServerRequestMetrics (metricGroup ));
139227 nettyServer .start ();
140228 }
141229 }
230+
231+ private static class MockMetricRegistry extends NOPMetricRegistry {
232+
233+ Map <String , Metric > registeredMetrics = new HashMap <>();
234+
235+ @ Override
236+ public void register (Metric metric , String metricName , AbstractMetricGroup group ) {
237+ registeredMetrics .put (metricName , metric );
238+ }
239+
240+ @ Override
241+ public void unregister (Metric metric , String metricName , AbstractMetricGroup group ) {
242+ registeredMetrics .remove (metricName , metric );
243+ }
244+ }
142245}
0 commit comments