Zum Hauptinhalt springen
Version: Nächste

Simulieren von JVM-Anwendungsfehlern

Inoffizielle Beta-Übersetzung

Diese Seite wurde von PageTurner AI übersetzt (Beta). Nicht offiziell vom Projekt unterstützt. Fehler gefunden? Problem melden →

Chaos Mesh simuliert Fehler in JVM-Anwendungen mithilfe von Byteman. Die unterstützten Fehlertypen sind:

  • Benutzerdefinierte Ausnahmen auslösen

  • Garbage Collection auslösen

  • Methodenlatenz erhöhen

  • Rückgabewerte von Methoden ändern

  • Fehler durch Konfiguration von Byteman-Dateien auslösen

  • JVM-Druck erhöhen

Dieses Dokument beschreibt, wie Sie mit Chaos Mesh Experimente für die genannten JVM-Fehlertypen erstellen.

Hinweis

Ihr Linux-Kernel muss Version 4.1 oder höher sein.

Experimente mit Chaos Dashboard erstellen

  1. Öffnen Sie Chaos Dashboard und klicken Sie auf der Seite auf NEW EXPERIMENT, um ein neues Experiment anzulegen.

    Neues Experiment erstellen
    Neues Experiment erstellen

  2. Wählen Sie im Bereich Choose a Target die Option JVM FAULT und eine konkrete Aktion wie RETURN. Tragen Sie dann die Details ein.

    JVMChaos-Experimente
    JVMChaos-Experimente

    Informationen zur Konfiguration finden Sie unter [Feldbeschreibung] (#field-description).

  3. Ergänzen Sie die Experimentinformationen und legen Sie den Anwendungsbereich sowie die geplante Laufzeit fest.

    Experimentinformationen
    Experimentinformationen

  4. Übermitteln Sie die Experimentinformationen.

Experimenterstellung mit YAML-Dateien

Das folgende Beispiel zeigt die Anwendung und Auswirkungen von JVMChaos. Dabei wird die Rückgabewert-Manipulation einer Methode demonstriert. Die verwendeten YAML-Dateien finden Sie unter examples/jvm. Das Standardarbeitsverzeichnis für die folgenden Schritte ist ebenfalls examples/jvm. Der Standard-Namespace für Chaos Mesh ist chaos-mesh.

Schritt 1: Zielanwendung erstellen

Helloworld ist eine einfache Java-Anwendung, die hier als Testobjekt dient. Die Anwendung ist in example/jvm/app.yaml definiert:

apiVersion: v1
kind: Pod
metadata:
name: helloworld
namespace: helloworld
spec:
containers:
- name: helloworld
# source code: https://github.com/WangXiangUSTC/byteman-example/tree/main/example.helloworld
# this application will print log like this below:
# 0. Hello World
# 1. Hello World
# ...
image: xiang13225080/helloworld:v1.0
imagePullPolicy: IfNotPresent
  1. Erstellen Sie den Namespace für die Zielanwendung:

    kubectl create namespace helloworld
  2. Stellen Sie den Anwendungs-Pod bereit:

    kubectl apply -f app.yaml
  3. Führen Sie kubectl -n helloworld get pods aus. Es sollte ein Pod namens helloworld im Namespace helloworld erscheinen.

    kubectl -n helloworld get pods

    Ergebnis:

    kubectl get pods -n helloworld
    NAME READY STATUS RESTARTS AGE
    helloworld 1/1 Running 0 2m

    Wenn in der Spalte READY 1/1 angezeigt wird, fahren Sie fort.

Schritt 2: Anwendungsverhalten vor Fehlerinjektion beobachten

Beobachten Sie das Verhalten der helloworld-Anwendung vor der Fehlerinjektion:

kubectl -n helloworld logs -f helloworld

Das Ergebnis sieht wie folgt aus:

0. Hello World
1. Hello World
2. Hello World
3. Hello World
4. Hello World
5. Hello World

Sie sehen, dass helloworld jede Sekunde eine Zeile Hello World ausgibt, wobei die Zeilennummer fortlaufend steigt.

Schritt 3: JVMChaos injizieren und prüfen

  1. Ein JVMChaos-Experiment mit spezifischem Rückgabewert sieht wie folgt aus:

    apiVersion: chaos-mesh.org/v1alpha1
    kind: JVMChaos
    metadata:
    name: return
    namespace: helloworld
    spec:
    action: return
    class: Main
    method: getnum
    value: '9999'
    mode: all
    selector:
    namespaces:
    - helloworld

    JVMChaos ändert den Rückgabewert der getnum-Methode auf den Wert 9999, sodass die Zeilennummer in der helloworld-Ausgabe stets 9999 anzeigt.

  2. JVMChaos mit spezifischem Wert injizieren:

    kubectl apply -f ./jvm-return-example.yaml
  3. Neuestes Log von helloworld prüfen:

    kubectl -n helloworld logs -f helloworld

    Das Log zeigt folgendes:

    Rule.execute called for return_0:0
    return execute
    caught ReturnException
    9999. Hello World

Feldbeschreibung

ParameterTypeDescriptionDefault valueRequiredExample
actionstringIndicates the specific fault type. The available fault types include latency, return, exception, stress, gc, and ruleData.NoneYesreturn
modestringIndicates how to select Pod. The supported modes include one, all, fixed, fixed-percent, and random-max-percent.NoneYesone

Die Bedeutungen der verschiedenen action-Werte sind:

ValueMeaning
latencyIncrease method latency
returnModify return values of a method
exceptionThrow custom exceptions
stressIncrease CPU usage of Java process, or cause memory overflow (support heap overflow and stack overflow)
gcTrigger garbage collection
ruleDataTrigger faults by setting Byteman configuration files

Für unterschiedliche action-Werte können verschiedene Konfigurationsfelder ausgefüllt werden.

Parameter für latency

ParameterTypeDescriptionRequired
classstringThe name of the Java classYes
methodstringThe name of the methodYes
latencyintThe duration of increasing method latency. The unit is millisecond.Yes
portintThe port ID attached to the Java process agent. The faults are injected into the Java process through this ID.No

Parameter für return

ParameterTypeDescriptionRequired
classstringThe name of the Java classYes
methodstringThe name of the methodYes
valuestringSpecifies the return value of the method. Currently, the item can be numeric and string types. If the item (return value) is string, double quotes are required, like "chaos".Yes
portintThe port ID attached to the Java process agent. The faults are injected into the Java process through this ID.No

Parameter für exception

ParameterTypeDescriptionRequired
classstringThe name of the Java classYes
methodstringThe name of the methodYes
exceptionstringThe thrown custom exception, such as 'java.io.IOException("BOOM")'.Yes
portintThe port ID attached to the Java process agent. The faults are injected into the Java process through this ID.No

Parameter für stress

ParameterTypeDescriptionRequired
cpuCountintThe number of CPU cores used for increasing CPU stress. You must configure one item between cpu-count and mem-type.No
memTypestringThe type of OOM. Currently, both 'stack' and 'heap' OOM types are supported. You must configure one item between cpu-count and mem-type.No
portintThe port ID attached to the Java process agent. The faults are injected into the Java process through this ID.No

Parameter für gc

ParameterTypeDescriptionRequired
portintThe port ID attached to the Java process agent. The faults are injected into the Java process through this ID.No

Parameter für ruleData

ParameterTypeDescriptionRequired
ruleDatastringSpecifies the Byteman configuration dataYes
portintThe port ID attached to the Java process agent. The faults are injected into the Java process through this ID.No

Beim Verfassen der Regelkonfigurationsdatei beachten Sie das spezifische Java-Programm und die byteman-rule-language. Beispiel:

RULE modify return value
CLASS Main
METHOD getnum
AT ENTRY
IF true
DO
return 9999
ENDRULE

Sie müssen die Zeilenumbrüche in der Konfigurationsdatei als Newline-Zeichen "\n" maskieren und den maskierten Text als Wert für "rule-data" verwenden, wie folgt:

\nRULE modify return value\nCLASS Main\nMETHOD getnum\nAT ENTRY\nIF true\nDO return 9999\nENDRULE\n"