You are here

replication.module in Replication 5.0

Same filename and directory in other branches
  1. 8.0 replication.module
  2. 5 replication.module

File

replication.module
View source
<?php

/**
 * Replication plugin for multi-master Mysql Replication
 * Author: Michael Heath
 * Date: 2007-06-13 17:46 GMT
 * Developed for use at eServGlobal Ltd.
 * http://www.eservglobal.com
 */
function replication_help($section = '') {
  $output = '';
  switch ($section) {
    case "admin/help#replication":
      $output = '<p>' . t("Database Replication Tool") . '</p>';
      break;
  }
  return $output;
}

// function replication_help
function replication_perm() {
  return array(
    'administer replication',
  );
}

// function replication_perm
function replication_menu() {
  $items = array();

  // Administration Options
  $items[] = array(
    'path' => 'admin/settings/replication',
    'title' => t('Replication settings'),
    'callback' => 'drupal_get_form',
    'callback arguments' => 'replication_admin',
    'access' => user_access('administer replication'),
    'description' => 'Configure database replication',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

// function replication_menu
function replication_admin() {
  $options = array();
  $i = 0;
  $themes = system_theme_data();
  foreach ($themes as $key => $values) {
    $options[$key] = $key;
    $i++;
  }

  //general replication settings
  $form['replication'] = array(
    '#type' => 'fieldset',
    '#title' => t('General Settings'),
    '#weight' => 0,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  //define whether we are:

  //0 - free-standing (no replication takes place)

  //1 - the master server

  //2 - a slave server

  //NOTE that if the replication module has not been installed then any application testing this

  //variable should assume the default 0 - that the server is not part of replication

  //note also that if a server is being used as a 'distributor' its setting should be as a slave

  //since writes will still be made to the overall master server
  $form['replication']['replication_server_mode'] = array(
    '#type' => 'select',
    '#title' => 'Server mode',
    '#required' => 'TRUE',
    '#options' => array(
      '0' => 'Single server',
      '1' => 'Master mode',
      '2' => 'Replication slave',
    ),
    '#default_value' => variable_get('replication_server_mode', '0'),
    '#description' => t('If you are not running a multi-server system, you should leave this set to "Single Server" mode'),
  );
  $form['replication']['replication_admin_identifier'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('replication_admin_identifier', ''),
    '#title' => t('Slave Server identifier'),
    '#maxlength' => '10',
    '#size' => '10',
    '#description' => t("Enter a unique server identifier - e.g 'slaveid001'"),
  );
  $form['replication']['replication_admin_max_recheck'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('replication_admin_max_recheck', '10'),
    '#title' => t('Maximum re-tests'),
    '#required' => 'TRUE',
    '#maxlength' => '2',
    '#size' => '2',
    '#description' => t("To test connectivity with the master server a slave writes data to the master then reads locally. Enter a value for the maximum number of times the slave will repeat this test during a replication check."),
  );
  $form['replication']['replication_admin_wait_recheck'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('replication_admin_wait_recheck', '1'),
    '#title' => t('Re-test delay.'),
    '#required' => 'TRUE',
    '#maxlength' => '2',
    '#size' => '2',
    '#description' => t("Enter a value in seconds to wait before re-checking connectivity after a failed test. Note that a longer delay may cause cron to fail."),
  );

  //theme settings
  $form['replication_theme'] = array(
    '#type' => 'fieldset',
    '#title' => t('Theme configuration'),
    '#weight' => 0,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['replication_theme']['replication_admin_normal_theme'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => variable_get('replication_admin_normal_theme', 'garland'),
    '#title' => t('Regular theme'),
    '#required' => 'TRUE',
    '#description' => t("The theme to be used when the mysql master is available."),
  );
  $form['replication_theme']['replication_admin_warning_theme'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => variable_get('replication_admin_warning_theme', 'garland'),
    '#title' => t('Recovery theme'),
    '#required' => 'TRUE',
    '#description' => t("The theme to be used when the mysql master is unavailable."),
  );

  // extended configuration settings - for applications doing additional replication using http
  $form['replication_http'] = array(
    '#type' => 'fieldset',
    '#title' => 'HTTP replication settings',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  //this should only be available if we're a slave, need some inline js to deal with it ZZZZZZZZZZZZ
  $form['replication_http']['replication_server_url'] = array(
    '#type' => 'textfield',
    '#title' => 'Master URL',
    '#maxlength' => 255,
    '#default_value' => variable_get('replication_server_url', ''),
    '#description' => t('If this node is a replication slave, you should specify the master server url here - including the leading http:// or https:// etc. Otherwise, leave this field blank. DO NOT include a trailing slash'),
  );
  $form['replication_http']['replication_http_port'] = array(
    '#type' => 'textfield',
    '#title' => 'Replication Port',
    '#maxlength' => 5,
    '#size' => 5,
    '#default_value' => variable_get('replication_http_port', ''),
    '#description' => t('By default the port used for http transfers is 80 for http and 443 for https. If you wish to use a different port you should specify it here. Otherwise, leave this field blank'),
  );
  $form['replication_http']['replication_http_connect'] = array(
    '#type' => 'textfield',
    '#title' => 'Replication connection timeout (in Seconds)',
    '#maxlength' => 5,
    '#size' => 5,
    '#default_value' => variable_get('replication_http_connect', '5'),
    '#description' => t('This is the time allowed to establish the http connection - Enter a value in seconds.'),
  );
  $form['replication_http']['replication_http_timeout'] = array(
    '#type' => 'textfield',
    '#title' => 'Replication timeout (Milliseconds per Kilobyte)',
    '#maxlength' => 6,
    '#size' => 6,
    '#default_value' => variable_get('replication_http_timeout', '500'),
    '#description' => t('This controls the MINIMUM data speed. Enter a value in milliseconds.'),
  );
  $form['replication_http']['replication_http_speedlimit'] = array(
    '#type' => 'textfield',
    '#title' => 'Maximum speed (Kilobits per second)',
    '#maxlength' => 10,
    '#size' => 10,
    '#default_value' => variable_get('replication_http_speedlimit', '128'),
    '#description' => t('Enter a value in kilobits.'),
  );
  $form['replication_http']['replication_http_retries'] = array(
    '#type' => 'textfield',
    '#title' => 'Maximum number of retries',
    '#maxlength' => 3,
    '#size' => 3,
    '#default_value' => variable_get('replication_http_retries', '5'),
    '#description' => t('Enter a number for the maximum number of attempts.'),
  );

  /*  Now replaced by application specific switches
    $form['replication_http']['replication_http_cron_enabled'] = array (
      '#type' => 'checkbox',
      '#title' => 'Enable file replication using cron',
      '#default_value' => variable_get('replication_http_cron_enabled', 0),
      '#description' => t('Unless checked no attempt will be made to replicate files from master server')
    );
  */
  return system_settings_form($form);
}

//function replication_admin

/**
 * Implementation of hook_cron
 * note that the variable 'replication_online' is an internal variable - there is no admin option to set it
 * in the long term it will also be switched off when any db_query fails in database.inc
 *
 */
function replication_cron() {
  if (variable_get('replication_server_mode', '0') != 2) {
    return;
  }

  // Check the timestamp from the last run
  $previoustimestamp = variable_get('replication_timestamp', 0);
  $recoverytheme = variable_get('replication_admin_warning_theme', 'garland');
  $normaltheme = variable_get('replication_admin_normal_theme', 'garland');

  // Here are the queries we run to check if replication is working
  $deletequery = sprintf("DELETE from {replication} where slaveid='%s'", variable_get('replication_admin_identifier', ''));
  $insertquery = sprintf("INSERT into {replication} (slaveid) VALUES ('%s')", variable_get('replication_admin_identifier', ''));
  $selectquery = sprintf("SELECT timestamp FROM {replication} WHERE slaveid='%s'", variable_get('replication_admin_identifier', 'slaveid001'));
  $delresult = db_query($deletequery);
  $insresult = db_query($insertquery);
  $retries = 0;

  // if we havne't tried more than the defined max times then...
  while ($retries < variable_get('replication_admin_max_recheck', '10')) {
    $selresult = db_query($selectquery);
    $links = db_fetch_object($selresult);

    // If the current timestamp is different then it's a success and we can stop
    if ($links->timestamp != $previoustimestamp) {
      variable_set('replication_online', '1');
      variable_set('replication_timestamp', $links->timestamp);
      variable_set('theme_default', $normaltheme);
      $retries = variable_get('replication_admin_max_recheck', '10');
    }
    else {
      sleep(variable_get('replication_admin_wait_recheck', '1'));
    }
    $retries++;
  }
  if ($links->timestamp == $previoustimestamp) {
    variable_set('replication_online', '0');
    variable_set('theme_default', $recoverytheme);
  }
}

Functions

Namesort descending Description
replication_admin
replication_cron Implementation of hook_cron note that the variable 'replication_online' is an internal variable - there is no admin option to set it in the long term it will also be switched off when any db_query fails in database.inc
replication_help Replication plugin for multi-master Mysql Replication Author: Michael Heath Date: 2007-06-13 17:46 GMT Developed for use at eServGlobal Ltd. http://www.eservglobal.com
replication_menu
replication_perm