Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serverStats to BrokerQueryEventListener #14807

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import org.apache.pinot.spi.exception.BadQueryRequestException;
import org.apache.pinot.spi.exception.DatabaseConflictException;
import org.apache.pinot.spi.trace.RequestContext;
import org.apache.pinot.spi.trace.ServerStatsInfo;
import org.apache.pinot.spi.trace.Tracing;
import org.apache.pinot.spi.utils.CommonConstants;
import org.apache.pinot.spi.utils.CommonConstants.Broker;
Expand Down Expand Up @@ -859,7 +860,7 @@ protected BrokerResponse handleRequest(long requestId, String query, SqlNodeAndO
// Log query and stats
_queryLogger.log(
new QueryLogger.QueryLogParams(requestContext, tableName, brokerResponse, requesterIdentity, serverStats));

requestContext.setServerStatsMap(serverStats.getServerStatsMap());
return brokerResponse;
} finally {
Tracing.ThreadAccountantOps.clear();
Expand Down Expand Up @@ -1898,14 +1899,23 @@ private String getGlobalQueryId(long requestId) {
*/
public static class ServerStats {
private String _serverStats;
private Map<String, ServerStatsInfo> _serverStatsMap;

public String getServerStats() {
return _serverStats;
}

public Map getServerStatsMap() {
return _serverStatsMap;
}

public void setServerStats(String serverStats) {
_serverStats = serverStats;
}

public void setServerStatsMap(Map<String, ServerStatsInfo> serverStatsMap) {
_serverStatsMap = serverStatsMap;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ protected BrokerResponseNative processBrokerRequest(long requestId, BrokerReques
System.nanoTime() - scatterGatherStartTimeNs);
// TODO Use scatterGatherStats as serverStats
serverStats.setServerStats(asyncQueryResponse.getServerStats());
serverStats.setServerStatsMap(asyncQueryResponse.getServerStatsMap());

int numServersQueried = finalResponses.size();
long totalResponseSize = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import org.apache.pinot.common.datatable.DataTable;
import org.apache.pinot.common.exception.QueryException;
import org.apache.pinot.common.utils.HashUtil;
import org.apache.pinot.core.transport.server.routing.stats.ServerRoutingStatsManager;
import org.apache.pinot.spi.trace.ServerStatsInfo;


/**
Expand Down Expand Up @@ -145,6 +147,20 @@ public String getServerStats() {
return stringBuilder.toString();
}

@Override
public Map<String, ServerStatsInfo> getServerStatsMap() {
return _responseMap.entrySet().stream()
.collect(Collectors.toMap(
entry -> entry.getKey().getInstanceId(),
entry -> new ServerStatsInfo(
entry.getValue().getSubmitDelayMs(),
entry.getValue().getRequestSentDelayMs(),
entry.getValue().getResponseDelayMs(),
entry.getValue().getResponseSize(),
entry.getValue().getDeserializationTimeMs())
));
}

@Override
public long getServerResponseDelayMs(ServerRoutingInstance serverRoutingInstance) {
return _responseMap.get(serverRoutingInstance).getResponseDelayMs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.annotation.concurrent.ThreadSafe;
import org.apache.pinot.spi.annotations.InterfaceAudience;
import org.apache.pinot.spi.annotations.InterfaceStability;
import org.apache.pinot.spi.trace.ServerStatsInfo;


/**
Expand Down Expand Up @@ -62,6 +63,8 @@ Map<ServerRoutingInstance, ServerResponse> getFinalResponses()
*/
String getServerStats();

Map<String, ServerStatsInfo> getServerStatsMap();

/**
* Returns the time taken for the server to respond to the query.
* @param serverRoutingInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class DefaultRequestContext implements RequestScope {
private Map<String, String> _traceInfo = new HashMap<>();
private List<String> _processingExceptions = new ArrayList<>();
private Map<String, List<String>> _requestHttpHeaders = new HashMap<>();
private Map<String, ServerStatsInfo> _serverStatsMap = new HashMap<>();

public DefaultRequestContext() {
}
Expand Down Expand Up @@ -573,6 +574,16 @@ public void setRequestHttpHeaders(Map<String, List<String>> requestHttpHeaders)
_requestHttpHeaders.putAll(requestHttpHeaders);
}

@Override
public Map<String, ServerStatsInfo> getServerStatsMap() {
return _serverStatsMap;
}

@Override
public void setServerStatsMap(Map<String, ServerStatsInfo> serverStatsMap) {
_serverStatsMap.putAll(serverStatsMap);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is counter-intuitive. Instead of set you can do addToServerStatsMap?

}

@Override
public void close() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ default boolean isSampledRequest() {
*/
void setRequestHttpHeaders(Map<String, List<String>> requestHttpHeaders);

Map<String, ServerStatsInfo> getServerStatsMap();

void setServerStatsMap(Map<String, ServerStatsInfo> serverStatsMap);

enum FanoutType {
OFFLINE, REALTIME, HYBRID
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pinot.spi.trace;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are putting this in the SPI we have to take care of a few things.

  1. A Public ctor with AllArgs is not going to be tenable, since in the future we may want to add more properties to this. Suggest using NoArgs ctor with setters that return this, or builder pattern. We can also check what convention Pinot uses right now.
  2. We should define precisely each of the fields here. You can add a javadoc for each field member var.
  3. Could we get rid of redundancies like ServerStats in SingleConnectionBrokerRequestHandler? If this is in the SPI would make sense to make this the broadly used class for handling this info.


public class ServerStatsInfo {
private final long _submitRequestTimeMs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format (2 spaces)

private final int _requestSentLatencyMs;
private final long _receiveDataTableTimeMs;
private final int _responseSize;
private final int _deserializationTimeMs;

public ServerStatsInfo(long submitRequestTimeMs, int requestSentLatencyMs,
long receiveDataTableTimeMs, int responseSize, int deserializationTimeMs) {
_submitRequestTimeMs = submitRequestTimeMs;
_requestSentLatencyMs = requestSentLatencyMs;
_receiveDataTableTimeMs = receiveDataTableTimeMs;
_responseSize = responseSize;
_deserializationTimeMs = deserializationTimeMs;
}

public long getSubmitRequestTimeMs() {
Copy link
Contributor

@ankitsultana ankitsultana Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSE also tracks a bunch of server/stage level stats albeit in a different form. I think we should extend this so it can accept Multistage Query stats too. (I meant that we incorporate it somehow.. not necessarily through the same POJO)

cc: @gortiz who worked on adding MultiStageQueryStats.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great, but unless I'm wrong, all these trace options only support SSE. It is unclear to me how this is being used, and AFAIK StarTree doesn't use this API. RequestContext itself seems to be SSE focused given it assumes a single table is involved in the query.

return _submitRequestTimeMs;
}

public int getRequestSentLatencyMs() {
return _requestSentLatencyMs;
}

public long getReceiveDataTableTimeMs() {
return _receiveDataTableTimeMs;
}

public int getResponseSize() {
return _responseSize;
}

public int getDeserializationTimeMs() {
return _deserializationTimeMs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pinot.spi.trace;

import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class ServerStatsInfoTest {
ServerStatsInfo _serverStatsInfo = new ServerStatsInfo(1, 2, 3, 4, 5);

@Test
public void testServerStatsInfoValues() {
assertEquals(1, _serverStatsInfo.getSubmitRequestTimeMs());
assertEquals(2, _serverStatsInfo.getRequestSentLatencyMs());
assertEquals(3, _serverStatsInfo.getReceiveDataTableTimeMs());
assertEquals(4, _serverStatsInfo.getResponseSize());
assertEquals(5, _serverStatsInfo.getDeserializationTimeMs());
}
}
Loading