-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid
299 lines (265 loc) · 8.71 KB
/
grid
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash -e
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# This script will download, setup, start, and stop servers for Kafka, YARN, and ZooKeeper,
# as well as downloading, building and locally publishing Samza
if [ -z "$JAVA_HOME" ]; then
if [ -x /usr/libexec/java_home ]; then
export JAVA_HOME="$(/usr/libexec/java_home)"
else
echo "JAVA_HOME not set. Exiting."
exit 1
fi
fi
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASE_DIR=/opt/samza
DEPLOY_ROOT_DIR=$BASE_DIR/deploy
DOWNLOAD_CACHE_DIR=$HOME/.samza/download
COMMAND=$1
SYSTEM=$2
# Version are in gradle properties
SAMZA_VERSION=
SAMZA_TAG=
SCALA_VERSION=
KAFKA_VERSION=
ZK_VERSION=
HADOOP_VERSION=
source $SCRIPT_DIR/../gradle.properties
# File and URL
KAFKA_PACKAGE="kafka_${SCALA_VERSION}-${KAFKA_VERSION}"
DOWNLOAD_KAFKA="https://archive.apache.org/dist/kafka/${KAFKA_VERSION}/${KAFKA_PACKAGE}.tgz"
YARN_PACKAGE="hadoop-${HADOOP_VERSION}"
DOWNLOAD_YARN="https://archive.apache.org/dist/hadoop/common/${YARN_PACKAGE}/${YARN_PACKAGE}.tar.gz"
ZOOKEEPER_PACKAGE="zookeeper-${ZK_VERSION}"
DOWNLOAD_ZOOKEEPER="https://archive.apache.org/dist/zookeeper/${ZOOKEEPER_PACKAGE}/${ZOOKEEPER_PACKAGE}.tar.gz"
if [ "${SAMZA_VERSION}" == "1.4.0" ]; then
SAMZA_TOOL_PACKAGE="samza-tools-1.3.1" # 1.4.1 not available
DOWNLOAD_SAMZA_TOOL="https://downloads.apache.org/samza/1.3.1/samza-tools_${SCALA_VERSION}-1.3.1.tgz"
else
SAMZA_TOOL_PACKAGE="samza-tools_${SCALA_VERSION}-${SAMZA_VERSION}"
DOWNLOAD_SAMZA_TOOL="https://downloads.apache.org/samza/${SAMZA_VERSION}/${SAMZA_TOOL_PACKAGE}.tgz"
fi
SERVICE_WAIT_TIMEOUT_SEC=20
ZOOKEEPER_PORT=2181
RESOURCEMANAGER_PORT=8032
NODEMANAGER_PORT=8042
KAFKA_PORT=9092
bootstrap() {
echo "Bootstrapping the system..."
stop_all
rm -rf "$DEPLOY_ROOT_DIR"
mkdir "$DEPLOY_ROOT_DIR"
install_all
start_all
exit 0
}
standalone() {
echo "Setting up the system..."
stop_all
rm -rf "$DEPLOY_ROOT_DIR"
mkdir "$DEPLOY_ROOT_DIR"
install_all_without_yarn
start_all_without_yarn
exit 0
}
install_all() {
$SCRIPT_DIR/grid install samza
$SCRIPT_DIR/grid install zookeeper
$SCRIPT_DIR/grid install yarn
$SCRIPT_DIR/grid install kafka
}
install_all_without_yarn() {
$SCRIPT_DIR/grid install samza
$SCRIPT_DIR/grid install zookeeper
$SCRIPT_DIR/grid install kafka
}
install_samza() {
echo "Building samza ${SAMZA_TAG} from master..."
mkdir -p "$DEPLOY_ROOT_DIR"
if [ -d "$DEPLOY_ROOT_DIR/samza/.git" ]; then
pushd "$DEPLOY_ROOT_DIR/samza"
git fetch origin
git reset --hard origin/master
else
mkdir -p $DEPLOY_ROOT_DIR
pushd $DEPLOY_ROOT_DIR
git clone https://gitbox.apache.org/repos/asf/samza.git
cd samza
fi
git checkout tags/${SAMZA_TAG}
# ./gradlew clean build -x test
./gradlew -PscalaSuffix=${SCALA_VERSION} clean publishToMavenLocal
popd
}
install_zookeeper() {
mkdir -p "$DEPLOY_ROOT_DIR"
install zookeeper $DOWNLOAD_ZOOKEEPER $ZOOKEEPER_PACKAGE
cp "$DEPLOY_ROOT_DIR/zookeeper/conf/zoo_sample.cfg" "$DEPLOY_ROOT_DIR/zookeeper/conf/zoo.cfg"
}
# https://samza.apache.org/learn/tutorials/0.14/samza-tools.html
install_samza_tool() {
mkdir -p "$DEPLOY_ROOT_DIR"
install samzatool $DOWNLOAD_SAMZA_TOOL $SAMZA_TOOL_PACKAGE
}
install_yarn() {
mkdir -p "$DEPLOY_ROOT_DIR"
install yarn $DOWNLOAD_YARN $YARN_PACKAGE
HADOOP_CONF_DIR="${DEPLOY_ROOT_DIR}/yarn/conf"
mkdir -p $HADOOP_CONF_DIR
cp "$DEPLOY_ROOT_DIR/yarn/etc/hadoop/yarn-site.xml" "${HADOOP_CONF_DIR}/yarn-site.xml"
if [ ! -f "$HOME/.samza/conf/yarn-site.xml" ]; then
mkdir -p "$HOME/.samza/conf"
cp "${HADOOP_CONF_DIR}/yarn-site.xml" "$HOME/.samza/conf/yarn-site.xml"
fi
}
install_kafka() {
mkdir -p "$DEPLOY_ROOT_DIR"
install kafka $DOWNLOAD_KAFKA $KAFKA_PACKAGE
# have to use SIGTERM since nohup on appears to ignore SIGINT
# and Kafka switched to SIGINT in KAFKA-1031.
sed -i.bak 's/SIGINT/SIGTERM/g' $DEPLOY_ROOT_DIR/kafka/bin/kafka-server-stop.sh
# in order to simplify the wikipedia-stats example job, set topic to have just 1 partition by default
sed -i.bak 's/^num\.partitions *=.*/num.partitions=1/' $DEPLOY_ROOT_DIR/kafka/config/server.properties
}
install() {
DESTINATION_DIR="$DEPLOY_ROOT_DIR/$1"
DOWNLOAD_URL=$2
PACKAGE_DIR="$DOWNLOAD_CACHE_DIR/$3"
PACKAGE_FILE="$DOWNLOAD_CACHE_DIR/$(basename $DOWNLOAD_URL)"
if [ -f "$PACKAGE_FILE" ]; then
echo "Using previously downloaded file $PACKAGE_FILE"
else
echo "Downloading $(basename $DOWNLOAD_URL)..."
mkdir -p $DOWNLOAD_CACHE_DIR
curl "$DOWNLOAD_URL" > "${PACKAGE_FILE}.tmp"
mv "${PACKAGE_FILE}.tmp" "$PACKAGE_FILE"
fi
rm -rf "$DESTINATION_DIR" "$PACKAGE_DIR"
tar -xf "$PACKAGE_FILE" -C $DOWNLOAD_CACHE_DIR
mv "$PACKAGE_DIR" "$DESTINATION_DIR"
}
start_all() {
$SCRIPT_DIR/grid start zookeeper
$SCRIPT_DIR/grid start yarn
$SCRIPT_DIR/grid start kafka
}
start_all_without_yarn() {
$SCRIPT_DIR/grid start zookeeper
$SCRIPT_DIR/grid start kafka
}
start_zookeeper() {
if [ -f $DEPLOY_ROOT_DIR/$SYSTEM/bin/zkServer.sh ]; then
cd $DEPLOY_ROOT_DIR/$SYSTEM
bin/zkServer.sh start
wait_for_service "zookeeper" $ZOOKEEPER_PORT
cd - > /dev/null
else
echo 'Zookeeper is not installed. Run: bin/grid install zookeeper'
fi
}
start_yarn() {
if [ -f $DEPLOY_ROOT_DIR/$SYSTEM/sbin/yarn-daemon.sh ]; then
$DEPLOY_ROOT_DIR/$SYSTEM/sbin/yarn-daemon.sh start resourcemanager
wait_for_service "resourcemanager" $RESOURCEMANAGER_PORT
$DEPLOY_ROOT_DIR/$SYSTEM/sbin/yarn-daemon.sh start nodemanager
wait_for_service "nodemanager" $NODEMANAGER_PORT
else
echo 'YARN is not installed. Run: bin/grid install yarn'
fi
}
start_kafka() {
if [ -f $DEPLOY_ROOT_DIR/$SYSTEM/bin/kafka-server-start.sh ]; then
mkdir -p $DEPLOY_ROOT_DIR/$SYSTEM/logs
cd $DEPLOY_ROOT_DIR/$SYSTEM
nohup bin/kafka-server-start.sh config/server.properties > logs/kafka.log 2>&1 &
cd - > /dev/null
wait_for_service "kafka" $KAFKA_PORT
else
echo 'Kafka is not installed. Run: bin/grid install kafka'
fi
}
wait_for_service() {
local SERVICE_NAME=$1
local PORT=$2
echo "Waiting for $SERVICE_NAME to start..."
local CURRENT_WAIT_TIME=0
while [[ $(echo | nc -w1 localhost $PORT >/dev/null 2>&1 ;echo $?) -ne 0 ]]; do
printf '.'
sleep 1
if [ $((++CURRENT_WAIT_TIME)) -eq $SERVICE_WAIT_TIMEOUT_SEC ]; then
printf "\nError: timed out while waiting for $SERVICE_NAME to start.\n"
exit 1
fi
done
printf '\n'
echo "$SERVICE_NAME has started";
}
stop_all() {
$SCRIPT_DIR/grid stop kafka
$SCRIPT_DIR/grid stop yarn
$SCRIPT_DIR/grid stop zookeeper
}
stop_zookeeper() {
if [ -f $DEPLOY_ROOT_DIR/$SYSTEM/bin/zkServer.sh ]; then
cd $DEPLOY_ROOT_DIR/$SYSTEM
bin/zkServer.sh stop
cd - > /dev/null
else
echo 'Zookeeper is not installed. Run: bin/grid install zookeeper'
fi
}
stop_yarn() {
if [ -f $DEPLOY_ROOT_DIR/$SYSTEM/sbin/yarn-daemon.sh ]; then
$DEPLOY_ROOT_DIR/$SYSTEM/sbin/yarn-daemon.sh stop resourcemanager
$DEPLOY_ROOT_DIR/$SYSTEM/sbin/yarn-daemon.sh stop nodemanager
else
echo 'YARN is not installed. Run: bin/grid install yarn'
fi
}
stop_kafka() {
if [ -f $DEPLOY_ROOT_DIR/$SYSTEM/bin/kafka-server-stop.sh ]; then
cd $DEPLOY_ROOT_DIR/$SYSTEM
bin/kafka-server-stop.sh || true # tolerate nonzero exit status if Kafka isn't running
cd - > /dev/null
else
echo 'Kafka is not installed. Run: bin/grid install kafka'
fi
}
# Check arguments
if [ "$COMMAND" == "bootstrap" ] && test -z "$SYSTEM"; then
bootstrap
exit 0
elif [ "$COMMAND" == "standalone" ] && test -z "$SYSTEM"; then
standalone
exit 0
elif (test -z "$COMMAND" && test -z "$SYSTEM") \
|| ( [ "$COMMAND" == "help" ] || test -z "$COMMAND" || test -z "$SYSTEM"); then
echo
echo " Usage.."
echo
echo " $ grid"
echo " $ grid bootstrap"
echo " $ grid standalone"
echo " $ grid install [yarn|kafka|zookeeper|samza|samza_tool|all]"
echo " $ grid start [yarn|kafka|zookeeper|all]"
echo " $ grid stop [yarn|kafka|zookeeper|all]"
echo
exit 1
else
echo "EXECUTING: $COMMAND $SYSTEM"
"$COMMAND"_"$SYSTEM"
fi