#!/bin/bash
#
# 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.
#
# test that we can insert and retrieve a simple record

# start zookeeper on localhost:2183
source setup_env.inc
export TEST_NAME=clm_zk_dummyprocess.test

zookeeper_server_ports="localhost:2188"
# default datadir integration_test/var/work/zookeeper/data/1
# start the zookeeper cluster
$SCRIPT_DIR/cm_driver.py -c zookeeper -o start --zookeeper_reset --zookeeper_server_ports=${zookeeper_server_ports}  --cmdline_props="tickTime=2000;initLimit=5;syncLimit=2;maxClientCnxns=0" 

# create storage cluster
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addCluster storage-cluster-12345"

$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addCluster relay-cluster-12345"

$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addResource storage-cluster-12345 db-12345 120"

$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8900"

$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8901"

$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8902"

$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8903"

$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8904"

$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="-zkSvr localhost:2188 -rebalance storage-cluster-12345 db-12345 3"

#Launch dummy process nodes

$SCRIPT_DIR/cm_driver.py -c dummy-process -o start -l "integration-test/config/log4j2.properties" --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345 -host localhost -port 8900"

$SCRIPT_DIR/cm_driver.py -c dummy-process -o start -l "integration-test/config/log4j2.properties" --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345 -host localhost -port 8901"

$SCRIPT_DIR/cm_driver.py -c dummy-process -o start -l "integration-test/config/log4j2.properties" --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345 -host localhost -port 8902"

$SCRIPT_DIR/cm_driver.py -c dummy-process -o start -l "integration-test/config/log4j2.properties" --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345 -host localhost -port 8903"

$SCRIPT_DIR/cm_driver.py -c dummy-process -o start -l "integration-test/config/log4j2.properties" --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345 -host localhost -port 8904"


#Launch the cluster manager
$SCRIPT_DIR/cm_driver.py -c cluster-manager -o start -l "integration-test/config/log4j2.properties" --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345"

exit 0
sleep 6

#$SCRIPT_DIR/cm_driver.py -c cluster-manager -o stop 

#verify cluster state
verifier_1_output=$VIEW_ROOT/$LOG_DIR_FROM_ROOT/verifier_1.log
$SCRIPT_DIR/cm_driver.py -c cluster-state-verifier -o start --logfile=$verifier_1_output -l "integration-test/config/log4j2.properties" --cmdline_args="-zkSvr localhost:2188 -cluster storage-cluster-12345"

$SCRIPT_DIR/cm_driver.py -c dummy-process -o stop

$SCRIPT_DIR/cm_driver.py -c cluster-manager -o stop
$SCRIPT_DIR/cm_driver.py -c zookeeper -o stop

echo == GREP SUCCEED ==
#find ~/EspressoLogs/ -mmin -10 -print | xargs grep succeed
grep Successful $verifier_1_output

source report_pass_fail.inc

exit $all_stat

# add 2 more nodes
#$SCRIPT_DIR/cm_driver.py -zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8905

#$SCRIPT_DIR/cm_driver.py -zkSvr localhost:2188 -addNode storage-cluster-12345 localhost:8906

# rebalance
#$SCRIPT_DIR/cm_driver.py -zkSvr localhost:2188 -rebalance storage-cluster-12345 db-12345 3

#Launch the cluster manager
#$SCRIPT_DIR/cm_driver.py -c cluster-manager -o start --cmdline_props="-zkSvr localhost:2188 -cluster storage-cluster-12345"

#sleep 5

#$SCRIPT_DIR/cm_driver.py -c cluster-manager -o stop

#verify cluster state
#$SCRIPT_DIR/cm_driver.py -c cluster-state-verifier -o start --cmdline_props="-zkSvr localhost:2188 -cluster storage-cluster-12345"

#$SCRIPT_DIR/cm_driver.py -c dummy-process -o stop

#$SCRIPT_DIR/cm_driver.py -c cluster-manager -o stop

# do not stop for now so that people can view the state
#$SCRIPT_DIR/cm_driver.py -c zookeeper -o start
#
