Skip to content

Commit 97e27fd

Browse files
authored
#2343 CollectionMetric: report the measurements as an ArrayList (#2344)
The measurements were reported as a LinkedList, which Storm does not register with Kryo, so a V1 metrics consumer in another worker could not receive them and the spout executor died.
1 parent 29c7bc1 commit 97e27fd

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎core/src/main/java/org/apache/stormcrawler/util/CollectionMetric.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,29 @@
1717

1818
package org.apache.stormcrawler.util;
1919

20-
import java.util.LinkedList;
20+
import java.util.ArrayList;
2121
import java.util.List;
2222
import org.apache.storm.metric.api.IMetric;
2323

2424
public class CollectionMetric implements IMetric {
2525

26-
private List<Long> measurements = new LinkedList<>();
26+
private List<Long> measurements = new ArrayList<>();
2727

2828
public void addMeasurement(long l) {
2929
synchronized (measurements) {
3030
measurements.add(l);
3131
}
3232
}
3333

34+
/**
35+
* Returns the measurements taken since the last call as an {@link ArrayList}: a V1 metrics
36+
* consumer in another worker gets them through Kryo, which Storm sets up for {@link ArrayList}
37+
* but not for other lists.
38+
*/
3439
@Override
3540
public Object getValueAndReset() {
3641
synchronized (measurements) {
37-
LinkedList<Long> copy = new LinkedList<>(measurements);
42+
ArrayList<Long> copy = new ArrayList<>(measurements);
3843
measurements.clear();
3944
return copy;
4045
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.stormcrawler.util;
19+
20+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import com.esotericsoftware.kryo.Kryo;
25+
import com.esotericsoftware.kryo.io.Output;
26+
import java.util.LinkedList;
27+
import java.util.List;
28+
import org.apache.storm.serialization.SerializationFactory;
29+
import org.apache.storm.utils.Utils;
30+
import org.junit.jupiter.api.Test;
31+
32+
class CollectionMetricTest {
33+
34+
/** Kryo as Storm sets it up for a topology that registers no classes of its own. */
35+
private static Kryo stormKryo() {
36+
return SerializationFactory.getKryo(Utils.readDefaultConfig());
37+
}
38+
39+
@Test
40+
void returnsTheMeasurementsSinceTheLastCall() {
41+
final CollectionMetric metric = new CollectionMetric();
42+
metric.addMeasurement(3);
43+
metric.addMeasurement(5);
44+
assertEquals(List.of(3L, 5L), metric.getValueAndReset());
45+
assertEquals(List.of(), metric.getValueAndReset());
46+
}
47+
48+
@Test
49+
void measurementsCanBeSentToAMetricsConsumerInAnotherWorker() {
50+
final CollectionMetric metric = new CollectionMetric();
51+
metric.addMeasurement(3);
52+
final Kryo kryo = stormKryo();
53+
assertDoesNotThrow(
54+
() -> kryo.writeClassAndObject(new Output(1024), metric.getValueAndReset()));
55+
}
56+
57+
@Test
58+
void linkedListCannotBeSent() {
59+
// why getValueAndReset() copies the measurements into an ArrayList
60+
final Kryo kryo = stormKryo();
61+
assertThrows(
62+
IllegalArgumentException.class,
63+
() -> kryo.writeClassAndObject(new Output(1024), new LinkedList<Long>()));
64+
}
65+
}

0 commit comments

Comments
 (0)