Skip to content

Commit

Permalink
fix(auto_balancer): fix potential memory leak (#1612) (#1613)
Browse files Browse the repository at this point in the history
Signed-off-by: Shichao Nie <niesc@automq.com>
  • Loading branch information
SCNieh committed Jul 22, 2024
1 parent a3a134c commit db8f384
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/main/java/kafka/autobalancer/model/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

public class Snapshot {
public static final double INVALID = -1;
private final Snapshot prev;
private final double[] values;
private final double latest;
private Snapshot prev;

public Snapshot(Snapshot prev, Collection<Double> values) {
this.prev = prev;
Expand Down Expand Up @@ -68,6 +68,10 @@ public Snapshot getPrev() {
return this.prev;
}

public void setPrev(Snapshot prev) {
this.prev = prev;
}

public int size() {
return this.values.length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public void append(double value) {
}

public Snapshot snapshot() {
if (this.prev != null) {
this.prev.setPrev(null);
}
Snapshot snapshot = new Snapshot(prev, values);
this.prev = snapshot;
return snapshot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ public void testAppendAndSnapshot() {
Assertions.assertEquals(1948, snapshot.get90thPercentile(), 1);
Assertions.assertEquals(1744, snapshot.getValue(0.5), 1);
}

@Test
public void testSnapshot() {
SnapshotSamples sequence = new SnapshotSamples(512);
for (int i = 0; i < 1000; i++) {
sequence.append(i);
}
for (int i = 0; i < 100; i++) {
sequence.snapshot();
}
Snapshot snapshot = sequence.snapshot();
Assertions.assertNotNull(snapshot);
Assertions.assertNotNull(snapshot.getPrev());
Assertions.assertNull(snapshot.getPrev().getPrev());
}
}

0 comments on commit db8f384

Please sign in to comment.