MgmtSrvrConfig.cpp 7.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <signaldata/TestOrd.hpp>
#include <OutputStream.hpp>

#include "MgmtSrvr.hpp"
#include "SignalQueue.hpp"
#include <InitConfigFileParser.hpp>
#include <ConfigRetriever.hpp>
#include <ndb_version.h>

void
MgmtSrvr::handle_MGM_LOCK_CONFIG_REQ(NdbApiSignal *signal) {
  NodeId sender = refToNode(signal->theSendersBlockRef);
  const MgmLockConfigReq * const req = CAST_CONSTPTR(MgmLockConfigReq, signal->getDataPtr());

  NdbApiSignal *reply = getSignal();
  if(signal == NULL)
    return; /** @todo handle allocation failure */

  reply->set(TestOrd::TraceAPI,
	      MGMSRV,
	      GSN_MGM_LOCK_CONFIG_REP,
	      MgmLockConfigRep::SignalLength);

  MgmLockConfigRep *lockRep = CAST_PTR(MgmLockConfigRep, reply->getDataPtrSend());

  lockRep->errorCode = MgmLockConfigRep::UNKNOWN_ERROR;

  if(req->newConfigGeneration < m_nextConfigGenerationNumber) {
    lockRep->errorCode = MgmLockConfigRep::GENERATION_MISMATCH;
    goto done;
  }
  NdbMutex_Lock(m_configMutex);

  m_nextConfigGenerationNumber = req->newConfigGeneration+1;

  lockRep->errorCode = MgmLockConfigRep::OK;

 done:
  sendSignal(sender, NO_WAIT, reply, true);
  NdbMutex_Unlock(m_configMutex);
  return;
}

void
MgmtSrvr::handle_MGM_UNLOCK_CONFIG_REQ(NdbApiSignal *signal) {
  NodeId sender = refToNode(signal->theSendersBlockRef);
  const MgmUnlockConfigReq * const req = CAST_CONSTPTR(MgmUnlockConfigReq, signal->getDataPtr());
  MgmUnlockConfigRep *unlockRep;

  NdbApiSignal *reply = getSignal();
  if(signal == NULL)
    goto error; /** @todo handle allocation failure */

  reply->set(TestOrd::TraceAPI,
	     MGMSRV,
	     GSN_MGM_UNLOCK_CONFIG_REP,
	     MgmUnlockConfigRep::SignalLength);

  unlockRep = CAST_PTR(MgmUnlockConfigRep,  reply->getDataPtrSend());

  unlockRep->errorCode = MgmUnlockConfigRep::UNKNOWN_ERROR;


  NdbMutex_Lock(m_configMutex);

  if(req->commitConfig == 1) {
    m_newConfig = fetchConfig();
    commitConfig();
  } else
    rollbackConfig();
  
  unlockRep->errorCode = MgmUnlockConfigRep::OK;

  sendSignal(sender, NO_WAIT, reply, true);
 error:
  NdbMutex_Unlock(m_configMutex);
  return;
}


/**
 * Prepare all MGM nodes for configuration changes
 * 
 * @returns 0 on success, or -1 on failure
 */
int
MgmtSrvr::lockConf() {
  int result = -1;
  MgmLockConfigReq* lockReq;
  NodeId node = 0;

  /* Check if this is the master node */
  if(getPrimaryNode() != _ownNodeId)
    goto done;

  if(NdbMutex_Trylock(m_configMutex) != 0)
    return -1;

  m_newConfig = new Config(*_config); /* copy the existing config */
  _config = m_newConfig;
  
  m_newConfig = new Config(*_config);

  m_nextConfigGenerationNumber++;

  /* Make sure the new configuration _always_ is at least one step older */
  if(m_nextConfigGenerationNumber < m_newConfig->getGenerationNumber()+1)
    m_nextConfigGenerationNumber = _config->getGenerationNumber()+1;

  m_newConfig->setGenerationNumber(m_nextConfigGenerationNumber);

  node = 0;
  while(getNextNodeId(&node, NDB_MGM_NODE_TYPE_MGM)) {
    if(node != _ownNodeId) {
      NdbApiSignal* signal = getSignal();
      if (signal == NULL) {
	result = COULD_NOT_ALLOCATE_MEMORY;
	goto done;
      }
      
      lockReq = CAST_PTR(MgmLockConfigReq, signal->getDataPtrSend());
      signal->set(TestOrd::TraceAPI,
		  MGMSRV,
		  GSN_MGM_LOCK_CONFIG_REQ,
		  MgmLockConfigReq::SignalLength);
      
      lockReq->newConfigGeneration = m_nextConfigGenerationNumber;
      
      result = sendSignal(node, NO_WAIT, signal, true);

      NdbApiSignal *reply = 
	m_signalRecvQueue.waitFor(GSN_MGM_LOCK_CONFIG_REP, 0);

      if(reply == NULL) {
	/** @todo handle timeout/error */
	ndbout << __FILE__ << ":" << __LINE__ << endl;
	result = -1;
	goto done;
      }

    }
  }

 done:
  NdbMutex_Unlock(m_configMutex);
  return result;
}

