When NTFD is started, the NtfAdmin class object is defined and log client is
initialized in this object. The initialiation of client may be loop forever
if TRY_AGAIN error always return. Somehow log service has not been started on
time or log service is busy, NTFD is kept in the loop. And the AMF hasn't
received csi callback.

This patch update the timeout when initializing log client.
---
 src/ntf/ntfd/NtfLogger.cc | 185 ++++++++++++++++++++++------------------------
 src/ntf/ntfd/NtfLogger.h  |  54 +++++++-------
 2 files changed, 116 insertions(+), 123 deletions(-)

diff --git a/src/ntf/ntfd/NtfLogger.cc b/src/ntf/ntfd/NtfLogger.cc
index fd17c58a2..a503ee0c8 100644
--- a/src/ntf/ntfd/NtfLogger.cc
+++ b/src/ntf/ntfd/NtfLogger.cc
@@ -20,17 +20,19 @@
  *   INCLUDE FILES
  * ========================================================================
  */
-#include <sys/poll.h>
+#include "ntf/ntfd/NtfLogger.h"
 
-#include "base/osaf_utility.h"
+#include <sys/poll.h>
 #include <saAis.h>
 #include <saLog.h>
-#include "ntf/ntfd/NtfAdmin.h"
-#include "ntf/ntfd/NtfLogger.h"
 #include "ntfs_com.h"
-#include "base/logtrace.h"
 #include "ntf/common/ntfsv_mem.h"
+#include "ntf/ntfd/NtfAdmin.h"
+#include "base/logtrace.h"
 #include "base/osaf_extended_name.h"
+#include "base/osaf_utility.h"
+#include "base/osaf_time.h"
+#include "base/saf_error.h"
 
 /* ========================================================================
  *   DEFINITIONS
@@ -38,7 +40,6 @@
  */
 #define LOG_OPEN_TIMEOUT SA_TIME_ONE_SECOND
 #define LOG_NOTIFICATION_TIMEOUT SA_TIME_ONE_SECOND
-#define AIS_TIMEOUT 500000
 
 /* ========================================================================
  *   TYPE DEFINITIONS
@@ -65,17 +66,12 @@ void saLogWriteLogCallback(SaInvocationT invocation, 
SaAisErrorT error);
  */
 
 static SaLogCallbacksT logCallbacks = {NULL, NULL, saLogWriteLogCallback};
-
 static SaLogHandleT logHandle;
-static SaLogStreamHandleT alarmStreamHandle;
+static SaLogStreamHandleT alarmStreamHandle = 0;
 const SaVersionT kLogVersion = {'A', 2, 3};
+const uint64_t kWait8Seconds = 8 * 1000;
 
