suchen
HeimJavajavaLernprogrammBelay the Metamorphosis: Analyse des Kafka-Projekts

Vous êtes-vous déjà demandé quels bugs pourraient se cacher dans le code source des projets des entreprises mondiales ? Ne manquez pas l'occasion de repérer des bugs intéressants détectés par l'analyseur statique PVS-Studio dans le projet open source Apache Kafka.

Belay the Metamorphosis: analyzing Kafka project

Introduction

Apache Kafka est un projet open source bien connu principalement écrit en Java. LinkedIn l'a développé en 2011 en tant que courtier de messages, c'est-à-dire un pipeline de données pour différents composants du système. Aujourd'hui, c'est l'une des solutions les plus populaires de sa catégorie.

Prêt à jeter un œil sous le capot ?

P.S.
Je voulais juste laisser un petit mot sur le titre. Il fait référence à "La Métamorphose" de Franz Kafka, où le personnage principal se transforme en une monstrueuse vermine. Notre analyseur statique se bat pour empêcher vos projets de se transformer en vermine monstrueuse se transformer en un bug colossal, alors dites non à "La Métamorphose".

Oh non, des bugs

Tout humour est enraciné dans la douleur

Ce ne sont pas mes mots ; la citation appartient à Richard Pryor. Mais qu’importe ? La première chose dont je voudrais vous parler est une erreur stupide. Pourtant, après de nombreuses tentatives pour comprendre pourquoi le programme ne fonctionne pas correctement, il est frustrant de tomber sur quelque chose comme l'exemple ci-dessous :

