Beispiel #1
0
def initial_reparent(keyspace, master_cell, num_shards, namespace):
  """Performs the first reparent."""
  successfully_reparented = []
  master_tablets = {}
  while len(master_tablets) < num_shards:
    for shard_name in sharding_utils.get_shard_names(num_shards):
      shard_name = sandbox_utils.fix_shard_name(shard_name)
      tablets = vtctl_sandbox.execute_vtctl_command(
          ['ListShardTablets', '%s/%s' % (
              keyspace, sandbox_utils.fix_shard_name(shard_name))],
          namespace=namespace)[0].split('\n')
      tablets = [x.split(' ') for x in tablets if x]
      potential_masters = [
          x[0] for x in tablets if x[3] == 'replica'
          and x[0].split('-')[0] == master_cell]
      if potential_masters:
        master_tablets[shard_name] = potential_masters[0]

  while len(successfully_reparented) < num_shards:
    for shard_name in sharding_utils.get_shard_names(num_shards):
      shard_name = sandbox_utils.fix_shard_name(shard_name)
      master_tablet_id = master_tablets[shard_name]
      if is_master(master_tablet_id, namespace):
        logging.info('Tablet %s is the master of %s/%s.',
                     master_tablet_id, keyspace, shard_name)
        successfully_reparented.append(shard_name)
      if shard_name in successfully_reparented:
        continue
      logging.info('Setting tablet %s as master for %s/%s.',
                   master_tablet_id, keyspace, shard_name)
      vtctl_sandbox.execute_vtctl_command(
          ['InitShardMaster', '-force', '%s/%s' % (keyspace, shard_name),
           master_tablet_id], namespace=namespace, timeout_s=5)
  logging.info('Done with initial reparent.')
Beispiel #2
0
def initial_reparent(keyspace, master_cell, num_shards, namespace):
    """Performs the first reparent."""
    successfully_reparented = []
    master_tablets = {}
    while len(master_tablets) < num_shards:
        for shard_name in sharding_utils.get_shard_names(num_shards):
            shard_name = sandbox_utils.fix_shard_name(shard_name)
            tablets = vtctl_sandbox.execute_vtctl_command(
                [
                    'ListShardTablets',
                    '%s/%s' %
                    (keyspace, sandbox_utils.fix_shard_name(shard_name))
                ],
                namespace=namespace)[0].split('\n')
            tablets = [x.split(' ') for x in tablets if x]
            potential_masters = [
                x[0] for x in tablets
                if x[3] == 'replica' and x[0].split('-')[0] == master_cell
            ]
            if potential_masters:
                master_tablets[shard_name] = potential_masters[0]

    while len(successfully_reparented) < num_shards:
        for shard_name in sharding_utils.get_shard_names(num_shards):
            shard_name = sandbox_utils.fix_shard_name(shard_name)
            master_tablet_id = master_tablets[shard_name]
            if is_master(master_tablet_id, namespace):
                logging.info('Tablet %s is the master of %s/%s.',
                             master_tablet_id, keyspace, shard_name)
                successfully_reparented.append(shard_name)
            if shard_name in successfully_reparented:
                continue
            logging.info('Setting tablet %s as master for %s/%s.',
                         master_tablet_id, keyspace, shard_name)
            vtctl_sandbox.execute_vtctl_command([
                'InitShardMaster', '-force',
                '%s/%s' % (keyspace, shard_name), master_tablet_id
            ],
                                                namespace=namespace,
                                                timeout_s=5)
    logging.info('Done with initial reparent.')
    def _generate_helm_keyspaces(self):
        """Create helm keyspace configurations.

    These configurations include entries for keyspaces, shards, and tablets.
    Note that the logic for calculating UIDs will go away once Kubernetes 1.5
    is widely available. Then tablets will use replication controllers with
    StatefulSet, and UIDs will be calculated automatically.

    Returns:
      Configuration for keyspaces in the form of a list of dicts.
    """
        starting_cell_index = 0
        if len(self.app_options.cells) > 1:
            starting_cell_index = self.cell_epsilon
        keyspaces = []
        for ks_index, ks in enumerate(self.app_options.keyspaces):
            keyspace = dict(name=ks['name'], shards=[])
            keyspaces.append(keyspace)

            for shard_index, shard_name in enumerate(
                    sharding_utils.get_shard_names(ks['shard_count'])):
                shard_name = sandbox_utils.fix_shard_name(shard_name)
                shard = dict(
                    name=shard_name,
                    tablets=[
                        dict(
                            type='replica',
                            vttablet=dict(replicas=ks['replica_count'], ),
                        )
                    ],
                )
                uid_base = ((100 + shard_index * self.shard_epsilon) +
                            starting_cell_index +
                            (ks_index * self.keyspace_epsilon))
                shard['tablets'][0]['uidBase'] = uid_base
                if ks['rdonly_count']:
                    shard['tablets'].append(
                        dict(type='rdonly',
                             uidBase=uid_base + ks['replica_count'],
                             vttablet=dict(replicas=ks['rdonly_count'], )))
                keyspace['shards'].append(shard)
        return keyspaces
  def _generate_helm_keyspaces(self):
    """Create helm keyspace configurations.

    These configurations include entries for keyspaces, shards, and tablets.
    Note that the logic for calculating UIDs will go away once Kubernetes 1.5
    is widely available. Then tablets will use replication controllers with
    StatefulSet, and UIDs will be calculated automatically.

    Returns:
      Configuration for keyspaces in the form of a list of dicts.
    """
    starting_cell_index = 0
    if len(self.app_options.cells) > 1:
      starting_cell_index = self.cell_epsilon
    keyspaces = []
    for ks_index, ks in enumerate(self.app_options.keyspaces):
      keyspace = dict(name=ks['name'], shards=[])
      keyspaces.append(keyspace)

      for shard_index, shard_name in enumerate(
          sharding_utils.get_shard_names(ks['shard_count'])):
        shard_name = sandbox_utils.fix_shard_name(shard_name)
        shard = dict(
            name=shard_name,
            tablets=[dict(
                type='replica',
                vttablet=dict(
                    replicas=ks['replica_count'],
                ),
            )],
        )
        uid_base = (
            (100 + shard_index * self.shard_epsilon) + starting_cell_index + (
                ks_index * self.keyspace_epsilon))
        shard['tablets'][0]['uidBase'] = uid_base
        if ks['rdonly_count']:
          shard['tablets'].append(dict(
              type='rdonly',
              uidBase=uid_base + ks['replica_count'],
              replicas=ks['rdonly_count']))
        keyspace['shards'].append(shard)
    return keyspaces
Beispiel #5
0
 def test_fix_shard_name(self):
     self.assertEquals(sandbox_utils.fix_shard_name('-80'), 'x80')
     self.assertEquals(sandbox_utils.fix_shard_name('80-'), '80x')
     self.assertEquals(sandbox_utils.fix_shard_name('40-80'), '40-80')
Beispiel #6
0
 def test_fix_shard_name(self):
   self.assertEquals(sandbox_utils.fix_shard_name('-80'), 'x80')
   self.assertEquals(sandbox_utils.fix_shard_name('80-'), '80x')
   self.assertEquals(sandbox_utils.fix_shard_name('40-80'), '40-80')