code.onehippo.org is currently readonly. We are migrating to code.bloomreach.com, please continue working there on Monday 14/12. See: https://docs.bloomreach.com/display/engineering/GitLab

Commit b05161a4 authored by Ate Douma's avatar Ate Douma

REPO-1881 [Backport 11.2] Implement new LockManager service and replace all...

REPO-1881 [Backport 11.2] Implement new LockManager service and replace all deprecated HippoLock and HippoLockManager usages

This is a cherry-picked and squashed backport from REPO-1811, REPO-1873, REPO-1874 and REPO-1875:

  REPO-1811 Implement in-memory lock manager

    (cherry picked from commit dfa6f279)

  REPO-1811 To the RepositoryImpl add a Journal ConnectionHelper accessor

    Via this Journal ConnectionHelper we can access the Journal DataSource
    via the ConnectionHelperDataSourceAccessor.

    Admittedly, a bit clumsy but we need access to the Journal DataSource
    because we need to know whether we are dealing with a database *and*
    whether we have a clustered setup : Only in that case, the lock mechanism
    has to be upgraded from in memory locking to db base locking because
    needs to be cluster wide

    (cherry picked from commit 4750450f)

  REPO-1811 Support for a Database lock manager

    The Database lock manager interaction is pretty much exactly the same
    as the MemoryLockManager interaction. The biggest difference is that
    the DbLockManager creates database based locks and the MemoryLockManager
    creates MemoryLocks. Note that a DbLock is the same as the MemoryLock
    but only contains a #destroy implementation that releases the database
    lock.

    The DbLockManager is still work in progress

    (cherry picked from commit e0f19fab)

  REPO-1811 Fix the LockClusterTest setup

    (still ignored but the setup failed since we do not allow SNS
    below jcr:root any more)

    (cherry picked from commit 1dba0d49)

  REPO-1811 Support database locking

    (cherry picked from commit 92c914ef)

  REPO-1811 Support background jobs in the Memory or Db LockManager

    - All the background jobs are run by a single thread
    - By default they run every 5 seconds with an initial delay of 5 seconds
    - Every background job is wrapped in a 'synchronized runnable' the synchronized
      on the LockManager instance: The reason for this is that we do not want
      background jobs to write concurrently to possibly the same records as
      other (background) jobs
    - We have the following background jobs:
      1) UnlockStoppedThreadJanitor : releases database locks for records
         that were held by a thread that is not alive any more
      2) DbResetExpiredLocksJanitor : Resets all database rows which have
         expired locks (rows with status 'RUNNING' or 'ABORT' and expirationTime
         has passed
      3) DbLockRefresher: Refreshes the expiresTime of a lock to currentTime + refreshRateSeconds
      4) LockThreadInterrupter : Interrupts the Thread that holds the lock that has been marked 'ABORT'

    (cherry picked from commit cbf14aed)

  REPO-1811 Make sure getLocks and isLocked works cluster wide in case of a database

    (cherry picked from commit 441beff4)

  REPO-1811 Fix sql statement

    (cherry picked from commit d3d25849)

  REPO-1811 Extract abstract test and add an 'abort' test

    (cherry picked from commit 5ab1e55f)

  REPO-1811 use getHoldCount() instead of protected access

    (cherry picked from commit 7027fce3)

  REPO-1811 improve feedback

    (cherry picked from commit 0bf87cb5)

  REPO-1811 use getHoldCount() instead of protected access

    (cherry picked from commit a268b5e6)

  REPO-1811 Fix abort statement

    (cherry picked from commit dac8d9cc)

  REPO-1811 Encapsulate the localLocks object

    Since localLocks is a hashmap, all access to it should be synchronized.
    This can be easier achieved by encapsulating it and whenever needed by
    other classes, return a new object containing the same locks

    (cherry picked from commit cc3e10d9)

  REPO-1811 Enhance abort tests

    (cherry picked from commit 43f17029)

  REPO-1811 use auto closable in integration tests

    (cherry picked from commit 05143edd)

  REPO-1811 Correct the expiresTime parameter

    (cherry picked from commit 62bd68c2)

  REPO-1811 Locks in status ABORT should also be refreshed

    Namely if a lock is in status ABORT and the lock is for the *CLUSTER NODE*
    on which the DbLockRefresher runs, then it means that on this cluster
    node the Thread that should be aborted did not yet abort (for example
    still busy with its job). All we can do is wait until that job finishes

    (cherry picked from commit f038ec5b)

  REPO-1811 remove unneeded auto commit

    (cherry picked from commit 591ff099)

  REPO-1811 If a Thread is already interrupted, don't interrupt again

    (cherry picked from commit 8a6b58b3)

  REPO-1811 Integration tests that confirm the correct working of the DbLockRefresher

    (cherry picked from commit a3bc5d1f)

  REPO-1811 Tests to confirm lock expiration

    Integration tests that confirm the correct working of the
    DbResetExpiredLocksJanitor

    (cherry picked from commit c2284223)

  REPO-1811 only run expires db lock test in integration test

    (cherry picked from commit 79490e41)

  REPO-1811 Fix test to have flexible logger node test

    (cherry picked from commit 547542ef)

  REPO-1811 Improve exception handling and logging

    (cherry picked from commit 4fdb9278)

  REPO-1811 Improved destroy of the LockManager and other improvements

    (cherry picked from commit 589c19b8)

  REPO-1811 Remove the 'refreshRateSeconds' logic

    Just fixed 60 seconds refresh rate

    (cherry picked from commit b51f32d9)

  REPO-1811 Deprecate JCR (hippo) locking

    Note all usages of JCR Locking still need to be replaced

    (cherry picked from commit 04c17617)

  REPO-1811 Use the new LockManager instead of JCR Locking

    (cherry picked from commit 8ddbbc21)

  REPO-1811 Implement autocloseable LockResource and integration tests

    (cherry picked from commit ac0b839d)

  REPO-1811 Use auto closeable

    (cherry picked from commit 6938d49c)

  REPO-1811 Add a cleanup background job that removes rows from database of old locks

    (cherry picked from commit bb1f233b)

  REPO-1811 Implement new LockResource getters

    (cherry picked from commit e209d242)

  REPO-1811 Better tearDown

    Make sure tearDown with clearing the repository still removes
    also the database entries because if they are kept, consequent runs can fail

    (cherry picked from commit ffb7b9a6)

  REPO-1811 shorter keys

    (cherry picked from commit 87e1cb4b)

  REPO-1811 Support all databases and most specifically Oracle

    (cherry picked from commit 03072bf6)

  REPO-1811 Handle the interrupted exception gracefully

    When the lockmanager has an indication for 'abort' for the lock for the
    thread that is executing the UpdateExecutor, it means, that the current
    or a different cluster node has requested the task to be stopped. Invoking
    #cancel is the graceful stop of the job

    (cherry picked from commit 6354174b)

  REPO-1811 further refactoring and cleanup of the new (Db)LockManager

    - construction moved to LocalHippoRepository as well as further generalized
    - LockManagerFactory moved to DbLockManagerFactory and now only is responsible for constructing a DbLockManager
    - DbLockManager: added support for schemaObjectPrefix and schemaCheckEnabled, like all JR schema definitions
    - Added (handling for) new AlreadyLockedException, indicating a lock retry might be feasible
    - Merged DbHelper logic into DbLockManager (which ittself now gets passed into background Runnables) and use JR ConnectionHelper to check if table already exists
    - Added LockManagerUtilsTest, in test module, for services-api provided LockManagerUtils

    (cherry picked from commit 035c7ff9)

  REPO-1811 Improve the logging statement

    (cherry picked from commit 11f334ee)

  REPO-1811 Make sure the lockThread finishes before tearDown kicks in

    (cherry picked from commit f81fb33e)

  REPO-1811 license header

  (cherry picked from commit ab46d905)

  REPO-1811 fix unit tests

    (cherry picked from commit 84858ae4)

  REPO-1873 Support closing a LockResource by a different thread - implementation

    (cherry picked from commit 8e614323)

  REPO-1873 improve LocalHippoRepository instantiation extensibility

    (cherry picked from commit 79f365d3)

  REPO-1874 Leverage LockManager for the RepositoryScheduler replacing the deprecated HippoLock usage

    (cherry picked from commit 7e91d6c3)

  REPO-1875 add support for mssql (Microsoft SQL Server)

    Also added test configurations for mssql, postgresql and oracle in repository-test module, and updated/aligned the repository.xml and test/connection parameters for each of these.

    (cherry picked from commit 92213cda)

