forked from cloudstax/firecamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zkcatalog.go
238 lines (193 loc) · 6.75 KB
/
zkcatalog.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package zkcatalog
import (
"fmt"
"github.com/cloudstax/firecamp/catalog"
"github.com/cloudstax/firecamp/common"
"github.com/cloudstax/firecamp/dns"
"github.com/cloudstax/firecamp/manage"
"github.com/cloudstax/firecamp/utils"
)
const (
defaultVersion = "3.4"
// ContainerImage is the main ZooKeeper running container.
ContainerImage = common.ContainerNamePrefix + "zookeeper:" + defaultVersion
// ClientPort is the port at which the clients will connect
ClientPort = 2181
peerConnectPort = 2888
leaderElectPort = 3888
jmxPort = 2191
// DefaultHeapMB is the default zookeeper java heap size
DefaultHeapMB = 4096
zooConfFileName = "zoo.cfg"
myidConfFileName = "myid"
logConfFileName = "log4j.properties"
)
// The default ZooKeeper catalog service. By default,
// 1) Distribute the node on the availability zones.
// 2) Listen on the standard ports, 2181 2888 3888.
// GenDefaultCreateServiceRequest returns the default service creation request.
func GenDefaultCreateServiceRequest(platform string, region string, azs []string,
cluster string, service string, opts *manage.CatalogZooKeeperOptions,
res *common.Resources) (req *manage.CreateServiceRequest, jmxUser string, jmxPasswd string) {
// check and set the jmx remote user and password
if len(opts.JmxRemoteUser) == 0 {
opts.JmxRemoteUser = catalog.JmxDefaultRemoteUser
}
if len(opts.JmxRemotePasswd) == 0 {
opts.JmxRemotePasswd = utils.GenUUID()
}
serviceCfgs := genServiceConfigs(platform, cluster, service, azs, opts)
// generate service ReplicaConfigs
replicaCfgs := genReplicaConfigs(platform, region, cluster, service, azs, opts)
portMappings := []common.PortMapping{
{ContainerPort: ClientPort, HostPort: ClientPort, IsServicePort: true},
{ContainerPort: peerConnectPort, HostPort: peerConnectPort},
{ContainerPort: leaderElectPort, HostPort: leaderElectPort},
{ContainerPort: jmxPort, HostPort: jmxPort},
}
reserveMemMB := res.ReserveMemMB
if res.ReserveMemMB < opts.HeapSizeMB {
reserveMemMB = opts.HeapSizeMB
}
req = &manage.CreateServiceRequest{
Service: &manage.ServiceCommonRequest{
Region: region,
Cluster: cluster,
ServiceName: service,
},
Resource: &common.Resources{
MaxCPUUnits: res.MaxCPUUnits,
ReserveCPUUnits: res.ReserveCPUUnits,
MaxMemMB: res.MaxMemMB,
ReserveMemMB: reserveMemMB,
},
ServiceType: common.ServiceTypeStateful,
CatalogServiceType: common.CatalogService_ZooKeeper,
ContainerImage: ContainerImage,
Replicas: opts.Replicas,
Volume: opts.Volume,
ContainerPath: common.DefaultContainerMountPath,
PortMappings: portMappings,
RegisterDNS: true,
ServiceConfigs: serviceCfgs,
ReplicaConfigs: replicaCfgs,
}
return req, opts.JmxRemoteUser, opts.JmxRemotePasswd
}
func genServiceConfigs(platform string, cluster string, service string, azs []string, opts *manage.CatalogZooKeeperOptions) []*manage.ConfigFileContent {
domain := dns.GenDefaultDomainName(cluster)
serverList := genServerList(service, domain, opts.Replicas)
// create the service.conf file
content := fmt.Sprintf(servicefileContent, platform, opts.HeapSizeMB, opts.JmxRemoteUser, opts.JmxRemotePasswd, catalog.JmxReadOnlyAccess, jmxPort)
serviceCfg := &manage.ConfigFileContent{
FileName: catalog.SERVICE_FILE_NAME,
FileMode: common.DefaultConfigFileMode,
Content: content,
}
// create the zoo.cfg file
zooCfg := &manage.ConfigFileContent{
FileName: zooConfFileName,
FileMode: common.DefaultConfigFileMode,
Content: fmt.Sprintf(zooConfigs, serverList),
}
// create the log config file
logCfg := &manage.ConfigFileContent{
FileName: logConfFileName,
FileMode: common.DefaultConfigFileMode,
Content: logConfContent,
}
return []*manage.ConfigFileContent{serviceCfg, zooCfg, logCfg}
}
// genReplicaConfigs generates the replica configs.
func genReplicaConfigs(platform string, region string, cluster string, service string, azs []string, opts *manage.CatalogZooKeeperOptions) []*manage.ReplicaConfig {
domain := dns.GenDefaultDomainName(cluster)
replicaCfgs := make([]*manage.ReplicaConfig, opts.Replicas)
for i := 0; i < int(opts.Replicas); i++ {
member := utils.GenServiceMemberName(service, int64(i))
memberHost := dns.GenDNSName(member, domain)
index := i % len(azs)
// create the member.conf file
content := fmt.Sprintf(memberfileContent, azs[index], memberHost)
memberCfg := &manage.ConfigFileContent{
FileName: catalog.MEMBER_FILE_NAME,
FileMode: common.DefaultConfigFileMode,
Content: content,
}
// create the myid file
content = fmt.Sprintf(myidConfig, i+1)
myidCfg := &manage.ConfigFileContent{
FileName: myidConfFileName,
FileMode: common.DefaultConfigFileMode,
Content: content,
}
configs := []*manage.ConfigFileContent{memberCfg, myidCfg}
replicaCfg := &manage.ReplicaConfig{Zone: azs[index], MemberName: member, Configs: configs}
replicaCfgs[i] = replicaCfg
}
return replicaCfgs
}
func genServerList(service string, domain string, replicas int64) string {
serverList := ""
for i := 0; i < int(replicas); i++ {
member := utils.GenServiceMemberName(service, int64(i))
dnsname := dns.GenDNSName(member, domain)
server := fmt.Sprintf(serverLine, i+1, dnsname, peerConnectPort, leaderElectPort)
serverList += server
}
return serverList
}
const (
servicefileContent = `
PLATFORM=%s
HEAP_SIZE_MB=%d
JMX_REMOTE_USER=%s
JMX_REMOTE_PASSWD=%s
JMX_REMOTE_ACCESS=%s
JMX_PORT=%d
`
memberfileContent = `
AVAILABILITY_ZONE=%s
SERVICE_MEMBER=%s
`
serverLine = `
server.%d=%s:%d:%d`
zooConfigs = `
# http://zookeeper.apache.org/doc/r3.4.10/zookeeperAdmin.html
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/data
# the port at which the clients will connect
clientPort=2181
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
# all zookeeper members
%s
`
myidConfig = `%d`
logConfContent = `
zookeeper.root.logger=INFO, CONSOLE
zookeeper.console.threshold=INFO
# DEFAULT: console appender only
log4j.rootLogger=${zookeeper.root.logger}
#
# Log INFO level and above messages to the console
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n
`
)