Apache Solr open-source search software
SOLR-4550: Create a stress test for CachingDirectoryFactory
Mark Robert Miller3da40d53df75
diff --git a/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.java b/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.javanew file mode 100644index 00000000000..a9287e600e2--- /dev/null+++ b/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.java@@ -0,0 +1,204 @@+package org.apache.solr.core;++import java.io.IOException;+import java.util.ArrayList;+import java.util.HashMap;+import java.util.List;+import java.util.Map;+import java.util.Random;+import java.util.concurrent.atomic.AtomicInteger;++import org.apache.lucene.store.Directory;+import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.core.DirectoryFactory.DirContext;+import org.junit.Test;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.+ */++public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {+ private Map<String,Tracker> dirs = new HashMap<String,Tracker>();+ private volatile boolean stop = false;++ private class Tracker {+ String path;+ AtomicInteger refCnt = new AtomicInteger(0);+ Directory dir;+ }++ @Test+ public void stressTest() throws Exception {+ final CachingDirectoryFactory df = new RAMDirectoryFactory();++ List<Thread> threads = new ArrayList<Thread>();+ int threadCount = 3;+ for (int i = 0; i < threadCount; i++) {+ Thread getDirThread = new GetDirThread(df);+ threads.add(getDirThread);+ getDirThread.start();+ }++ for (int i = 0; i < 2; i++) {+ Thread releaseDirThread = new ReleaseDirThread(df);+ threads.add(releaseDirThread);+ releaseDirThread.start();+ }++ for (int i = 0; i < 1; i++) {+ Thread incRefThread = new IncRefThread(df);+ threads.add(incRefThread);+ incRefThread.start();+ }++ Thread.sleep(TEST_NIGHTLY ? 30000 : 8000);++ stop = true;++ for (Thread thread : threads) {+ thread.join();+ }++ // do any remaining releases+ synchronized (dirs) {+ int sz = dirs.size();+ if (sz > 0) {+ for (Tracker tracker : dirs.values()) {+ int cnt = tracker.refCnt.get();+ for (int i = 0; i < cnt; i++) {+ tracker.refCnt.decrementAndGet();+ df.release(tracker.dir);+ }+ }+ }+ }++ df.close();+ }++ private class ReleaseDirThread extends Thread {+ Random random;+ private CachingDirectoryFactory df;++ public ReleaseDirThread(CachingDirectoryFactory df) {+ this.df = df;+ }++ @Override+ public void run() {+ random = random();+ while (!stop) {+ try {+ Thread.sleep(random.nextInt(50) + 1);+ } catch (InterruptedException e1) {+ throw new RuntimeException(e1);+ }+ try {+ synchronized (dirs) {+ int sz = dirs.size();+ List<Tracker> dirsList = new ArrayList<Tracker>();+ dirsList.addAll(dirs.values());+ if (sz > 0) {+ Tracker tracker = dirsList.get(Math.min(dirsList.size() - 1,+ random.nextInt(sz + 1)));+ if (tracker.refCnt.get() > 0) {+ if (random.nextBoolean()) {+ df.doneWithDirectory(tracker.dir);+ }+ tracker.refCnt.decrementAndGet();+ df.release(tracker.dir);+ }+ }+ }+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ }+ }++ private class GetDirThread extends Thread {+ Random random;+ private CachingDirectoryFactory df;++ public GetDirThread(CachingDirectoryFactory df) {+ this.df = df;+ }++ @Override+ public void run() {+ random = random();+ while (!stop) {+ try {+ Thread.sleep(random.nextInt(50) + 1);+ } catch (InterruptedException e1) {+ throw new RuntimeException(e1);+ }+ try {+ String path = "path" + random.nextInt(20);+ synchronized (dirs) {+ Tracker tracker = dirs.get(path);+ if (tracker == null) {+ tracker = new Tracker();+ tracker.path = path;+ tracker.dir = df.get(path, DirContext.DEFAULT, null);+ dirs.put(path, tracker);+ } else {+ tracker.dir = df.get(path, DirContext.DEFAULT, null);+ }+ tracker.refCnt.incrementAndGet();+ }++ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ }+ }++ private class IncRefThread extends Thread {+ Random random;+ private CachingDirectoryFactory df;++ public IncRefThread(CachingDirectoryFactory df) {+ this.df = df;+ }++ @Override+ public void run() {+ random = random();+ while (!stop) {+ try {+ Thread.sleep(random.nextInt(300) + 1);+ } catch (InterruptedException e1) {+ throw new RuntimeException(e1);+ }++ String path = "path" + random.nextInt(20);+ synchronized (dirs) {+ Tracker tracker = dirs.get(path);++ if (tracker != null && tracker.refCnt.get() > 0) {+ tracker.refCnt.incrementAndGet();+ df.incRef(tracker.dir);+ }+ }++ }+ }+ }++}