|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | + |
| 4 | +#include <unistd.h> |
| 5 | +#include <getopt.h> |
| 6 | + |
| 7 | +#include "notificationproducer.h" |
| 8 | +#include "notificationconsumer.h" |
| 9 | +#include "select.h" |
| 10 | +#include "logger.h" |
| 11 | + |
| 12 | + |
| 13 | +void printUsage() |
| 14 | +{ |
| 15 | + SWSS_LOG_ENTER(); |
| 16 | + |
| 17 | + std::cout << "Usage: orchagent_restart_check [-s] " << std::endl; |
| 18 | + std::cout << " -n --noFreeze" << std::endl; |
| 19 | + std::cout << " Don't freeze orchagent even if check succeeded" << std::endl; |
| 20 | + std::cout << " -s --skipPendingTaskCheck" << std::endl; |
| 21 | + std::cout << " Skip pending task dependency check for orchagent" << std::endl; |
| 22 | + std::cout << " -w --waitTime" << std::endl; |
| 23 | + std::cout << " Wait time for response from orchagent, in milliseconds. Default value: 1000" << std::endl; |
| 24 | + std::cout << " -h --help:" << std::endl; |
| 25 | + std::cout << " Print out this message" << std::endl; |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +/* |
| 30 | + * Before stopping orchagent for warm restart, basic state check is preferred to |
| 31 | + * ensure orchagent is not in transient state, so a deterministic state may be restored after restart. |
| 32 | + * |
| 33 | + * Here is to implement orchagent_restart_check binary which may talk to orchagent and |
| 34 | + * ask it to do self-check, return "READY " signal and freeze if everything is ok, |
| 35 | + * otherwise "NOT_READY" signal should be returned. |
| 36 | + * |
| 37 | + * Optionally: |
| 38 | + * if --noFreeze option is provided, orchagent won't freeze. |
| 39 | + * if --skipPendingTaskCheck option is provided, orchagent won't use |
| 40 | + * whether there is pending task existing as state check criterion. |
| 41 | + */ |
| 42 | +int main(int argc, char **argv) |
| 43 | +{ |
| 44 | + swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_INFO); |
| 45 | + SWSS_LOG_ENTER(); |
| 46 | + |
| 47 | + std::string skipPendingTaskCheck = "fasle"; |
| 48 | + std::string noFreeze = "fasle"; |
| 49 | + /* Default wait time is 1000 millisecond */ |
| 50 | + int waitTime = 1000; |
| 51 | + |
| 52 | + const char* const optstring = "nsw:"; |
| 53 | + while(true) |
| 54 | + { |
| 55 | + static struct option long_options[] = |
| 56 | + { |
| 57 | + { "noFreeze", no_argument, 0, 'n' }, |
| 58 | + { "skipPendingTaskCheck", no_argument, 0, 's' }, |
| 59 | + { "waitTime", required_argument, 0, 'w' } |
| 60 | + }; |
| 61 | + |
| 62 | + int option_index = 0; |
| 63 | + |
| 64 | + int c = getopt_long(argc, argv, optstring, long_options, &option_index); |
| 65 | + |
| 66 | + if (c == -1) |
| 67 | + { |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + switch (c) |
| 72 | + { |
| 73 | + case 'n': |
| 74 | + SWSS_LOG_NOTICE("Won't freeze orchagent even if check succeeded"); |
| 75 | + noFreeze = "true"; |
| 76 | + break; |
| 77 | + case 's': |
| 78 | + SWSS_LOG_NOTICE("Skipping pending task check for orchagent"); |
| 79 | + skipPendingTaskCheck = "true"; |
| 80 | + break; |
| 81 | + case 'w': |
| 82 | + SWSS_LOG_NOTICE("Wait time for response from orchagent set to %s milliseconds", optarg); |
| 83 | + waitTime = atoi(optarg); |
| 84 | + break; |
| 85 | + case 'h': |
| 86 | + printUsage(); |
| 87 | + exit(EXIT_SUCCESS); |
| 88 | + |
| 89 | + case '?': |
| 90 | + SWSS_LOG_WARN("unknown option %c", optopt); |
| 91 | + printUsage(); |
| 92 | + exit(EXIT_FAILURE); |
| 93 | + |
| 94 | + default: |
| 95 | + SWSS_LOG_ERROR("getopt_long failure"); |
| 96 | + exit(EXIT_FAILURE); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + swss::DBConnector db(APPL_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, 0); |
| 101 | + // Send warm restart query via "RESTARTCHECK" notification channel |
| 102 | + swss::NotificationProducer restartQuery(&db, "RESTARTCHECK"); |
| 103 | + // Will listen for the reply on "RESTARTCHECKREPLY" channel |
| 104 | + swss::NotificationConsumer restartQueryReply(&db, "RESTARTCHECKREPLY"); |
| 105 | + |
| 106 | + std::vector<swss::FieldValueTuple> values; |
| 107 | + values.emplace_back("NoFreeze", noFreeze); |
| 108 | + values.emplace_back("SkipPendingTaskCheck", skipPendingTaskCheck); |
| 109 | + std::string op = "orchagent"; |
| 110 | + SWSS_LOG_NOTICE("requested %s to do warm restart state check", op.c_str()); |
| 111 | + restartQuery.send(op, op, values); |
| 112 | + |
| 113 | + |
| 114 | + swss::Select s; |
| 115 | + s.addSelectable(&restartQueryReply); |
| 116 | + swss::Selectable *sel; |
| 117 | + std::string op_ret, data; |
| 118 | + values.clear(); |
| 119 | + int result = s.select(&sel, waitTime); |
| 120 | + if (result == swss::Select::OBJECT) |
| 121 | + { |
| 122 | + restartQueryReply.pop(op_ret, data, values); |
| 123 | + if (data == "READY") |
| 124 | + { |
| 125 | + SWSS_LOG_NOTICE("RESTARTCHECK success, %s is frozen and ready for warm restart", op_ret.c_str()); |
| 126 | + std::cout << "RESTARTCHECK succeeded" << std::endl; |
| 127 | + return EXIT_SUCCESS; |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + SWSS_LOG_NOTICE("RESTARTCHECK failed, %s is not ready for warm restart with status %s", |
| 132 | + op_ret.c_str(), data.c_str()); |
| 133 | + } |
| 134 | + } |
| 135 | + else if (result == swss::Select::TIMEOUT) |
| 136 | + { |
| 137 | + SWSS_LOG_NOTICE("RESTARTCHECK for %s timed out", op_ret.c_str()); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + SWSS_LOG_NOTICE("RESTARTCHECK for %s error", op_ret.c_str()); |
| 142 | + } |
| 143 | + std::cout << "RESTARTCHECK failed" << std::endl; |
| 144 | + return EXIT_FAILURE; |
| 145 | +} |
0 commit comments