|
| 1 | +# TiDB Test Optimization: Switch to TiDB Playground |
| 2 | + |
| 3 | +## Current Problem |
| 4 | + |
| 5 | +TiDB tests are slow because they: |
| 6 | +1. **Pull 3 large Docker images** (PD, TiKV, TiDB) - ~500MB-1GB total |
| 7 | +2. **Start 3 containers sequentially** (PD → TiKV → TiDB) |
| 8 | +3. **Wait for cluster coordination** - PD and TiKV need to sync before TiDB can start |
| 9 | +4. **No image caching** - Images are pulled fresh each time |
| 10 | + |
| 11 | +**Current timing**: TiDB tests take 5-10+ minutes vs 1-2 minutes for regular MySQL tests |
| 12 | + |
| 13 | +## Solution: Use TiDB Playground |
| 14 | + |
| 15 | +TiDB Playground is TiDB's official tool for quickly spinning up local TiDB clusters. It's optimized for testing and development. |
| 16 | + |
| 17 | +### Benefits: |
| 18 | +- **Single command** to start entire cluster |
| 19 | +- **Pre-configured** - no manual coordination needed |
| 20 | +- **Faster startup** - optimized for local use |
| 21 | +- **Single Docker image** option (all-in-one) or binary |
| 22 | +- **Better caching** - can use pre-pulled images |
| 23 | + |
| 24 | +## Implementation Options |
| 25 | + |
| 26 | +### Option 1: TiUP Playground (Recommended) |
| 27 | + |
| 28 | +TiUP is TiDB's package manager and includes `tiup playground` command. |
| 29 | + |
| 30 | +**Pros:** |
| 31 | +- Official TiDB tool |
| 32 | +- Supports all TiDB versions |
| 33 | +- Fast startup (~30-60 seconds) |
| 34 | +- Can specify exact version |
| 35 | +- Single command |
| 36 | + |
| 37 | +**Cons:** |
| 38 | +- Requires installing TiUP first |
| 39 | +- Binary download (~10-20MB) |
| 40 | + |
| 41 | +**Installation:** |
| 42 | +```bash |
| 43 | +curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh |
| 44 | +``` |
| 45 | + |
| 46 | +**Usage:** |
| 47 | +```bash |
| 48 | +tiup playground v7.5.2 --db 1 --kv 1 --pd 1 --tiflash 0 --without-monitor |
| 49 | +``` |
| 50 | + |
| 51 | +### Option 2: Docker Compose with Pre-pulled Images |
| 52 | + |
| 53 | +Use docker-compose with image caching. |
| 54 | + |
| 55 | +**Pros:** |
| 56 | +- Uses existing Docker infrastructure |
| 57 | +- Can cache images between runs |
| 58 | +- More control |
| 59 | + |
| 60 | +**Cons:** |
| 61 | +- Still slower than playground |
| 62 | +- More complex setup |
| 63 | + |
| 64 | +### Option 3: TiDB Playground Docker Image |
| 65 | + |
| 66 | +Use the official `pingcap/tidb-playground` Docker image. |
| 67 | + |
| 68 | +**Pros:** |
| 69 | +- Single Docker image |
| 70 | +- Pre-configured |
| 71 | +- Fast startup |
| 72 | + |
| 73 | +**Cons:** |
| 74 | +- May not support all versions |
| 75 | +- Less control over components |
| 76 | + |
| 77 | +## Recommended Implementation: TiUP Playground |
| 78 | + |
| 79 | +### Step 1: Install TiUP in GitHub Actions |
| 80 | + |
| 81 | +Add to workflow: |
| 82 | +```yaml |
| 83 | +- name: Install TiUP |
| 84 | + run: | |
| 85 | + curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh |
| 86 | + export PATH=$HOME/.tiup/bin:$PATH |
| 87 | + tiup --version |
| 88 | +``` |
| 89 | +
|
| 90 | +### Step 2: Update Makefile |
| 91 | +
|
| 92 | +Replace the current `testtidb` target: |
| 93 | + |
| 94 | +```makefile |
| 95 | +testtidb: |
| 96 | + @export PATH=$$HOME/.tiup/bin:$$PATH && \ |
| 97 | + tiup playground $(MYSQL_VERSION) --db 1 --kv 1 --pd 1 --tiflash 0 --without-monitor --host 0.0.0.0 --db.port $(MYSQL_PORT) & \ |
| 98 | + PLAYGROUND_PID=$$! && \ |
| 99 | + echo "Waiting for TiDB..." && \ |
| 100 | + while ! mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -e 'SELECT 1' >/dev/null 2>&1; do \ |
| 101 | + printf '.'; sleep 1; \ |
| 102 | + done; \ |
| 103 | + echo "Connected!" && \ |
| 104 | + MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc; \ |
| 105 | + TEST_RESULT=$$?; \ |
| 106 | + kill $$PLAYGROUND_PID || true; \ |
| 107 | + exit $$TEST_RESULT |
| 108 | +``` |
| 109 | + |
| 110 | +### Step 3: Alternative - Simpler Script Approach |
| 111 | + |
| 112 | +Create a new script `scripts/tidb-playground.sh`: |
| 113 | + |
| 114 | +```bash |
| 115 | +#!/usr/bin/env bash |
| 116 | +set -e |
| 117 | +
|
| 118 | +VERSION=${1:-7.5.2} |
| 119 | +PORT=${2:-4000} |
| 120 | +MODE=${3:-start} # start or stop |
| 121 | +
|
| 122 | +export PATH=$HOME/.tiup/bin:$PATH |
| 123 | +
|
| 124 | +if [ "$MODE" = "start" ]; then |
| 125 | + echo "Starting TiDB Playground v${VERSION} on port ${PORT}..." |
| 126 | + tiup playground ${VERSION} \ |
| 127 | + --db 1 \ |
| 128 | + --kv 1 \ |
| 129 | + --pd 1 \ |
| 130 | + --tiflash 0 \ |
| 131 | + --without-monitor \ |
| 132 | + --host 0.0.0.0 \ |
| 133 | + --db.port ${PORT} \ |
| 134 | + --db.config "" \ |
| 135 | + & |
| 136 | + PLAYGROUND_PID=$! |
| 137 | + echo $PLAYGROUND_PID > /tmp/tidb-playground-${PORT}.pid |
| 138 | + |
| 139 | + # Wait for TiDB to be ready |
| 140 | + echo "Waiting for TiDB..." |
| 141 | + for i in {1..60}; do |
| 142 | + if mysql -h 127.0.0.1 -P ${PORT} -u root -e 'SELECT 1' >/dev/null 2>&1; then |
| 143 | + echo "TiDB is ready!" |
| 144 | + exit 0 |
| 145 | + fi |
| 146 | + sleep 1 |
| 147 | + done |
| 148 | + echo "TiDB failed to start" |
| 149 | + exit 1 |
| 150 | +elif [ "$MODE" = "stop" ]; then |
| 151 | + if [ -f /tmp/tidb-playground-${PORT}.pid ]; then |
| 152 | + PID=$(cat /tmp/tidb-playground-${PORT}.pid) |
| 153 | + kill $PID 2>/dev/null || true |
| 154 | + rm /tmp/tidb-playground-${PORT}.pid |
| 155 | + fi |
| 156 | + # Also kill any remaining tiup processes |
| 157 | + pkill -f "tiup playground" || true |
| 158 | +fi |
| 159 | +``` |
| 160 | + |
| 161 | +Then update Makefile: |
| 162 | +```makefile |
| 163 | +testtidb: |
| 164 | + @export PATH=$$HOME/.tiup/bin:$$PATH && \ |
| 165 | + $(CURDIR)/scripts/tidb-playground.sh $(MYSQL_VERSION) $(MYSQL_PORT) start && \ |
| 166 | + MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc; \ |
| 167 | + TEST_RESULT=$$?; \ |
| 168 | + $(CURDIR)/scripts/tidb-playground.sh $(MYSQL_VERSION) $(MYSQL_PORT) stop; \ |
| 169 | + exit $$TEST_RESULT |
| 170 | +``` |
| 171 | + |
| 172 | +## Expected Performance Improvement |
| 173 | + |
| 174 | +**Current**: 5-10+ minutes per TiDB test |
| 175 | +- Docker image pulls: 2-5 minutes |
| 176 | +- Container startup: 1-2 minutes |
| 177 | +- Cluster coordination: 1-2 minutes |
| 178 | +- Test execution: 1-2 minutes |
| 179 | + |
| 180 | +**With TiUP Playground**: 2-3 minutes per TiDB test |
| 181 | +- TiUP install (cached): ~5 seconds |
| 182 | +- Playground startup: 30-60 seconds |
| 183 | +- Test execution: 1-2 minutes |
| 184 | + |
| 185 | +**Savings**: ~3-7 minutes per TiDB test × 6 TiDB tests = **18-42 minutes** per workflow run! |
| 186 | + |
| 187 | +## Implementation Checklist |
| 188 | + |
| 189 | +- [ ] Install TiUP in GitHub Actions workflow |
| 190 | +- [ ] Create tidb-playground.sh script (or update existing) |
| 191 | +- [ ] Update Makefile testtidb target |
| 192 | +- [ ] Test locally with TiUP |
| 193 | +- [ ] Test in GitHub Actions |
| 194 | +- [ ] Remove old tidb-test-cluster.sh script (or keep as fallback) |
| 195 | + |
| 196 | +## References |
| 197 | + |
| 198 | +- TiUP Documentation: https://docs.pingcap.com/tidb/stable/tiup-overview |
| 199 | +- TiUP Playground: https://docs.pingcap.com/tidb/stable/tiup-playground |
| 200 | +- TiUP Installation: https://docs.pingcap.com/tidb/stable/tiup-overview#install-tiup |
0 commit comments