Merge pull request #1343 from metamx/fix-npe

fix NPE
This commit is contained in:
Fangjin Yang 2015-05-05 11:02:44 -07:00
commit 7c4054aaa3
3 changed files with 61 additions and 10 deletions

View File

@ -101,9 +101,7 @@ public class AggregateTopNMetricFirstAlgorithm implements TopNAlgorithm<int[], T
dimValSelector = getDimValSelectorForTopNMetric(singleMetricParam, singleMetricResultBuilder);
}
finally {
if (singleMetricParam != null) {
singleMetricAlgo.cleanup(singleMetricParam);
}
singleMetricAlgo.cleanup(singleMetricParam);
}
PooledTopNAlgorithm allMetricAlgo = new PooledTopNAlgorithm(capabilities, query, bufferPool);
@ -118,9 +116,7 @@ public class AggregateTopNMetricFirstAlgorithm implements TopNAlgorithm<int[], T
);
}
finally {
if (allMetricsParam != null) {
allMetricAlgo.cleanup(allMetricsParam);
}
allMetricAlgo.cleanup(allMetricsParam);
}
}

View File

@ -311,12 +311,14 @@ public class PooledTopNAlgorithm
@Override
public void cleanup(PooledTopNParams params)
{
ResourceHolder<ByteBuffer> resultsBufHolder = params.getResultsBufHolder();
if (params != null) {
ResourceHolder<ByteBuffer> resultsBufHolder = params.getResultsBufHolder();
if (resultsBufHolder != null) {
resultsBufHolder.get().clear();
if (resultsBufHolder != null) {
resultsBufHolder.get().clear();
}
CloseQuietly.close(resultsBufHolder);
}
CloseQuietly.close(resultsBufHolder);
}
public static class PooledTopNParams extends TopNParams

View File

@ -0,0 +1,53 @@
/*
* Druid - a distributed column store.
* Copyright 2012 - 2015 Metamarkets Group Inc.
*
* Licensed 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.
*/
package io.druid.query.topn;
import io.druid.collections.ResourceHolder;
import io.druid.segment.Capabilities;
import org.easymock.EasyMock;
import org.junit.Test;
import java.io.IOException;
import java.nio.ByteBuffer;
public class PooledTopNAlgorithmTest
{
@Test
public void testCleanupWithNullParams()
{
PooledTopNAlgorithm pooledTopNAlgorithm = new PooledTopNAlgorithm(Capabilities.builder().build(), null, null);
pooledTopNAlgorithm.cleanup(null);
}
@Test
public void cleanup() throws IOException
{
PooledTopNAlgorithm pooledTopNAlgorithm = new PooledTopNAlgorithm(Capabilities.builder().build(), null, null);
PooledTopNAlgorithm.PooledTopNParams params = EasyMock.createMock(PooledTopNAlgorithm.PooledTopNParams.class);
ResourceHolder<ByteBuffer> resourceHolder = EasyMock.createMock(ResourceHolder.class);
EasyMock.expect(params.getResultsBufHolder()).andReturn(resourceHolder).times(1);
EasyMock.expect(resourceHolder.get()).andReturn(ByteBuffer.allocate(1)).times(1);
resourceHolder.close();
EasyMock.expectLastCall().once();
EasyMock.replay(params);
EasyMock.replay(resourceHolder);
pooledTopNAlgorithm.cleanup(params);
EasyMock.verify(params);
EasyMock.verify(resourceHolder);
}
}