mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
Fix QueueRecycler.
Double-release protection added in 1c758b0b made QueueRecycler throw NPEs when trying to recycle existing instances.
This commit is contained in:
parent
a04d18d2d2
commit
f3c1a885fb
@ -28,13 +28,13 @@ import java.util.Queue;
|
|||||||
*/
|
*/
|
||||||
public class QueueRecycler<T> extends Recycler<T> {
|
public class QueueRecycler<T> extends Recycler<T> {
|
||||||
|
|
||||||
final Queue<V<T>> queue;
|
final Queue<T> queue;
|
||||||
|
|
||||||
public QueueRecycler(C<T> c) {
|
public QueueRecycler(C<T> c) {
|
||||||
this(c, ConcurrentCollections.<V<T>>newQueue());
|
this(c, ConcurrentCollections.<T>newQueue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueueRecycler(C<T> c, Queue<V<T>> queue) {
|
public QueueRecycler(C<T> c, Queue<T> queue) {
|
||||||
super(c);
|
super(c);
|
||||||
this.queue = queue;
|
this.queue = queue;
|
||||||
}
|
}
|
||||||
@ -46,11 +46,11 @@ public class QueueRecycler<T> extends Recycler<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V<T> obtain(int sizing) {
|
public V<T> obtain(int sizing) {
|
||||||
V<T> v = queue.poll();
|
T v = queue.poll();
|
||||||
if (v == null) {
|
if (v == null) {
|
||||||
v = new QV(c.newInstance(sizing));
|
v = c.newInstance(sizing);
|
||||||
}
|
}
|
||||||
return v;
|
return new QV(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
class QV implements Recycler.V<T> {
|
class QV implements Recycler.V<T> {
|
||||||
@ -77,7 +77,7 @@ public class QueueRecycler<T> extends Recycler<T> {
|
|||||||
throw new ElasticSearchIllegalStateException("recycler entry already released...");
|
throw new ElasticSearchIllegalStateException("recycler entry already released...");
|
||||||
}
|
}
|
||||||
c.clear(value);
|
c.clear(value);
|
||||||
queue.offer(this);
|
queue.offer(value);
|
||||||
value = null;
|
value = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to ElasticSearch and Shay Banon under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. ElasticSearch licenses this
|
||||||
|
* file to you 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 org.elasticsearch.common.recycler;
|
||||||
|
|
||||||
|
import org.elasticsearch.ElasticSearchIllegalStateException;
|
||||||
|
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public abstract class AbstractRecyclerTests extends ElasticsearchTestCase {
|
||||||
|
|
||||||
|
protected static final Recycler.C<byte[]> RECYCLER_C = new Recycler.C<byte[]>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] newInstance(int sizing) {
|
||||||
|
return new byte[10];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear(byte[] value) {
|
||||||
|
Arrays.fill(value, (byte) 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
protected abstract Recycler<byte[]> newRecycler();
|
||||||
|
|
||||||
|
public void testReuse() {
|
||||||
|
Recycler<byte[]> r = newRecycler();
|
||||||
|
Recycler.V<byte[]> o = r.obtain();
|
||||||
|
final byte[] b1 = o.v();
|
||||||
|
o.release();
|
||||||
|
o = r.obtain();
|
||||||
|
final byte[] b2 = o.v();
|
||||||
|
if (o.isRecycled()) {
|
||||||
|
assertSame(b1, b2);
|
||||||
|
}
|
||||||
|
o.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testClear() {
|
||||||
|
Recycler<byte[]> r = newRecycler();
|
||||||
|
Recycler.V<byte[]> o = r.obtain();
|
||||||
|
getRandom().nextBytes(o.v());
|
||||||
|
o.release();
|
||||||
|
o = r.obtain();
|
||||||
|
for (int i = 0; i < o.v().length; ++i) {
|
||||||
|
assertEquals(0, o.v()[i]);
|
||||||
|
}
|
||||||
|
o.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDoubleRelease() {
|
||||||
|
final Recycler<byte[]> r = newRecycler();
|
||||||
|
final Recycler.V<byte[]> v1 = r.obtain();
|
||||||
|
v1.release();
|
||||||
|
try {
|
||||||
|
v1.release();
|
||||||
|
} catch (ElasticSearchIllegalStateException e) {
|
||||||
|
// impl has protection against double release: ok
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// otherwise ensure that the impl may not be returned twice
|
||||||
|
final Recycler.V<byte[]> v2 = r.obtain();
|
||||||
|
final Recycler.V<byte[]> v3 = r.obtain();
|
||||||
|
assertNotSame(v2.v(), v3.v());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to ElasticSearch and Shay Banon under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. ElasticSearch licenses this
|
||||||
|
* file to you 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 org.elasticsearch.common.recycler;
|
||||||
|
|
||||||
|
public class NoneRecyclerTests extends AbstractRecyclerTests {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Recycler<byte[]> newRecycler() {
|
||||||
|
return new NoneRecycler<byte[]>(RECYCLER_C);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to ElasticSearch and Shay Banon under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. ElasticSearch licenses this
|
||||||
|
* file to you 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 org.elasticsearch.common.recycler;
|
||||||
|
|
||||||
|
public class QueueRecyclerTests extends AbstractRecyclerTests {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Recycler<byte[]> newRecycler() {
|
||||||
|
return new QueueRecycler<byte[]>(RECYCLER_C);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to ElasticSearch and Shay Banon under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. ElasticSearch licenses this
|
||||||
|
* file to you 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 org.elasticsearch.common.recycler;
|
||||||
|
|
||||||
|
public class SoftThreadLocalRecyclerTests extends AbstractRecyclerTests {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Recycler<byte[]> newRecycler() {
|
||||||
|
return new SoftThreadLocalRecycler<byte[]>(RECYCLER_C, randomIntBetween(5, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to ElasticSearch and Shay Banon under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. ElasticSearch licenses this
|
||||||
|
* file to you 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 org.elasticsearch.common.recycler;
|
||||||
|
|
||||||
|
public class ThreadLocalRecyclerTests extends AbstractRecyclerTests {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Recycler<byte[]> newRecycler() {
|
||||||
|
return new ThreadLocalRecycler<byte[]>(RECYCLER_C, randomIntBetween(5, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user