<commit_before>"use strict";

var f = require('util').format
  , path = require('path')
  , fs = require('fs')
  , mkdirp = require('mkdirp')
  , rimraf = require('rimraf')
  , exec = require('child_process').exec
  , ServerManager = require('./server_manager')
  , ReadPreference = require('../../lib/topologies/read_preference')
  , Server = require('../../lib/topologies/server')
  , ReplSet = require('../../lib/topologies/replset')

var Logger = require('../../lib/connection/logger')

//
// Clone the options
var cloneOptions = function(options) {
  var opts = {};
  for(var name in options) {
    opts[name] = options[name];
  }
  return opts;
}

//
// Remove any non-server specific settings
var filterInternalOptionsOut = function(options, internalOptions) {
  var opts = {};

  for(var name in options) {
    if(internalOptions.indexOf(name) == -1) {
      opts[name] = options[name];
    }
  }

  return opts;
}

var ReplSetManager = function(replsetOptions) {
  replsetOptions = replsetOptions || {};
  var startPort = replsetOptions.port = replsetOptions.startPort || 31000;

  // Get the settings
  var secondaries = replsetOptions.secondaries || 2;
  var arbiters = replsetOptions.arbiters || 0;
  var passives = replsetOptions.passives || 1;
  var replSet = replsetOptions.replSet = replsetOptions.replSet || 'rs';
  var version = 1;
  var configSet = null;
  var tags = replsetOptions.tags;
  var host = replsetOptions.host || 'localhost';

  // All server addresses
  var serverAddresses = [];
  var secondaryServers = [];
  var arbiterServers = [];
  var passiveServers = [];
  var primaryServer = [];;
  // DbPath
  var dbpath = replsetOptions.dbpath = replsetOptions.dbpath || path.resolve('data');

  // Get the keys
  var keys = Object.keys(replsetOptions);

  // Internal check server
  var server = null;

  // Clone the options
  replsetOptions = cloneOptions(replsetOptions);
  
  // Any needed credentials
  var credentials;

  // Contains all the server managers
  var serverManagers = [];

  // filtered out internal keys
  var internalOptions = filterInternalOptionsOut(replsetOptions
    , ["bin", "host", "secondaries", "arbiters", "startPort", "tags"]);

  Object.defineProperty(this, 'secondaries', {
    enumerable:true, get: function() { return secondaryServers.slice(0); }
  });

  Object.defineProperty(this, 'passives', {
    enumerable:true, get: function() { return passiveServers.slice(0); }
  });

  Object.defineProperty(this, 'arbiters', {
    enumerable:true, get: function() { return arbiterServers.slice(0); }
  });

  Object.defineProperty(this, 'name', {
    enumerable:true, get: function() { return replSet; }
  });

  Object.defineProperty(this, 'startPort', {
    enumerable:true, get: function() { return startPort; }
  });

  Object.defineProperty(this, 'replicasetName', {
    enumerable:true, get: function() { return replSet; }
  });

  //
  // ensure replicaset is up and running
  var ensureUp = function(server, callback) {
    process.stdout.write(".");

    // Get the replicaset status
    server.command('admin.$cmd', {"replSetGetStatus": 1}, function(err, result) {
      if(err || result.result.ok == 0) return setTimeout(function() {
        ensureUp(server, callback);
      }, 1000);

      // The result
      var result = result.result;
      var ready = true;
      var hasPrimary = false;

      // Figure out if all the servers are ready
      result.members.forEach(function(m) {
        if([1, 2, 7].indexOf(m.state) == -1) ready = false;
      });

      if(ready) {
        result.members.forEach(function(m) {
          if(m.state == 1) hasPrimary = true;
        });
      }

      if(ready && hasPrimary) {
        // Get the ismaster
        server.destroy();
        return callback(null, result.result);
      }

      // Query the state of the replicaset again
      setTimeout(function() {
        ensureUp(server, callback);
      }, 1000);        
    });
  }

  //
  // Check all hosts to locate the primary
  var ensureIsMasterUp = function(configSet, callback) {
    var members = configSet.members.slice(0);
    var foundPrimary = false;

    // Query the server
    var queryServer = function(hosts, callback) {
      if(foundPrimary) {
        return callback(null, null);
      }

      if(hosts.length == 0 && !foundPrimary) {
        return setTimeout(function() {
          ensureIsMasterUp(configSet, callback);
        }, 1000);
      }

      var hostString = hosts.shift().host;
      var host = hostString.split(':')[0];
      var port = parseInt(hostString.split(':')[1], 10);
      var server = new Server({host: host, port: port, size: 1});

      // Process result
      var processResult = function(_s) {
        _s.destroy();
        if(_s.lastIsMaster().ismaster) foundPrimary = true;
        queryServer(hosts, callback);
      }

      var connectFailed = function() {
        server.destroy();
        queryServer(hosts, callback); 
      }<commit_msg>Fixed issued with restarting ssl based servers when using replicasets<commit_after>"use strict";

var f = require('util').format
  , path = require('path')
  , fs = require('fs')
  , mkdirp = require('mkdirp')
  , rimraf = require('rimraf')
  , exec = require('child_process').exec
  , ServerManager = require('./server_manager')
  , ReadPreference = require('../../lib/topologies/read_preference')
  , Server = require('../../lib/topologies/server')
  , ReplSet = require('../../lib/topologies/replset')

var Logger = require('../../lib/connection/logger')

//
// Clone the options
var cloneOptions = function(options) {
  var opts = {};
  for(var name in options) {
    opts[name] = options[name];
  }
  return opts;
}

//
// Remove any non-server specific settings
var filterInternalOptionsOut = function(options, internalOptions) {
  var opts = {};

  for(var name in options) {
    if(internalOptions.indexOf(name) == -1) {
      opts[name] = options[name];
    }
  }

  return opts;
}

var ReplSetManager = function(replsetOptions) {
  replsetOptions = replsetOptions || {};
  var startPort = replsetOptions.port = replsetOptions.startPort || 31000;

  // Get the settings
  var secondaries = replsetOptions.secondaries || 2;
  var arbiters = replsetOptions.arbiters || 0;
  var passives = replsetOptions.passives || 1;
  var replSet = replsetOptions.replSet = replsetOptions.replSet || 'rs';
  var version = 1;
  var configSet = null;
  var tags = replsetOptions.tags;
  var host = replsetOptions.host || 'localhost';

  // All server addresses
  var serverAddresses = [];
  var secondaryServers = [];
  var arbiterServers = [];
  var passiveServers = [];
  var primaryServer = [];;
  // DbPath
  var dbpath = replsetOptions.dbpath = replsetOptions.dbpath || path.resolve('data');

  // Get the keys
  var keys = Object.keys(replsetOptions);

  // Internal check server
  var server = null;

  // Clone the options
  replsetOptions = cloneOptions(replsetOptions);
  
  // Any needed credentials
  var credentials;

  // Contains all the server managers
  var serverManagers = [];

  // filtered out internal keys
  var internalOptions = filterInternalOptionsOut(replsetOptions
    , ["bin", "host", "secondaries", "arbiters", "startPort", "tags"]);

  Object.defineProperty(this, 'secondaries', {
    enumerable:true, get: function() { return secondaryServers.slice(0); }
  });

  Object.defineProperty(this, 'passives', {
    enumerable:true, get: function() { return passiveServers.slice(0); }
  });

  Object.defineProperty(this, 'arbiters', {
    enumerable:true, get: function() { return arbiterServers.slice(0); }
  });

  Object.defineProperty(this, 'name', {
    enumerable:true, get: function() { return replSet; }
  });

  Object.defineProperty(this, 'startPort', {
    enumerable:true, get: function() { return startPort; }
  });

  Object.defineProperty(this, 'replicasetName', {
    enumerable:true, get: function() { return replSet; }
  });

  //
  // ensure replicaset is up and running
  var ensureUp = function(server, callback) {
    process.stdout.write(".");

    // Get the replicaset status
    server.command('admin.$cmd', {"replSetGetStatus": 1}, function(err, result) {
      if(err || result.result.ok == 0) return setTimeout(function() {
        ensureUp(server, callback);
      }, 1000);

      // The result
      var result = result.result;
      var ready = true;
      var hasPrimary = false;

      // Figure out if all the servers are ready
      result.members.forEach(function(m) {
        if([1, 2, 7].indexOf(m.state) == -1) ready = false;
      });

      if(ready) {
        result.members.forEach(function(m) {
          if(m.state == 1) hasPrimary = true;
        });
      }

      if(ready && hasPrimary) {
        // Get the ismaster
        server.destroy();
        return callback(null, result.result);
      }

      // Query the state of the replicaset again
      setTimeout(function() {
        ensureUp(server, callback);
      }, 1000);        
    });
  }

  //
  // Check all hosts to locate the primary
  var ensureIsMasterUp = function(configSet, callback) {
    var members = configSet.members.slice(0);
    var foundPrimary = false;

    // Query the server
    var queryServer = function(hosts, callback) {
      if(foundPrimary) {
        return callback(null, null);
      }

      if(hosts.length == 0 && !foundPrimary) {
        return setTimeout(function() {
          ensureIsMasterUp(configSet, callback);
        }, 1000);
      }

      var hostString = hosts.shift().host;
      var host = hostString.split(':')[0];
      var port = parseInt(hostString.split(':')[1], 10);

      // Set the basic options
      var opts = {host: host, port: port, size: 1};

      // Set the key
      if(keys.indexOf('sslOnNormalPorts') != -1) opts.ssl = true;
      if(keys.indexOf('ssl') != -1) opts.ssl = replsetOptions.ssl;
      if(keys.indexOf('ca') != -1) opts.ca = replsetOptions.ca;
      if(keys.indexOf('cert') != -1) opts.cert = replsetOptions.cert;
      if(keys.indexOf('rejectUnauthorized') != -1) opts.rejectUnauthorized = replsetOptions.rejectUnauthorized;
      if(keys.indexOf('key') != -1) opts.key = replsetOptions.key;
      if(keys.indexOf('passphrase') != -1) opts.passphrase = replsetOptions.passphrase;

      // Create a server instance
      // var server = new Server({host: host, port: port, size: 1});
      var server = new Server(opts);

      // Process result
      var processResult = function(_s) {
        _s.destroy();
        if(_s.lastIsMaster().ismaster) foundPrimary = true;
        queryServer(hosts, callback);
      }

      var connectFailed = function() {
        server.destroy();
        queryServer(hosts, callback); 
      }
{'fraction_non_alphanumeric': 0.14271037638927805, 'fraction_numerical': 0.008032128514056224, 'mean_word_length': 2.9556704839305503, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3482308', 'n_tokens_mistral': 3324, 'n_tokens_neox': 3184, 'n_words': 782}