2 * ============LICENSE_START=======================================================
3 * feature-distributed-locking
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.distributed.locking;
22 import java.sql.Connection;
23 import java.sql.DriverManager;
24 import java.sql.PreparedStatement;
25 import java.sql.SQLException;
26 import java.util.UUID;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.Future;
29 import java.util.concurrent.ScheduledExecutorService;
30 import java.util.concurrent.TimeUnit;
32 import org.onap.policy.common.utils.properties.exception.PropertyException;
33 import org.onap.policy.drools.core.lock.LockRequestFuture;
34 import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureAPI;
35 import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
36 import org.onap.policy.drools.persistence.SystemPersistence;
37 import org.onap.policy.drools.system.PolicyEngine;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class DistributedLockingFeature implements PolicyEngineFeatureAPI, PolicyResourceLockFeatureAPI {
46 private static final Logger logger = LoggerFactory.getLogger(DistributedLockingFeature.class);
49 * Properties Configuration Name
51 public static final String CONFIGURATION_PROPERTIES_NAME = "feature-distributed-locking";
54 * Properties for locking feature
56 private DistributedLockingProperties lockProps;
59 *ScheduledExecutorService for LockHeartbeat
61 private ScheduledExecutorService scheduledExecutorService;
66 private static final UUID uuid = UUID.randomUUID();
72 public int getSequenceNumber() {
77 public Future<Boolean> beforeLock(String resourceId, String owner, Callback callback) {
79 TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps);
81 return new LockRequestFuture(resourceId, owner, tLock.lock());
86 public Boolean beforeUnlock(String resourceId, String owner) {
87 TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps);
89 return tLock.unlock();
93 public Boolean beforeIsLockedBy(String resourceId, String owner) {
94 TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps);
96 return tLock.isActive();
100 public Boolean beforeIsLocked(String resourceId) {
101 TargetLock tLock = new TargetLock(resourceId, this.uuid, "dummyOwner", lockProps);
103 return tLock.isLocked();
107 public boolean afterStart(PolicyEngine engine) {
110 this.lockProps = new DistributedLockingProperties(SystemPersistence.manager.getProperties(DistributedLockingFeature.CONFIGURATION_PROPERTIES_NAME));
111 } catch (PropertyException e) {
112 logger.error("DistributedLockingFeature feature properies have not been loaded", e);
113 throw new DistributedLockingFeatureException(e);
116 long heartbeatInterval = this.lockProps.getHeartBeatIntervalProperty();
119 Heartbeat heartbeat = new Heartbeat(this.uuid, lockProps);
121 this.scheduledExecutorService = Executors.newScheduledThreadPool(1);
122 this.scheduledExecutorService.scheduleAtFixedRate(heartbeat, heartbeatInterval, heartbeatInterval, TimeUnit.MILLISECONDS);
127 * This method kills the heartbeat thread and calls refreshLockTable which removes
128 * any records from the db where the current host is the owner.
131 public boolean beforeShutdown(PolicyEngine engine) {
132 scheduledExecutorService.shutdown();
138 * This method removes all records owned by the current host from the db.
140 private void cleanLockTable() {
142 try (Connection conn = DriverManager.getConnection(lockProps.getDbUrl(),
143 lockProps.getDbUser(),
144 lockProps.getDbPwd());
145 PreparedStatement statement = conn.prepareStatement("DELETE FROM pooling.locks WHERE host = ? OR expirationTime < ?");
148 statement.setString(1, this.uuid.toString());
149 statement.setLong(2, System.currentTimeMillis());
150 statement.executeUpdate();
152 } catch (SQLException e) {
153 logger.error("error in refreshLockTable()", e);