Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
farapono committed Jan 11, 2019
1 parent f03d04f commit 5c22ec2
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 134 deletions.
19 changes: 0 additions & 19 deletions from_redis.go

This file was deleted.

5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module gitlab.com/microredis/migrator

require (
github.com/go-redis/redis v6.15.0+incompatible
github.com/go-redis/redis v6.15.1+incompatible
github.com/google/wire v0.2.0
github.com/microredis/tools v0.0.0-20190110175036-524ffa60acd4
github.com/microredis/tools v0.0.0-20190110212942-e344d1ec4efe
golang.org/x/tools v0.0.0-20190110211028-68c5ac90f574 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
github.com/go-redis/redis v6.15.0+incompatible h1:/Wib9cA7CF3SQxBZRMHyQvqzlwzc8PJGDMkRfqQebSE=
github.com/go-redis/redis v6.15.0+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis v6.15.1+incompatible h1:BZ9s4/vHrIqwOb0OPtTQ5uABxETJ3NRuUNoSUurnkew=
github.com/go-redis/redis v6.15.1+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/wire v0.2.0 h1:l8O+yxT6Kx49nR2KzotgPOQpHFcIvpDY0rGzlCZ1wIE=
github.com/google/wire v0.2.0/go.mod h1:ptBl5bWD3nzmJHVNwYHV3v4wdtKzBMlU2YbtKQCG9GI=
github.com/microredis/tools v0.0.0-20190110175036-524ffa60acd4 h1:hN64+34woWytF3LGNSqrue96xd+z3HmYOxOkoB85mAI=
github.com/microredis/tools v0.0.0-20190110175036-524ffa60acd4/go.mod h1:QgZ583LHnlOJ1jC5pgUIsMNrrSfqZDLNDcHR4JruiJo=
github.com/microredis/tools v0.0.0-20190110212942-e344d1ec4efe h1:Bgdw0Sef9H5kbKd09NzlnamcqfbBhOwuVGW03M5aDdA=
github.com/microredis/tools v0.0.0-20190110212942-e344d1ec4efe/go.mod h1:QgZ583LHnlOJ1jC5pgUIsMNrrSfqZDLNDcHR4JruiJo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28 h1:vnbqcYKfOxPnXXUlBo7t+R4pVIh0wInyOSNxih1S9Dc=
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190110211028-68c5ac90f574 h1:sNWcmYE7tNFTCVg/HrEF29VgTSwiTN4xxhc0i2dhJ44=
golang.org/x/tools v0.0.0-20190110211028-68c5ac90f574/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5 changes: 2 additions & 3 deletions injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"github.com/google/wire"
)

func initializeApp() (*Migrator, error) {
func initializeApp() (Migrator, error) {
panic(wire.Build(
NewFromOptions,
NewToOptions,
NewToRedisClient,
NewFromRedisClient,
NewRedisClients,
NewMigrator,
))
}
90 changes: 3 additions & 87 deletions migrator.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,5 @@
package main

import (
"github.com/go-redis/redis"
"log"
"net"
)

type Migrator struct {
from *redis.Client
to *redis.Client
maxOutBuff int64
maxOutBuffCommands int64
}

func NewMigrator(fromRedisClient FromRedisClient, toRedisClient ToRedisClient) *Migrator {
return &Migrator{fromRedisClient, toRedisClient, 0, 0}
}

func (m *Migrator) Migrate() {
defer m.to.Close()
defer m.from.Close()
log.Println("Started...")
m.prepareTo()
log.Println("Waiting...")
m.waitForUp()
log.Println("Waiting for complete...")
m.waitForComplete()
log.Println("Finish...")
m.onComplete()
}

func (m *Migrator) prepareTo() {
if err := m.to.ConfigSet("slave-read-only", "yes").Err(); err != nil {
panic(err)
}
if err := m.to.ConfigSet("masterauth", m.from.Options().Password).Err(); err != nil {
panic(err)
}
if host, port, err := net.SplitHostPort(m.from.Options().Addr); err != nil {
panic(err)
} else if err := m.to.SlaveOf(host, port).Err(); err != nil {
panic(err)
}
}

func (m *Migrator) waitForUp() {
info := new(Info)
for {
if err := m.to.Info().Scan(info); err != nil {
panic(err)
}
if info.MasterLinkStatus == "up" {
return
}
}
}

func (m *Migrator) waitForComplete() {
var clientList ClientList
for {
if err := m.from.ClientList().Scan(&clientList); err != nil {
panic(err)
}
for _, client := range clientList {
if client.Flags == "S" {
oblAndOll := toBinary(client.Obl) + client.Oll
if m.maxOutBuff == 0 || m.maxOutBuff < client.Omem {
m.maxOutBuff = client.Omem
}
if m.maxOutBuffCommands == 0 || m.maxOutBuffCommands < oblAndOll {
m.maxOutBuffCommands = oblAndOll
}
}
}
if (m.maxOutBuff + m.maxOutBuffCommands) == 0 {
return
}
}
}

func (m *Migrator) onComplete() {
if err := m.to.SlaveOf("no", "one").Err(); err != nil {
panic(err)
}
if err := m.to.ConfigSet("slave-read-only", "no").Err(); err != nil {
panic(err)
}
}
type Migrator interface {
Migrate()
}
30 changes: 30 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"github.com/go-redis/redis"
"os"
)

