Make it possible to delete log streams in the internal OpenSAF log server.  This
will free up resources for log streams that are no longer used, as well as make
it possible to create a new log stream with the same name but different
configuration options (max file size and number of backups).
---
 src/dtm/README                  |  7 ++++--
 src/dtm/tools/osaflog.cc        | 49 +++++++++++++++++++++++++++++------------
 src/dtm/transport/log_server.cc | 19 ++++++++++++++--
 src/dtm/transport/log_server.h  |  2 ++
 4 files changed, 59 insertions(+), 18 deletions(-)

diff --git a/src/dtm/README b/src/dtm/README
index c671cfd39..430ff1950 100644
--- a/src/dtm/README
+++ b/src/dtm/README
@@ -185,8 +185,11 @@ Options:
                       server to disk even when no LOGSTREAM
                       is specified.
 --print               print the messages stored on disk for the
-                      specified LOGSTREAM. This option is default
-                      when no option is specified.
+                      specified LOGSTREAM(s). This option is the
+                      default when no option is specified.
+--delete              Delete the specified LOGSTREAM(s) by
+                      removing allocated resources in the log
+                      server. Does not delete log files from disk.
 --max-file-size=SIZE  Set the maximum size of the log file to
                       SIZE bytes. The log file will be rotated
                       when it exceeds this size. Suffixes k, M and
diff --git a/src/dtm/tools/osaflog.cc b/src/dtm/tools/osaflog.cc
index 572c68ad1..c5946eea7 100644
--- a/src/dtm/tools/osaflog.cc
+++ b/src/dtm/tools/osaflog.cc
@@ -47,6 +47,7 @@ bool Flush();
 base::UnixServerSocket* CreateSocket();
 uint64_t Random64Bits(uint64_t seed);
 bool PrettyPrint(const std::string& log_stream);
+bool Delete(const std::string& log_stream);
 std::list<int> OpenLogFiles(const std::string& log_stream);
 std::string PathName(const std::string& log_stream, int suffix);
 uint64_t GetInode(int fd);
