bool start(const char* working_dir, NdbProcess::Args& args) { g_info << "Starting " << name() << " "; for (unsigned i = 0; i < args.args().size(); i++) g_info << args.args()[i].c_str() << " "; g_info << endl; if (exe_valgrind == 0) { m_proc = NdbProcess::create(name(), exe(), working_dir, args); } else { NdbProcess::Args copy; if (arg_valgrind) { copy.add(arg_valgrind); } copy.add(exe()); copy.add(args); m_proc = NdbProcess::create(name(), BaseString(exe_valgrind), working_dir, copy); } return (m_proc != NULL); }
int runCreateOneTable(NDBT_Context* ctx, NDBT_Step* step) { // Table is already created... // so we just add it to table_list table_list.push_back(BaseString(ctx->getTab()->getName())); return NDBT_OK; }
BaseString BaseString::substr(ssize_t start, ssize_t stop) { if(stop < 0) stop = length(); ssize_t len = stop-start; if(len <= 0) return BaseString(""); BaseString s; s.assign(m_chr+start, len); return s; }
BaseString set_env_var(const BaseString& existing, const BaseString& name, const BaseString& value) { /* Split existing on space * (may have issues with env vars with spaces) * Split assignments on = * Where name == name, output new value */ BaseString newEnv; Vector<BaseString> assignments; int assignmentCount = existing.split(assignments, BaseString(" ")); for (int i=0; i < assignmentCount; i++) { Vector<BaseString> terms; int termCount = assignments[i].split(terms, BaseString("=")); if (termCount) { if (strcmp(name.c_str(), terms[0].c_str()) == 0) { /* Found element */ newEnv.append(name); newEnv.append('='); newEnv.append(value); } else { newEnv.append(assignments[i]); } } newEnv.append(' '); } return newEnv; }
int runCreateAllTables(NDBT_Context* ctx, NDBT_Step* step) { Uint32 useRangeScanT1 = ctx->getProperty("UseRangeScanT1", (Uint32)0); ndbout_c("createAllTables"); if (NDBT_Tables::createAllTables(GETNDB(step), false, true)) return NDBT_FAILED; for (int i = 0; i<NDBT_Tables::getNumTables(); i++) table_list.push_back(BaseString(NDBT_Tables::getTable(i)->getName())); if (useRangeScanT1) if (runCreateIndexT1(ctx, step) != NDBT_OK) return NDBT_FAILED; return NDBT_OK; }
int runGetTableList(NDBT_Context* ctx, NDBT_Step* step) { table_list.clear(); ndbout << "Looking for tables ... "; for (int i = 0; i<NDBT_Tables::getNumTables(); i++) { const NdbDictionary::Table* tab = GETNDB(step)->getDictionary() ->getTable(NDBT_Tables::getTable(i) ->getName()); if (tab != NULL) { ndbout << tab->getName() << " "; table_list.push_back(BaseString(tab->getName())); } } ndbout << endl; return NDBT_OK; }
int BaseString::split(Vector<BaseString> &v, const BaseString &separator, int maxSize) const { char *str = strdup(m_chr); int i, start, len, num = 0; len = strlen(str); for(start = i = 0; (i <= len) && ( (maxSize<0) || ((int)v.size()<=maxSize-1) ); i++) { if(strchr(separator.c_str(), str[i]) || i == len) { if(maxSize < 0 || (int)v.size() < maxSize-1) str[i] = '\0'; v.push_back(BaseString(str+start)); num++; start = i+1; } } free(str); return num; }
/** @brief Assigns from char *s, with maximum length n */ BaseString& assign(const char* s, size_t n); /** @brief Assigns from another BaseString, with maximum length n */ BaseString& assign(const BaseString& str, size_t n); /** * Assings from a Vector of BaseStrings, each Vector entry * separated by separator. * * @param vector Vector of BaseStrings to append * @param separator Separation between appended strings */ BaseString& assign(const Vector<BaseString> &vector, const BaseString &separator = BaseString(" ")); /** @brief Appends a char * to the end */ BaseString& append(const char* s); /** @brief Appends a char to the end */ BaseString& append(char c); /** @brief Appends another BaseString to the end */ BaseString& append(const BaseString& str); /** * Appends a Vector of BaseStrings to the end, each Vector entry * separated by separator. * * @param vector Vector of BaseStrings to append
int main() { BaseString s("abc"); BaseString t(s); s.assign("def"); t.append("123"); assert(s == "def"); assert(t == "abc123"); s.assign(""); t.assign(""); for (unsigned i = 0; i < 1000; i++) { s.append("xyz"); t.assign(s); assert(strlen(t.c_str()) % 3 == 0); } { BaseString s(":123:abc:;:foo:"); Vector<BaseString> v; assert(s.split(v, ":;") == 7); assert(v[0] == ""); assert(v[1] == "123"); assert(v[2] == "abc"); assert(v[3] == ""); assert(v[4] == ""); assert(v[5] == "foo"); assert(v[6] == ""); } { BaseString s(":123:abc:foo:bar"); Vector<BaseString> v; assert(s.split(v, ":;", 4) == 4); assert(v[0] == ""); assert(v[1] == "123"); assert(v[2] == "abc"); assert(v[3] == "foo:bar"); BaseString n; n.append(v, "()"); assert(n == "()123()abc()foo:bar"); n = ""; n.append(v); assert(n == " 123 abc foo:bar"); } { assert(BaseString("hamburger").substr(4,2) == ""); assert(BaseString("hamburger").substr(3) == "burger"); assert(BaseString("hamburger").substr(4,8) == "urge"); assert(BaseString("smiles").substr(1,5) == "mile"); assert(BaseString("012345").indexOf('2') == 2); assert(BaseString("hej").indexOf('X') == -1); } { assert(BaseString(" 1").trim(" ") == "1"); assert(BaseString("1 ").trim(" ") == "1"); assert(BaseString(" 1 ").trim(" ") == "1"); assert(BaseString("abc\t\n\r kalleabc\t\r\n").trim("abc\t\r\n ") == "kalle"); assert(BaseString(" ").trim(" ") == ""); } return 0; }
static bool do_change_version(atrt_config& config, SqlResultSet& command, AtrtClient& atrtdb){ /** * TODO make option to restart "not" initial */ uint process_id= command.columnAsInt("process_id"); const char* process_args= command.column("process_args"); g_logger.info("Change version for process: %d, args: %s", process_id, process_args); // Get the process if (process_id > config.m_processes.size()){ g_logger.critical("Invalid process id %d", process_id); return false; } atrt_process& proc= *config.m_processes[process_id]; const char* new_prefix= g_prefix1 ? g_prefix1 : g_prefix; const char* old_prefix= g_prefix; const char *start= strstr(proc.m_proc.m_path.c_str(), old_prefix); if (!start){ /* Process path does not contain old prefix. * Perhaps it contains the new prefix - e.g. is already * upgraded? */ if (strstr(proc.m_proc.m_path.c_str(), new_prefix)) { /* Process is already upgraded, *assume* that this * is ok * Alternatives could be - error, or downgrade. */ g_logger.info("Process already upgraded"); return true; } g_logger.critical("Could not find '%s' in '%s'", old_prefix, proc.m_proc.m_path.c_str()); return false; } // Save current proc state if (proc.m_save.m_saved == false) { proc.m_save.m_proc= proc.m_proc; proc.m_save.m_saved= true; } g_logger.info("stopping process..."); if (!stop_process(proc)) return false; BaseString newEnv = set_env_var(proc.m_proc.m_env, BaseString("MYSQL_BASE_DIR"), BaseString(new_prefix)); proc.m_proc.m_env.assign(newEnv); BaseString suffix(proc.m_proc.m_path.substr(strlen(old_prefix))); proc.m_proc.m_path.assign(new_prefix).append(suffix); if (process_args && strlen(process_args)) { /* Beware too long args */ proc.m_proc.m_args.append(" "); proc.m_proc.m_args.append(process_args); } ndbout << proc << endl; g_logger.info("starting process..."); if (!start_process(proc)) return false; return true; }
static bool do_change_version(atrt_config& config, SqlResultSet& command, AtrtClient& atrtdb){ /** * TODO make option to restart "not" initial */ uint process_id= command.columnAsInt("process_id"); const char* process_args= command.column("process_args"); g_logger.info("Change version for process: %d, args: %s", process_id, process_args); // Get the process if (process_id > config.m_processes.size()){ g_logger.critical("Invalid process id %d", process_id); return false; } atrt_process& proc= *config.m_processes[process_id]; const char* new_prefix= g_prefix1 ? g_prefix1 : g_prefix; const char* old_prefix= g_prefix; const char *start= strstr(proc.m_proc.m_path.c_str(), old_prefix); if (!start){ /* Process path does not contain old prefix. * Perhaps it contains the new prefix - e.g. is already * upgraded? */ if (strstr(proc.m_proc.m_path.c_str(), new_prefix)) { /* Process is already upgraded, *assume* that this * is ok * Alternatives could be - error, or downgrade. */ g_logger.info("Process already upgraded"); return true; } g_logger.critical("Could not find '%s' in '%s'", old_prefix, proc.m_proc.m_path.c_str()); return false; } // Save current proc state if (proc.m_save.m_saved == false) { proc.m_save.m_proc= proc.m_proc; proc.m_save.m_saved= true; } g_logger.info("stopping process..."); if (!stop_process(proc)) return false; BaseString newEnv = set_env_var(proc.m_proc.m_env, BaseString("MYSQL_BASE_DIR"), BaseString(new_prefix)); proc.m_proc.m_env.assign(newEnv); ssize_t pos = proc.m_proc.m_path.lastIndexOf('/') + 1; BaseString exename(proc.m_proc.m_path.substr(pos)); char * exe = find_bin_path(new_prefix, exename.c_str()); proc.m_proc.m_path = exe; if (exe) { free(exe); } if (process_args && strlen(process_args)) { /* Beware too long args */ proc.m_proc.m_args.append(" "); proc.m_proc.m_args.append(process_args); } { /** * In 5.5...binaries aren't compiled with rpath * So we need an explicit LD_LIBRARY_PATH * So when upgrading..we need to change LD_LIBRARY_PATH * So I hate 5.5... */ #if defined(__MACH__) ssize_t p0 = proc.m_proc.m_env.indexOf(" DYLD_LIBRARY_PATH="); #else ssize_t p0 = proc.m_proc.m_env.indexOf(" LD_LIBRARY_PATH="); #endif ssize_t p1 = proc.m_proc.m_env.indexOf(' ', p0 + 1); BaseString part0 = proc.m_proc.m_env.substr(0, p0); BaseString part1 = proc.m_proc.m_env.substr(p1); proc.m_proc.m_env.assfmt("%s%s", part0.c_str(), part1.c_str()); BaseString lib(g_libmysqlclient_so_path); ssize_t pos = lib.lastIndexOf('/') + 1; BaseString libname(lib.substr(pos)); char * exe = find_bin_path(new_prefix, libname.c_str()); char * dir = dirname(exe); #if defined(__MACH__) proc.m_proc.m_env.appfmt(" DYLD_LIBRARY_PATH=%s", dir); #else proc.m_proc.m_env.appfmt(" LD_LIBRARY_PATH=%s", dir); #endif free(exe); free(dir); } ndbout << proc << endl; g_logger.info("starting process..."); if (!start_process(proc)) return false; return true; }
int main(int argc, const char** argv){ ndb_init(); int help = 0; const char *cmd=0, *name=0, *group=0, *owner=0; int list = 0, start = 0, stop = 0, rm = 0; struct getargs args[] = { { "cmd", 'c', arg_string, &cmd, "command", "command to run (default ls)" } ,{ "name", 'n', arg_string, &name, "apply command for all processes with name" } ,{ "group", 'g', arg_string, &group, "apply command for all processes in group" } ,{ "owner", 'g', arg_string, &owner, "apply command for all processes with owner" } ,{ "long", 'l', arg_flag, &g_settings.m_longl, "long", "long listing"} ,{ "usage", '?', arg_flag, &help, "Print help", "" } ,{ "ls", 0, arg_flag, &list, "-c list", "list process(es)" } ,{ "start", 0, arg_flag, &start, "-c start", "start process(es)" } ,{ "stop", 0, arg_flag, &stop, "-c stop", "stop process(es)" } ,{ "rm", 0, arg_flag, &rm, "-c rm", "undefine process(es)" } }; const int num_args = 10; int i; int optind = 0; char desc[] = "[host:[port]]\n"; if(getarg(args, num_args, argc, argv, &optind) || help) { arg_printusage(args, num_args, argv[0], desc); return 1; } if(list + start + stop + rm > 1){ ndbout_c("Can only specify one command"); arg_printusage(args, num_args, argv[0], desc); return 1; } if(list) cmd = "list"; if(start) cmd = "start"; if(stop) cmd = "stop"; if(rm) cmd = "rm"; if(!cmd) cmd = "list"; Expression * m_expr = 0; for(i = optind; i<argc; i++){ add_host(g_hosts, argv[i]); } OrExpr * orE = new OrExpr(new Operate(cmd, g_settings), true); m_expr = orE; for(i = optind; i<argc; i++){ BaseString tmp(argv[i]); Vector<BaseString> split; tmp.split(split, ":"); if(split.size() > 2){ Uint32 id = atoi(split[2].c_str()); orE->push_back(new ProcEQ(g_hosts[i-optind], id)); } } if(g_hosts.size() == 0){ char buf[1024]; if(NdbEnv_GetEnv(ENV_HOSTS, buf, sizeof(buf))){ add_hosts(g_hosts, BaseString(buf)); } } if(g_hosts.size() == 0){ g_hosts.push_back(new SimpleCpcClient("localhost", g_settings.m_port)); } if(group != 0){ Expression * tmp = new FieldEQ("group", group); m_expr = new Match(* tmp, * m_expr); } if(name != 0){ Expression * tmp = new FieldEQ("name", name); m_expr = new Match(* tmp, * m_expr); } if(owner != 0){ Expression * tmp = new FieldEQ("owner", owner); m_expr = new Match(* tmp, * m_expr); } connect(g_hosts); for_each(g_hosts, * m_expr); return 0; }