vitess.io/vitess@v0.16.2/examples/compose/vttablet-up.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2020 The Vitess Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -u
    18  export VTROOT=/vt
    19  export VTDATAROOT=/vt/vtdataroot
    20  
    21  keyspace=${KEYSPACE:-'test_keyspace'}
    22  shard=${SHARD:-'0'}
    23  grpc_port=${GRPC_PORT:-'15999'}
    24  web_port=${WEB_PORT:-'8080'}
    25  role=${ROLE:-'replica'}
    26  vthost=${VTHOST:-`hostname -i`}
    27  sleeptime=${SLEEPTIME:-'0'}
    28  uid=$1
    29  external=${EXTERNAL_DB:-0}
    30  
    31  # If DB is not explicitly set, we default to behaviour of prefixing with vt_
    32  # If there is an external db, the db_nmae will always match the keyspace name
    33  [ $external = 0 ] && db_name=${DB:-"vt_$keyspace"} ||  db_name=${DB:-"$keyspace"}
    34  db_charset=${DB_CHARSET:-''}
    35  tablet_hostname=''
    36  
    37  # Use IPs to simplify connections when testing in docker.
    38  # Otherwise, blank hostname means the tablet auto-detects FQDN.
    39  # This is now set further up
    40  
    41  printf -v alias '%s-%010d' $CELL $uid
    42  printf -v tablet_dir 'vt_%010d' $uid
    43  
    44  tablet_role=$role
    45  tablet_type='replica'
    46  
    47  # Make every 3rd tablet rdonly
    48  if (( $uid % 100 % 3 == 0 )) ; then
    49      tablet_type='rdonly'
    50  fi
    51  
    52  # Consider every tablet with %d00 as external primary
    53  if [ $external = 1 ] && (( $uid % 100 == 0 )) ; then
    54      tablet_type='replica'
    55      tablet_role='externalprimary'
    56      keyspace="ext_$keyspace"
    57  fi
    58  
    59  # Copy config directory
    60  cp -R /script/config $VTROOT
    61  init_db_sql_file="$VTROOT/config/init_db.sql"
    62  # Clear in-place edits of init_db_sql_file if any exist
    63  sed -i '/##\[CUSTOM_SQL/{:a;N;/END\]##/!ba};//d' $init_db_sql_file
    64  
    65  echo "##[CUSTOM_SQL_START]##" >> $init_db_sql_file
    66  
    67  if [ "$external" = "1" ]; then
    68    # We need a common user for the unmanaged and managed tablets else tools like orchestrator will not function correctly
    69    echo "Creating matching user for managed tablets..."
    70    echo "CREATE USER IF NOT EXISTS '$DB_USER'@'%' IDENTIFIED BY '$DB_PASS';" >> $init_db_sql_file
    71    echo "GRANT ALL ON *.* TO '$DB_USER'@'%';FLUSH PRIVILEGES;" >> $init_db_sql_file
    72  fi
    73  echo "##[CUSTOM_SQL_END]##" >> $init_db_sql_file
    74  
    75  echo "##[CUSTOM_SQL_END]##" >> $init_db_sql_file
    76  
    77  mkdir -p $VTDATAROOT/backups
    78  
    79  
    80  export KEYSPACE=$keyspace
    81  export SHARD=$shard
    82  export TABLET_ID=$alias
    83  export TABLET_DIR=$tablet_dir
    84  export MYSQL_PORT=3306
    85  export TABLET_ROLE=$tablet_role
    86  export DB_PORT=${DB_PORT:-3306}
    87  export DB_HOST=${DB_HOST:-""}
    88  export DB_NAME=$db_name
    89  
    90  # Delete socket files before running mysqlctld if exists.
    91  # This is the primary reason for unhealthy state on restart.
    92  # https://github.com/vitessio/vitess/pull/5115/files
    93  echo "Removing $VTDATAROOT/$tablet_dir/{mysql.sock,mysql.sock.lock}..."
    94  rm -rf $VTDATAROOT/$tablet_dir/{mysql.sock,mysql.sock.lock}
    95  
    96  # Create mysql instances
    97  # Do not create mysql instance for primary if connecting to external mysql database
    98  if [[ $tablet_role != "externalprimary" ]]; then
    99    echo "Initing mysql for tablet: $uid role: $role external: $external.. "
   100    $VTROOT/bin/mysqlctld \
   101    --init_db_sql_file=$init_db_sql_file \
   102    --logtostderr=true \
   103    --tablet_uid=$uid \
   104    &
   105  fi
   106  
   107  sleep $sleeptime
   108  
   109  # Create the cell
   110  # https://vitess.io/blog/2020-04-27-life-of-a-cluster/
   111  $VTROOT/bin/vtctlclient --server vtctld:$GRPC_PORT -- AddCellInfo --root vitess/$CELL --server_address consul1:8500 $CELL || true
   112  
   113  #Populate external db conditional args
   114  if [ $tablet_role = "externalprimary" ]; then
   115      echo "Setting external db args for primary: $DB_NAME"
   116      external_db_args="--db_host $DB_HOST \
   117                        --db_port $DB_PORT \
   118                        --init_db_name_override $DB_NAME \
   119                        --init_tablet_type $tablet_type \
   120                        --mycnf_server_id $uid \
   121                        --db_app_user $DB_USER \
   122                        --db_app_password $DB_PASS \
   123                        --db_allprivs_user $DB_USER \
   124                        --db_allprivs_password $DB_PASS \
   125                        --db_appdebug_user $DB_USER \
   126                        --db_appdebug_password $DB_PASS \
   127                        --db_dba_user $DB_USER \
   128                        --db_dba_password $DB_PASS \
   129                        --db_filtered_user $DB_USER \
   130                        --db_filtered_password $DB_PASS \
   131                        --db_repl_user $DB_USER \
   132                        --db_repl_password $DB_PASS \
   133                        --enable_replication_reporter=false \
   134                        --enforce_strict_trans_tables=false \
   135                        --track_schema_versions=true \
   136                        --vreplication_tablet_type=primary \
   137                        --watch_replication_stream=true"
   138  else
   139      external_db_args="--init_db_name_override $DB_NAME \
   140                        --init_tablet_type $tablet_type \
   141                        --enable_replication_reporter=true \
   142                        --restore_from_backup"
   143  fi
   144  
   145  
   146  echo "Starting vttablet..."
   147  exec $VTROOT/bin/vttablet \
   148    $TOPOLOGY_FLAGS \
   149    --logtostderr=true \
   150    --tablet-path $alias \
   151    --tablet_hostname "$vthost" \
   152    --health_check_interval 5s \
   153    --disable_active_reparents=true \
   154    --port $web_port \
   155    --grpc_port $grpc_port \
   156    --service_map 'grpc-queryservice,grpc-tabletmanager,grpc-updatestream' \
   157    --vtctld_addr "http://vtctld:$WEB_PORT/" \
   158    --init_keyspace $keyspace \
   159    --init_shard $shard \
   160    --backup_storage_implementation file \
   161    --file_backup_storage_root $VTDATAROOT/backups \
   162    --queryserver-config-schema-reload-time 60 \
   163    $external_db_args