type (
FromOptions *redis.Options
ToOptions *redis.Options
RedisClients struct {
From *redis.Client
To *redis.Client
}
)

func NewFromOptions() (FromOptions, error) {
return redis.ParseURL(os.Getenv("MICROREDIS_FROM_ADDRESS"))
}

func NewToOptions() (ToOptions, error) {
return redis.ParseURL(os.Getenv("MICROREDIS_TO_ADDRESS"))
}

func NewRedisClients(from FromOptions, to ToOptions) RedisClients {
return RedisClients{
From: redis.NewClient(from),
To: redis.NewClient(to),
}
}
87 changes: 87 additions & 0 deletions redis_migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"log"
"net"
)

type redisMigrator struct {
RedisClients
maxOutBuff int64
maxOutBuffCommands int64
}

func (m *redisMigrator) Migrate() {
defer m.From.Close()
defer m.To.Close()
log.Println("Started...")
m.prepareTo()
log.Println("Waiting...")
m.waitForUp()
log.Println("Waiting...")
m.waitForComplete()
log.Println("Finish...")
m.onComplete()
}

func (m *redisMigrator) prepareTo() {
if err := m.To.ConfigSet("slave-read-only", "yes").Err(); err != nil {
panic(err)
}
if err := m.To.ConfigSet("masterauth", m.From.Options().Password).Err(); err != nil {
panic(err)
}
if host, port, err := net.SplitHostPort(m.From.Options().Addr); err != nil {
panic(err)
} else if err := m.To.SlaveOf(host, port).Err(); err != nil {
panic(err)
}
}

func (m *redisMigrator) waitForUp() {
info := new(Info)
for {
if err := m.To.Info().Scan(info); err != nil {
panic(err)
}
if info.MasterLinkStatus == "up" {
return
}
}
}

func (m *redisMigrator) waitForComplete() {
var clientList ClientList
for {
if err := m.From.ClientList().Scan(&clientList); err != nil {
panic(err)
}
for _, client := range clientList {
if client.Flags == "S" {
oblAndOll := toBinary(client.Obl) + client.Oll
if m.maxOutBuff == 0 || m.maxOutBuff < client.Omem {
m.maxOutBuff = client.Omem
}
if m.maxOutBuffCommands == 0 || m.maxOutBuffCommands < oblAndOll {
m.maxOutBuffCommands = oblAndOll
}
}
}
if (m.maxOutBuff + m.maxOutBuffCommands) == 0 {
return
}
}
}

func (m *redisMigrator) onComplete() {
if err := m.To.SlaveOf("no", "one").Err(); err != nil {
panic(err)
}
if err := m.To.ConfigSet("slave-read-only", "no").Err(); err != nil {
panic(err)
}
}

func NewMigrator(clients RedisClients) Migrator {
return &redisMigrator{clients, 0, 0}
}
19 changes: 0 additions & 19 deletions to_redis.go

This file was deleted.

7 changes: 3 additions & 4 deletions wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5c22ec2

Please sign in to comment.