(cherry picked from commit fceda60165179a1aee27e095c7c649553debd36e)
parent 86c75b23
/*
* Copyright 2015 Hippo B.V. (http://www.onehippo.com)
* Copyright 2015-2017 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -20,6 +20,25 @@ import javax.jcr.RepositoryException;
import javax.jcr.lock.Lock;
import javax.jcr.lock.LockException;
/**
* JCR locking is deprecated, use {@link org.onehippo.cms7.services.lock.LockManager} instead. Creating a (cluster wide)
* lock with {@link org.onehippo.cms7.services.lock.LockManager} can be achieved as follows:
* <code>
* <pre>
* final LockManager lockManager = HippoServiceRegistry.getService(LockManager.class);
* try {
* lockManager.lock(key);
* // do locked work
* } catch (LockException e) {
* log.info("{} already locked", key);
* } finally {
* lockManager.unlock(key);
* }
* </pre>
* </code>
* @deprecated since 5.0.3
*/
@Deprecated
public interface HippoLock extends Lock {
/**
......
/*
* Copyright 2015 Hippo B.V. (http://www.onehippo.com)
* Copyright 2015-2017 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -18,6 +18,25 @@ package org.onehippo.repository.locking;
import javax.jcr.RepositoryException;
import javax.jcr.lock.LockManager;
/**
* JCR locking is deprecated, use {@link org.onehippo.cms7.services.lock.LockManager} instead. Creating a (cluster wide)
* lock with {@link org.onehippo.cms7.services.lock.LockManager} can be achieved as follows:
* <code>
* <pre>
* final LockManager lockManager = HippoServiceRegistry.getService(LockManager.class);
* try {
* lockManager.lock(key);
* // do locked work
* } catch (LockException e) {
* log.info("{} already locked", key);
* } finally {
* lockManager.unlock(key);
* }
* </pre>
* </code>
* @deprecated since 5.0.3
*/
@Deprecated
public interface HippoLockManager extends LockManager {
/**
......
/*
* Copyright 2015 Hippo B.V. (http://www.onehippo.com)
* Copyright 2015-2017 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -25,7 +25,10 @@ import javax.jcr.lock.Lock;
import javax.jcr.lock.LockException;
import javax.jcr.lock.LockManager;
/**
* @deprecated since 5.0.3
*/
@Deprecated
public class LockManagerDecorator implements LockManager {
protected final Session session;
......
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.apache.jackrabbit.core.journal;
import org.apache.jackrabbit.core.util.db.ConnectionHelper;
public class JournalConnectionHelperAccessorImpl {
public static ConnectionHelper getConnectionHelper(final DatabaseJournal journal)
{
return journal.conHelper;
}
}
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.apache.jackrabbit.core.util.db;
import javax.sql.DataSource;
public class ConnectionHelperDataSourceAccessor {
private ConnectionHelperDataSourceAccessor(){
}
public static DataSource getDataSource(final ConnectionHelper connectionHelper) {
return connectionHelper.dataSource;
}
}
/*
* Copyright 2008-2016 Hippo B.V. (http://www.onehippo.com)
* Copyright 2008-2017 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -36,10 +36,18 @@ import javax.jcr.SimpleCredentials;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.core.cluster.ClusterNode;
import org.apache.jackrabbit.core.config.RepositoryConfig;
import org.apache.jackrabbit.core.fs.FileSystem;
import org.apache.jackrabbit.core.fs.FileSystemException;
import org.apache.jackrabbit.core.journal.DatabaseJournal;
import org.apache.jackrabbit.core.journal.JournalConnectionHelperAccessorImpl;
import org.apache.jackrabbit.core.util.db.ConnectionHelper;
import org.hippoecm.repository.nodetypes.NodeTypesChangeTracker;
import org.onehippo.cms7.services.HippoServiceRegistry;
import org.onehippo.cms7.services.lock.LockManager;
import org.onehippo.cms7.services.lock.LockManagerUtils;
import org.onehippo.cms7.services.lock.LockResource;
import org.onehippo.repository.bootstrap.InitializationProcessor;
import org.onehippo.repository.bootstrap.PostStartupTask;
import org.hippoecm.repository.api.ReferenceWorkspace;
......@@ -49,7 +57,11 @@ import org.hippoecm.repository.impl.ReferenceWorkspaceImpl;
import org.hippoecm.repository.jackrabbit.RepositoryImpl;
import org.hippoecm.repository.security.HippoSecurityManager;
import org.hippoecm.repository.util.RepoUtils;
import org.onehippo.repository.bootstrap.util.BootstrapConstants;
import org.onehippo.repository.bootstrap.util.BootstrapUtils;
import org.onehippo.repository.lock.InternalLockManager;
import org.onehippo.repository.lock.db.DbLockManagerFactory;
import org.onehippo.repository.lock.memory.MemoryLockManager;
import org.onehippo.repository.modules.ModuleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -78,6 +90,12 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
/** Default config file */
public static final String DEFAULT_REPOSITORY_CONFIG = "repository.xml";
/** key to lock during repository initialization */
private static final String INIT_LOCK_KEY = BootstrapConstants.INIT_FOLDER_PATH;
/** wait interval for retrying to lock on {@link #INIT_LOCK_KEY} during repository initialization */
private static final long INIT_LOCK_ATTEMPT_INTERVAL = 500;
/** The advised threshold on the number of modified nodes to hold in transient session state */
public static int batchThreshold = 96;
......@@ -88,6 +106,8 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
private String repoPath;
private String repoConfig;
protected InternalLockManager lockManager;
private ModuleManager moduleManager;
private NodeTypesChangeTracker nodeTypesChangeTracker;
......@@ -199,7 +219,7 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
* @return InputStream to the repository config
* @throws RepositoryException
*/
private InputStream getRepositoryConfigAsStream() throws RepositoryException {
protected InputStream getRepositoryConfigAsStream() throws RepositoryException {
String configPath = repoConfig;
......@@ -237,14 +257,17 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
}
}
private class LocalRepositoryImpl extends RepositoryImpl {
LocalRepositoryImpl(RepositoryConfig repConfig) throws RepositoryException {
protected class LocalRepositoryImpl extends RepositoryImpl {
protected LocalRepositoryImpl(RepositoryConfig repConfig) throws RepositoryException {
super(repConfig);
}
@Override
public Session getRootSession(String workspaceName) throws RepositoryException {
return super.getRootSession(workspaceName);
}
void enableVirtualLayer(boolean enabled) throws RepositoryException {
isStarted = enabled;
}
......@@ -252,7 +275,23 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
protected FileSystem getFileSystem() {
return super.getFileSystem();
}
}
protected LocalRepositoryImpl createLocalRepository(final RepositoryConfig repConfig) throws RepositoryException {
return new LocalRepositoryImpl(repConfig);
}
protected void initializeLockManager() throws RepositoryException {
ClusterNode clusterNode = jackrabbitRepository.getClusterNode();
if (clusterNode != null && clusterNode.getJournal() instanceof DatabaseJournal) {
DatabaseJournal dbJournal = (DatabaseJournal) clusterNode.getJournal();
ConnectionHelper connectionHelper =
JournalConnectionHelperAccessorImpl.getConnectionHelper((DatabaseJournal) clusterNode.getJournal());
this.lockManager = DbLockManagerFactory.create(connectionHelper, dbJournal.getSchemaObjectPrefix(),
dbJournal.isSchemaCheckEnabled(), jackrabbitRepository.getDescriptor("jackrabbit.cluster.id"));
} else {
this.lockManager = new MemoryLockManager();
}
}
protected void initialize() throws RepositoryException {
......@@ -260,16 +299,17 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
Modules.setModules(new Modules(Thread.currentThread().getContextClassLoader()));
final RepositoryConfig repConfig = RepositoryConfig.create(getRepositoryConfigAsStream(), getRepositoryPath());
jackrabbitRepository = new LocalRepositoryImpl(repConfig);
jackrabbitRepository = createLocalRepository(createRepositoryConfig());
initializeLockManager();
HippoServiceRegistry.registerService(lockManager, new Class[]{LockManager.class, InternalLockManager.class});
repository = new DecoratorFactoryImpl().getRepositoryDecorator(jackrabbitRepository);
Session bootstrapSession = null, lockSession = null;
Session bootstrapSession = null;
final InitializationProcessorImpl initializationProcessor = new InitializationProcessorImpl();
boolean locked = false;
try {
final Session rootSession = jackrabbitRepository.getRootSession(null);
try (LockResource ignore = LockManagerUtils.waitForLock(lockManager, INIT_LOCK_KEY, INIT_LOCK_ATTEMPT_INTERVAL)) {
final Session rootSession = jackrabbitRepository.getRootSession(null);
ensureRootIsReferenceable(rootSession);
final boolean initializedBefore = initializedBefore(rootSession);
......@@ -278,7 +318,6 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
if (!initializedBefore || isContentBootstrapEnabled()) {
final SimpleCredentials credentials = new SimpleCredentials("system", new char[]{});
bootstrapSession = DecoratorFactoryImpl.getSessionDecorator(rootSession.impersonate(credentials), credentials);
lockSession = DecoratorFactoryImpl.getSessionDecorator(rootSession.impersonate(credentials), credentials);
initializeSystemNodeTypes(initializationProcessor, bootstrapSession, jackrabbitRepository.getFileSystem());
if (!bootstrapSession.nodeExists("/hippo:configuration")) {
log.debug("Initializing configuration content");
......@@ -287,8 +326,6 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
} else {
log.debug("Initial configuration content already present");
}
initializationProcessor.lock(lockSession);
locked = true;
postStartupTasks = contentBootstrap(initializationProcessor, bootstrapSession);
}
......@@ -306,19 +343,24 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
}
((HippoSecurityManager) jackrabbitRepository.getSecurityManager()).configure();
} finally {
if (lockSession != null) {
if (locked) {
initializationProcessor.unlock(lockSession);
}
lockSession.logout();
} catch (Exception e) {
if (e instanceof RepositoryException) {
throw (RepositoryException)e;
} else if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new RepositoryException(e);
} finally {
if (bootstrapSession != null) {
bootstrapSession.logout();
}
}
}
protected RepositoryConfig createRepositoryConfig() throws RepositoryException {
return RepositoryConfig.create(getRepositoryConfigAsStream(), getRepositoryPath());
}
private void ensureRootIsReferenceable(final Session rootSession) throws RepositoryException {
if(!rootSession.getRootNode().isNodeType(MIX_REFERENCEABLE)) {
rootSession.getRootNode().addMixin(MIX_REFERENCEABLE);
......@@ -429,6 +471,10 @@ public class LocalHippoRepository extends HippoRepositoryImpl {
nodeTypesChangeTracker.stop();
nodeTypesChangeTracker = null;
}
if (lockManager != null) {
HippoServiceRegistry.unregisterService(lockManager, LockManager.class);
lockManager.destroy();
}
if (jackrabbitRepository != null) {
try {
jackrabbitRepository.shutdown();
......
......@@ -37,6 +37,25 @@ import org.slf4j.LoggerFactory;
import static org.hippoecm.repository.api.HippoNodeType.HIPPO_LOCKEXPIRATIONTIME;
import static org.hippoecm.repository.api.HippoNodeType.NT_LOCKABLE;
/**
* JCR locking is deprecated, use {@link org.onehippo.cms7.services.lock.LockManager} instead. Creating a (cluster wide)
* lock with {@link org.onehippo.cms7.services.lock.LockManager} can be achieved as follows:
* <code>
* <pre>
* final LockManager lockManager = HippoServiceRegistry.getService(LockManager.class);
* try {
* lockManager.lock(key);
* // do locked work
* } catch (LockException e) {
* log.info("{} already locked", key);
* } finally {
* lockManager.unlock(key);
* }
* </pre>
* </code>
* @deprecated since 5.0.3
*/
@Deprecated
public class LockManagerDecorator extends org.hippoecm.repository.decorating.LockManagerDecorator implements HippoLockManager {
private static final Logger log = LoggerFactory.getLogger(LockManagerDecorator.class);
......@@ -108,6 +127,10 @@ public class LockManagerDecorator extends org.hippoecm.repository.decorating.Loc
lockNode.getSession().save();
}
/**
* Using jcr locking is deprecated, use {@link org.onehippo.cms7.services.lock.LockManager} instead
*/
@Deprecated
public class LockDecorator implements HippoLock {
private Lock lock;
......
......@@ -52,10 +52,12 @@ public class RepositorySchedulerImpl implements RepositoryScheduler {
private final Session session;
private final JCRScheduler scheduler;
private final String moduleConfigPath;
RepositorySchedulerImpl(Session session, JCRScheduler scheduler) {
RepositorySchedulerImpl(final Session session, final JCRScheduler scheduler, final String moduleConfigPath) {
this.session = session;
this.scheduler = scheduler;
this.moduleConfigPath = moduleConfigPath;
}
@Override
......@@ -153,7 +155,7 @@ public class RepositorySchedulerImpl implements RepositoryScheduler {
private Node newJobNode(final RepositoryJobInfo info) throws RepositoryException {
final String name = info.getName();
final String group = info.getGroup();
final Node moduleConfig = session.getNode(SchedulerModule.getModuleConfigPath());
final Node moduleConfig = session.getNode(moduleConfigPath);
final Node jobGroup;
if (moduleConfig.hasNode(group)) {
jobGroup = moduleConfig.getNode(group);
......@@ -168,7 +170,7 @@ public class RepositorySchedulerImpl implements RepositoryScheduler {
private Node getJobNode(final String jobName, final String groupName) throws RepositoryException {
synchronized (session) {
final Node moduleConfig = session.getNode(SchedulerModule.getModuleConfigPath());
final Node moduleConfig = session.getNode(moduleConfigPath);
final Node groupNode = JcrUtils.getNodeIfExists(moduleConfig, getGroupName(groupName));
if (groupNode != null) {
return JcrUtils.getNodeIfExists(groupNode, jobName);
......
/*
* Copyright 2008-2013 Hippo B.V. (http://www.onehippo.com)
* Copyright 2008-2017 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -51,8 +51,6 @@ public class SchedulerModule implements DaemonModule, ConfigurableDaemonModule {
SCHEDULER_FACTORY_PROPERTIES.put(JcrSchedulerFactory.PROP_JOB_STORE_CLASS, JCRJobStore.class.getName());
}
private static SchedulerModule instance;
private Session session;
private JCRScheduler scheduler = null;
private RepositoryScheduler service;
......@@ -69,7 +67,6 @@ public class SchedulerModule implements DaemonModule, ConfigurableDaemonModule {
@Override
public void initialize(Session session) throws RepositoryException {
instance = this;
this.session = session;
try {
final JcrSchedulerFactory schedFactory = new JcrSchedulerFactory(SCHEDULER_FACTORY_PROPERTIES);
......@@ -84,18 +81,10 @@ public class SchedulerModule implements DaemonModule, ConfigurableDaemonModule {
log.error("Failed to initialize quartz scheduler", e);
return;
}
service = new RepositorySchedulerImpl(session, scheduler);
service = new RepositorySchedulerImpl(session, scheduler, moduleConfigPath);
HippoServiceRegistry.registerService(service, RepositoryScheduler.class);
}
static Session getSession() {
return instance.session;
}
static String getModuleConfigPath() {
return instance.moduleConfigPath;
}
@Override
public void shutdown() {
if (service != null) {
......@@ -112,13 +101,14 @@ public class SchedulerModule implements DaemonModule, ConfigurableDaemonModule {
private static final String PROP_THREAD_POOL_THREADCOUNT = "org.quartz.threadPool.threadCount";
private static final String PROP_THREAD_POOL_THREADPRIORITY = "org.quartz.threadPool.threadPriority";
public JcrSchedulerFactory(Properties properties) throws SchedulerException {
public JcrSchedulerFactory(final Properties properties) throws SchedulerException {
super(properties);
}
@Override
protected Scheduler instantiate(QuartzSchedulerResources rcs, QuartzScheduler qs) {
try {
((JCRJobStore)rcs.getJobStore()).init(session, moduleConfigPath);
qs.getSchedulerContext().put(Session.class.getName(), session);
return new JCRScheduler(qs);
} catch (SchedulerException e) {
......
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.repository.lock;
import org.onehippo.cms7.services.lock.LockManager;
public interface InternalLockManager extends LockManager {
void destroy();
/**
* does similar logic as #destroy without closing background threads or marking the InternalLockManager as destroyed:
* This is useful / needed for integration tests which need to be able to validate certain behavior without destroying
* the InternalLockManager for real (since next test needs it again since repository is kept)
*/
void clear();
void addJob(final Runnable runnable, final long initialDelaySeconds, final long periodSeconds);
}
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.repository.lock;
import java.lang.ref.WeakReference;
import org.onehippo.cms7.services.lock.Lock;
public class MutableLock extends Lock {
private final WeakReference<Thread> thread;
private int holdCount;
public MutableLock(final String lockKey, final String lockOwner, final String lockThread,
final long creationTime, final String status) {
super(lockKey, lockOwner, lockThread, creationTime, status);
thread = new WeakReference<>(Thread.currentThread());
holdCount = 1;
}
public WeakReference<Thread> getThread() {
return thread;
}
public void increment() {
holdCount++;
}
public void decrement() {
holdCount--;
}
public int getHoldCount() {
return holdCount;
}
}
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.repository.lock.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Removes all locks that are free for longer than a day
*/
public class DbLockCleanupJanitor implements Runnable {
private static final Logger log = LoggerFactory.getLogger(DbLockCleanupJanitor.class);
private final DbLockManager dbLockManager;
public DbLockCleanupJanitor(final DbLockManager dbLockManager) {
this.dbLockManager = dbLockManager;
}
@Override
public void run() {
Connection connection = null;
boolean originalAutoCommit = false;
try {
connection = dbLockManager.getConnection();
originalAutoCommit = connection.getAutoCommit();
connection.setAutoCommit(true);
final PreparedStatement removeStatement = connection.prepareStatement(dbLockManager.getRemoveOutdatedStatement());
long dayAgoTime = System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
removeStatement.setLong(1, dayAgoTime);
int updated = removeStatement.executeUpdate();
log.info("Removed {} outdated locks", updated);
removeStatement.close();
} catch (SQLException e) {
log.error("Error while trying remove outdated locks", e);
} finally {
dbLockManager.close(connection, originalAutoCommit);
}
}
}
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.repository.lock.db;
import java.sql.Connection;
import java.sql.SQLException;
import javax.jcr.RepositoryException;
import javax.sql.DataSource;
import org.apache.jackrabbit.core.util.db.ConnectionHelper;
import org.apache.jackrabbit.core.util.db.ConnectionHelperDataSourceAccessor;
import org.apache.jackrabbit.core.util.db.OracleConnectionHelper;
public class DbLockManagerFactory {
public static DbLockManager create(final ConnectionHelper connectionHelper, final String schemaObjectPrefix,
final boolean schemaCheckEnabled, final String clusterNodeId) throws RepositoryException {
return create(ConnectionHelperDataSourceAccessor.getDataSource(connectionHelper), connectionHelper,