@@ -61,21 +62,23 @@ int main(int argc, char** argv) {
   struct option long_options[] = {{"max-file-size", required_argument, 0, 'm'},
                                   {"max-backups", required_argument, 0, 'b'},
                                   {"flush", no_argument, 0, 'f'},
-                                  {"print", required_argument, nullptr, 'p'},
+                                  {"print", no_argument, nullptr, 'p'},
+                                  {"delete", no_argument, nullptr, 'd'},
                                   {0, 0, 0, 0}};
 
   uint64_t max_file_size = 0;
   uint64_t max_backups = 0;
-  char *pretty_print_argument = NULL;
   int option = 0;
 
   int long_index = 0;
   bool flush_result =  true;
   bool print_result =  true;
+  bool delete_result =  true;
   bool max_file_size_result = true;
   bool number_of_backups_result = true;
   bool flush_set = false;
   bool pretty_print_set = false;
+  bool delete_set = false;
   bool max_file_size_set = false;
   bool max_backups_set = false;
 
@@ -88,10 +91,12 @@ int main(int argc, char** argv) {
                    long_options, &long_index)) != -1) {
         switch (option) {
              case 'p':
-                   pretty_print_argument = optarg;
                    pretty_print_set = true;
                    flush_set = true;
                  break;
+             case 'd':
+                   delete_set = true;
+                 break;
              case 'f':
                    flush_set = true;
                  break;
@@ -115,12 +120,13 @@ int main(int argc, char** argv) {
         }
   }
 
-  if (argc - optind == 1) {
-     flush_result = Flush();
-     flush_set = false;
-     print_result = PrettyPrint(argv[optind]);
-     pretty_print_set = false;
-  } else if (argc - optind > 1) {
+  if (argc > optind && !pretty_print_set && !delete_set) {
+    pretty_print_set = true;
+    flush_set = true;
+  }
+
+  if ((argc <= optind && (pretty_print_set || delete_set)) ||
+      (pretty_print_set && delete_set)) {
      PrintUsage(argv[0]);
      exit(EXIT_FAILURE);
   }
@@ -129,7 +135,14 @@ int main(int argc, char** argv) {
      flush_result = Flush();
   }
   if (pretty_print_set == true) {
-     print_result = PrettyPrint(pretty_print_argument);
+    while (print_result && optind < argc) {
+      print_result = PrettyPrint(argv[optind++]);
+    }
+  }
+  if (delete_set == true) {
+    while (delete_result && optind < argc) {
+      delete_result = Delete(argv[optind++]);
+    }
   }
   if (max_backups_set == true) {
      number_of_backups_result = NoOfBackupFiles(max_backups);
@@ -138,7 +151,7 @@ int main(int argc, char** argv) {
      max_file_size_result = MaxTraceFileSize(max_file_size);
   }
   if (flush_result && print_result && max_file_size_result &&
-                                          number_of_backups_result)
+      delete_result && number_of_backups_result)
      exit(EXIT_SUCCESS);
   exit(EXIT_FAILURE);
 }
@@ -147,7 +160,7 @@ namespace {
 
 void PrintUsage(const char* program_name) {
   fprintf(stderr,
-          "Usage: %s [OPTION] [LOGSTREAM]\n"
+          "Usage: %s [OPTION] [LOGSTREAM...]\n"
           "\n"
           "print the messages stored on disk for the specified\n"
           "LOGSTREAM. When a LOGSTREAM argument is specified, the option\n"
@@ -159,8 +172,11 @@ void PrintUsage(const char* program_name) {
           "                      server to disk even when no LOGSTREAM\n"
           "                      is specified.\n"
           "--print               print the messages stored on disk for the\n"
-          "                      specified LOGSTREAM. This option is default\n"
-          "                      when no option is specified.\n"
+          "                      specified LOGSTREAM(s). This option is the\n"
+          "                      default when no option is specified.\n"
+          "--delete              Delete the specified LOGSTREAM(s) by\n"
+          "                      removing allocated resources in the log\n"
+          "                      server. Does not delete log files from 
disk.\n"
           "--max-file-size=SIZE  Set the maximum size of the log file to\n"
           "                      SIZE bytes. The log file will be rotated\n"
           "                      when it exceeds this size. Suffixes k, M 
and\n"
@@ -297,6 +313,11 @@ bool PrettyPrint(const std::string& log_stream) {
   return result;
 }
 
+bool Delete(const std::string& log_stream) {
+  return SendCommand(std::string("delete ") +
+                     log_stream);
+}
+
 std::list<int> OpenLogFiles(const std::string& log_stream) {
   std::list<int> result{};
   bool last_open_failed = false;
diff --git a/src/dtm/transport/log_server.cc b/src/dtm/transport/log_server.cc
index f7ccdf765..7765d0790 100644
--- a/src/dtm/transport/log_server.cc
+++ b/src/dtm/transport/log_server.cc
@@ -36,9 +36,9 @@ LogServer::LogServer(int term_fd)
       max_file_size_{5 * 1024 * 1024},
       log_socket_{Osaflog::kServerSocketPath, base::UnixSocket::kNonblocking},
       log_streams_{},
-      current_stream_{new LogStream{"mds.log", 1, 5 * 1024 * 1024}},
+      current_stream_{new LogStream{kMdsLogStreamName, 1, 5 * 1024 * 1024}},
       no_of_log_streams_{1} {
-  log_streams_["mds.log"] = current_stream_;
+  log_streams_[kMdsLogStreamName] = current_stream_;
   }
 
 LogServer::~LogServer() {
@@ -236,6 +236,21 @@ std::string LogServer::ExecuteCommand(const std::string& 
command,
     uint64_t max_backups = base::StrToUint64(argument.c_str(), &success);
     if (success && max_backups <= SIZE_MAX) max_backups_ = max_backups;
     return std::string{"!max-backups " + std::to_string(max_backups_)};
+  } else if (command == "?delete") {
+    if (!ValidateLogName(argument.c_str(), argument.size()) ||
+        argument == kMdsLogStreamName) {
+      return std::string{"Invalid stream name"};
+    }
+    auto iter = log_streams_.find(argument);
+    if (iter == log_streams_.end()) {
+      return std::string{"Stream not found"};
+    }
+    LogStream* stream = iter->second;
+    log_streams_.erase(iter);
+    if (current_stream_ == stream) {
+      current_stream_ = log_streams_.begin()->second;
+    }
+    return std::string{"!delete " + argument};
   } else if (command == "?flush") {
     for (const auto& s : log_streams_) {
       LogStream* stream = s.second;
diff --git a/src/dtm/transport/log_server.h b/src/dtm/transport/log_server.h
index a767c5c63..793465d40 100644
--- a/src/dtm/transport/log_server.h
+++ b/src/dtm/transport/log_server.h
@@ -35,6 +35,8 @@ class LogServer {
   static constexpr size_t kMaxNoOfStreams = 32;
   static constexpr const char* kTransportdConfigFile =
                                        PKGSYSCONFDIR "/transportd.conf";
+
+  static constexpr const char* kMdsLogStreamName = "mds.log";
   // @a term_fd is a file descriptor that will become readable when the program
   // should exit because it has received the SIGTERM signal.
   explicit LogServer(int term_fd);
-- 
2.13.3


------------------------------------------------------------------------------
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