ramackri commented on code in PR #986:
URL: https://github.com/apache/ranger/pull/986#discussion_r3317229768


##########
audit-server/audit-dispatcher/dispatcher-opensearch/src/main/java/org/apache/ranger/audit/dispatcher/OpenSearchDispatcherManager.java:
##########
@@ -0,0 +1,318 @@
+/*
+ * 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.ranger.audit.dispatcher;
+
+import org.apache.ranger.audit.dispatcher.kafka.AuditDispatcher;
+import org.apache.ranger.audit.dispatcher.kafka.AuditDispatcherTracker;
+import org.apache.ranger.audit.provider.MiscUtil;
+import org.apache.ranger.audit.server.AuditServerConstants;
+import org.apache.ranger.audit.utils.AuditServerLogFormatter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Properties;
+
+/**
+ * Manages the lifecycle of the OpenSearch dispatcher.
+ */
+public final class OpenSearchDispatcherManager {
+    /** Class logger. */
+    private static final Logger LOG =
+        LoggerFactory.getLogger(
+            OpenSearchDispatcherManager.class);
+    /** System property for dispatcher type selection. */
+    private static final String CONFIG_DISPATCHER_TYPE =
+        AuditServerConstants.PROP_DISPATCHER_TYPE;
+    /** Dispatcher type identifier. */
+    private static final String TYPE_OPENSEARCH =
+        "opensearch";
+    /** Property controlling OpenSearch destination. */
+    private static final String ES_DEST_PROP =
+        "xasecure.audit.destination.elasticsearch";
+    /** Maximum initialization retry attempts. */
+    private static final int    MAX_INIT_ATTEMPTS = 5;
+    /** Base delay between initialization retries. */
+    private static final long   INIT_RETRY_MS     = 5000L;
+    /** Maximum wait for dispatcher thread shutdown. */
+    private static final long   SHUTDOWN_WAIT_MS  = 10000L;
+
+    /** Tracks active dispatchers for health reporting. */
+    private final AuditDispatcherTracker tracker =
+        AuditDispatcherTracker.getInstance();
+    /** The active OpenSearch dispatcher instance. */
+    private AuditDispatcher dispatcher;
+    /** Thread running the dispatcher's consume loop. */
+    private Thread          dispatcherThread;
+
+    /**
+     * Initializes the OpenSearch dispatcher from properties.
+     *
+     * @param props configuration properties
+     */
+    public void init(final Properties props) {
+        LOG.info("==> OpenSearchDispatcherManager.init()");
+
+        String dispatcherType =
+            System.getProperty(CONFIG_DISPATCHER_TYPE);
+        if (dispatcherType != null
+                && !dispatcherType.equalsIgnoreCase(
+                    TYPE_OPENSEARCH)) {
+            LOG.info("Skipping OpenSearchDispatcherManager"
+                + " initialization since dispatcher"
+                + " type is {}", dispatcherType);
+            return;
+        }
+
+        try {
+            if (props == null) {
+                LOG.error("Configuration properties are null");
+                throw new RuntimeException(
+                    "Failed to load configuration");
+            }
+
+            boolean isEnabled = MiscUtil.getBooleanProperty(
+                props, ES_DEST_PROP, false);
+            if (!isEnabled) {
+                String clsName = MiscUtil.getStringProperty(
+                    props,
+                    AuditServerConstants.PROP_DISPATCHER_CLASS);
+                if (clsName != null && clsName.contains(
+                        "AuditOpenSearchDispatcher")) {
+                    isEnabled = true;
+                    props.setProperty(ES_DEST_PROP, "true");

Review Comment:
   When dispatcher class name contains AuditOpenSearchDispatcher, code sets 
props.setProperty(ES_DEST_PROP, "true"). Side effect on the shared config 
object may surprise other components reading the same Properties. Prefer local 
flag instead of mutating props.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to