Skip to content

Commit

Permalink
Test wsl2 redis cluster connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhima-Mochi committed Jan 23, 2024
1 parent cc52f9f commit edddd95
Show file tree
Hide file tree
Showing 19 changed files with 176 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ README.md
docker/
tests/
docs/design/
init.sh
Dockerfile
docker-compose*
5 changes: 1 addition & 4 deletions docker/config.linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ redis:
- 172.17.0.1:7000
- 172.17.0.1:7001
- 172.17.0.1:7002
- 172.17.0.1:7003
- 172.17.0.1:7004
- 172.17.0.1:7005
password: ""
password: password
db: 0
5 changes: 1 addition & 4 deletions docker/config.windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ redis:
- host.docker.internal:7000
- host.docker.internal:7001
- host.docker.internal:7002
- host.docker.internal:7003
- host.docker.internal:7004
- host.docker.internal:7005
password: ""
password: password
db: 0
2 changes: 2 additions & 0 deletions docker/mongodb/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ sleep 10

# Execute the final initialization script
docker-compose exec router01 sh -c "mongosh -u root --authenticationDatabase admin -p password < /scripts/mongo-init.js"

echo "MongoDB Sharded Cluster is ready!"
4 changes: 2 additions & 2 deletions docker/mongodb/scripts/mongo-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
sh.enableSharding("linkZapURL")

// Setup shardingKey for collection `linkZapURL.url`
db.adminCommand({ shardCollection: "linkZapURL.url", key: { seq: 1 } })
db.adminCommand({ shardCollection: "linkZapURL.url", key: { shardID: 1 } })

db = db.getSiblingDB("linkZapURL");

db.url.createIndex({ seq: 1, ID: 1 }, { unique: true });
db.url.createIndex({ shardID: 1, ID: 1 }, { unique: true });


77 changes: 77 additions & 0 deletions docker/redis/docker-compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
version: '3.9'

services:

redis-node-1:
image: redis:7.2.4
container_name: redis-node-1
links:
- redis-node-2
- redis-node-3
- redis-node-4
- redis-node-5
- redis-node-6
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf --requirepass password --masterauth password --protected-mode no
restart: always
volumes:
- redis-node-1-data:/data
ports:
- "7000:6379"

redis-node-2:
image: redis:7.2.4
container_name: redis-node-2
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf --requirepass password --masterauth password --protected-mode no
restart: always
volumes:
- redis-node-2-data:/data
ports:
- "7001:6379"

redis-node-3:
image: redis:7.2.4
container_name: redis-node-3
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf --requirepass password --masterauth password --protected-mode no
restart: always
volumes:
- redis-node-3-data:/data
ports:
- "7002:6379"

redis-node-4:
image: redis:7.2.4
container_name: redis-node-4
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf --requirepass password --masterauth password --protected-mode no
restart: always
volumes:
- redis-node-4-data:/data
ports:
- "7003:6379"

redis-node-5:
image: redis:7.2.4
container_name: redis-node-5
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf --requirepass password --masterauth password --protected-mode no
restart: always
volumes:
- redis-node-5-data:/data
ports:
- "7004:6379"

redis-node-6:
image: redis:7.2.4
container_name: redis-node-6
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf --requirepass password --masterauth password --protected-mode no
restart: always
volumes:
- redis-node-6-data:/data
ports:
- "7005:6379"

volumes:
redis-node-1-data:
redis-node-2-data:
redis-node-3-data:
redis-node-4-data:
redis-node-5-data:
redis-node-6-data:
67 changes: 3 additions & 64 deletions docker/redis/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,12 @@ services:
redis-node-1:
image: redis:7.2.4
container_name: redis-node-1
links:
- redis-node-2
- redis-node-3
- redis-node-4
- redis-node-5
- redis-node-6
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf
command: redis-server --port 6379 --bind 0.0.0.0 --requirepass password --masterauth password --protected-mode no
restart: always
volumes:
- redis-cluster-node-1-data:/data
- redis-node-1-data:/data
ports:
- "7000:6379"

redis-node-2:
image: redis:7.2.4
container_name: redis-node-2
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf
restart: always
volumes:
- redis-cluster-node-2-data:/data
ports:
- "7001:6379"

redis-node-3:
image: redis:7.2.4
container_name: redis-node-3
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf
restart: always
volumes:
- redis-cluster-node-3-data:/data
ports:
- "7002:6379"

redis-node-4:
image: redis:7.2.4
container_name: redis-node-4
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf
restart: always
volumes:
- redis-cluster-node-4-data:/data
ports:
- "7003:6379"

redis-node-5:
image: redis:7.2.4
container_name: redis-node-5
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf
restart: always
volumes:
- redis-cluster-node-5-data:/data
ports:
- "7004:6379"

redis-node-6:
image: redis:7.2.4
container_name: redis-node-6
command: redis-server --port 6379 --bind 0.0.0.0 --cluster-enabled yes --cluster-node-timeout 5000 --cluster-config-file nodes.conf
restart: always
volumes:
- redis-cluster-node-6-data:/data
ports:
- "7005:6379"

volumes:
redis-cluster-node-1-data:
redis-cluster-node-2-data:
redis-cluster-node-3-data:
redis-cluster-node-4-data:
redis-cluster-node-5-data:
redis-cluster-node-6-data:
redis-node-1-data:
9 changes: 2 additions & 7 deletions docker/redis/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ docker-compose build
docker-compose down -v