-NtfLogger::NtfLogger() : readCounter(0) {
-  if (SA_AIS_OK != initLog()) {
-    LOG_ER("initialize saflog failed exiting...");
-    exit(EXIT_FAILURE);
-  }
-}
+NtfLogger::NtfLogger() : readCounter(0) { }
 
 /* Callbacks */
 void saLogFilterSetCallback(SaLogStreamHandleT logStreamHandle,
@@ -95,7 +91,7 @@ void saLogWriteLogCallback(SaInvocationT invocation, 
SaAisErrorT error) {
   if (SA_AIS_OK != error) {
     NtfSmartPtr notification;
 
-    TRACE_1("Error when logging (%d), queue for relogging", error);
+    TRACE_1("Error when logging (%s), queue for relogging", saf_error(error));
 
     notification = NtfAdmin::theNtfAdmin->getNotificationById(
         (SaNtfIdentifierT)invocation);
@@ -151,12 +147,19 @@ void NtfLogger::queueNotifcation(NtfSmartPtr& notif) {
 
 void NtfLogger::checkQueueAndLog(NtfSmartPtr& newNotif) {
   TRACE_ENTER();
+
+  // Initialize log and open alarm stream if they haven't
+  if (initLogAndOpenAlarmStream() == false) {
+    queueNotifcation(newNotif);
+    return;
+  }
+
   /* Check if there are not logged notifications in queue */
   while (!queuedNotificationList.empty()) {
     NtfSmartPtr notification = queuedNotificationList.front();
     queuedNotificationList.pop_front();
     TRACE_2("Log queued notification: %llu", 
notification->getNotificationId());
-    if (SA_AIS_OK != this->logNotification(notification)) {
+    if (logNotification(notification) == false) {
       TRACE_2("Push back queued notification: %llu",
               notification->getNotificationId());
       queuedNotificationList.push_front(notification); /* keep order */
@@ -166,15 +169,15 @@ void NtfLogger::checkQueueAndLog(NtfSmartPtr& newNotif) {
     }
   }
 
-  if (SA_AIS_OK != this->logNotification(newNotif)) {
+  if (logNotification(newNotif) == false) {
     queueNotifcation(newNotif);
   }
   TRACE_LEAVE();
 }
 
-SaAisErrorT NtfLogger::logNotification(NtfSmartPtr& notif) {
+bool NtfLogger::logNotification(NtfSmartPtr& notif) {
   /* Write to the log if we're the local node */
-  SaAisErrorT errorCode = SA_AIS_OK;
+  SaAisErrorT rc = SA_AIS_OK;
   SaLogHeaderT logHeader;
   char addTextBuf[MAX_ADDITIONAL_TEXT_LENGTH] = {0};
   SaLogBufferT logBuffer;
@@ -219,127 +222,113 @@ SaAisErrorT NtfLogger::logNotification(NtfSmartPtr& 
notif) {
   if ((notif->sendNotInfo_->notificationType == SA_NTF_TYPE_ALARM) ||
       (notif->sendNotInfo_->notificationType == SA_NTF_TYPE_SECURITY_ALARM)) {
     TRACE_2("Logging notification to alarm stream");
-    errorCode =
+    rc =
         saLogWriteLogAsync(alarmStreamHandle, notif->getNotificationId(),
                            SA_LOG_RECORD_WRITE_ACK, &logRecord);
-    switch (errorCode) {
+    switch (rc) {
       case SA_AIS_OK:
         break;
 
       /* LOGsv is busy. Put the notification to queue and re-send next time */
       case SA_AIS_ERR_TRY_AGAIN:
       case SA_AIS_ERR_TIMEOUT:
-        TRACE("Failed to log notification (ret: %d). Try next time.",
-              errorCode);
+        TRACE("Failed to log notification (ret: %s). Try next time.",
+              saf_error(rc));
+        break;
+      case SA_AIS_ERR_BAD_HANDLE:
+        TRACE("BAD HANDLE. Log client should be re-initialized");
+        alarmStreamHandle = 0;
+        if (initLogAndOpenAlarmStream() == true) {
+          rc = saLogWriteLogAsync(alarmStreamHandle,
+                                  notif->getNotificationId(),
+                                  SA_LOG_RECORD_WRITE_ACK, &logRecord);
+        }
         break;
-
       default:
-        osaf_abort(errorCode);
+        LOG_ER("Writing failed: rc = %s", saf_error(rc));
+        osaf_abort(rc);
     }
   }
 
   TRACE_LEAVE();
-  return errorCode;
+  return (rc == SA_AIS_OK);
 }
 
-SaAisErrorT NtfLogger::initLog() {
-  SaAisErrorT result;
+bool NtfLogger::initLogAndOpenAlarmStream() {
+  SaAisErrorT rc = SA_AIS_OK;
   SaNameT alarmStreamName;
-  osaf_extended_name_lend(SA_LOG_STREAM_ALARM, &alarmStreamName);
-  int first_try = 1;
   SaVersionT log_version = kLogVersion;
+  struct timespec timeout_time;
 
   TRACE_ENTER();
 
-  /* Initialize the Log service */
-  do {
-    result = saLogInitialize(&logHandle, &logCallbacks, &log_version);
-    if (SA_AIS_ERR_TRY_AGAIN == result) {
-      if (first_try) {
-        LOG_WA("saLogInitialize returns try again, retries...");
-        first_try = 0;
-      }
-      usleep(AIS_TIMEOUT);
-      log_version = kLogVersion;
-    }
-  } while (SA_AIS_ERR_TRY_AGAIN == result);
+  if (alarmStreamHandle != 0) return true;
+  osaf_extended_name_lend(SA_LOG_STREAM_ALARM, &alarmStreamName);
 
-  if (SA_AIS_OK != result) {
-    LOG_ER("Log initialize result is %d", result);
-    goto exit_point;
-  }
-  if (!first_try) {
-    LOG_IN("saLogInitialize ok");
-    first_try = 1;
+  // Initialize the Log service
+  osaf_set_millis_timeout(kWait8Seconds, &timeout_time);
+  while (!osaf_is_timeout(&timeout_time)) {
+    rc = saLogInitialize(&logHandle, &logCallbacks, &log_version);
+    if (rc != SA_AIS_ERR_TRY_AGAIN) break;
+    log_version = kLogVersion;
+    osaf_nanosleep(&kHundredMilliseconds);
   }
 
-  /* Get file descriptor to use in select */
-  do {
-    result = saLogSelectionObjectGet(logHandle, &ntfs_cb->logSelectionObject);
-    if (SA_AIS_ERR_TRY_AGAIN == result) {
-      if (first_try) {
-        LOG_WA("saLogSelectionObjectGet returns try again, retries...");
-        first_try = 0;
-      }
-      usleep(AIS_TIMEOUT);
-    }
-  } while (SA_AIS_ERR_TRY_AGAIN == result);
+  if (rc != SA_AIS_OK) {
+    LOG_WA("Initialization of log client failed rc = %s", saf_error(rc));
+    return false;
+  }
 
-  if (SA_AIS_OK != result) {
-    LOG_ER("Log SelectionObjectGet result is %d", result);
-    goto exit_point;
+  // Get file descriptor to use in select
+  osaf_set_millis_timeout(kWait8Seconds, &timeout_time);
+  while (!osaf_is_timeout(&timeout_time)) {
+    rc = saLogSelectionObjectGet(logHandle, &ntfs_cb->logSelectionObject);
+    if (rc != SA_AIS_ERR_TRY_AGAIN) break;
+    osaf_nanosleep(&kHundredMilliseconds);
   }
 
-  if (SA_AIS_OK != result) {
-    LOG_ER("Failed to open the notification log stream (%d)", result);
+  if (rc != SA_AIS_OK) {
+    LOG_WA("Log SelectionObjectGet failed rc = %s", saf_error(rc));
     goto exit_point;
   }
-  if (!first_try) {
-    LOG_IN("saLogSelectionObjectGet ok");
-    first_try = 1;
-  }
-
-  /* Open the alarm stream */
-  do {
-    result = saLogStreamOpen_2(logHandle, &alarmStreamName, NULL, 0,
-                               LOG_OPEN_TIMEOUT, &alarmStreamHandle);
-    if (SA_AIS_ERR_TRY_AGAIN == result) {
-      if (first_try) {
-        LOG_WA("saLogStreamOpen_2 returns try again, retries...");
-        first_try = 0;
-      }
-      usleep(AIS_TIMEOUT);
-    }
-  } while (SA_AIS_ERR_TRY_AGAIN == result);
 
-  if (SA_AIS_OK != result) {
-    LOG_ER("Failed to open the alarm log stream (%d)", result);
-    goto exit_point;
+  // Open the alarm stream
+  osaf_set_millis_timeout(kWait8Seconds, &timeout_time);
+  while (!osaf_is_timeout(&timeout_time)) {
+    rc = saLogStreamOpen_2(logHandle, &alarmStreamName, NULL, 0,
+                           LOG_OPEN_TIMEOUT, &alarmStreamHandle);
+    if (rc != SA_AIS_ERR_TRY_AGAIN) break;
+    osaf_nanosleep(&kHundredMilliseconds);
   }
-  if (!first_try) {
-    LOG_IN("saLogStreamOpen_2 ok");
-    first_try = 1;
+
+  if (rc != SA_AIS_OK) {
+    LOG_WA("Log saLogStreamOpen_2 failed rc = %s", saf_error(rc));
   }
 
 exit_point:
+  if (rc != SA_AIS_OK) {
+    // Finalize client
+    osaf_set_millis_timeout(kWait8Seconds / 2, &timeout_time);
+    alarmStreamHandle = 0;
+    while (!osaf_is_timeout(&timeout_time)) {
+      SaAisErrorT ret = saLogFinalize(logHandle);
+      if (ret != SA_AIS_ERR_TRY_AGAIN) break;
+      osaf_nanosleep(&kHundredMilliseconds);
+    }
+  }
   TRACE_LEAVE();
-  return (result);
+  return true;
 }
 
 void logEvent() {
-  SaAisErrorT errorCode;
-  errorCode = saLogDispatch(logHandle, SA_DISPATCH_ALL);
-  if (SA_AIS_OK != errorCode) {
-    TRACE_1("Failed to dispatch log events (%d)", errorCode);
+  SaAisErrorT rc;
+  rc = saLogDispatch(logHandle, SA_DISPATCH_ALL);
+  if (rc != SA_AIS_OK) {
+    LOG_WA("Fail to dispatch log events rc = %s", saf_error(rc));
   }
   return;
 }
 
-/*
- * This method is to sync cached notification stored in this
- * class to the standby NTFD
- * param: uba, encoder pointer
- */
 void NtfLogger::syncRequest(NCS_UBAID *uba) {
 
   TRACE_ENTER();
diff --git a/src/ntf/ntfd/NtfLogger.h b/src/ntf/ntfd/NtfLogger.h
index cc0ac7dba..a95fa9764 100644
--- a/src/ntf/ntfd/NtfLogger.h
+++ b/src/ntf/ntfd/NtfLogger.h
@@ -21,49 +21,53 @@
 #ifndef NTF_NTFD_NTFLOGGER_H_
 #define NTF_NTFD_NTFLOGGER_H_
 
-/* ========================================================================
- *   INCLUDE FILES
- * ========================================================================
- */
-
 #include <list>
 #include <saLog.h>
-
 #include "ntf/ntfd/NtfNotification.h"
 #include "ntf/ntfd/NtfReader.h"
 
-/* ========================================================================
- *   DEFINITIONS
- * ========================================================================
- */
-
-/* ========================================================================
- *   TYPE DEFINITIONS
- * ========================================================================
- */
-
-/* ========================================================================
- *   DATA DECLARATIONS
- * ========================================================================
- */
-// class NtfReader;
 
 class NtfLogger {
   friend class NtfReader;
 
  public:
   NtfLogger();
-  //    virtual ~NtfLogger();
 
+  // Log the notification if the node is active side
+  // This function is not thread safe
   void log(NtfSmartPtr& notif, bool isLocal);
-  void checkQueueAndLog(NtfSmartPtr& notif);
-  SaAisErrorT logNotification(NtfSmartPtr& notif);
+
+  // Push the notification to the tail of queue
+  // This function is not thread safe
   void queueNotifcation(NtfSmartPtr& notif);
+
+  // Print logger information that includes the notification queue list size
+  // and reader cache size
+  // This function is not thread safe
   void printInfo();
+
+  // This method is to sync cached notification stored in this
+  // class to the standby NTFD
   void syncRequest(NCS_UBAID *uba);
 
  private:
-  SaAisErrorT initLog();
+  // Initialize log service with follow actions:
+  //  - Initialize the log client
+  //  - Open the alarm stream
+  //  - Updates the global variable logSelectionObject
+  // Return true if all actions have done
+  // This function is not thread safe
+  bool initLogAndOpenAlarmStream();
+
+  // Check if any notifications in the queue and log they to alarm stream
+  // Log the input notification to the alarm stream
+  // This function is not thread safe
+  void checkQueueAndLog(NtfSmartPtr& notif);
+
+  // Write the notification to the alarm stream
+  // Return true if the writing is successful
+  // This function is not thread safe
+  bool logNotification(NtfSmartPtr& notif);
 
   readerNotificationListT coll_;
   unsigned int readCounter;
-- 
2.15.1


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Opensaf-devel mailing list
Opensaf-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to