|
| 1 | +# TiKV UI |
| 2 | + |
| 3 | +A REST API server for exploring and managing TiKV key-value data with multi-cluster support. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +``` |
| 8 | +tikv-ui/ |
| 9 | +├── cmd/ |
| 10 | +│ └── tikv-ui/ |
| 11 | +│ └── main.go # Application entry point |
| 12 | +├── pkg/ |
| 13 | +│ ├── handlers/ # HTTP request handlers |
| 14 | +│ │ ├── health.go # Health check endpoint |
| 15 | +│ │ ├── cluster.go # Cluster management |
| 16 | +│ │ ├── get.go # GET operation |
| 17 | +│ │ ├── put.go # PUT operation |
| 18 | +│ │ ├── delete.go # DELETE operation |
| 19 | +│ │ └── scan.go # SCAN operation |
| 20 | +│ ├── server/ # Server setup and middleware |
| 21 | +│ │ ├── server.go # Server struct with multi-cluster support |
| 22 | +│ │ └── middleware.go # HTTP middleware (logging, etc.) |
| 23 | +│ ├── types/ # Type definitions |
| 24 | +│ │ ├── requests.go # API request types |
| 25 | +│ │ └── responses.go # API response types |
| 26 | +│ └── utils/ # Utility functions |
| 27 | +│ ├── http.go # HTTP helpers (JSON responses, errors) |
| 28 | +│ ├── msgpack.go # Msgpack/JSON parsing |
| 29 | +│ └── string.go # String manipulation utilities |
| 30 | +├── go.mod |
| 31 | +├── go.sum |
| 32 | +└── README.md |
| 33 | +``` |
| 34 | + |
| 35 | +## Building |
| 36 | + |
| 37 | +```bash |
| 38 | +go build -o bin/tikv-ui ./cmd/tikv-ui |
| 39 | +``` |
| 40 | + |
| 41 | +## Running |
| 42 | + |
| 43 | +Set the `TIKV_PD_ADDRS` environment variable with comma-separated PD addresses for the default cluster: |
| 44 | + |
| 45 | +```bash |
| 46 | +export TIKV_PD_ADDRS="127.0.0.1:2379,127.0.0.1:23790" |
| 47 | +./bin/tikv-ui |
| 48 | +``` |
| 49 | + |
| 50 | +The server will start on port 8081 and connect to the default cluster. |
| 51 | + |
| 52 | +## API Endpoints |
| 53 | + |
| 54 | +### Health Check |
| 55 | +``` |
| 56 | +GET /health |
| 57 | +``` |
| 58 | + |
| 59 | +### Cluster Management |
| 60 | + |
| 61 | +#### Connect to New Cluster |
| 62 | +``` |
| 63 | +POST /api/clusters/connect |
| 64 | +Body: { |
| 65 | + "pd_addrs": ["127.0.0.1:2379", "127.0.0.1:23790"], |
| 66 | + "name": "production" // optional, auto-generated if not provided |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +#### List All Clusters |
| 71 | +``` |
| 72 | +GET /api/clusters |
| 73 | +Response: { |
| 74 | + "clusters": [ |
| 75 | + { |
| 76 | + "name": "default", |
| 77 | + "cluster_id": 123456, |
| 78 | + "pd_addrs": ["127.0.0.1:2379"], |
| 79 | + "active": true |
| 80 | + } |
| 81 | + ] |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +#### Switch Active Cluster |
| 86 | +``` |
| 87 | +POST /api/clusters/switch |
| 88 | +Body: {"name": "production"} |
| 89 | +``` |
| 90 | + |
| 91 | +### Raw KV Operations |
| 92 | + |
| 93 | +All operations use the currently active cluster. |
| 94 | + |
| 95 | +#### Get Value |
| 96 | +``` |
| 97 | +POST /api/raw/get |
| 98 | +Body: {"key": "mykey"} |
| 99 | +Response: { |
| 100 | + "key": "mykey", |
| 101 | + "value": {...}, // parsed msgpack/JSON |
| 102 | + "raw_value": "...", // raw string |
| 103 | + "found": true |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +#### Put Value |
| 108 | +``` |
| 109 | +POST /api/raw/put |
| 110 | +Body: {"key": "mykey", "value": "myvalue"} |
| 111 | +``` |
| 112 | + |
| 113 | +#### Delete Key |
| 114 | +``` |
| 115 | +POST /api/raw/delete |
| 116 | +Body: {"key": "mykey"} |
| 117 | +``` |
| 118 | + |
| 119 | +#### Scan Range |
| 120 | +``` |
| 121 | +POST /api/raw/scan |
| 122 | +Body: {"start_key": "a", "end_key": "z", "limit": 100} |
| 123 | +Response: { |
| 124 | + "items": [ |
| 125 | + { |
| 126 | + "key": "key1", |
| 127 | + "value": {...}, // parsed msgpack/JSON |
| 128 | + "raw_value": "..." // raw string |
| 129 | + } |
| 130 | + ] |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Features |
| 135 | + |
| 136 | +### Multi-Cluster Support |
| 137 | +- Connect to multiple TiKV clusters dynamically via API |
| 138 | +- Switch between clusters without restarting the server |
| 139 | +- Each cluster maintains its own connection pool |
| 140 | +- Thread-safe cluster management |
| 141 | + |
| 142 | +### Smart Value Parsing |
| 143 | +- Automatically detects and parses msgpack-encoded values |
| 144 | +- Falls back to JSON parsing if msgpack fails |
| 145 | +- Returns both parsed (structured) and raw (string) values |
| 146 | +- Handles plain text values correctly |
| 147 | + |
| 148 | +### Graceful Shutdown |
| 149 | +- Properly closes all cluster connections on shutdown |
| 150 | +- Handles SIGINT and SIGTERM signals |
| 151 | + |
| 152 | +## Architecture |
| 153 | + |
| 154 | +The project follows a clean architecture pattern: |
| 155 | + |
| 156 | +- **cmd/**: Application entry points (main packages) |
| 157 | +- **pkg/handlers/**: HTTP handlers organized by operation |
| 158 | +- **pkg/server/**: Server configuration, middleware, and multi-cluster management |
| 159 | +- **pkg/types/**: Shared type definitions |
| 160 | +- **pkg/utils/**: Reusable utility functions |
| 161 | + |
| 162 | +This structure provides: |
| 163 | +- Clear separation of concerns |
| 164 | +- Easy testing (each handler can be tested independently) |
| 165 | +- Maintainability (changes are localized to specific packages) |
| 166 | +- Scalability (easy to add new handlers or utilities) |
0 commit comments