@Override
public KeyValueIterator<windowed>, V> backwardFetch(
  K keyFrom,
  K keyTo,
  Instant timeFrom,
  Instant timeTo) {
  ....
  if (keyFrom == null && keyFrom == null) {   // 



<p>Comme vous pouvez le constater, voici quelque chose qu'aucun développeur ne peut éviter : une faute de frappe triviale. Dans la toute première condition, les développeurs ont souhaité utiliser l'expression logique suivante :<br>
</p>

<pre class="brush:php;toolbar:false">keyFrom == null && keyTo == null

L'analyseur a émis deux avertissements :

V6001 Il y a des sous-expressions identiques 'keyFrom == null' à gauche et à droite de l'opérateur '&&'. ReadOnlyWindowStoreStub.java 327, ReadOnlyWindowStoreStub.java 327

V6007 L'expression 'keyFrom == null' est toujours fausse. ReadOnlyWindowStoreStub.java 329

Nous pouvons comprendre pourquoi. De telles fautes de frappe riantes sont intemporelles pour chaque développeur. Certes, nous pouvons passer beaucoup de temps à les chercher, mais ce ne sera pas un jeu d'enfant de se rappeler où ils se sont cachés.

Dans la même classe, il y a exactement la même erreur dans une autre méthode. Je pense qu'il est juste d'appeler cela du copypasta.

@Override
public KeyValueIterator<windowed>, V> fetch(
  K keyFrom,
  K keyTo,
  Instant timeFrom,
  Instant timeTo) {
  ....
  NavigableMap<k v> kvMap = data.get(now);
  if (kvMap != null) {
    NavigableMap<k v> kvSubMap;
    if (keyFrom == null && keyFrom == null) {      // 



<p>Voici les mêmes avertissements :</p>

<p>V6007 L'expression 'keyFrom == null' est toujours fausse. ReadOnlyWindowStoreStub.java 273 </p>

<p>V6001 Il y a des sous-expressions identiques 'keyFrom == null' à gauche et à droite de l'opérateur '&&'. ReadOnlyWindowStoreStub.java 271, ReadOnlyWindowStoreStub.java 271</p>

<p>Ne vous inquiétez pas, nous n'aurons pas à examiner des centaines de lignes de code à la fois. PVS-Studio est excellent pour gérer des choses aussi simples. Que diriez-vous d’aborder quelque chose d’un peu plus difficile ?</p>

<h3>
  
  
  Mutable synchronisé
</h3>

<p>À quoi sert le mot-clé <em>synchronized</em> en Java ? Ici, je me concentrerai uniquement sur les méthodes synchronisées, pas sur les blocs. Selon la documentation Oracle, le mot-clé <em>synchronized</em> déclare une méthode comme synchronisée pour garantir une interaction thread-safe avec une instance. Si un thread invoque une méthode synchronisée de l'instance, les autres threads qui tentent d'invoquer des méthodes synchronisées de la même instance seront bloqués (c'est-à-dire que leur exécution sera suspendue). Ils seront bloqués jusqu'à ce que la méthode invoquée par le premier thread traite son exécution. Ceci est nécessaire lorsque l'instance est visible par plusieurs threads. Les opérations de lecture/écriture de telles instances doivent être exécutées uniquement via des méthodes synchronisées. </p>

<p>Les développeurs ont enfreint la règle dans la classe <em>Sensor</em>, comme illustré dans le fragment de code simplifié ci-dessous. Les opérations de lecture/écriture sur le champ d'instance sont exécutées via des méthodes synchronisées et non synchronisées. Cela peut conduire à une condition de concurrence critique et rendre le résultat imprévisible.<br>
</p>

<pre class="brush:php;toolbar:false">private final Map<metricname kafkametric> metrics;

public void checkQuotas(long timeMs) {                  // 



<p>L'avertissement de l'analyseur ressemble à ceci :</p>

<p>V6102 Synchronisation incohérente du champ 'metrics'. Pensez à synchroniser le terrain sur tous les usages. Capteur.java 49, Capteur.java 254</p>

<p>Si différents threads peuvent modifier l'état de l'instance en même temps, les méthodes qui le permettent doivent être synchronisées. Si le programme ne prévoit pas que plusieurs threads puissent interagir avec l'instance, il est inutile de synchroniser ses méthodes. Dans le pire des cas, cela peut même nuire aux performances du programme.</p>

<p>Il y a beaucoup de telles erreurs dans le programme. Voici un fragment de code similaire pour lequel l'analyseur a émis l'avertissement :<br>
</p>

<pre class="brush:php;toolbar:false">private final PrefixKeyFormatter prefixKeyFormatter; 

@Override
public synchronized void destroy() {                // (
    prefixKeyFormatter.addPrefix(record.key),
    record.value
    ), batch
  );
} 

@Override
public synchronized void deleteRange(....) {        // 



<p>L'avertissement de l'analyseur :</p>

<p>V6102 Synchronisation incohérente du champ 'prefixKeyFormatter'. Pensez à synchroniser le terrain sur tous les usages. LogicalKeyValueSegment.java 60, LogicalKeyValueSegment.java 247</p>

<h3>
  
  
  Iterator, iterator, and iterator again...
</h3>

<p>In the example, there are two rather unpleasant errors within one line at once. I'll explain their nature within the part of the article. Here's a code snippet:<br>
</p>

<pre class="brush:php;toolbar:false">private final Map<string uuid> topicIds = new HashMap(); 

private Map<string kafkafuturevoid> handleDeleteTopicsUsingNames(....) { 
  ....
  Collection<string> topicNames = new ArrayList(topicNameCollection);

  for (final String topicName : topicNames) {
    KafkaFutureImpl<void> future = new KafkaFutureImpl();

    if (allTopics.remove(topicName) == null) {
      ....
    } else {
      topicNames.remove(topicIds.remove(topicName));      // 



<p>That's what the analyzer shows us:</p>

<p>V6066 The type of object passed as argument is incompatible with the type of collection: String, Uuid. MockAdminClient.java 569</p>

<p>V6053 The 'topicNames' collection of 'ArrayList' type is modified while iteration is in progress. ConcurrentModificationException may occur. MockAdminClient.java 569</p>

<p>Now that's a big dilemma! What's going on here, and how should we address it?! </p>

<p>First, let's talk about collections and generics. Using the generic types of collections helps us avoid <em>ClassCastExceptions</em> and cumbersome constructs where we convert types. </p>

<p>If we specify a certain data type when initializing a collection and add an incompatible type, the compiler won't compile the code. </p>

<p>Here's an example:<br>
</p>

<pre class="brush:php;toolbar:false">public class Test {
  public static void main(String[] args) {
    Set<string> set = new HashSet();
    set.add("str");
    set.add(UUID.randomUUID()); // java.util.UUID cannot be converted to
                                // java.lang.String
  }
}
</string>

However, if we delete an incompatible type from our Set, no exception will be thrown. The method returns false.

Here's an example:

public class Test {
  public static void main(String[] args) {
    Set<string> set = new HashSet();
    set.add("abc");
    set.add("def");
    System.out.println(set.remove(new Integer(13))); // false
  }
}
</string>

It's a waste of time. Most likely, if we encounter something like this in the code, this is an error. I suggest you go back to the code at the beginning of this subchapter and try to spot a similar case.

Second, let's talk about the Iterator. We can talk about iterating through collections for a long time. I don't want to bore you or digress from the main topic, so I'll just cover the key points to ensure we understand why we get the warning.

So, how do we iterate through the collection here? Here is what the for loop in the code fragment looks like:

for (Type collectionElem : collection) {
  ....
}

The for loop entry is just syntactic sugar. The construction is equivalent to this one:

for (Iterator<type> iter = collection.iterator(); iter.hasNext();) {
  Type collectionElem = iter.next();
  ....
}
</type>

We're basically working with the collection iterator. All right, that's sorted! Now, let's discuss ConcurrentModificationException.

ConcurrentModificationException is an exception that covers a range of situations both in single-threaded and multi-threaded programs. Here, we're focusing on single-threading. We can find an explanation quite easily. Let's take a peek at the Oracle docs: a method can throw the exception when it detects parallel modification of an object that doesn't support it. In our case, while the iterator is running, we delete objects from the collection. This may cause the iterator to throw a ConcurrentModificationException.

How does the iterator know when to throw the exception? If we look at the ArrayList collection, we see that its parent, AbstactList, has the modCount field that stores the number of modifications to the collection:

protected transient int modCount = 0;

Here are some usages of the modCount counter in the ArrayList class:

public boolean add(E e) {
  modCount++;
  add(e, elementData, size);
  return true;
}

private void fastRemove(Object[] es, int i) {
  modCount++;
  final int newSize;
  if ((newSize = size - 1) > i)
    System.arraycopy(es, i + 1, es, i, newSize - i);
  es[size = newSize] = null;
}

So, the counter is incremented each time when the collection is modified.

Btw, the fastRemove method is used in the remove method, which we use inside the loop.

Here's the small code fragment of the ArrayList iterator inner workings:

private class Itr implements Iterator<e> {
  ....
  int expectedModCount = modCount;            

  final void checkForComodification() {
  if (modCount != expectedModCount)               // 



<p>Let me explain that last fragment. If the collection modifications don't match the expected number of modifications (which is the sum of the initial modifications before the iterator was created and the number of the iterator operations), a <em>ConcurrentModificationException</em> is thrown. That's only possible when we modify the collection using its methods while iterating over it (i.e. <strong>in parallel</strong> with the iterator). That's what the second warning is about.</p>

<p>So, I've explained you the analyzer messages. Now let's put it all together: </p>

<p>We attempt to delete an element from the collection when the <em>Iterator</em> is still running:<br>
</p>

<pre class="brush:php;toolbar:false">topicNames.remove(topicIds.remove(topicName)); 
// topicsNames – Collection<string>
// topicsIds – Map<string uuid>
</string></string>

However, since the incompatible element is passed to ArrayList for deletion (the remove method returns a UUID object from topicIds), the modification count won't increase, but the object won't be deleted. Simply put, that code section is rudimentary.

I'd venture to guess that the developer's intent is clear. If that's the case, one way to fix these two warnings could be as follows:

Collection<string> topicNames = new ArrayList(topicNameCollection);

List<string> removableItems = new ArrayList();

for (final String topicName : topicNames) {
  KafkaFutureImpl<void> future = new KafkaFutureImpl();

  if (allTopics.remove(topicName) == null) {
    ....
  } else {
    topicIds.remove(topicName);
    removableItems.add(topicName);
    future.complete(null);
  }
  ....
}
topicNames.removeAll(removableItems);
</void></string></string>

Void, sweet void

Where would we go without our all-time favorite null and its potential problems, right? Let me show you the code fragment for which the analyzer issued the following warning:

V6008 Potential null dereference of 'oldMember' in function 'removeStaticMember'. ConsumerGroup.java 311, ConsumerGroup.java 323

@Override
public void removeMember(String memberId) {
  ConsumerGroupMember oldMember = members.remove(memberId);
  ....
  removeStaticMember(oldMember);
  ....
}

private void removeStaticMember(ConsumerGroupMember oldMember) {
  if (oldMember.instanceId() != null) {
    staticMembers.remove(oldMember.instanceId());
  }
}

If members doesn't contain an object with the memberId key, oldMember will be null. It can lead to a NullPointerException in the removeStaticMember method.

Boom! The parameter is checked for null:

if (oldMember != null && oldMember.instanceId() != null) {

The next error will be the last one in the article—I'd like to wrap things up on a positive note. The code below—as well as the one at the beginning of this article—has a common and silly typo. However, it can certainly lead to unpleasant consequences.

Let's take a look at this code fragment:

protected SchemaAndValue roundTrip(...., SchemaAndValue input) {
  String serialized = Values.convertToString(input.schema(),
                                             input.value());

  if (input != null && input.value() != null) {   
    ....
  }
  ....
}

Yeah, that's right. The method actually accesses the input object first, and then checks whether it's referencing null.

V6060 The 'input' reference was utilized before it was verified against null. ValuesTest.java 1212, ValuesTest.java 1213

Again, I'll note that such typos are ok. However, they can lead to some pretty nasty results. It's tough and inefficient to search for these things in the code manually.

Conclusion

In sum, I'd like to circle back to the previous point. Manually searching through the code for all these errors is a very time-consuming and tedious task. It's not unusual for issues like the ones I've shown to lurk in code for a long time. The last bug dates back to 2018. That's why it's a good idea to use static analysis tools. If you'd like to know more about PVS-Studio, the tool we have used to detect all those errors, you can find out more here.

That's all. Let's wrap things up here. "Oh, and in case I don't see ya, good afternoon, good evening, and good night."

Belay the Metamorphosis: analyzing Kafka project

I almost forgot! Catch a link to learn more about a free license for open-source projects.

Das obige ist der detaillierte Inhalt vonBelay the Metamorphosis: Analyse des Kafka-Projekts. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Wie benutze ich Maven oder Gradle für das fortschrittliche Java -Projektmanagement, die Erstellung von Automatisierung und Abhängigkeitslösung?Wie benutze ich Maven oder Gradle für das fortschrittliche Java -Projektmanagement, die Erstellung von Automatisierung und Abhängigkeitslösung?Mar 17, 2025 pm 05:46 PM

In dem Artikel werden Maven und Gradle für Java -Projektmanagement, Aufbau von Automatisierung und Abhängigkeitslösung erörtert, die ihre Ansätze und Optimierungsstrategien vergleichen.

Wie erstelle und verwende ich benutzerdefinierte Java -Bibliotheken (JAR -Dateien) mit ordnungsgemäßem Versioning und Abhängigkeitsmanagement?Wie erstelle und verwende ich benutzerdefinierte Java -Bibliotheken (JAR -Dateien) mit ordnungsgemäßem Versioning und Abhängigkeitsmanagement?Mar 17, 2025 pm 05:45 PM

In dem Artikel werden benutzerdefinierte Java -Bibliotheken (JAR -Dateien) mit ordnungsgemäßem Versioning- und Abhängigkeitsmanagement erstellt und verwendet, wobei Tools wie Maven und Gradle verwendet werden.

Wie implementiere ich mehrstufige Caching in Java-Anwendungen mit Bibliotheken wie Koffein oder Guava-Cache?Wie implementiere ich mehrstufige Caching in Java-Anwendungen mit Bibliotheken wie Koffein oder Guava-Cache?Mar 17, 2025 pm 05:44 PM

In dem Artikel wird in der Implementierung von mehrstufigem Caching in Java mithilfe von Koffein- und Guava-Cache zur Verbesserung der Anwendungsleistung erläutert. Es deckt die Einrichtungs-, Integrations- und Leistungsvorteile sowie die Bestrafung des Konfigurations- und Räumungsrichtlinienmanagements ab

Wie kann ich JPA (Java Persistence-API) für Objektrelationszuordnungen mit erweiterten Funktionen wie Caching und faulen Laden verwenden?Wie kann ich JPA (Java Persistence-API) für Objektrelationszuordnungen mit erweiterten Funktionen wie Caching und faulen Laden verwenden?Mar 17, 2025 pm 05:43 PM

In dem Artikel werden mit JPA für Objektrelationszuordnungen mit erweiterten Funktionen wie Caching und faulen Laden erläutert. Es deckt Setup, Entity -Mapping und Best Practices zur Optimierung der Leistung ab und hebt potenzielle Fallstricke hervor. [159 Charaktere]

Wie funktioniert der Klassenladungsmechanismus von Java, einschließlich verschiedener Klassenloader und deren Delegationsmodelle?Wie funktioniert der Klassenladungsmechanismus von Java, einschließlich verschiedener Klassenloader und deren Delegationsmodelle?Mar 17, 2025 pm 05:35 PM

Mit der Klassenbelastung von Java wird das Laden, Verknüpfen und Initialisieren von Klassen mithilfe eines hierarchischen Systems mit Bootstrap-, Erweiterungs- und Anwendungsklassenloadern umfasst. Das übergeordnete Delegationsmodell stellt sicher

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

MinGW – Minimalistisches GNU für Windows

MinGW – Minimalistisches GNU für Windows

Dieses Projekt wird derzeit auf osdn.net/projects/mingw migriert. Sie können uns dort weiterhin folgen. MinGW: Eine native Windows-Portierung der GNU Compiler Collection (GCC), frei verteilbare Importbibliotheken und Header-Dateien zum Erstellen nativer Windows-Anwendungen, einschließlich Erweiterungen der MSVC-Laufzeit zur Unterstützung der C99-Funktionalität. Die gesamte MinGW-Software kann auf 64-Bit-Windows-Plattformen ausgeführt werden.

SublimeText3 Linux neue Version

SublimeText3 Linux neue Version

SublimeText3 Linux neueste Version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) ist eine PHP/MySQL-Webanwendung, die sehr anfällig ist. Seine Hauptziele bestehen darin, Sicherheitsexperten dabei zu helfen, ihre Fähigkeiten und Tools in einem rechtlichen Umfeld zu testen, Webentwicklern dabei zu helfen, den Prozess der Sicherung von Webanwendungen besser zu verstehen, und Lehrern/Schülern dabei zu helfen, in einer Unterrichtsumgebung Webanwendungen zu lehren/lernen Sicherheit. Das Ziel von DVWA besteht darin, einige der häufigsten Web-Schwachstellen über eine einfache und unkomplizierte Benutzeroberfläche mit unterschiedlichen Schwierigkeitsgraden zu üben. Bitte beachten Sie, dass diese Software

Herunterladen der Mac-Version des Atom-Editors

Herunterladen der Mac-Version des Atom-Editors

Der beliebteste Open-Source-Editor

Sicherer Prüfungsbrowser

Sicherer Prüfungsbrowser

Safe Exam Browser ist eine sichere Browserumgebung für die sichere Teilnahme an Online-Prüfungen. Diese Software verwandelt jeden Computer in einen sicheren Arbeitsplatz. Es kontrolliert den Zugriff auf alle Dienstprogramme und verhindert, dass Schüler nicht autorisierte Ressourcen nutzen.