ankitsultana commented on code in PR #11437:
URL: https://github.com/apache/pinot/pull/11437#discussion_r1322289960


##########
pinot-spi/src/main/java/org/apache/pinot/spi/queryeventlistener/BrokerQueryEventListener.java:
##########
@@ -0,0 +1,24 @@
+/**
+ * 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.queryeventlistener;

Review Comment:
   nit (opinionated): can we rename `queryeventlistener` to 
`eventlistener.query`?



##########
pinot-spi/src/main/java/org/apache/pinot/spi/queryeventlistener/PinotBrokerQueryEventListenerUtils.java:
##########
@@ -0,0 +1,105 @@
+/**
+ * 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.queryeventlistener;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Collections;
+import java.util.Optional;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static 
org.apache.pinot.spi.utils.CommonConstants.CONFIG_OF_BROKER_EVENT_LISTENER_CLASS_NAME;
+import static 
org.apache.pinot.spi.utils.CommonConstants.DEFAULT_BROKER_EVENT_LISTENER_CLASS_NAME;
+
+
+public class PinotBrokerQueryEventListenerUtils {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PinotBrokerQueryEventListenerUtils.class);
+  private static BrokerQueryEventListener _brokerQueryEventListener = null;
+
+  private PinotBrokerQueryEventListenerUtils() {
+  }
+
+  /**
+   * Initialize the BrokerQueryEventListener and registers the eventListener
+   */
+  @VisibleForTesting
+  public synchronized static void init(PinotConfiguration 
eventListenerConfiguration) {
+    // Initializes BrokerQueryEventListener.
+    initializeBrokerQueryEventListener(eventListenerConfiguration);
+  }
+
+  /**
+   * Initializes PinotBrokerQueryEventListener with event-listener 
configurations.
+   * @param eventListenerConfiguration The subset of the configuration 
containing the event-listener-related keys
+   */
+  private static void initializeBrokerQueryEventListener(PinotConfiguration 
eventListenerConfiguration) {
+    String brokerQueryEventListenerClassName =
+        
eventListenerConfiguration.getProperty(CONFIG_OF_BROKER_EVENT_LISTENER_CLASS_NAME,
+            DEFAULT_BROKER_EVENT_LISTENER_CLASS_NAME);
+    LOGGER.info("{} will be initialized as the PinotBrokerQueryEventListener", 
brokerQueryEventListenerClassName);
+
+    Optional<Class<?>> clazzFound;
+    try {
+      clazzFound = 
Optional.of(Class.forName(brokerQueryEventListenerClassName));
+    } catch (ClassNotFoundException e) {
+      throw new RuntimeException("Failed to initialize 
BrokerQueryEventListener. "
+          + "Please check if any pinot-event-listener related jar is actually 
added to the classpath.");
+    }
+
+    clazzFound.ifPresent(clazz -> {
+      try {
+        BrokerQueryEventListener brokerQueryEventListener = 
(BrokerQueryEventListener) clazz.newInstance();
+        registerBrokerEventListener(brokerQueryEventListener);
+      } catch (Exception e) {
+        LOGGER.error("Caught exception while initializing event listener 
registry: {}, skipping it", clazz, e);
+      }
+    });
+
+    Preconditions.checkState(_brokerQueryEventListener != null, "Failed to 
initialize BrokerQueryEventListener. "
+        + "Please check if any pinot-event-listener related jar is actually 
added to the classpath.");
+  }
+
+  /**
+   * Registers an broker event listener.

Review Comment:
   nit: "Registers a broker.."



##########
pinot-spi/src/main/java/org/apache/pinot/spi/queryeventlistener/BrokerQueryEventInfo.java:
##########
@@ -0,0 +1,462 @@
+/**
+ * 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.queryeventlistener;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.apache.pinot.spi.trace.RequestContext;
+
+
+public class BrokerQueryEventInfo {
+  private String _requestId;
+  private String _brokerId;
+  private String _query;
+  private String _queryStatus;
+  private String _failureJson;
+  private int _errorCode;
+
+  private long _requestArrivalTimeMillis;
+  private int _numServersQueried;
+  private int _numServersResponded = 0;

Review Comment:
   nit: all int/long will anyways be initialized to 0. Strings would be null 
though. Should we initialize those to `""`?



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -248,13 +252,15 @@ public BrokerResponse handleRequest(JsonNode request, 
@Nullable SqlNodeAndOption
     if (!hasAccess) {
       
_brokerMetrics.addMeteredGlobalValue(BrokerMeter.REQUEST_DROPPED_DUE_TO_ACCESS_ERROR,
 1);
       requestContext.setErrorCode(QueryException.ACCESS_DENIED_ERROR_CODE);
+      _brokerQueryEventListener.onQueryCompletion(new 
BrokerQueryEventInfo(requestContext));
       throw new WebApplicationException("Permission denied", 
Response.Status.FORBIDDEN);
     }
 
     long requestId = _brokerIdGenerator.get();
     requestContext.setRequestId(requestId);
     JsonNode sql = request.get(Broker.Request.SQL);
     if (sql == null) {
+      _brokerQueryEventListener.onQueryCompletion(new 
BrokerQueryEventInfo(requestContext));

Review Comment:
   We are not setting the error code in `RequestContext` in this path, so this 
might get logged as success?



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -248,13 +252,15 @@ public BrokerResponse handleRequest(JsonNode request, 
@Nullable SqlNodeAndOption
     if (!hasAccess) {
       
_brokerMetrics.addMeteredGlobalValue(BrokerMeter.REQUEST_DROPPED_DUE_TO_ACCESS_ERROR,
 1);
       requestContext.setErrorCode(QueryException.ACCESS_DENIED_ERROR_CODE);
+      _brokerQueryEventListener.onQueryCompletion(new 
BrokerQueryEventInfo(requestContext));

Review Comment:
   Looks like we don't increment `requestId` or set it in `requestContext` if 
there's no access.
   
   Can we move the requestId increment and `requestContext.setRequestId` call 
before this check?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to