# Start all services
docker-compose up -d --remove-orphans
docker-compose up -d


# Wait for the services to start.
echo "Waiting for services to start..."
sleep 3

docker-compose exec redis-node-1 sh -c "echo 'yes' | redis-cli --cluster create redis-node-1:6379 redis-node-2:6379 redis-node-3:6379 redis-node-4:6379 redis-node-5:6379 redis-node-6:6379 --cluster-replicas 1"
echo "Redis Cluster is ready!"
6 changes: 6 additions & 0 deletions docker/redis/redis/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly no
protected-mode no
bind 0.0.0.0
2 changes: 1 addition & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const docTemplate = `{
"properties": {
"expireAt": {
"type": "string",
"example": "2021-02-08T09:20:41Z"
"example": "2025-02-08T09:20:41Z"
},
"url": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"properties": {
"expireAt": {
"type": "string",
"example": "2021-02-08T09:20:41Z"
"example": "2025-02-08T09:20:41Z"
},
"url": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ definitions:
main.ShortenRequest:
properties:
expireAt:
example: "2021-02-08T09:20:41Z"
example: "2025-02-08T09:20:41Z"
type: string
url:
example: https://example.com
Expand Down
2 changes: 1 addition & 1 deletion handlers/redirection/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (im *impl) Redirect(ctx context.Context, code string) (*models.URL, error)
Code: code,
}

err := u.ToBSON()
err := u.ToModel()
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions init.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/bin/bash

# Stop and remove containers
docker-compose -f docker-compose.windows.yml down -v

# Build Docker images
docker-compose -f docker-compose.windows.yml build

# Start all services
docker-compose -f docker-compose.windows.yml up -d
docker-compose -f docker-compose.windows.yml up
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func main() {

type ShortenRequest struct {

Check failure on line 112 in main.go

View workflow job for this annotation

GitHub Actions / Build

exported type ShortenRequest should have comment or be unexported
URL string `json:"url" binding:"required" example:"https://example.com"`
ExpireAt string `json:"expireAt" binding:"required" example:"2021-02-08T09:20:41Z"`
ExpireAt string `json:"expireAt" binding:"required" example:"2025-02-08T09:20:41Z"`
ExpireAtInt64 int64 `json:"-" binding:"-"`
}

Expand Down Expand Up @@ -172,7 +172,7 @@ func (h *Handler) Shorten(g *gin.Context) {
// @Description Redirects to the original URL.
// @Tags URL Redirection
// @Param code path string true "Shortened URL Code"
// @Success 301 string string "Moved Permanently"
// @Success 301 {string} string "Moved Permanently"
// @Failure 404 {object} map[string]any
// @Failure 500 {object} map[string]any
// @Router /{code} [get]
Expand All @@ -190,5 +190,7 @@ func (h *Handler) Redirect(g *gin.Context) {
return
}

log.Printf("Redirecting to %s", url.URL)

g.Redirect(http.StatusMovedPermanently, url.URL)
}
10 changes: 5 additions & 5 deletions models/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (

var num int64

for i := 0; i < len(code); i++ {
for i := len(code) - 1; i >= 0; i-- {
if n, ok := base58alphabetMap[code[i]]; !ok {
return 0, ErrInvalidCode
} else {
Expand All @@ -61,14 +61,14 @@ func init() {

type URL struct {
ID int64 `json:"-" bson:"ID"`
Seq uint8 `json:"-" bson:"seq"`
ShardID int64 `json:"-" bson:"shardID"`
Code string `json:"code" bson:"-"`
URL string `json:"url" bson:"url"`
ExpireAt int64 `json:"expireAt" bson:"expireAt"`
}

func (u *URL) ToBSON() error {
u.Seq = getSeq(u)
u.ShardID = getShardID(u)

return nil
}
Expand Down Expand Up @@ -104,8 +104,8 @@ func getCode(u *URL) (string, error) {
return code, nil
}

func getSeq(u *URL) uint8 {
return uint8(u.ID & 0xFF)
func getShardID(u *URL) int64 {
return (u.ID >> 24) % 1000
}

func getID(u *URL) (int64, error) {
Expand Down
12 changes: 11 additions & 1 deletion pkg/cache/redis/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ import (
)

type impl struct {
// client *redis.ClusterClient
client *redis.Client
config *config.Redis
}

func NewRedis(config *config.Redis) (cache.Cache, error) {
// client := redis.NewClusterClient(&redis.ClusterOptions{
// Addrs: config.Addrs,
// Password: config.Password,

// RouteByLatency: true,
// RouteRandomly: true,
// })

client := redis.NewClient(&redis.Options{
Addr: config.Addrs[0],
Addr: config.Addrs[0],
Password: config.Password,
})

// Ping the primary
Expand Down
5 changes: 2 additions & 3 deletions pkg/database/mongodb/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func (im *impl) getCollection(collectionName string) *mongo.Collection {
func (im *impl) Get(ctx context.Context, table string, key int64, result interface{}) error {
collection := im.getCollection(table)

seq := key & 0xFF
filter := bson.M{
"seq": seq,
"ID": key,
"shardID": (key >> 24) % 1000,
"ID": key,
}

err := collection.FindOne(ctx, filter).Decode(result)
Expand Down
Loading

0 comments on commit edddd95

Please sign in to comment.