/**
 * Unlocks configuration
 * 
 * @returns 0 on success, ! 0 on error
 */
int
MgmtSrvr::unlockConf(bool commit) {
  int result = -1;
  MgmUnlockConfigReq* unlockReq;
  NodeId node = 0;

  /* Check if this is the master node */
  if(getPrimaryNode() != _ownNodeId)
    goto done;

  errno = 0;
  if(NdbMutex_Lock(m_configMutex) != 0)
    return -1;

  if(commit)
    commitConfig();
  else
    rollbackConfig();

  node = 0;
  while(getNextNodeId(&node, NDB_MGM_NODE_TYPE_MGM)) {
    if(node != _ownNodeId) {
      NdbApiSignal* signal = getSignal();
      if (signal == NULL) {
	result = COULD_NOT_ALLOCATE_MEMORY;
	goto done;
      }
      
      unlockReq = CAST_PTR(MgmUnlockConfigReq, signal->getDataPtrSend());
      signal->set(TestOrd::TraceAPI,
		  MGMSRV,
		  GSN_MGM_UNLOCK_CONFIG_REQ,
		  MgmUnlockConfigReq::SignalLength);
      unlockReq->commitConfig = commit;
      
      result = sendSignal(node, NO_WAIT, signal, true);

      NdbApiSignal *reply = 
	m_signalRecvQueue.waitFor(GSN_MGM_UNLOCK_CONFIG_REP, 0);

      if(reply == NULL) {
	/** @todo handle timeout/error */
	result = -1;
	goto done;
      }

    }
  }

 done:
  NdbMutex_Unlock(m_configMutex);
  return result;
}

/**
 * Commit the new configuration
 */
int
MgmtSrvr::commitConfig() {
  int ret = saveConfig(m_newConfig);
  delete _config;
  _config = m_newConfig;
  m_newConfig = NULL;
  ndbout << "commit " << ret << endl;
  return ret;
}

/**
 * Rollback to the old configuration
 */
int
MgmtSrvr::rollbackConfig() {
  delete m_newConfig;
  m_newConfig = NULL;
  ndbout << "rollback" << endl;
  return saveConfig(_config);
}

/**
 * Save a configuration to the running configuration file
 */
int
MgmtSrvr::saveConfig(const Config *conf) {
  BaseString newfile;
  newfile.appfmt("%s.new", m_configFilename.c_str());
  
  /* Open and write to the new config file */
  FILE *f = fopen(newfile.c_str(), "w");
  if(f == NULL) {
    /** @todo Send something apropriate to the log */
    return -1;
  }
  FileOutputStream stream(f);
  conf->printConfigFile(stream);

  fclose(f);

  /* Rename file to real name */
  rename(newfile.c_str(), m_configFilename.c_str());

  return 0;
}

Config *
MgmtSrvr::readConfig() {
  Config *conf = NULL;
  if(m_configFilename.length() != 0) {
    /* Use config file */
278 279 280 281
    InitConfigFileParser parser;
    conf = parser.parseConfig(m_configFilename.c_str());
    
    if(conf == NULL) {
282
      /* Try to get configuration from other MGM server */
283
      return fetchConfig();
284 285 286 287 288 289 290
    }
  }
  return conf;
}

Config *
MgmtSrvr::fetchConfig() {
joreland@mysql.com's avatar
joreland@mysql.com committed
291
  ConfigRetriever cr(NDB_VERSION, NODE_TYPE_MGM);
292
  cr.setLocalConfigFileName(m_localNdbConfigFilename.c_str());
joreland@mysql.com's avatar
joreland@mysql.com committed
293
  struct ndb_mgm_configuration * tmp = cr.getConfig();
294 295 296 297 298
  if(tmp != 0){
    Config * conf = new Config();
    conf->m_configValues = tmp;
    return conf;
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
299
  
300
  return 0;
301 302 303 304 305 306 307 308 309 310
}

bool
MgmtSrvr::changeConfig(const BaseString &section,
		       const BaseString &param,
		       const BaseString &value) {
  if(m_newConfig == NULL)
    return false;
  return m_newConfig->change(section, param, value);
}