web-dev-qa-db-fra.com

kafka-server-stop.sh ne fonctionnait pas lorsque Kafka a démarré à partir d'un script Python

Après avoir déployé certaines instances Apache Kafka sur des nœuds distants, j'ai constaté un problème avec le script kafka-server-stop.sh faisant partie de l'archive Kafka.

Par défaut, il contient:

#!/bin/sh
# 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.
ps ax | grep -i 'kafka\.Kafka' | grep Java | grep -v grep | awk '{print $1}' | xargs kill -SIGTERM

et ce script fonctionne très bien si j'exécute Apache kafka en tant que processus en arrière-plan, par exemple:

/var/lib/kafka/bin/kafka-server-start.sh /var/lib/kafka/config/server.properties

cela fonctionne aussi lorsque je l'exécute en tant que processus en arrière-plan:

/var/lib/kafka/bin/kafka-server-start.sh /var/lib/kafka/config/server.properties &

mais sur mes nœuds distants je l'exécute (avec l'utilisation de Ansible) avec ce script python:

#!/usr/bin/env python
import argparse
import os
import subprocess

KAFKA_PATH = "/var/lib/kafka/"

def execute_command_pipe_output(command_to_call):
  return subprocess.Popen(command_to_call, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

def execute_command_no_output(command_to_call):
  with open(os.devnull, "w") as null_file:
    return subprocess.Popen(command_to_call, stdout=null_file, stderr=subprocess.STDOUT)  

def start_kafka(args):
  command_to_call = ["Nohup"]
  command_to_call += [KAFKA_PATH + "bin/zookeeper-server-start.sh"]
  command_to_call += [KAFKA_PATH + "config/zookeeper.properties"]

  proc = execute_command_no_output(command_to_call)

  command_to_call = ["Nohup"]
  command_to_call += [KAFKA_PATH + "bin/kafka-server-start.sh"]
  command_to_call += [KAFKA_PATH + "config/server.properties"]

  proc = execute_command_no_output(command_to_call)

def stop_kafka(args):
  command_to_call = [KAFKA_PATH + "bin/kafka-server-stop.sh"]

  proc = execute_command_pipe_output(command_to_call)
  for line in iter(proc.stdout.readline, b''):
    print line,

  command_to_call = [KAFKA_PATH + "bin/zookeeper-server-stop.sh"]

  proc = execute_command_pipe_output(command_to_call)
  for line in iter(proc.stdout.readline, b''):
    print line,


if __== "__main__":
  parser = argparse.ArgumentParser(description="Starting Zookeeper and Kafka instances")
  parser.add_argument('action', choices=['start', 'stop'], help="action to take")

  args = parser.parse_args()

  if args.action == 'start':
    start_kafka(args)
  Elif args.action == 'stop':
    stop_kafka(args)
  else:
    parser.print_help()

après avoir exécuté 

manage-kafka.py start
manage-kafka.py stop

Zookeeper est arrêté (comme il se doit) mais Kafka est toujours en cours d'exécution. 

Quoi de plus intéressant, quand j'invoque (à la main)

Nohup /var/lib/kafka/bin/kafka-server-stop.sh

ou 

Nohup /var/lib/kafka/bin/kafka-server-stop.sh &

kafka-server-stop.sh ferme correctement l'instance de Kafka. Je soupçonne que ce problème peut être causé par quelque chose de Linux/Python.

12
Andna

J'ai beaucoup affronté ce problème avant de trouver un moyen brutal de résoudre le problème… .. Donc, ce qui s'est passé, c'est que Kafka a fermé brusquement mais le port est toujours utilisé. 

Suivez les étapes suivantes:

  1. Recherchez l'ID de processus du processus en cours d'exécution sur ce port: lsof -t -i :YOUR_PORT_NUMBER. ## c'est pour mac
  2. Tuez ce processus kill -9 process_id
2
anarcky

Kafka doit terminer le processus d'arrêt avant que les gardiens de zoo ne s'arrêtent.

Donc, démarrez les gardiens de zoo, puis les courtiers réessayeront le processus d'arrêt.

J'ai eu un cas similaire. Le problème était que ma configuration n'attendait pas la fermeture des courtiers de kafka.

J'espère que ça aide quelqu'un. Il m'a fallu un certain temps pour comprendre ...

2
Vicente Rocha

Mon hypothèse: kafka-server-stop.sh utilise des pipes Shell. Donc, Popen aurait besoin de l'argument Shell=True.

Voir https://docs.python.org/2/library/subprocess.html#subprocess.Popen

0
Stephane Martin