Skip to content

Commit

Permalink
Merge recent changes from trunk to the JCR-2936 branch
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/branches/JCR-2936@1169813 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
jukka committed Sep 12, 2011
2 parents eb1cce5 + 0682ef9 commit 5af3d1a
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 167 deletions.
@@ -0,0 +1,77 @@
/*
* 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.
*/
package org.apache.jackrabbit.core;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Thread pool used by the repository.
*/
class JackrabbitThreadPool extends ScheduledThreadPoolExecutor {

/**
* Size of the per-repository thread pool.
*/
private static final int size =
Runtime.getRuntime().availableProcessors() * 2;

/**
* The classloader used as the context classloader of threads in the pool.
*/
private static final ClassLoader loader =
JackrabbitThreadPool.class.getClassLoader();

/**
* Thread counter for generating unique names for the threads in the pool.
*/
private static final AtomicInteger counter = new AtomicInteger(1);

/**
* Thread factory for creating the threads in the pool
*/
private static final ThreadFactory factory = new ThreadFactory() {
public Thread newThread(Runnable runnable) {
int count = counter.getAndIncrement();
String name = "jackrabbit-pool-" + count;
Thread thread = new Thread(runnable, name);
thread.setDaemon(true);
if (thread.getPriority() != Thread.NORM_PRIORITY) {
thread.setPriority(Thread.NORM_PRIORITY);
}
thread.setContextClassLoader(loader);
return thread;
}
};

/**
* Handler for tasks for which no free thread is found within the pool.
*/
private static final RejectedExecutionHandler handler =
new ThreadPoolExecutor.CallerRunsPolicy();

/**
* Creates a new thread pool.
*/
public JackrabbitThreadPool() {
super(size, factory, handler);
}

}
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.jackrabbit.core;

import java.util.concurrent.ScheduledExecutorService;

import org.apache.jackrabbit.core.cluster.ClusterNode;
import org.apache.jackrabbit.core.data.DataStore;
import org.apache.jackrabbit.core.fs.FileSystem;
Expand All @@ -27,7 +29,6 @@
import org.apache.jackrabbit.core.state.ItemStateCacheFactory;
import org.apache.jackrabbit.core.stats.StatManager;
import org.apache.jackrabbit.core.version.InternalVersionManagerImpl;
import org.apache.jackrabbit.util.Timer;

/**
* Internal component context of a Jackrabbit content repository.
Expand Down Expand Up @@ -101,9 +102,10 @@ public class RepositoryContext {
private NodeIdFactory nodeIdFactory;

/**
* Repository-wide timer instance.
* Thread pool of this repository.
*/
private final Timer timer = new Timer(false);
private final ScheduledExecutorService executor =
new JackrabbitThreadPool();

/**
* The Statistics manager, handles statistics and jmx support
Expand All @@ -130,12 +132,12 @@ public RepositoryImpl getRepository() {
}

/**
* Returns the repository-wide timer instance.
* Returns the thread pool of this repository.
*
* @return repository timer
* @return repository thread pool
*/
public Timer getTimer() {
return timer;
public ScheduledExecutorService getExecutor() {
return executor;
}

/**
Expand Down
Expand Up @@ -32,11 +32,7 @@
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import javax.jcr.AccessDeniedException;
import javax.jcr.Credentials;
Expand Down Expand Up @@ -242,11 +238,6 @@ public class RepositoryImpl extends AbstractRepository
*/
private WorkspaceEventChannel createWorkspaceEventChannel;

/**
* Scheduled executor service.
*/
protected final ScheduledExecutorService executor;

/**
* Protected constructor.
*
Expand All @@ -256,32 +247,6 @@ public class RepositoryImpl extends AbstractRepository
* or another error occurs.
*/
protected RepositoryImpl(RepositoryConfig repConfig) throws RepositoryException {
// we should use the jackrabbit classloader for all background threads
// from the pool
final ClassLoader poolClassLoader = this.getClass().getClassLoader();
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
Runtime.getRuntime().availableProcessors() * 2,
new ThreadFactory() {

final AtomicInteger threadNumber = new AtomicInteger(1);

/**
* @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
*/
public Thread newThread(Runnable r) {
final Thread t = new Thread(null, r,
"jackrabbit-pool-" + threadNumber.getAndIncrement(),
0);
t.setDaemon(true);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
t.setContextClassLoader(poolClassLoader);
return t;
}
},
new ThreadPoolExecutor.CallerRunsPolicy());
this.executor = executor;

// Acquire a lock on the repository home
repLock = repConfig.getRepositoryLockMechanism();
repLock.init(repConfig.getHomeDir());
Expand Down Expand Up @@ -657,8 +622,7 @@ private SearchManager getSystemSearchManager(String wspName)
repConfig,
getWorkspaceInfo(wspName).itemStateMgr,
context.getInternalVersionManager().getPersistenceManager(),
SYSTEM_ROOT_NODE_ID,
null, null, executor);
SYSTEM_ROOT_NODE_ID, null, null);

SystemSession defSysSession = getSystemSession(wspName);
ObservationManager obsMgr = defSysSession.getWorkspace().getObservationManager();
Expand Down Expand Up @@ -1182,6 +1146,7 @@ protected synchronized void doShutdown() {
notifyAll();

// Shut down the executor service
ScheduledExecutorService executor = context.getExecutor();
executor.shutdown();
try {
// Wait for all remaining background threads to terminate
Expand All @@ -1204,8 +1169,6 @@ protected synchronized void doShutdown() {
}
}

context.getTimer().cancel();

statManager.stop();

log.info("Repository has been shutdown");
Expand Down Expand Up @@ -1914,7 +1877,7 @@ protected SearchManager getSearchManager() throws RepositoryException {
itemStateMgr, persistMgr,
context.getRootNodeId(),
getSystemSearchManager(getName()),
SYSTEM_ROOT_NODE_ID, executor);
SYSTEM_ROOT_NODE_ID);
}
return searchMgr;
}
Expand Down Expand Up @@ -1954,7 +1917,8 @@ protected LockManagerImpl getLockManager() throws RepositoryException {
* @return the lock manager
*/
protected LockManagerImpl createLockManager() throws RepositoryException {
return new LockManagerImpl(getSystemSession(), fs, executor);
return new LockManagerImpl(
getSystemSession(), fs, context.getExecutor());
}

/**
Expand Down
Expand Up @@ -130,12 +130,11 @@ public class SearchManager implements SynchronousEventListener {
public SearchManager(
RepositoryContext repositoryContext,
QueryHandlerFactory qhf,
SharedItemStateManager itemMgr,
PersistenceManager pm,
NodeId rootNodeId,
SearchManager parentMgr,
NodeId excludedNodeId,
Executor executor) throws RepositoryException {
SharedItemStateManager itemMgr,
PersistenceManager pm,
NodeId rootNodeId,
SearchManager parentMgr,
NodeId excludedNodeId) throws RepositoryException {
this.nsReg = repositoryContext.getNamespaceRegistry();
this.itemMgr = itemMgr;
this.parentHandler = (parentMgr != null) ? parentMgr.handler : null;
Expand Down Expand Up @@ -174,7 +173,7 @@ public SearchManager(
this.handler = qhf.getQueryHandler(new QueryHandlerContext(
repositoryContext,
itemMgr, pm, rootNodeId,
parentHandler, excludedNodeId, executor));
parentHandler, excludedNodeId));
}

/**
Expand Down

0 comments on commit 5af3d1a

Please sign in to comment.