-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (113 loc) · 2.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bufio"
"context"
"crypto/tls"
"github.com/bradfitz/gomemcache/memcache"
"log"
"net"
"os"
"os/exec"
"strings"
"sync"
)
func main() {
memcachedOfficialTool := "memcached-tool"
tool := os.Getenv("MEMCACHED_TOOL_PATH")
if tool != "" {
memcachedOfficialTool = tool
}
srcTLS := false
srcTLSEnv := os.Getenv("DEST_MEMCACHED_TLS")
if strings.ToUpper(srcTLSEnv) == "TRUE" {
srcTLS = true
}
destTLS := false
destTLSEnv := os.Getenv("DEST_MEMCACHED_TLS")
if strings.ToUpper(destTLSEnv) == "TRUE" {
destTLS = true
}
srcServerList := os.Getenv("SRC_MEMCACHED_SERVERS")
if srcServerList == "" {
log.Fatalln("SRC_MEMCACHED_SERVERS environment variable not set")
}
srcServers := strings.Split(srcServerList, ",")
destServerList := os.Getenv("DEST_MEMCACHED_SERVERS")
if destServerList == "" {
log.Fatalln("DEST_MEMCACHED_SERVERS environment variable not set")
}
destServers := strings.Split(destServerList, ",")
mcDest := memcache.New(destServers...)
if destTLS {
mcDest.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
var td tls.Dialer
td.Config = &tls.Config{
InsecureSkipVerify: true,
}
return td.DialContext(ctx, network, addr)
}
}
var wg sync.WaitGroup
for _, srcServer := range srcServers {
wg.Add(1)
go func() {
mcSrc := memcache.New(srcServer)
if srcTLS {
mcSrc.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
var td tls.Dialer
td.Config = &tls.Config{
InsecureSkipVerify: true,
}
return td.DialContext(ctx, network, addr)
}
}
log.Printf("Start migrate data from server: %s\n", srcServer)
migrate(memcachedOfficialTool, srcServer, mcSrc, mcDest)
defer wg.Done()
}()
}
wg.Wait()
}
func migrate(memcachedOfficialTool, server string, mcSrc, mcDest *memcache.Client) {
cmd := exec.Command(memcachedOfficialTool, server, "keys")
// Get the standard output pipe of the command
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Printf("Unable to obtain standard output pipe: %v", err)
return
}
// Start the command
if err := cmd.Start(); err != nil {
log.Printf("Failed to start command: %v", err)
return
}
// Read the output line by line
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
key := strings.Split(strings.Split(line, " ")[0], "=")[1]
item, err := mcSrc.Get(key)
if err != nil {
log.Printf("Get key %s error: %v", key, err)
continue
}
if item != nil {
err = mcDest.Set(item)
if err != nil {
log.Printf("Set key %s error: %v", key, err)
continue
}
} else {
log.Printf("Key %s not found", key)
continue
}
}
// Check if there was an error reading the output
if err := scanner.Err(); err != nil {
log.Printf("Error reading output: %v", err)
}
// Wait for the command to finish executing
if err := cmd.Wait(); err != nil {
log.Printf("Command failed: %v", err)
}
}